Skip to content

Commit 6776c03

Browse files
committed
Implement diffUtils in RemoteRoutingTable
Signed-off-by: Arpit Bandejiya <abandeji@amazon.com>
1 parent bb335f6 commit 6776c03

File tree

3 files changed

+41
-42
lines changed

3 files changed

+41
-42
lines changed

server/src/main/java/org/opensearch/cluster/routing/remote/RemoteRoutingTableService.java

+26-39
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.apache.lucene.store.IndexInput;
1414
import org.opensearch.action.LatchedActionListener;
1515
import org.opensearch.cluster.ClusterState;
16+
import org.opensearch.cluster.Diff;
17+
import org.opensearch.cluster.DiffableUtils;
1618
import org.opensearch.cluster.routing.IndexRoutingTable;
1719
import org.opensearch.cluster.routing.RoutingTable;
1820
import org.opensearch.common.CheckedRunnable;
@@ -28,6 +30,8 @@
2830
import org.opensearch.common.settings.Settings;
2931
import org.opensearch.common.util.io.IOUtils;
3032
import org.opensearch.core.action.ActionListener;
33+
import org.opensearch.core.common.io.stream.StreamInput;
34+
import org.opensearch.core.common.io.stream.StreamOutput;
3135
import org.opensearch.core.index.Index;
3236
import org.opensearch.gateway.remote.ClusterMetadataManifest;
3337
import org.opensearch.gateway.remote.RemoteClusterStateUtils;
@@ -88,6 +92,19 @@ public class RemoteRoutingTableService implements Closeable {
8892
private BlobStoreRepository blobStoreRepository;
8993
private final ThreadPool threadPool;
9094

95+
private static final DiffableUtils.NonDiffableValueSerializer<String, IndexRoutingTable> CUSTOM_ROUTING_TABLE_VALUE_SERIALIZER = new DiffableUtils.NonDiffableValueSerializer<String, IndexRoutingTable>() {
96+
@Override
97+
public void write(IndexRoutingTable value, StreamOutput out) throws IOException {
98+
value.writeTo(out);
99+
}
100+
101+
@Override
102+
public IndexRoutingTable read(StreamInput in, String key) throws IOException {
103+
return IndexRoutingTable.readFrom(in);
104+
}
105+
};
106+
107+
91108
public RemoteRoutingTableService(Supplier<RepositoriesService> repositoriesService,
92109
Settings settings,
93110
ThreadPool threadPool) {
@@ -97,19 +114,6 @@ public RemoteRoutingTableService(Supplier<RepositoriesService> repositoriesServi
97114
this.threadPool = threadPool;
98115
}
99116

100-
public List<IndexRoutingTable> getChangedIndicesRouting( ClusterState previousClusterState,
101-
ClusterState clusterState) {
102-
Map<String, IndexRoutingTable> previousIndexRoutingTable = previousClusterState.getRoutingTable().getIndicesRouting();
103-
List<IndexRoutingTable> changedIndicesRouting = new ArrayList<>();
104-
for (IndexRoutingTable indexRouting : clusterState.getRoutingTable().getIndicesRouting().values()) {
105-
if (!(previousIndexRoutingTable.containsKey(indexRouting.getIndex().getName()) && indexRouting.equals(previousIndexRoutingTable.get(indexRouting.getIndex().getName())))) {
106-
changedIndicesRouting.add(indexRouting);
107-
logger.info("changedIndicesRouting {}", indexRouting.prettyPrint());
108-
}
109-
}
110-
111-
return changedIndicesRouting;
112-
}
113117

114118
public CheckedRunnable<IOException> getIndexRoutingAsyncAction(
115119
ClusterState clusterState,
@@ -202,7 +206,7 @@ public List<ClusterMetadataManifest.UploadedIndexMetadata> getAllUploadedIndices
202206
uploadedIndexRouting -> allUploadedIndicesRouting.put(uploadedIndexRouting.getIndexName(), uploadedIndexRouting)
203207
);
204208

205-
indicesRoutingToDelete.forEach(index -> allUploadedIndicesRouting.remove(index));
209+
indicesRoutingToDelete.forEach(allUploadedIndicesRouting::remove);
206210

207211
logger.info("allUploadedIndicesRouting ROUTING {}", allUploadedIndicesRouting);
208212

@@ -274,34 +278,17 @@ public List<ClusterMetadataManifest.UploadedIndexMetadata> getUpdatedIndexRoutin
274278
}).collect(Collectors.toList());
275279
}
276280

277-
public static List<String> getIndicesRoutingDeleted(RoutingTable previousRoutingTable, RoutingTable currentRoutingTable) {
278-
List<String> deletedIndicesRouting = new ArrayList<>();
279-
for(IndexRoutingTable previousIndexRouting: previousRoutingTable.getIndicesRouting().values()) {
280-
if(!currentRoutingTable.getIndicesRouting().containsKey(previousIndexRouting.getIndex().getName())) {
281-
// Latest Routing Table does not have entry for the index which means the index is deleted
282-
deletedIndicesRouting.add(previousIndexRouting.getIndex().getName());
283-
}
284-
}
285-
return deletedIndicesRouting;
286-
}
287281

