Skip to content

Commit 376eb8c

Browse files
committed
Address flakiness of testRemoteCleanupDeleteStale
Signed-off-by: Shivansh Arora <hishiv@amazon.com>
1 parent 2e13e9c commit 376eb8c

File tree

3 files changed

+64
-17
lines changed

3 files changed

+64
-17
lines changed

server/src/internalClusterTest/java/org/opensearch/gateway/remote/RemoteClusterStateCleanupManagerIT.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ public void testRemoteCleanupDeleteStale() throws Exception {
108108
.add("cluster-state")
109109
.add(getClusterState().metadata().clusterUUID());
110110
BlobPath manifestContainerPath = baseMetadataPath.add("manifest");
111+
RemoteClusterStateCleanupManager remoteClusterStateCleanupManager = internalCluster().getClusterManagerNodeInstance(
112+
RemoteClusterStateCleanupManager.class
113+
);
111114

112115
// set cleanup interval to 100 ms to make the test faster
113116
ClusterUpdateSettingsResponse response = client().admin()
@@ -117,6 +120,7 @@ public void testRemoteCleanupDeleteStale() throws Exception {
117120
.get();
118121

119122
assertTrue(response.isAcknowledged());
123+
assertBusy(() -> assertEquals(100, remoteClusterStateCleanupManager.getStaleFileDeletionTask().getInterval().getMillis()));
120124

121125
assertBusy(() -> {
122126
int manifestFiles = repository.blobStore().blobContainer(manifestContainerPath).listBlobsByPrefix("manifest").size();
@@ -128,7 +132,7 @@ public void testRemoteCleanupDeleteStale() throws Exception {
128132
"Current number of manifest files: " + manifestFiles,
129133
manifestFiles >= RETAINED_MANIFESTS && manifestFiles < RETAINED_MANIFESTS + 2 * SKIP_CLEANUP_STATE_CHANGES
130134
);
131-
}, 500, TimeUnit.MILLISECONDS);
135+
});
132136

133137
// disable the clean up to avoid race condition during shutdown
134138
response = client().admin()

server/src/main/java/org/opensearch/gateway/remote/model/RemoteClusterMetadataManifest.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,27 @@ public String getType() {
9393

9494
@Override
9595
public String generateBlobFileName() {
96-
// 123456789012_test-cluster/cluster-state/dsgYj10Nkso7/manifest/manifest__<inverted_term>__<inverted_version>__C/P__<inverted__timestamp>__
97-
// <codec_version>
98-
String blobFileName = String.join(
99-
DELIMITER,
100-
MANIFEST,
101-
RemoteStoreUtils.invertLong(clusterMetadataManifest.getClusterTerm()),
102-
RemoteStoreUtils.invertLong(clusterMetadataManifest.getStateVersion()),
103-
(clusterMetadataManifest.isCommitted() ? COMMITTED : PUBLISHED),
104-
RemoteStoreUtils.invertLong(System.currentTimeMillis()),
105-
String.valueOf(clusterMetadataManifest.getCodecVersion())
106-
// Keep the codec version at last place only, during we read last place to determine codec version.
107-
);
108-
this.blobFileName = blobFileName;
96+
if (blobFileName != null) {
97+
return blobFileName;
98+
}
99+
// filename is something like: manifest__<inverted_term>__<inverted_version>__C/P__<inverted__timestamp>__<codec_version>
100+
if (clusterMetadataManifest != null) {
101+
this.blobFileName = String.join(
102+
DELIMITER,
103+
MANIFEST,
104+
RemoteStoreUtils.invertLong(clusterMetadataManifest.getClusterTerm()),
105+
RemoteStoreUtils.invertLong(clusterMetadataManifest.getStateVersion()),
106+
(clusterMetadataManifest.isCommitted() ? COMMITTED : PUBLISHED),
107+
RemoteStoreUtils.invertLong(System.currentTimeMillis()),
108+
String.valueOf(clusterMetadataManifest.getCodecVersion())
109+
// Keep the codec version at last place only, during we read last place to determine codec version.
110+
);
111+
} else {
112+
String[] pathTokens = getBlobPathTokens();
113+
assert pathTokens != null : "path tokens must not be null";
114+
assert pathTokens.length > 0 : "path tokens must have at least one segment";
115+
this.blobFileName = pathTokens[pathTokens.length - 1];
116+
}
109117
return blobFileName;
110118
}
111119

@@ -131,16 +139,17 @@ public ClusterMetadataManifest deserialize(final InputStream inputStream) throws
131139
return blobStoreFormat.deserialize(blobName, getNamedXContentRegistry(), Streams.readFully(inputStream));
132140
}
133141

