Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AliasOrIndex abstraction. #53982

Merged
merged 18 commits into from
Mar 30, 2020

Conversation

martijnvg
Copy link
Member

@martijnvg martijnvg commented Mar 23, 2020

In order to prepare the AliasOrIndex abstraction for the introduction of data streams,
the abstraction needs to be made more flexible, because currently it really can be only
an alias or an index.

The following changes were made:

  • Renamed AliasOrIndex to IndexAbstraction.
  • Introduced a IndexSpace.Type enum to indicate what a IndexSpace instance is.
  • Replaced the isAlias() method that returns a boolean with the getType() method that returns the new Type enum.
  • Moved getWriteIndex() up from the IndexSpace.Alias to the IndexSpace interface.
  • Moved getAliasName() up from the IndexSpace.Alias to the IndexSpace interface and renamed it to getName().
  • Removed unnecessary casting to IndexSpace.Alias by just checking the getType() method.

Relates to #53100

In order to prepare the `AliasOrIndex` abstraction for the introduction of data streams,
the abstraction needs to be made more flexible, because currently it really can be only
an alias or an index.

* Introduced a `AliasOrIndex.Type` enum to indicate what a `AliasOrIndex` instance is.
* Replaced the `isAlias()`` method that returns a boolean with the `getType()`` method that returns the new Type enum.
* Moved `getWriteIndex()`` up from the `AliasOrIndex.Alias` to the `AliasOrIndex` interface.
* Moved `g`etAliasName()`` up from the `AliasOrIndex.Alias` to the `AliasOrIndex` interface and renamed it to `getName()``.
* Removed unnecessary casting to `AliasOrIndex.Alias` by just checking the `getType()`` method.

Finally `AliasOrIndex` should be renamed to reflect that it can be more than just an index or alias, since
in the near future it can also be a data stream. The name AliasOrIndexOrDataStream is not appealing to me.
We can rename it to `Namespace`, but that sounds to generic to me. `ResolvedIndicesExpression` sounds better
to me, since it reflects more what it is (an expression from api that has been resolved to alias/index/datasteam),
but the name itself is a bit on the long side.

Relates to elastic#53100
@martijnvg martijnvg added >non-issue :Data Management/Data streams Data streams and their lifecycles labels Mar 23, 2020
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-features (:Core/Features/Data streams)

@martijnvg martijnvg added :Data Management/Indices APIs APIs to create and manage indices and templates v7.7.0 v8.0.0 and removed :Data Management/Data streams Data streams and their lifecycles labels Mar 23, 2020
@martijnvg martijnvg marked this pull request as ready for review March 25, 2020 08:02
@bpintea bpintea added v7.8.0 and removed v7.7.0 labels Mar 25, 2020
throw new IllegalArgumentException("source alias does not exist");
}
if (aliasOrIndex.isAlias() == false) {
if (indexSpace.getType() != IndexSpace.Type.ALIAS) {
throw new IllegalArgumentException("source alias is a concrete index");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a lot of error messages such as this one that assume only alias or index types would benefit from wording changes that accommodate data streams, too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I will go over these checks and messages and adjust.

if (indexOrAlias != null && indexOrAlias.isAlias()) {
AliasOrIndex.Alias alias = (AliasOrIndex.Alias) indexOrAlias;
indexMetaData = alias.getWriteIndex();
IndexSpace indexOrAlias = metaData.getAliasAndIndexLookup().get(indexRequest.index());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll probably want to rename that variable from indexOrAlias. Can be done in a later PR, though.

* @return whether this alias/index is hidden or not
* A write index is a dedicated concrete index, that accepts all the new documents that belong to an index space.
*
* A write index may also be a regular concrete index of a index space and may be therefor also be returned
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "may be therefor also be returned" -> "may therefore also be returned"

@@ -14,7 +14,7 @@
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.AliasOrIndex;
import org.elasticsearch.cluster.metadata.IndexSpace;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we want to eventually rename this class to something like IndexSpaceResolver?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so.

Copy link
Contributor

@danhermann danhermann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I left a few comments that are minor, not actionable, and/or addressable in later PRs.

renamed `MetaData#aliasAndIndexLookup` field (and getter) to `MetaData#indexSpaceLookup`.
@henningandersen
Copy link
Contributor

I wonder if we should use AbstractIndex instead of IndexSpace? It could be good to come up with a terminology for alias/index/datastream that we would consider using in documentation too.

@danhermann
Copy link
Contributor

I wonder if we should use AbstractIndex instead of IndexSpace? It could be good to come up with a terminology for alias/index/datastream that we would consider using in documentation too.

We considered a bunch of different options including AliasOrIndexOrDataStream (not really), Namespace, ResolvedIndexExpression, and IndexFamily and landed on IndexSpace as the best of them. I agree that it would be good to have one that works well in documentation, too. Naming things is hard.

@martijnvg
Copy link
Member Author

Dan and I did came up with the following alternative: IndexAbstraction.

AbstractIndex is very 'programming' term and could indicate to someone reading that name, that it isn't concrete. Whereas IndexAbstraction reads as an abstraction of an index, which represents something and which could be a concrete index, an alias or a data stream.

Copy link
Contributor

@henningandersen henningandersen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

I left a few smaller comments to consider.

