Skip to content

Commit 4b4fbc1

Browse files
authored
Refactor AliasOrIndex abstraction. (#54394)
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
1 parent 00eaa0e commit 4b4fbc1

File tree

41 files changed

+345
-290
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

+345
-290
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;
@@ -79,9 +79,9 @@ public RolloverResult rolloverClusterState(ClusterState currentState, String ali
7979
boolean silent) throws Exception {
8080
final MetaData metaData = currentState.metaData();
8181
validate(metaData, aliasName);
82-
final AliasOrIndex.Alias alias = (AliasOrIndex.Alias) metaData.getAliasAndIndexLookup().get(aliasName);
82+
final IndexAbstraction alias = metaData.getIndicesLookup().get(aliasName);
8383
final IndexMetaData indexMetaData = alias.getWriteIndex();
84-
final AliasMetaData aliasMetaData = indexMetaData.getAliases().get(alias.getAliasName());
84+
final AliasMetaData aliasMetaData = indexMetaData.getAliases().get(alias.getName());
8585
final String sourceProvidedName = indexMetaData.getSettings().get(IndexMetaData.SETTING_INDEX_PROVIDED_NAME,
8686
indexMetaData.getIndex().getName());
8787
final String sourceIndexName = indexMetaData.getIndex().getName();
@@ -176,16 +176,16 @@ static void checkNoDuplicatedAliasInIndexTemplate(MetaData metaData, String roll
176176
}
177177

178178
static void validate(MetaData metaData, String aliasName) {
179-
final AliasOrIndex aliasOrIndex = metaData.getAliasAndIndexLookup().get(aliasName);
180-
if (aliasOrIndex == null) {
179+
final IndexAbstraction indexAbstraction = metaData.getIndicesLookup().get(aliasName);
180+
if (indexAbstraction == null) {
181181
throw new IllegalArgumentException("source alias does not exist");
182182
}
183-
if (aliasOrIndex.isAlias() == false) {
184-
throw new IllegalArgumentException("source alias is a concrete index");
183+
if (indexAbstraction.getType() != IndexAbstraction.Type.ALIAS) {
184+
throw new IllegalArgumentException("source alias is a [" + indexAbstraction.getType().getDisplayName() +
185+
"], but an [" + IndexAbstraction.Type.ALIAS.getDisplayName() + "] was expected");
185186
}
186-
final AliasOrIndex.Alias alias = (AliasOrIndex.Alias) aliasOrIndex;
187-
if (alias.getWriteIndex() == null) {
188-
throw new IllegalArgumentException("source alias [" + alias.getAliasName() + "] does not point to a write index");
187+
if (indexAbstraction.getWriteIndex() == null) {
188+
throw new IllegalArgumentException("source alias [" + indexAbstraction.getName() + "] does not point to a write index");
189189
}
190190
}
191191
}

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;
@@ -285,18 +285,16 @@ static boolean resolvePipelines(final DocWriteRequest<?> originalRequest, final
285285
IndexMetaData indexMetaData = metaData.indices().get(originalRequest.index());
286286
// check the alias for the index request (this is how normal index requests are modeled)
287287
if (indexMetaData == null && indexRequest.index() != null) {
288-
AliasOrIndex indexOrAlias = metaData.getAliasAndIndexLookup().get(indexRequest.index());
289-
if (indexOrAlias != null && indexOrAlias.isAlias()) {
290-
AliasOrIndex.Alias alias = (AliasOrIndex.Alias) indexOrAlias;
291-
indexMetaData = alias.getWriteIndex();
288+
IndexAbstraction indexAbstraction = metaData.getIndicesLookup().get(indexRequest.index());
289+
if (indexAbstraction != null) {
290+
indexMetaData = indexAbstraction.getWriteIndex();
292291
}
293292
}
294293
// check the alias for the action request (this is how upserts are modeled)
295294
if (indexMetaData == null && originalRequest.index() != null) {
296-
AliasOrIndex indexOrAlias = metaData.getAliasAndIndexLookup().get(originalRequest.index());
297-
if (indexOrAlias != null && indexOrAlias.isAlias()) {
298-
AliasOrIndex.Alias alias = (AliasOrIndex.Alias) indexOrAlias;
299-
indexMetaData = alias.getWriteIndex();
295+
IndexAbstraction indexAbstraction = metaData.getIndicesLookup().get(originalRequest.index());
296+
if (indexAbstraction != null) {
297+
indexMetaData = indexAbstraction.getWriteIndex();
300298
}
301299
}
302300
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

+72-16
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;
@@ -35,30 +34,77 @@
3534
import static org.elasticsearch.cluster.metadata.IndexMetaData.INDEX_HIDDEN_SETTING;
3635

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

4248
/**
43-
* @return whether this an alias or concrete index
49+
* @return the name of the index abstraction
4450
*/
45-
boolean isAlias();
51+
String getName();
4652

4753
/**
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}
54+
* @return All {@link IndexMetaData} of all concrete indices this index abstraction is referring to.
5055
*/
5156
List<IndexMetaData> getIndices();
5257

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

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

63109
private final IndexMetaData concreteIndex;
64110

@@ -67,15 +113,25 @@ public Index(IndexMetaData indexMetaData) {
67113
}
68114

69115
@Override
70-
public boolean isAlias() {
71-
return false;
116+
public String getName() {
117+
return concreteIndex.getIndex().getName();
118+
}
119+
120+
@Override
121+
public Type getType() {
122+
return Type.CONCRETE_INDEX;
72123
}
73124

74125
@Override
75126
public List<IndexMetaData> getIndices() {
76127
return Collections.singletonList(concreteIndex);
77128
}
78129

130+
@Override
131+
public IndexMetaData getWriteIndex() {
132+
return concreteIndex;
133+
}
134+
79135
@Override
80136
public boolean isHidden() {
81137
return INDEX_HIDDEN_SETTING.get(concreteIndex.getSettings());
@@ -85,7 +141,7 @@ public boolean isHidden() {
85141
/**
86142
* Represents an alias and groups all {@link IndexMetaData} instances sharing the same alias name together.
87143
*/
88-
class Alias implements AliasOrIndex {
144+
class Alias implements IndexAbstraction {
89145

90146
private final String aliasName;
91147
private final List<IndexMetaData> referenceIndexMetaDatas;
@@ -100,11 +156,11 @@ public Alias(AliasMetaData aliasMetaData, IndexMetaData indexMetaData) {
100156
}
101157

102158
@Override
103-
public boolean isAlias() {
104-
return true;
159+
public Type getType() {
160+
return Type.ALIAS;
105161
}
106162

107-
public String getAliasName() {
163+
public String getName() {
108164
return aliasName;
109165
}
110166

@@ -131,7 +187,7 @@ public boolean isHidden() {
131187
* and filters)
132188
*/
133189
public Iterable<Tuple<String, AliasMetaData>> getConcreteIndexAndAliasMetaDatas() {
134-
return () -> new Iterator<Tuple<String,AliasMetaData>>() {
190+
return () -> new Iterator<Tuple<String, AliasMetaData>>() {
135191

136192
int index = 0;
137193

0 commit comments

Comments
 (0)