Skip to content

Commit

Permalink
Add authority rule on test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed May 25, 2024
2 parents 4fc448c + 1daa1fa commit e1ab881
Show file tree
Hide file tree
Showing 80 changed files with 417 additions and 293 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
Expand Down Expand Up @@ -79,7 +80,7 @@ private ContextManager mockContextManager() {
when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"));
ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
when(metaData.getDatabases()).thenReturn(Collections.singletonMap("sharding_db", database));
MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), metaData);
MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData);
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(result.getMetaDataContexts()).thenReturn(metaDataContexts);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.manager.standalone.workerid.generator.StandaloneWorkerIdGenerator;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory;
import org.apache.shardingsphere.mode.spi.PersistRepository;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
Expand Down Expand Up @@ -79,10 +81,10 @@ void assertExportWithContextManager() {
}

private ContextManager mockContextManager() {
MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData());
MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData());
ComputeNodeInstanceContext computeNodeInstanceContext = new ComputeNodeInstanceContext(
new ComputeNodeInstance(mock(InstanceMetaData.class)), new StandaloneWorkerIdGenerator(), new ModeConfiguration("Standalone", null),
mock(LockContext.class), new EventBusContext());
return new ContextManager(metaDataContexts, computeNodeInstanceContext);
return new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.manager.standalone.workerid.generator.StandaloneWorkerIdGenerator;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory;
import org.apache.shardingsphere.mode.spi.PersistRepository;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
Expand Down Expand Up @@ -68,10 +70,10 @@ void assertStart() throws IOException {
}

private ContextManager mockContextManager() {
MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData());
MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData());
ComputeNodeInstanceContext computeNodeInstanceContext = new ComputeNodeInstanceContext(
new ComputeNodeInstance(mock(InstanceMetaData.class)), new StandaloneWorkerIdGenerator(), new ModeConfiguration("Standalone", null),
mock(LockContext.class), new EventBusContext());
return new ContextManager(metaDataContexts, computeNodeInstanceContext);
return new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ simpleSelect
2. We can use IDEA’s ANTLR4 plugin to easily view the syntax tree of the SQL statement.

