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 UidGeneratorService extends from DefaultUidGenerator, based on a lock free RingBuffer

The spring properties you can specified as below:

  • boostPower: RingBuffer size boost for a power of 2, Sample: boostPower is 3, it means the buffer size will be getMaxSequence() + 1
  • paddingFactor: Represents a percent value of (0 - 100). When the count of rest available UIDs reach the threshold, it will trigger padding buffer. Default asRingBuffer.DEFAULT_PADDING_PERCENT Sample: paddingFactor=20, bufferSize=1000 -> threshold=1000 * 20 /100, padding buffer will be triggered when tail-cursor < threshold
  • 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
  • Author:
    yutianbao, wujun
    • 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 boostPower
        RingBuffer 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

        private Long scheduleInterval
        另外一种RingBuffer填充时机, 在Schedule线程中, 周期性检查填充 默认:不配置此项, 即不使用Schedule线程. 如需使用, 请指定Schedule线程时间间隔, 单位:秒
      • rejectedPutBufferHandler

        @Autowired(required=false) private RejectedPutBufferHandler rejectedPutBufferHandler
        拒绝策略: 当环已满, 无法继续填充时 默认无需指定, 将丢弃Put操作, 仅日志记录. 如有特殊需求, 请实现RejectedPutBufferHandler接口(支持Lambda表达式)并以@Autowired方式注入
      • rejectedTakeBufferHandler

        @Autowired(required=false) private RejectedTakeBufferHandler rejectedTakeBufferHandler
        拒绝策略: 当环已空, 无法继续获取时 默认无需指定, 将记录日志, 并抛出UidGenerateException异常. 如有特殊需求, 请实现RejectedTakeBufferHandler接口(支持Lambda表达式)并以@Autowired方式注入
      • ringBuffer

        private RingBuffer ringBuffer
        RingBuffer
      • bufferPaddingExecutor

        private BufferPaddingExecutor bufferPaddingExecutor
    • Constructor Details

      • CachedUidGenerator

        public CachedUidGenerator(UidProperties uidProperties)
    • Method Details

      • afterPropertiesSet

        public void afterPropertiesSet() throws Exception
        Specified by:
        afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
        Overrides:
        afterPropertiesSet in class DefaultUidGenerator
        Throws:
        Exception
      • getUID

        public long getUID()
        Description copied from interface: UidGeneratorService
        Get a unique ID
        Specified by:
        getUID in interface UidGeneratorService
        Overrides:
        getUID in class DefaultUidGenerator
        Returns:
        UID
      • parseUID

        public String parseUID(long uid)
        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 interface UidGeneratorService
        Overrides:
        parseUID in class DefaultUidGenerator
        Returns:
        Parsed info
      • destroy

        public void destroy() throws Exception
        Specified by:
        destroy in interface org.springframework.beans.factory.DisposableBean
        Throws:
        Exception
      • nextIdsForOneSecond

        protected List<Long> nextIdsForOneSecond(long currentSecond)
        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

        public void setRejectedPutBufferHandler(RejectedPutBufferHandler rejectedPutBufferHandler)
      • setRejectedTakeBufferHandler

        public void setRejectedTakeBufferHandler(RejectedTakeBufferHandler rejectedTakeBufferHandler)
      • setScheduleInterval

        public void setScheduleInterval(long scheduleInterval)