Skip to content

Commit

Permalink
feat: support CustomizeSegmentIdProperties/`CustomizeSnowflakeIdPro…
Browse files Browse the repository at this point in the history
…perties`. (#383)
  • Loading branch information
Ahoo-Wang authored Jul 25, 2023
1 parent ad22986 commit c0178bf
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (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.spring.boot.starter;

@FunctionalInterface
public interface CustomizeIdProperties<P> {
void customize(P idProperties);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.lang.Nullable;

/**
* CosId Segment AutoConfiguration.
Expand Down Expand Up @@ -61,15 +62,18 @@ public CosIdLifecyclePrefetchWorkerExecutorService lifecycleSegmentChainId(Prefe

@Bean
public SegmentIdBeanRegistrar segmentIdBeanRegistrar(IdSegmentDistributorFactory distributorFactory,
IdGeneratorProvider idGeneratorProvider,
PrefetchWorkerExecutorService prefetchWorkerExecutorService,
ConfigurableApplicationContext applicationContext) {
IdGeneratorProvider idGeneratorProvider,
PrefetchWorkerExecutorService prefetchWorkerExecutorService,
ConfigurableApplicationContext applicationContext,
@Nullable
CustomizeSegmentIdProperties customizeSegmentIdProperties
) {

return new SegmentIdBeanRegistrar(cosIdProperties,
segmentIdProperties,
distributorFactory,
idGeneratorProvider,
prefetchWorkerExecutorService,
applicationContext);
applicationContext, customizeSegmentIdProperties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (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.spring.boot.starter.segment;

import me.ahoo.cosid.spring.boot.starter.CustomizeIdProperties;

public interface CustomizeSegmentIdProperties extends CustomizeIdProperties<SegmentIdProperties> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.lang.Nullable;

@Slf4j
public class SegmentIdBeanRegistrar implements InitializingBean {
Expand All @@ -40,19 +41,23 @@ public class SegmentIdBeanRegistrar implements InitializingBean {
private final IdGeneratorProvider idGeneratorProvider;
private final PrefetchWorkerExecutorService prefetchWorkerExecutorService;
private final ConfigurableApplicationContext applicationContext;
@Nullable
private final CustomizeSegmentIdProperties customizeSegmentIdProperties;

public SegmentIdBeanRegistrar(CosIdProperties cosIdProperties,
SegmentIdProperties segmentIdProperties,
IdSegmentDistributorFactory distributorFactory,
IdGeneratorProvider idGeneratorProvider,
PrefetchWorkerExecutorService prefetchWorkerExecutorService,
ConfigurableApplicationContext applicationContext) {
ConfigurableApplicationContext applicationContext,
@Nullable CustomizeSegmentIdProperties customizeSegmentIdProperties) {
this.cosIdProperties = cosIdProperties;
this.segmentIdProperties = segmentIdProperties;
this.distributorFactory = distributorFactory;
this.idGeneratorProvider = idGeneratorProvider;
this.prefetchWorkerExecutorService = prefetchWorkerExecutorService;
this.applicationContext = applicationContext;
this.customizeSegmentIdProperties = customizeSegmentIdProperties;
}

@Override
Expand All @@ -61,6 +66,9 @@ public void afterPropertiesSet() {
}

public void register() {
if (customizeSegmentIdProperties != null) {
customizeSegmentIdProperties.customize(segmentIdProperties);
}
SegmentIdProperties.ShardIdDefinition shareIdDefinition = segmentIdProperties.getShare();
if (shareIdDefinition.isEnabled()) {
registerIdDefinition(IdGeneratorProvider.SHARE, shareIdDefinition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.lang.Nullable;

/**
* CosId Snowflake AutoConfiguration.
Expand Down Expand Up @@ -53,15 +54,19 @@ public SnowflakeIdBeanRegistrar snowflakeIdBeanRegistrar(final InstanceId instan
IdGeneratorProvider idGeneratorProvider,
MachineIdDistributor machineIdDistributor,
ClockBackwardsSynchronizer clockBackwardsSynchronizer,
ConfigurableApplicationContext applicationContext) {
ConfigurableApplicationContext applicationContext,
@Nullable
CustomizeSnowflakeIdProperties customizeSnowflakeIdProperties
) {
return new SnowflakeIdBeanRegistrar(cosIdProperties,
machineProperties,
snowflakeIdProperties,
instanceId,
idGeneratorProvider,
machineIdDistributor,
clockBackwardsSynchronizer,
applicationContext);
applicationContext,
customizeSnowflakeIdProperties);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (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.spring.boot.starter.snowflake;

import me.ahoo.cosid.spring.boot.starter.CustomizeIdProperties;

public interface CustomizeSnowflakeIdProperties extends CustomizeIdProperties<SnowflakeIdProperties> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.common.base.MoreObjects;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.lang.Nullable;

import java.time.ZoneId;

Expand All @@ -41,6 +42,8 @@ public class SnowflakeIdBeanRegistrar implements InitializingBean {
private final MachineIdDistributor machineIdDistributor;
private final ClockBackwardsSynchronizer clockBackwardsSynchronizer;
private final ConfigurableApplicationContext applicationContext;
@Nullable
private final CustomizeSnowflakeIdProperties customizeSnowflakeIdProperties;

public SnowflakeIdBeanRegistrar(CosIdProperties cosIdProperties,
MachineProperties machineProperties,
Expand All @@ -49,7 +52,8 @@ public SnowflakeIdBeanRegistrar(CosIdProperties cosIdProperties,
IdGeneratorProvider idGeneratorProvider,
MachineIdDistributor machineIdDistributor,
ClockBackwardsSynchronizer clockBackwardsSynchronizer,
ConfigurableApplicationContext applicationContext) {
ConfigurableApplicationContext applicationContext,
@Nullable CustomizeSnowflakeIdProperties customizeSnowflakeIdProperties) {
this.cosIdProperties = cosIdProperties;
this.machineProperties = machineProperties;
this.snowflakeIdProperties = snowflakeIdProperties;
Expand All @@ -58,6 +62,7 @@ public SnowflakeIdBeanRegistrar(CosIdProperties cosIdProperties,
this.machineIdDistributor = machineIdDistributor;
this.clockBackwardsSynchronizer = clockBackwardsSynchronizer;
this.applicationContext = applicationContext;
this.customizeSnowflakeIdProperties = customizeSnowflakeIdProperties;
}

@Override
Expand All @@ -66,6 +71,9 @@ public void afterPropertiesSet() {
}

public void register() {
if (customizeSnowflakeIdProperties != null) {
customizeSnowflakeIdProperties.customize(snowflakeIdProperties);
}
SnowflakeIdProperties.ShardIdDefinition shareIdDefinition = snowflakeIdProperties.getShare();
if (shareIdDefinition.isEnabled()) {
registerIdDefinition(IdGeneratorProvider.SHARE, shareIdDefinition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

import me.ahoo.cosid.segment.SegmentId;
import me.ahoo.cosid.segment.concurrent.PrefetchWorkerExecutorService;
import me.ahoo.cosid.spring.boot.starter.CosIdAutoConfiguration;

Expand All @@ -25,13 +24,15 @@ void contextLoads() {
.withPropertyValues("spring.datasource.url=jdbc:mysql://localhost:3306/cosid_db")
.withPropertyValues("spring.datasource.username=root")
.withPropertyValues("spring.datasource.password=root")
.withBean(CustomizeSegmentIdProperties.class, () -> idProperties -> idProperties.getProvider().put("test", new SegmentIdProperties.IdDefinition()))
.withUserConfiguration(CosIdAutoConfiguration.class, DataSourceAutoConfiguration.class, CosIdJdbcSegmentAutoConfiguration.class, CosIdSegmentAutoConfiguration.class)
.run(context -> {
assertThat(context)
.hasSingleBean(CosIdSegmentAutoConfiguration.class)
.hasSingleBean(PrefetchWorkerExecutorService.class)
.hasSingleBean(CosIdLifecyclePrefetchWorkerExecutorService.class)
.hasSingleBean(SegmentId.class)
.hasBean("__share__SegmentId")
.hasBean("testSegmentId")
;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

import me.ahoo.cosid.machine.ClockBackwardsSynchronizer;
import me.ahoo.cosid.snowflake.SnowflakeId;
import me.ahoo.cosid.machine.InstanceId;
import me.ahoo.cosid.machine.MachineId;
import me.ahoo.cosid.machine.MachineStateStorage;
import me.ahoo.cosid.machine.ManualMachineIdDistributor;
import me.ahoo.cosid.machine.k8s.StatefulSetMachineIdDistributor;
import me.ahoo.cosid.spring.boot.starter.CosIdAutoConfiguration;
import me.ahoo.cosid.spring.boot.starter.machine.ConditionalOnCosIdMachineEnabled;
import me.ahoo.cosid.spring.boot.starter.machine.CosIdLifecycleMachineIdDistributor;
import me.ahoo.cosid.spring.boot.starter.machine.CosIdMachineAutoConfiguration;
import me.ahoo.cosid.spring.boot.starter.machine.MachineProperties;

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetEnvironmentVariable;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.commons.util.UtilAutoConfiguration;

Expand All @@ -47,6 +43,7 @@ void contextLoads() {
.withPropertyValues(ConditionalOnCosIdMachineEnabled.ENABLED_KEY + "=true")
.withPropertyValues(ConditionalOnCosIdSnowflakeEnabled.ENABLED_KEY + "=true")
.withPropertyValues(MachineProperties.PREFIX + ".distributor.manual.machineId=1")
.withBean(CustomizeSnowflakeIdProperties.class, () -> idProperties -> idProperties.getProvider().put("test", new SnowflakeIdProperties.IdDefinition()))
.withUserConfiguration(UtilAutoConfiguration.class,
CosIdAutoConfiguration.class,
CosIdMachineAutoConfiguration.class,
Expand All @@ -60,7 +57,8 @@ void contextLoads() {
.hasSingleBean(ClockBackwardsSynchronizer.class)
.hasSingleBean(MachineId.class)
.hasSingleBean(CosIdLifecycleMachineIdDistributor.class)
.hasSingleBean(SnowflakeId.class)
.hasBean("__share__SnowflakeId")
.hasBean("testSnowflakeId")
;
});
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# limitations under the License.
#
group=me.ahoo.cosid
version=2.2.4
version=2.2.5
description=Universal, flexible, high-performance distributed ID generator.
website=https://github.com/Ahoo-Wang/CosId
issues=https://github.com/Ahoo-Wang/CosId/issues
Expand Down

0 comments on commit c0178bf

Please sign in to comment.