Skip to content

Commit

Permalink
(perf) server: batch SQL Metadata deleteSegments
Browse files Browse the repository at this point in the history
pr feedback
- improve logging accuracy
- restore missing newline
  • Loading branch information
jasonk000 committed Jul 23, 2023
1 parent 1f462ff commit d28f768
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1774,31 +1774,30 @@ public Void inTransaction(Handle handle, TransactionStatus transactionStatus) th
public void deleteSegments(final Set<DataSegment> segments)
{
if (segments.isEmpty()) {
log.info("Removed [0] segments from metadata storage for dataSource [\"\"]!");
log.info("No segments to delete.");
return;
}

final int segmentSize = segments.size();
final String deleteSql = StringUtils.format("DELETE from %s WHERE id = :id", dbTables.getSegmentsTable());
final String dataSource = segments.stream().findFirst().map(DataSegment::getDataSource).get();

// generate the IDs outside the transaction block
final List<String> ids = segments.stream().map(s -> s.getId().toString()).collect(Collectors.toList());

connector.getDBI().inTransaction((TransactionCallback<Void>) (handle, transactionStatus) -> {
int numDeletedSegments = connector.getDBI().inTransaction((handle, transactionStatus) -> {
final PreparedBatch batch = handle.prepareBatch(deleteSql);

for (final String id : ids) {
batch.bind("id", id).add();
}

batch.execute();
return null;
int[] deletedRows = batch.execute();
return Arrays.stream(deletedRows).sum();
}
);

log.debugSegments(segments, "Delete the metadata of segments");
log.info("Removed [%d] segments from metadata storage for dataSource [%s]!", segmentSize, dataSource);
log.info("Deleted [%d] segments from metadata storage for dataSource [%s].", numDeletedSegments, dataSource);
}

private void updatePayload(final Handle handle, final DataSegment segment) throws IOException
Expand All @@ -1817,6 +1816,7 @@ private void updatePayload(final Handle handle, final DataSegment segment) throw
throw e;
}
}

@Override
public boolean insertDataSourceMetadata(String dataSource, DataSourceMetadata metadata)
{
Expand Down

0 comments on commit d28f768

Please sign in to comment.