From 1b3712492c7ccdab573fb4ae9961af27e114e596 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Sun, 4 Feb 2024 13:30:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):?= =?UTF-8?q?=20UID=E7=94=9F=E6=88=90=E5=99=A8=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../listener/AppStartListener.java | 5 + .../metersphere/system/uid/IDGenerator.java | 6 +- .../system/uid/impl/CachedUidGenerator.java | 147 ------------------ .../system/uid/impl/DefaultUidGenerator.java | 9 +- 4 files changed, 16 insertions(+), 151 deletions(-) delete mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/uid/impl/CachedUidGenerator.java diff --git a/backend/app/src/main/java/io/metersphere/listener/AppStartListener.java b/backend/app/src/main/java/io/metersphere/listener/AppStartListener.java index 8013056a12..f59a8f7035 100644 --- a/backend/app/src/main/java/io/metersphere/listener/AppStartListener.java +++ b/backend/app/src/main/java/io/metersphere/listener/AppStartListener.java @@ -9,6 +9,7 @@ import io.metersphere.sdk.util.CommonBeanFactory; import io.metersphere.sdk.util.LogUtils; import io.metersphere.system.service.BaseScheduleService; import io.metersphere.system.service.PluginLoadService; +import io.metersphere.system.uid.impl.DefaultUidGenerator; import io.minio.MinioClient; import jakarta.annotation.Resource; import org.springframework.boot.ApplicationArguments; @@ -26,9 +27,13 @@ public class AppStartListener implements ApplicationRunner { @Resource private BaseScheduleService baseScheduleService; + @Resource + private DefaultUidGenerator defaultUidGenerator; + @Override public void run(ApplicationArguments args) throws Exception { LogUtils.info("================= 应用启动 ================="); + defaultUidGenerator.afterPropertiesSet(); // 初始化MinIO配置 ((MinioRepository) FileCenter.getRepository(StorageType.MINIO)).init(minioClient); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/uid/IDGenerator.java b/backend/services/system-setting/src/main/java/io/metersphere/system/uid/IDGenerator.java index 20e576b9ae..f25901ef35 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/uid/IDGenerator.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/uid/IDGenerator.java @@ -1,13 +1,13 @@ package io.metersphere.system.uid; -import io.metersphere.system.uid.impl.CachedUidGenerator; import io.metersphere.sdk.util.CommonBeanFactory; +import io.metersphere.system.uid.impl.DefaultUidGenerator; public class IDGenerator { - private static final CachedUidGenerator DEFAULT_UID_GENERATOR; + private static final DefaultUidGenerator DEFAULT_UID_GENERATOR; static { - DEFAULT_UID_GENERATOR = CommonBeanFactory.getBean(CachedUidGenerator.class); + DEFAULT_UID_GENERATOR = CommonBeanFactory.getBean(DefaultUidGenerator.class); } /** diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/uid/impl/CachedUidGenerator.java b/backend/services/system-setting/src/main/java/io/metersphere/system/uid/impl/CachedUidGenerator.java deleted file mode 100644 index 1c1f4e2771..0000000000 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/uid/impl/CachedUidGenerator.java +++ /dev/null @@ -1,147 +0,0 @@ - -package io.metersphere.system.uid.impl; - -import io.metersphere.sdk.exception.MSException; -import io.metersphere.system.uid.BitsAllocator; -import io.metersphere.system.uid.buffer.BufferPaddingExecutor; -import io.metersphere.system.uid.buffer.RejectedPutBufferHandler; -import io.metersphere.system.uid.buffer.RejectedTakeBufferHandler; -import io.metersphere.system.uid.buffer.RingBuffer; -import io.metersphere.sdk.util.LogUtils; -import org.springframework.beans.factory.DisposableBean; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.stereotype.Service; -import org.springframework.util.Assert; - -import java.util.ArrayList; -import java.util.List; - -/** - * from {@link DefaultUidGenerator}, based on a lock free {@link RingBuffer}

- */ -@Service -public class CachedUidGenerator extends DefaultUidGenerator implements DisposableBean, InitializingBean { - private static final int DEFAULT_BOOST_POWER = 3; - - /** - * Spring properties - */ - private int boostPower = DEFAULT_BOOST_POWER; - private Long scheduleInterval; - - private RejectedPutBufferHandler rejectedPutBufferHandler; - private RejectedTakeBufferHandler rejectedTakeBufferHandler; - - /** - * RingBuffer - */ - private RingBuffer ringBuffer; - private BufferPaddingExecutor bufferPaddingExecutor; - - @Override - public void afterPropertiesSet() { - // initialize workerId & bitsAllocator - super.afterPropertiesSet(); - - // initialize RingBuffer & RingBufferPaddingExecutor - this.initRingBuffer(); - LogUtils.info("Initialized RingBuffer successfully."); - } - - @Override - public long getUID() { - try { - return ringBuffer.take(); - } catch (Exception e) { - LogUtils.error("Generate unique id exception. ", e); - throw new MSException(e); - } - } - - @Override - public String parseUID(long uid) { - return super.parseUID(uid); - } - - @Override - public void destroy() throws Exception { - bufferPaddingExecutor.shutdown(); - } - - /** - * Get the UIDs in the same specified second under the max sequence - * * @return UID list, size of {@link BitsAllocator#getMaxSequence()} + 1 - */ - protected List nextIdsForOneSecond(long currentSecond) { - // Initialize result list size of (max sequence + 1) - int listSize = (int) bitsAllocator.getMaxSequence() + 1; - List uidList = new ArrayList<>(listSize); - - // Allocate the first sequence of the second, the others can be calculated with the offset - long firstSeqUid = bitsAllocator.allocate(currentSecond - epochSeconds, workerId, 0L); - for (int offset = 0; offset < listSize; offset++) { - uidList.add(firstSeqUid + offset); - } - - return uidList; - } - - /** - * Initialize RingBuffer & RingBufferPaddingExecutor - */ - private void initRingBuffer() { - // initialize RingBuffer - int bufferSize = ((int) bitsAllocator.getMaxSequence() + 1) << boostPower; - int paddingFactor = RingBuffer.DEFAULT_PADDING_PERCENT; - this.ringBuffer = new RingBuffer(bufferSize, paddingFactor); - LogUtils.info("Initialized ring buffer size:{}, paddingFactor:{}", bufferSize, paddingFactor); - - // initialize RingBufferPaddingExecutor - boolean usingSchedule = (scheduleInterval != null); - this.bufferPaddingExecutor = new BufferPaddingExecutor(ringBuffer, this::nextIdsForOneSecond, usingSchedule); - if (usingSchedule) { - bufferPaddingExecutor.setScheduleInterval(scheduleInterval); - } - - LogUtils.info("Initialized BufferPaddingExecutor. Using schdule:{}, interval:{}", usingSchedule, scheduleInterval); - - // set rejected put/take handle policy - this.ringBuffer.setBufferPaddingExecutor(bufferPaddingExecutor); - if (rejectedPutBufferHandler != null) { - this.ringBuffer.setRejectedPutHandler(rejectedPutBufferHandler); - } - if (rejectedTakeBufferHandler != null) { - this.ringBuffer.setRejectedTakeHandler(rejectedTakeBufferHandler); - } - - // fill in all slots of the RingBuffer - bufferPaddingExecutor.paddingBuffer(); - - // start buffer padding threads - bufferPaddingExecutor.start(); - } - - /** - * Setters for spring property - */ - public void setBoostPower(int boostPower) { - Assert.isTrue(boostPower > 0, "Boost power must be positive!"); - this.boostPower = boostPower; - } - - public void setRejectedPutBufferHandler(RejectedPutBufferHandler rejectedPutBufferHandler) { - Assert.notNull(rejectedPutBufferHandler, "RejectedPutBufferHandler can't be null!"); - this.rejectedPutBufferHandler = rejectedPutBufferHandler; - } - - public void setRejectedTakeBufferHandler(RejectedTakeBufferHandler rejectedTakeBufferHandler) { - Assert.notNull(rejectedTakeBufferHandler, "RejectedTakeBufferHandler can't be null!"); - this.rejectedTakeBufferHandler = rejectedTakeBufferHandler; - } - - public void setScheduleInterval(long scheduleInterval) { - Assert.isTrue(scheduleInterval > 0, "Schedule interval must positive!"); - this.scheduleInterval = scheduleInterval; - } - -} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/uid/impl/DefaultUidGenerator.java b/backend/services/system-setting/src/main/java/io/metersphere/system/uid/impl/DefaultUidGenerator.java index 1f8e7b049c..d5f8b11fa2 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/uid/impl/DefaultUidGenerator.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/uid/impl/DefaultUidGenerator.java @@ -8,13 +8,15 @@ import io.metersphere.system.uid.worker.WorkerIdAssigner; import io.metersphere.sdk.util.LogUtils; import jakarta.annotation.Resource; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Service; import java.util.Date; import java.util.concurrent.TimeUnit; @Service -public class DefaultUidGenerator { +public class DefaultUidGenerator implements DisposableBean, InitializingBean { /** * Bits allocate */ @@ -176,4 +178,9 @@ public class DefaultUidGenerator { this.epochSeconds = TimeUnit.MILLISECONDS.toSeconds(TimeUtils.parseByDayPattern(epochStr).getTime()); } } + + @Override + public void destroy() throws Exception { + LogUtils.info("Shutdown UidGenerator..."); + } }