Skip to content

Commit

Permalink
Add document counts to migrations (#3781)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: David Gauldie <[email protected]>
  • Loading branch information
3 people authored Jun 7, 2024
1 parent 9343cda commit b040684
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public enum MigrationState {

private MigrationState state;
private Timestamp timestamp;

private Integer totalDocuments;
private Integer successfulDocuments;
private Integer failedDocuments;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import software.uncharted.terarium.hmiserver.configuration.ElasticsearchConfiguration;
Expand Down Expand Up @@ -73,14 +74,14 @@ private static class MigrationConfig<T extends TerariumAsset, R extends PSCrudSo
this.index = index;
}

void migrateFromEsToPg(final ElasticsearchService elasticService) throws IOException {
Pair<Integer, Integer> migrateFromEsToPg(final ElasticsearchService elasticService) throws IOException {
// check if there is a target index to migrate from
if (!elasticService.indexExists(index)) {
return;
return Pair.of(0, 0);
}

if (elasticService.count(index) == 0) {
return;
return Pair.of(0, 0);
}

// check if the data has already been migrated
Expand All @@ -92,6 +93,9 @@ void migrateFromEsToPg(final ElasticsearchService elasticService) throws IOExcep

final String SORT_FIELD = "createdOn";

Integer totalSuccess = 0;
Integer totalFailed = 0;

while (true) {
final SearchRequest.Builder reqBuilder = new SearchRequest.Builder()
.index(index)
Expand Down Expand Up @@ -132,13 +136,15 @@ void migrateFromEsToPg(final ElasticsearchService elasticService) throws IOExcep
for (final T asset : assets) {
try {
service.getRepository().save(asset);
totalSuccess++;
} catch (final Exception e) {
log.warn("Failed to insert id: {}", asset.getId(), e);
failed += 1;
totalFailed++;
}
}
if (failed == assets.size()) {
throw new RuntimeException("All assets faield to insert");
throw new RuntimeException("All assets failed to insert");
}
}

Expand All @@ -150,6 +156,8 @@ void migrateFromEsToPg(final ElasticsearchService elasticService) throws IOExcep
// over and over
lastPagesLastId = lastId;
}

return Pair.of(totalSuccess, totalFailed);
}
}

Expand Down Expand Up @@ -180,12 +188,15 @@ void runMigrations() {
continue;
}

migration.migrateFromEsToPg(elasticService);
final Pair<Integer, Integer> res = migration.migrateFromEsToPg(elasticService);

migrationRecord = new DataMigration();
migrationRecord.setTableName(tableName);
migrationRecord.setTimestamp(new Timestamp(System.currentTimeMillis()));
migrationRecord.setState(MigrationState.SUCCESS);
migrationRecord.setTotalDocuments(res.getLeft() + res.getRight());
migrationRecord.setSuccessfulDocuments(res.getLeft());
migrationRecord.setFailedDocuments(res.getRight());
migrationRepository.save(migrationRecord);

log.info("Migrated ES index {} to PG table {} successfully", migration.getIndex(), tableName);
Expand Down

0 comments on commit b040684

Please sign in to comment.