@@ -220,7 +220,6 @@ public ApiFuture<String> publish(PubsubMessage message) {
220220 List <OutstandingBatch > batchesToSend ;
221221 messagesBatchLock .lock ();
222222 try {
223- // Check if the next message makes the current batch exceed the max batch byte size.
224223 MessagesBatch messagesBatch = messagesBatches .get (orderingKey );
225224 if (messagesBatch == null ) {
226225 messagesBatch = new MessagesBatch (batchingSettings , orderingKey );
@@ -232,23 +231,21 @@ public ApiFuture<String> publish(PubsubMessage message) {
232231 messagesBatches .remove (orderingKey );
233232 }
234233 setupAlarm ();
234+ if (!batchesToSend .isEmpty ()) {
235+ // TODO: if this is not an ordering keys scenario, will this do anything?
236+ publishAllWithoutInflight ();
237+
238+ // TODO: if this is an ordering keys scenario, is this safe without messagesBatchLock?
239+ for (final OutstandingBatch batch : batchesToSend ) {
240+ logger .log (Level .FINER , "Scheduling a batch for immediate sending." );
241+ publishOutstandingBatch (batch );
242+ }
243+ }
235244 } finally {
236245 messagesBatchLock .unlock ();
237246 }
238247
239248 messagesWaiter .incrementPendingMessages (1 );
240-
241- if (!batchesToSend .isEmpty ()) {
242- // TODO: if this is not an ordering keys scenario, will this do anything?
243- publishAllWithoutInflight ();
244-
245- // TODO: if this is an ordering keys scenario, is this safe without messagesBatchLock?
246- for (final OutstandingBatch batch : batchesToSend ) {
247- logger .log (Level .FINER , "Scheduling a batch for immediate sending." );
248- publishOutstandingBatch (batch );
249- }
250- }
251-
252249 return outstandingPublish .publishResult ;
253250 }
254251
@@ -331,11 +328,6 @@ private void publishAllWithoutInflight() {
331328 if (batch .isEmpty ()) {
332329 it .remove ();
333330 } else if (key .isEmpty () || !sequentialExecutor .hasTasksInflight (key )) {
334- // TODO(kimkyung-goog): Do not release `messagesBatchLock` when publishing a batch. If
335- // it's released, the order of publishing cannot be guaranteed if `publish()` is called
336- // while this function is running. This locking mechanism needs to be improved if it
337- // causes any performance degradation.
338-
339331 // TODO: Will this cause a performance problem for non-ordering keys scenarios?
340332 publishOutstandingBatch (batch .popOutstandingBatch ());
341333 it .remove ();
0 commit comments