Skip to content

Commit

Permalink
fix: unique index conflict issue after backup restoration preventing …
Browse files Browse the repository at this point in the history
…startup
  • Loading branch information
guqing committed Sep 25, 2024
1 parent f6409a0 commit adf8c10
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,14 @@ public Mono<Void> restore(Publisher<DataBuffer> content) {
return Mono.usingWhen(
createTempDir("halo-restore-", scheduler),
tempDir -> unpackBackup(content, tempDir)
.then(Mono.defer(() -> restoreExtensions(tempDir)))
.then(Mono.defer(() ->
// This step skips index verification such as unique index.
// In order to avoid index conflicts after recovery or
// OptimisticLockingFailureException when updating the same record,
// so we need to truncate all extension stores before saving(create or update).
repository.deleteAll()
.then(restoreExtensions(tempDir)))
)
.then(Mono.defer(() -> restoreWorkdir(tempDir))),
tempDir -> deleteRecursivelyAndSilently(tempDir, scheduler)
);
Expand Down Expand Up @@ -239,13 +246,9 @@ private Mono<Void> restoreExtensions(Path backupRoot) {
sink.complete();
})
// reset version
.doOnNext(extensionStore -> extensionStore.setVersion(null)).buffer(100)
// We might encounter OptimisticLockingFailureException when saving extension
// store,
// So we have to delete all extension stores before saving.
.flatMap(extensionStores -> repository.deleteAll(extensionStores)
.thenMany(repository.saveAll(extensionStores))
)
.doOnNext(extensionStore -> extensionStore.setVersion(null))
.buffer(100)
.flatMap(repository::saveAll)
.doOnNext(extensionStore -> log.info("Restored extension store: {}",
extensionStore.getName()))
.then(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void restoreTest() throws IOException, URISyntaxException {
expectStore.setVersion(null);

when(haloProperties.getWorkDir()).thenReturn(workdir);
when(repository.deleteAll(List.of(expectStore))).thenReturn(Mono.empty());
when(repository.deleteAll()).thenReturn(Mono.empty());
when(repository.saveAll(List.of(expectStore))).thenReturn(Flux.empty());

var content = DataBufferUtils.read(backupFile,
Expand All @@ -132,7 +132,7 @@ void restoreTest() throws IOException, URISyntaxException {


verify(haloProperties).getWorkDir();
verify(repository).deleteAll(List.of(expectStore));
verify(repository).deleteAll();
verify(repository).saveAll(List.of(expectStore));

// make sure the workdir is recovered.
Expand Down

0 comments on commit adf8c10

Please sign in to comment.