Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Introducing auto/timed flush for new Batcher.#744

Merged
igorbernstein2 merged 1 commit intogoogleapis:masterfrom
rahulKQL:delay_threshold
Jul 19, 2019
Merged

Introducing auto/timed flush for new Batcher.#744
igorbernstein2 merged 1 commit intogoogleapis:masterfrom
rahulKQL:delay_threshold

Conversation

@rahulKQL
Copy link
Copy Markdown
Contributor

This is related to new Batching API and is a followUp PR after #734

This change contains:

  • DelayThreshold in BatchingSettings without any default delay value.
  • BatcherImpl now accepts user-provided scheduledExecutor, which auto triggers sendBatch() after a given delay.
  • Javadoc & test case for the same.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Jun 25, 2019
@rahulKQL
Copy link
Copy Markdown
Contributor Author

@igorbernstein2 Please have a look

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 25, 2019

Codecov Report

Merging #744 into master will increase coverage by 0.27%.
The diff coverage is 97.05%.

Impacted file tree graph

@@            Coverage Diff             @@
##             master   #744      +/-   ##
==========================================
+ Coverage     77.72%    78%   +0.27%     
- Complexity     1094   1100       +6     
==========================================
  Files           198    198              
  Lines          4799   4823      +24     
  Branches        377    382       +5     
==========================================
+ Hits           3730   3762      +32     
+ Misses          898    889       -9     
- Partials        171    172       +1
Impacted Files Coverage Δ Complexity Δ
...va/com/google/api/gax/batching/v2/BatcherImpl.java 94.68% <100%> (+1%) 12 <0> (+1) ⬆️
...m/google/api/gax/batching/v2/BatchingSettings.java 75% <50%> (-5%) 2 <0> (ø)
...rc/main/java/com/google/api/gax/rpc/Callables.java 67.85% <0%> (+0.51%) 12% <0%> (+2%) ⬆️
...a/com/google/api/gax/grpc/GrpcCallableFactory.java 82.5% <0%> (+3.75%) 11% <0%> (+1%) ⬆️
.../grpc/GrpcServerStreamingRequestParamCallable.java 80% <0%> (+80%) 2% <0%> (+2%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9b3357c...9564189. Read the comment docs.

@igorbernstein2 igorbernstein2 added the needs work This is a pull request that needs a little love. label Jun 25, 2019
Copy link
Copy Markdown
Contributor

@igorbernstein2 igorbernstein2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a bit of work. Before updating the code, lets talk through how to address the issues.

The biggest issue here is the race condition between the scheduled flush thread and the caller's thread. The scheduled flush thread can interleave with the caller adding an element and the caller triggering a flush. This can lead to data loss.

Another issue is that scheduling the a flush for each element adds unnecessary overhead. I think it might better to track last flush time and have a single recurring task per batcher rather then per element.

Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
@codecov-io
Copy link
Copy Markdown

codecov-io commented Jul 2, 2019

Codecov Report

Merging #744 into master will increase coverage by 0.2%.
The diff coverage is 97.05%.

Impacted file tree graph

@@             Coverage Diff             @@
##             master     #744     +/-   ##
===========================================
+ Coverage     77.94%   78.14%   +0.2%     
- Complexity     1099     1101      +2     
===========================================
  Files           198      198             
  Lines          4806     4832     +26     
  Branches        379      383      +4     
===========================================
+ Hits           3746     3776     +30     
+ Misses          889      886      -3     
+ Partials        171      170      -1
Impacted Files Coverage Δ Complexity Δ
...va/com/google/api/gax/batching/v2/BatcherImpl.java 100% <100%> (+6.32%) 13 <4> (+2) ⬆️
...m/google/api/gax/batching/v2/BatchingSettings.java 75% <50%> (-5%) 2 <0> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5fd82c7...7c6462e. Read the comment docs.

@codecov-io
Copy link
Copy Markdown

Codecov Report

Merging #744 into master will increase coverage by 0.27%.
The diff coverage is 97.05%.

Impacted file tree graph

@@            Coverage Diff             @@
##             master   #744      +/-   ##
==========================================
+ Coverage     77.72%    78%   +0.27%     
- Complexity     1094   1100       +6     
==========================================
  Files           198    198              
  Lines          4799   4823      +24     
  Branches        377    382       +5     
==========================================
+ Hits           3730   3762      +32     
+ Misses          898    889       -9     
- Partials        171    172       +1
Impacted Files Coverage Δ Complexity Δ
...va/com/google/api/gax/batching/v2/BatcherImpl.java 94.68% <100%> (+1%) 12 <0> (+1) ⬆️
...m/google/api/gax/batching/v2/BatchingSettings.java 75% <50%> (-5%) 2 <0> (ø)
...rc/main/java/com/google/api/gax/rpc/Callables.java 67.85% <0%> (+0.51%) 12% <0%> (+2%) ⬆️
...a/com/google/api/gax/grpc/GrpcCallableFactory.java 82.5% <0%> (+3.75%) 11% <0%> (+1%) ⬆️
.../grpc/GrpcServerStreamingRequestParamCallable.java 80% <0%> (+80%) 2% <0%> (+2%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9b3357c...d4481b6. Read the comment docs.

Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
Comment thread gax/src/test/java/com/google/api/gax/batching/v2/BatcherImplTest.java Outdated
Comment thread gax/src/test/java/com/google/api/gax/batching/v2/BatcherImplTest.java Outdated
Comment thread gax/src/test/java/com/google/api/gax/batching/v2/BatcherImplTest.java Outdated
Comment thread gax/src/test/java/com/google/api/gax/batching/v2/BatcherImplTest.java Outdated
Comment thread gax/src/test/java/com/google/api/gax/batching/v2/BatcherImplTest.java Outdated
Copy link
Copy Markdown
Contributor

@igorbernstein2 igorbernstein2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits, but LGTM overall @vam-google, can you take a pass?

Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
Comment thread gax/src/test/java/com/google/api/gax/batching/v2/BatcherImplTest.java Outdated
Comment thread gax/src/test/java/com/google/api/gax/batching/v2/BatcherImplTest.java Outdated
Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java
Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java
Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
Comment thread gax/src/main/java/com/google/api/gax/batching/v2/BatcherImpl.java Outdated
This PR adds DelayThreshold in Batcher without any default delay value. DelayThreshold cannot be less than 1 as 0 or less throws IllegalStateEx.

 - Accepting user provided executor to run the auto-flush.
 - Ongoing Runnable will be unscheduled after Batcher is GCed(PushCurrentBatchRunnable).
 - Added `elementLock` to skip race condition between user thread and gax-thread.
 - Empty batches are never sent to UnaryCallable
 - Added java docs for Delay threshold

**Test to verify:**
 - Auto flush and blocking flush.
 - Elements are not leaking to more than one batch even if concurrent flushing happens.
 - Empty batches are never sent.
 - Refactored Mockito based test for Exception in the descriptor.

Feedback Updates

 - Fix test case for blocking flush
 - Removed testBlockingFlush by dividing it to two individual test case.
 - Updated Java docs according to feedback
Copy link
Copy Markdown
Contributor

@igorbernstein2 igorbernstein2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me

@Override
public void close() throws InterruptedException {
isClosed = true;
if (isClosed) return;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@igorbernstein2
Copy link
Copy Markdown
Contributor

I'm going to merge this for now to unblock further work.
When @vam-google gets back from vacation we can circle back to this and improve it

@igorbernstein2 igorbernstein2 merged commit 5ad6fee into googleapis:master Jul 19, 2019
This was referenced Aug 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla: yes This human has signed the Contributor License Agreement. needs work This is a pull request that needs a little love.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants