Skip to content

Commit

Permalink
atlas: avoid intermediate list for LWC batches (#1124)
Browse files Browse the repository at this point in the history
  • Loading branch information
brharrington authored Mar 14, 2024
1 parent 5a6c9ba commit 48bfb38
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,7 @@ void sendToLWC() {
EvalPayload payload = evaluator.eval(t);
if (!payload.getMetrics().isEmpty()) {
List<CompletableFuture<Void>> futures = new ArrayList<>();
for (EvalPayload batch : payload.toBatches(batchSize)) {
CompletableFuture<Void> future = publisher.publish(batch);
futures.add(future);
}
payload.consumeBatches(batchSize, p -> futures.add(publisher.publish(p)));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
* Wraps a list of measurements with a set of common tags. The common tags are
Expand Down Expand Up @@ -70,18 +71,31 @@ public List<Message> getMessages() {
* List of payloads that have at most {@code batchSize} metrics per payload.
*/
public List<EvalPayload> toBatches(int batchSize) {
List<EvalPayload> payloads = new ArrayList<>(metrics.size() / batchSize + 1);
consumeBatches(batchSize, payloads::add);
return payloads;
}

/**
* Break the payload down to a set of batches to limit the size of requests going to the
* service.
*
* @param batchSize
* Size of the metric batches to create.
* @param consumer
* Consumer to receive an eval payload batch.
*/
public void consumeBatches(int batchSize, Consumer<EvalPayload> consumer) {
int size = metrics.size();
if (size <= batchSize) {
return Collections.singletonList(this);
consumer.accept(this);
} else {
List<EvalPayload> payloads = new ArrayList<>(size / batchSize + 1);
for (int i = 0; i < size; i += batchSize) {
List<Metric> batch = metrics.subList(i, Math.min(size, i + batchSize));
// There shouldn't be many messages, stick in the first batch
List<Message> msgs = (i == 0) ? messages : Collections.emptyList();
payloads.add(new EvalPayload(timestamp, batch, msgs));
consumer.accept(new EvalPayload(timestamp, batch, msgs));
}
return payloads;
}
}

Expand Down

0 comments on commit 48bfb38

Please sign in to comment.