You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2019/09/06 09:05:16 UTC

[GitHub] [incubator-shardingsphere] NikoTung opened a new issue #2988: The design purpose of the defaultDatasourceName in `ShardingRuleConfiguration`

NikoTung opened a new issue #2988: The design purpose of the defaultDatasourceName in `ShardingRuleConfiguration`
URL: https://github.com/apache/incubator-shardingsphere/issues/2988
 
 
   ## Question
   
   I recently started a new project and using shardingsphere to do the sharding job, table sharding. I came into the following problem:
   
   My schema have two logic tables, let's call them tableA and tableB. I made the tableB into 1000 sharding, like: tableB_0 ... table_999. The configuration just like below.
   
   	spring.shardingsphere.datasource.names=ds
   	spring.shardingsphere.datasource.ds.type=com.zaxxer.hikari.HikariDataSource
   	spring.shardingsphere.datasource.ds.driver-class-name=com.mysql.jdbc.Driver
   	spring.shardingsphere.datasource.ds.jdbc-url=jdbc:mysql://localhost:3306/demo_ds?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
   	spring.shardingsphere.datasource.ds.username=root
   	spring.shardingsphere.datasource.ds.password=
   
   	spring.shardingsphere.sharding.tables.tableB.actual-data-nodes=ds.t_order_$->{0..1}
   	spring.shardingsphere.sharding.tables.tableB.table-strategy.inline.sharding-column=id
   	spring.shardingsphere.sharding.tables.tableB.table-strategy.inline.algorithm-expression=tableB$->{id % 1000}
   
   But I found it cost too much time for the application to startup. After doing some Google search, I found the `check.table.metadata.enabled` is set to false by default.But why it still cost so much time to startup.It really confused me, then I tried to read the source code.
   
   In the `TableMetaDataInitializer`, it loads all the default table meta data, and the default tables are from the default defaultDatasoucreName in `ShardingRuleConfiguration`. In the `ShardingDataSourceNames` file, I can see the how the default datasource name is loaded:
   
   	public String getDefaultDataSourceName() {
   	        return 1 == dataSourceNames.size() ? dataSourceNames.iterator().next() : shardingRuleConfig.getDefaultDataSourceName();
   	    }
   
   * If we config only one datasource, then it will be used as the default datasource name.
   * If we config more than one datasource, then it will use the  dafault datasource name in `ShardingRuleConfiguration` as the default datasource name.
   * If the default datasource name is not configed, then it will skip loading the default table meta data.
   
   So I try the following code configuration to skip the default table meta data loading proccess.
   
   		HikariDataSource dataSource = new HikariDataSource(hikariConfig);
   		List<TableRuleConfiguration> configurations = new ArrayList();
   		...
       	Map<String, DataSource> dataSourceMap = new HashMap<>();
       	dataSourceMap.put("ds", dataSource);    
       	dataSourceMap.put("ds-1", dataSource); //point to the same datasource, to skip loading the default table meta data
   	    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
   	    shardingRuleConfig.getTableRuleConfigs().addAll(configurations);
   	    ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new HashMap<String, Object>(), new Properties());
   
   It worked, the application start faster.
   
   But when I used the same datasource(ds) to insert into tableA, a exception was throw: can not found datasource.
   
   So here is my question:
   
   In my situation, I have only one datasource, it will be used as the default datasource name.That is why it cost so much time to startup the application. Then the `check.table.metadata.enabled` option can not reduce the startup time. Which make me to think about the purpose of defaultDatasourceName: the shardingsphere is for those system which need to do some sharding when the data grow is too much.
   
   Let me make a example:
   
   At first, the schema(schema_old) only have two: tableA and tableB, all the data are written in these two tables. After years of system running, the data in tableB is too big and need to do sharing. Then we create a new schema for sharding,e.g schema_new, and create 1000 tables in it.After that we have two schemas and tables like below:
   
   	schema_old
   	 -- tableA
   	 -- tableB
   	schema_new
   	 -- tableB_0
   	 -- tableB_1
   	 ...
   	 --- table_999
   
   then the defaultDatasourceName point to the schema : schema_old. the sharding datasource point to : schema_new.
   
   From this point, the  defaultDatasourceName make sense to me. I don't know whether it is right or not. I need someone to help me to make this clear.
   
   
   [relative issue](https://github.com/apache/incubator-shardingsphere/issues/1951)
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services