From b8326ea7915befb424a374d5d036ca8d613d2176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=85=89?= Date: Mon, 18 Mar 2019 19:11:07 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=A7=92=E7=BA=A7=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=E8=BE=BE=E5=88=B0700=E4=B8=87=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=E7=94=9F=E6=88=90=E8=B7=9F=E4=B8=8D=E4=B8=8A?= =?UTF-8?q?=E6=B6=88=E8=B4=B9=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fsg/uid/buffer/BufferPaddingExecutor.java | 21 +++++++++++++++++++ .../com/baidu/fsg/uid/buffer/RingBuffer.java | 11 ++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/baidu/fsg/uid/buffer/BufferPaddingExecutor.java b/src/main/java/com/baidu/fsg/uid/buffer/BufferPaddingExecutor.java index 80c37ad..89b78b2 100644 --- a/src/main/java/com/baidu/fsg/uid/buffer/BufferPaddingExecutor.java +++ b/src/main/java/com/baidu/fsg/uid/buffer/BufferPaddingExecutor.java @@ -147,6 +147,27 @@ public void paddingBuffer() { } // fill the rest slots until to catch the cursor + putBuffer(); + } + /** + * Padding buffer fill the slots until to catch the cursor + */ + public Boolean booleanPaddingBuffer() { + LOGGER.info("Ready to padding buffer lastSecond:{}. {}", lastSecond.get(), ringBuffer); + + // is still running + if (!running.compareAndSet(false, true)) { + LOGGER.info("Padding buffer is still running. {}", ringBuffer); + return false; + } + + // fill the rest slots until to catch the cursor + putBuffer(); + return true; + } + + private void putBuffer() { + boolean isFullRingBuffer = false; while (!isFullRingBuffer) { List uidList = uidProvider.provide(lastSecond.incrementAndGet()); diff --git a/src/main/java/com/baidu/fsg/uid/buffer/RingBuffer.java b/src/main/java/com/baidu/fsg/uid/buffer/RingBuffer.java index 2b5ab62..a982e27 100644 --- a/src/main/java/com/baidu/fsg/uid/buffer/RingBuffer.java +++ b/src/main/java/com/baidu/fsg/uid/buffer/RingBuffer.java @@ -166,9 +166,16 @@ public long take() { // cursor catch the tail, means that there is no more available UID to take if (nextCursor == currentCursor) { - rejectedTakeHandler.rejectTakeBuffer(this); + Boolean running = false; + while (!running) { + running = bufferPaddingExecutor.booleanPaddingBuffer(); + } + nextCursor = cursor.updateAndGet(old -> old == tail.get() ? old : old + 1); + + if (nextCursor == currentCursor) { + rejectedTakeHandler.rejectTakeBuffer(this); + } } - // 1. check next slot flag is CAN_TAKE_FLAG int nextCursorIndex = calSlotIndex(nextCursor); Assert.isTrue(flags[nextCursorIndex].get() == CAN_TAKE_FLAG, "Curosr not in can take status");