From f6189a18f245de2a5cee2b512b215b68832a01bb Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Wed, 28 Jun 2023 18:10:55 +0800 Subject: [PATCH 01/11] feat: Support auto config `GroupedIdSegmentDistributor` . --- .../starter/segment/SegmentIdBeanRegistrar.java | 6 ++++++ .../boot/starter/segment/SegmentIdProperties.java | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java index ba524eb398..1682b61058 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java @@ -21,6 +21,8 @@ import me.ahoo.cosid.segment.SegmentChainId; import me.ahoo.cosid.segment.SegmentId; import me.ahoo.cosid.segment.concurrent.PrefetchWorkerExecutorService; +import me.ahoo.cosid.segment.grouped.DateGroupedSupplier; +import me.ahoo.cosid.segment.grouped.GroupedIdSegmentDistributorFactory; import me.ahoo.cosid.spring.boot.starter.CosIdProperties; import me.ahoo.cosid.spring.boot.starter.IdConverterDefinition; import me.ahoo.cosid.spring.boot.starter.Namespaces; @@ -69,6 +71,10 @@ public void register() { private void registerIdDefinition(String name, SegmentIdProperties.IdDefinition idDefinition) { IdSegmentDistributorDefinition distributorDefinition = asDistributorDefinition(name, idDefinition); IdSegmentDistributor idSegmentDistributor = distributorFactory.create(distributorDefinition); + if (idDefinition.getGrouped() == SegmentIdProperties.IdDefinition.Grouped.YEAR) { + idSegmentDistributor = new GroupedIdSegmentDistributorFactory(DateGroupedSupplier.YEAR, distributorFactory).create(distributorDefinition); + } + SegmentId idGenerator = createSegment(segmentIdProperties, idDefinition, idSegmentDistributor, prefetchWorkerExecutorService); registerSegmentId(name, idGenerator); } diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdProperties.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdProperties.java index 54d72710bd..bdc914d733 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdProperties.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdProperties.java @@ -329,6 +329,7 @@ public static class IdDefinition { private Chain chain; @NestedConfigurationProperty private IdConverterDefinition converter = new IdConverterDefinition(); + private Grouped grouped = Grouped.NEVER; public String getNamespace() { return namespace; @@ -386,6 +387,18 @@ public void setConverter(IdConverterDefinition converter) { this.converter = converter; } + public Grouped getGrouped() { + return grouped; + } + + public void setGrouped(Grouped grouped) { + this.grouped = grouped; + } + + public enum Grouped { + YEAR, + NEVER + } } public static class ShardIdDefinition extends IdDefinition { From d1b8cc0145e74d115e3e4009291bf41ce32c81fb Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Thu, 29 Jun 2023 09:06:10 +0800 Subject: [PATCH 02/11] feat: Support auto config `GroupedIdSegmentDistributor` . --- .../spring/boot/starter/segment/SegmentIdBeanRegistrar.java | 4 +++- .../starter/segment/CosIdSegmentAutoConfigurationTest.java | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java index 1682b61058..559dd25ec6 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java @@ -70,9 +70,11 @@ public void register() { private void registerIdDefinition(String name, SegmentIdProperties.IdDefinition idDefinition) { IdSegmentDistributorDefinition distributorDefinition = asDistributorDefinition(name, idDefinition); - IdSegmentDistributor idSegmentDistributor = distributorFactory.create(distributorDefinition); + IdSegmentDistributor idSegmentDistributor; if (idDefinition.getGrouped() == SegmentIdProperties.IdDefinition.Grouped.YEAR) { idSegmentDistributor = new GroupedIdSegmentDistributorFactory(DateGroupedSupplier.YEAR, distributorFactory).create(distributorDefinition); + } else { + idSegmentDistributor = distributorFactory.create(distributorDefinition); } SegmentId idGenerator = createSegment(segmentIdProperties, idDefinition, idSegmentDistributor, prefetchWorkerExecutorService); diff --git a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java index 41fccc807c..7076534cbb 100644 --- a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java +++ b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java @@ -44,6 +44,7 @@ void contextLoadsWithConfig() { .withPropertyValues("spring.datasource.username=root") .withPropertyValues("spring.datasource.password=root") .withPropertyValues(SegmentIdProperties.PREFIX + ".share.enabled=false") + .withPropertyValues(SegmentIdProperties.PREFIX + ".provider.test.grouped=year") .withPropertyValues(SegmentIdProperties.PREFIX + ".provider.test.converter.type=to_string") .withPropertyValues(SegmentIdProperties.PREFIX + ".provider.test.converter.to_string.pad-start=true") .withUserConfiguration(CosIdAutoConfiguration.class, DataSourceAutoConfiguration.class, CosIdJdbcSegmentAutoConfiguration.class, CosIdSegmentAutoConfiguration.class) From 570ae40f1fdadab235cd81aca48dac8a63255366 Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Thu, 29 Jun 2023 09:20:17 +0800 Subject: [PATCH 03/11] feat: Support auto config `GroupedIdSegmentDistributor` . --- .../java/me/ahoo/cosid/segment/SegmentChainId.java | 2 +- ...oupedSupplier.java => DateGroupBySupplier.java} | 2 +- .../DefaultGroupedIdSegmentDistributor.java | 14 +++++++------- .../{GroupedSupplier.java => GroupBySupplier.java} | 2 +- .../grouped/GroupedIdSegmentDistributor.java | 2 +- .../GroupedIdSegmentDistributorFactory.java | 8 ++++---- ...plierTest.java => DateGroupBySupplierTest.java} | 4 ++-- .../DefaultGroupedIdSegmentDistributorTest.java | 12 ++++++------ .../starter/segment/SegmentIdBeanRegistrar.java | 6 +++--- .../boot/starter/segment/SegmentIdProperties.java | 13 +++++++------ .../GroupedIdSegmentDistributorSpec.java | 9 ++++----- 11 files changed, 37 insertions(+), 37 deletions(-) rename cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/{DateGroupedSupplier.java => DateGroupBySupplier.java} (95%) rename cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/{GroupedSupplier.java => GroupBySupplier.java} (91%) rename cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/{DateGroupedSupplierTest.java => DateGroupBySupplierTest.java} (89%) diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentChainId.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentChainId.java index 68b4fc6619..fb20178b2a 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentChainId.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentChainId.java @@ -32,7 +32,7 @@ */ @Slf4j public class SegmentChainId implements SegmentId { - public static final int DEFAULT_SAFE_DISTANCE = 1; + public static final int DEFAULT_SAFE_DISTANCE = 2; private final long idSegmentTtl; private final int safeDistance; diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DateGroupedSupplier.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DateGroupBySupplier.java similarity index 95% rename from cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DateGroupedSupplier.java rename to cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DateGroupBySupplier.java index 2074bfb022..9835a0cd96 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DateGroupedSupplier.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DateGroupBySupplier.java @@ -19,7 +19,7 @@ import java.time.Year; import java.time.ZoneId; -public enum DateGroupedSupplier implements GroupedSupplier { +public enum DateGroupBySupplier implements GroupBySupplier { YEAR { @Override public GroupedKey get() { diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java index 1388b35124..b2acb8780c 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java @@ -22,20 +22,20 @@ import javax.annotation.Nonnull; public class DefaultGroupedIdSegmentDistributor implements GroupedIdSegmentDistributor { - private final GroupedSupplier groupedSupplier; + private final GroupBySupplier groupBySupplier; private final IdSegmentDistributorDefinition idSegmentDistributorDefinition; private final IdSegmentDistributorFactory idSegmentDistributorFactory; private volatile GroupedBinding currentGroup; - public DefaultGroupedIdSegmentDistributor(GroupedSupplier groupedSupplier, IdSegmentDistributorDefinition idSegmentDistributorDefinition, IdSegmentDistributorFactory idSegmentDistributorFactory) { - this.groupedSupplier = groupedSupplier; + public DefaultGroupedIdSegmentDistributor(GroupBySupplier groupBySupplier, IdSegmentDistributorDefinition idSegmentDistributorDefinition, IdSegmentDistributorFactory idSegmentDistributorFactory) { + this.groupBySupplier = groupBySupplier; this.idSegmentDistributorDefinition = idSegmentDistributorDefinition; this.idSegmentDistributorFactory = idSegmentDistributorFactory; this.ensureGrouped(); } private GroupedBinding ensureGrouped() { - GroupedKey groupedKey = groupedSupplier.get(); + GroupedKey groupedKey = groupBySupplier.get(); if (currentGroup != null && currentGroup.group().equals(groupedKey)) { return currentGroup; } @@ -43,7 +43,7 @@ private GroupedBinding ensureGrouped() { if (currentGroup != null && currentGroup.group().equals(groupedKey)) { return currentGroup; } - String groupedName = idSegmentDistributorDefinition.getName() + "@" + groupedKey; + String groupedName = idSegmentDistributorDefinition.getName() + "@" + groupedKey.getKey(); IdSegmentDistributorDefinition groupedDef = new IdSegmentDistributorDefinition(idSegmentDistributorDefinition.getNamespace(), groupedName, idSegmentDistributorDefinition.getOffset(), @@ -55,8 +55,8 @@ private GroupedBinding ensureGrouped() { } @Override - public GroupedSupplier groupedSupplier() { - return groupedSupplier; + public GroupBySupplier groupBySupplier() { + return groupBySupplier; } @Nonnull diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedSupplier.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupBySupplier.java similarity index 91% rename from cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedSupplier.java rename to cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupBySupplier.java index cb486fdac7..38e3fa7d2b 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedSupplier.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupBySupplier.java @@ -15,6 +15,6 @@ import java.util.function.Supplier; -public interface GroupedSupplier extends Supplier { +public interface GroupBySupplier extends Supplier { } diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributor.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributor.java index d1aeb79652..48980878b9 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributor.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributor.java @@ -16,6 +16,6 @@ import me.ahoo.cosid.segment.IdSegmentDistributor; public interface GroupedIdSegmentDistributor extends IdSegmentDistributor { - GroupedSupplier groupedSupplier(); + GroupBySupplier groupBySupplier(); } diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributorFactory.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributorFactory.java index 1bb1dc7a1d..3985af61f8 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributorFactory.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributorFactory.java @@ -20,17 +20,17 @@ import javax.annotation.Nonnull; public class GroupedIdSegmentDistributorFactory implements IdSegmentDistributorFactory { - private final GroupedSupplier groupedSupplier; + private final GroupBySupplier groupBySupplier; private final IdSegmentDistributorFactory actual; - public GroupedIdSegmentDistributorFactory(GroupedSupplier groupedSupplier, IdSegmentDistributorFactory actual) { - this.groupedSupplier = groupedSupplier; + public GroupedIdSegmentDistributorFactory(GroupBySupplier groupBySupplier, IdSegmentDistributorFactory actual) { + this.groupBySupplier = groupBySupplier; this.actual = actual; } @Nonnull @Override public IdSegmentDistributor create(IdSegmentDistributorDefinition definition) { - return new DefaultGroupedIdSegmentDistributor(groupedSupplier, definition, actual); + return new DefaultGroupedIdSegmentDistributor(groupBySupplier, definition, actual); } } diff --git a/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/DateGroupedSupplierTest.java b/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/DateGroupBySupplierTest.java similarity index 89% rename from cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/DateGroupedSupplierTest.java rename to cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/DateGroupBySupplierTest.java index 4c3c7e49ee..076f97b9f3 100644 --- a/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/DateGroupedSupplierTest.java +++ b/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/DateGroupBySupplierTest.java @@ -18,11 +18,11 @@ import org.junit.jupiter.api.Test; -class DateGroupedSupplierTest { +class DateGroupBySupplierTest { @Test void year() { - assertThat(DateGroupedSupplier.YEAR.get(), notNullValue()); + assertThat(DateGroupBySupplier.YEAR.get(), notNullValue()); } } \ No newline at end of file diff --git a/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributorTest.java b/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributorTest.java index 705140705c..163d10df96 100644 --- a/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributorTest.java +++ b/cosid-core/src/test/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributorTest.java @@ -26,7 +26,7 @@ class DefaultGroupedIdSegmentDistributorTest { @Test void ensureGrouped() { - MockGroupedSupplier groupedSupplier = new MockGroupedSupplier(GroupedKey.forever("group-1")); + MockGroupBySupplier groupedSupplier = new MockGroupBySupplier(GroupedKey.forever("group-1")); IdSegmentDistributorDefinition definition = new IdSegmentDistributorDefinition("ns", "n", 0, 1); IdSegmentDistributorFactory actual = definition1 -> new IdSegmentDistributor.Mock(); DefaultGroupedIdSegmentDistributor distributor = new DefaultGroupedIdSegmentDistributor(groupedSupplier, definition, actual); @@ -34,20 +34,20 @@ void ensureGrouped() { assertThat(maxId1, equalTo(1L)); long maxId2 = distributor.nextMaxId(1); assertThat(maxId2, equalTo(2L)); - assertThat(distributor.groupedSupplier().get().getKey(), equalTo("group-1")); + assertThat(distributor.groupBySupplier().get().getKey(), equalTo("group-1")); groupedSupplier.setGroup(GroupedKey.forever("group-2")); maxId1 = distributor.nextMaxId(1); assertThat(maxId1, equalTo(1L)); maxId2 = distributor.nextMaxId(1); assertThat(maxId2, equalTo(2L)); - assertThat(distributor.groupedSupplier().get().getKey(), equalTo("group-2")); + assertThat(distributor.groupBySupplier().get().getKey(), equalTo("group-2")); } - public static class MockGroupedSupplier implements GroupedSupplier { + public static class MockGroupBySupplier implements GroupBySupplier { private GroupedKey group; - public MockGroupedSupplier(GroupedKey group) { + public MockGroupBySupplier(GroupedKey group) { this.group = group; } @@ -55,7 +55,7 @@ public GroupedKey getGroup() { return group; } - public MockGroupedSupplier setGroup(GroupedKey group) { + public MockGroupBySupplier setGroup(GroupedKey group) { this.group = group; return this; } diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java index 559dd25ec6..bccda9c757 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java @@ -21,7 +21,7 @@ import me.ahoo.cosid.segment.SegmentChainId; import me.ahoo.cosid.segment.SegmentId; import me.ahoo.cosid.segment.concurrent.PrefetchWorkerExecutorService; -import me.ahoo.cosid.segment.grouped.DateGroupedSupplier; +import me.ahoo.cosid.segment.grouped.DateGroupBySupplier; import me.ahoo.cosid.segment.grouped.GroupedIdSegmentDistributorFactory; import me.ahoo.cosid.spring.boot.starter.CosIdProperties; import me.ahoo.cosid.spring.boot.starter.IdConverterDefinition; @@ -71,8 +71,8 @@ public void register() { private void registerIdDefinition(String name, SegmentIdProperties.IdDefinition idDefinition) { IdSegmentDistributorDefinition distributorDefinition = asDistributorDefinition(name, idDefinition); IdSegmentDistributor idSegmentDistributor; - if (idDefinition.getGrouped() == SegmentIdProperties.IdDefinition.Grouped.YEAR) { - idSegmentDistributor = new GroupedIdSegmentDistributorFactory(DateGroupedSupplier.YEAR, distributorFactory).create(distributorDefinition); + if (idDefinition.getGroupBy() == SegmentIdProperties.IdDefinition.GroupBy.YEAR) { + idSegmentDistributor = new GroupedIdSegmentDistributorFactory(DateGroupBySupplier.YEAR, distributorFactory).create(distributorDefinition); } else { idSegmentDistributor = distributorFactory.create(distributorDefinition); } diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdProperties.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdProperties.java index bdc914d733..45b7136dca 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdProperties.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdProperties.java @@ -329,7 +329,7 @@ public static class IdDefinition { private Chain chain; @NestedConfigurationProperty private IdConverterDefinition converter = new IdConverterDefinition(); - private Grouped grouped = Grouped.NEVER; + private GroupBy groupBy = GroupBy.NEVER; public String getNamespace() { return namespace; @@ -387,15 +387,16 @@ public void setConverter(IdConverterDefinition converter) { this.converter = converter; } - public Grouped getGrouped() { - return grouped; + public GroupBy getGroupBy() { + return groupBy; } - public void setGrouped(Grouped grouped) { - this.grouped = grouped; + public IdDefinition setGroupBy(GroupBy groupBy) { + this.groupBy = groupBy; + return this; } - public enum Grouped { + public enum GroupBy { YEAR, NEVER } diff --git a/cosid-test/src/main/java/me/ahoo/cosid/test/segment/distributor/GroupedIdSegmentDistributorSpec.java b/cosid-test/src/main/java/me/ahoo/cosid/test/segment/distributor/GroupedIdSegmentDistributorSpec.java index ca90cbcc15..6ba3f455af 100644 --- a/cosid-test/src/main/java/me/ahoo/cosid/test/segment/distributor/GroupedIdSegmentDistributorSpec.java +++ b/cosid-test/src/main/java/me/ahoo/cosid/test/segment/distributor/GroupedIdSegmentDistributorSpec.java @@ -13,7 +13,6 @@ package me.ahoo.cosid.test.segment.distributor; -import static me.ahoo.cosid.segment.IdSegment.TIME_TO_LIVE_FOREVER; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; @@ -21,17 +20,17 @@ import me.ahoo.cosid.segment.IdSegmentDistributor; import me.ahoo.cosid.segment.IdSegmentDistributorDefinition; import me.ahoo.cosid.segment.IdSegmentDistributorFactory; -import me.ahoo.cosid.segment.grouped.DateGroupedSupplier; +import me.ahoo.cosid.segment.grouped.DateGroupBySupplier; import me.ahoo.cosid.segment.grouped.GroupedIdSegmentDistributorFactory; -import me.ahoo.cosid.segment.grouped.GroupedSupplier; +import me.ahoo.cosid.segment.grouped.GroupBySupplier; import me.ahoo.cosid.test.MockIdGenerator; import org.junit.jupiter.api.Test; public abstract class GroupedIdSegmentDistributorSpec extends IdSegmentDistributorSpec { - protected GroupedSupplier groupedSupplier() { - return DateGroupedSupplier.YEAR; + protected GroupBySupplier groupedSupplier() { + return DateGroupBySupplier.YEAR; } @Override From 158dd5796b8f0a215d3df8b8c784dca9816ee3f2 Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Thu, 29 Jun 2023 09:34:15 +0800 Subject: [PATCH 04/11] feat: Support auto config `GroupedIdSegmentDistributor` . --- .../DefaultGroupedIdSegmentDistributor.java | 6 ++--- .../segment/SegmentIdBeanRegistrar.java | 2 +- .../starter/segment/SegmentIdProperties.java | 24 ++++++++++++++----- .../CosIdSegmentAutoConfigurationTest.java | 2 +- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java index b2acb8780c..f1c1fa7cfd 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java @@ -62,18 +62,18 @@ public GroupBySupplier groupBySupplier() { @Nonnull @Override public String getNamespace() { - return idSegmentDistributorDefinition.getNamespace(); + return this.ensureGrouped().getNamespace(); } @Nonnull @Override public String getName() { - return idSegmentDistributorDefinition.getName(); + return this.ensureGrouped().getName(); } @Override public long getStep() { - return idSegmentDistributorDefinition.getStep(); + return this.ensureGrouped().getStep(); } @Override diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java index bccda9c757..b5ba0e402e 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java @@ -71,7 +71,7 @@ public void register() { private void registerIdDefinition(String name, SegmentIdProperties.IdDefinition idDefinition) { IdSegmentDistributorDefinition distributorDefinition = asDistributorDefinition(name, idDefinition); IdSegmentDistributor idSegmentDistributor; - if (idDefinition.getGroupBy() == SegmentIdProperties.IdDefinition.GroupBy.YEAR) { + if (idDefinition.getGroup().getBy() == SegmentIdProperties.IdDefinition.GroupBy.YEAR) { idSegmentDistributor = new GroupedIdSegmentDistributorFactory(DateGroupBySupplier.YEAR, distributorFactory).create(distributorDefinition); } else { idSegmentDistributor = distributorFactory.create(distributorDefinition); diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdProperties.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdProperties.java index 45b7136dca..738dea44e1 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdProperties.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdProperties.java @@ -329,7 +329,7 @@ public static class IdDefinition { private Chain chain; @NestedConfigurationProperty private IdConverterDefinition converter = new IdConverterDefinition(); - private GroupBy groupBy = GroupBy.NEVER; + private Group group = new Group(); public String getNamespace() { return namespace; @@ -387,13 +387,25 @@ public void setConverter(IdConverterDefinition converter) { this.converter = converter; } - public GroupBy getGroupBy() { - return groupBy; + public Group getGroup() { + return group; } - public IdDefinition setGroupBy(GroupBy groupBy) { - this.groupBy = groupBy; - return this; + public void setGroup(Group group) { + this.group = group; + } + + public static class Group { + private GroupBy by = GroupBy.NEVER; + + public GroupBy getBy() { + return by; + } + + public Group setBy(GroupBy by) { + this.by = by; + return this; + } } public enum GroupBy { diff --git a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java index 7076534cbb..5a7bd2e80f 100644 --- a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java +++ b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java @@ -44,7 +44,7 @@ void contextLoadsWithConfig() { .withPropertyValues("spring.datasource.username=root") .withPropertyValues("spring.datasource.password=root") .withPropertyValues(SegmentIdProperties.PREFIX + ".share.enabled=false") - .withPropertyValues(SegmentIdProperties.PREFIX + ".provider.test.grouped=year") + .withPropertyValues(SegmentIdProperties.PREFIX + ".provider.test.group.by=year") .withPropertyValues(SegmentIdProperties.PREFIX + ".provider.test.converter.type=to_string") .withPropertyValues(SegmentIdProperties.PREFIX + ".provider.test.converter.to_string.pad-start=true") .withUserConfiguration(CosIdAutoConfiguration.class, DataSourceAutoConfiguration.class, CosIdJdbcSegmentAutoConfiguration.class, CosIdSegmentAutoConfiguration.class) From 259e8f43299f99b644b45c642079037720f68a84 Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Thu, 29 Jun 2023 09:43:32 +0800 Subject: [PATCH 05/11] feat: Support auto config `GroupedIdSegmentDistributor` . --- .../segment/grouped/DefaultGroupedIdSegmentDistributor.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java index f1c1fa7cfd..684c841e86 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java @@ -62,18 +62,18 @@ public GroupBySupplier groupBySupplier() { @Nonnull @Override public String getNamespace() { - return this.ensureGrouped().getNamespace(); + return this.idSegmentDistributorDefinition.getNamespace(); } @Nonnull @Override public String getName() { - return this.ensureGrouped().getName(); + return this.idSegmentDistributorDefinition.getName(); } @Override public long getStep() { - return this.ensureGrouped().getStep(); + return this.idSegmentDistributorDefinition.getStep(); } @Override From 303a2ba9470b358606c77ea69fced20f5faed5b2 Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Thu, 29 Jun 2023 10:14:19 +0800 Subject: [PATCH 06/11] feat: Support auto config `GroupedIdSegmentDistributor` . --- .../converter/YearPrefixIdConverter.java | 60 +++++++++++++++++++ .../converter/YearPrefixIdConverterTest.java | 32 ++++++++++ .../boot/starter/IdConverterDecorator.java | 9 +++ .../boot/starter/IdConverterDefinition.java | 42 +++++++++++++ .../CosIdSegmentAutoConfigurationTest.java | 1 + .../SegmentIdConverterDecoratorTest.java | 21 ++++++- .../src/main/resources/application.yaml | 12 +++- 7 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 cosid-core/src/main/java/me/ahoo/cosid/converter/YearPrefixIdConverter.java create mode 100644 cosid-core/src/test/java/me/ahoo/cosid/converter/YearPrefixIdConverterTest.java diff --git a/cosid-core/src/main/java/me/ahoo/cosid/converter/YearPrefixIdConverter.java b/cosid-core/src/main/java/me/ahoo/cosid/converter/YearPrefixIdConverter.java new file mode 100644 index 0000000000..642636dc92 --- /dev/null +++ b/cosid-core/src/main/java/me/ahoo/cosid/converter/YearPrefixIdConverter.java @@ -0,0 +1,60 @@ +/* + * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)]. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package me.ahoo.cosid.converter; + +import me.ahoo.cosid.IdConverter; + +import com.google.common.base.Preconditions; + +import javax.annotation.Nonnull; +import java.time.Year; + +/** + * Converter for setting string ID prefix. + * + * @author ahoo wang + */ +public class YearPrefixIdConverter implements IdConverter { + + private final String delimiter; + private final IdConverter idConverter; + + public YearPrefixIdConverter(String delimiter, IdConverter idConverter) { + Preconditions.checkNotNull(delimiter, "prefix can not be null!"); + this.delimiter = delimiter; + this.idConverter = idConverter; + } + + public String getDelimiter() { + return delimiter; + } + + @Nonnull + @Override + public String asString(long id) { + String idStr = idConverter.asString(id); + Year nowYear = Year.now(); + if (delimiter.isEmpty()) { + return nowYear + idStr; + } + return nowYear + delimiter + idStr; + } + + @Override + public long asLong(@Nonnull String idString) { + int beginIndex = delimiter.length() + 4; + String idStr = idString.substring(beginIndex); + return idConverter.asLong(idStr); + } +} diff --git a/cosid-core/src/test/java/me/ahoo/cosid/converter/YearPrefixIdConverterTest.java b/cosid-core/src/test/java/me/ahoo/cosid/converter/YearPrefixIdConverterTest.java new file mode 100644 index 0000000000..88693d055a --- /dev/null +++ b/cosid-core/src/test/java/me/ahoo/cosid/converter/YearPrefixIdConverterTest.java @@ -0,0 +1,32 @@ +/* + * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)]. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package me.ahoo.cosid.converter; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +import org.junit.jupiter.api.Test; + +import java.time.Year; + +class YearPrefixIdConverterTest { + + @Test + void asString() { + YearPrefixIdConverter converter = new YearPrefixIdConverter("-", ToStringIdConverter.INSTANCE); + assertThat(converter.getDelimiter(), equalTo("-")); + assertThat(converter.asString(1), equalTo(Year.now() + "-1")); + assertThat(converter.asLong("2023-1"), equalTo(1L)); + } +} \ No newline at end of file diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/IdConverterDecorator.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/IdConverterDecorator.java index b85e90087c..34fa1b2c25 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/IdConverterDecorator.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/IdConverterDecorator.java @@ -19,6 +19,7 @@ import me.ahoo.cosid.converter.Radix62IdConverter; import me.ahoo.cosid.converter.SuffixIdConverter; import me.ahoo.cosid.converter.ToStringIdConverter; +import me.ahoo.cosid.converter.YearPrefixIdConverter; import com.google.common.base.Strings; @@ -43,9 +44,17 @@ public T decorate() { default -> throw new IllegalStateException("Unexpected value: " + converterDefinition.getType()); } + IdConverterDefinition.YearPrefix yearPrefix = converterDefinition.getYearPrefix(); + + if (yearPrefix.isEnabled() && yearPrefix.isBeforePrefix()) { + idConverter = new YearPrefixIdConverter(yearPrefix.getDelimiter(), idConverter); + } if (!Strings.isNullOrEmpty(converterDefinition.getPrefix())) { idConverter = new PrefixIdConverter(converterDefinition.getPrefix(), idConverter); } + if (yearPrefix.isEnabled() && !yearPrefix.isBeforePrefix()) { + idConverter = new YearPrefixIdConverter(yearPrefix.getDelimiter(), idConverter); + } if (!Strings.isNullOrEmpty(converterDefinition.getSuffix())) { idConverter = new SuffixIdConverter(converterDefinition.getSuffix(), idConverter); } diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/IdConverterDefinition.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/IdConverterDefinition.java index 469985c252..4b8520ec1a 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/IdConverterDefinition.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/IdConverterDefinition.java @@ -25,6 +25,7 @@ public class IdConverterDefinition { private Type type = Type.RADIX; private String prefix; + private YearPrefix yearPrefix = new YearPrefix(); private String suffix; private Radix radix = new Radix(); private ToString toString; @@ -46,6 +47,14 @@ public void setPrefix(String prefix) { this.prefix = prefix; } + public YearPrefix getYearPrefix() { + return yearPrefix; + } + + public void setYearPrefix(YearPrefix yearPrefix) { + this.yearPrefix = yearPrefix; + } + public String getSuffix() { return suffix; } @@ -129,6 +138,39 @@ public void setCharSize(int charSize) { } } + public static class YearPrefix { + private boolean enabled = false; + private String delimiter = ""; + private boolean beforePrefix = true; + + public boolean isEnabled() { + return enabled; + } + + public YearPrefix setEnabled(boolean enabled) { + this.enabled = enabled; + return this; + } + + public String getDelimiter() { + return delimiter; + } + + public YearPrefix setDelimiter(String delimiter) { + this.delimiter = delimiter; + return this; + } + + public boolean isBeforePrefix() { + return beforePrefix; + } + + public YearPrefix setBeforePrefix(boolean beforePrefix) { + this.beforePrefix = beforePrefix; + return this; + } + } + public static class Custom { private Class type; diff --git a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java index 5a7bd2e80f..089e063554 100644 --- a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java +++ b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java @@ -46,6 +46,7 @@ void contextLoadsWithConfig() { .withPropertyValues(SegmentIdProperties.PREFIX + ".share.enabled=false") .withPropertyValues(SegmentIdProperties.PREFIX + ".provider.test.group.by=year") .withPropertyValues(SegmentIdProperties.PREFIX + ".provider.test.converter.type=to_string") + .withPropertyValues(SegmentIdProperties.PREFIX + ".provider.test.converter.year-prefix.enabled=true") .withPropertyValues(SegmentIdProperties.PREFIX + ".provider.test.converter.to_string.pad-start=true") .withUserConfiguration(CosIdAutoConfiguration.class, DataSourceAutoConfiguration.class, CosIdJdbcSegmentAutoConfiguration.class, CosIdSegmentAutoConfiguration.class) .run(context -> { diff --git a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdConverterDecoratorTest.java b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdConverterDecoratorTest.java index 62b5452005..c1992fc4a6 100644 --- a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdConverterDecoratorTest.java +++ b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdConverterDecoratorTest.java @@ -13,12 +13,12 @@ package me.ahoo.cosid.spring.boot.starter.segment; -import static org.junit.jupiter.api.Assertions.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; import me.ahoo.cosid.IdConverter; import me.ahoo.cosid.converter.SuffixIdConverter; +import me.ahoo.cosid.converter.YearPrefixIdConverter; import me.ahoo.cosid.segment.DefaultSegmentId; import me.ahoo.cosid.segment.IdSegmentDistributor; import me.ahoo.cosid.segment.SegmentId; @@ -61,6 +61,25 @@ void withPrefixAndSuffix() { assertThat(newIdGen.idConverter(), instanceOf(SuffixIdConverter.class)); } + @Test + void withYearPrefix() { + IdConverterDefinition idConverterDefinition = new IdConverterDefinition(); + idConverterDefinition.setYearPrefix(new IdConverterDefinition.YearPrefix().setEnabled(true)); + SegmentId segmentId = new DefaultSegmentId(new IdSegmentDistributor.Mock()); + SegmentId newIdGen = new SegmentIdConverterDecorator(segmentId, idConverterDefinition).decorate(); + assertThat(newIdGen.idConverter(), instanceOf(YearPrefixIdConverter.class)); + } + + @Test + void withYearPrefixAfterPrefix() { + IdConverterDefinition idConverterDefinition = new IdConverterDefinition(); + idConverterDefinition.setYearPrefix(new IdConverterDefinition.YearPrefix().setEnabled(true).setBeforePrefix(false)); + idConverterDefinition.setPrefix("prefix-"); + SegmentId segmentId = new DefaultSegmentId(new IdSegmentDistributor.Mock()); + SegmentId newIdGen = new SegmentIdConverterDecorator(segmentId, idConverterDefinition).decorate(); + assertThat(newIdGen.idConverter(), instanceOf(YearPrefixIdConverter.class)); + } + public static class CustomIdConverter implements IdConverter { @Nonnull @Override diff --git a/examples/cosid-example-redis/src/main/resources/application.yaml b/examples/cosid-example-redis/src/main/resources/application.yaml index ff74f2d7a7..6702fe7b20 100644 --- a/examples/cosid-example-redis/src/main/resources/application.yaml +++ b/examples/cosid-example-redis/src/main/resources/application.yaml @@ -3,8 +3,9 @@ server: spring: application: name: ${service.name:cosid-example-redis} - redis: - host: localhost + data: + redis: + host: localhost cosid: namespace: ${spring.application.name} machine: @@ -25,6 +26,13 @@ cosid: type: to_string to-string: pad-start: true + char-size: 8 + prefix: COSID- + year-prefix: + enabled: true + delimiter: "-" + group: + by: year logging: level: From 9acf33d62b878eaf9a546567bfb55f4e57694c6e Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Thu, 29 Jun 2023 11:47:34 +0800 Subject: [PATCH 07/11] feat: Support auto config `GroupedIdSegmentDistributor` . --- .../ahoo/cosid/segment/DefaultIdSegment.java | 42 ++++++++++-------- .../java/me/ahoo/cosid/segment/IdSegment.java | 7 +++ .../me/ahoo/cosid/segment/IdSegmentChain.java | 43 +++++++++++-------- .../cosid/segment/IdSegmentDistributor.java | 8 +++- .../ahoo/cosid/segment/MergedIdSegment.java | 5 +++ .../me/ahoo/cosid/segment/SegmentChainId.java | 18 +++++--- .../DefaultGroupedIdSegmentDistributor.java | 6 ++- .../grouped/GroupedIdSegmentDistributor.java | 6 ++- .../src/main/resources/application.yaml | 1 + 9 files changed, 89 insertions(+), 47 deletions(-) diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultIdSegment.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultIdSegment.java index 6a6bd1d075..d4bce0a9fc 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultIdSegment.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultIdSegment.java @@ -25,9 +25,9 @@ * @author ahoo wang */ public class DefaultIdSegment implements IdSegment { - - public static final DefaultIdSegment OVERFLOW = new DefaultIdSegment(IdSegment.SEQUENCE_OVERFLOW, 0, Clock.CACHE.secondTime(), TIME_TO_LIVE_FOREVER); - + + public static final DefaultIdSegment OVERFLOW = new DefaultIdSegment(IdSegment.SEQUENCE_OVERFLOW, 0, Clock.CACHE.secondTime(), TIME_TO_LIVE_FOREVER, false); + /** * include. */ @@ -37,14 +37,14 @@ public class DefaultIdSegment implements IdSegment { private volatile long sequence; private final long fetchTime; private final long ttl; - + private final boolean allowReset; private static final AtomicLongFieldUpdater S = AtomicLongFieldUpdater.newUpdater(DefaultIdSegment.class, "sequence"); - + public DefaultIdSegment(long maxId, long step) { - this(maxId, step, Clock.CACHE.secondTime(), TIME_TO_LIVE_FOREVER); + this(maxId, step, Clock.CACHE.secondTime(), TIME_TO_LIVE_FOREVER, false); } - - public DefaultIdSegment(long maxId, long step, long fetchTime, long ttl) { + + public DefaultIdSegment(long maxId, long step, long fetchTime, long ttl, boolean allowReset) { Preconditions.checkArgument(ttl > 0, "ttl:[%s] must be greater than 0.", ttl); this.maxId = maxId; this.step = step; @@ -52,52 +52,58 @@ public DefaultIdSegment(long maxId, long step, long fetchTime, long ttl) { this.sequence = offset; this.fetchTime = fetchTime; this.ttl = ttl; + this.allowReset = allowReset; } - + @Override public long getFetchTime() { return fetchTime; } - + @Override public long getTtl() { return ttl; } - + @Override public long getMaxId() { return maxId; } - + @Override public long getOffset() { return offset; } - + @Override public long getSequence() { return sequence; } - + @Override public long getStep() { return step; } - + @Override public long incrementAndGet() { if (isOverflow()) { return SEQUENCE_OVERFLOW; } - + final long nextSeq = S.incrementAndGet(this); - + if (isOverflow(nextSeq)) { return SEQUENCE_OVERFLOW; } return nextSeq; } - + + @Override + public boolean allowReset() { + return allowReset; + } + @Override public String toString() { return "DefaultIdSegment{" diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegment.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegment.java index 9daf23833d..3a02cb2bf9 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegment.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegment.java @@ -92,6 +92,10 @@ default boolean isAvailable() { long incrementAndGet(); + default boolean allowReset() { + return false; + } + @Override default int compareTo(IdSegment other) { if (getOffset() == other.getOffset()) { @@ -101,6 +105,9 @@ default int compareTo(IdSegment other) { } default void ensureNextIdSegment(IdSegment nextIdSegment) throws NextIdSegmentExpiredException { + if (allowReset()) { + return; + } if (compareTo(nextIdSegment) >= 0) { throw new NextIdSegmentExpiredException(this, nextIdSegment); } diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentChain.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentChain.java index 27c94ecbf1..94b79eb205 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentChain.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentChain.java @@ -24,21 +24,21 @@ public class IdSegmentChain implements IdSegment { public static final int ROOT_VERSION = -1; public static final IdSegmentChain NOT_SET = null; - + private final long version; private final IdSegment idSegment; @GuardedBy("this") private volatile IdSegmentChain next; - + public IdSegmentChain(IdSegmentChain previousChain, IdSegment idSegment) { this(previousChain.getVersion() + 1, idSegment); } - + public IdSegmentChain(long version, IdSegment idSegment) { this.version = version; this.idSegment = idSegment; } - + /** * try set next Chained ID segment. * @@ -51,7 +51,7 @@ public boolean trySetNext(Function idSegmentChai if (NOT_SET != next) { return false; } - + synchronized (this) { if (NOT_SET != next) { return false; @@ -61,12 +61,12 @@ public boolean trySetNext(Function idSegmentChai return true; } } - + public void setNext(IdSegmentChain nextIdSegmentChain) { ensureNextIdSegment(nextIdSegmentChain); next = nextIdSegmentChain; } - + public IdSegmentChain ensureSetNext(Function idSegmentChainSupplier) throws NextIdSegmentExpiredException { IdSegmentChain currentChain = this; while (!currentChain.trySetNext(idSegmentChainSupplier)) { @@ -74,57 +74,62 @@ public IdSegmentChain ensureSetNext(Function idS } return currentChain; } - + public IdSegmentChain getNext() { return next; } - + public IdSegment getIdSegment() { return idSegment; } - + public long getVersion() { return version; } - + public int gap(IdSegmentChain end, long step) { return (int) ((end.getMaxId() - getSequence()) / step); } - + public static IdSegmentChain newRoot() { return new IdSegmentChain(IdSegmentChain.ROOT_VERSION, DefaultIdSegment.OVERFLOW); } - + @Override public long getFetchTime() { return idSegment.getFetchTime(); } - + @Override public long getMaxId() { return idSegment.getMaxId(); } - + @Override public long getOffset() { return idSegment.getOffset(); } - + @Override public long getSequence() { return idSegment.getSequence(); } - + @Override public long getStep() { return idSegment.getStep(); } - + @Override public long incrementAndGet() { return idSegment.incrementAndGet(); } - + + @Override + public boolean allowReset() { + return idSegment.allowReset(); + } + @Override public String toString() { return "IdSegmentChain{" diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentDistributor.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentDistributor.java index 17f561d86a..1d1ef95179 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentDistributor.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentDistributor.java @@ -57,6 +57,10 @@ default long getStep(int segments) { return Math.multiplyExact(getStep(), segments); } + default boolean allowReset() { + return false; + } + long nextMaxId(long step); default long nextMaxId() { @@ -73,7 +77,7 @@ default IdSegment nextIdSegment(long ttl) { Preconditions.checkArgument(ttl > 0, "ttl:[%s] must be greater than 0.", ttl); final long maxId = nextMaxId(); - return new DefaultIdSegment(maxId, getStep(), Clock.CACHE.secondTime(), ttl); + return new DefaultIdSegment(maxId, getStep(), Clock.CACHE.secondTime(), ttl, allowReset()); } @Nonnull @@ -83,7 +87,7 @@ default IdSegment nextIdSegment(int segments, long ttl) { final long totalStep = getStep(segments); final long maxId = nextMaxId(totalStep); - final IdSegment nextIdSegment = new DefaultIdSegment(maxId, totalStep, Clock.CACHE.secondTime(), ttl); + final IdSegment nextIdSegment = new DefaultIdSegment(maxId, totalStep, Clock.CACHE.secondTime(), ttl, allowReset()); return new MergedIdSegment(segments, nextIdSegment); } diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/MergedIdSegment.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/MergedIdSegment.java index 161187b21b..3824855b77 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/MergedIdSegment.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/MergedIdSegment.java @@ -76,6 +76,11 @@ public long getTtl() { return idSegment.getTtl(); } + @Override + public boolean allowReset() { + return idSegment.allowReset(); + } + @Override public long incrementAndGet() { return idSegment.incrementAndGet(); diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentChainId.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentChainId.java index fb20178b2a..860a543876 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentChainId.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentChainId.java @@ -63,8 +63,8 @@ public IdSegmentChain getHead() { * ----- *
      * synchronized (this) {
-     *   if (currentChain.getVersion() > headChain.getVersion()) {
-     *      headChain = currentChain;
+     *   if (forwardChain.getVersion() > headChain.getVersion()) {
+     *      headChain = forwardChain;
      *  }
      * }
      * 
@@ -72,12 +72,18 @@ public IdSegmentChain getHead() { * @param forwardChain forward IdSegmentChain */ private void forward(IdSegmentChain forwardChain) { - if (forwardChain.compareTo(headChain) > 0) { - if (log.isDebugEnabled()) { - log.debug("Forward [{}] - [{}] -> [{}].", maxIdDistributor.getNamespacedName(), headChain, forwardChain); - } + if (headChain.getVersion() >= forwardChain.getVersion()) { + return; + } + if (log.isDebugEnabled()) { + log.debug("Forward [{}] - [{}] -> [{}].", maxIdDistributor.getNamespacedName(), headChain, forwardChain); + } + if (forwardChain.allowReset()) { + headChain = forwardChain; + } else if (forwardChain.compareTo(headChain) > 0) { headChain = forwardChain; } + } private IdSegmentChain generateNext(IdSegmentChain previousChain, int segments) { diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java index 684c841e86..36e7e05d85 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/DefaultGroupedIdSegmentDistributor.java @@ -82,7 +82,7 @@ public long nextMaxId(long step) { } private long getMinTtl(long ttl) { - long groupedTtl = currentGroup.group().ttl(); + long groupedTtl = ensureGrouped().group.ttl(); return Math.min(groupedTtl, ttl); } @@ -143,5 +143,9 @@ public long nextMaxId(long step) { return idSegmentDistributor.nextMaxId(step); } + @Override + public boolean allowReset() { + return true; + } } } diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributor.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributor.java index 48980878b9..0488bbce9b 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributor.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/GroupedIdSegmentDistributor.java @@ -17,5 +17,9 @@ public interface GroupedIdSegmentDistributor extends IdSegmentDistributor { GroupBySupplier groupBySupplier(); - + + @Override + default boolean allowReset() { + return true; + } } diff --git a/examples/cosid-example-redis/src/main/resources/application.yaml b/examples/cosid-example-redis/src/main/resources/application.yaml index 6702fe7b20..265faf4b35 100644 --- a/examples/cosid-example-redis/src/main/resources/application.yaml +++ b/examples/cosid-example-redis/src/main/resources/application.yaml @@ -33,6 +33,7 @@ cosid: delimiter: "-" group: by: year + mode: chain logging: level: From 788ef8546ccd4a00bdb787259ac2880fe3d9cb5f Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Thu, 29 Jun 2028 12:36:09 +0800 Subject: [PATCH 08/11] feat: Support auto config `GroupedIdSegmentDistributor` . --- .../ahoo/cosid/segment/DefaultIdSegment.java | 13 +++------ .../ahoo/cosid/segment/DefaultSegmentId.java | 22 ++++++++------- .../java/me/ahoo/cosid/segment/IdSegment.java | 10 ++----- .../me/ahoo/cosid/segment/IdSegmentChain.java | 27 +++++++++++-------- .../cosid/segment/IdSegmentDistributor.java | 8 +++--- .../ahoo/cosid/segment/MergedIdSegment.java | 5 ---- .../me/ahoo/cosid/segment/SegmentChainId.java | 7 ++--- 7 files changed, 41 insertions(+), 51 deletions(-) diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultIdSegment.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultIdSegment.java index d4bce0a9fc..73b5e1df66 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultIdSegment.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultIdSegment.java @@ -26,7 +26,7 @@ */ public class DefaultIdSegment implements IdSegment { - public static final DefaultIdSegment OVERFLOW = new DefaultIdSegment(IdSegment.SEQUENCE_OVERFLOW, 0, Clock.CACHE.secondTime(), TIME_TO_LIVE_FOREVER, false); + public static final DefaultIdSegment OVERFLOW = new DefaultIdSegment(IdSegment.SEQUENCE_OVERFLOW, 0, Clock.SYSTEM.secondTime(), TIME_TO_LIVE_FOREVER); /** * include. @@ -37,14 +37,13 @@ public class DefaultIdSegment implements IdSegment { private volatile long sequence; private final long fetchTime; private final long ttl; - private final boolean allowReset; private static final AtomicLongFieldUpdater S = AtomicLongFieldUpdater.newUpdater(DefaultIdSegment.class, "sequence"); public DefaultIdSegment(long maxId, long step) { - this(maxId, step, Clock.CACHE.secondTime(), TIME_TO_LIVE_FOREVER, false); + this(maxId, step, Clock.SYSTEM.secondTime(), TIME_TO_LIVE_FOREVER); } - public DefaultIdSegment(long maxId, long step, long fetchTime, long ttl, boolean allowReset) { + public DefaultIdSegment(long maxId, long step, long fetchTime, long ttl) { Preconditions.checkArgument(ttl > 0, "ttl:[%s] must be greater than 0.", ttl); this.maxId = maxId; this.step = step; @@ -52,7 +51,6 @@ public DefaultIdSegment(long maxId, long step, long fetchTime, long ttl, boolean this.sequence = offset; this.fetchTime = fetchTime; this.ttl = ttl; - this.allowReset = allowReset; } @Override @@ -99,11 +97,6 @@ public long incrementAndGet() { return nextSeq; } - @Override - public boolean allowReset() { - return allowReset; - } - @Override public String toString() { return "DefaultIdSegment{" diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultSegmentId.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultSegmentId.java index 867bbd82f3..03ff603035 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultSegmentId.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultSegmentId.java @@ -28,27 +28,27 @@ */ @Slf4j public class DefaultSegmentId implements SegmentId { - + private final long idSegmentTtl; private final IdSegmentDistributor maxIdDistributor; - + @GuardedBy("this") private volatile IdSegment segment = DefaultIdSegment.OVERFLOW; - + public DefaultSegmentId(IdSegmentDistributor maxIdDistributor) { this(TIME_TO_LIVE_FOREVER, maxIdDistributor); } - + public DefaultSegmentId(long idSegmentTtl, IdSegmentDistributor maxIdDistributor) { Preconditions.checkArgument(idSegmentTtl > 0, "idSegmentTtl:[%s] must be greater than 0.", idSegmentTtl); - + this.idSegmentTtl = idSegmentTtl; this.maxIdDistributor = maxIdDistributor; } - + @Override public long generate() { - + if (maxIdDistributor.getStep() == ONE_STEP) { return maxIdDistributor.nextMaxId(); } @@ -59,7 +59,7 @@ public long generate() { return nextSeq; } } - + synchronized (this) { while (true) { if (segment.isAvailable()) { @@ -69,10 +69,12 @@ public long generate() { } } IdSegment nextIdSegment = maxIdDistributor.nextIdSegment(idSegmentTtl); - segment.ensureNextIdSegment(nextIdSegment); + if (!maxIdDistributor.allowReset()) { + segment.ensureNextIdSegment(nextIdSegment); + } segment = nextIdSegment; } } } - + } diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegment.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegment.java index 3a02cb2bf9..9521aadfe8 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegment.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegment.java @@ -70,7 +70,8 @@ default boolean isExpired() { */ return false; } - return Clock.CACHE.secondTime() - getFetchTime() > getTtl(); + + return Clock.SYSTEM.secondTime() - getFetchTime() > getTtl(); } default boolean isOverflow() { @@ -92,10 +93,6 @@ default boolean isAvailable() { long incrementAndGet(); - default boolean allowReset() { - return false; - } - @Override default int compareTo(IdSegment other) { if (getOffset() == other.getOffset()) { @@ -105,9 +102,6 @@ default int compareTo(IdSegment other) { } default void ensureNextIdSegment(IdSegment nextIdSegment) throws NextIdSegmentExpiredException { - if (allowReset()) { - return; - } if (compareTo(nextIdSegment) >= 0) { throw new NextIdSegmentExpiredException(this, nextIdSegment); } diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentChain.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentChain.java index 94b79eb205..d3b91e9f06 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentChain.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentChain.java @@ -29,14 +29,16 @@ public class IdSegmentChain implements IdSegment { private final IdSegment idSegment; @GuardedBy("this") private volatile IdSegmentChain next; + private final boolean allowReset; - public IdSegmentChain(IdSegmentChain previousChain, IdSegment idSegment) { - this(previousChain.getVersion() + 1, idSegment); + public IdSegmentChain(IdSegmentChain previousChain, IdSegment idSegment, boolean allowReset) { + this(previousChain.getVersion() + 1, idSegment, allowReset); } - public IdSegmentChain(long version, IdSegment idSegment) { + public IdSegmentChain(long version, IdSegment idSegment, boolean allowReset) { this.version = version; this.idSegment = idSegment; + this.allowReset = allowReset; } /** @@ -63,7 +65,10 @@ public boolean trySetNext(Function idSegmentChai } public void setNext(IdSegmentChain nextIdSegmentChain) { - ensureNextIdSegment(nextIdSegmentChain); + if (!allowReset) { + ensureNextIdSegment(nextIdSegmentChain); + } + next = nextIdSegmentChain; } @@ -91,8 +96,8 @@ public int gap(IdSegmentChain end, long step) { return (int) ((end.getMaxId() - getSequence()) / step); } - public static IdSegmentChain newRoot() { - return new IdSegmentChain(IdSegmentChain.ROOT_VERSION, DefaultIdSegment.OVERFLOW); + public static IdSegmentChain newRoot(boolean allowReset) { + return new IdSegmentChain(IdSegmentChain.ROOT_VERSION, DefaultIdSegment.OVERFLOW, allowReset); } @Override @@ -100,6 +105,11 @@ public long getFetchTime() { return idSegment.getFetchTime(); } + @Override + public long getTtl() { + return idSegment.getTtl(); + } + @Override public long getMaxId() { return idSegment.getMaxId(); @@ -125,11 +135,6 @@ public long incrementAndGet() { return idSegment.incrementAndGet(); } - @Override - public boolean allowReset() { - return idSegment.allowReset(); - } - @Override public String toString() { return "IdSegmentChain{" diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentDistributor.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentDistributor.java index 1d1ef95179..a81e4a2450 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentDistributor.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentDistributor.java @@ -77,7 +77,7 @@ default IdSegment nextIdSegment(long ttl) { Preconditions.checkArgument(ttl > 0, "ttl:[%s] must be greater than 0.", ttl); final long maxId = nextMaxId(); - return new DefaultIdSegment(maxId, getStep(), Clock.CACHE.secondTime(), ttl, allowReset()); + return new DefaultIdSegment(maxId, getStep(), Clock.SYSTEM.secondTime(), ttl); } @Nonnull @@ -87,7 +87,7 @@ default IdSegment nextIdSegment(int segments, long ttl) { final long totalStep = getStep(segments); final long maxId = nextMaxId(totalStep); - final IdSegment nextIdSegment = new DefaultIdSegment(maxId, totalStep, Clock.CACHE.secondTime(), ttl, allowReset()); + final IdSegment nextIdSegment = new DefaultIdSegment(maxId, totalStep, Clock.SYSTEM.secondTime(), ttl); return new MergedIdSegment(segments, nextIdSegment); } @@ -100,11 +100,11 @@ default IdSegmentChain nextIdSegmentChain(IdSegmentChain previousChain) { default IdSegmentChain nextIdSegmentChain(IdSegmentChain previousChain, int segments, long ttl) { if (DEFAULT_SEGMENTS == segments) { IdSegment nextIdSegment = nextIdSegment(ttl); - return new IdSegmentChain(previousChain, nextIdSegment); + return new IdSegmentChain(previousChain, nextIdSegment, allowReset()); } IdSegment nextIdSegment = nextIdSegment(segments, ttl); - return new IdSegmentChain(previousChain, nextIdSegment); + return new IdSegmentChain(previousChain, nextIdSegment, allowReset()); } static void ensureStep(long step) { diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/MergedIdSegment.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/MergedIdSegment.java index 3824855b77..161187b21b 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/MergedIdSegment.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/MergedIdSegment.java @@ -76,11 +76,6 @@ public long getTtl() { return idSegment.getTtl(); } - @Override - public boolean allowReset() { - return idSegment.allowReset(); - } - @Override public long incrementAndGet() { return idSegment.incrementAndGet(); diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentChainId.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentChainId.java index 860a543876..6db4797332 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentChainId.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentChainId.java @@ -38,7 +38,7 @@ public class SegmentChainId implements SegmentId { private final int safeDistance; private final IdSegmentDistributor maxIdDistributor; private final PrefetchJob prefetchJob; - private volatile IdSegmentChain headChain = IdSegmentChain.newRoot(); + private volatile IdSegmentChain headChain; public SegmentChainId(IdSegmentDistributor maxIdDistributor) { this(TIME_TO_LIVE_FOREVER, DEFAULT_SAFE_DISTANCE, maxIdDistributor, PrefetchWorkerExecutorService.DEFAULT); @@ -47,6 +47,7 @@ public SegmentChainId(IdSegmentDistributor maxIdDistributor) { public SegmentChainId(long idSegmentTtl, int safeDistance, IdSegmentDistributor maxIdDistributor, PrefetchWorkerExecutorService prefetchWorkerExecutorService) { Preconditions.checkArgument(idSegmentTtl > 0, Strings.lenientFormat("Illegal idSegmentTtl parameter:[%s].", idSegmentTtl)); Preconditions.checkArgument(safeDistance > 0, "The safety distance must be greater than 0."); + this.headChain = IdSegmentChain.newRoot(maxIdDistributor.allowReset()); this.idSegmentTtl = idSegmentTtl; this.safeDistance = safeDistance; this.maxIdDistributor = maxIdDistributor; @@ -78,7 +79,7 @@ private void forward(IdSegmentChain forwardChain) { if (log.isDebugEnabled()) { log.debug("Forward [{}] - [{}] -> [{}].", maxIdDistributor.getNamespacedName(), headChain, forwardChain); } - if (forwardChain.allowReset()) { + if (maxIdDistributor.allowReset()) { headChain = forwardChain; } else if (forwardChain.compareTo(headChain) > 0) { headChain = forwardChain; @@ -174,7 +175,7 @@ public void run() { public void prefetch() { - long wakeupTimeGap = Clock.CACHE.secondTime() - lastHungerTime; + long wakeupTimeGap = Clock.SYSTEM.secondTime() - lastHungerTime; final boolean hunger = wakeupTimeGap < hungerThreshold; final int prePrefetchDistance = this.prefetchDistance; From d672544176ca9f2d41374143eb25c7164e155001 Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Thu, 29 Jun 2023 12:39:42 +0800 Subject: [PATCH 09/11] feat: Support auto config `GroupedIdSegmentDistributor` . --- .../test/segment/distributor/IdSegmentDistributorSpec.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cosid-test/src/main/java/me/ahoo/cosid/test/segment/distributor/IdSegmentDistributorSpec.java b/cosid-test/src/main/java/me/ahoo/cosid/test/segment/distributor/IdSegmentDistributorSpec.java index 39ba2e46f5..bd812baa61 100644 --- a/cosid-test/src/main/java/me/ahoo/cosid/test/segment/distributor/IdSegmentDistributorSpec.java +++ b/cosid-test/src/main/java/me/ahoo/cosid/test/segment/distributor/IdSegmentDistributorSpec.java @@ -182,7 +182,7 @@ public void nextIdSegmentWithSegmentsAndTtl() { @Test public void nextIdSegmentChain() { - IdSegmentChain root = IdSegmentChain.newRoot(); + IdSegmentChain root = IdSegmentChain.newRoot(false); String namespace = MockIdGenerator.INSTANCE.generateAsString(); IdSegmentDistributorDefinition definition = new IdSegmentDistributorDefinition(namespace, "nextIdSegmentChain", TEST_OFFSET, TEST_STEP); IdSegmentDistributor distributor = factory().create(definition); From 6c751941bf6c138cbebaca3addc8cf9085f1c7a6 Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Thu, 29 Jun 2023 12:40:12 +0800 Subject: [PATCH 10/11] feat: Support auto config `GroupedIdSegmentDistributor` . --- .../java/me/ahoo/cosid/segment/SegmentChainIdTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cosid-core/src/test/java/me/ahoo/cosid/segment/SegmentChainIdTest.java b/cosid-core/src/test/java/me/ahoo/cosid/segment/SegmentChainIdTest.java index 36f079ccef..9984c9e544 100644 --- a/cosid-core/src/test/java/me/ahoo/cosid/segment/SegmentChainIdTest.java +++ b/cosid-core/src/test/java/me/ahoo/cosid/segment/SegmentChainIdTest.java @@ -34,9 +34,9 @@ class SegmentChainIdTest { @Test void sort() { IdSegmentDistributor idSegmentDistributor = new IdSegmentDistributor.Atomic(); - IdSegmentChain idSegmentChain1 = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot()); - IdSegmentChain idSegmentChain2 = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot()); - IdSegmentChain idSegmentChain3 = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot()); + IdSegmentChain idSegmentChain1 = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot(false)); + IdSegmentChain idSegmentChain2 = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot(false)); + IdSegmentChain idSegmentChain3 = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot(false)); List chainList = Arrays.asList(idSegmentChain2, idSegmentChain1, idSegmentChain3); chainList.sort(null); Assertions.assertEquals(idSegmentChain1, chainList.get(0)); @@ -47,7 +47,7 @@ void sort() { @Test void nextIdSegmentsChain() { IdSegmentDistributor idSegmentDistributor = new IdSegmentDistributor.Atomic(); - IdSegmentChain rootChain = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot(), 3, TIME_TO_LIVE_FOREVER); + IdSegmentChain rootChain = idSegmentDistributor.nextIdSegmentChain(IdSegmentChain.newRoot(true), 3, TIME_TO_LIVE_FOREVER); Assertions.assertEquals(0, rootChain.getVersion()); Assertions.assertEquals(0, rootChain.getIdSegment().getOffset()); Assertions.assertEquals(30, rootChain.getStep()); From 2ab9237a46bd21db8cc8ea13770b06ec77efd44c Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Thu, 29 Jun 2023 12:42:38 +0800 Subject: [PATCH 11/11] feat: Support auto config `GroupedIdSegmentDistributor` . --- .../GroupedIdSegmentDistributorSpec.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/cosid-test/src/main/java/me/ahoo/cosid/test/segment/distributor/GroupedIdSegmentDistributorSpec.java b/cosid-test/src/main/java/me/ahoo/cosid/test/segment/distributor/GroupedIdSegmentDistributorSpec.java index 6ba3f455af..623c691b14 100644 --- a/cosid-test/src/main/java/me/ahoo/cosid/test/segment/distributor/GroupedIdSegmentDistributorSpec.java +++ b/cosid-test/src/main/java/me/ahoo/cosid/test/segment/distributor/GroupedIdSegmentDistributorSpec.java @@ -13,16 +13,18 @@ package me.ahoo.cosid.test.segment.distributor; +import static me.ahoo.cosid.segment.IdSegmentDistributor.DEFAULT_SEGMENTS; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; import me.ahoo.cosid.segment.IdSegment; +import me.ahoo.cosid.segment.IdSegmentChain; import me.ahoo.cosid.segment.IdSegmentDistributor; import me.ahoo.cosid.segment.IdSegmentDistributorDefinition; import me.ahoo.cosid.segment.IdSegmentDistributorFactory; import me.ahoo.cosid.segment.grouped.DateGroupBySupplier; -import me.ahoo.cosid.segment.grouped.GroupedIdSegmentDistributorFactory; import me.ahoo.cosid.segment.grouped.GroupBySupplier; +import me.ahoo.cosid.segment.grouped.GroupedIdSegmentDistributorFactory; import me.ahoo.cosid.test.MockIdGenerator; import org.junit.jupiter.api.Test; @@ -61,5 +63,19 @@ public void nextIdSegment() { assertThat(actual.getSequence(), equalTo(0L)); assertThat(actual.getTtl(), equalTo(groupedSupplier().get().ttl())); } - + + @Test + @Override + public void nextIdSegmentChain() { + IdSegmentChain root = IdSegmentChain.newRoot(false); + String namespace = MockIdGenerator.INSTANCE.generateAsString(); + IdSegmentDistributorDefinition definition = new IdSegmentDistributorDefinition(namespace, "nextIdSegmentChain", TEST_OFFSET, TEST_STEP); + IdSegmentDistributor distributor = factory().create(definition); + long expectedMaxId = TEST_OFFSET + Math.multiplyExact(TEST_STEP, DEFAULT_SEGMENTS); + IdSegment actual = distributor.nextIdSegmentChain(root); + assertThat(actual.getMaxId(), equalTo(expectedMaxId)); + assertThat(actual.getStep(), equalTo(TEST_STEP)); + assertThat(actual.getSequence(), equalTo(0L)); + assertThat(actual.getTtl(), equalTo(groupedSupplier().get().ttl())); + } }