Skip to content

Commit 62e128f

Browse files
Cleanup Old index-N Blobs in Repository Cleanup (#49862) (#49902)
* Cleanup Old index-N Blobs in Repository Cleanup Repository cleanup didn't deal with old index-N, this change adds cleaning up all old index-N found in the repository.
1 parent e4f838e commit 62e128f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java

+3
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,9 @@ private List<String> staleRootBlobs(RepositoryData repositoryData, Set<String> r
736736
return false;
737737
}
738738
return allSnapshotIds.contains(foundUUID) == false;
739+
} else if (blob.startsWith(INDEX_FILE_PREFIX)) {
740+
// TODO: Include the current generation here once we remove keeping index-(N-1) around from #writeIndexGen
741+
return repositoryData.getGenId() > Long.parseLong(blob.substring(INDEX_FILE_PREFIX.length()));
739742
}
740743
return false;
741744
}

server/src/test/java/org/elasticsearch/repositories/blobstore/BlobStoreRepositoryCleanupIT.java

+40
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,24 @@
1919
package org.elasticsearch.repositories.blobstore;
2020

2121
import org.elasticsearch.action.ActionRunnable;
22+
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse;
2223
import org.elasticsearch.action.support.PlainActionFuture;
2324
import org.elasticsearch.cluster.RepositoryCleanupInProgress;
2425
import org.elasticsearch.common.settings.Settings;
2526
import org.elasticsearch.common.unit.ByteSizeUnit;
2627
import org.elasticsearch.common.unit.TimeValue;
2728
import org.elasticsearch.repositories.RepositoriesService;
2829
import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase;
30+
import org.elasticsearch.snapshots.SnapshotState;
2931
import org.elasticsearch.test.ESIntegTestCase;
3032

3133
import java.io.ByteArrayInputStream;
34+
import java.util.concurrent.ExecutionException;
3235
import java.util.concurrent.TimeUnit;
3336

3437
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
3538
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;
39+
import static org.hamcrest.Matchers.is;
3640

3741
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0)
3842
public class BlobStoreRepositoryCleanupIT extends AbstractSnapshotIntegTestCase {
@@ -107,4 +111,40 @@ private String startBlockedCleanup(String repoName) throws Exception {
107111
waitForBlock(masterNode, repoName, TimeValue.timeValueSeconds(60));
108112
return masterNode;
109113
}
114+
115+
public void testCleanupOldIndexN() throws ExecutionException, InterruptedException {
116+
internalCluster().startNodes(Settings.EMPTY);
117+
118+
final String repoName = "test-repo";
119+
logger.info("--> creating repository");
120+
assertAcked(client().admin().cluster().preparePutRepository(repoName).setType("fs").setSettings(Settings.builder()
121+
.put("location", randomRepoPath())
122+
.put("compress", randomBoolean())
123+
.put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
124+
125+
logger.info("--> create three snapshots");
126+
for (int i = 0; i < 3; ++i) {
127+
CreateSnapshotResponse createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot(repoName, "test-snap-" + i)
128+
.setWaitForCompletion(true).get();
129+
assertThat(createSnapshotResponse.getSnapshotInfo().state(), is(SnapshotState.SUCCESS));
130+
}
131+
132+
final RepositoriesService service = internalCluster().getInstance(RepositoriesService.class, internalCluster().getMasterName());
133+
final BlobStoreRepository repository = (BlobStoreRepository) service.repository(repoName);
134+
135+
logger.info("--> write two outdated index-N blobs");
136+
for (int i = 0; i < 2; ++i) {
137+
final PlainActionFuture<Void> createOldIndexNFuture = PlainActionFuture.newFuture();
138+
final int generation = i;
139+
repository.threadPool().generic().execute(ActionRunnable.run(createOldIndexNFuture, () -> repository.blobStore()
140+
.blobContainer(repository.basePath()).writeBlob(BlobStoreRepository.INDEX_FILE_PREFIX + generation,
141+
new ByteArrayInputStream(new byte[1]), 1, true)));
142+
createOldIndexNFuture.get();
143+
}
144+
145+
logger.info("--> cleanup repository");
146+
client().admin().cluster().prepareCleanupRepository(repoName).get();
147+
148+
BlobStoreTestUtil.assertConsistency(repository, repository.threadPool().generic());
149+
}
110150
}

0 commit comments

Comments
 (0)