asafm commented on code in PR #16758:
URL: https://github.com/apache/pulsar/pull/16758#discussion_r939950792
##########
pulsar-transaction/coordinator/src/main/java/org/apache/pulsar/transaction/coordinator/impl/TxnLogBufferedWriter.java:
##########
@@ -198,33 +209,99 @@ public void asyncAddData(T data, AddDataCallback
callback, Object ctx){
AsyncAddArgs.newInstance(callback, ctx,
System.currentTimeMillis(), byteBuf));
return;
}
- singleThreadExecutorForWrite.execute(() -> internalAsyncAddData(data,
callback, ctx));
+ singleThreadExecutorForWrite.execute(() -> {
+ try {
+ internalAsyncAddData(data, callback, ctx);
+ } catch (Exception e){
+ log.error("Internal async add data fail", e);
+ }
+ });
}
+ /**
+ * Append data to queue, if reach {@link #batchedWriteMaxRecords} or
{@link #batchedWriteMaxSize}, do flush. And if
+ * accept a request that {@param data} is too large (larger than {@link
#batchedWriteMaxSize}), then two flushes
+ * are executed:
+ * 1. Write the data cached in the queue to BK.
+ * 2. Direct write the large data to BK, this flush event will not
record to Metrics.
+ * This ensures the sequential nature of multiple writes to BK.
+ */
private void internalAsyncAddData(T data, AddDataCallback callback, Object
ctx){
if (state == State.CLOSING || state == State.CLOSED){
callback.addFailed(BUFFERED_WRITER_CLOSED_EXCEPTION, ctx);
return;
}
- int len = dataSerializer.getSerializedSize(data);
- if (len >= batchedWriteMaxSize){
- if (!flushContext.asyncAddArgsList.isEmpty()) {
- doTrigFlush(true, false);
- }
+ int dataLength = dataSerializer.getSerializedSize(data);
+ if (dataLength >= batchedWriteMaxSize){
+ trigFlushByLargeSingleData();
ByteBuf byteBuf = dataSerializer.serialize(data);
managedLedger.asyncAddEntry(byteBuf,
DisabledBatchCallback.INSTANCE,
AsyncAddArgs.newInstance(callback, ctx,
System.currentTimeMillis(), byteBuf));
return;
}
- // Add data.
- this.dataArray.add(data);
- // Add callback info.
+ // Append data to the data-array.
+ dataArray.add(data);
+ // Append callback to the flushContext.
AsyncAddArgs asyncAddArgs = AsyncAddArgs.newInstance(callback, ctx,
System.currentTimeMillis());
- this.flushContext.asyncAddArgsList.add(asyncAddArgs);
- // Calculate bytes-size.
- this.bytesSize += len;
- // trig flush.
- doTrigFlush(false, false);
+ flushContext.asyncAddArgsList.add(asyncAddArgs);
+ // Calculate bytes size.
+ bytesSize += dataLength;
+ trigFlushIfReachMaxRecordsOrMaxSize();
+ }
+
+ /**
+ * Change to IO thread and do flush, only called by {@link
#timingFlushTask}.
+ */
+ private void trigFlushByTimingTask(){
+ singleThreadExecutorForWrite.execute(() -> {
+ try {
+ if (flushContext.asyncAddArgsList.isEmpty()) {
+ return;
+ }
+ if (metrics != null) {
+
metrics.triggerFlushByByMaxDelay(flushContext.asyncAddArgsList.size(),
bytesSize,
+ System.currentTimeMillis() -
flushContext.asyncAddArgsList.get(0).addedTime);
+ }
+ doFlush();
+ } catch (Exception e){
+ log.error("Trig flush by timing task fail.", e);
+ } finally {
+ // Start the next timing task.
+ nextTimingTrigger();
Review Comment:
Can't you just create your own single thread executor service which is
ScheduledExecutorService and use it for the scheduling of period flush instead
of implementing it on your own using Timer?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]