This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 0ad269a Adjust Register Center and add TODO (#16371)
0ad269a is described below
commit 0ad269abd0823336d26d8429483c08b7b5a2f37b
Author: zhaojinchao <[email protected]>
AuthorDate: Fri Mar 25 19:13:58 2022 +0800
Adjust Register Center and add TODO (#16371)
---
.../mode/manager/ContextManager.java | 13 +++--
.../mode/metadata/MetaDataContextsBuilder.java | 3 +-
.../metadata/persist/node/SchemaMetaDataNode.java | 12 ++--
.../service/SchemaMetaDataPersistService.java | 64 +++++++++++++++-------
.../mode/manager/ContextManagerTest.java | 2 +-
.../persist/node/SchemaMetaDataNodeTest.java | 2 +-
.../service/SchemaMetaDataPersistServiceTest.java | 7 +--
.../cluster/ClusterContextManagerBuilder.java | 2 +-
8 files changed, 65 insertions(+), 40 deletions(-)
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
index bb0a10a..0c1b1f8 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
@@ -350,7 +350,7 @@ public final class ContextManager implements AutoCloseable {
try {
ShardingSphereSchema schema = loadActualSchema(schemaName);
alterSchema(schemaName, Collections.singletonMap(schemaName,
schema));
- metaDataContexts.getMetaDataPersistService().ifPresent(optional ->
optional.getSchemaMetaDataService().persist(schemaName, schema));
+ metaDataContexts.getMetaDataPersistService().ifPresent(optional ->
optional.getSchemaMetaDataService().persist(schemaName, schemaName, schema));
} catch (final SQLException ex) {
log.error("Reload schema:{} meta data failed", schemaName, ex);
}
@@ -396,7 +396,8 @@ public final class ContextManager implements AutoCloseable {
TableMetaData tableMetaData =
TableMetaDataBuilder.load(Collections.singletonList(tableName),
materials).getOrDefault(tableName, new TableMetaData());
if (!tableMetaData.getColumns().isEmpty()) {
metaDataContexts.getMetaData(schemaName).getSchema().put(tableName,
tableMetaData);
- metaDataContexts.getMetaDataPersistService().ifPresent(optional ->
optional.getSchemaMetaDataService().persist(schemaName,
metaDataContexts.getMetaData(schemaName).getSchema()));
+ metaDataContexts.getMetaDataPersistService().ifPresent(optional ->
+ optional.getSchemaMetaDataService().persist(schemaName,
schemaName, metaDataContexts.getMetaData(schemaName).getSchema()));
}
}
@@ -481,7 +482,7 @@ public final class ContextManager implements AutoCloseable {
MetaDataContextsBuilder metaDataContextsBuilder = new
MetaDataContextsBuilder(metaDataContexts.getGlobalRuleMetaData().getConfigurations(),
props);
metaDataContextsBuilder.addSchema(originalMetaData.getName(),
originalMetaData.getResource().getDatabaseType(), schemaConfiguration, props);
metaDataContexts.getMetaDataPersistService().ifPresent(
- optional ->
optional.getSchemaMetaDataService().persist(originalMetaData.getName(),
metaDataContextsBuilder.getSchemaMap(originalMetaData.getName())));
+ optional ->
optional.getSchemaMetaDataService().persist(originalMetaData.getName(),
originalMetaData.getName(),
metaDataContextsBuilder.getSchemaMap(originalMetaData.getName())));
return
metaDataContextsBuilder.build(metaDataContexts.getMetaDataPersistService().orElse(null));
}
@@ -499,7 +500,7 @@ public final class ContextManager implements AutoCloseable {
metaDataContextsBuilder.addSchema(originalMetaData.getName(),
originalMetaData.getResource().getDatabaseType(),
new
DataSourceProvidedSchemaConfiguration(originalMetaData.getResource().getDataSources(),
ruleConfigs), props);
metaDataContexts.getMetaDataPersistService().ifPresent(
- optional ->
optional.getSchemaMetaDataService().persist(originalMetaData.getName(),
metaDataContextsBuilder.getSchemaMap(originalMetaData.getName())));
+ optional ->
optional.getSchemaMetaDataService().persist(originalMetaData.getName(),
originalMetaData.getName(),
metaDataContextsBuilder.getSchemaMap(originalMetaData.getName())));
return
metaDataContextsBuilder.build(metaDataContexts.getMetaDataPersistService().orElse(null));
}
@@ -513,7 +514,7 @@ public final class ContextManager implements AutoCloseable {
getNewDataSources(originalMetaData.getResource().getDataSources(),
getAddedDataSources(originalMetaData, newDataSourceProps), changedDataSources,
deletedDataSources),
originalMetaData.getRuleMetaData().getConfigurations()),
props);
metaDataContexts.getMetaDataPersistService().ifPresent(
- optional ->
optional.getSchemaMetaDataService().persist(originalMetaData.getName(),
metaDataContextsBuilder.getSchemaMap(originalMetaData.getName())));
+ optional ->
optional.getSchemaMetaDataService().persist(originalMetaData.getName(),
originalMetaData.getName(),
metaDataContextsBuilder.getSchemaMap(originalMetaData.getName())));
return
metaDataContextsBuilder.build(metaDataContexts.getMetaDataPersistService().orElse(null));
}
@@ -527,7 +528,7 @@ public final class ContextManager implements AutoCloseable {
new
DataSourceProvidedSchemaConfiguration(getNewDataSources(originalMetaData.getResource().getDataSources(),
getAddedDataSources(originalMetaData, newDataSourceProps),
changedDataSources, deletedDataSources), ruleConfigs), props);
metaDataContexts.getMetaDataPersistService().ifPresent(
- optional ->
optional.getSchemaMetaDataService().persist(originalMetaData.getName(),
metaDataContextsBuilder.getSchemaMap(originalMetaData.getName())));
+ optional ->
optional.getSchemaMetaDataService().persist(originalMetaData.getName(),
originalMetaData.getName(),
metaDataContextsBuilder.getSchemaMap(originalMetaData.getName())));
return
metaDataContextsBuilder.build(metaDataContexts.getMetaDataPersistService().orElse(null));
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsBuilder.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsBuilder.java
index ce1983a..c9c7887 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsBuilder.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsBuilder.java
@@ -40,8 +40,8 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
-import java.util.Map.Entry;
import java.util.Properties;
+import java.util.Map.Entry;
/**
* Meta data contexts builder.
@@ -82,6 +82,7 @@ public final class MetaDataContextsBuilder {
schemaConfigMap.put(schemaName, schemaConfig);
schemaRulesMap.put(schemaName, schemaRules);
databaseMap.put(schemaName, database);
+ //TODO load system schemas and persist
}
/**
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNode.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNode.java
index 86ea525..5cfe78f 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNode.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNode.java
@@ -97,22 +97,24 @@ public final class SchemaMetaDataNode {
/**
* Get meta data tables path.
*
+ * @param databaseName database name
* @param schemaName schema name
* @return tables path
*/
- public static String getMetaDataTablesPath(final String schemaName) {
- return getFullMetaDataPath(schemaName, TABLES_NODE);
+ public static String getMetaDataTablesPath(final String databaseName,
final String schemaName) {
+ return String.join("/", getDatabaseNamePath(databaseName), schemaName,
TABLES_NODE);
}
/**
* Get table meta data path.
- *
+ *
+ * @param databaseName database name
* @param schemaName schema name
* @param table table name
* @return table meta data path
*/
- public static String getTableMetaDataPath(final String schemaName, final
String table) {
- return String.join("/", getMetaDataTablesPath(schemaName), table);
+ public static String getTableMetaDataPath(final String databaseName, final
String schemaName, final String table) {
+ return String.join("/", getMetaDataTablesPath(databaseName,
schemaName), table);
}
private static String getFullMetaDataPath(final String schemaName, final
String node) {
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/SchemaMetaDataPersistService.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/SchemaMetaDataPersistService.java
index 93ddbff..037275f 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/SchemaMetaDataPersistService.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/SchemaMetaDataPersistService.java
@@ -28,7 +28,6 @@ import
org.apache.shardingsphere.mode.persist.PersistRepository;
import java.util.Collection;
import java.util.LinkedHashMap;
-import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;
@@ -43,19 +42,20 @@ public final class SchemaMetaDataPersistService {
/**
* Persist schema.
*
+ * @param databaseName database name to be persisted
* @param schemaName schema name to be persisted
* @param schema schema to be persisted
*/
- public void persist(final String schemaName, final ShardingSphereSchema
schema) {
+ public void persist(final String databaseName, final String schemaName,
final ShardingSphereSchema schema) {
if (null == schema) {
return;
}
- Optional<ShardingSphereSchema> originalSchema = load(schemaName);
+ Optional<ShardingSphereSchema> originalSchema = load(databaseName,
schemaName);
if (originalSchema.isPresent()) {
- compareAndPersist(schemaName, schema, originalSchema.get());
+ compareAndPersist(databaseName, schemaName, schema,
originalSchema.get());
return;
}
- persistTables(schemaName, schema.getTables());
+ persistTables(databaseName, schemaName, schema.getTables());
}
/**
@@ -65,7 +65,19 @@ public final class SchemaMetaDataPersistService {
* @param tableMetaData table meta data
*/
public void persist(final String schemaName, final TableMetaData
tableMetaData) {
- repository.persist(SchemaMetaDataNode.getTableMetaDataPath(schemaName,
tableMetaData.getName().toLowerCase()),
+ repository.persist(SchemaMetaDataNode.getTableMetaDataPath(schemaName,
schemaName, tableMetaData.getName().toLowerCase()),
+ YamlEngine.marshal(new
TableMetaDataYamlSwapper().swapToYamlConfiguration(tableMetaData)));
+ }
+
+ /**
+ * Persist table meta data.
+ *
+ * @param databaseName database name
+ * @param schemaName schema name
+ * @param tableMetaData table meta data
+ */
+ public void persist(final String databaseName, final String schemaName,
final TableMetaData tableMetaData) {
+
repository.persist(SchemaMetaDataNode.getTableMetaDataPath(databaseName,
schemaName, tableMetaData.getName().toLowerCase()),
YamlEngine.marshal(new
TableMetaDataYamlSwapper().swapToYamlConfiguration(tableMetaData)));
}
@@ -75,29 +87,30 @@ public final class SchemaMetaDataPersistService {
* @param schemaName schema name
*/
public void persist(final String schemaName) {
-
repository.persist(SchemaMetaDataNode.getMetaDataTablesPath(schemaName), "");
+
repository.persist(SchemaMetaDataNode.getMetaDataTablesPath(schemaName,
schemaName), "");
}
- private void compareAndPersist(final String schemaName, final
ShardingSphereSchema schema, final ShardingSphereSchema originalSchema) {
+ private void compareAndPersist(final String databaseName, final String
schemaName, final ShardingSphereSchema schema, final ShardingSphereSchema
originalSchema) {
Map<String, TableMetaData> cachedLocalTables = new
LinkedHashMap<>(schema.getTables());
for (Map.Entry<String, TableMetaData> entry :
originalSchema.getTables().entrySet()) {
String onlineTableName = entry.getKey();
TableMetaData localTableMetaData =
cachedLocalTables.remove(onlineTableName);
if (null == localTableMetaData) {
- delete(schemaName, onlineTableName);
+ delete(databaseName, schemaName, onlineTableName);
continue;
}
if (!localTableMetaData.equals(entry.getValue())) {
- persist(schemaName, localTableMetaData);
+ persist(databaseName, schemaName, localTableMetaData);
}
}
if (!cachedLocalTables.isEmpty()) {
- persistTables(schemaName, cachedLocalTables);
+ persistTables(databaseName, schemaName, cachedLocalTables);
}
}
- private void persistTables(final String schemaName, final Map<String,
TableMetaData> tables) {
- tables.forEach((key, value) ->
repository.persist(SchemaMetaDataNode.getTableMetaDataPath(schemaName, key),
YamlEngine.marshal(new
TableMetaDataYamlSwapper().swapToYamlConfiguration(value))));
+ private void persistTables(final String databaseName, final String
schemaName, final Map<String, TableMetaData> tables) {
+ tables.forEach((key, value) ->
repository.persist(SchemaMetaDataNode.getTableMetaDataPath(databaseName,
schemaName, key),
+ YamlEngine.marshal(new
TableMetaDataYamlSwapper().swapToYamlConfiguration(value))));
}
/**
@@ -116,23 +129,35 @@ public final class SchemaMetaDataPersistService {
* @param tableName table name
*/
public void delete(final String schemaName, final String tableName) {
- repository.delete(SchemaMetaDataNode.getTableMetaDataPath(schemaName,
tableName));
+ repository.delete(SchemaMetaDataNode.getTableMetaDataPath(schemaName,
schemaName, tableName));
+ }
+
+ /**
+ * Delete table meta data.
+ *
+ * @param databaseName database name
+ * @param schemaName schema name
+ * @param tableName table name
+ */
+ public void delete(final String databaseName, final String schemaName,
final String tableName) {
+
repository.delete(SchemaMetaDataNode.getTableMetaDataPath(databaseName,
schemaName, tableName));
}
/**
* Load schema.
*
+ * @param databaseName database name to be loaded
* @param schemaName schema name to be loaded
* @return Loaded schema
*/
- public Optional<ShardingSphereSchema> load(final String schemaName) {
- Collection<String> tables =
repository.getChildrenKeys(SchemaMetaDataNode.getMetaDataTablesPath(schemaName));
+ public Optional<ShardingSphereSchema> load(final String databaseName,
final String schemaName) {
+ Collection<String> tables =
repository.getChildrenKeys(SchemaMetaDataNode.getMetaDataTablesPath(databaseName,
schemaName));
if (tables.isEmpty()) {
return Optional.empty();
}
ShardingSphereSchema schema = new ShardingSphereSchema();
tables.forEach(each -> {
- String content =
repository.get(SchemaMetaDataNode.getTableMetaDataPath(schemaName, each));
+ String content =
repository.get(SchemaMetaDataNode.getTableMetaDataPath(databaseName,
schemaName, each));
TableMetaData tableMetaData = new
TableMetaDataYamlSwapper().swapToObject(YamlEngine.unmarshal(content,
YamlTableMetaData.class));
schema.getTables().put(each, tableMetaData);
});
@@ -145,9 +170,6 @@ public final class SchemaMetaDataPersistService {
* @return all schema names
*/
public Collection<String> loadAllNames() {
- Collection<String> result = new LinkedList<>();
-
repository.getChildrenKeys(SchemaMetaDataNode.getMetaDataNodePath()).forEach(each
->
-
result.addAll(repository.getChildrenKeys(SchemaMetaDataNode.getDatabaseNamePath(each))));
- return result;
+ return
repository.getChildrenKeys(SchemaMetaDataNode.getMetaDataNodePath());
}
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
index 2e43152..f02c678 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
@@ -263,7 +263,7 @@ public final class ContextManagerTest {
when(metaDataPersistService.getSchemaMetaDataService()).thenReturn(schemaMetaDataPersistService);
when(metaDataContexts.getMetaDataPersistService()).thenReturn(Optional.of(metaDataPersistService));
contextManager.reloadMetaData("foo_schema");
- verify(schemaMetaDataPersistService,
times(1)).persist(eq("foo_schema"), any(ShardingSphereSchema.class));
+ verify(schemaMetaDataPersistService,
times(1)).persist(eq("foo_schema"), eq("foo_schema"),
any(ShardingSphereSchema.class));
contextManager.reloadMetaData("foo_schema", "foo_table");
assertNotNull(contextManager.getMetaDataContexts().getMetaData("foo_schema"));
contextManager.reloadMetaData("foo_schema", "foo_table", "foo_ds");
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNodeTest.java
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNodeTest.java
index 9af5f15..267db14 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNodeTest.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNodeTest.java
@@ -54,7 +54,7 @@ public class SchemaMetaDataNodeTest {
@Test
public void assertGetMetaDataTablesPath() {
- assertThat(SchemaMetaDataNode.getMetaDataTablesPath("sharding_db"),
is("/metadata/sharding_db/sharding_db/tables"));
+ assertThat(SchemaMetaDataNode.getMetaDataTablesPath("sharding_db",
"sharding_db"), is("/metadata/sharding_db/sharding_db/tables"));
}
@Test
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/SchemaMetaDataPersistServiceTest.java
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/SchemaMetaDataPersistServiceTest.java
index f2cbc6d..ad79de2 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/SchemaMetaDataPersistServiceTest.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/SchemaMetaDataPersistServiceTest.java
@@ -60,7 +60,7 @@ public final class SchemaMetaDataPersistServiceTest {
TableMetaData tableMetaData = new
TableMetaDataYamlSwapper().swapToObject(YamlEngine.unmarshal(readYAML(),
YamlTableMetaData.class));
ShardingSphereSchema schema = new ShardingSphereSchema();
schema.getTables().put("t_order", tableMetaData);
- new SchemaMetaDataPersistService(repository).persist("foo_db", schema);
+ new SchemaMetaDataPersistService(repository).persist("foo_db",
"foo_db", schema);
verify(repository).persist(eq("/metadata/foo_db/foo_db/tables/t_order"),
anyString());
}
@@ -81,9 +81,9 @@ public final class SchemaMetaDataPersistServiceTest {
SchemaMetaDataPersistService schemaMetaDataPersistService = new
SchemaMetaDataPersistService(repository);
when(repository.getChildrenKeys("/metadata/foo_db/foo_db/tables")).thenReturn(Lists.newArrayList("t_order"));
when(repository.get("/metadata/foo_db/foo_db/tables/t_order")).thenReturn(readYAML());
- Optional<ShardingSphereSchema> schemaOptional =
schemaMetaDataPersistService.load("foo_db");
+ Optional<ShardingSphereSchema> schemaOptional =
schemaMetaDataPersistService.load("foo_db", "foo_db");
assertTrue(schemaOptional.isPresent());
- Optional<ShardingSphereSchema> empty =
schemaMetaDataPersistService.load("test");
+ Optional<ShardingSphereSchema> empty =
schemaMetaDataPersistService.load("test", "test");
assertThat(empty, is(Optional.empty()));
ShardingSphereSchema schema = schemaOptional.get();
assertThat(schema.getAllTableNames(),
is(Collections.singleton("t_order")));
@@ -95,7 +95,6 @@ public final class SchemaMetaDataPersistServiceTest {
@Test
public void assertLoadAllNames() {
when(repository.getChildrenKeys("/metadata")).thenReturn(Arrays.asList("foo_db"));
-
when(repository.getChildrenKeys("/metadata/foo_db")).thenReturn(Arrays.asList("foo_db"));
Collection<String> actual = new
SchemaMetaDataPersistService(repository).loadAllNames();
assertThat(actual.size(), is(1));
assertThat(actual, hasItems("foo_db"));
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
index 865b5da..3b06923 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
@@ -150,7 +150,7 @@ public final class ClusterContextManagerBuilder implements
ContextManagerBuilder
private void persistMetaData(final MetaDataPersistService
metaDataPersistService, final Map<String, ShardingSphereDatabase> databaseMap) {
databaseMap.entrySet().forEach(entry ->
entry.getValue().getSchemas().entrySet().forEach(schema ->
-
metaDataPersistService.getSchemaMetaDataService().persist(schema.getKey(),
schema.getValue())));
+
metaDataPersistService.getSchemaMetaDataService().persist(entry.getKey(),
schema.getKey(), schema.getValue())));
}
private ContextManager createContextManager(final ClusterPersistRepository
repository, final MetaDataPersistService metaDataPersistService,