@@ -130,8 +130,8 @@ public void testAuthorizeClusterAction() {

public void testAuthorizeIndexAction() {
CustomAuthorizationEngine engine = new CustomAuthorizationEngine();
Map<String, AliasOrIndex> aliasOrIndexMap = new HashMap<>();
aliasOrIndexMap.put("index", new Index(IndexMetaData.builder("index")
Map<String, IndexAbstraction> indexSpaceMap = new HashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove "Space" from variable name?

@@ -341,7 +341,7 @@ public void testRolloverClusterState() throws Exception {
IndexMetaData rolloverIndexMetaData = rolloverMetaData.index(newIndexName);
assertThat(rolloverIndexMetaData.getNumberOfShards(), equalTo(numberOfShards));

AliasOrIndex.Alias alias = (AliasOrIndex.Alias) rolloverMetaData.getAliasAndIndexLookup().get(aliasName);
IndexAbstraction alias = rolloverMetaData.getIndicesLookup().get(aliasName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: also verify type (since we no longer do the cast).

@@ -212,7 +212,7 @@ public void testValidation() {
assertThat(exception.getMessage(), equalTo("source alias [" + aliasWithNoWriteIndex + "] does not point to a write index"));
exception = expectThrows(IllegalArgumentException.class, () ->
MetaDataRolloverService.validate(metaData, randomFrom(index1, index2)));
assertThat(exception.getMessage(), equalTo("source alias is a concrete index"));
assertThat(exception.getMessage(), equalTo("source alias is not an alias"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the change to the exception message is not really a big issue. But with data streams also appearing, it could be nice to stick to the old message since that would give you some information on whether it is a datastream or concrete index. I think we could easily generate such a message by adding a display name ("concrete index") to the IndexAbstract.Type enum?

Also, this does make me wonder if Type.INDEX should be Type.CONCRETE_INDEX for consistency with that display name? Not truly important to me, feel free to ignore this part.

} else if (aliasInfo.isAlias() == false) {
logger.warn("service provider index [{}] exists as a concrete index, but it should be an alias", ALIAS_NAME);
} else if (aliasInfo.getType() != IndexAbstraction.Type.ALIAS) {
logger.warn("service provider index [{}] does not exist as an alias, but it should be", ALIAS_NAME);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment on retaining the original message here.

@@ -83,8 +83,8 @@ public void setUpTests() {
ClusterSettings clusterSettings = new ClusterSettings(nodeSettings(), new HashSet<>(Collections.singletonList(
MlConfigMigrationEligibilityCheck.ENABLE_CONFIG_MIGRATION)));
MetaData metaData = mock(MetaData.class);
SortedMap<String, AliasOrIndex> aliasOrIndexSortedMap = new TreeMap<>();
when(metaData.getAliasAndIndexLookup()).thenReturn(aliasOrIndexSortedMap);
SortedMap<String, IndexAbstraction> indexSpaceSortedMap = new TreeMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove "Space" from variable name?

} else if (aliasOrIndex.isAlias()) {
throw new IllegalStateException("concrete index [" + concreteIndexName + "] is an alias but should not be");
} else if (indexAbstraction.getType() != IndexAbstraction.Type.INDEX) {
throw new IllegalStateException("concrete index [" + concreteIndexName + "] is not an index but should be");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also include the display name of the actual type here.

@martijnvg martijnvg merged commit b7af852 into elastic:master Mar 30, 2020
martijnvg added a commit to martijnvg/elasticsearch that referenced this pull request Mar 30, 2020
In order to prepare the `AliasOrIndex` abstraction for the introduction of data streams,
the abstraction needs to be made more flexible, because currently it really can be only
an alias or an index.

* Renamed `AliasOrIndex` to `IndexAbstraction`.
* Introduced a `IndexAbstraction.Type` enum to indicate what a `IndexAbstraction` instance is.
* Replaced the `isAlias()` method that returns a boolean with the `getType()` method that returns the new Type enum.
* Moved `getWriteIndex()` up from the `IndexAbstraction.Alias` to the `IndexAbstraction` interface.
* Moved `getAliasName()` up from the `IndexAbstraction.Alias` to the `IndexAbstraction` interface and renamed it to `getName()`.
* Removed unnecessary casting to `IndexAbstraction.Alias` by just checking the `getType()` method.

Relates to elastic#53100
martijnvg added a commit that referenced this pull request Mar 30, 2020
Backport of #53982

In order to prepare the `AliasOrIndex` abstraction for the introduction of data streams,
the abstraction needs to be made more flexible, because currently it really can be only
an alias or an index.

* Renamed `AliasOrIndex` to `IndexAbstraction`.
* Introduced a `IndexAbstraction.Type` enum to indicate what a `IndexAbstraction` instance is.
* Replaced the `isAlias()` method that returns a boolean with the `getType()` method that returns the new Type enum.
* Moved `getWriteIndex()` up from the `IndexAbstraction.Alias` to the `IndexAbstraction` interface.
* Moved `getAliasName()` up from the `IndexAbstraction.Alias` to the `IndexAbstraction` interface and renamed it to `getName()`.
* Removed unnecessary casting to `IndexAbstraction.Alias` by just checking the `getType()` method.

Relates to #53100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Data Management/Indices APIs APIs to create and manage indices and templates >non-issue v7.8.0 v8.0.0-alpha1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants