Skip to content

Commit b7af852

Browse files
authored
Refactor AliasOrIndex abstraction. (#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
1 parent 6ff4476 commit b7af852

File tree

41 files changed

+346
-292
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+346
-292
lines changed

plugins/examples/security-authorization-engine/src/main/java/org/elasticsearch/example/CustomAuthorizationEngine.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package org.elasticsearch.example;
2121

2222
import org.elasticsearch.action.ActionListener;
23-
import org.elasticsearch.cluster.metadata.AliasOrIndex;
23+
import org.elasticsearch.cluster.metadata.IndexAbstraction;
2424
import org.elasticsearch.xpack.core.security.action.user.GetUserPrivilegesRequest;
2525
import org.elasticsearch.xpack.core.security.action.user.GetUserPrivilegesResponse;
2626
import org.elasticsearch.xpack.core.security.action.user.GetUserPrivilegesResponse.Indices;
@@ -90,7 +90,7 @@ public void authorizeClusterAction(RequestInfo requestInfo, AuthorizationInfo au
9090
@Override
9191
public void authorizeIndexAction(RequestInfo requestInfo, AuthorizationInfo authorizationInfo,
9292
AsyncSupplier<ResolvedIndices> indicesAsyncSupplier,
93-
Map<String, AliasOrIndex> aliasOrIndexLookup,
93+
Map<String, IndexAbstraction> aliasOrIndexLookup,
9494
ActionListener<IndexAuthorizationResult> listener) {
9595
if (isSuperuser(requestInfo.getAuthentication().getUser())) {
9696
indicesAsyncSupplier.getAsync(ActionListener.wrap(resolvedIndices -> {
@@ -109,9 +109,9 @@ public void authorizeIndexAction(RequestInfo requestInfo, AuthorizationInfo auth
109109

110110
@Override
111111
public void loadAuthorizedIndices(RequestInfo requestInfo, AuthorizationInfo authorizationInfo,
112-
Map<String, AliasOrIndex> aliasOrIndexLookup, ActionListener<List<String>> listener) {
112+
Map<String, IndexAbstraction> indicesLookup, ActionListener<List<String>> listener) {
113113
if (isSuperuser(requestInfo.getAuthentication().getUser())) {
114-
listener.onResponse(new ArrayList<>(aliasOrIndexLookup.keySet()));
114+
listener.onResponse(new ArrayList<>(indicesLookup.keySet()));
115115
} else {
116116
listener.onResponse(Collections.emptyList());
117117
}

plugins/examples/security-authorization-engine/src/test/java/org/elasticsearch/example/CustomAuthorizationEngineTests.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import org.elasticsearch.Version;
2323
import org.elasticsearch.action.search.SearchRequest;
2424
import org.elasticsearch.action.support.PlainActionFuture;
25-
import org.elasticsearch.cluster.metadata.AliasOrIndex;
26-
import org.elasticsearch.cluster.metadata.AliasOrIndex.Index;
25+
import org.elasticsearch.cluster.metadata.IndexAbstraction;
26+
import org.elasticsearch.cluster.metadata.IndexAbstraction.Index;
2727
import org.elasticsearch.cluster.metadata.IndexMetaData;
2828
import org.elasticsearch.common.settings.Settings;
2929
import org.elasticsearch.test.ESTestCase;
@@ -130,8 +130,8 @@ public void testAuthorizeClusterAction() {
130130

131131
public void testAuthorizeIndexAction() {
132132
CustomAuthorizationEngine engine = new CustomAuthorizationEngine();
133-
Map<String, AliasOrIndex> aliasOrIndexMap = new HashMap<>();
134-
aliasOrIndexMap.put("index", new Index(IndexMetaData.builder("index")
133+
Map<String, IndexAbstraction> indicesMap = new HashMap<>();
134+
indicesMap.put("index", new Index(IndexMetaData.builder("index")
135135
.settings(Settings.builder().put("index.version.created", Version.CURRENT))
136136
.numberOfShards(1)
137137
.numberOfReplicas(0)
@@ -148,7 +148,7 @@ public void testAuthorizeIndexAction() {
148148
PlainActionFuture<IndexAuthorizationResult> resultFuture = new PlainActionFuture<>();
149149
engine.authorizeIndexAction(requestInfo, authzInfo,
150150
listener -> listener.onResponse(new ResolvedIndices(Collections.singletonList("index"), Collections.emptyList())),
151-
aliasOrIndexMap, resultFuture);
151+
indicesMap, resultFuture);
152152
IndexAuthorizationResult result = resultFuture.actionGet();
153153
assertThat(result.isGranted(), is(true));
154154
assertThat(result.isAuditable(), is(true));
@@ -169,7 +169,7 @@ public void testAuthorizeIndexAction() {
169169
PlainActionFuture<IndexAuthorizationResult> resultFuture = new PlainActionFuture<>();
170170
engine.authorizeIndexAction(requestInfo, authzInfo,
171171
listener -> listener.onResponse(new ResolvedIndices(Collections.singletonList("index"), Collections.emptyList())),
172-
aliasOrIndexMap, resultFuture);
172+
indicesMap, resultFuture);
173173
IndexAuthorizationResult result = resultFuture.actionGet();
174174
assertThat(result.isGranted(), is(false));
175175
assertThat(result.isAuditable(), is(true));

server/src/main/java/org/elasticsearch/action/admin/indices/rollover/MetaDataRolloverService.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
import org.elasticsearch.cluster.ClusterState;
2626
import org.elasticsearch.cluster.metadata.AliasAction;
2727
import org.elasticsearch.cluster.metadata.AliasMetaData;
28-
import org.elasticsearch.cluster.metadata.AliasOrIndex;
2928
import org.elasticsearch.cluster.metadata.IndexMetaData;
3029
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
30+
import org.elasticsearch.cluster.metadata.IndexAbstraction;
3131
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
3232
import org.elasticsearch.cluster.metadata.MetaData;
3333
import org.elasticsearch.cluster.metadata.MetaDataCreateIndexService;
@@ -77,9 +77,9 @@ public RolloverResult rolloverClusterState(ClusterState currentState, String ali
7777
boolean silent) throws Exception {
7878
final MetaData metaData = currentState.metaData();
7979
validate(metaData, aliasName);
80-
final AliasOrIndex.Alias alias = (AliasOrIndex.Alias) metaData.getAliasAndIndexLookup().get(aliasName);
80+
final IndexAbstraction alias = metaData.getIndicesLookup().get(aliasName);
8181
final IndexMetaData indexMetaData = alias.getWriteIndex();
82-
final AliasMetaData aliasMetaData = indexMetaData.getAliases().get(alias.getAliasName());
82+
final AliasMetaData aliasMetaData = indexMetaData.getAliases().get(alias.getName());
8383
final String sourceProvidedName = indexMetaData.getSettings().get(IndexMetaData.SETTING_INDEX_PROVIDED_NAME,
8484
indexMetaData.getIndex().getName());
8585
final String sourceIndexName = indexMetaData.getIndex().getName();
@@ -174,16 +174,16 @@ static void checkNoDuplicatedAliasInIndexTemplate(MetaData metaData, String roll
174174
}
175175

176176
static void validate(MetaData metaData, String aliasName) {
177-
final AliasOrIndex aliasOrIndex = metaData.getAliasAndIndexLookup().get(aliasName);
178-
if (aliasOrIndex == null) {
177+
final IndexAbstraction indexAbstraction = metaData.getIndicesLookup().get(aliasName);
178+
if (indexAbstraction == null) {
179179
throw new IllegalArgumentException("source alias does not exist");
180180
}
181-
if (aliasOrIndex.isAlias() == false) {
182-
throw new IllegalArgumentException("source alias is a concrete index");
181+
if (indexAbstraction.getType() != IndexAbstraction.Type.ALIAS) {
182+
throw new IllegalArgumentException("source alias is a [" + indexAbstraction.getType().getDisplayName() +
183+
"], but an [" + IndexAbstraction.Type.ALIAS.getDisplayName() + "] was expected");
183184
}
184-
final AliasOrIndex.Alias alias = (AliasOrIndex.Alias) aliasOrIndex;
185-
if (alias.getWriteIndex() == null) {
186-
throw new IllegalArgumentException("source alias [" + alias.getAliasName() + "] does not point to a write index");
185+
if (indexAbstraction.getWriteIndex() == null) {
186+
throw new IllegalArgumentException("source alias [" + indexAbstraction.getName() + "] does not point to a write index");
187187
}
188188
}
189189
}

server/src/main/java/org/elasticsearch/action/bulk/TransportBulkAction.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
import org.elasticsearch.cluster.ClusterStateObserver;
4949
import org.elasticsearch.cluster.block.ClusterBlockException;
5050
import org.elasticsearch.cluster.block.ClusterBlockLevel;
51-
import org.elasticsearch.cluster.metadata.AliasOrIndex;
51+
import org.elasticsearch.cluster.metadata.IndexAbstraction;
5252
import org.elasticsearch.cluster.metadata.IndexMetaData;
5353
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
5454
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
@@ -281,18 +281,16 @@ static boolean resolvePipelines(final DocWriteRequest<?> originalRequest, final
281281
IndexMetaData indexMetaData = metaData.indices().get(originalRequest.index());
282282
// check the alias for the index request (this is how normal index requests are modeled)
283283
if (indexMetaData == null && indexRequest.index() != null) {
284-
AliasOrIndex indexOrAlias = metaData.getAliasAndIndexLookup().get(indexRequest.index());
285-
if (indexOrAlias != null && indexOrAlias.isAlias()) {
286-
AliasOrIndex.Alias alias = (AliasOrIndex.Alias) indexOrAlias;
287-
indexMetaData = alias.getWriteIndex();
284+
IndexAbstraction indexAbstraction = metaData.getIndicesLookup().get(indexRequest.index());
285+
if (indexAbstraction != null) {
286+
indexMetaData = indexAbstraction.getWriteIndex();
288287
}
289288
}
290289
// check the alias for the action request (this is how upserts are modeled)
291290
if (indexMetaData == null && originalRequest.index() != null) {
292-
AliasOrIndex indexOrAlias = metaData.getAliasAndIndexLookup().get(originalRequest.index());
293-
if (indexOrAlias != null && indexOrAlias.isAlias()) {
294-
AliasOrIndex.Alias alias = (AliasOrIndex.Alias) indexOrAlias;
295-
indexMetaData = alias.getWriteIndex();
291+
IndexAbstraction indexAbstraction = metaData.getIndicesLookup().get(originalRequest.index());
292+
if (indexAbstraction != null) {
293+
indexMetaData = indexAbstraction.getWriteIndex();
296294
}
297295
}
298296
if (indexMetaData != null) {

server/src/main/java/org/elasticsearch/cluster/metadata/AliasOrIndex.java renamed to server/src/main/java/org/elasticsearch/cluster/metadata/IndexAbstraction.java

+73-18
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
2019
package org.elasticsearch.cluster.metadata;
2120

2221
import org.apache.lucene.util.SetOnce;
@@ -25,7 +24,6 @@
2524
import org.elasticsearch.common.collect.Tuple;
2625

2726
import java.util.ArrayList;
28-
import java.util.Collections;
2927
import java.util.Iterator;
3028
import java.util.List;
3129
import java.util.Map;
@@ -35,30 +33,77 @@
3533
import static org.elasticsearch.cluster.metadata.IndexMetaData.INDEX_HIDDEN_SETTING;
3634

3735
/**
38-
* Encapsulates the {@link IndexMetaData} instances of a concrete index or indices an alias is pointing to.
36+
* An index abstraction is a reference to one or more concrete indices.
37+
* An index abstraction has a unique name and encapsulates all the {@link IndexMetaData} instances it is pointing to.
38+
* Also depending on type it may refer to a single or many concrete indices and may or may not have a write index.
3939
*/
40-
public interface AliasOrIndex {
40+
public interface IndexAbstraction {
41+
42+
/**
43+
* @return the type of the index abstraction
44+
*/
45+
Type getType();
4146

4247
/**
43-
* @return whether this an alias or concrete index
48+
* @return the name of the index abstraction
4449
*/
45-
boolean isAlias();
50+
String getName();
4651

4752
/**
48-
* @return All {@link IndexMetaData} of all concrete indices this alias is referring to
49-
* or if this is a concrete index its {@link IndexMetaData}
53+
* @return All {@link IndexMetaData} of all concrete indices this index abstraction is referring to.
5054
*/
5155
List<IndexMetaData> getIndices();
5256

5357
/**
54-
* @return whether this alias/index is hidden or not
58+
* A write index is a dedicated concrete index, that accepts all the new documents that belong to an index abstraction.
59+
*
60+
* A write index may also be a regular concrete index of a index abstraction and may therefore also be returned
61+
* by {@link #getIndices()}. An index abstraction may also not have a dedicated write index.
62+
*
63+
* @return the write index of this index abstraction or
64+
* <code>null</code> if this index abstraction doesn't have a write index.
65+
*/
66+
@Nullable
67+
IndexMetaData getWriteIndex();
68+
69+
/**
70+
* @return whether this index abstraction is hidden or not
5571
*/
5672
boolean isHidden();
5773

74+
/**
75+
* An index abstraction type.
76+
*/
77+
enum Type {
78+
79+
/**
80+
* An index abstraction that refers to a single concrete index.
81+
* This concrete index is also the write index.
82+
*/
83+
CONCRETE_INDEX("concrete index"),
84+
85+
/**
86+
* An index abstraction that refers to an alias.
87+
* An alias typically refers to many concrete indices and
88+
* may have a write index.
89+
*/
90+
ALIAS("alias");
91+
92+
private final String displayName;
93+
94+
Type(String displayName) {
95+
this.displayName = displayName;
96+
}
97+
98+
public String getDisplayName() {
99+
return displayName;
100+
}
101+
}
102+
58103
/**
59104
* Represents an concrete index and encapsulates its {@link IndexMetaData}
60105
*/
61-
class Index implements AliasOrIndex {
106+
class Index implements IndexAbstraction {
62107

63108
private final IndexMetaData concreteIndex;
64109

@@ -67,13 +112,23 @@ public Index(IndexMetaData indexMetaData) {
67112
}
68113

69114
@Override
70-
public boolean isAlias() {
71-
return false;
115+
public String getName() {
116+
return concreteIndex.getIndex().getName();
117+
}
118+
119+
@Override
120+
public Type getType() {
121+
return Type.CONCRETE_INDEX;
72122
}
73123

74124
@Override
75125
public List<IndexMetaData> getIndices() {
76-
return Collections.singletonList(concreteIndex);
126+
return List.of(concreteIndex);
127+
}
128+
129+
@Override
130+
public IndexMetaData getWriteIndex() {
131+
return concreteIndex;
77132
}
78133

79134
@Override
@@ -85,7 +140,7 @@ public boolean isHidden() {
85140
/**
86141
* Represents an alias and groups all {@link IndexMetaData} instances sharing the same alias name together.
87142
*/
88-
class Alias implements AliasOrIndex {
143+
class Alias implements IndexAbstraction {
89144

90145
private final String aliasName;
91146
private final List<IndexMetaData> referenceIndexMetaDatas;
@@ -100,11 +155,11 @@ public Alias(AliasMetaData aliasMetaData, IndexMetaData indexMetaData) {
100155
}
101156

102157
@Override
103-
public boolean isAlias() {
104-
return true;
158+
public Type getType() {
159+
return Type.ALIAS;
105160
}
106161

107-
public String getAliasName() {
162+
public String getName() {
108163
return aliasName;
109164
}
110165

@@ -131,7 +186,7 @@ public boolean isHidden() {
131186
* and filters)
132187
*/
133188
public Iterable<Tuple<String, AliasMetaData>> getConcreteIndexAndAliasMetaDatas() {
134-
return () -> new Iterator<Tuple<String,AliasMetaData>>() {
189+
return () -> new Iterator<>() {
135190

136191
int index = 0;
137192

0 commit comments

Comments
 (0)