134-
private int getManifestCodecVersion() {
142+
// package private for testing
143+
int getManifestCodecVersion() {
135144
assert blobName != null;
136-
String[] splitName = blobName.split(DELIMITER);
145+
String[] splitName = generateBlobFileName().split(DELIMITER);
137146
if (splitName.length == SPLITTED_MANIFEST_FILE_LENGTH) {
138147
return Integer.parseInt(splitName[splitName.length - 1]); // Last value would be codec version.
139148
} else if (splitName.length < SPLITTED_MANIFEST_FILE_LENGTH) { // Where codec is not part of file name, i.e. default codec version 0
140149
// is used.
141150
return ClusterMetadataManifest.CODEC_V0;
142151
} else {
143-
throw new IllegalArgumentException("Manifest file name is corrupted");
152+
throw new IllegalArgumentException("Manifest file name is corrupted : " + blobName);
144153
}
145154
}
146155

server/src/test/java/org/opensearch/gateway/remote/model/RemoteClusterMetadataManifestTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import java.util.stream.Stream;
4242

4343
import static java.util.stream.Collectors.toList;
44+
import static org.opensearch.gateway.remote.ClusterMetadataManifest.CODEC_V0;
45+
import static org.opensearch.gateway.remote.ClusterMetadataManifest.CODEC_V2;
4446
import static org.opensearch.gateway.remote.model.RemoteClusterMetadataManifest.MANIFEST;
4547
import static org.opensearch.gateway.remote.model.RemoteClusterMetadataManifest.MANIFEST_CURRENT_CODEC_VERSION;
4648
import static org.hamcrest.Matchers.greaterThan;
@@ -191,6 +193,16 @@ public void testGenerateBlobFileName() {
191193
assertThat(nameTokens[3], is("C"));
192194
assertThat(RemoteStoreUtils.invertLong(nameTokens[4]), lessThanOrEqualTo(System.currentTimeMillis()));
193195
assertThat(nameTokens[5], is(String.valueOf(MANIFEST_CURRENT_CODEC_VERSION)));
196+
197+
String blobName = "/usr/local/random/path/to/manifest/manifest__1__2__3__4__2";
198+
RemoteClusterMetadataManifest remoteObjectForDownload = new RemoteClusterMetadataManifest(
199+
blobName,
200+
clusterUUID,
201+
compressor,
202+
namedXContentRegistry
203+
);
204+
assertEquals("manifest__1__2__3__4__2", remoteObjectForDownload.generateBlobFileName());
205+
assertEquals(remoteObjectForDownload.getManifestCodecVersion(), 2);
194206
}
195207

196208
public void testGetUploadedMetadata() throws IOException {
@@ -236,6 +248,28 @@ public void testSerDe() throws IOException {
236248
assertThrows(IllegalArgumentException.class, () -> invalidRemoteObject.deserialize(new ByteArrayInputStream(new byte[0])));
237249
}
238250

251+
public void testGetManifestCodecVersion() {
252+
String manifestFileWithDelimiterInPath =
253+
"123456789012_test-cluster/cluster-state/dsgYj10__Nkso7/manifest/manifest__9223372036854775806__9223372036854775804__C__9223370319103329556__2";
254+
RemoteClusterMetadataManifest remoteManifestForDownload = new RemoteClusterMetadataManifest(
255+
manifestFileWithDelimiterInPath,
256+
clusterUUID,
257+
compressor,
258+
namedXContentRegistry
259+
);
260+
assertEquals(CODEC_V2, remoteManifestForDownload.getManifestCodecVersion());
261+
262+
String v0ManifestFileWithDelimiterInPath =
263+
"123456789012_test-cluster/cluster-state/dsgYj10__Nkso7/manifest/manifest__9223372036854775806__9223372036854775804__C__9223370319103329556";
264+
RemoteClusterMetadataManifest remoteManifestV0ForDownload = new RemoteClusterMetadataManifest(
265+
v0ManifestFileWithDelimiterInPath,
266+
clusterUUID,
267+
compressor,
268+
namedXContentRegistry
269+
);
270+
assertEquals(CODEC_V0, remoteManifestV0ForDownload.getManifestCodecVersion());
271+
}
272+
239273
private ClusterMetadataManifest getClusterMetadataManifest() {
240274
return ClusterMetadataManifest.builder()
241275
.opensearchVersion(Version.CURRENT)

0 commit comments

Comments
 (0)