iamaleksey commented on code in PR #2982:
URL: https://github.com/apache/cassandra/pull/2982#discussion_r1444946818
##########
src/java/org/apache/cassandra/service/accord/AccordJournal.java:
##########
@@ -699,6 +1003,246 @@ private static int msVersion(int version)
}
}
+ /*
+ * Record framing logic
+ */
+
+ private final class FrameAggregator implements Interruptible.Task
+ {
+ /* external MPSC pending request queue */
+ private final ManyToOneConcurrentLinkedQueue<PendingRequest>
unframedRequests = new ManyToOneConcurrentLinkedQueue<>();
+
+ private final LongArrayList waitForEpochs = new LongArrayList();
+ private final Long2ObjectHashMap<ArrayList<PendingRequest>>
delayedRequests = new Long2ObjectHashMap<>();
+
+ private volatile Interruptible executor;
+
+ // a signal and flag that callers outside the aggregator thread can use
+ // to signal they want the aggregator to run again
+ private final Semaphore haveWork = newSemaphore(1);
+
+ void onWrite(Pointer pointer, RequestContext context)
+ {
+ unframedRequests.add(new PendingRequest(pointer, context));
+ haveWork.release(1);
+ }
+
+ void notifyOfEpoch()
+ {
+ haveWork.release(1);
+ }
+
+ void start()
+ {
+ executor =
executorFactory().infiniteLoop("AccordJournal#FrameAggregator", this, SAFE,
NON_DAEMON, SYNCHRONIZED);
+ }
+
+ void shutdown()
+ {
+ executor.shutdown();
+ }
+
+ /* internal reusable buffers used for frame generation */
+ private final ArrayList<PendingRequest> requestBuffer = new
ArrayList<>();
+ private final ArrayList<Pointer> pointersBuffer = new ArrayList<>();
+ private final ArrayList<RequestContext> contextsBuffer = new
ArrayList<>();
+
+ @Override
+ public void run(Interruptible.State state) throws InterruptedException
+ {
+ if (unframedRequests.isEmpty() && delayedRequests.isEmpty())
+ return;
+
+ try
+ {
+ doRun();
+ }
+ finally
+ {
+ requestBuffer.clear();
+ pointersBuffer.clear();
+ contextsBuffer.clear();
+ }
+
+ haveWork.acquire(1);
+ }
+
+ private void doRun()
+ {
+ /*
+ * Deal with delayed requests
+ */
+
+ waitForEpochs.sort(null);
+
+ for (int i = 0; i < waitForEpochs.size(); i++)
+ {
+ long waitForEpoch = waitForEpochs.getLong(i);
+ if (!node.topology().hasEpoch(waitForEpoch))
+ break;
+ requestBuffer.addAll(delayedRequests.remove(waitForEpoch));
+ }
+
+ waitForEpochs.removeIfLong(epoch ->
!delayedRequests.containsKey(epoch));
+
+ /*
+ * Deal with regular pending requests
+ */
+
+ PendingRequest request;
+ while (null != (request = unframedRequests.poll()))
+ {
+ long waitForEpoch = request.context.waitForEpoch;
+ if (!node.topology().hasEpoch(waitForEpoch))
+ {
+ delayedRequests.computeIfAbsent(waitForEpoch, ignore ->
new ArrayList<>()).add(request);
+ if (!waitForEpochs.containsLong(waitForEpoch))
+ {
+ waitForEpochs.addLong(waitForEpoch);
+ node.withEpoch(waitForEpoch, this::notifyOfEpoch);
+ }
+ }
+ else
+ {
+ requestBuffer.add(request);
+ }
+ }
+
+ for (PendingRequest req : requestBuffer)
+ {
+ pointersBuffer.add(req.pointer);
+ contextsBuffer.add(req.context);
+ }
+
+ if (!requestBuffer.isEmpty())
+ {
+ FrameRecord frame = new FrameRecord(node.uniqueNow(),
pointersBuffer.toArray(new Pointer[0]));
+ FrameContext context = new
FrameContext(contextsBuffer.toArray(new RequestContext[0]));
+ appendAuxiliaryRecord(frame, context);
+ }
+ }
+
+ private final class PendingRequest
+ {
+ final Pointer pointer;
+ final RequestContext context;
+
+ PendingRequest(Pointer pointer, RequestContext context)
+ {
+ this.pointer = pointer;
+ this.context = context;
+ }
+ }
+ }
+
+ private final class FrameApplicator implements Runnable
+ {
+ /** external SPSC written frame queue */
+ private final SpscLinkedQueue<PendingFrame> newFrames = new
SpscLinkedQueue<>();
+
+ /* single-thread accessed internal frame buffer */
+ private final ArrayList<PendingFrame> pendingFrames = new
ArrayList<>();
+
+ /* furthest flushed journal segment + position */
+ private volatile Pointer flushedUntil = null;
+
+ private volatile SequentialExecutorPlus executor;
+
+ /* invoked from FrameGenerator thread via appendAuxiliaryRecord() call
*/
+ void onWrite(Pointer start, int size, FrameContext context)
+ {
+ newFrames.add(new PendingFrame(start, new Pointer(start.segment,
start.position + size), context));
+ }
+
+ /* invoked only from Journal Flusher thread (single) */
+ void onFlush(long segment, int position)
+ {
+ flushedUntil = new Pointer(segment, position);
+ executor.submit(this);
+ }
+
+ void start()
+ {
+ executor =
executorFactory().sequential("AccordJournal#FrameApplicator");
+ }
+
+ void shutdown()
+ {
+ executor.shutdown();
+ }
+
+ @Override
+ public void run()
+ {
+ if (newFrames.drain(pendingFrames::add) > 0)
+ {
+ /* order by position in the journal, DESC */
+ pendingFrames.sort((f1, f2) -> f2.start.compareTo(f1.start));
+ }
+
+ Pointer flushedUntil = this.flushedUntil;
+ for (int i = pendingFrames.size() - 1; i >= 0; i--)
+ {
+ PendingFrame frame = pendingFrames.get(i);
+ if (frame.end.compareTo(flushedUntil) > 0)
+ break;
+ applyFrame((FrameRecord) cachedRecords.remove(frame.start),
frame.context);
+ pendingFrames.remove(i);
+ }
+ }
+
+ private void applyFrame(FrameRecord frame, FrameContext context)
+ {
+ Invariants.checkState(frame.pointers.length ==
context.requestContexts.length);
+ for (int i = 0; i < frame.pointers.length; i++)
+ applyRequest(frame.pointers[i], context.requestContexts[i]);
+ }
+
+ private void applyRequest(Pointer pointer, RequestContext context)
+ {
+ Request request = (Request) cachedRecords.remove(pointer);
Review Comment:
Yep.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]