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:</b> RingBuffer size boost for a power of 2, Sample: boostPower is 3, it means the buffer size
will be paddingFactor:</b> Represents a percent value of (0 - 100). When the count of rest available UIDs reach the
threshold, it will trigger padding buffer. Default asscheduleInterval:</b> Padding buffer in a schedule, specify padding buffer interval, Unit as second
rejectedPutBufferHandler:</b> Policy for rejected put buffer. Default as discard put request, just do logging
rejectedTakeBufferHandler:</b> Policy for rejected take buffer. Default as throwing up an exception
UidGeneratorService extends
from DefaultUidGenerator, based on a lock free RingBuffer<p>
<p>
The spring properties you can specified as below:<br>
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 IntegerRingBuffer size扩容参数, 可提高UID生成的吞吐量.private BufferPaddingExecutorprivate static final intprivate static final org.slf4j.Loggerprivate Integer指定何时向RingBuffer中填充UID, 取值为百分比(0, 100), 默认为50 举例: bufferSize=1024, paddingFactor=50 -> threshold=1024 * 50 / 100 = 512.private RejectedPutBufferHandler拒绝策略: 当环已满, 无法继续填充时 默认无需指定, 将丢弃Put操作, 仅日志记录.private RejectedTakeBufferHandler拒绝策略: 当环已空, 无法继续获取时 默认无需指定, 将记录日志, 并抛出UidGenerateException异常.private RingBufferRingBufferprivate 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 TypeMethodDescriptionvoidvoiddestroy()longgetUID()Get a unique IDprivate voidInitialize 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.voidsetBoostPower(int boostPower) Setters for spring propertyvoidsetPaddingFactor(int paddingFactor) voidsetRejectedPutBufferHandler(RejectedPutBufferHandler rejectedPutBufferHandler) voidsetRejectedTakeBufferHandler(RejectedTakeBufferHandler rejectedTakeBufferHandler) voidsetScheduleInterval(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
RingBuffer size扩容参数, 可提高UID生成的吞吐量. 默认:3, 原bufferSize=8192, 扩容后bufferSize= 8192 << 3 = 65536 -
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:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Overrides:
afterPropertiesSetin classDefaultUidGenerator- Throws:
Exception
-
getUID
public long getUID()Description copied from interface:UidGeneratorServiceGet a unique ID- Specified by:
getUIDin interfaceUidGeneratorService- Overrides:
getUIDin classDefaultUidGenerator- Returns:
- UID
-
parseUID
Description copied from interface:UidGeneratorServiceParse the UID into elements which are used to generate the UID. <br&gt; Such as timestamp and workerId and sequence...- Specified by:
parseUIDin interfaceUidGeneratorService- Overrides:
parseUIDin classDefaultUidGenerator- Returns:
- Parsed info
-
destroy
- Specified by:
destroyin 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 (max sequence + 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)
-