Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure BigTable client to use MAX_BULK_READS #7548

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ public static void configureDataClient(BigtableDataSettings.Builder settings) {
.setElementCountThreshold((long) BigTableConstants.MAX_BULK_MUTATIONS)
.build());

stubSettings
.bulkReadRowsSettings()
.setBatchingSettings(
stubSettings.bulkReadRowsSettings().getBatchingSettings().toBuilder()
.setElementCountThreshold((long) BigTableConstants.MAX_BULK_READS)
.build());

// Enable tracing & metrics
BigtableDataSettings.enableOpenCensusStats();
BigtableDataSettings.enableGfeOpenCensusStats();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static org.projectnessie.versioned.storage.bigtable.BigTableConstants.CELL_TIMESTAMP;
import static org.projectnessie.versioned.storage.bigtable.BigTableConstants.FAMILY_OBJS;
import static org.projectnessie.versioned.storage.bigtable.BigTableConstants.FAMILY_REFS;
import static org.projectnessie.versioned.storage.bigtable.BigTableConstants.MAX_BULK_READS;
import static org.projectnessie.versioned.storage.bigtable.BigTableConstants.MAX_PARALLEL_READS;
import static org.projectnessie.versioned.storage.bigtable.BigTableConstants.QUALIFIER_OBJS;
import static org.projectnessie.versioned.storage.bigtable.BigTableConstants.QUALIFIER_OBJ_TYPE;
Expand Down Expand Up @@ -669,81 +668,40 @@ private <ID, R> void bulkFetch(
return;
}

ApiFuture<Row>[] handles;
if (num <= MAX_PARALLEL_READS) {
bulkFetchSome(tableId, ids, r, keyGen, resultGen, notFound);
handles = doBulkFetch(ids, keyGen, key -> backend.client().readRowAsync(tableId, key));
} else {
bulkFetchMany(tableId, ids, r, keyGen, resultGen, notFound);
try (Batcher<ByteString, Row> batcher = backend.client().newBulkReadRowsBatcher(tableId)) {
handles = doBulkFetch(ids, keyGen, batcher::add);
}
}
}

private <ID, R> void bulkFetchSome(
String tableId,
ID[] ids,
R[] r,
Function<ID, ByteString> keyGen,
Function<Row, R> resultGen,
Consumer<ID> notFound)
throws InterruptedException, ExecutionException, TimeoutException {
int num = ids.length;
@SuppressWarnings("unchecked")
ApiFuture<Row>[] handles = new ApiFuture[num];
for (int idx = 0; idx < num; idx++) {
ID id = ids[idx];
if (id != null) {
ByteString key = keyGen.apply(id);
handles[idx] = backend.client().readRowAsync(tableId, key);
ApiFuture<Row> handle = handles[idx];
if (handle != null) {
Row row = handle.get(READ_TIMEOUT_MILLIS, MILLISECONDS);
if (row != null) {
r[idx] = resultGen.apply(row);
} else {
notFound.accept(ids[idx]);
}
}
}

bulkFetchCollectResults(ids, r, resultGen, notFound, num, handles);
}

private <ID, R> void bulkFetchMany(
String tableId,
ID[] ids,
R[] r,
Function<ID, ByteString> keyGen,
Function<Row, R> resultGen,
Consumer<ID> notFound)
throws InterruptedException, ExecutionException, TimeoutException {
private <ID, R> ApiFuture<Row>[] doBulkFetch(
ID[] ids, Function<ID, ByteString> keyGen, Function<ByteString, ApiFuture<Row>> handleGen) {
int num = ids.length;
@SuppressWarnings("unchecked")
ApiFuture<Row>[] handles = new ApiFuture[num];
int idx = 0;
try (Batcher<ByteString, Row> batcher = backend.client().newBulkReadRowsBatcher(tableId)) {
for (int outer = 0; outer < num; outer += MAX_BULK_READS) {
for (int inner = outer; inner < outer + MAX_BULK_READS && inner < num; inner++) {
ID id = ids[inner];
if (id != null) {
ByteString key = keyGen.apply(id);
handles[idx] = batcher.add(key);
}
idx++;
}
}
}

bulkFetchCollectResults(ids, r, resultGen, notFound, num, handles);
}

private static <ID, R> void bulkFetchCollectResults(
ID[] ids,
R[] r,
Function<Row, R> resultGen,
Consumer<ID> notFound,
int num,
ApiFuture<Row>[] handles)
throws InterruptedException, ExecutionException, TimeoutException {
for (int i = 0; i < num; i++) {
ApiFuture<Row> handle = handles[i];
if (handle != null) {
Row row = handle.get(READ_TIMEOUT_MILLIS, MILLISECONDS);
if (row != null) {
r[i] = resultGen.apply(row);
} else {
notFound.accept(ids[i]);
}
for (int idx = 0; idx < num; idx++) {
ID id = ids[idx];
if (id != null) {
ByteString key = keyGen.apply(id);
handles[idx] = handleGen.apply(key);
}
}
return handles;
}
}