Package com.bytedesk.core.uid.impl
Class CachedUidGenerator
java.lang.Object
com.bytedesk.core.uid.impl.DefaultUidGenerator
com.bytedesk.core.uid.impl.CachedUidGenerator
- All Implemented Interfaces:
UidGeneratorService
,org.springframework.beans.factory.DisposableBean
,org.springframework.beans.factory.InitializingBean
@ConfigurationProperties(prefix="bytedesk.uid.cached-uid-generator")
public class CachedUidGenerator
extends DefaultUidGenerator
implements org.springframework.beans.factory.DisposableBean
Represents a cached implementation of boostPower: RingBuffer size boost for a power of 2, Sample: boostPower is 3, it means the buffer size
will be paddingFactor: Represents a percent value of (0 - 100). When the count of rest available UIDs reach the
threshold, it will trigger padding buffer. Default as scheduleInterval: Padding buffer in a schedule, specify padding buffer interval, Unit as second
rejectedPutBufferHandler: Policy for rejected put buffer. Default as discard put request, just do logging
rejectedTakeBufferHandler: Policy for rejected take buffer. Default as throwing up an exception
UidGeneratorService
extends
from DefaultUidGenerator
, based on a lock free RingBuffer
The spring properties you can specified as below:
getMaxSequence() + 1
RingBuffer.DEFAULT_PADDING_PERCENT
Sample: paddingFactor=20, bufferSize=1000 -> threshold=1000 * 20 /100, padding buffer will be triggered when tail-cursor < threshold
- Author:
- yutianbao, wujun
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
RingBuffer size扩容参数, 可提高UID生成的吞吐量.private BufferPaddingExecutor
private static final int
private static final org.slf4j.Logger
private int
指定何时向RingBuffer中填充UID, 取值为百分比(0, 100), 默认为50 举例: bufferSize=1024, paddingFactor=50 -> threshold=1024 * 50 / 100 = 512.private RejectedPutBufferHandler
拒绝策略: 当环已满, 无法继续填充时 默认无需指定, 将丢弃Put操作, 仅日志记录.private RejectedTakeBufferHandler
拒绝策略: 当环已空, 无法继续获取时 默认无需指定, 将记录日志, 并抛出UidGenerateException异常.private RingBuffer
RingBufferprivate Long
另外一种RingBuffer填充时机, 在Schedule线程中, 周期性检查填充 默认:不配置此项, 即不使用Schedule线程.Fields inherited from class com.bytedesk.core.uid.impl.DefaultUidGenerator
bitsAllocator, lastSecond, sequence, uidProperties, workerId, workerIdAssigner
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
destroy()
long
getUID()
Get a unique IDprivate void
Initialize RingBuffer & RingBufferPaddingExecutornextIdsForOneSecond
(long currentSecond) Get the UIDs in the same specified second under the max sequenceparseUID
(long uid) Parse the UID into elements which are used to generate the UID.void
setBoostPower
(int boostPower) Setters for spring propertyvoid
setPaddingFactor
(int paddingFactor) void
setRejectedPutBufferHandler
(RejectedPutBufferHandler rejectedPutBufferHandler) void
setRejectedTakeBufferHandler
(RejectedTakeBufferHandler rejectedTakeBufferHandler) void
setScheduleInterval
(long scheduleInterval) Methods inherited from class com.bytedesk.core.uid.impl.DefaultUidGenerator
nextId, setWorkerIdAssigner
-
Field Details
-
LOGGER
private static final org.slf4j.Logger LOGGER -
DEFAULT_BOOST_POWER
private static final int DEFAULT_BOOST_POWER- See Also:
-
boostPower
private int boostPowerRingBuffer size扩容参数, 可提高UID生成的吞吐量. 默认:3, 原bufferSize=8192, 扩容后bufferSize= 8192 << 3 = 65536 -
paddingFactor
private int paddingFactor指定何时向RingBuffer中填充UID, 取值为百分比(0, 100), 默认为50 举例: bufferSize=1024, paddingFactor=50 -> threshold=1024 * 50 / 100 = 512. 当环上可用UID数量 < 512时, 将自动对RingBuffer进行填充补全 -
scheduleInterval
另外一种RingBuffer填充时机, 在Schedule线程中, 周期性检查填充 默认:不配置此项, 即不使用Schedule线程. 如需使用, 请指定Schedule线程时间间隔, 单位:秒 -
rejectedPutBufferHandler
拒绝策略: 当环已满, 无法继续填充时 默认无需指定, 将丢弃Put操作, 仅日志记录. 如有特殊需求, 请实现RejectedPutBufferHandler接口(支持Lambda表达式)并以@Autowired方式注入 -
rejectedTakeBufferHandler
拒绝策略: 当环已空, 无法继续获取时 默认无需指定, 将记录日志, 并抛出UidGenerateException异常. 如有特殊需求, 请实现RejectedTakeBufferHandler接口(支持Lambda表达式)并以@Autowired方式注入 -
ringBuffer
RingBuffer -
bufferPaddingExecutor
-
-
Constructor Details
-
CachedUidGenerator
-
-
Method Details
-
afterPropertiesSet
- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
- Overrides:
afterPropertiesSet
in classDefaultUidGenerator
- Throws:
Exception
-
getUID
public long getUID()Description copied from interface:UidGeneratorService
Get a unique ID- Specified by:
getUID
in interfaceUidGeneratorService
- Overrides:
getUID
in classDefaultUidGenerator
- Returns:
- UID
-
parseUID
Description copied from interface:UidGeneratorService
Parse the UID into elements which are used to generate the UID.
Such as timestamp and workerId and sequence...- Specified by:
parseUID
in interfaceUidGeneratorService
- Overrides:
parseUID
in classDefaultUidGenerator
- Returns:
- Parsed info
-
destroy
- Specified by:
destroy
in interfaceorg.springframework.beans.factory.DisposableBean
- Throws:
Exception
-
nextIdsForOneSecond
Get the UIDs in the same specified second under the max sequence- Parameters:
currentSecond
-- Returns:
- UID list, size of
BitsAllocator#getMaxSequence()
+ 1
-
initRingBuffer
private void initRingBuffer()Initialize RingBuffer & RingBufferPaddingExecutor -
setBoostPower
public void setBoostPower(int boostPower) Setters for spring property -
setPaddingFactor
public void setPaddingFactor(int paddingFactor) -
setRejectedPutBufferHandler
-
setRejectedTakeBufferHandler
-
setScheduleInterval
public void setScheduleInterval(long scheduleInterval)
-