Skip to content

Commit 7f7f235

Browse files
committed
1.2.2 snapshot
fix cached uid block error!
1 parent d55c288 commit 7f7f235

File tree

11 files changed

+310
-12
lines changed

11 files changed

+310
-12
lines changed

example/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>io.github.cooperlyt</groupId>
88
<artifactId>uid-reactive-generator-spring</artifactId>
9-
<version>1.2.2-snapshot</version>
9+
<version>1.2.2</version>
1010
</parent>
1111

1212
<artifactId>example</artifactId>
@@ -82,7 +82,7 @@
8282
<dependency>
8383
<groupId>io.github.cooperlyt</groupId>
8484
<artifactId>uid-reactive-generator-db-spring-boot-starter</artifactId>
85-
<version>1.2.2-snapshot</version>
85+
<version>1.2.2</version>
8686
</dependency>
8787

8888
<!-- jdbc -->

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.cooperlyt</groupId>
88
<artifactId>uid-reactive-generator-spring</artifactId>
9-
<version>1.2.2-snapshot</version>
9+
<version>1.2.2</version>
1010
<packaging>pom</packaging>
1111

1212
<name>uid-reactive-generator-spring</name>

uid-reactive-generator-api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>io.github.cooperlyt</groupId>
88
<artifactId>uid-reactive-generator-spring</artifactId>
9-
<version>1.2.2-snapshot</version>
9+
<version>1.2.2</version>
1010
</parent>
1111

1212
<artifactId>uid-reactive-generator-api</artifactId>

uid-reactive-generator-db-spring-boot-starter/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>io.github.cooperlyt</groupId>
88
<artifactId>uid-reactive-generator-spring</artifactId>
9-
<version>1.2.2-snapshot</version>
9+
<version>1.2.2</version>
1010
</parent>
1111

1212
<artifactId>uid-reactive-generator-db-spring-boot-starter</artifactId>
@@ -15,7 +15,7 @@
1515
<dependency>
1616
<groupId>io.github.cooperlyt</groupId>
1717
<artifactId>uid-reactive-generator-spring-boot-starter</artifactId>
18-
<version>1.2.2-snapshot</version>
18+
<version>1.2.2</version>
1919
</dependency>
2020

2121
<dependency>

uid-reactive-generator-spring-boot-starter/pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>io.github.cooperlyt</groupId>
88
<artifactId>uid-reactive-generator-spring</artifactId>
9-
<version>1.2.2-snapshot</version>
9+
<version>1.2.2</version>
1010
</parent>
1111

1212
<artifactId>uid-reactive-generator-spring-boot-starter</artifactId>
@@ -21,10 +21,14 @@
2121
<dependency>
2222
<groupId>io.github.cooperlyt</groupId>
2323
<artifactId>uid-reactive-generator</artifactId>
24-
<version>1.2.2-snapshot</version>
24+
<version>1.2.2</version>
2525
</dependency>
2626

2727

28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-logging</artifactId>
31+
</dependency>
2832

2933
<!-- test by jdbc and mariadb -->
3034

@@ -47,6 +51,7 @@
4751
<scope>test</scope>
4852
</dependency>
4953

54+
5055
</dependencies>
5156

