Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
81124ed9
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,体验更适合开发者的 AI 搜索 >>
未验证
提交
81124ed9
编写于
1月 10, 2019
作者:
ShardingSphere
提交者:
GitHub
1月 10, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1728 from tristaZero/dev
Modify API for key generator
上级
76653b17
be14f2c5
变更
42
隐藏空白更改
内联
并排
Showing
42 changed file
with
506 addition
and
139 deletion
+506
-139
sharding-core/src/main/java/io/shardingsphere/core/keygen/KeyGenerator.java
...main/java/io/shardingsphere/core/keygen/KeyGenerator.java
+16
-0
sharding-core/src/main/java/io/shardingsphere/core/keygen/KeyGeneratorType.java
.../java/io/shardingsphere/core/keygen/KeyGeneratorType.java
+53
-0
sharding-core/src/main/java/io/shardingsphere/core/keygen/SnowflakeKeyGenerator.java
.../io/shardingsphere/core/keygen/SnowflakeKeyGenerator.java
+23
-25
sharding-core/src/main/java/io/shardingsphere/core/rule/ShardingRule.java
...c/main/java/io/shardingsphere/core/rule/ShardingRule.java
+2
-2
sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlKeyGeneratorConfiguration.java
...ere/core/yaml/sharding/YamlKeyGeneratorConfiguration.java
+77
-0
sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlShardingRuleConfiguration.java
...ere/core/yaml/sharding/YamlShardingRuleConfiguration.java
+19
-5
sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlTableRuleConfiguration.java
...sphere/core/yaml/sharding/YamlTableRuleConfiguration.java
+21
-11
sharding-core/src/test/java/io/shardingsphere/core/AllCoreTests.java
...re/src/test/java/io/shardingsphere/core/AllCoreTests.java
+3
-1
sharding-core/src/test/java/io/shardingsphere/core/keygen/AllKeygenTests.java
...st/java/io/shardingsphere/core/keygen/AllKeygenTests.java
+1
-1
sharding-core/src/test/java/io/shardingsphere/core/keygen/KeyGeneratorFactoryTest.java
...o/shardingsphere/core/keygen/KeyGeneratorFactoryTest.java
+13
-1
sharding-core/src/test/java/io/shardingsphere/core/keygen/KeyGeneratorTypeTest.java
...a/io/shardingsphere/core/keygen/KeyGeneratorTypeTest.java
+40
-0
sharding-core/src/test/java/io/shardingsphere/core/keygen/SnowflakeKeyGeneratorTest.java
...shardingsphere/core/keygen/SnowflakeKeyGeneratorTest.java
+49
-29
sharding-core/src/test/java/io/shardingsphere/core/keygen/fixture/FixedTimeService.java
.../shardingsphere/core/keygen/fixture/FixedTimeService.java
+2
-2
sharding-core/src/test/java/io/shardingsphere/core/keygen/fixture/IncrementKeyGenerator.java
...dingsphere/core/keygen/fixture/IncrementKeyGenerator.java
+7
-0
sharding-core/src/test/java/io/shardingsphere/core/rule/ShardingRuleTest.java
...st/java/io/shardingsphere/core/rule/ShardingRuleTest.java
+2
-2
sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlKeyGeneratorConfigurationTest.java
...core/yaml/sharding/YamlKeyGeneratorConfigurationTest.java
+69
-0
sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlShardingConfigurationTest.java
...ere/core/yaml/sharding/YamlShardingConfigurationTest.java
+4
-4
sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlShardingRuleConfigurationTest.java
...core/yaml/sharding/YamlShardingRuleConfigurationTest.java
+5
-3
sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlTableRuleConfigurationTest.java
...re/core/yaml/sharding/YamlTableRuleConfigurationTest.java
+5
-4
sharding-core/src/test/resources/yaml/optimize-rule.yaml
sharding-core/src/test/resources/yaml/optimize-rule.yaml
+3
-2
sharding-core/src/test/resources/yaml/parser-rule.yaml
sharding-core/src/test/resources/yaml/parser-rule.yaml
+5
-2
sharding-core/src/test/resources/yaml/rewrite-rule.yaml
sharding-core/src/test/resources/yaml/rewrite-rule.yaml
+3
-1
sharding-core/src/test/resources/yaml/sharding-rule.yaml
sharding-core/src/test/resources/yaml/sharding-rule.yaml
+5
-3
sharding-jdbc/sharding-jdbc-core/src/test/java/io/shardingsphere/dbtest/fixture/ConstantKeyGenerator.java
...o/shardingsphere/dbtest/fixture/ConstantKeyGenerator.java
+8
-0
sharding-jdbc/sharding-jdbc-core/src/test/java/io/shardingsphere/shardingjdbc/fixture/IncrementKeyGenerator.java
...ingsphere/shardingjdbc/fixture/IncrementKeyGenerator.java
+7
-0
sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/db/sharding-rule.yaml
...re/src/test/resources/integrate/env/db/sharding-rule.yaml
+3
-2
sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/dbtbl_with_masterslave/sharding-rule.yaml
...s/integrate/env/dbtbl_with_masterslave/sharding-rule.yaml
+3
-2
sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/tbl/sharding-rule.yaml
...e/src/test/resources/integrate/env/tbl/sharding-rule.yaml
+3
-2
sharding-jdbc/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/shardingjdbc/orchestration/api/yaml/fixture/DecrementKeyGenerator.java
...orchestration/api/yaml/fixture/DecrementKeyGenerator.java
+7
-0
sharding-jdbc/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/shardingjdbc/orchestration/api/yaml/fixture/IncrementKeyGenerator.java
...orchestration/api/yaml/fixture/IncrementKeyGenerator.java
+7
-0
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithDataSourceWithProps.yaml
...aml/integrate/sharding/configWithDataSourceWithProps.yaml
+3
-2
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithDataSourceWithoutProps.yaml
.../integrate/sharding/configWithDataSourceWithoutProps.yaml
+3
-2
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithoutDataSourceWithProps.yaml
.../integrate/sharding/configWithoutDataSourceWithProps.yaml
+3
-2
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithoutDataSourceWithoutProps.yaml
...tegrate/sharding/configWithoutDataSourceWithoutProps.yaml
+3
-2
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithDataSourceWithProps.yaml
.../integrate/sharding_ms/configWithDataSourceWithProps.yaml
+3
-2
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithDataSourceWithoutProps.yaml
...tegrate/sharding_ms/configWithDataSourceWithoutProps.yaml
+3
-2
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithoutDataSourceWithProps.yaml
...tegrate/sharding_ms/configWithoutDataSourceWithProps.yaml
+3
-2
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithoutDataSourceWithoutProps.yaml
...rate/sharding_ms/configWithoutDataSourceWithoutProps.yaml
+3
-2
sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/internal/registry/config/service/ConfigurationServiceTest.java
...nal/registry/config/service/ConfigurationServiceTest.java
+3
-9
sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/yaml/AllYamlTests.java
...va/io/shardingsphere/orchestration/yaml/AllYamlTests.java
+1
-0
sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/yaml/ConfigurationYamlConverterTest.java
...re/orchestration/yaml/ConfigurationYamlConverterTest.java
+6
-6
sharding-proxy/src/main/resources/conf/config-sharding.yaml
sharding-proxy/src/main/resources/conf/config-sharding.yaml
+7
-4
未找到文件。
sharding-core/src/main/java/io/shardingsphere/core/keygen/KeyGenerator.java
浏览文件 @
81124ed9
...
...
@@ -17,6 +17,8 @@
package
io.shardingsphere.core.keygen
;
import
java.util.Properties
;
/**
* Key generator interface.
*
...
...
@@ -31,4 +33,18 @@ public interface KeyGenerator {
* @return generated key
*/
Comparable
<?>
generateKey
();
/**
* Get properties.
*
* @return The properties of key generator
*/
Properties
getProperties
();
/**
* Set Properties.
*
* @param properties properties
*/
void
setProperties
(
Properties
properties
);
}
sharding-core/src/main/java/io/shardingsphere/core/keygen/KeyGeneratorType.java
0 → 100644
浏览文件 @
81124ed9
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/
package
io.shardingsphere.core.keygen
;
import
com.google.common.base.Optional
;
import
lombok.Getter
;
import
lombok.RequiredArgsConstructor
;
/**
* Key generator type.
*
* @author panjuan
*/
@RequiredArgsConstructor
@Getter
public
enum
KeyGeneratorType
{
SNOWFLAKE
(
"io.shardingsphere.core.keygen.SnowflakeKeyGenerator"
),
UUID
(
""
),
LEAF
(
""
);
private
final
String
keyGeneratorClassName
;
/**
* Get key generator type.
*
* @param keyGeneratorClassName key generator class name
* @return key generator type
*/
public
static
Optional
<
KeyGeneratorType
>
getKeyGeneratorType
(
final
String
keyGeneratorClassName
)
{
for
(
KeyGeneratorType
each
:
KeyGeneratorType
.
values
())
{
if
(
each
.
getKeyGeneratorClassName
().
equals
(
keyGeneratorClassName
))
{
return
Optional
.
of
(
each
);
}
}
return
Optional
.
absent
();
}
}
sharding-core/src/main/java/io/shardingsphere/core/keygen/
Default
KeyGenerator.java
→
sharding-core/src/main/java/io/shardingsphere/core/keygen/
Snowflake
KeyGenerator.java
浏览文件 @
81124ed9
...
...
@@ -18,36 +18,39 @@
package
io.shardingsphere.core.keygen
;
import
com.google.common.base.Preconditions
;
import
lombok.Getter
;
import
lombok.Setter
;
import
lombok.SneakyThrows
;
import
java.util.Calendar
;
import
java.util.Properties
;
/**
*
Default
distributed primary key generator.
*
Snowflake
distributed primary key generator.
*
* <p>
* Use snowflake algorithm. Length is 64 bit.
* </p>
*
* <pre>
* 1bit
sign bit.
* 1bit sign bit.
* 41bits timestamp offset from 2016.11.01(ShardingSphere distributed primary key published data) to now.
* 10bits worker process id.
* 12bits auto increment offset in one mills
* </pre>
*
* <p>
* Call @{@code
Default
KeyGenerator.setWorkerId} to set worker id, default value is 0.
* Call @{@code
Snowflake
KeyGenerator.setWorkerId} to set worker id, default value is 0.
* </p>
*
* <p>
* Call @{@code
Default
KeyGenerator.setMaxTolerateTimeDifferenceMilliseconds} to set max tolerate time difference milliseconds, default value is 0.
* Call @{@code
Snowflake
KeyGenerator.setMaxTolerateTimeDifferenceMilliseconds} to set max tolerate time difference milliseconds, default value is 0.
* </p>
*
* @author gaohongtao
* @author panjuan
*/
public
final
class
Default
KeyGenerator
implements
KeyGenerator
{
public
final
class
Snowflake
KeyGenerator
implements
KeyGenerator
{
public
static
final
long
EPOCH
;
...
...
@@ -63,12 +66,16 @@ public final class DefaultKeyGenerator implements KeyGenerator {
private
static
final
long
WORKER_ID_MAX_VALUE
=
1L
<<
WORKER_ID_BITS
;
private
static
final
long
WORKER_ID
=
0
;
private
static
final
int
MAX_TOLERATE_TIME_DIFFERENCE_MILLISECONDS
=
10
;
@Setter
private
static
TimeService
timeService
=
new
TimeService
();
private
static
long
workerId
;
private
static
int
maxTolerateTimeDifferenceMilliseconds
=
10
;
@Getter
@Setter
private
Properties
properties
=
new
Properties
()
;
static
{
Calendar
calendar
=
Calendar
.
getInstance
();
...
...
@@ -86,23 +93,14 @@ public final class DefaultKeyGenerator implements KeyGenerator {
private
long
lastMilliseconds
;
/**
* Set work process id.
*
* @param workerId work process id
*/
public
static
void
setWorkerId
(
final
long
workerId
)
{
Preconditions
.
checkArgument
(
workerId
>=
0L
&&
workerId
<
WORKER_ID_MAX_VALUE
);
DefaultKeyGenerator
.
workerId
=
workerId
;
private
long
getWorkerId
()
{
long
result
=
Long
.
valueOf
(
properties
.
getProperty
(
"worker.id"
,
String
.
valueOf
(
WORKER_ID
)));
Preconditions
.
checkArgument
(
result
>=
0L
&&
result
<
WORKER_ID_MAX_VALUE
);
return
result
;
}
/**
* Set max tolerate time difference milliseconds.
*
* @param maxTolerateTimeDifferenceMilliseconds max tolerate time difference milliseconds
*/
public
static
void
setMaxTolerateTimeDifferenceMilliseconds
(
final
int
maxTolerateTimeDifferenceMilliseconds
)
{
DefaultKeyGenerator
.
maxTolerateTimeDifferenceMilliseconds
=
maxTolerateTimeDifferenceMilliseconds
;
private
int
getMaxTolerateTimeDifferenceMilliseconds
()
{
return
Integer
.
valueOf
(
properties
.
getProperty
(
"max.tolerate.time.difference.milliseconds"
,
String
.
valueOf
(
MAX_TOLERATE_TIME_DIFFERENCE_MILLISECONDS
)));
}
/**
...
...
@@ -125,7 +123,7 @@ public final class DefaultKeyGenerator implements KeyGenerator {
sequence
=
sequenceOffset
;
}
lastMilliseconds
=
currentMilliseconds
;
return
((
currentMilliseconds
-
EPOCH
)
<<
TIMESTAMP_LEFT_SHIFT_BITS
)
|
(
workerId
<<
WORKER_ID_LEFT_SHIFT_BITS
)
|
sequence
;
return
((
currentMilliseconds
-
EPOCH
)
<<
TIMESTAMP_LEFT_SHIFT_BITS
)
|
(
getWorkerId
()
<<
WORKER_ID_LEFT_SHIFT_BITS
)
|
sequence
;
}
@SneakyThrows
...
...
@@ -134,7 +132,7 @@ public final class DefaultKeyGenerator implements KeyGenerator {
return
false
;
}
long
timeDifferenceMilliseconds
=
lastMilliseconds
-
currentMilliseconds
;
Preconditions
.
checkState
(
timeDifferenceMilliseconds
<
maxTolerateTimeDifferenceMilliseconds
,
Preconditions
.
checkState
(
timeDifferenceMilliseconds
<
getMaxTolerateTimeDifferenceMilliseconds
()
,
"Clock is moving backwards, last time is %d milliseconds, current time is %d milliseconds"
,
lastMilliseconds
,
currentMilliseconds
);
Thread
.
sleep
(
timeDifferenceMilliseconds
);
return
true
;
...
...
sharding-core/src/main/java/io/shardingsphere/core/rule/ShardingRule.java
浏览文件 @
81124ed9
...
...
@@ -27,7 +27,7 @@ import io.shardingsphere.api.config.rule.TableRuleConfiguration;
import
io.shardingsphere.api.config.strategy.ShardingStrategyConfiguration
;
import
io.shardingsphere.core.exception.ShardingConfigurationException
;
import
io.shardingsphere.core.exception.ShardingException
;
import
io.shardingsphere.core.keygen.
Default
KeyGenerator
;
import
io.shardingsphere.core.keygen.
Snowflake
KeyGenerator
;
import
io.shardingsphere.core.keygen.KeyGenerator
;
import
io.shardingsphere.core.parsing.parser.context.condition.Column
;
import
io.shardingsphere.core.routing.strategy.ShardingStrategy
;
...
...
@@ -111,7 +111,7 @@ public class ShardingRule {
}
private
KeyGenerator
createDefaultKeyGenerator
(
final
KeyGenerator
defaultKeyGenerator
)
{
return
null
==
defaultKeyGenerator
?
new
Default
KeyGenerator
()
:
defaultKeyGenerator
;
return
null
==
defaultKeyGenerator
?
new
Snowflake
KeyGenerator
()
:
defaultKeyGenerator
;
}
private
Collection
<
MasterSlaveRule
>
createMasterSlaveRules
(
final
Collection
<
MasterSlaveRuleConfiguration
>
masterSlaveRuleConfigurations
)
{
...
...
sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlKeyGeneratorConfiguration.java
0 → 100644
浏览文件 @
81124ed9
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/
package
io.shardingsphere.core.yaml.sharding
;
import
com.google.common.base.Strings
;
import
io.shardingsphere.core.exception.ShardingConfigurationException
;
import
io.shardingsphere.core.keygen.KeyGenerator
;
import
io.shardingsphere.core.keygen.KeyGeneratorFactory
;
import
io.shardingsphere.core.keygen.KeyGeneratorType
;
import
lombok.Getter
;
import
lombok.Setter
;
import
java.util.Properties
;
/**
* Yaml key generator configuration.
*
* @author panjuan
*/
@Getter
@Setter
public
final
class
YamlKeyGeneratorConfiguration
{
private
String
column
;
private
String
type
;
private
String
className
;
private
Properties
props
=
new
Properties
();
/**
* Build table rule configuration.
*
* @return table rule configuration
*/
public
KeyGenerator
getKeyGenerator
()
{
KeyGenerator
result
;
if
(!
Strings
.
isNullOrEmpty
(
className
))
{
result
=
KeyGeneratorFactory
.
newInstance
(
className
);
}
else
if
(!
Strings
.
isNullOrEmpty
(
type
))
{
result
=
KeyGeneratorFactory
.
newInstance
(
getKeyGeneratorClassName
());
}
else
{
result
=
KeyGeneratorFactory
.
newInstance
(
KeyGeneratorType
.
SNOWFLAKE
.
getKeyGeneratorClassName
());
}
result
.
setProperties
(
props
);
return
result
;
}
private
String
getKeyGeneratorClassName
()
{
if
(
type
.
equalsIgnoreCase
(
KeyGeneratorType
.
SNOWFLAKE
.
name
()))
{
return
KeyGeneratorType
.
SNOWFLAKE
.
getKeyGeneratorClassName
();
}
if
(
type
.
equalsIgnoreCase
(
KeyGeneratorType
.
UUID
.
name
()))
{
return
KeyGeneratorType
.
UUID
.
getKeyGeneratorClassName
();
}
if
(
type
.
equalsIgnoreCase
(
KeyGeneratorType
.
LEAF
.
name
()))
{
return
KeyGeneratorType
.
LEAF
.
getKeyGeneratorClassName
();
}
throw
new
ShardingConfigurationException
(
"Invalid key generator type."
);
}
}
sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlShardingRuleConfiguration.java
浏览文件 @
81124ed9
...
...
@@ -17,10 +17,11 @@
package
io.shardingsphere.core.yaml.sharding
;
import
com.google.common.base.Optional
;
import
io.shardingsphere.api.config.rule.MasterSlaveRuleConfiguration
;
import
io.shardingsphere.api.config.rule.ShardingRuleConfiguration
;
import
io.shardingsphere.api.config.rule.TableRuleConfiguration
;
import
io.shardingsphere.core.keygen.KeyGenerator
Factory
;
import
io.shardingsphere.core.keygen.KeyGenerator
Type
;
import
io.shardingsphere.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration
;
import
lombok.Getter
;
import
lombok.NoArgsConstructor
;
...
...
@@ -57,7 +58,7 @@ public class YamlShardingRuleConfiguration {
private
YamlShardingStrategyConfiguration
defaultTableStrategy
;
private
String
defaultKeyGeneratorClassName
;
private
YamlKeyGeneratorConfiguration
defaultKeyGenerator
;
private
Map
<
String
,
YamlMasterSlaveRuleConfiguration
>
masterSlaveRules
=
new
LinkedHashMap
<>();
...
...
@@ -70,12 +71,25 @@ public class YamlShardingRuleConfiguration {
bindingTables
.
addAll
(
shardingRuleConfiguration
.
getBroadcastTables
());
defaultDatabaseStrategy
=
new
YamlShardingStrategyConfiguration
(
shardingRuleConfiguration
.
getDefaultDatabaseShardingStrategyConfig
());
defaultTableStrategy
=
new
YamlShardingStrategyConfiguration
(
shardingRuleConfiguration
.
getDefaultTableShardingStrategyConfig
());
defaultKeyGenerator
ClassName
=
null
==
shardingRuleConfiguration
.
getDefaultKeyGenerator
()
?
null
:
shardingRuleConfiguration
.
getDefaultKeyGenerator
().
getClass
().
getName
(
);
defaultKeyGenerator
=
null
==
shardingRuleConfiguration
.
getDefaultKeyGenerator
()
?
null
:
getYamlKeyGeneratorConfiguration
(
shardingRuleConfiguration
);
for
(
MasterSlaveRuleConfiguration
each
:
shardingRuleConfiguration
.
getMasterSlaveRuleConfigs
())
{
masterSlaveRules
.
put
(
each
.
getName
(),
new
YamlMasterSlaveRuleConfiguration
(
each
));
}
}
private
YamlKeyGeneratorConfiguration
getYamlKeyGeneratorConfiguration
(
final
ShardingRuleConfiguration
shardingRuleConfiguration
)
{
YamlKeyGeneratorConfiguration
result
=
new
YamlKeyGeneratorConfiguration
();
String
keyGeneratorClassName
=
shardingRuleConfiguration
.
getDefaultKeyGenerator
().
getClass
().
getName
();
Optional
<
KeyGeneratorType
>
keyGeneratorType
=
KeyGeneratorType
.
getKeyGeneratorType
(
keyGeneratorClassName
);
if
(!
keyGeneratorType
.
isPresent
())
{
result
.
setClassName
(
keyGeneratorClassName
);
}
else
{
result
.
setType
(
keyGeneratorType
.
get
().
name
());
}
result
.
setProps
(
shardingRuleConfiguration
.
getDefaultKeyGenerator
().
getProperties
());
return
result
;
}
/**
* Get sharding rule configuration.
*
...
...
@@ -97,8 +111,8 @@ public class YamlShardingRuleConfiguration {
if
(
null
!=
defaultTableStrategy
)
{
result
.
setDefaultTableShardingStrategyConfig
(
defaultTableStrategy
.
build
());
}
if
(
null
!=
defaultKeyGenerator
ClassName
)
{
result
.
setDefaultKeyGenerator
(
KeyGeneratorFactory
.
newInstance
(
defaultKeyGeneratorClassName
));
if
(
null
!=
defaultKeyGenerator
)
{
result
.
setDefaultKeyGenerator
(
defaultKeyGenerator
.
getKeyGenerator
(
));
}
Collection
<
MasterSlaveRuleConfiguration
>
masterSlaveRuleConfigs
=
new
LinkedList
<>();
for
(
Entry
<
String
,
YamlMasterSlaveRuleConfiguration
>
entry
:
masterSlaveRules
.
entrySet
())
{
...
...
sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlTableRuleConfiguration.java
浏览文件 @
81124ed9
...
...
@@ -17,10 +17,10 @@
package
io.shardingsphere.core.yaml.sharding
;
import
com.google.common.base.Optional
;
import
com.google.common.base.Preconditions
;
import
com.google.common.base.Strings
;
import
io.shardingsphere.api.config.rule.TableRuleConfiguration
;
import
io.shardingsphere.core.keygen.KeyGenerator
Factory
;
import
io.shardingsphere.core.keygen.KeyGenerator
Type
;
import
lombok.Getter
;
import
lombok.NoArgsConstructor
;
import
lombok.Setter
;
...
...
@@ -44,9 +44,7 @@ public class YamlTableRuleConfiguration {
private
YamlShardingStrategyConfiguration
tableStrategy
;
private
String
keyGeneratorColumnName
;
private
String
keyGeneratorClassName
;
private
YamlKeyGeneratorConfiguration
keyGenerator
;
private
String
logicIndex
;
...
...
@@ -55,9 +53,21 @@ public class YamlTableRuleConfiguration {
actualDataNodes
=
tableRuleConfiguration
.
getActualDataNodes
();
databaseStrategy
=
new
YamlShardingStrategyConfiguration
(
tableRuleConfiguration
.
getDatabaseShardingStrategyConfig
());
tableStrategy
=
new
YamlShardingStrategyConfiguration
(
tableRuleConfiguration
.
getTableShardingStrategyConfig
());
keyGeneratorColumnName
=
tableRuleConfiguration
.
getKeyGeneratorColumnName
();
keyGeneratorClassName
=
null
==
tableRuleConfiguration
.
getKeyGenerator
()
?
null
:
tableRuleConfiguration
.
getKeyGenerator
().
getClass
().
getName
();
keyGenerator
=
null
==
tableRuleConfiguration
.
getKeyGenerator
()
?
null
:
getYamlKeyGeneratorConfiguration
(
tableRuleConfiguration
);
}
private
YamlKeyGeneratorConfiguration
getYamlKeyGeneratorConfiguration
(
final
TableRuleConfiguration
tableRuleConfiguration
)
{
YamlKeyGeneratorConfiguration
result
=
new
YamlKeyGeneratorConfiguration
();
String
keyGeneratorClassName
=
tableRuleConfiguration
.
getKeyGenerator
().
getClass
().
getName
();
Optional
<
KeyGeneratorType
>
keyGeneratorType
=
KeyGeneratorType
.
getKeyGeneratorType
(
keyGeneratorClassName
);
if
(!
keyGeneratorType
.
isPresent
())
{
result
.
setClassName
(
keyGeneratorClassName
);
}
else
{
result
.
setType
(
keyGeneratorType
.
get
().
name
());
}
result
.
setColumn
(
tableRuleConfiguration
.
getKeyGeneratorColumnName
());
result
.
setProps
(
tableRuleConfiguration
.
getKeyGenerator
().
getProperties
());
return
result
;
}
/**
...
...
@@ -76,10 +86,10 @@ public class YamlTableRuleConfiguration {
if
(
null
!=
tableStrategy
)
{
result
.
setTableShardingStrategyConfig
(
tableStrategy
.
build
());
}
if
(!
Strings
.
isNullOrEmpty
(
keyGeneratorClassName
))
{
result
.
setKeyGenerator
(
KeyGeneratorFactory
.
newInstance
(
keyGeneratorClassName
));
if
(
null
!=
keyGenerator
)
{
result
.
setKeyGenerator
(
keyGenerator
.
getKeyGenerator
());
result
.
setKeyGeneratorColumnName
(
keyGenerator
.
getColumn
());
}
result
.
setKeyGeneratorColumnName
(
keyGeneratorColumnName
);
result
.
setLogicIndex
(
logicIndex
);
return
result
;
}
...
...
sharding-core/src/test/java/io/shardingsphere/core/AllCoreTests.java
浏览文件 @
81124ed9
...
...
@@ -21,6 +21,7 @@ import io.shardingsphere.core.config.AllConfigTests;
import
io.shardingsphere.core.constant.AllConstantsTests
;
import
io.shardingsphere.core.executor.AllExecutorTests
;
import
io.shardingsphere.core.hint.AllHintTests
;
import
io.shardingsphere.core.keygen.AllKeygenTests
;
import
io.shardingsphere.core.merger.AllMergerTests
;
import
io.shardingsphere.core.metadata.AllMetaDataTests
;
import
io.shardingsphere.core.optimizer.AllOptimizerTests
;
...
...
@@ -46,7 +47,8 @@ import org.junit.runners.Suite.SuiteClasses;
AllExecutorTests
.
class
,
AllMergerTests
.
class
,
AllHintTests
.
class
,
AllYamlTests
.
class
AllYamlTests
.
class
,
AllKeygenTests
.
class
})
public
final
class
AllCoreTests
{
}
sharding-core/src/test/java/io/shardingsphere/core/keygen/AllKeygenTests.java
浏览文件 @
81124ed9
...
...
@@ -23,7 +23,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith
(
Suite
.
class
)
@SuiteClasses
({
Default
KeyGeneratorTest
.
class
,
Snowflake
KeyGeneratorTest
.
class
,
KeyGeneratorFactoryTest
.
class
})
public
final
class
AllKeygenTests
{
...
...
sharding-core/src/test/java/io/shardingsphere/core/keygen/KeyGeneratorFactoryTest.java
浏览文件 @
81124ed9
...
...
@@ -18,10 +18,14 @@
package
io.shardingsphere.core.keygen
;
import
lombok.AccessLevel
;
import
lombok.Getter
;
import
lombok.NoArgsConstructor
;
import
lombok.RequiredArgsConstructor
;
import
lombok.Setter
;
import
org.junit.Test
;
import
java.util.Properties
;
import
static
org
.
hamcrest
.
CoreMatchers
.
instanceOf
;
import
static
org
.
junit
.
Assert
.
assertThat
;
...
...
@@ -29,7 +33,7 @@ public final class KeyGeneratorFactoryTest {
@Test
public
void
assertCreateKeyGeneratorSuccess
()
{
assertThat
(
KeyGeneratorFactory
.
newInstance
(
DefaultKeyGenerator
.
class
.
getName
()),
instanceOf
(
Default
KeyGenerator
.
class
));
assertThat
(
KeyGeneratorFactory
.
newInstance
(
SnowflakeKeyGenerator
.
class
.
getName
()),
instanceOf
(
Snowflake
KeyGenerator
.
class
));
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
...
...
@@ -46,6 +50,10 @@ public final class KeyGeneratorFactoryTest {
public
static
final
class
InstantiationKeyGenerator
implements
KeyGenerator
{
private
final
int
field
;
@Getter
@Setter
private
Properties
properties
=
new
Properties
();
@Override
public
Comparable
<?>
generateKey
()
{
...
...
@@ -55,6 +63,10 @@ public final class KeyGeneratorFactoryTest {
@NoArgsConstructor
(
access
=
AccessLevel
.
PRIVATE
)
public
static
final
class
IllegalAccessKeyGenerator
implements
KeyGenerator
{
@Getter
@Setter
private
Properties
properties
=
new
Properties
();
@Override
public
Comparable
<?>
generateKey
()
{
...
...
sharding-core/src/test/java/io/shardingsphere/core/keygen/KeyGeneratorTypeTest.java
0 → 100644
浏览文件 @
81124ed9
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/
package
io.shardingsphere.core.keygen
;
import
com.google.common.base.Optional
;
import
org.junit.Test
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
public
class
KeyGeneratorTypeTest
{
@Test
public
void
assertGetKeyGeneratorType
()
{
assertThat
(
KeyGeneratorType
.
getKeyGeneratorType
(
"io.shardingsphere.core.keygen.SnowflakeKeyGenerator"
),
is
(
Optional
.
of
(
KeyGeneratorType
.
SNOWFLAKE
)));
assertThat
(
KeyGeneratorType
.
getKeyGeneratorType
(
""
),
is
(
Optional
.
of
(
KeyGeneratorType
.
UUID
)));
}
@Test
public
void
assertGetKeyGeneratorClassName
()
{
assertThat
(
KeyGeneratorType
.
SNOWFLAKE
.
getKeyGeneratorClassName
(),
is
(
"io.shardingsphere.core.keygen.SnowflakeKeyGenerator"
));
}
}
sharding-core/src/test/java/io/shardingsphere/core/keygen/
Default
KeyGeneratorTest.java
→
sharding-core/src/test/java/io/shardingsphere/core/keygen/
Snowflake
KeyGeneratorTest.java
浏览文件 @
81124ed9
...
...
@@ -26,6 +26,7 @@ import java.util.ArrayList;
import
java.util.Arrays
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Properties
;
import
java.util.Set
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.ExecutorService
;
...
...
@@ -35,11 +36,11 @@ import static org.hamcrest.CoreMatchers.is;
import
static
org
.
junit
.
Assert
.
assertNotEquals
;
import
static
org
.
junit
.
Assert
.
assertThat
;
public
final
class
Default
KeyGeneratorTest
{
public
final
class
Snowflake
KeyGeneratorTest
{
static
final
long
DEFAULT_SEQUENCE_BITS
=
12L
;
private
static
final
long
DEFAULT_SEQUENCE_BITS
=
12L
;
static
final
int
DEFAULT_KEY_AMOUNT
=
10
;
private
static
final
int
DEFAULT_KEY_AMOUNT
=
10
;
@Test
@SneakyThrows
...
...
@@ -47,7 +48,8 @@ public final class DefaultKeyGeneratorTest {
int
threadNumber
=
Runtime
.
getRuntime
().
availableProcessors
()
<<
1
;
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
threadNumber
);
int
taskNumber
=
threadNumber
<<
2
;
final
DefaultKeyGenerator
keyGenerator
=
new
DefaultKeyGenerator
();
final
SnowflakeKeyGenerator
keyGenerator
=
new
SnowflakeKeyGenerator
();
keyGenerator
.
setProperties
(
new
Properties
());
Set
<
Comparable
<?>>
actual
=
new
HashSet
<>();
for
(
int
i
=
0
;
i
<
taskNumber
;
i
++)
{
actual
.
add
(
executor
.
submit
(
new
Callable
<
Comparable
<?>>()
{
...
...
@@ -63,9 +65,10 @@ public final class DefaultKeyGeneratorTest {
@Test
public
void
assertGenerateKeyWithSingleThread
()
{
SnowflakeKeyGenerator
keyGenerator
=
new
SnowflakeKeyGenerator
();
keyGenerator
.
setProperties
(
new
Properties
());
SnowflakeKeyGenerator
.
setTimeService
(
new
FixedTimeService
(
1
));
List
<
Comparable
<?>>
expected
=
Arrays
.<
Comparable
<?>>
asList
(
1L
,
4194304L
,
4194305L
,
8388609L
,
8388610L
,
12582912L
,
12582913L
,
16777217L
,
16777218L
,
20971520L
);
DefaultKeyGenerator
keyGenerator
=
new
DefaultKeyGenerator
();
DefaultKeyGenerator
.
setTimeService
(
new
FixedTimeService
(
1
));
List
<
Comparable
<?>>
actual
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
DEFAULT_KEY_AMOUNT
;
i
++)
{
actual
.
add
(
keyGenerator
.
generateKey
());
...
...
@@ -76,9 +79,10 @@ public final class DefaultKeyGeneratorTest {
@Test
@SneakyThrows
public
void
assertGenerateKeyWithClockCallBack
()
{
DefaultKeyGenerator
keyGenerator
=
new
Default
KeyGenerator
();
SnowflakeKeyGenerator
keyGenerator
=
new
Snowflake
KeyGenerator
();
TimeService
timeService
=
new
FixedTimeService
(
1
);
DefaultKeyGenerator
.
setTimeService
(
timeService
);
SnowflakeKeyGenerator
.
setTimeService
(
timeService
);
keyGenerator
.
setProperties
(
new
Properties
());
setLastMilliseconds
(
keyGenerator
,
timeService
.
getCurrentMillis
()
+
2
);
List
<
Comparable
<?>>
expected
=
Arrays
.<
Comparable
<?>>
asList
(
4194305L
,
8388608L
,
8388609L
,
12582913L
,
12582914L
,
16777216L
,
16777217L
,
20971521L
,
20971522L
,
25165824L
);
List
<
Comparable
<?>>
actual
=
new
ArrayList
<>();
...
...
@@ -91,10 +95,13 @@ public final class DefaultKeyGeneratorTest {
@Test
(
expected
=
IllegalStateException
.
class
)
@SneakyThrows
public
void
assertGenerateKeyWithClockCallBackBeyondTolerateTime
()
{
DefaultKeyGenerator
keyGenerator
=
new
Default
KeyGenerator
();
SnowflakeKeyGenerator
keyGenerator
=
new
Snowflake
KeyGenerator
();
TimeService
timeService
=
new
FixedTimeService
(
1
);
DefaultKeyGenerator
.
setTimeService
(
timeService
);
DefaultKeyGenerator
.
setMaxTolerateTimeDifferenceMilliseconds
(
0
);
SnowflakeKeyGenerator
.
setTimeService
(
timeService
);
keyGenerator
.
setProperties
(
new
Properties
());
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"max.tolerate.time.difference.milliseconds"
,
String
.
valueOf
(
0
));
keyGenerator
.
setProperties
(
properties
);
setLastMilliseconds
(
keyGenerator
,
timeService
.
getCurrentMillis
()
+
2
);
List
<
Comparable
<?>>
actual
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
DEFAULT_KEY_AMOUNT
;
i
++)
{
...
...
@@ -105,9 +112,10 @@ public final class DefaultKeyGeneratorTest {
@Test
public
void
assertGenerateKeyBeyondMaxSequencePerMilliSecond
()
{
final
DefaultKeyGenerator
keyGenerator
=
new
Default
KeyGenerator
();
final
SnowflakeKeyGenerator
keyGenerator
=
new
Snowflake
KeyGenerator
();
TimeService
timeService
=
new
FixedTimeService
(
2
);
DefaultKeyGenerator
.
setTimeService
(
timeService
);
SnowflakeKeyGenerator
.
setTimeService
(
timeService
);
keyGenerator
.
setProperties
(
new
Properties
());
setLastMilliseconds
(
keyGenerator
,
timeService
.
getCurrentMillis
());
setSequence
(
keyGenerator
,
(
1
<<
DEFAULT_SEQUENCE_BITS
)
-
1
);
List
<
Comparable
<?>>
expected
=
Arrays
.<
Comparable
<?>>
asList
(
4194304L
,
4194305L
,
4194306L
,
8388609L
,
8388610L
,
8388611L
,
12582912L
,
12582913L
,
12582914L
,
16777217L
);
...
...
@@ -119,46 +127,58 @@ public final class DefaultKeyGeneratorTest {
}
@SneakyThrows
private
void
setSequence
(
final
Default
KeyGenerator
keyGenerator
,
final
Number
value
)
{
Field
sequence
=
Default
KeyGenerator
.
class
.
getDeclaredField
(
"sequence"
);
private
void
setSequence
(
final
Snowflake
KeyGenerator
keyGenerator
,
final
Number
value
)
{
Field
sequence
=
Snowflake
KeyGenerator
.
class
.
getDeclaredField
(
"sequence"
);
sequence
.
setAccessible
(
true
);
sequence
.
set
(
keyGenerator
,
value
);
}
@SneakyThrows
private
void
setLastMilliseconds
(
final
Default
KeyGenerator
keyGenerator
,
final
Number
value
)
{
Field
lastMilliseconds
=
Default
KeyGenerator
.
class
.
getDeclaredField
(
"lastMilliseconds"
);
private
void
setLastMilliseconds
(
final
Snowflake
KeyGenerator
keyGenerator
,
final
Number
value
)
{
Field
lastMilliseconds
=
Snowflake
KeyGenerator
.
class
.
getDeclaredField
(
"lastMilliseconds"
);
lastMilliseconds
.
setAccessible
(
true
);
lastMilliseconds
.
set
(
keyGenerator
,
value
);
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
assertSetWorkerIdFailureWhenNegative
()
{
DefaultKeyGenerator
.
setWorkerId
(-
1L
);
SnowflakeKeyGenerator
keyGenerator
=
new
SnowflakeKeyGenerator
();
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"worker.id"
,
String
.
valueOf
(-
1L
));
keyGenerator
.
setProperties
(
properties
);
keyGenerator
.
generateKey
();
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
assertSetWorkerIdFailureWhenTooMuch
()
{
DefaultKeyGenerator
.
setWorkerId
(-
Long
.
MAX_VALUE
);
SnowflakeKeyGenerator
keyGenerator
=
new
SnowflakeKeyGenerator
();
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"worker.id"
,
String
.
valueOf
(-
Long
.
MAX_VALUE
));
keyGenerator
.
setProperties
(
properties
);
keyGenerator
.
generateKey
();
}
@Test
@SneakyThrows
public
void
assertSetWorkerIdSuccess
()
{
DefaultKeyGenerator
.
setWorkerId
(
1L
);
Field
workerIdField
=
DefaultKeyGenerator
.
class
.
getDeclaredField
(
"workerId"
);
workerIdField
.
setAccessible
(
true
);
assertThat
(
workerIdField
.
getLong
(
DefaultKeyGenerator
.
class
),
is
(
1L
));
DefaultKeyGenerator
.
setWorkerId
(
0L
);
SnowflakeKeyGenerator
keyGenerator
=
new
SnowflakeKeyGenerator
();
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"worker.id"
,
String
.
valueOf
(
1L
));
keyGenerator
.
setProperties
(
properties
);
Field
props
=
keyGenerator
.
getClass
().
getDeclaredField
(
"properties"
);
props
.
setAccessible
(
true
);
assertThat
(((
Properties
)
props
.
get
(
keyGenerator
)).
get
(
"worker.id"
),
is
((
Object
)
"1"
));
}
@Test
@SneakyThrows
public
void
assertSetMaxTolerateTimeDifferenceMilliseconds
()
{
DefaultKeyGenerator
.
setMaxTolerateTimeDifferenceMilliseconds
(
1
);
Field
maxTolerateTimeDifferenceMillisecondsField
=
DefaultKeyGenerator
.
class
.
getDeclaredField
(
"maxTolerateTimeDifferenceMilliseconds"
);
maxTolerateTimeDifferenceMillisecondsField
.
setAccessible
(
true
);
assertThat
(
maxTolerateTimeDifferenceMillisecondsField
.
getInt
(
DefaultKeyGenerator
.
class
),
is
(
1
));
DefaultKeyGenerator
.
setMaxTolerateTimeDifferenceMilliseconds
(
10
);
SnowflakeKeyGenerator
keyGenerator
=
new
SnowflakeKeyGenerator
();
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"max.tolerate.time.difference.milliseconds"
,
String
.
valueOf
(
1
));
keyGenerator
.
setProperties
(
properties
);
Field
props
=
keyGenerator
.
getClass
().
getDeclaredField
(
"properties"
);
props
.
setAccessible
(
true
);
assertThat
(((
Properties
)
props
.
get
(
keyGenerator
)).
get
(
"max.tolerate.time.difference.milliseconds"
),
is
((
Object
)
"1"
));
}
}
sharding-core/src/test/java/io/shardingsphere/core/keygen/fixture/FixedTimeService.java
浏览文件 @
81124ed9
...
...
@@ -17,7 +17,7 @@
package
io.shardingsphere.core.keygen.fixture
;
import
io.shardingsphere.core.keygen.
Default
KeyGenerator
;
import
io.shardingsphere.core.keygen.
Snowflake
KeyGenerator
;
import
io.shardingsphere.core.keygen.TimeService
;
import
lombok.RequiredArgsConstructor
;
...
...
@@ -30,7 +30,7 @@ public final class FixedTimeService extends TimeService {
private
final
AtomicInteger
invokedTimes
=
new
AtomicInteger
();
private
long
current
=
Default
KeyGenerator
.
EPOCH
;
private
long
current
=
Snowflake
KeyGenerator
.
EPOCH
;
@Override
public
long
getCurrentMillis
()
{
...
...
sharding-core/src/test/java/io/shardingsphere/core/keygen/fixture/IncrementKeyGenerator.java
浏览文件 @
81124ed9
...
...
@@ -18,13 +18,20 @@
package
io.shardingsphere.core.keygen.fixture
;
import
io.shardingsphere.core.keygen.KeyGenerator
;
import
lombok.Getter
;
import
lombok.Setter
;
import
java.util.Properties
;
import
java.util.concurrent.atomic.AtomicInteger
;
public
final
class
IncrementKeyGenerator
implements
KeyGenerator
{
private
final
AtomicInteger
count
=
new
AtomicInteger
();
@Getter
@Setter
private
Properties
properties
=
new
Properties
();
@Override
public
Comparable
<?>
generateKey
()
{
return
count
.
incrementAndGet
();
...
...
sharding-core/src/test/java/io/shardingsphere/core/rule/ShardingRuleTest.java
浏览文件 @
81124ed9
...
...
@@ -26,7 +26,7 @@ import io.shardingsphere.api.config.strategy.InlineShardingStrategyConfiguration
import
io.shardingsphere.api.config.strategy.NoneShardingStrategyConfiguration
;
import
io.shardingsphere.api.config.strategy.StandardShardingStrategyConfiguration
;
import
io.shardingsphere.core.exception.ShardingConfigurationException
;
import
io.shardingsphere.core.keygen.
Default
KeyGenerator
;
import
io.shardingsphere.core.keygen.
Snowflake
KeyGenerator
;
import
io.shardingsphere.core.keygen.fixture.IncrementKeyGenerator
;
import
io.shardingsphere.core.parsing.parser.context.condition.Column
;
import
io.shardingsphere.core.routing.strategy.inline.InlineShardingStrategy
;
...
...
@@ -73,7 +73,7 @@ public final class ShardingRuleTest {
assertTrue
(
actual
.
getBroadcastTables
().
isEmpty
());
assertThat
(
actual
.
getDefaultDatabaseShardingStrategy
(),
instanceOf
(
NoneShardingStrategy
.
class
));
assertThat
(
actual
.
getDefaultTableShardingStrategy
(),
instanceOf
(
NoneShardingStrategy
.
class
));
assertThat
(
actual
.
getDefaultKeyGenerator
(),
instanceOf
(
Default
KeyGenerator
.
class
));
assertThat
(
actual
.
getDefaultKeyGenerator
(),
instanceOf
(
Snowflake
KeyGenerator
.
class
));
}
@Test
...
...
sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlKeyGeneratorConfigurationTest.java
0 → 100644
浏览文件 @
81124ed9
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/
package
io.shardingsphere.core.yaml.sharding
;
import
io.shardingsphere.core.exception.ShardingConfigurationException
;
import
io.shardingsphere.core.keygen.SnowflakeKeyGenerator
;
import
org.junit.Test
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
public
class
YamlKeyGeneratorConfigurationTest
{
@Test
public
void
assertGetKeyGeneratorWithClassName
()
{
YamlKeyGeneratorConfiguration
keyGeneratorConfiguration
=
new
YamlKeyGeneratorConfiguration
();
keyGeneratorConfiguration
.
setClassName
(
"io.shardingsphere.core.keygen.SnowflakeKeyGenerator"
);
assertThat
(
keyGeneratorConfiguration
.
getKeyGenerator
().
getClass
().
getName
(),
is
(
SnowflakeKeyGenerator
.
class
.
getName
()));
}
@Test
public
void
assertGetKeyGeneratorWithSnowflakeType
()
{
YamlKeyGeneratorConfiguration
keyGeneratorConfiguration
=
new
YamlKeyGeneratorConfiguration
();
keyGeneratorConfiguration
.
setType
(
"SNOWFLAKE"
);
assertThat
(
keyGeneratorConfiguration
.
getKeyGenerator
().
getClass
().
getName
(),
is
(
SnowflakeKeyGenerator
.
class
.
getName
()));
}
@Test
public
void
assertGetKeyGeneratorWithoutTypeAndClassName
()
{
YamlKeyGeneratorConfiguration
keyGeneratorConfiguration
=
new
YamlKeyGeneratorConfiguration
();
assertThat
(
keyGeneratorConfiguration
.
getKeyGenerator
().
getClass
().
getName
(),
is
(
SnowflakeKeyGenerator
.
class
.
getName
()));
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
assertGetKeyGeneratorClassNameWithLeaf
()
{
YamlKeyGeneratorConfiguration
keyGeneratorConfiguration
=
new
YamlKeyGeneratorConfiguration
();
keyGeneratorConfiguration
.
setType
(
"LEAF"
);
keyGeneratorConfiguration
.
getKeyGenerator
();
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
assertGetKeyGeneratorClassNameWithUUID
()
{
YamlKeyGeneratorConfiguration
keyGeneratorConfiguration
=
new
YamlKeyGeneratorConfiguration
();
keyGeneratorConfiguration
.
setType
(
"UUID"
);
keyGeneratorConfiguration
.
getKeyGenerator
();
}
@Test
(
expected
=
ShardingConfigurationException
.
class
)
public
void
assertGetKeyGeneratorClassNameWithException
()
{
YamlKeyGeneratorConfiguration
keyGeneratorConfiguration
=
new
YamlKeyGeneratorConfiguration
();
keyGeneratorConfiguration
.
setType
(
"DEFAULT"
);
keyGeneratorConfiguration
.
getKeyGenerator
();
}
}
sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlShardingConfigurationTest.java
浏览文件 @
81124ed9
...
...
@@ -18,7 +18,7 @@
package
io.shardingsphere.core.yaml.sharding
;
import
io.shardingsphere.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithmType
;
import
io.shardingsphere.core.keygen.
Default
KeyGenerator
;
import
io.shardingsphere.core.keygen.
Snowflake
KeyGenerator
;
import
org.hamcrest.CoreMatchers
;
import
org.junit.Test
;
...
...
@@ -104,8 +104,8 @@ public final class YamlShardingConfigurationTest {
assertThat
(
actual
.
getShardingRule
().
getTables
().
get
(
"t_order"
).
getActualDataNodes
(),
is
(
"ds_${0..1}.t_order_${0..1}"
));
assertThat
(
actual
.
getShardingRule
().
getTables
().
get
(
"t_order"
).
getTableStrategy
().
getInline
().
getShardingColumn
(),
is
(
"order_id"
));
assertThat
(
actual
.
getShardingRule
().
getTables
().
get
(
"t_order"
).
getTableStrategy
().
getInline
().
getAlgorithmExpression
(),
is
(
"t_order_${order_id % 2}"
));
assertThat
(
actual
.
getShardingRule
().
getTables
().
get
(
"t_order"
).
getKeyGenerator
ColumnName
(),
is
(
"order_id"
));
assertThat
(
actual
.
getShardingRule
().
getTables
().
get
(
"t_order"
).
getKeyGenerator
ClassName
(),
is
(
Default
KeyGenerator
.
class
.
getName
()));
assertThat
(
actual
.
getShardingRule
().
getTables
().
get
(
"t_order"
).
getKeyGenerator
().
getColumn
(),
is
(
"order_id"
));
assertThat
(
actual
.
getShardingRule
().
getTables
().
get
(
"t_order"
).
getKeyGenerator
().
getClassName
(),
is
(
Snowflake
KeyGenerator
.
class
.
getName
()));
assertThat
(
actual
.
getShardingRule
().
getTables
().
get
(
"t_order"
).
getLogicIndex
(),
is
(
"order_index"
));
}
...
...
@@ -130,7 +130,7 @@ public final class YamlShardingConfigurationTest {
assertThat
(
actual
.
getShardingRule
().
getDefaultDataSourceName
(),
is
(
"default_ds"
));
assertThat
(
actual
.
getShardingRule
().
getDefaultDatabaseStrategy
().
getInline
().
getShardingColumn
(),
is
(
"order_id"
));
assertThat
(
actual
.
getShardingRule
().
getDefaultDatabaseStrategy
().
getInline
().
getAlgorithmExpression
(),
is
(
"ds_${order_id % 2}"
));
assertThat
(
actual
.
getShardingRule
().
getDefaultKeyGenerator
ClassName
(),
is
(
Default
KeyGenerator
.
class
.
getName
()));
assertThat
(
actual
.
getShardingRule
().
getDefaultKeyGenerator
().
getKeyGenerator
().
getClass
().
getName
(),
is
(
Snowflake
KeyGenerator
.
class
.
getName
()));
}
private
void
assertMasterSlaveRules
(
final
YamlShardingConfiguration
actual
)
{
...
...
sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlShardingRuleConfigurationTest.java
浏览文件 @
81124ed9
...
...
@@ -23,7 +23,7 @@ import io.shardingsphere.api.config.rule.MasterSlaveRuleConfiguration;
import
io.shardingsphere.api.config.rule.ShardingRuleConfiguration
;
import
io.shardingsphere.api.config.rule.TableRuleConfiguration
;
import
io.shardingsphere.api.config.strategy.NoneShardingStrategyConfiguration
;
import
io.shardingsphere.core.keygen.
Default
KeyGenerator
;
import
io.shardingsphere.core.keygen.
Snowflake
KeyGenerator
;
import
io.shardingsphere.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration
;
import
io.shardingsphere.core.yaml.sharding.strategy.YamlNoneShardingStrategyConfiguration
;
import
org.hamcrest.CoreMatchers
;
...
...
@@ -61,7 +61,9 @@ public final class YamlShardingRuleConfigurationTest {
result
.
getTables
().
put
(
"t_order_item"
,
new
YamlTableRuleConfiguration
());
result
.
getBindingTables
().
add
(
"t_order, t_order_item"
);
result
.
getBroadcastTables
().
add
(
"t_config"
);
result
.
setDefaultKeyGeneratorClassName
(
DefaultKeyGenerator
.
class
.
getName
());
YamlKeyGeneratorConfiguration
keyGeneratorConfiguration
=
new
YamlKeyGeneratorConfiguration
();
keyGeneratorConfiguration
.
setClassName
(
SnowflakeKeyGenerator
.
class
.
getName
());
result
.
setDefaultKeyGenerator
(
keyGeneratorConfiguration
);
result
.
getMasterSlaveRules
().
put
(
"master_slave_ds"
,
createYamlMasterSlaveRuleConfig
());
return
result
;
}
...
...
@@ -95,7 +97,7 @@ public final class YamlShardingRuleConfigurationTest {
assertThat
(
actual
.
getBindingTableGroups
().
iterator
().
next
(),
is
(
"t_order, t_order_item"
));
assertThat
(
actual
.
getBroadcastTables
().
size
(),
is
(
1
));
assertThat
(
actual
.
getBroadcastTables
().
iterator
().
next
(),
is
(
"t_config"
));
assertThat
(
actual
.
getDefaultKeyGenerator
(),
instanceOf
(
Default
KeyGenerator
.
class
));
assertThat
(
actual
.
getDefaultKeyGenerator
(),
instanceOf
(
Snowflake
KeyGenerator
.
class
));
assertMasterSlaveRuleConfig
(
actual
.
getMasterSlaveRuleConfigs
().
iterator
().
next
());
}
...
...
sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlTableRuleConfigurationTest.java
浏览文件 @
81124ed9
...
...
@@ -19,7 +19,7 @@ package io.shardingsphere.core.yaml.sharding;
import
io.shardingsphere.api.config.rule.TableRuleConfiguration
;
import
io.shardingsphere.api.config.strategy.NoneShardingStrategyConfiguration
;
import
io.shardingsphere.core.keygen.
Default
KeyGenerator
;
import
io.shardingsphere.core.keygen.
Snowflake
KeyGenerator
;
import
io.shardingsphere.core.yaml.sharding.strategy.YamlNoneShardingStrategyConfiguration
;
import
org.junit.Test
;
...
...
@@ -53,8 +53,9 @@ public final class YamlTableRuleConfigurationTest {
YamlTableRuleConfiguration
result
=
new
YamlTableRuleConfiguration
();
result
.
setLogicTable
(
"t_order"
);
result
.
setActualDataNodes
(
"ds_${0..1}.t_order_${0..1}"
);
result
.
setKeyGeneratorColumnName
(
"order_id"
);
result
.
setKeyGeneratorClassName
(
DefaultKeyGenerator
.
class
.
getName
());
result
.
setKeyGenerator
(
new
YamlKeyGeneratorConfiguration
());
result
.
getKeyGenerator
().
setColumn
(
"order_id"
);
result
.
getKeyGenerator
().
setClassName
(
SnowflakeKeyGenerator
.
class
.
getName
());
result
.
setLogicIndex
(
"order_index"
);
return
result
;
}
...
...
@@ -72,7 +73,7 @@ public final class YamlTableRuleConfigurationTest {
assertThat
(
actual
.
getLogicTable
(),
is
(
"t_order"
));
assertThat
(
actual
.
getActualDataNodes
(),
is
(
"ds_${0..1}.t_order_${0..1}"
));
assertThat
(
actual
.
getKeyGeneratorColumnName
(),
is
(
"order_id"
));
assertThat
(
actual
.
getKeyGenerator
(),
instanceOf
(
Default
KeyGenerator
.
class
));
assertThat
(
actual
.
getKeyGenerator
(),
instanceOf
(
Snowflake
KeyGenerator
.
class
));
assertThat
(
actual
.
getLogicIndex
(),
is
(
"order_index"
));
}
...
...
sharding-core/src/test/resources/yaml/optimize-rule.yaml
浏览文件 @
81124ed9
...
...
@@ -22,6 +22,7 @@ shardingRule:
inline
:
shardingColumn
:
order_id
algorithmExpression
:
t_order_${order_id % 2}
keyGeneratorColumnName
:
order_id
keyGeneratorClassName
:
io.shardingsphere.core.keygen.DefaultKeyGenerator
keyGenerator
:
column
:
order_id
className
:
io.shardingsphere.core.keygen.SnowflakeKeyGenerator
logicIndex
:
order_index
sharding-core/src/test/resources/yaml/parser-rule.yaml
浏览文件 @
81124ed9
...
...
@@ -25,7 +25,9 @@ shardingRule:
complex
:
shardingColumns
:
user_id, order_id, item_id
algorithmClassName
:
io.shardingsphere.api.algorithm.fixture.TestComplexKeysShardingAlgorithm
keyGeneratorColumnName
:
item_id
keyGenerator
:
column
:
item_id
className
:
io.shardingsphere.core.keygen.SnowflakeKeyGenerator
t_place
:
actualDataNodes
:
db${0..1}.t_place
tableStrategy
:
...
...
@@ -34,4 +36,5 @@ shardingRule:
algorithmClassName
:
io.shardingsphere.api.algorithm.fixture.TestComplexKeysShardingAlgorithm
bindingTables
:
-
t_order, t_order_item
defaultKeyGeneratorClassName
:
io.shardingsphere.core.keygen.fixture.IncrementKeyGenerator
defaultKeyGenerator
:
type
:
SNOWFLAKE
sharding-core/src/test/resources/yaml/rewrite-rule.yaml
浏览文件 @
81124ed9
...
...
@@ -14,7 +14,9 @@ shardingRule:
tables
:
table_x
:
actualDataNodes
:
db${0..1}.table_x
keyGeneratorColumnName
:
id
keyGenerator
:
column
:
id
type
:
SNOWFLAKE
logicIndex
:
logic_index
table_y
:
actualDataNodes
:
db${0..1}.table_y
...
...
sharding-core/src/test/resources/yaml/sharding-rule.yaml
浏览文件 @
81124ed9
...
...
@@ -61,8 +61,9 @@ shardingRule:
inline
:
shardingColumn
:
order_id
algorithmExpression
:
t_order_${order_id % 2}
keyGeneratorColumnName
:
order_id
keyGeneratorClassName
:
io.shardingsphere.core.keygen.DefaultKeyGenerator
keyGenerator
:
column
:
order_id
className
:
io.shardingsphere.core.keygen.SnowflakeKeyGenerator
logicIndex
:
order_index
t_order_item
:
actualDataNodes
:
ds_${0..1}.t_order_item_${0..1}
...
...
@@ -82,7 +83,8 @@ shardingRule:
algorithmExpression
:
ds_${order_id % 2}
defaultTableStrategy
:
none
:
defaultKeyGeneratorClassName
:
io.shardingsphere.core.keygen.DefaultKeyGenerator
defaultKeyGenerator
:
type
:
SNOWFLAKE
masterSlaveRules
:
ds_0
:
...
...
sharding-jdbc/sharding-jdbc-core/src/test/java/io/shardingsphere/dbtest/fixture/ConstantKeyGenerator.java
浏览文件 @
81124ed9
...
...
@@ -18,9 +18,17 @@
package
io.shardingsphere.dbtest.fixture
;
import
io.shardingsphere.core.keygen.KeyGenerator
;
import
lombok.Getter
;
import
lombok.Setter
;
import
java.util.Properties
;
public
final
class
ConstantKeyGenerator
implements
KeyGenerator
{
@Getter
@Setter
private
Properties
properties
=
new
Properties
();
@Override
public
Comparable
<?>
generateKey
()
{
return
1
;
...
...
sharding-jdbc/sharding-jdbc-core/src/test/java/io/shardingsphere/shardingjdbc/fixture/IncrementKeyGenerator.java
浏览文件 @
81124ed9
...
...
@@ -18,13 +18,20 @@
package
io.shardingsphere.shardingjdbc.fixture
;
import
io.shardingsphere.core.keygen.KeyGenerator
;
import
lombok.Getter
;
import
lombok.Setter
;
import
java.util.Properties
;
import
java.util.concurrent.atomic.AtomicInteger
;
public
final
class
IncrementKeyGenerator
implements
KeyGenerator
{
private
final
AtomicInteger
count
=
new
AtomicInteger
();
@Getter
@Setter
private
Properties
properties
=
new
Properties
();
@Override
public
Comparable
<?>
generateKey
()
{
return
count
.
incrementAndGet
();
...
...
sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/db/sharding-rule.yaml
浏览文件 @
81124ed9
...
...
@@ -15,8 +15,9 @@ shardingRule:
shardingColumn
:
user_id
preciseAlgorithmClassName
:
io.shardingsphere.dbtest.fixture.PreciseModuloAlgorithm
rangeAlgorithmClassName
:
io.shardingsphere.dbtest.fixture.RangeModuloAlgorithm
keyGeneratorColumnName
:
item_id
keyGeneratorClassName
:
io.shardingsphere.dbtest.fixture.ConstantKeyGenerator
keyGenerator
:
column
:
item_id
className
:
io.shardingsphere.dbtest.fixture.ConstantKeyGenerator
bindingTables
:
-
t_order,t_order_item
defaultDataSourceName
:
db_0
sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/dbtbl_with_masterslave/sharding-rule.yaml
浏览文件 @
81124ed9
...
...
@@ -27,8 +27,9 @@ shardingRule:
shardingColumn
:
order_id
preciseAlgorithmClassName
:
io.shardingsphere.dbtest.fixture.PreciseModuloAlgorithm
rangeAlgorithmClassName
:
io.shardingsphere.dbtest.fixture.RangeModuloAlgorithm
keyGeneratorColumnName
:
item_id
keyGeneratorClassName
:
io.shardingsphere.dbtest.fixture.ConstantKeyGenerator
keyGenerator
:
column
:
item_id
className
:
io.shardingsphere.dbtest.fixture.ConstantKeyGenerator
bindingTables
:
-
t_order,t_order_item
defaultDataSourceName
:
db_ms_0
...
...
sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/tbl/sharding-rule.yaml
浏览文件 @
81124ed9
...
...
@@ -15,8 +15,9 @@ shardingRule:
shardingColumn
:
order_id
preciseAlgorithmClassName
:
io.shardingsphere.dbtest.fixture.PreciseModuloAlgorithm
rangeAlgorithmClassName
:
io.shardingsphere.dbtest.fixture.RangeModuloAlgorithm
keyGeneratorColumnName
:
item_id
keyGeneratorClassName
:
io.shardingsphere.dbtest.fixture.ConstantKeyGenerator
keyGenerator
:
column
:
item_id
className
:
io.shardingsphere.dbtest.fixture.ConstantKeyGenerator
bindingTables
:
-
t_order,t_order_item
defaultDataSourceName
:
tbl
sharding-jdbc/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/shardingjdbc/orchestration/api/yaml/fixture/DecrementKeyGenerator.java
浏览文件 @
81124ed9
...
...
@@ -18,13 +18,20 @@
package
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture
;
import
io.shardingsphere.core.keygen.KeyGenerator
;
import
lombok.Getter
;
import
lombok.Setter
;
import
java.util.Properties
;
import
java.util.concurrent.atomic.AtomicInteger
;
public
final
class
DecrementKeyGenerator
implements
KeyGenerator
{
private
final
AtomicInteger
sequence
=
new
AtomicInteger
(
100
);
@Getter
@Setter
private
Properties
properties
=
new
Properties
();
@Override
public
Comparable
<?>
generateKey
()
{
return
sequence
.
decrementAndGet
();
...
...
sharding-jdbc/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/shardingjdbc/orchestration/api/yaml/fixture/IncrementKeyGenerator.java
浏览文件 @
81124ed9
...
...
@@ -18,13 +18,20 @@
package
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture
;
import
io.shardingsphere.core.keygen.KeyGenerator
;
import
lombok.Getter
;
import
lombok.Setter
;
import
java.util.Properties
;
import
java.util.concurrent.atomic.AtomicInteger
;
public
final
class
IncrementKeyGenerator
implements
KeyGenerator
{
private
static
final
AtomicInteger
SEQUENCE
=
new
AtomicInteger
(
100
);
@Getter
@Setter
private
Properties
properties
=
new
Properties
();
@Override
public
Comparable
<?>
generateKey
()
{
return
SEQUENCE
.
incrementAndGet
();
...
...
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithDataSourceWithProps.yaml
浏览文件 @
81124ed9
...
...
@@ -27,8 +27,9 @@ shardingRule:
inline
:
shardingColumn
:
order_id
algorithmExpression
:
t_order_${order_id % 2}
keyGeneratorColumnName
:
order_id
keyGeneratorClassName
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
keyGenerator
:
column
:
order_id
className
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
t_order_item
:
actualDataNodes
:
db${0..1}.t_order_item_${0..1}
...
...
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithDataSourceWithoutProps.yaml
浏览文件 @
81124ed9
...
...
@@ -27,8 +27,9 @@ shardingRule:
inline
:
shardingColumn
:
order_id
algorithmExpression
:
t_order_${order_id % 2}
keyGeneratorColumnName
:
order_id
keyGeneratorClassName
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
keyGenerator
:
column
:
order_id
className
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
t_order_item
:
actualDataNodes
:
db${0..1}.t_order_item_${0..1}
...
...
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithoutDataSourceWithProps.yaml
浏览文件 @
81124ed9
...
...
@@ -13,8 +13,9 @@ shardingRule:
inline
:
shardingColumn
:
order_id
algorithmExpression
:
t_order_${order_id % 2}
keyGeneratorColumnName
:
order_id
keyGeneratorClassName
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
keyGenerator
:
column
:
order_id
className
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
t_order_item
:
actualDataNodes
:
db${0..1}.t_order_item_${0..1}
...
...
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithoutDataSourceWithoutProps.yaml
浏览文件 @
81124ed9
...
...
@@ -12,8 +12,9 @@ shardingRule:
inline
:
shardingColumn
:
order_id
algorithmExpression
:
t_order_${order_id % 2}
keyGeneratorColumnName
:
order_id
keyGeneratorClassName
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
keyGenerator
:
column
:
order_id
className
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
t_order_item
:
actualDataNodes
:
db${0..1}.t_order_item_${0..1}
...
...
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithDataSourceWithProps.yaml
浏览文件 @
81124ed9
...
...
@@ -39,8 +39,9 @@ shardingRule:
inline
:
shardingColumn
:
order_id
algorithmExpression
:
t_order_${order_id % 2}
keyGeneratorColumnName
:
order_id
keyGeneratorClassName
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
keyGenerator
:
column
:
order_id
className
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
t_order_item
:
actualDataNodes
:
db_ms_${0..1}.t_order_item_${0..1}
...
...
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithDataSourceWithoutProps.yaml
浏览文件 @
81124ed9
...
...
@@ -39,8 +39,9 @@ shardingRule:
inline
:
shardingColumn
:
order_id
algorithmExpression
:
t_order_${order_id % 2}
keyGeneratorColumnName
:
order_id
keyGeneratorClassName
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
keyGenerator
:
column
:
order_id
className
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
t_order_item
:
actualDataNodes
:
db_ms_${0..1}.t_order_item_${0..1}
...
...
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithoutDataSourceWithProps.yaml
浏览文件 @
81124ed9
...
...
@@ -13,8 +13,9 @@ shardingRule:
inline
:
shardingColumn
:
order_id
algorithmExpression
:
t_order_${order_id % 2}
keyGeneratorColumnName
:
order_id
keyGeneratorClassName
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
keyGenerator
:
column
:
order_id
className
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
t_order_item
:
actualDataNodes
:
db_ms_${0..1}.t_order_item_${0..1}
...
...
sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithoutDataSourceWithoutProps.yaml
浏览文件 @
81124ed9
...
...
@@ -13,8 +13,9 @@ shardingRule:
inline
:
shardingColumn
:
order_id
algorithmExpression
:
t_order_${order_id % 2}
keyGeneratorColumnName
:
order_id
keyGeneratorClassName
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
keyGenerator
:
column
:
order_id
className
:
io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator
t_order_item
:
actualDataNodes
:
db_ms_${0..1}.t_order_item_${0..1}
...
...
sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/internal/registry/config/service/ConfigurationServiceTest.java
浏览文件 @
81124ed9
...
...
@@ -70,18 +70,12 @@ public final class ConfigurationServiceTest {
+
"ds_1: !!io.shardingsphere.core.rule.DataSourceParameter\n"
+
" url: jdbc:mysql://localhost:3306/ds_1\n"
+
" username: root\n"
+
" password: root\n"
;
private
static
final
String
SHARDING_RULE_YAML
=
"tables:\n"
+
" t_order:\n"
+
"
actualDataNodes: ds_${0..1}.t_order_${0..1}\n"
+
" logicTable: t_order\n"
+
" "
+
"
tableStrategy:\n"
+
" inline:\n"
+
" algorithmExpression: t_order_${order_id % 2}\n"
+
"
shardingColumn: order_id\n"
;
private
static
final
String
SHARDING_RULE_YAML
=
"tables:\n"
+
" t_order:\n"
+
" actualDataNodes: ds_${0..1}.t_order_${0..1}\n"
+
" keyGenerator:\n"
+
"
type: SNOWFLAKE\n"
+
" logicTable: t_order\n"
+
" tableStrategy:\n"
+
" inline:\n"
+
" algorithmExpression: t_order_${order_id % 2}\n"
+
" shardingColumn: order_id\n"
;
private
static
final
String
MASTER_SLAVE_RULE_YAML
=
"masterDataSourceName: master_ds\n"
+
"name: ms_ds\n"
+
"slaveDataSourceNames:\n"
+
"- slave_ds_0\n"
+
"- slave_ds_1\n"
;
private
static
final
String
SHARDING_MASTER_SLAVE_RULE_YAML
=
"tables:\n"
+
" t_order: \n"
+
" actualDataNodes: db_ms_${0..1}.t_order_${0..1}\n"
+
" databaseStrategy: \n"
+
" inline:\n"
+
" shardingColumn: user_id\n"
+
" algorithmExpression: db_ms_${user_id % 2}\n"
+
" tableStrategy: \n"
+
" inline:\n"
+
" shardingColumn: order_id\n"
+
" algorithmExpression: t_order_${order_id % 2}\n"
+
" keyGeneratorColumnName: order_id\n"
+
"bindingTables:\n"
+
" - t_order,t_order_item\n"
+
"masterSlaveRules:\n"
+
" db_ms_0:\n"
+
" masterDataSourceName: db0_master\n"
+
" slaveDataSourceNames:\n"
+
" - db0_slave\n"
+
" db_ms_1:\n"
+
" masterDataSourceName: db1_master\n"
+
" slaveDataSourceNames:\n"
+
" - db1_slave"
;
private
static
final
String
AUTHENTICATION_YAML
=
"password: root\n"
+
"username: root\n"
;
private
static
final
String
CONFIG_MAP_YAML
=
"{}\n"
;
...
...
sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/yaml/AllYamlTests.java
浏览文件 @
81124ed9
...
...
@@ -23,6 +23,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith
(
Suite
.
class
)
@SuiteClasses
({
ConfigurationYamlConverterTest
.
class
,
DefaultYamlRepresenterTest
.
class
,
YamlOrchestrationConfigurationTest
.
class
,
YamlDataSourceConfigurationTest
.
class
...
...
sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/yaml/ConfigurationYamlConverterTest.java
浏览文件 @
81124ed9
...
...
@@ -43,12 +43,12 @@ public class ConfigurationYamlConverterTest {
+
" dataSourceClassName: com.zaxxer.hikari.HikariDataSource\n"
+
" properties:\n"
+
" url: jdbc:mysql://localhost:3306/demo_ds_master\n"
+
" username: root\n"
+
" password: null\n"
;
private
static
final
String
SHARDING_RULE_YAML
=
" tables:\n"
+
" t_order:\n"
+
" actualDataNodes: ds_${0..1}.t_order_${0..1}\n"
+
" tableStrategy:\n"
+
" inline:\n"
+
" shardingColumn: order_id\n"
+
" algorithmExpression: t_order_${order_id % 2}\n"
+
"
keyGeneratorColumnName: order_id\n"
+
" t_order_item:\n"
+
" actualDataNodes: ds_${0..1}.t_order_item_${0..1}\n"
+
" tableStrategy:\n"
+
" inline:\n"
+
"
shardingColumn: order_id\n"
+
" algorithmExpression: t_order_item_${order_id % 2}\n"
+
" keyGeneratorColumnName: order_item_id\n"
+
" bindingTables:\n"
+
"
- t_order,t_order_item\n"
+
" defaultDataSourceName: ds_1\n"
+
" defaultDatabaseStrategy:\n"
+
" inline:\n"
+
" shardingColumn: user_id\n"
+
" algorithmExpression: ds_${user_id % 2}"
;
private
static
final
String
SHARDING_RULE_YAML
=
" tables:\n"
+
" t_order:\n"
+
" actualDataNodes: ds_${0..1}.t_order_${0..1}\n"
+
" tableStrategy:\n"
+
" inline:\n"
+
" shardingColumn: order_id\n"
+
" algorithmExpression: t_order_${order_id % 2}\n"
+
" keyGenerator:\n"
+
"
column: order_id\n"
+
" t_order_item:\n"
+
" actualDataNodes: ds_${0..1}.t_order_item_${0..1}\n"
+
" tableStrategy:\n"
+
"
inline:\n"
+
" shardingColumn: order_id\n"
+
" algorithmExpression: t_order_item_${order_id % 2}\n"
+
" keyGenerator:\n"
+
"
column: order_item_id\n"
+
" bindingTables:\n"
+
" - t_order,t_order_item\n"
+
" defaultDataSourceName: ds_1\n"
+
"
defaultDatabaseStrategy:\n"
+
" inline:\n"
+
" shardingColumn: user_id\n"
+
"
algorithmExpression: ds_${user_id % 2}"
;
private
static
final
String
MASTER_SLAVE_RULE_YAML
=
"masterDataSourceName: master_ds\n"
+
"name: ms_ds\n"
+
"slaveDataSourceNames:\n"
+
"- slave_ds_0\n"
+
"- slave_ds_1\n"
;
...
...
sharding-proxy/src/main/resources/conf/config-sharding.yaml
浏览文件 @
81124ed9
...
...
@@ -7,7 +7,7 @@
# if you want to use master-slave, please refer to the config-master_slave.yaml.
#
######################################################################################################
#
#schemaName: sharding_db
#
#dataSources:
...
...
@@ -36,14 +36,16 @@
# inline:
# shardingColumn: order_id
# algorithmExpression: t_order_${order_id % 2}
# keyGeneratorColumnName: order_id
# keyGenerator:
# column: order_id
# t_order_item:
# actualDataNodes: ds_${0..1}.t_order_item_${0..1}
# tableStrategy:
# inline:
# shardingColumn: order_id
# algorithmExpression: t_order_item_${order_id % 2}
# keyGeneratorColumnName: order_item_id
# keyGenerator:
# column: order_item_id
# bindingTables:
# - t_order,t_order_item
# defaultDatabaseStrategy:
...
...
@@ -52,4 +54,5 @@
# algorithmExpression: ds_${user_id % 2}
# defaultTableStrategy:
# none:
# defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator
# defaultKeyGenerator:
# className: io.shardingsphere.core.keygen.SnowflakeKeyGenerator
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录