-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
ShardingRule.java
475 lines (428 loc) · 18.7 KB
/
ShardingRule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shardingsphere.core.rule;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import lombok.Getter;
import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.ShardingStrategyConfiguration;
import org.apache.shardingsphere.core.config.ShardingConfigurationException;
import org.apache.shardingsphere.core.spi.algorithm.keygen.ShardingKeyGeneratorServiceLoader;
import org.apache.shardingsphere.core.strategy.route.ShardingStrategy;
import org.apache.shardingsphere.core.strategy.route.ShardingStrategyFactory;
import org.apache.shardingsphere.core.strategy.route.hint.HintShardingStrategy;
import org.apache.shardingsphere.core.strategy.route.none.NoneShardingStrategy;
import org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;
/**
* Databases and tables sharding rule.
*
* @author zhangliang
* @author maxiaoguang
* @author panjuan
*/
@Getter
public class ShardingRule implements BaseRule {
private final ShardingRuleConfiguration shardingRuleConfig;
private final ShardingDataSourceNames shardingDataSourceNames;
private final Collection<TableRule> tableRules;
private final Collection<BindingTableRule> bindingTableRules;
private final Collection<String> broadcastTables;
private final ShardingStrategy defaultDatabaseShardingStrategy;
private final ShardingStrategy defaultTableShardingStrategy;
private final ShardingKeyGenerator defaultShardingKeyGenerator;
private final Collection<MasterSlaveRule> masterSlaveRules;
private final EncryptRule encryptRule;
public ShardingRule(final ShardingRuleConfiguration shardingRuleConfig, final Collection<String> dataSourceNames) {
Preconditions.checkArgument(null != shardingRuleConfig, "ShardingRuleConfig cannot be null.");
Preconditions.checkArgument(null != dataSourceNames && !dataSourceNames.isEmpty(), "Data sources cannot be empty.");
this.shardingRuleConfig = shardingRuleConfig;
shardingDataSourceNames = new ShardingDataSourceNames(shardingRuleConfig, dataSourceNames);
tableRules = createTableRules(shardingRuleConfig);
bindingTableRules = createBindingTableRules(shardingRuleConfig.getBindingTableGroups());
broadcastTables = shardingRuleConfig.getBroadcastTables();
defaultDatabaseShardingStrategy = createDefaultShardingStrategy(shardingRuleConfig.getDefaultDatabaseShardingStrategyConfig());
defaultTableShardingStrategy = createDefaultShardingStrategy(shardingRuleConfig.getDefaultTableShardingStrategyConfig());
defaultShardingKeyGenerator = createDefaultKeyGenerator(shardingRuleConfig.getDefaultKeyGeneratorConfig());
masterSlaveRules = createMasterSlaveRules(shardingRuleConfig.getMasterSlaveRuleConfigs());
encryptRule = createEncryptRule(shardingRuleConfig.getEncryptRuleConfig());
}
private Collection<TableRule> createTableRules(final ShardingRuleConfiguration shardingRuleConfig) {
Collection<TableRuleConfiguration> tableRuleConfigurations = shardingRuleConfig.getTableRuleConfigs();
Collection<TableRule> result = new ArrayList<>(tableRuleConfigurations.size());
for (TableRuleConfiguration each : tableRuleConfigurations) {
result.add(new TableRule(each, shardingDataSourceNames, getDefaultGenerateKeyColumn(shardingRuleConfig)));
}
return result;
}
private String getDefaultGenerateKeyColumn(final ShardingRuleConfiguration shardingRuleConfig) {
return null == shardingRuleConfig.getDefaultKeyGeneratorConfig() ? null : shardingRuleConfig.getDefaultKeyGeneratorConfig().getColumn();
}
private Collection<BindingTableRule> createBindingTableRules(final Collection<String> bindingTableGroups) {
Collection<BindingTableRule> result = new ArrayList<>(bindingTableGroups.size());
for (String each : bindingTableGroups) {
result.add(createBindingTableRule(each));
}
return result;
}
private BindingTableRule createBindingTableRule(final String bindingTableGroup) {
List<TableRule> tableRules = new LinkedList<>();
for (String each : Splitter.on(",").trimResults().splitToList(bindingTableGroup)) {
tableRules.add(getTableRule(each));
}
return new BindingTableRule(tableRules);
}
private ShardingStrategy createDefaultShardingStrategy(final ShardingStrategyConfiguration shardingStrategyConfiguration) {
return null == shardingStrategyConfiguration ? new NoneShardingStrategy() : ShardingStrategyFactory.newInstance(shardingStrategyConfiguration);
}
private ShardingKeyGenerator createDefaultKeyGenerator(final KeyGeneratorConfiguration keyGeneratorConfiguration) {
ShardingKeyGeneratorServiceLoader serviceLoader = new ShardingKeyGeneratorServiceLoader();
return containsKeyGeneratorConfiguration(keyGeneratorConfiguration)
? serviceLoader.newService(keyGeneratorConfiguration.getType(), keyGeneratorConfiguration.getProperties()) : serviceLoader.newService();
}
private boolean containsKeyGeneratorConfiguration(final KeyGeneratorConfiguration keyGeneratorConfiguration) {
return null != keyGeneratorConfiguration && !Strings.isNullOrEmpty(keyGeneratorConfiguration.getType());
}
private Collection<MasterSlaveRule> createMasterSlaveRules(final Collection<MasterSlaveRuleConfiguration> masterSlaveRuleConfigurations) {
Collection<MasterSlaveRule> result = new ArrayList<>(masterSlaveRuleConfigurations.size());
for (MasterSlaveRuleConfiguration each : masterSlaveRuleConfigurations) {
result.add(new MasterSlaveRule(each));
}
return result;
}
private EncryptRule createEncryptRule(final EncryptRuleConfiguration encryptRuleConfig) {
return null == encryptRuleConfig ? new EncryptRule() : new EncryptRule(shardingRuleConfig.getEncryptRuleConfig());
}
/**
* Find table rule.
*
* @param logicTableName logic table name
* @return table rule
*/
public Optional<TableRule> findTableRule(final String logicTableName) {
for (TableRule each : tableRules) {
if (each.getLogicTable().equalsIgnoreCase(logicTableName)) {
return Optional.of(each);
}
}
return Optional.absent();
}
/**
* Find table rule via actual table name.
*
* @param actualTableName actual table name
* @return table rule
*/
public Optional<TableRule> findTableRuleByActualTable(final String actualTableName) {
for (TableRule each : tableRules) {
if (each.isExisted(actualTableName)) {
return Optional.of(each);
}
}
return Optional.absent();
}
/**
* Get table rule.
*
* @param logicTableName logic table name
* @return table rule
*/
public TableRule getTableRule(final String logicTableName) {
Optional<TableRule> tableRule = findTableRule(logicTableName);
if (tableRule.isPresent()) {
return tableRule.get();
}
if (isBroadcastTable(logicTableName)) {
return new TableRule(shardingDataSourceNames.getDataSourceNames(), logicTableName);
}
if (!Strings.isNullOrEmpty(shardingDataSourceNames.getDefaultDataSourceName())) {
return new TableRule(shardingDataSourceNames.getDefaultDataSourceName(), logicTableName);
}
throw new ShardingConfigurationException("Cannot find table rule and default data source with logic table: '%s'", logicTableName);
}
/**
* Get database sharding strategy.
*
* <p>
* Use default database sharding strategy if not found.
* </p>
*
* @param tableRule table rule
* @return database sharding strategy
*/
public ShardingStrategy getDatabaseShardingStrategy(final TableRule tableRule) {
return null == tableRule.getDatabaseShardingStrategy() ? defaultDatabaseShardingStrategy : tableRule.getDatabaseShardingStrategy();
}
/**
* Get table sharding strategy.
*
* <p>
* Use default table sharding strategy if not found.
* </p>
*
* @param tableRule table rule
* @return table sharding strategy
*/
public ShardingStrategy getTableShardingStrategy(final TableRule tableRule) {
return null == tableRule.getTableShardingStrategy() ? defaultTableShardingStrategy : tableRule.getTableShardingStrategy();
}
/**
* Judge logic tables is all belong to binding encryptors.
*
* @param logicTableNames logic table names
* @return logic tables is all belong to binding encryptors or not
*/
public boolean isAllBindingTables(final Collection<String> logicTableNames) {
if (logicTableNames.isEmpty()) {
return false;
}
Optional<BindingTableRule> bindingTableRule = findBindingTableRule(logicTableNames);
if (!bindingTableRule.isPresent()) {
return false;
}
Collection<String> result = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
result.addAll(bindingTableRule.get().getAllLogicTables());
return !result.isEmpty() && result.containsAll(logicTableNames);
}
private Optional<BindingTableRule> findBindingTableRule(final Collection<String> logicTableNames) {
for (String each : logicTableNames) {
Optional<BindingTableRule> result = findBindingTableRule(each);
if (result.isPresent()) {
return result;
}
}
return Optional.absent();
}
/**
* Find binding table rule via logic table name.
*
* @param logicTableName logic table name
* @return binding table rule
*/
public Optional<BindingTableRule> findBindingTableRule(final String logicTableName) {
for (BindingTableRule each : bindingTableRules) {
if (each.hasLogicTable(logicTableName)) {
return Optional.of(each);
}
}
return Optional.absent();
}
/**
* Judge logic tables is all belong to broadcast encryptors.
*
* @param logicTableNames logic table names
* @return logic tables is all belong to broadcast encryptors or not
*/
public boolean isAllBroadcastTables(final Collection<String> logicTableNames) {
if (logicTableNames.isEmpty()) {
return false;
}
for (String each : logicTableNames) {
if (!isBroadcastTable(each)) {
return false;
}
}
return true;
}
/**
* Judge logic table is belong to broadcast tables.
*
* @param logicTableName logic table name
* @return logic table is belong to broadcast tables or not
*/
public boolean isBroadcastTable(final String logicTableName) {
for (String each : broadcastTables) {
if (each.equalsIgnoreCase(logicTableName)) {
return true;
}
}
return false;
}
/**
* Judge logic tables is all belong to default data source.
*
* @param logicTableNames logic table names
* @return logic tables is all belong to default data source
*/
public boolean isAllInDefaultDataSource(final Collection<String> logicTableNames) {
for (String each : logicTableNames) {
if (findTableRule(each).isPresent() || isBroadcastTable(each)) {
return false;
}
}
return !logicTableNames.isEmpty();
}
/**
* Judge is sharding column or not.
*
* @param columnName column name
* @param tableName table name
* @return is sharding column or not
*/
public boolean isShardingColumn(final String columnName, final String tableName) {
for (TableRule each : tableRules) {
if (each.getLogicTable().equalsIgnoreCase(tableName) && isShardingColumn(each, columnName)) {
return true;
}
}
return false;
}
private boolean isShardingColumn(final TableRule tableRule, final String columnName) {
return getDatabaseShardingStrategy(tableRule).getShardingColumns().contains(columnName) || getTableShardingStrategy(tableRule).getShardingColumns().contains(columnName);
}
/**
* Find column name of generated key.
*
* @param logicTableName logic table name
* @return column name of generated key
*/
public Optional<String> findGenerateKeyColumnName(final String logicTableName) {
for (TableRule each : tableRules) {
if (each.getLogicTable().equalsIgnoreCase(logicTableName) && null != each.getGenerateKeyColumn()) {
return Optional.of(each.getGenerateKeyColumn());
}
}
return Optional.absent();
}
/**
* Generate key.
*
* @param logicTableName logic table name
* @return generated key
*/
public Comparable<?> generateKey(final String logicTableName) {
Optional<TableRule> tableRule = findTableRule(logicTableName);
if (!tableRule.isPresent()) {
throw new ShardingConfigurationException("Cannot find strategy for generate keys.");
}
ShardingKeyGenerator shardingKeyGenerator = null == tableRule.get().getShardingKeyGenerator() ? defaultShardingKeyGenerator : tableRule.get().getShardingKeyGenerator();
return shardingKeyGenerator.generateKey();
}
/**
* Get logic table names base on actual table name.
*
* @param actualTableName actual table name
* @return logic table name
*/
public Collection<String> getLogicTableNames(final String actualTableName) {
Collection<String> result = new LinkedList<>();
for (TableRule each : tableRules) {
if (each.isExisted(actualTableName)) {
result.add(each.getLogicTable());
}
}
return result;
}
/**
* Find data node by logic table name.
*
* @param logicTableName logic table name
* @return data node
*/
public DataNode getDataNode(final String logicTableName) {
TableRule tableRule = getTableRule(logicTableName);
return tableRule.getActualDataNodes().get(0);
}
/**
* Find data node by data source and logic table.
*
* @param dataSourceName data source name
* @param logicTableName logic table name
* @return data node
*/
public DataNode getDataNode(final String dataSourceName, final String logicTableName) {
TableRule tableRule = getTableRule(logicTableName);
for (DataNode each : tableRule.getActualDataNodes()) {
if (shardingDataSourceNames.getDataSourceNames().contains(each.getDataSourceName()) && each.getDataSourceName().equals(dataSourceName)) {
return each;
}
}
throw new ShardingConfigurationException("Cannot find actual data node for data source name: '%s' and logic table name: '%s'", dataSourceName, logicTableName);
}
/**
* Find actual default data source name.
*
* <p>If use master-slave rule, return master data source name.</p>
*
* @return actual default data source name
*/
public Optional<String> findActualDefaultDataSourceName() {
String defaultDataSourceName = shardingDataSourceNames.getDefaultDataSourceName();
if (Strings.isNullOrEmpty(defaultDataSourceName)) {
return Optional.absent();
}
Optional<String> masterDefaultDataSourceName = findMasterDataSourceName(defaultDataSourceName);
return masterDefaultDataSourceName.isPresent() ? masterDefaultDataSourceName : Optional.of(defaultDataSourceName);
}
private Optional<String> findMasterDataSourceName(final String masterSlaveRuleName) {
for (MasterSlaveRule each : masterSlaveRules) {
if (each.getName().equals(masterSlaveRuleName)) {
return Optional.of(each.getMasterDataSourceName());
}
}
return Optional.absent();
}
/**
* Find master slave rule.
*
* @param dataSourceName data source name
* @return master slave rule
*/
public Optional<MasterSlaveRule> findMasterSlaveRule(final String dataSourceName) {
for (MasterSlaveRule each : masterSlaveRules) {
if (each.containDataSourceName(dataSourceName)) {
return Optional.of(each);
}
}
return Optional.absent();
}
/**
* Get sharding logic table names.
*
* @param logicTableNames logic table names
* @return sharding logic table names
*/
public Collection<String> getShardingLogicTableNames(final Collection<String> logicTableNames) {
Collection<String> result = new LinkedList<>();
for (String each : logicTableNames) {
Optional<TableRule> tableRule = findTableRule(each);
if (tableRule.isPresent()) {
result.add(each);
}
}
return result;
}
/**
* Is routing by hint.
*
* @param tableRule table rule
* @return route by hint is true
*/
public boolean isRoutingByHint(final TableRule tableRule) {
return this.getDatabaseShardingStrategy(tableRule) instanceof HintShardingStrategy && this.getTableShardingStrategy(tableRule) instanceof HintShardingStrategy;
}
}