Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
108437b
feat(proxy): integrate with CoAPI and refactor proxy components
Ahoo-Wang May 12, 2025
040182f
refactor(proxy): simplify @DeleteExchange in MachineApi
Ahoo-Wang May 12, 2025
15914a5
refactor(proxy): update SegmentApi to use @RequestParam for primitive…
Ahoo-Wang May 12, 2025
1933c25
refactor(proxy): generalize API client creation with generics
Ahoo-Wang May 12, 2025
c5cb730
feat(cosid): add proxy support and update configuration
Ahoo-Wang May 12, 2025
c7824b7
test(cosid-proxy): update redis container version and network configu…
Ahoo-Wang May 12, 2025
9b9fbd1
build(cosid-example-proxy): update dependencies and simplify network …
Ahoo-Wang May 12, 2025
1dcb6ff
test(cosid-proxy): remove exposed port 6379 from Redis container
Ahoo-Wang May 13, 2025
594740f
ci(github-actions): add stacktrace to cosid-proxy test command
Ahoo-Wang May 13, 2025
3208d14
ci: remove redundant workflow triggers
Ahoo-Wang May 13, 2025
fc9a54f
ci: ignore specific folders in Codecov checks
Ahoo-Wang May 13, 2025
06abd3e
Merge branch 'main' into proxy-coapi
Ahoo-Wang May 13, 2025
95b18cf
refactor(proxy): extract ApiClientFactory for creating API clients
Ahoo-Wang May 13, 2025
527248b
refactor(proxy): reorganize imports and update Redis container version
Ahoo-Wang May 13, 2025
a4b7ebc
feat(proxy): integrate coapi for machine and segment clients
Ahoo-Wang May 13, 2025
10c5ca5
refactor(proxy): enhance logging for ProxyIdSegmentDistributorFactory
Ahoo-Wang May 13, 2025
e0c9355
ci: change test execution to debug level for CosId-Proxy
Ahoo-Wang May 13, 2025
7cb6539
test(cosid-proxy): disable container reuse in ProxyServerLauncher
Ahoo-Wang May 13, 2025
c3447c6
ci: reduce log level for CosId-Proxy test
Ahoo-Wang May 13, 2025
16a4dbc
build(gradle): enable build caching and optimize test timing
Ahoo-Wang May 13, 2025
5d7a723
test(proxy): disable distributors tests and add SegmentClient test
Ahoo-Wang May 13, 2025
746c3aa
test: enable ProxyMachineIdDistributorTest
Ahoo-Wang May 13, 2025
d06d685
test: update test cases for machine id distributor and proxy id segme…
Ahoo-Wang May 13, 2025
055d9dd
test(cosid): add unit test for CosIdProxyMachineIdDistributorAutoConf…
Ahoo-Wang May 13, 2025
15eefaa
refactor(cosid): remove unnecessary `CosIdProperties` dependency
Ahoo-Wang May 13, 2025
dd18c24
test(proxy): enable integration tests for ProxyIdSegmentDistributor
Ahoo-Wang May 13, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ jobs:
settings-path: ${{ github.workspace }}

- name: Test CosId-Proxy
run: ./gradlew cosid-proxy:clean cosid-proxy:check --stacktrace
run: ./gradlew cosid-proxy:clean cosid-proxy:check --info

