Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
albertzaharovits committed Sep 3, 2024
1 parent fdddde5 commit a94a6ae
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,21 @@ public MetadataCreateIndexService(
/**
* Validate the name for an index against some static rules and a cluster state.
*/
public static void validateIndexName(String index, ClusterState state) {
public static void validateIndexName(String index, Metadata metadata, RoutingTable routingTable) {
validateIndexOrAliasName(index, InvalidIndexNameException::new);
if (index.toLowerCase(Locale.ROOT).equals(index) == false) {
throw new InvalidIndexNameException(index, "must be lowercase");
}

// NOTE: dot-prefixed index names are validated after template application, not here

if (state.routingTable().hasIndex(index)) {
throw new ResourceAlreadyExistsException(state.routingTable().index(index).getIndex());
if (routingTable.hasIndex(index)) {
throw new ResourceAlreadyExistsException(routingTable.index(index).getIndex());
}
if (state.metadata().hasIndex(index)) {
throw new ResourceAlreadyExistsException(state.metadata().index(index).getIndex());
if (metadata.hasIndex(index)) {
throw new ResourceAlreadyExistsException(metadata.index(index).getIndex());
}
if (state.metadata().hasAlias(index)) {
if (metadata.hasAlias(index)) {
throw new InvalidIndexNameException(index, "already exists as alias");
}
}
Expand Down Expand Up @@ -344,7 +344,7 @@ public ClusterState applyCreateIndexRequest(
normalizeRequestSetting(request);
logger.trace("executing IndexCreationTask for [{}] against cluster state version [{}]", request, currentState.version());

validate(request, currentState);
validate(request, currentState.metadata(), currentState.routingTable());

final Index recoverFromIndex = request.recoverFrom();
final IndexMetadata sourceMetadata = recoverFromIndex == null ? null : currentState.metadata().getIndexSafe(recoverFromIndex);
Expand Down Expand Up @@ -1069,7 +1069,9 @@ static Settings aggregateIndexSettings(
if (sourceMetadata != null) {
assert request.resizeType() != null;
prepareResizeIndexSettings(
currentState,
currentState.metadata(),
currentState.blocks(),
currentState.routingTable(),
indexSettingsBuilder,
request.recoverFrom(),
request.index(),
Expand All @@ -1084,7 +1086,7 @@ static Settings aggregateIndexSettings(
* We can not validate settings until we have applied templates, otherwise we do not know the actual settings
* that will be used to create this index.
*/
shardLimitValidator.validateShardLimit(indexSettings, currentState);
shardLimitValidator.validateShardLimit(indexSettings, currentState.nodes(), currentState.metadata());
validateSoftDeleteSettings(indexSettings);
validateTranslogRetentionSettings(indexSettings);
validateStoreTypeSetting(indexSettings);
Expand Down Expand Up @@ -1363,8 +1365,8 @@ private static void validateActiveShardCount(ActiveShardCount waitForActiveShard
}
}

private void validate(CreateIndexClusterStateUpdateRequest request, ClusterState state) {
validateIndexName(request.index(), state);
private void validate(CreateIndexClusterStateUpdateRequest request, Metadata metadata, RoutingTable routingTable) {
validateIndexName(request.index(), metadata, routingTable);
validateIndexSettings(request.index(), request.settings(), forbidPrivateIndexSettings);
}

Expand Down Expand Up @@ -1428,8 +1430,15 @@ private static List<String> validateIndexCustomPath(Settings settings, @Nullable
*
* @return the list of nodes at least one instance of the source index shards are allocated
*/
static List<String> validateShrinkIndex(ClusterState state, String sourceIndex, String targetIndexName, Settings targetIndexSettings) {
IndexMetadata sourceMetadata = validateResize(state, sourceIndex, targetIndexName, targetIndexSettings);
static List<String> validateShrinkIndex(
Metadata metadata,
ClusterBlocks clusterBlocks,
RoutingTable routingTable,
String sourceIndex,
String targetIndexName,
Settings targetIndexSettings
) {
IndexMetadata sourceMetadata = validateResize(metadata, clusterBlocks, sourceIndex, targetIndexName, targetIndexSettings);
if (sourceMetadata.isSearchableSnapshot()) {
throw new IllegalArgumentException("can't shrink searchable snapshot index [" + sourceIndex + ']');
}
Expand All @@ -1441,7 +1450,7 @@ static List<String> validateShrinkIndex(ClusterState state, String sourceIndex,
}

// now check that index is all on one node
final IndexRoutingTable table = state.routingTable().index(sourceIndex);
final IndexRoutingTable table = routingTable.index(sourceIndex);
Map<String, AtomicInteger> nodesToNumRouting = new HashMap<>();
int numShards = sourceMetadata.getNumberOfShards();
for (ShardRouting routing : table.shardsWithState(ShardRoutingState.STARTED)) {
Expand All @@ -1461,16 +1470,28 @@ static List<String> validateShrinkIndex(ClusterState state, String sourceIndex,
return nodesToAllocateOn;
}

static void validateSplitIndex(ClusterState state, String sourceIndex, String targetIndexName, Settings targetIndexSettings) {
IndexMetadata sourceMetadata = validateResize(state, sourceIndex, targetIndexName, targetIndexSettings);
static void validateSplitIndex(
Metadata metadata,
ClusterBlocks clusterBlocks,
String sourceIndex,
String targetIndexName,
Settings targetIndexSettings
) {
IndexMetadata sourceMetadata = validateResize(metadata, clusterBlocks, sourceIndex, targetIndexName, targetIndexSettings);
if (sourceMetadata.isSearchableSnapshot()) {
throw new IllegalArgumentException("can't split searchable snapshot index [" + sourceIndex + ']');
}
IndexMetadata.selectSplitShard(0, sourceMetadata, INDEX_NUMBER_OF_SHARDS_SETTING.get(targetIndexSettings));
}

static void validateCloneIndex(ClusterState state, String sourceIndex, String targetIndexName, Settings targetIndexSettings) {
IndexMetadata sourceMetadata = validateResize(state, sourceIndex, targetIndexName, targetIndexSettings);
static void validateCloneIndex(
Metadata metadata,
ClusterBlocks clusterBlocks,
String sourceIndex,
String targetIndexName,
Settings targetIndexSettings
) {
IndexMetadata sourceMetadata = validateResize(metadata, clusterBlocks, sourceIndex, targetIndexName, targetIndexSettings);
if (sourceMetadata.isSearchableSnapshot()) {
for (Setting<?> nonCloneableSetting : Arrays.asList(INDEX_STORE_TYPE_SETTING, INDEX_RECOVERY_TYPE_SETTING)) {
if (nonCloneableSetting.exists(targetIndexSettings) == false) {
Expand All @@ -1487,16 +1508,22 @@ static void validateCloneIndex(ClusterState state, String sourceIndex, String ta
IndexMetadata.selectCloneShard(0, sourceMetadata, INDEX_NUMBER_OF_SHARDS_SETTING.get(targetIndexSettings));
}

static IndexMetadata validateResize(ClusterState state, String sourceIndex, String targetIndexName, Settings targetIndexSettings) {
if (state.metadata().hasIndex(targetIndexName)) {
throw new ResourceAlreadyExistsException(state.metadata().index(targetIndexName).getIndex());
static IndexMetadata validateResize(
Metadata metadata,
ClusterBlocks clusterBlocks,
String sourceIndex,
String targetIndexName,
Settings targetIndexSettings
) {
if (metadata.hasIndex(targetIndexName)) {
throw new ResourceAlreadyExistsException(metadata.index(targetIndexName).getIndex());
}
final IndexMetadata sourceMetadata = state.metadata().index(sourceIndex);
final IndexMetadata sourceMetadata = metadata.index(sourceIndex);
if (sourceMetadata == null) {
throw new IndexNotFoundException(sourceIndex);
}

IndexAbstraction source = state.metadata().getIndicesLookup().get(sourceIndex);
IndexAbstraction source = metadata.getIndicesLookup().get(sourceIndex);
assert source != null;
if (source.getParentDataStream() != null && source.getParentDataStream().getWriteIndex().equals(sourceMetadata.getIndex())) {
throw new IllegalArgumentException(
Expand All @@ -1509,7 +1536,7 @@ static IndexMetadata validateResize(ClusterState state, String sourceIndex, Stri
);
}
// ensure index is read-only
if (state.blocks().indexBlocked(ClusterBlockLevel.WRITE, sourceIndex) == false) {
if (clusterBlocks.indexBlocked(ClusterBlockLevel.WRITE, sourceIndex) == false) {
throw new IllegalStateException("index " + sourceIndex + " must be read-only to resize index. use \"index.blocks.write=true\"");
}

Expand All @@ -1522,28 +1549,32 @@ static IndexMetadata validateResize(ClusterState state, String sourceIndex, Stri
}

static void prepareResizeIndexSettings(
final ClusterState currentState,
final Metadata metadata,
final ClusterBlocks clusterBlocks,
final RoutingTable routingTable,
final Settings.Builder indexSettingsBuilder,
final Index resizeSourceIndex,
final String resizeIntoName,
final ResizeType type,
final boolean copySettings,
final IndexScopedSettings indexScopedSettings
) {
final IndexMetadata sourceMetadata = currentState.metadata().index(resizeSourceIndex.getName());
final IndexMetadata sourceMetadata = metadata.index(resizeSourceIndex.getName());
if (type == ResizeType.SHRINK) {
final List<String> nodesToAllocateOn = validateShrinkIndex(
currentState,
metadata,
clusterBlocks,
routingTable,
resizeSourceIndex.getName(),
resizeIntoName,
indexSettingsBuilder.build()
);
indexSettingsBuilder.put(INDEX_SHRINK_INITIAL_RECOVERY_KEY, Strings.arrayToCommaDelimitedString(nodesToAllocateOn.toArray()));
} else if (type == ResizeType.SPLIT) {
validateSplitIndex(currentState, resizeSourceIndex.getName(), resizeIntoName, indexSettingsBuilder.build());
validateSplitIndex(metadata, clusterBlocks, resizeSourceIndex.getName(), resizeIntoName, indexSettingsBuilder.build());
indexSettingsBuilder.putNull(INDEX_SHRINK_INITIAL_RECOVERY_KEY);
} else if (type == ResizeType.CLONE) {
validateCloneIndex(currentState, resizeSourceIndex.getName(), resizeIntoName, indexSettingsBuilder.build());
validateCloneIndex(metadata, clusterBlocks, resizeSourceIndex.getName(), resizeIntoName, indexSettingsBuilder.build());
indexSettingsBuilder.putNull(INDEX_SHRINK_INITIAL_RECOVERY_KEY);
} else {
throw new IllegalStateException("unknown resize type is " + type);
Expand Down
Loading

0 comments on commit a94a6ae

Please sign in to comment.