288-
public static List<String> getIndicesRoutingUpdated(RoutingTable previousRoutingTable, RoutingTable currentRoutingTable) {
289-
List<String> updatedIndicesRouting = new ArrayList<>();
290-
for(IndexRoutingTable currentIndicesRouting: currentRoutingTable.getIndicesRouting().values()) {
291-
if(!previousRoutingTable.getIndicesRouting().containsKey(currentIndicesRouting.getIndex().getName())) {
292-
// Latest Routing Table does not have entry for the index which means the index is created
293-
updatedIndicesRouting.add(currentIndicesRouting.getIndex().getName());
294-
} else {
295-
if(previousRoutingTable.getIndicesRouting().get(currentIndicesRouting.getIndex().getName()).equals(currentIndicesRouting)) {
296-
// if the latest routing table has the same routing table as the previous routing table, then the index is not updated
297-
continue;
298-
}
299-
updatedIndicesRouting.add(currentIndicesRouting.getIndex().getName());
300-
}
301-
}
302-
return updatedIndicesRouting;
282+
public static DiffableUtils.MapDiff<String, IndexRoutingTable, Map<String, IndexRoutingTable>> getIndicesRoutingMapDiff(RoutingTable before, RoutingTable after) {
283+
return DiffableUtils.diff(
284+
before.getIndicesRouting(),
285+
after.getIndicesRouting(),
286+
DiffableUtils.getStringKeySerializer(),
287+
CUSTOM_ROUTING_TABLE_VALUE_SERIALIZER
288+
);
303289
}
304290

291+
305292
@Override
306293
public void close() throws IOException {
307294
if (blobStoreRepository != null) {

server/src/main/java/org/opensearch/gateway/remote/ClusterStateDiffManifest.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.apache.lucene.util.packed.DirectMonotonicReader;
1212
import org.opensearch.cluster.ClusterState;
13+
import org.opensearch.cluster.DiffableUtils;
1314
import org.opensearch.cluster.metadata.IndexMetadata;
1415
import org.opensearch.cluster.metadata.Metadata;
1516
import org.opensearch.cluster.routing.IndexRoutingTable;
@@ -89,8 +90,14 @@ public class ClusterStateDiffManifest implements ToXContentObject {
8990
customMetadataDeleted.add(custom);
9091
}
9192
}
92-
indicesRoutingUpdated = RemoteRoutingTableService.getIndicesRoutingUpdated(previousState.routingTable(), state.routingTable());
93-
indicesRoutingDeleted = RemoteRoutingTableService.getIndicesRoutingDeleted(previousState.routingTable(), state.routingTable());
93+
94+
DiffableUtils.MapDiff<String, IndexRoutingTable, Map<String, IndexRoutingTable>> routingTableDiff = RemoteRoutingTableService.getIndicesRoutingMapDiff(previousState.getRoutingTable(),
95+
state.getRoutingTable());
96+
97+
indicesRoutingUpdated = new ArrayList<>();
98+
routingTableDiff.getUpserts().forEach((k,v) -> indicesRoutingUpdated.add(k));
99+
100+
indicesRoutingDeleted = routingTableDiff.getDeletes();
94101
hashesOfConsistentSettingsUpdated = !state.metadata().hashesOfConsistentSettings().equals(previousState.metadata().hashesOfConsistentSettings());
95102
clusterStateCustomUpdated = new ArrayList<>();
96103
clusterStateCustomDeleted = new ArrayList<>();

server/src/main/java/org/opensearch/gateway/remote/RemoteClusterStateService.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
import org.opensearch.action.LatchedActionListener;
5252
import org.opensearch.cluster.ClusterName;
5353
import org.opensearch.cluster.ClusterState;
54+
import org.opensearch.cluster.Diff;
55+
import org.opensearch.cluster.DiffableUtils;
5456
import org.opensearch.cluster.block.ClusterBlocks;
5557
import org.opensearch.cluster.coordination.CoordinationMetadata;
5658
import org.opensearch.cluster.metadata.DiffableStringMap;
@@ -337,9 +339,12 @@ public ClusterMetadataManifest writeIncrementalMetadata(
337339
}
338340

339341
List<IndexRoutingTable> indicesRoutingToUpload = new ArrayList<>();
342+
DiffableUtils.MapDiff<String, IndexRoutingTable, Map<String, IndexRoutingTable>> routingTableDiff = null;
340343
if (remoteRoutingTableService != null) {
341-
indicesRoutingToUpload = remoteRoutingTableService.getChangedIndicesRouting(previousClusterState, clusterState);
344+
routingTableDiff = RemoteRoutingTableService.getIndicesRoutingMapDiff(previousClusterState.getRoutingTable(), clusterState.getRoutingTable());
345+
routingTableDiff.getUpserts().forEach((k, v) -> indicesRoutingToUpload.add(v));
342346
}
347+
343348
UploadedMetadataResults uploadedMetadataResults;
344349
// For migration case from codec V0 or V1 to V2, we have added null check on metadata attribute files,
345350
// If file is empty and codec is 1 then write global metadata.

0 commit comments

Comments
 (0)