Skip to content

Commit bc6d493

Browse files
committed
Moving path strategy method to Utils
Signed-off-by: Shourya Dutta Biswas <114977491+shourya035@users.noreply.github.com>
1 parent 4cf8f6e commit bc6d493

File tree

2 files changed

+46
-39
lines changed

2 files changed

+46
-39
lines changed

server/src/main/java/org/opensearch/index/remote/RemoteMigrationIndexMetadataUpdater.java

+5-30
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,16 @@
99
package org.opensearch.index.remote;
1010

1111
import org.apache.logging.log4j.Logger;
12-
import org.opensearch.Version;
13-
import org.opensearch.cluster.ClusterState;
1412
import org.opensearch.cluster.metadata.IndexMetadata;
1513
import org.opensearch.cluster.node.DiscoveryNodes;
1614
import org.opensearch.cluster.routing.IndexRoutingTable;
1715
import org.opensearch.cluster.routing.RoutingTable;
1816
import org.opensearch.cluster.routing.ShardRouting;
1917
import org.opensearch.cluster.routing.ShardRoutingState;
2018
import org.opensearch.common.settings.Settings;
21-
import org.opensearch.index.remote.RemoteStoreEnums.PathHashAlgorithm;
2219
import org.opensearch.index.remote.RemoteStoreEnums.PathType;
2320
import org.opensearch.indices.replication.common.ReplicationType;
2421

25-
import java.util.HashMap;
2622
import java.util.List;
2723
import java.util.Map;
2824
import java.util.Objects;
@@ -32,9 +28,8 @@
3228
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_REMOTE_STORE_ENABLED;
3329
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_REMOTE_TRANSLOG_STORE_REPOSITORY;
3430
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_REPLICATION_TYPE;
31+
import static org.opensearch.index.remote.RemoteStoreUtils.determineRemoteStorePathStrategyDuringMigration;
3532
import static org.opensearch.index.remote.RemoteStoreUtils.getRemoteStoreRepoName;
36-
import static org.opensearch.indices.RemoteStoreSettings.CLUSTER_REMOTE_STORE_PATH_HASH_ALGORITHM_SETTING;
37-
import static org.opensearch.indices.RemoteStoreSettings.CLUSTER_REMOTE_STORE_PATH_TYPE_SETTING;
3833
import static org.opensearch.node.remotestore.RemoteStoreNodeAttribute.REMOTE_STORE_SEGMENT_REPOSITORY_NAME_ATTRIBUTE_KEY;
3934
import static org.opensearch.node.remotestore.RemoteStoreNodeAttribute.REMOTE_STORE_TRANSLOG_REPOSITORY_NAME_ATTRIBUTE_KEY;
4035

