From 0287f83f9cabbb9dff6cb7e8ea3068ccc1ab12d9 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Wed, 19 Oct 2022 13:42:07 -0400 Subject: [PATCH] fix: scope the throttling metric to exclude element size calculation (#1835) This should prevent off by 1 failures in unit tests on slow vms --- .../main/java/com/google/api/gax/batching/BatcherImpl.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gax/src/main/java/com/google/api/gax/batching/BatcherImpl.java b/gax/src/main/java/com/google/api/gax/batching/BatcherImpl.java index d29e40d60..2118ae24b 100644 --- a/gax/src/main/java/com/google/api/gax/batching/BatcherImpl.java +++ b/gax/src/main/java/com/google/api/gax/batching/BatcherImpl.java @@ -213,6 +213,8 @@ public ApiFuture add(ElementT element) { // will only be done from a single calling thread. Preconditions.checkState(closeFuture == null, "Cannot add elements on a closed batcher"); + long bytesSize = batchingDescriptor.countBytes(element); + // This is not the optimal way of throttling. It does not send out partial batches, which // means that the Batcher might not use up all the resources allowed by FlowController. // The more efficient implementation should look like: @@ -230,7 +232,7 @@ public ApiFuture add(ElementT element) { // defer it till we decide on if refactoring FlowController is necessary. Stopwatch stopwatch = Stopwatch.createStarted(); try { - flowController.reserve(1, batchingDescriptor.countBytes(element)); + flowController.reserve(1, bytesSize); } catch (FlowControlException e) { // This exception will only be thrown if the FlowController is set to ThrowException behavior throw FlowControlRuntimeException.fromFlowControlException(e);