cosid-zookeeper-test:
name: CosId Zookeeper Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void sequenceIncrement() {
long id = snowflakeId.generate();
SnowflakeIdState snowflakeIdState = snowflakeId.friendlyId(id);

LockSupport.parkNanos(Duration.ofMillis(2).toNanos());
LockSupport.parkNanos(Duration.ofMillis(10).toNanos());

long id2 = snowflakeId.generate();
SnowflakeIdState snowflakeIdState2 = snowflakeId.friendlyId(id2);
Expand Down
2 changes: 1 addition & 1 deletion cosid-dependencies/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
dependencies {
api(platform(libs.spring.boot.dependencies))
api(platform(libs.spring.cloud.dependencies))
api(platform(libs.okhttp.bom))
api(platform(libs.coapi.bom))
api(platform(libs.testcontainers.bom))
constraints {
api(libs.guava)
Expand Down
1 change: 1 addition & 0 deletions cosid-spring-boot-starter/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ dependencies {
"zookeeperSupportImplementation"(project(":cosid-zookeeper"))

"proxySupportImplementation"(project(":cosid-proxy"))
"proxySupportImplementation"("me.ahoo.coapi:coapi-spring-boot-starter")
"mongoSupportImplementation"(project(":cosid-mongo"))
"activitiSupportImplementation"(project(":cosid-activiti"))
"activitiSupportImplementation"(libs.activiti.spring.boot.starter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import me.ahoo.cosid.CosId;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

/**
* CosId Properties.
Expand All @@ -28,7 +29,8 @@ public class CosIdProperties {
private boolean enabled = true;

private String namespace = DEFAULT_NAMESPACE;


@NestedConfigurationProperty
private ProxyProperties proxy = new ProxyProperties();

public boolean isEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
*/
public class ProxyProperties {
private String host = "http://localhost:8688";

public String getHost() {
return host;
}

public ProxyProperties setHost(String host) {
this.host = host;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

package me.ahoo.cosid.spring.boot.starter.machine;

import me.ahoo.coapi.spring.EnableCoApi;
import me.ahoo.cosid.machine.ClockBackwardsSynchronizer;
import me.ahoo.cosid.machine.MachineStateStorage;
import me.ahoo.cosid.proxy.ProxyMachineIdDistributor;
import me.ahoo.cosid.proxy.api.MachineClient;
import me.ahoo.cosid.spring.boot.starter.ConditionalOnCosIdEnabled;
import me.ahoo.cosid.spring.boot.starter.CosIdProperties;

import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -34,23 +34,15 @@
@ConditionalOnCosIdEnabled
@ConditionalOnCosIdMachineEnabled
@ConditionalOnProperty(value = MachineProperties.Distributor.TYPE, havingValue = "proxy")
@EnableCoApi(clients = MachineClient.class)
public class CosIdProxyMachineIdDistributorAutoConfiguration {
private final CosIdProperties cosIdProperties;

public CosIdProxyMachineIdDistributorAutoConfiguration(CosIdProperties cosIdProperties) {
this.cosIdProperties = cosIdProperties;
}

@Bean
@ConditionalOnMissingBean
public OkHttpClient okHttpClient() {
return new OkHttpClient.Builder().build();
}


@Bean
@ConditionalOnMissingBean
public ProxyMachineIdDistributor proxyMachineIdDistributor(OkHttpClient httpClient, MachineStateStorage localMachineState,
public ProxyMachineIdDistributor proxyMachineIdDistributor(MachineClient machineClient, MachineStateStorage localMachineState,
ClockBackwardsSynchronizer clockBackwardsSynchronizer) {
return new ProxyMachineIdDistributor(httpClient, cosIdProperties.getProxy().getHost(), localMachineState, clockBackwardsSynchronizer);
return new ProxyMachineIdDistributor(machineClient, localMachineState, clockBackwardsSynchronizer);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

package me.ahoo.cosid.spring.boot.starter.segment;

import me.ahoo.coapi.spring.EnableCoApi;
import me.ahoo.cosid.proxy.ProxyIdSegmentDistributorFactory;
import me.ahoo.cosid.proxy.api.SegmentClient;
import me.ahoo.cosid.segment.IdSegmentDistributorFactory;
import me.ahoo.cosid.spring.boot.starter.ConditionalOnCosIdEnabled;
import me.ahoo.cosid.spring.boot.starter.CosIdProperties;

import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -34,25 +34,14 @@
@ConditionalOnCosIdEnabled
@ConditionalOnCosIdSegmentEnabled
@EnableConfigurationProperties(SegmentIdProperties.class)
@ConditionalOnProperty(value = SegmentIdProperties.Distributor.TYPE, matchIfMissing = true, havingValue = "proxy")
@ConditionalOnProperty(value = SegmentIdProperties.Distributor.TYPE, havingValue = "proxy")
@EnableCoApi(clients = SegmentClient.class)
public class CosIdProxySegmentAutoConfiguration {

private final CosIdProperties cosIdProperties;

public CosIdProxySegmentAutoConfiguration(CosIdProperties cosIdProperties) {
this.cosIdProperties = cosIdProperties;
}

@Bean
@ConditionalOnMissingBean
public OkHttpClient okHttpClient() {
return new OkHttpClient.Builder().build();
}


@Bean
@ConditionalOnMissingBean
public IdSegmentDistributorFactory idSegmentDistributorFactory(OkHttpClient httpClient) {
return new ProxyIdSegmentDistributorFactory(httpClient, cosIdProperties.getProxy().getHost());
public IdSegmentDistributorFactory idSegmentDistributorFactory(SegmentClient segmentClient) {
return new ProxyIdSegmentDistributorFactory(segmentClient);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright [2021-present] [ahoo wang <[email protected]> (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.machine;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.testcontainers.shaded.org.bouncycastle.oer.OERDefinition.BaseType.Supplier;

import me.ahoo.coapi.spring.boot.starter.CoApiAutoConfiguration;
import me.ahoo.cosid.machine.ClockBackwardsSynchronizer;
import me.ahoo.cosid.machine.MachineStateStorage;
import me.ahoo.cosid.proxy.ProxyMachineIdDistributor;
import me.ahoo.cosid.spring.boot.starter.CosIdAutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
import org.springframework.web.client.RestClient;

class CosIdProxyMachineIdDistributorAutoConfigurationTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();

@Test
void contextLoads() {
this.contextRunner
.withPropertyValues(ConditionalOnCosIdMachineEnabled.ENABLED_KEY + "=true")
.withPropertyValues(MachineProperties.Distributor.TYPE + "=proxy")
.withPropertyValues("cosid.proxy.host" + "=http://localhost:8688")
.withBean(RestClient.Builder.class, RestClient::builder)
.withBean(MachineStateStorage.class, () -> MachineStateStorage.IN_MEMORY)
.withBean(ClockBackwardsSynchronizer.class, () -> ClockBackwardsSynchronizer.DEFAULT)
.withUserConfiguration(UtilAutoConfiguration.class, CosIdAutoConfiguration.class, CosIdHostNameAutoConfiguration.class,
CoApiAutoConfiguration.class)
.withUserConfiguration(CosIdProxyMachineIdDistributorAutoConfiguration.class)
.run(context -> {
assertThat(context)
.hasSingleBean(CosIdProxyMachineIdDistributorAutoConfiguration.class)
.hasSingleBean(ProxyMachineIdDistributor.class)
;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright [2021-present] [ahoo wang <[email protected]> (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 static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

import me.ahoo.coapi.spring.boot.starter.CoApiAutoConfiguration;
import me.ahoo.cosid.segment.IdSegmentDistributorFactory;
import me.ahoo.cosid.spring.boot.starter.machine.ConditionalOnCosIdMachineEnabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.web.client.RestClient;

class CosIdProxySegmentAutoConfigurationTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();

@Test
void contextLoads() {
this.contextRunner
.withPropertyValues(ConditionalOnCosIdSegmentEnabled.ENABLED_KEY + "=true")
.withPropertyValues(SegmentIdProperties.Distributor.TYPE + "=proxy")
.withPropertyValues("cosid.proxy.host" + "=http://localhost:8688")
.withBean(RestClient.Builder.class, RestClient::builder)
.withUserConfiguration(CoApiAutoConfiguration.class)
.withUserConfiguration(CosIdProxySegmentAutoConfiguration.class)
.run(context -> {
assertThat(context)
.hasSingleBean(CosIdProxySegmentAutoConfiguration.class)
.hasSingleBean(IdSegmentDistributorFactory.class)
;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,27 @@
public class DistributeConcurrent implements TestSpec {
private final Supplier<MachineIdDistributor> implFactory;
private final int machineBit;

public DistributeConcurrent(Supplier<MachineIdDistributor> implFactory, int machineBit) {
this.implFactory = implFactory;
this.machineBit = machineBit;
}


@SuppressWarnings("unchecked")
@Override
public void verify() {
MachineIdDistributor distributor = implFactory.get();
int totalMachineIds = MachineIdDistributor.totalMachineIds(machineBit);
CompletableFuture<Integer>[] results = new CompletableFuture[totalMachineIds];
String namespace = MockIdGenerator.usePrefix("DistributeConcurrent").generateAsString();

for (int i = 0; i < totalMachineIds; i++) {
InstanceId instanceId = mockInstance(i, false);
results[i] = CompletableFuture.supplyAsync(() -> distributor.distribute(namespace, machineBit, instanceId, MachineIdDistributor.FOREVER_SAFE_GUARD_DURATION).getMachineId());
}

CompletableFuture.allOf(results).join();

Integer[] machineIds = Arrays.stream(results).map(CompletableFuture::join).sorted().toArray(Integer[]::new);
for (int i = 0; i < machineIds.length; i++) {
assertThat(machineIds[i], equalTo(i));
Expand Down
2 changes: 2 additions & 0 deletions examples/cosid-example-proxy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ dependencies {
implementation(project(":cosid-spring-boot-starter"))
implementation(project(":cosid-jackson"))
implementation(project(":cosid-proxy"))
implementation("me.ahoo.coapi:coapi-spring-boot-starter")
implementation("org.springframework.cloud:spring-cloud-starter-loadbalancer")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("com.google.guava:guava")
implementation("org.springframework.boot:spring-boot-starter-web")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ cosid:
enabled: true
snowflake:
enabled: true
proxy:
host: http://localhost:8688

# segment:
# enabled: true
Expand Down
4 changes: 4 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
org.gradle.caching=true
org.gradle.parallel=true
org.gradle.jvmargs=-Xmx2g -Dfile.encoding=UTF-8

group=me.ahoo.cosid
version=2.12.3
description=Universal, flexible, high-performance distributed ID generator.
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
spring-boot = "3.4.5"
spring-cloud = "2024.0.1"
jakarta-annotation-api = "3.0.0"
okhttp = "4.12.0"
coapi = "1.11.0"
testcontainers = "1.21.0"
guava = "33.4.8-jre"
mybatis = "3.5.19"
Expand All @@ -25,7 +25,7 @@ spotbugs = "6.1.11"
spring-boot-dependencies = { module = "org.springframework.boot:spring-boot-dependencies", version.ref = "spring-boot" }
spring-cloud-dependencies = { module = "org.springframework.cloud:spring-cloud-dependencies", version.ref = "spring-cloud" }
jakarta-annotation-api = { module = "jakarta.annotation:jakarta.annotation-api", version.ref = "jakarta-annotation-api" }
okhttp-bom = { module = "com.squareup.okhttp3:okhttp-bom", version.ref = "okhttp" }
coapi-bom = { module = "me.ahoo.coapi:coapi-bom", version.ref = "coapi" }
axon-bom = { module = "org.axonframework:axon-bom", version.ref = "axon" }
testcontainers-bom = { module = "org.testcontainers:testcontainers-bom", version.ref = "testcontainers" }
guava = { module = "com.google.guava:guava", version.ref = "guava" }
Expand Down
2 changes: 2 additions & 0 deletions proxy/cosid-proxy-api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
dependencies{
api(project(":cosid-core"))
implementation("me.ahoo.coapi:coapi-api")
api("com.fasterxml.jackson.core:jackson-databind")
implementation("org.springframework:spring-web")
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* limitations under the License.
*/

package me.ahoo.cosid.proxy;
package me.ahoo.cosid.proxy.api;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Loading
Loading