5257
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package io.github.cooperlyt.cloud.uid;
2+
3+
import io.github.cooperlyt.cloud.uid.impl.CachedUidGenerator;
4+
import org.apache.commons.lang.StringUtils;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Disabled;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.extension.ExtendWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.context.junit.jupiter.SpringExtension;
12+
13+
import java.io.IOException;
14+
import java.util.ArrayList;
15+
import java.util.HashSet;
16+
import java.util.List;
17+
import java.util.Set;
18+
import java.util.concurrent.ConcurrentSkipListSet;
19+
import java.util.concurrent.atomic.AtomicInteger;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertTrue;
23+
24+
/**
25+
* Test for {@link CachedUidGenerator}
26+
*
27+
* @author yutianbao
28+
* @author wujun
29+
*/
30+
31+
@Disabled
32+
@ExtendWith(SpringExtension.class)
33+
@SpringBootTest
34+
public class CachedUidGeneratorTest {
35+
private static final int SIZE = 7000; //7000000 700w
36+
private static final boolean VERBOSE = false;
37+
private static final int THREADS = Runtime.getRuntime().availableProcessors() << 1;
38+
39+
@Autowired
40+
private UidGenerator cachedUidGenerator;
41+
42+
/**
43+
* Test for serially generate
44+
*
45+
* @throws IOException
46+
*/
47+
@Test
48+
public void testSerialGenerate() throws IOException {
49+
// Generate UID serially
50+
Set<Long> uidSet = new HashSet<>(SIZE);
51+
for (int i = 0; i < SIZE; i++) {
52+
doGenerate(uidSet, i);
53+
}
54+
55+
// Check UIDs are all unique
56+
checkUniqueID(uidSet);
57+
}
58+
59+
/**
60+
* Test for parallel generate
61+
*
62+
* @throws InterruptedException
63+
* @throws IOException
64+
*/
65+
@Test
66+
public void testParallelGenerate() throws InterruptedException, IOException {
67+
AtomicInteger control = new AtomicInteger(-1);
68+
Set<Long> uidSet = new ConcurrentSkipListSet<>();
69+
70+
// Initialize threads
71+
List<Thread> threadList = new ArrayList<>(THREADS);
72+
for (int i = 0; i < THREADS; i++) {
73+
Thread thread = new Thread(() -> workerRun(uidSet, control));
74+
thread.setName("UID-generator-" + i);
75+
76+
threadList.add(thread);
77+
thread.start();
78+
}
79+
80+
// Wait for worker done
81+
for (Thread thread : threadList) {
82+
thread.join();
83+
}
84+
85+
// Check generate 700w times
86+
assertEquals(SIZE, control.get());
87+
88+
// Check UIDs are all unique
89+
checkUniqueID(uidSet);
90+
}
91+
92+
/**
93+
* Woker run
94+
*/
95+
private void workerRun(Set<Long> uidSet, AtomicInteger control) {
96+
for (;;) {
97+
int myPosition = control.updateAndGet(old -> (old == SIZE ? SIZE : old + 1));
98+
if (myPosition == SIZE) {
99+
return;
100+
}
101+
102+
doGenerate(uidSet, myPosition);
103+
}
104+
}
105+
106+
/**
107+
* Do generating
108+
*/
109+
private void doGenerate(Set<Long> uidSet, int index) {
110+
long uid = cachedUidGenerator.getUID().block() ;
111+
String parsedInfo = cachedUidGenerator.parseUID(uid);
112+
System.out.println("gen id: " + parsedInfo);
113+
boolean existed = !uidSet.add(uid);
114+
if (existed) {
115+
System.out.println("Found duplicate UID " + uid);
116+
}
117+
118+
// Check UID is positive, and can be parsed
119+
assertTrue(uid > 0L);
120+
Assertions.assertTrue(StringUtils.isNotBlank(parsedInfo));
121+
122+
if (VERBOSE) {
123+
System.out.println(Thread.currentThread().getName() + " No." + index + " >>> " + parsedInfo);
124+
}
125+
}
126+
127+
/**
128+
* Check UIDs are all unique
129+
*/
130+
private void checkUniqueID(Set<Long> uidSet) throws IOException {
131+
System.out.println(uidSet.size());
132+
assertEquals(SIZE, uidSet.size());
133+
}
134+
135+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package io.github.cooperlyt.cloud.uid;
2+
3+
import io.github.cooperlyt.cloud.uid.impl.DefaultUidGenerator;
4+
import org.apache.commons.lang.StringUtils;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Disabled;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.extension.ExtendWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.context.junit.jupiter.SpringExtension;
12+
13+
import java.util.ArrayList;
14+
import java.util.HashSet;
15+
import java.util.List;
16+
import java.util.Set;
17+
import java.util.concurrent.ConcurrentSkipListSet;
18+
import java.util.concurrent.atomic.AtomicInteger;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
23+
/**
24+
* Test for {@link DefaultUidGenerator}
25+
*
26+
* @author yutianbao
27+
* @author wujun
28+
*/
29+
30+
@Disabled
31+
@ExtendWith(SpringExtension.class)
32+
@SpringBootTest
33+
public class DefaultUidGeneratorTest {
34+
private static final int SIZE = 50000; // 10w
35+
private static final boolean VERBOSE = true;
36+
private static final int THREADS = Runtime.getRuntime().availableProcessors() << 1;
37+
38+
@Autowired
39+
private UidGenerator defaultUidGenerator;
40+
41+
42+
/**
43+
* Test for serially generate
44+
*/
45+
@Test
46+
public void testSerialGenerate() {
47+
// Generate UID serially
48+
Set<Long> uidSet = new HashSet<>(SIZE);
49+
for (int i = 0; i < SIZE; i++) {
50+
doGenerate(uidSet, i);
51+
}
52+
53+
// Check UIDs are all unique
54+
checkUniqueID(uidSet);
55+
}
56+
57+
/**
58+
* Test for parallel generate
59+
*
60+
* @throws InterruptedException
61+
*/
62+
@Test
63+
public void testParallelGenerate() throws InterruptedException {
64+
AtomicInteger control = new AtomicInteger(-1);
65+
Set<Long> uidSet = new ConcurrentSkipListSet<>();
66+
67+
// Initialize threads
68+
List<Thread> threadList = new ArrayList<>(THREADS);
69+
for (int i = 0; i < THREADS; i++) {
70+
Thread thread = new Thread(() -> workerRun(uidSet, control));
71+
thread.setName("UID-generator-" + i);
72+
73+
threadList.add(thread);
74+
thread.start();
75+
}
76+
77+
// Wait for worker done
78+
for (Thread thread : threadList) {
79+
thread.join();
80+
}
81+
82+
// Check generate 10w times
83+
assertEquals(SIZE, control.get());
84+
85+
// Check UIDs are all unique
86+
checkUniqueID(uidSet);
87+
}
88+
89+
/**
90+
* Worker run
91+
*/
92+
private void workerRun(Set<Long> uidSet, AtomicInteger control) {
93+
for (;;) {
94+
int myPosition = control.updateAndGet(old -> (old == SIZE ? SIZE : old + 1));
95+
if (myPosition == SIZE) {
96+
return;
97+
}
98+
99+
doGenerate(uidSet, myPosition);
100+
}
101+
}
102+
103+
/**
104+
* Do generating
105+
*/
106+
private void doGenerate(Set<Long> uidSet, int index) {
107+
108+
109+
long uid = defaultUidGenerator.getUID().block() ;
110+
String parsedInfo = defaultUidGenerator.parseUID(uid);
111+
//System.out.println("gen id: " + parsedInfo);
112+
boolean existed = !uidSet.add(uid);
113+
if (existed) {
114+
System.out.println("Found duplicate UID " + uid);
115+
}
116+
117+
// Check UID is positive, and can be parsed
118+
assertTrue(uid > 0L);
119+
Assertions.assertTrue(StringUtils.isNotBlank(parsedInfo));
120+
121+
if (VERBOSE) {
122+
System.out.println(Thread.currentThread().getName() + " No." + index + " >>> " + parsedInfo);
123+
}
124+
}
125+
126+
/**
127+
* Check UIDs are all unique
128+
*/
129+
private void checkUniqueID(Set<Long> uidSet) {
130+
System.out.println(uidSet.size());
131+
assertEquals(SIZE, uidSet.size());
132+
}
133+
134+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.github.cooperlyt.cloud.uid;
2+
3+
import io.github.cooperlyt.cloud.uid.worker.WorkerIdAssigner;
4+
import org.springframework.stereotype.Component;
5+
import reactor.core.publisher.Mono;
6+
7+
@Component
8+
public class MockWorkerAssigner implements WorkerIdAssigner {
9+
10+
11+
@Override
12+
public Mono<Long> assignWorkerId() {
13+
return Mono.just(1L);
14+
}
15+
16+
@Override
17+
public void releaseWorkerId(long id, long lastTime) {
18+
19+
}
20+
}

uid-reactive-generator-spring-cloud-starter-discovery/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>io.github.cooperlyt</groupId>
88
<artifactId>uid-reactive-generator-spring</artifactId>
9-
<version>1.2.2-snapshot</version>
9+
<version>1.2.2</version>
1010
</parent>
1111

1212
<artifactId>uid-reactive-generator-spring-cloud-starter-discovery</artifactId>
@@ -16,7 +16,7 @@
1616
<dependency>
1717
<groupId>io.github.cooperlyt</groupId>
1818
<artifactId>uid-reactive-generator-spring-boot-starter</artifactId>
19-
<version>1.2.2-snapshot</version>
19+
<version>1.2.2</version>
2020
</dependency>
2121

2222
<dependency>

0 commit comments

Comments
 (0)