Skip to content

Commit 35be8d1

Browse files
feat: Add support for batching configuration (#1164)
* feat: Add support for batching configuration * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Remove redundant batchingSettings.toBuilder().build() and rename to defaultBatchSettings Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent f6c4ebe commit 35be8d1

File tree

5 files changed

+163
-10
lines changed

5 files changed

+163
-10
lines changed

‎.readme-partials.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,38 @@ custom_content: |
9191
> may lead to unexpected results such as absense of expected log entries or abnormal program execution.
9292
> To avoid these unexpected results, it is recommended to use synchronous mode.
9393
94+
#### Controlling the batching settings
95+
As mentioned before, in the asynchronous mode the call(s) to Logging API takes place asynchronously and few calls to `write()`
96+
method may be batched together to compose a single call to Logging API. In order to control the batching settings, the `LoggingOptions`
97+
is enhanced with `BatchingSettings` which can be set as shown in example below:
98+
99+
```java
100+
import com.google.api.gax.batching.BatchingSettings;
101+
import com.google.api.gax.batching.FlowControlSettings;
102+
import com.google.api.gax.batching.FlowController;
103+
104+
LoggingOptions actual =
105+
LoggingOptions.newBuilder()
106+
.setBatchingSettings(
107+
BatchingSettings.newBuilder()
108+
.setIsEnabled(true)
109+
.setElementCountThreshold(1000L)
110+
.setRequestByteThreshold(1048576L)
111+
.setDelayThreshold(Duration.ofMillis(50L))
112+
.setFlowControlSettings(
113+
FlowControlSettings.newBuilder()
114+
.setMaxOutstandingElementCount(100000L)
115+
.setMaxOutstandingRequestBytes(10485760L)
116+
.setLimitExceededBehavior(
117+
FlowController.LimitExceededBehavior.ThrowException)
118+
.build())
119+
.build())
120+
.setProjectId('Your project ID')
121+
.build();
122+
```
123+
124+
You can find more information about batching parameters see [BatchingSettings](https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.batching.BatchingSettings).
125+
94126
#### Listing log entries
95127
96128
With Logging you can also list log entries that have been previously written. Add the following

‎README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,38 @@ NOTE:
191191
> may lead to unexpected results such as absense of expected log entries or abnormal program execution.
192192
> To avoid these unexpected results, it is recommended to use synchronous mode.
193193
194+
#### Controlling the batching settings
195+
As mentioned before, in the asynchronous mode the call(s) to Logging API takes place asynchronously and few calls to `write()`
196+
method may be batched together to compose a single call to Logging API. In order to control the batching settings, the `LoggingOptions`
197+
is enhanced with `BatchingSettings` which can be set as shown in example below:
198+
199+
```java
200+
import com.google.api.gax.batching.BatchingSettings;
201+
import com.google.api.gax.batching.FlowControlSettings;
202+
import com.google.api.gax.batching.FlowController;
203+
204+
LoggingOptions actual =
205+
LoggingOptions.newBuilder()
206+
.setBatchingSettings(
207+
BatchingSettings.newBuilder()
208+
.setIsEnabled(true)
209+
.setElementCountThreshold(1000L)
210+
.setRequestByteThreshold(1048576L)
211+
.setDelayThreshold(Duration.ofMillis(50L))
212+
.setFlowControlSettings(
213+
FlowControlSettings.newBuilder()
214+
.setMaxOutstandingElementCount(100000L)
215+
.setMaxOutstandingRequestBytes(10485760L)
216+
.setLimitExceededBehavior(
217+
FlowController.LimitExceededBehavior.ThrowException)
218+
.build())
219+
.build())
220+
.setProjectId('Your project ID')
221+
.build();
222+
```
223+
224+
You can find more information about batching parameters see [BatchingSettings](https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.batching.BatchingSettings).
225+
194226
#### Listing log entries
195227

196228
With Logging you can also list log entries that have been previously written. Add the following

‎google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingOptions.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.logging;
1818

1919
import com.google.api.core.InternalApi;
20+
import com.google.api.gax.batching.BatchingSettings;
2021
import com.google.cloud.ServiceDefaults;
2122
import com.google.cloud.ServiceOptions;
2223
import com.google.cloud.ServiceRpc;
@@ -40,6 +41,7 @@ public class LoggingOptions extends ServiceOptions<Logging, LoggingOptions> {
4041
private static final long serialVersionUID = 5753499510627426717L;
4142

4243
private Boolean autoPopulateMetadataOnWrite = null;
44+
private BatchingSettings batchingSettings = null;
4345

4446
public static class DefaultLoggingFactory implements LoggingFactory {
4547
private static final LoggingFactory INSTANCE = new DefaultLoggingFactory();
@@ -76,6 +78,7 @@ protected String getDefaultHost() {
7678
public static class Builder extends ServiceOptions.Builder<Logging, LoggingOptions, Builder> {
7779

7880
private Boolean autoPopulateMetadataOnWrite = true;
81+
private BatchingSettings batchingSettings = null;
7982

8083
private Builder() {}
8184

@@ -98,6 +101,12 @@ public Builder setAutoPopulateMetadata(boolean autoPopulateMetadataOnWrite) {
98101
return this;
99102
}
100103

104+
@CanIgnoreReturnValue
105+
public Builder setBatchingSettings(BatchingSettings batchingSettings) {
106+
this.batchingSettings = batchingSettings;
107+
return this;
108+
}
109+
101110
@Override
102111
public LoggingOptions build() {
103112
return new LoggingOptions(this);
@@ -108,6 +117,8 @@ public LoggingOptions build() {
108117
protected LoggingOptions(Builder builder) {
109118
super(LoggingFactory.class, LoggingRpcFactory.class, builder, new LoggingDefaults());
110119
this.autoPopulateMetadataOnWrite = builder.autoPopulateMetadataOnWrite;
120+
this.batchingSettings =
121+
builder.batchingSettings == null ? null : builder.batchingSettings.toBuilder().build();
111122
}
112123

113124
@SuppressWarnings("serial")
@@ -146,6 +157,10 @@ public Boolean getAutoPopulateMetadata() {
146157
return this.autoPopulateMetadataOnWrite;
147158
}
148159

160+
public BatchingSettings getBatchingSettings() {
161+
return this.batchingSettings;
162+
}
163+
149164
@Override
150165
public boolean equals(Object obj) {
151166
return obj instanceof LoggingOptions && baseEquals((LoggingOptions) obj);

‎google-cloud-logging/src/main/java/com/google/cloud/logging/spi/v2/GrpcLoggingRpc.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,27 @@ public Void apply(UnaryCallSettings.Builder<?, ?> builder) {
167167
// TODO(pongad): Take advantage of
168168
// https://github.com/googleapis/gax-java/pull/452 when it's
169169
// released.
170-
BatchingSettings oldBatchSettings =
170+
BatchingSettings defaultBatchSettings =
171171
logBuilder.writeLogEntriesSettings().getBatchingSettings();
172+
173+
// The BatchingSettings from LoggingOptions should override
174+
// ones provided in defaultBatchSettings
175+
BatchingSettings batchingSettings = options.getBatchingSettings();
176+
172177
logBuilder
173178
.writeLogEntriesSettings()
174179
.setBatchingSettings(
175-
oldBatchSettings
176-
.toBuilder()
177-
.setFlowControlSettings(
178-
oldBatchSettings
179-
.getFlowControlSettings()
180-
.toBuilder()
181-
.setLimitExceededBehavior(LimitExceededBehavior.Block)
182-
.build())
183-
.build());
180+
batchingSettings != null
181+
? batchingSettings
182+
: defaultBatchSettings
183+
.toBuilder()
184+
.setFlowControlSettings(
185+
defaultBatchSettings
186+
.getFlowControlSettings()
187+
.toBuilder()
188+
.setLimitExceededBehavior(LimitExceededBehavior.Block)
189+
.build())
190+
.build());
184191

185192
configClient = ConfigClient.create(confBuilder.build());
186193
loggingClient = LoggingClient.create(logBuilder.build());

‎google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingOptionsTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,29 @@
1818

1919
import static org.easymock.EasyMock.createMock;
2020
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertNull;
2123
import static org.junit.Assert.assertThrows;
2224

25+
import com.google.api.gax.batching.BatchingSettings;
26+
import com.google.api.gax.batching.FlowControlSettings;
27+
import com.google.api.gax.batching.FlowController;
28+
import com.google.cloud.NoCredentials;
2329
import com.google.cloud.TransportOptions;
2430
import org.junit.Test;
2531
import org.junit.runner.RunWith;
2632
import org.junit.runners.JUnit4;
33+
import org.threeten.bp.Duration;
2734

2835
@RunWith(JUnit4.class)
2936
public class LoggingOptionsTest {
3037
private static final Boolean DONT_AUTO_POPULATE_METADATA = false;
3138
private static final String PROJECT_ID = "fake-project-id";
39+
private static final Long ELEMENTS_TRESHOLD_COUNT = 100L;
40+
private static final Long REQUEST_BYTE_TRESHOLD_COUNT = 10485760L;
41+
private static final long DURATION = 501L;
42+
private static final Long MAX_OUTSTANDING_ELEMENTS_COUNT = 1000001L;
43+
private static final Long MAX_OUTSTANDING_REQUEST_BYTES_COUNT = 104857601L;
3244

3345
@Test
3446
public void testNonGrpcTransportOptions() {
@@ -53,4 +65,59 @@ public void testAutoPopulateMetadataDefaultOption() {
5365
LoggingOptions actual = LoggingOptions.newBuilder().setProjectId(PROJECT_ID).build();
5466
assertEquals(Boolean.TRUE, actual.getAutoPopulateMetadata());
5567
}
68+
69+
@Test
70+
public void testBatchingSettingsDefaultOption() {
71+
LoggingOptions actual = LoggingOptions.newBuilder().setProjectId(PROJECT_ID).build();
72+
assertNull("Batching settings should be null by default!", actual.getBatchingSettings());
73+
}
74+
75+
@Test
76+
public void testBatchingSettingsOption() {
77+
verifyBatchingSettings(generateLoggingOptions().getBatchingSettings());
78+
}
79+
80+
@Test
81+
public void testBatchingSettingsOptionWithGrpc() {
82+
verifyBatchingSettings(
83+
generateLoggingOptions().getService().getOptions().getBatchingSettings());
84+
}
85+
86+
private static LoggingOptions generateLoggingOptions() {
87+
return LoggingOptions.newBuilder()
88+
.setBatchingSettings(
89+
BatchingSettings.newBuilder()
90+
.setIsEnabled(true)
91+
.setElementCountThreshold(ELEMENTS_TRESHOLD_COUNT)
92+
.setRequestByteThreshold(REQUEST_BYTE_TRESHOLD_COUNT)
93+
.setDelayThreshold(Duration.ofMillis(DURATION))
94+
.setFlowControlSettings(
95+
FlowControlSettings.newBuilder()
96+
.setMaxOutstandingElementCount(MAX_OUTSTANDING_ELEMENTS_COUNT)
97+
.setMaxOutstandingRequestBytes(MAX_OUTSTANDING_REQUEST_BYTES_COUNT)
98+
.setLimitExceededBehavior(
99+
FlowController.LimitExceededBehavior.ThrowException)
100+
.build())
101+
.build())
102+
.setProjectId(PROJECT_ID)
103+
.setCredentials(NoCredentials.getInstance())
104+
.build();
105+
}
106+
107+
private static void verifyBatchingSettings(BatchingSettings settings) {
108+
assertEquals(true, settings.getIsEnabled());
109+
assertEquals(ELEMENTS_TRESHOLD_COUNT, settings.getElementCountThreshold());
110+
assertEquals(REQUEST_BYTE_TRESHOLD_COUNT, settings.getRequestByteThreshold());
111+
assertNotNull(settings.getDelayThreshold());
112+
assertEquals(DURATION, settings.getDelayThreshold().toMillis());
113+
assertEquals(
114+
MAX_OUTSTANDING_ELEMENTS_COUNT,
115+
settings.getFlowControlSettings().getMaxOutstandingElementCount());
116+
assertEquals(
117+
MAX_OUTSTANDING_REQUEST_BYTES_COUNT,
118+
settings.getFlowControlSettings().getMaxOutstandingRequestBytes());
119+
assertEquals(
120+
FlowController.LimitExceededBehavior.ThrowException,
121+
settings.getFlowControlSettings().getLimitExceededBehavior());
122+
}
56123
}

0 commit comments

Comments
 (0)