Skip to content

Commit d256b66

Browse files
committed
Introduce remote store path type in customData in IndexMetadata
Signed-off-by: Ashish Singh <ssashish@amazon.com>
1 parent e6eec36 commit d256b66

File tree

6 files changed

+169
-9
lines changed

6 files changed

+169
-9
lines changed

server/src/main/java/org/opensearch/cluster/metadata/IndexMetadata.java

+1
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ public static APIBlock readFrom(StreamInput input) throws IOException {
635635
static final String KEY_ROLLOVER_INFOS = "rollover_info";
636636
static final String KEY_SYSTEM = "system";
637637
public static final String KEY_PRIMARY_TERMS = "primary_terms";
638+
public static final String REMOTE_STORE_CUSTOM_KEY = "remote_store";
638639

639640
public static final String INDEX_STATE_FILE_PREFIX = "state-";
640641

server/src/main/java/org/opensearch/cluster/metadata/MetadataCreateIndexService.java

+26-8
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
import org.opensearch.index.mapper.MapperService;
8989
import org.opensearch.index.mapper.MapperService.MergeReason;
9090
import org.opensearch.index.query.QueryShardContext;
91+
import org.opensearch.index.remote.RemoteStorePathType;
9192
import org.opensearch.index.shard.IndexSettingProvider;
9293
import org.opensearch.index.translog.Translog;
9394
import org.opensearch.indices.IndexCreationException;
@@ -498,7 +499,8 @@ private ClusterState applyCreateIndexWithTemporaryService(
498499
temporaryIndexMeta.getSettings(),
499500
temporaryIndexMeta.getRoutingNumShards(),
500501
sourceMetadata,
501-
temporaryIndexMeta.isSystem()
502+
temporaryIndexMeta.isSystem(),
503+
temporaryIndexMeta.getCustomData()
502504
);
503505
} catch (Exception e) {
504506
logger.info("failed to build index metadata [{}]", request.index());
@@ -522,10 +524,11 @@ private ClusterState applyCreateIndexWithTemporaryService(
522524

523525
/**
524526
* Given a state and index settings calculated after applying templates, validate metadata for
525-
* the new index, returning an {@link IndexMetadata} for the new index
527+
* the new index, returning an {@link IndexMetadata} for the new index.
528+
* <p>
529+
* The access level of the method changed to default level for visibility to test.
526530
*/
527-
private IndexMetadata buildAndValidateTemporaryIndexMetadata(
528-
final ClusterState currentState,
531+
IndexMetadata buildAndValidateTemporaryIndexMetadata(
529532
final Settings aggregatedIndexSettings,
530533
final CreateIndexClusterStateUpdateRequest request,
531534
final int routingNumShards
@@ -544,6 +547,16 @@ private IndexMetadata buildAndValidateTemporaryIndexMetadata(
544547
tmpImdBuilder.settings(indexSettings);
545548
tmpImdBuilder.system(isSystem);
546549

550+
if (isRemoteStoreAttributePresent(settings)) {
551+
String pathType;
552+
if (clusterService.getClusterSettings().get(IndicesService.CLUSTER_REMOTE_STORE_PATH_PREFIX_OPTIMISED_SETTING)) {
553+
pathType = RemoteStorePathType.HASHED_PREFIX.toString();
554+
} else {
555+
pathType = RemoteStorePathType.FIXED.toString();
556+
}
557+
tmpImdBuilder.putCustom(IndexMetadata.REMOTE_STORE_CUSTOM_KEY, Map.of(RemoteStorePathType.NAME, pathType));
558+
}
559+
547560
// Set up everything, now locally create the index to see that things are ok, and apply
548561
IndexMetadata tempMetadata = tmpImdBuilder.build();
549562
validateActiveShardCount(request.waitForActiveShards(), tempMetadata);
@@ -582,7 +595,7 @@ private ClusterState applyCreateIndexRequestWithV1Templates(
582595
clusterService.getClusterSettings()
583596
);
584597
int routingNumShards = getIndexNumberOfRoutingShards(aggregatedIndexSettings, null);
585-
IndexMetadata tmpImd = buildAndValidateTemporaryIndexMetadata(currentState, aggregatedIndexSettings, request, routingNumShards);
598+
IndexMetadata tmpImd = buildAndValidateTemporaryIndexMetadata(aggregatedIndexSettings, request, routingNumShards);
586599

587600
return applyCreateIndexWithTemporaryService(
588601
currentState,
@@ -647,7 +660,7 @@ private ClusterState applyCreateIndexRequestWithV2Template(
647660
clusterService.getClusterSettings()
648661
);
649662
int routingNumShards = getIndexNumberOfRoutingShards(aggregatedIndexSettings, null);
650-
IndexMetadata tmpImd = buildAndValidateTemporaryIndexMetadata(currentState, aggregatedIndexSettings, request, routingNumShards);
663+
IndexMetadata tmpImd = buildAndValidateTemporaryIndexMetadata(aggregatedIndexSettings, request, routingNumShards);
651664

652665
return applyCreateIndexWithTemporaryService(
653666
currentState,
@@ -728,7 +741,7 @@ private ClusterState applyCreateIndexRequestWithExistingMetadata(
728741
clusterService.getClusterSettings()
729742
);
730743
final int routingNumShards = getIndexNumberOfRoutingShards(aggregatedIndexSettings, sourceMetadata);
731-
IndexMetadata tmpImd = buildAndValidateTemporaryIndexMetadata(currentState, aggregatedIndexSettings, request, routingNumShards);
744+
IndexMetadata tmpImd = buildAndValidateTemporaryIndexMetadata(aggregatedIndexSettings, request, routingNumShards);
732745

733746
return applyCreateIndexWithTemporaryService(
734747
currentState,
@@ -1147,7 +1160,8 @@ static IndexMetadata buildIndexMetadata(
11471160
Settings indexSettings,
11481161
int routingNumShards,
11491162
@Nullable IndexMetadata sourceMetadata,
1150-
boolean isSystem
1163+
boolean isSystem,
1164+
Map<String, DiffableStringMap> customData
11511165
) {
11521166
IndexMetadata.Builder indexMetadataBuilder = createIndexMetadataBuilder(indexName, sourceMetadata, indexSettings, routingNumShards);
11531167
indexMetadataBuilder.system(isSystem);
@@ -1168,6 +1182,10 @@ static IndexMetadata buildIndexMetadata(
11681182
indexMetadataBuilder.putAlias(aliases.get(i));
11691183
}
11701184

1185+
for (Map.Entry<String, DiffableStringMap> entry : customData.entrySet()) {
1186+
indexMetadataBuilder.putCustom(entry.getKey(), entry.getValue());
1187+
}
1188+
11711189
indexMetadataBuilder.state(IndexMetadata.State.OPEN);
11721190
return indexMetadataBuilder.build();
11731191
}

server/src/main/java/org/opensearch/common/settings/ClusterSettings.java

+1
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ public void apply(Settings value, Settings current, Settings previous) {
709709
CpuBasedAdmissionControllerSettings.INDEXING_CPU_USAGE_LIMIT,
710710
CpuBasedAdmissionControllerSettings.SEARCH_CPU_USAGE_LIMIT,
711711
IndicesService.CLUSTER_INDEX_RESTRICT_REPLICATION_TYPE_SETTING,
712+
IndicesService.CLUSTER_REMOTE_STORE_PATH_PREFIX_OPTIMISED_SETTING,
712713

713714
// Concurrent segment search settings
714715
SearchService.CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.index.remote;
10+
11+
/**
12+
* Enumerates the types of remote store paths resolution techniques supported by OpenSearch.
13+
* For more information, see <a href="https://github.com/opensearch-project/OpenSearch/issues/12567">Github issue #12567</a>.
14+
*
15+
* @opensearch.internal
16+
*/
17+
public enum RemoteStorePathType {
18+
19+
FIXED,
20+
HASHED_PREFIX;
21+
22+
public static RemoteStorePathType parseString(String remoteStorePathType) {
23+
try {
24+
return RemoteStorePathType.valueOf(remoteStorePathType);
25+
} catch (IllegalArgumentException e) {
26+
throw new IllegalArgumentException("Could not parse RemoteStorePathType for [" + remoteStorePathType + "]");
27+
} catch (NullPointerException npe) {
28+
// return a default value for null input
29+
return FIXED;
30+
}
31+
}
32+
33+
/**
34+
* This string is used as key for storing information in the custom data in index settings.
35+
*/
36+
public static final String NAME = "path_type";
37+
}

server/src/main/java/org/opensearch/indices/IndicesService.java

+11
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,17 @@ public class IndicesService extends AbstractLifecycleComponent
314314
Property.Final
315315
);
316316

317+
/**
318+
* This setting is used to enable the optimisation in prefix path which helps in achieving higher throughput and lesser
319+
* rate limiting by remote store providers. This setting is effective only for remote store enabled cluster.
320+
*/
321+
public static final Setting<Boolean> CLUSTER_REMOTE_STORE_PATH_PREFIX_OPTIMISED_SETTING = Setting.boolSetting(
322+
"cluster.remote_store.index.path.prefix.optimised",
323+
false,
324+
Property.NodeScope,
325+
Property.Dynamic
326+
);
327+
317328
/**
318329
* The node's settings.
319330
*/

server/src/test/java/org/opensearch/cluster/metadata/MetadataCreateIndexServiceTests.java

+93-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import org.opensearch.index.IndexSettings;
7272
import org.opensearch.index.mapper.MapperService;
7373
import org.opensearch.index.query.QueryShardContext;
74+
import org.opensearch.index.remote.RemoteStorePathType;
7475
import org.opensearch.index.translog.Translog;
7576
import org.opensearch.indices.IndexCreationException;
7677
import org.opensearch.indices.IndicesService;
@@ -1563,13 +1564,104 @@ public void testBuildIndexMetadata() {
15631564
.put(SETTING_NUMBER_OF_SHARDS, 1)
15641565
.build();
15651566
List<AliasMetadata> aliases = singletonList(AliasMetadata.builder("alias1").build());
1566-
IndexMetadata indexMetadata = buildIndexMetadata("test", aliases, () -> null, indexSettings, 4, sourceIndexMetadata, false);
1567+
IndexMetadata indexMetadata = buildIndexMetadata(
1568+
"test",
1569+
aliases,
1570+
() -> null,
1571+
indexSettings,
1572+
4,
1573+
sourceIndexMetadata,
1574+
false,
1575+
new HashMap<>()
1576+
);
15671577

15681578
assertThat(indexMetadata.getAliases().size(), is(1));
15691579
assertThat(indexMetadata.getAliases().keySet().iterator().next(), is("alias1"));
15701580
assertThat("The source index primary term must be used", indexMetadata.primaryTerm(0), is(3L));
15711581
}
15721582

1583+
/**
1584+
* This test checks if the cluster is a remote store cluster then we populate custom data for remote settings in
1585+
* index metadata of the underlying index. This captures information around the resolution pattern of the path for
1586+
* remote segments and translog.
1587+
*/
1588+
public void testRemoteCustomData() {
1589+
// Case 1 - Remote store is not enabled
1590+
IndexMetadata indexMetadata = testRemoteCustomData(false, randomBoolean());
1591+
assertNull(indexMetadata.getCustomData(IndexMetadata.REMOTE_STORE_CUSTOM_KEY));
1592+
1593+
// Case 2 - cluster.remote_store.index.path.prefix.optimised=false (default value)
1594+
indexMetadata = testRemoteCustomData(true, false);
1595+
validateRemoteCustomData(
1596+
indexMetadata.getCustomData(IndexMetadata.REMOTE_STORE_CUSTOM_KEY),
1597+
RemoteStorePathType.NAME,
1598+
RemoteStorePathType.FIXED.toString()
1599+
);
1600+
1601+
// Case 3 - cluster.remote_store.index.path.prefix.optimised=true
1602+
indexMetadata = testRemoteCustomData(true, true);
1603+
validateRemoteCustomData(
1604+
indexMetadata.getCustomData(IndexMetadata.REMOTE_STORE_CUSTOM_KEY),
1605+
RemoteStorePathType.NAME,
1606+
RemoteStorePathType.HASHED_PREFIX.toString()
1607+
);
1608+
}
1609+
1610+
private IndexMetadata testRemoteCustomData(boolean remoteStoreEnabled, boolean optimisedPrefix) {
1611+
Settings.Builder settingsBuilder = Settings.builder();
1612+
if (remoteStoreEnabled) {
1613+
settingsBuilder.put(NODE_ATTRIBUTES.getKey() + REMOTE_STORE_SEGMENT_REPOSITORY_NAME_ATTRIBUTE_KEY, "test");
1614+
}
1615+
if (optimisedPrefix) {
1616+
settingsBuilder.put(IndicesService.CLUSTER_REMOTE_STORE_PATH_PREFIX_OPTIMISED_SETTING.getKey(), true);
1617+
}
1618+
Settings settings = settingsBuilder.build();
1619+
1620+
ClusterService clusterService = mock(ClusterService.class);
1621+
Metadata metadata = Metadata.builder()
1622+
.transientSettings(Settings.builder().put(Metadata.DEFAULT_REPLICA_COUNT_SETTING.getKey(), 1).build())
1623+
.build();
1624+
ClusterState clusterState = ClusterState.builder(org.opensearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY))
1625+
.metadata(metadata)
1626+
.build();
1627+
ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
1628+
when(clusterService.getSettings()).thenReturn(settings);
1629+
when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
1630+
when(clusterService.state()).thenReturn(clusterState);
1631+
1632+
ThreadPool threadPool = new TestThreadPool(getTestName());
1633+
MetadataCreateIndexService metadataCreateIndexService = new MetadataCreateIndexService(
1634+
settings,
1635+
clusterService,
1636+
null,
1637+
null,
1638+
null,
1639+
createTestShardLimitService(randomIntBetween(1, 1000), false, clusterService),
1640+
new Environment(Settings.builder().put("path.home", "dummy").build(), null),
1641+
IndexScopedSettings.DEFAULT_SCOPED_SETTINGS,
1642+
threadPool,
1643+
null,
1644+
new SystemIndices(Collections.emptyMap()),
1645+
true,
1646+
new AwarenessReplicaBalance(settings, clusterService.getClusterSettings())
1647+
);
1648+
CreateIndexClusterStateUpdateRequest request = new CreateIndexClusterStateUpdateRequest("create index", "test", "test");
1649+
Settings indexSettings = Settings.builder()
1650+
.put("index.version.created", Version.CURRENT)
1651+
.put(INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 3)
1652+
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
1653+
.build();
1654+
1655+
IndexMetadata indexMetadata = metadataCreateIndexService.buildAndValidateTemporaryIndexMetadata(indexSettings, request, 0);
1656+
threadPool.shutdown();
1657+
return indexMetadata;
1658+
}
1659+
1660+
private void validateRemoteCustomData(Map<String, String> customData, String expectedKey, String expectedValue) {
1661+
assertTrue(customData.containsKey(expectedKey));
1662+
assertEquals(expectedValue, customData.get(expectedKey));
1663+
}
1664+
15731665
public void testGetIndexNumberOfRoutingShardsWithNullSourceIndex() {
15741666
Settings indexSettings = Settings.builder()
15751667
.put("index.version.created", Version.CURRENT)

0 commit comments

Comments
 (0)