For more information of ANTLR4 , please refer to: [https://plugins.jetbrains.com/plugin/7358-antlr-v4](https://plugins.jetbrains.com/plugin/7358-antlr-v4.).

![Image description](https://miro.medium.com/max/700/1*EiWkP_kYN3sLOH4qsPonDA.jpeg)

ANTLR4 can compile the syntax file we define: it first performs lexical analysis on the SQL statement, splits it into indivisible parts, namely tokens, and divides these tokens into keywords, expressions, according to the dictionary values of different databases.
Expand Down Expand Up @@ -157,6 +158,7 @@ At first, in terms of the above-mentioned `Visitor` method, the visitor to be us
Next, let’s still take the SQL statement as an example and provide specific code to show how `Visitor` formats it.

`MySQLFormatSQLVisitor` is used to visit SQL. Based on the `DEBUG` code, we can clearly see the execution path of this visit as shown in the figure below. Visitor traverses all parts of the syntax tree, and ANTLR4 generates default methods for visiting each node according to the defined grammar rules. Apache ShardingSphere leverages key methods and successfully develops complete the SQL formatting function.

![Image description](https://miro.medium.com/max/700/1*xjjACczbInC-K4t8EX-pEw.jpeg)

The following code can help us better understand how `Visitor` can format SQL.
Expand Down Expand Up @@ -235,6 +237,7 @@ public static void main(String[] args) {
```

- Parameters Supported by Properties

![Image description](https://miro.medium.com/max/550/1*1Ft7G0EKkayVy5vrbcsDBg.png)

You can also use DistSQL in ShardingSphere-Proxy to perform operations on the SQL Parse Format function:
Expand Down Expand Up @@ -279,6 +282,7 @@ Currently, Apache ShardingSphere’s Format function only supports [MySQL](https
## Author

**Chen Chuxin**

![Image description](https://miro.medium.com/max/634/1*smrIU5STVJsJRais0_Tghg.png)

> SphereEx Middleware Engineer & Apache ShardingSphere Committer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ chapter = true
> - Li Yong, Head of WMS Technology, Dangdang
Ffollowing [Apache ShardingSphere 5.0.0 GA](https://medium.com/codex/apache-shardingsphere-5-0-0-new-features-middleware-to-ecosystem-evolution-e69de00bfb1b) release in November 2021, the [5.1.0 version](https://shardingsphere.medium.com/apache-shardingsphere-5-1-0-now-avaliable-4244ac470e77) was released last month. Having gone through over two years of polishing, ShardingSphere’s plugin-oriented ecosystem is beginning to take shape, and the project embarks on the evolution from a simple data sharding middleware to a mature distributed database ecosystem driven by the concept of [Database Plus](https://www.infoq.com/articles/next-evolution-of-database-sharding-architecture/).
Following [Apache ShardingSphere 5.0.0 GA](https://medium.com/codex/apache-shardingsphere-5-0-0-new-features-middleware-to-ecosystem-evolution-e69de00bfb1b) release in November 2021, the [5.1.0 version](https://shardingsphere.medium.com/apache-shardingsphere-5-1-0-now-avaliable-4244ac470e77) was released last month. Having gone through over two years of polishing, ShardingSphere’s plugin-oriented ecosystem is beginning to take shape, and the project embarks on the evolution from a simple data sharding middleware to a mature distributed database ecosystem driven by the concept of [Database Plus](https://www.infoq.com/articles/next-evolution-of-database-sharding-architecture/).

Dangdang, established at the end of 1999, has become a leading e-commerce platform selling books of any kind, and by integrating new Internet technologies with the traditional book industry. Dangdang was founded during the surge in China’s Internet industry in the early 2000s.

Expand Down Expand Up @@ -115,7 +115,7 @@ Recently, to celebrate the third anniversary of ShardingSphere entering Apache S
- On November 10, 2021, Version 5.0.0 GA was released as a third-anniversity celebration with the whole Apache ShardingSphere community, and the distributed database industry.
![Apache ShardingSphere—Roadmap](https://miro.medium.com/max/1400/0*ejOCiszgebnrZ2kx)

Since Version 5.0.0, Apache ShardingSphere has embarked on its new journey: with the plugin oriented architect at its core, it evloved from a data sharding application to a comprehensive and enhanced data governance tool applicable to various complex application scenarios. Concurrently, Apache ShardingSphere also has more features, and big data solutions.
Since Version 5.0.0, Apache ShardingSphere has embarked on its new journey: With the plugin oriented architect at its core, it evloved from a data sharding application to a comprehensive and enhanced data governance tool applicable to various complex application scenarios. Concurrently, Apache ShardingSphere also has more features, and big data solutions.

## Conclusion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ rules:
worker-id: 123
```

We use the JMH test program to test different CASEs:
We use the JMH test program to test different cases:

```
@State(Scope.Thread)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
|-------------|-----------|---------------------------------------------------------|
| 20100 | 42000 | Unsupported SQL node conversion for SQL statement '%s'. |
| 20101 | 42000 | SQL federation does not support SQL '%s'. |
| 20102 | 42S02 | SQL federation schema not found SQL '%s'. |

### 读写分离

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,11 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi

### SQL Federation

| Vendor Code | SQL State | Reason |
|-------------|-----------|---------------------------------------------------------|
| Vendor Code | SQL State | Reason |
|-------------|-----------|--------------------------------------------------------|
| 20100 | 42000 | Unsupported SQL node conversion for SQL statement '%s'. |
| 20101 | 42000 | SQL federation does not support SQL '%s'. |
| 20101 | 42000 | SQL federation does not support SQL '%s'. |
| 20102 | 42S02 | SQL federation schema not found SQL '%s'. |

### Readwrite-splitting

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private void checkBeforeUpdate(final AlterReadwriteSplittingStorageUnitStatusSta

private void updateStatus(final ContextManager contextManager, final AlterReadwriteSplittingStorageUnitStatusStatement sqlStatement) {
DataSourceState status = sqlStatement.isEnable() ? DataSourceState.ENABLED : DataSourceState.DISABLED;
new QualifiedDataSourceStatusService(contextManager.getMetaDataContexts().getPersistService().getRepository())
new QualifiedDataSourceStatusService(contextManager.getRepository())
.changeStatus(database.getName(), sqlStatement.getRuleName(), sqlStatement.getStorageUnitName(), status);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void executeUpdate() {
checkBeforeUpdate();
RuleConfiguration currentRuleConfig = rule.map(ShardingSphereRule::getConfiguration).orElse(null);
if (getRefreshStatus(rule.isPresent())) {
contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService()
contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService()
.switchActiveVersion(DatabaseRuleOperatorFactory.newInstance(contextManager, executor).operate(sqlStatement, database, currentRuleConfig));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,18 +528,18 @@
"name":"org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaDataBuilder"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service.ComputeNodeStatusService"},
"condition":{"typeReachable":"org.apache.shardingsphere.mode.service.ComputeNodePersistService"},
"name":"org.apache.shardingsphere.infra.instance.yaml.YamlComputeNodeData",
"allDeclaredFields":true,
"queryAllPublicMethods":true,
"methods":[{"name":"<init>","parameterTypes":[] }, {"name":"getAttribute","parameterTypes":[] }, {"name":"getVersion","parameterTypes":[] }, {"name":"setAttribute","parameterTypes":["java.lang.String"] }, {"name":"setVersion","parameterTypes":["java.lang.String"] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service.ComputeNodeStatusService"},
"condition":{"typeReachable":"org.apache.shardingsphere.mode.service.ComputeNodePersistService"},
"name":"org.apache.shardingsphere.infra.instance.yaml.YamlComputeNodeDataBeanInfo"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service.ComputeNodeStatusService"},
"condition":{"typeReachable":"org.apache.shardingsphere.mode.service.ComputeNodePersistService"},
"name":"org.apache.shardingsphere.infra.instance.yaml.YamlComputeNodeDataCustomizer"
},
{
Expand Down Expand Up @@ -572,26 +572,26 @@
"queryAllDeclaredMethods":true
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.subsciber.EventSubscriberRegistry$$Lambda/0x00007feecf3840e0"},
"condition":{"typeReachable":"org.apache.shardingsphere.mode.subsciber.EventSubscriberRegistry$$Lambda/0x00007f12133825d0"},
"name":"org.apache.shardingsphere.infra.util.eventbus.EventSubscriber"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.connection.refresher.type.table.CreateTableStatementSchemaRefresher"},
"condition":{"typeReachable":"org.apache.shardingsphere.infra.util.yaml.YamlEngine"},
"name":"org.apache.shardingsphere.infra.util.yaml.YamlConfiguration",
"queryAllPublicMethods":true
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.util.yaml.YamlEngine"},
"condition":{"typeReachable":"org.apache.shardingsphere.metadata.persist.service.schema.ShardingSphereTableRowDataPersistService"},
"name":"org.apache.shardingsphere.infra.util.yaml.YamlConfiguration",
"queryAllPublicMethods":true
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.metadata.persist.service.schema.ShardingSphereTableRowDataPersistService"},
"condition":{"typeReachable":"org.apache.shardingsphere.mode.metadata.refresher.type.table.CreateTableStatementSchemaRefresher"},
"name":"org.apache.shardingsphere.infra.util.yaml.YamlConfiguration",
"queryAllPublicMethods":true
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service.ComputeNodeStatusService"},
"condition":{"typeReachable":"org.apache.shardingsphere.mode.service.ComputeNodePersistService"},
"name":"org.apache.shardingsphere.infra.util.yaml.YamlConfiguration",
"queryAllPublicMethods":true
},
Expand Down Expand Up @@ -694,11 +694,6 @@
"allDeclaredFields":true,
"methods":[{"name":"<init>","parameterTypes":[] }, {"name":"setName","parameterTypes":["java.lang.String"] }, {"name":"setUnique","parameterTypes":["boolean"] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.connection.refresher.type.table.CreateTableStatementSchemaRefresher"},
"name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTable",
"queryAllPublicMethods":true
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase"},
"name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTable",
Expand All @@ -717,11 +712,16 @@
"methods":[{"name":"<init>","parameterTypes":[] }, {"name":"getColumns","parameterTypes":[] }, {"name":"getConstraints","parameterTypes":[] }, {"name":"getIndexes","parameterTypes":[] }, {"name":"getName","parameterTypes":[] }, {"name":"getType","parameterTypes":[] }, {"name":"setColumns","parameterTypes":["java.util.Map"] }, {"name":"setIndexes","parameterTypes":["java.util.Map"] }, {"name":"setName","parameterTypes":["java.lang.String"] }, {"name":"setType","parameterTypes":["org.apache.shardingsphere.infra.database.core.metadata.database.enums.TableType"] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.connection.refresher.type.table.CreateTableStatementSchemaRefresher"},
"condition":{"typeReachable":"org.apache.shardingsphere.mode.metadata.refresher.type.table.CreateTableStatementSchemaRefresher"},
"name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTable",
"queryAllPublicMethods":true
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.metadata.refresher.type.table.CreateTableStatementSchemaRefresher"},
"name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTableBeanInfo"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.connection.refresher.type.table.CreateTableStatementSchemaRefresher"},
"condition":{"typeReachable":"org.apache.shardingsphere.mode.metadata.refresher.type.table.CreateTableStatementSchemaRefresher"},
"name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTableCustomizer"
},
{
Expand Down Expand Up @@ -910,20 +910,10 @@
"name":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.process.subscriber.ClusterProcessSubscriber",
"queryAllDeclaredMethods":true
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.ClusterContextManagerBuilder"},
"name":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.subscriber.ClusterStateSubscriber",
"queryAllDeclaredMethods":true
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcherFactory"},
"name":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.watcher.ClusterStateChangedWatcher"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.ClusterContextManagerBuilder"},
"name":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.subscriber.ComputeNodeStatusSubscriber",
"queryAllDeclaredMethods":true
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcherFactory"},
"name":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.watcher.ComputeNodeStateChangedWatcher"
Expand Down Expand Up @@ -960,6 +950,11 @@
"name":"org.apache.shardingsphere.mode.manager.cluster.coordinator.subscriber.StateChangedSubscriber",
"methods":[{"name":"renew","parameterTypes":["org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.event.ClusterStateEvent"] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.service.PersistServiceFacade"},
"name":"org.apache.shardingsphere.mode.manager.cluster.service.ClusterMetaDataManagerPersistServiceBuilder",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlModeConfigurationSwapper"},
"name":"org.apache.shardingsphere.mode.manager.cluster.yaml.ClusterYamlPersistRepositoryConfigurationSwapper"
Expand All @@ -968,6 +963,11 @@
"condition":{"typeReachable":"org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource"},
"name":"org.apache.shardingsphere.mode.manager.standalone.StandaloneContextManagerBuilder"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.service.PersistServiceFacade"},
"name":"org.apache.shardingsphere.mode.manager.standalone.service.StandaloneMetaDataManagerPersistServiceBuilder",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlModeConfigurationSwapper"},
"name":"org.apache.shardingsphere.mode.manager.standalone.yaml.StandaloneYamlPersistRepositoryConfigurationSwapper"
Expand Down
Loading

0 comments on commit e1ab881

Please sign in to comment.