@@ -135,35 +130,15 @@ private boolean needsRemoteIndexSettingsUpdate(
135130
public void maybeUpdateRemoteStorePathStrategy(IndexMetadata.Builder indexMetadataBuilder, String index) {
136131
if (indexHasRemotePathMetadata(indexMetadata) == false) {
137132
logger.info("Adding remote store path strategy for index [{}] during migration", index);
138-
indexMetadataBuilder.putCustom(REMOTE_STORE_CUSTOM_KEY, createRemoteStorePathTypeMetadata(clusterSettings, discoveryNodes));
133+
indexMetadataBuilder.putCustom(
134+
REMOTE_STORE_CUSTOM_KEY,
135+
determineRemoteStorePathStrategyDuringMigration(clusterSettings, discoveryNodes)
136+
);
139137
} else {
140138
logger.debug("Index {} already has remote store path strategy", index);
141139
}
142140
}
143141

144-
/**
145-
* Generates the remote store path type information to be added to custom data of index metadata.
146-
*
147-
* @param clusterSettings Current Cluster settings from {@link ClusterState}
148-
* @param discoveryNodes Current {@link DiscoveryNodes} from the cluster state
149-
* @return {@link Map} to be added as custom data in index metadata
150-
*/
151-
private Map<String, String> createRemoteStorePathTypeMetadata(Settings clusterSettings, DiscoveryNodes discoveryNodes) {
152-
Version minNodeVersion = discoveryNodes.getMinNodeVersion();
153-
PathType pathType = Version.CURRENT.compareTo(minNodeVersion) <= 0
154-
? CLUSTER_REMOTE_STORE_PATH_TYPE_SETTING.get(clusterSettings)
155-
: PathType.FIXED;
156-
PathHashAlgorithm pathHashAlgorithm = pathType == PathType.FIXED
157-
? null
158-
: CLUSTER_REMOTE_STORE_PATH_HASH_ALGORITHM_SETTING.get(clusterSettings);
159-
Map<String, String> remoteCustomData = new HashMap<>();
160-
remoteCustomData.put(PathType.NAME, pathType.name());
161-
if (Objects.nonNull(pathHashAlgorithm)) {
162-
remoteCustomData.put(PathHashAlgorithm.NAME, pathHashAlgorithm.name());
163-
}
164-
return remoteCustomData;
165-
}
166-
167142
public static boolean indexHasAllRemoteStoreRelatedMetadata(IndexMetadata indexMetadata) {
168143
return indexHasRemoteStoreSettings(indexMetadata.getSettings()) && indexHasRemotePathMetadata(indexMetadata);
169144
}

server/src/main/java/org/opensearch/index/remote/RemoteStoreUtils.java

+41-9
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010

1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
13+
import org.opensearch.Version;
14+
import org.opensearch.cluster.ClusterState;
15+
import org.opensearch.cluster.metadata.IndexMetadata;
1316
import org.opensearch.cluster.node.DiscoveryNode;
1417
import org.opensearch.cluster.node.DiscoveryNodes;
15-
import org.opensearch.cluster.metadata.IndexMetadata;
1618
import org.opensearch.common.collect.Tuple;
19+
import org.opensearch.common.settings.Settings;
1720
import org.opensearch.node.remotestore.RemoteStoreNodeAttribute;
1821

1922
import java.nio.ByteBuffer;
@@ -23,10 +26,13 @@
2326
import java.util.List;
2427
import java.util.Locale;
2528
import java.util.Map;
26-
import java.util.Optional;
2729
import java.util.Objects;
30+
import java.util.Optional;
2831
import java.util.function.Function;
2932

33+
import static org.opensearch.indices.RemoteStoreSettings.CLUSTER_REMOTE_STORE_PATH_HASH_ALGORITHM_SETTING;
34+
import static org.opensearch.indices.RemoteStoreSettings.CLUSTER_REMOTE_STORE_PATH_TYPE_SETTING;
35+
3036
/**
3137
* Utils for remote store
3238
*
@@ -161,17 +167,43 @@ public static RemoteStorePathStrategy determineRemoteStorePathStrategy(IndexMeta
161167
assert remoteCustomData == null || remoteCustomData.containsKey(RemoteStoreEnums.PathType.NAME);
162168
if (remoteCustomData != null && remoteCustomData.containsKey(RemoteStoreEnums.PathType.NAME)) {
163169
RemoteStoreEnums.PathType pathType = RemoteStoreEnums.PathType.parseString(
164-
remoteCustomData.get(RemoteStoreEnums.PathType.NAME)
170+
remoteCustomData.get(RemoteStoreEnums.PathType.NAME)
165171
);
166172
String hashAlgoStr = remoteCustomData.get(RemoteStoreEnums.PathHashAlgorithm.NAME);
167173
RemoteStoreEnums.PathHashAlgorithm hashAlgorithm = Objects.nonNull(hashAlgoStr)
168-
? RemoteStoreEnums.PathHashAlgorithm.parseString(hashAlgoStr)
169-
: null;
174+
? RemoteStoreEnums.PathHashAlgorithm.parseString(hashAlgoStr)
175+
: null;
170176
return new RemoteStorePathStrategy(pathType, hashAlgorithm);
171177
}
172178
return new RemoteStorePathStrategy(RemoteStoreEnums.PathType.FIXED);
173179
}
174180

181+
/**
182+
* Generates the remote store path type information to be added to custom data of index metadata during migration
183+
*
184+
* @param clusterSettings Current Cluster settings from {@link ClusterState}
185+
* @param discoveryNodes Current {@link DiscoveryNodes} from the cluster state
186+
* @return {@link Map} to be added as custom data in index metadata
187+
*/
188+
public static Map<String, String> determineRemoteStorePathStrategyDuringMigration(
189+
Settings clusterSettings,
190+
DiscoveryNodes discoveryNodes
191+
) {
192+
Version minNodeVersion = discoveryNodes.getMinNodeVersion();
193+
RemoteStoreEnums.PathType pathType = Version.CURRENT.compareTo(minNodeVersion) <= 0
194+
? CLUSTER_REMOTE_STORE_PATH_TYPE_SETTING.get(clusterSettings)
195+
: RemoteStoreEnums.PathType.FIXED;
196+
RemoteStoreEnums.PathHashAlgorithm pathHashAlgorithm = pathType == RemoteStoreEnums.PathType.FIXED
197+
? null
198+
: CLUSTER_REMOTE_STORE_PATH_HASH_ALGORITHM_SETTING.get(clusterSettings);
199+
Map<String, String> remoteCustomData = new HashMap<>();
200+
remoteCustomData.put(RemoteStoreEnums.PathType.NAME, pathType.name());
201+
if (Objects.nonNull(pathHashAlgorithm)) {
202+
remoteCustomData.put(RemoteStoreEnums.PathHashAlgorithm.NAME, pathHashAlgorithm.name());
203+
}
204+
return remoteCustomData;
205+
}
206+
175207
/**
176208
* Fetches segment and translog repository names from remote store node attributes.
177209
* Returns a blank {@link HashMap} if the cluster does not contain any remote nodes.
@@ -183,10 +215,10 @@ public static RemoteStorePathStrategy determineRemoteStorePathStrategy(IndexMeta
183215
*/
184216
public static Map<String, String> getRemoteStoreRepoName(DiscoveryNodes discoveryNodes) {
185217
Optional<DiscoveryNode> remoteNode = discoveryNodes.getNodes()
186-
.values()
187-
.stream()
188-
.filter(DiscoveryNode::isRemoteStoreNode)
189-
.findFirst();
218+
.values()
219+
.stream()
220+
.filter(DiscoveryNode::isRemoteStoreNode)
221+
.findFirst();
190222
return remoteNode.map(RemoteStoreNodeAttribute::getDataRepoNames).orElseGet(HashMap::new);
191223
}
192224
}

0 commit comments

Comments
 (0)