Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
9fc2e916
Shardingsphere
项目概览
apache
/
Shardingsphere
通知
56
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Shardingsphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9fc2e916
编写于
8月 28, 2019
作者:
W
wgy8283335
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Modify LeafSegmentKeyGenerator and add test cases.
上级
7cf5a285
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
174 addition
and
44 deletion
+174
-44
sharding-orchestration/sharding-orchestration-core/src/main/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSegmentKeyGenerator.java
...rchestration/internal/keygen/LeafSegmentKeyGenerator.java
+55
-39
sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSegmentKeyGeneratorTest.java
...stration/internal/keygen/LeafSegmentKeyGeneratorTest.java
+42
-3
sharding-orchestration/sharding-orchestration-zookeeper-curator-integration-test/src/test/java/org/apache/shardingsphere/orchestration/zookeeper/curator/integration/test/LeafSegmentKeyGeneratorIT.java
...r/curator/integration/test/LeafSegmentKeyGeneratorIT.java
+77
-2
未找到文件。
sharding-orchestration/sharding-orchestration-core/src/main/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSegmentKeyGenerator.java
浏览文件 @
9fc2e916
...
...
@@ -47,8 +47,6 @@ public final class LeafSegmentKeyGenerator implements ShardingKeyGenerator {
private
static
final
String
DEFAULT_REGISTRY_CENTER
=
"zookeeper"
;
private
static
final
float
DEFAULT_THRESHOLD
=
0.5
F
;
private
static
final
String
SLANTING_BAR
=
"/"
;
private
static
final
String
REGULAR_PATTERN
=
"^((?!/).)*$"
;
...
...
@@ -82,73 +80,81 @@ public final class LeafSegmentKeyGenerator implements ShardingKeyGenerator {
String
leafKey
=
getLeafKey
();
if
(
null
==
leafRegistryCenter
)
{
initLeafSegmentKeyGenerator
(
leafKey
);
return
id
;
}
else
{
increaseIdWhenLeafKeyStoredInCenter
(
leafKey
);
}
increaseIdWhenLeafKeyStoredInCenter
(
leafKey
);
return
id
;
}
private
void
initLeafSegmentKeyGenerator
(
final
String
leafKey
)
{
RegistryCenterConfiguration
leafConfiguration
=
getRegistryCenterConfiguration
();
leafRegistryCenter
=
new
RegistryCenterServiceLoader
().
load
(
leafConfiguration
);
if
(
leafRegistryCenter
.
isExisted
(
leafKey
))
{
id
=
incrementCacheId
(
leafKey
,
getStep
());
}
else
{
id
=
getInitialValue
();
leafRegistryCenter
.
persist
(
leafKey
,
String
.
valueOf
(
id
));
leafRegistryCenter
.
initLock
(
leafKey
);
}
step
=
getStep
();
id
=
initializeId
(
leafKey
);
initializeLeafKeyInCenter
(
leafKey
,
id
,
step
);
initializeCacheIdAsynchronous
(
id
,
step
);
}
private
void
increaseIdWhenLeafKeyStoredInCenter
(
final
String
leafKey
)
{
++
id
;
if
(((
id
%
step
)
>=
(
step
*
DEFAULT_THRESHOLD
-
1
))
&&
cacheIdQueue
.
isEmpty
())
{
incrementCacheIdAsynchronous
(
leafKey
,
step
);
}
if
((
id
%
step
)
==
(
step
-
1
))
{
if
((
id
%
step
)
==
0
)
{
id
=
tryTakeCacheId
();
incrementCacheIdAsynchronous
(
leafKey
,
step
-
(
id
%
step
));
}
++
id
;
}
private
RegistryCenterConfiguration
getRegistryCenterConfiguration
()
{
RegistryCenterConfiguration
result
=
new
RegistryCenterConfiguration
(
getRegistryCenterType
(),
properties
);
result
.
setNamespace
(
DEFAULT_NAMESPACE
)
;
result
.
setServerLists
(
getServerList
());
result
.
setDigest
(
getDigest
()
);
return
result
;
private
long
initializeId
(
final
String
leafKey
)
{
if
(
leafRegistryCenter
.
isExisted
(
leafKey
))
{
return
Long
.
parseLong
(
leafRegistryCenter
.
getDirectly
(
leafKey
))+
1
;
}
else
{
return
getInitialValue
(
);
}
}
private
void
incrementCacheIdAsynchronous
(
final
String
leafKey
,
final
long
step
)
{
private
void
initializeLeafKeyInCenter
(
final
String
leafKey
,
final
long
id
,
final
long
step
){
leafRegistryCenter
.
initLock
(
leafKey
);
while
(!
leafRegistryCenter
.
tryLock
()){
}
leafRegistryCenter
.
persist
(
leafKey
,
String
.
valueOf
(
id
+
step
-
id
%
step
));
leafRegistryCenter
.
tryRelease
();
}
private
void
initializeCacheIdAsynchronous
(
final
long
id
,
final
long
step
){
incrementCacheIdExecutor
.
execute
(
new
Runnable
()
{
@Override
public
void
run
()
{
long
id
=
incrementCacheId
(
leafKey
,
step
);
tryPutCacheId
(
id
);
tryPutCacheId
(
id
+
step
-
id
%
step
);
}
});
}
@SneakyThrows
private
long
incrementCacheId
(
final
String
leafKey
,
final
long
step
)
{
long
result
=
Long
.
MIN_VALUE
;
boolean
lockIsAcquired
=
leafRegistryCenter
.
tryLock
();
if
(
lockIsAcquired
)
{
result
=
updateCacheIdInCenter
(
leafKey
,
step
);
leafRegistryCenter
.
tryRelease
(
);
}
return
result
;
private
void
incrementCacheIdAsynchronous
(
final
String
leafKey
,
final
long
step
)
{
incrementCacheIdExecutor
.
execute
(
new
Runnable
(
)
{
@Override
public
void
run
(
)
{
long
newId
=
incrementCacheId
(
leafKey
,
step
);
tryPutCacheId
(
newId
);
}
})
;
}
@SneakyThrows
private
void
tryPutCacheId
(
final
long
id
)
{
cacheIdQueue
.
put
(
id
);
private
RegistryCenterConfiguration
getRegistryCenterConfiguration
()
{
RegistryCenterConfiguration
result
=
new
RegistryCenterConfiguration
(
getRegistryCenterType
(),
properties
);
result
.
setNamespace
(
DEFAULT_NAMESPACE
);
result
.
setServerLists
(
getServerList
());
result
.
setDigest
(
getDigest
());
return
result
;
}
@SneakyThrows
private
long
tryTakeCacheId
()
{
return
cacheIdQueue
.
take
();
private
long
incrementCacheId
(
final
String
leafKey
,
final
long
step
)
{
while
(!
leafRegistryCenter
.
tryLock
()){
}
long
result
=
updateCacheIdInCenter
(
leafKey
,
step
);
leafRegistryCenter
.
tryRelease
();
return
result
;
}
private
long
updateCacheIdInCenter
(
final
String
leafKey
,
final
long
step
)
{
...
...
@@ -162,6 +168,16 @@ public final class LeafSegmentKeyGenerator implements ShardingKeyGenerator {
return
result
;
}
@SneakyThrows
private
void
tryPutCacheId
(
final
long
id
)
{
cacheIdQueue
.
put
(
id
);
}
@SneakyThrows
private
long
tryTakeCacheId
()
{
return
cacheIdQueue
.
take
();
}
private
long
getStep
()
{
long
result
=
Long
.
parseLong
(
properties
.
getProperty
(
"step"
,
DEFAULT_STEP
));
Preconditions
.
checkArgument
(
result
>
0L
&&
result
<
Long
.
MAX_VALUE
);
...
...
sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/keygen/LeafSegmentKeyGeneratorTest.java
浏览文件 @
9fc2e916
...
...
@@ -17,7 +17,9 @@
package
org.apache.shardingsphere.orchestration.internal.keygen
;
import
org.junit.FixMethodOrder
;
import
org.junit.Test
;
import
org.junit.runners.MethodSorters
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
...
...
@@ -32,6 +34,7 @@ import java.util.concurrent.Executors;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
@FixMethodOrder
(
value
=
MethodSorters
.
NAME_ASCENDING
)
public
final
class
LeafSegmentKeyGeneratorTest
{
private
final
LeafSegmentKeyGenerator
leafSegmentKeyGenerator
=
new
LeafSegmentKeyGenerator
();
...
...
@@ -54,7 +57,7 @@ public final class LeafSegmentKeyGeneratorTest {
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"serverList"
,
"127.0.0.1:2181"
);
properties
.
setProperty
(
"initialValue"
,
"100001"
);
properties
.
setProperty
(
"step"
,
"
3
"
);
properties
.
setProperty
(
"step"
,
"
5
"
);
properties
.
setProperty
(
"digest"
,
""
);
properties
.
setProperty
(
"leafKey"
,
"test_table_1"
);
properties
.
setProperty
(
"registryCenterType"
,
"ThirdTestRegistryCenter"
);
...
...
@@ -67,6 +70,42 @@ public final class LeafSegmentKeyGeneratorTest {
assertThat
(
actual
,
is
(
expected
));
}
@Test
public
void
assertGenerateKeyWithFirstSpecialStep
()
{
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"serverList"
,
"127.0.0.1:2181"
);
properties
.
setProperty
(
"initialValue"
,
"100001"
);
properties
.
setProperty
(
"step"
,
"3"
);
properties
.
setProperty
(
"digest"
,
""
);
properties
.
setProperty
(
"leafKey"
,
"test_table_6"
);
properties
.
setProperty
(
"registryCenterType"
,
"ThirdTestRegistryCenter"
);
leafSegmentKeyGenerator
.
setProperties
(
properties
);
List
<
Comparable
<?>>
expected
=
Arrays
.<
Comparable
<?>>
asList
(
100001L
,
100002L
,
100003L
,
100004L
,
100005L
,
100006L
,
100007L
,
100008L
,
100009L
,
100010L
);
List
<
Comparable
<?>>
actual
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
actual
.
add
(
leafSegmentKeyGenerator
.
generateKey
());
}
assertThat
(
actual
,
is
(
expected
));
}
@Test
public
void
assertGenerateKeyWithSecondSpecialStep
()
{
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"serverList"
,
"127.0.0.1:2181"
);
properties
.
setProperty
(
"initialValue"
,
"100001"
);
properties
.
setProperty
(
"step"
,
"7"
);
properties
.
setProperty
(
"digest"
,
""
);
properties
.
setProperty
(
"leafKey"
,
"test_table_7"
);
properties
.
setProperty
(
"registryCenterType"
,
"ThirdTestRegistryCenter"
);
leafSegmentKeyGenerator
.
setProperties
(
properties
);
List
<
Comparable
<?>>
expected
=
Arrays
.<
Comparable
<?>>
asList
(
100001L
,
100002L
,
100003L
,
100004L
,
100005L
,
100006L
,
100007L
,
100008L
,
100009L
,
100010L
);
List
<
Comparable
<?>>
actual
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
actual
.
add
(
leafSegmentKeyGenerator
.
generateKey
());
}
assertThat
(
actual
,
is
(
expected
));
}
@Test
public
void
assertGenerateKeyWithMultipleThreads
()
throws
Exception
{
int
threadNumber
=
Runtime
.
getRuntime
().
availableProcessors
()
<<
1
;
...
...
@@ -127,7 +166,7 @@ public final class LeafSegmentKeyGeneratorTest {
properties
.
setProperty
(
"serverList"
,
"127.0.0.1:2181"
);
properties
.
setProperty
(
"initialValue"
,
"100001"
);
properties
.
setProperty
(
"digest"
,
""
);
properties
.
setProperty
(
"leafKey"
,
"test_table_
6
"
);
properties
.
setProperty
(
"leafKey"
,
"test_table_
4
"
);
properties
.
setProperty
(
"registryCenterType"
,
"ThirdTestRegistryCenter"
);
leafSegmentKeyGenerator
.
setProperties
(
properties
);
Set
<
Comparable
<?>>
actual
=
new
HashSet
<>();
...
...
@@ -152,7 +191,7 @@ public final class LeafSegmentKeyGeneratorTest {
properties
.
setProperty
(
"serverList"
,
"127.0.0.1:2181"
);
properties
.
setProperty
(
"step"
,
"3"
);
properties
.
setProperty
(
"digest"
,
""
);
properties
.
setProperty
(
"leafKey"
,
"test_table_
7
"
);
properties
.
setProperty
(
"leafKey"
,
"test_table_
5
"
);
properties
.
setProperty
(
"registryCenterType"
,
"ThirdTestRegistryCenter"
);
leafSegmentKeyGenerator
.
setProperties
(
properties
);
int
taskNumber
=
threadNumber
<<
2
;
...
...
sharding-orchestration/sharding-orchestration-zookeeper-curator-integration-test/src/test/java/org/apache/shardingsphere/orchestration/zookeeper/curator/integration/test/LeafSegmentKeyGeneratorIT.java
浏览文件 @
9fc2e916
...
...
@@ -20,7 +20,9 @@ package org.apache.shardingsphere.orchestration.zookeeper.curator.integration.te
import
org.apache.shardingsphere.orchestration.internal.keygen.LeafSegmentKeyGenerator
;
import
org.apache.shardingsphere.orchestration.zookeeper.curator.integration.util.EmbedTestingServer
;
import
org.junit.BeforeClass
;
import
org.junit.FixMethodOrder
;
import
org.junit.Test
;
import
org.junit.runners.MethodSorters
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
...
...
@@ -40,6 +42,7 @@ import static org.junit.Assert.assertThat;
*
* @author wangguangyuan
*/
@FixMethodOrder
(
value
=
MethodSorters
.
NAME_ASCENDING
)
public
final
class
LeafSegmentKeyGeneratorIT
{
private
final
LeafSegmentKeyGenerator
leafSegmentKeyGenerator
=
new
LeafSegmentKeyGenerator
();
...
...
@@ -67,6 +70,78 @@ public final class LeafSegmentKeyGeneratorIT {
assertThat
(
actual
,
is
(
expected
));
}
@Test
public
void
assertGenerateKeyWithFirstSpecialStep
()
{
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"serverList"
,
"127.0.0.1:3181"
);
properties
.
setProperty
(
"initialValue"
,
"100001"
);
properties
.
setProperty
(
"step"
,
"5"
);
properties
.
setProperty
(
"digest"
,
""
);
properties
.
setProperty
(
"leafKey"
,
"test_table_6"
);
properties
.
setProperty
(
"registryCenterType"
,
"zookeeper"
);
leafSegmentKeyGenerator
.
setProperties
(
properties
);
List
<
Comparable
<?>>
expected
=
Arrays
.<
Comparable
<?>>
asList
(
100001L
,
100002L
,
100003L
,
100004L
,
100005L
,
100006L
,
100007L
,
100008L
,
100009L
,
100010L
);
List
<
Comparable
<?>>
actual
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
actual
.
add
(
leafSegmentKeyGenerator
.
generateKey
());
}
assertThat
(
actual
,
is
(
expected
));
}
@Test
public
void
assertGenerateKeyWithFirstSpecialStepAgain
()
{
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"serverList"
,
"127.0.0.1:3181"
);
properties
.
setProperty
(
"initialValue"
,
"100001"
);
properties
.
setProperty
(
"step"
,
"5"
);
properties
.
setProperty
(
"digest"
,
""
);
properties
.
setProperty
(
"leafKey"
,
"test_table_6"
);
properties
.
setProperty
(
"registryCenterType"
,
"zookeeper"
);
leafSegmentKeyGenerator
.
setProperties
(
properties
);
List
<
Comparable
<?>>
expected
=
Arrays
.<
Comparable
<?>>
asList
(
100011L
,
100012L
,
100013L
,
100014L
,
100015L
,
100016L
,
100017L
,
100018L
,
100019L
,
100020L
);
List
<
Comparable
<?>>
actual
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
actual
.
add
(
leafSegmentKeyGenerator
.
generateKey
());
}
assertThat
(
actual
,
is
(
expected
));
}
@Test
public
void
assertGenerateKeyWithSecondSpecialStep
()
{
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"serverList"
,
"127.0.0.1:3181"
);
properties
.
setProperty
(
"initialValue"
,
"100001"
);
properties
.
setProperty
(
"step"
,
"7"
);
properties
.
setProperty
(
"digest"
,
""
);
properties
.
setProperty
(
"leafKey"
,
"test_table_7"
);
properties
.
setProperty
(
"registryCenterType"
,
"zookeeper"
);
leafSegmentKeyGenerator
.
setProperties
(
properties
);
List
<
Comparable
<?>>
expected
=
Arrays
.<
Comparable
<?>>
asList
(
100001L
,
100002L
,
100003L
,
100004L
,
100005L
,
100006L
,
100007L
,
100008L
,
100009L
,
100010L
);
List
<
Comparable
<?>>
actual
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
actual
.
add
(
leafSegmentKeyGenerator
.
generateKey
());
}
assertThat
(
actual
,
is
(
expected
));
}
@Test
public
void
assertGenerateKeyWithSecondSpecialStepAgain
()
{
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"serverList"
,
"127.0.0.1:3181"
);
properties
.
setProperty
(
"initialValue"
,
"100001"
);
properties
.
setProperty
(
"step"
,
"7"
);
properties
.
setProperty
(
"digest"
,
""
);
properties
.
setProperty
(
"leafKey"
,
"test_table_7"
);
properties
.
setProperty
(
"registryCenterType"
,
"zookeeper"
);
leafSegmentKeyGenerator
.
setProperties
(
properties
);
List
<
Comparable
<?>>
expected
=
Arrays
.<
Comparable
<?>>
asList
(
100017L
,
100018L
,
100019L
,
100020L
,
100021L
,
100022L
,
100023L
,
100024L
,
100025L
,
100026L
);
List
<
Comparable
<?>>
actual
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
actual
.
add
(
leafSegmentKeyGenerator
.
generateKey
());
}
assertThat
(
actual
,
is
(
expected
));
}
@Test
public
void
assertGenerateKeyWithMultipleThreads
()
throws
Exception
{
int
threadNumber
=
Runtime
.
getRuntime
().
availableProcessors
()
<<
1
;
...
...
@@ -127,7 +202,7 @@ public final class LeafSegmentKeyGeneratorIT {
properties
.
setProperty
(
"serverList"
,
"127.0.0.1:3181"
);
properties
.
setProperty
(
"initialValue"
,
"100001"
);
properties
.
setProperty
(
"digest"
,
""
);
properties
.
setProperty
(
"leafKey"
,
"test_table_
6
"
);
properties
.
setProperty
(
"leafKey"
,
"test_table_
4
"
);
properties
.
setProperty
(
"registryCenterType"
,
"zookeeper"
);
leafSegmentKeyGenerator
.
setProperties
(
properties
);
Set
<
Comparable
<?>>
actual
=
new
HashSet
<>();
...
...
@@ -152,7 +227,7 @@ public final class LeafSegmentKeyGeneratorIT {
properties
.
setProperty
(
"serverList"
,
"127.0.0.1:3181"
);
properties
.
setProperty
(
"step"
,
"3"
);
properties
.
setProperty
(
"digest"
,
""
);
properties
.
setProperty
(
"leafKey"
,
"test_table_
7
"
);
properties
.
setProperty
(
"leafKey"
,
"test_table_
5
"
);
properties
.
setProperty
(
"registryCenterType"
,
"zookeeper"
);
leafSegmentKeyGenerator
.
setProperties
(
properties
);
int
taskNumber
=
threadNumber
<<
2
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录