Skip to content

Commit f81a5ad

Browse files
authored
---
yaml --- r: 28223 b: refs/heads/autosynth-spanner c: 909b0e8 h: refs/heads/master i: 28221: 3717da9 28219: e506c94 28215: 1bad696 28207: 71cdd70 28191: 2c103a6 28159: 5447bc8
1 parent 8f26e28 commit f81a5ad

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

  • branches/autosynth-spanner/google-cloud-clients/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ refs/heads/autosynth-language: e73905aa7672afa47240e65b25c087207f4594f9
137137
refs/heads/autosynth-os-login: 123ba209c5769d0ee067e0ce5848bec13b42a4f4
138138
refs/heads/autosynth-redis: 6bedce4d7c7c6ca6a22e83ad1780e08fdc565a9e
139139
refs/heads/autosynth-scheduler: 57f9fdb1e7de30c85f4ec7198931a07f50603e55
140-
refs/heads/autosynth-spanner: e67fd018a79de01181f1806a4597055d5d2104bb
140+
refs/heads/autosynth-spanner: 909b0e8ef2d87a5d75da3ebfb595868a94645aae
141141
refs/heads/autosynth-speech: 64692f6db11364f663921be02c08072b966b6e7b
142142
refs/heads/autosynth-tasks: eb03eeab747e925175890db923945384d89b273a
143143
refs/heads/autosynth-texttospeech: 2c442fe0b7f089fbab266edfe4dd83c532e82dd0

branches/autosynth-spanner/google-cloud-clients/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1/Publisher.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,23 +197,22 @@ public ApiFuture<String> publish(PubsubMessage message) {
197197
}
198198

199199
message = messageTransform.apply(message);
200-
final int messageSize = message.getSerializedSize();
201200
OutstandingBatch batchToSend = null;
202-
SettableApiFuture<String> publishResult = SettableApiFuture.<String>create();
203-
final OutstandingPublish outstandingPublish = new OutstandingPublish(publishResult, message);
201+
final OutstandingPublish outstandingPublish = new OutstandingPublish(message);
204202
messagesBatchLock.lock();
205203
try {
206204
// Check if the next message makes the current batch exceed the max batch byte size.
207205
if (!messagesBatch.isEmpty()
208206
&& hasBatchingBytes()
209-
&& messagesBatch.getBatchedBytes() + messageSize >= getMaxBatchBytes()) {
207+
&& messagesBatch.getBatchedBytes() + outstandingPublish.messageSize
208+
>= getMaxBatchBytes()) {
210209
batchToSend = messagesBatch.popOutstandingBatch();
211210
}
212211

213212
// Border case if the message to send is greater or equals to the max batch size then can't
214213
// be included in the current batch and instead sent immediately.
215-
if (!hasBatchingBytes() || messageSize < getMaxBatchBytes()) {
216-
messagesBatch.addMessage(outstandingPublish, messageSize);
214+
if (!hasBatchingBytes() || outstandingPublish.messageSize < getMaxBatchBytes()) {
215+
messagesBatch.addMessage(outstandingPublish, outstandingPublish.messageSize);
217216

218217
// If after adding the message we have reached the batch max messages then we have a batch
219218
// to send.
@@ -243,20 +242,21 @@ public void run() {
243242

244243
// If the message is over the size limit, it was not added to the pending messages and it will
245244
// be sent in its own batch immediately.
246-
if (hasBatchingBytes() && messageSize >= getMaxBatchBytes()) {
245+
if (hasBatchingBytes() && outstandingPublish.messageSize >= getMaxBatchBytes()) {
247246
logger.log(
248247
Level.FINER, "Message exceeds the max batch bytes, scheduling it for immediate send.");
249248
executor.execute(
250249
new Runnable() {
251250
@Override
252251
public void run() {
253252
publishOutstandingBatch(
254-
new OutstandingBatch(ImmutableList.of(outstandingPublish), messageSize));
253+
new OutstandingBatch(
254+
ImmutableList.of(outstandingPublish), outstandingPublish.messageSize));
255255
}
256256
});
257257
}
258258

259-
return publishResult;
259+
return outstandingPublish.publishResult;
260260
}
261261

262262
private void setupAlarm() {
@@ -382,12 +382,14 @@ public int size() {
382382
}
383383

384384
private static final class OutstandingPublish {
385-
SettableApiFuture<String> publishResult;
386-
PubsubMessage message;
385+
final SettableApiFuture<String> publishResult;
386+
final PubsubMessage message;
387+
final int messageSize;
387388

388-
OutstandingPublish(SettableApiFuture<String> publishResult, PubsubMessage message) {
389-
this.publishResult = publishResult;
389+
OutstandingPublish(PubsubMessage message) {
390+
this.publishResult = SettableApiFuture.create();
390391
this.message = message;
392+
this.messageSize = message.getSerializedSize();
391393
}
392394
}
393395

0 commit comments

Comments
 (0)