Shadow DB

Background

In the distributed application architecture based on microservices, businesses require multiple services to be completed through a series of services and middleware, so the stress test of a single service can no longer meet the needs of real scenarios. If we reconstruct a stress test environment similar to the production environment, it is too expensive and often fails to simulate the complexity and traffic of the online environment. For this reason, the industry often chooses the full link stress test, which is performed in the production environment, so that the test results can accurately reflect the true capacity and performance of the system.

Parameters

Root Configuration

Class name: org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration

Attributes:

NameData TypeDescription
dataSourcesMap<String, ShadowDataSourceConfiguration>shadow data source mapping name and configuration
tablesMap<String, ShadowTableConfiguration>shadow table name and configuration
shadowAlgorithmsMap<String, AlgorithmConfiguration>shadow algorithm name and configuration
defaultShadowAlgorithmNameStringdefault shadow algorithm name

Shadow Data Source Configuration

Class name: org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration

Attributes:

NameDataTypeDescription
productionDataSourceNameStringProduction data source name
shadowDataSourceNameStringShadow data source name

Shadow Table Configuration

Class name: org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration

Attributes:

NameData TypeDescription
dataSourceNamesCollection<String>shadow table associates shadow data source mapping name list
shadowAlgorithmNamesCollection<String>shadow table associates shadow algorithm name list

Shadow Algorithm Configuration

Class name:org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration

Attributes:

NameData TypeDescription
typeStringshadow algorithm type
propsPropertiesshadow algorithm configuration

Please refer to Built-in Shadow Algorithm List.

Procedure

  1. Create production and shadow data source.
  2. Configure shadow rule.
  • Configure shadow data source
  • Configure shadow table
  • Configure shadow algorithm

Sample

  1. public final class ShadowConfiguration {
  2. @Override
  3. public DataSource getDataSource() throws SQLException {
  4. Map<String, DataSource> dataSourceMap = createDataSourceMap();
  5. return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, createRuleConfigurations(), createShardingSphereProps());
  6. }
  7. private Map<String, DataSource> createDataSourceMap() {
  8. Map<String, DataSource> result = new LinkedHashMap<>();
  9. result.put("ds", DataSourceUtil.createDataSource("demo_ds"));
  10. result.put("ds_shadow", DataSourceUtil.createDataSource("shadow_demo_ds"));
  11. return result;
  12. }
  13. private Collection<RuleConfiguration> createRuleConfigurations() {
  14. Collection<RuleConfiguration> result = new LinkedList<>();
  15. ShadowRuleConfiguration shadowRule = new ShadowRuleConfiguration();
  16. shadowRule.setDataSources(createShadowDataSources());
  17. shadowRule.setTables(createShadowTables());
  18. shadowRule.setShadowAlgorithms(createShadowAlgorithmConfigurations());
  19. result.add(shadowRule);
  20. return result;
  21. }
  22. private Map<String, ShadowDataSourceConfiguration> createShadowDataSources() {
  23. Map<String, ShadowDataSourceConfiguration> result = new LinkedHashMap<>();
  24. result.put("shadow-data-source", new ShadowDataSourceConfiguration("ds", "ds_shadow"));
  25. return result;
  26. }
  27. private Map<String, ShadowTableConfiguration> createShadowTables() {
  28. Map<String, ShadowTableConfiguration> result = new LinkedHashMap<>();
  29. result.put("t_user", new ShadowTableConfiguration(Collections.singletonList("shadow-data-source"), createShadowAlgorithmNames()));
  30. return result;
  31. }
  32. private Collection<String> createShadowAlgorithmNames() {
  33. Collection<String> result = new LinkedList<>();
  34. result.add("user-id-insert-match-algorithm");
  35. result.add("simple-hint-algorithm");
  36. return result;
  37. }
  38. private Map<String, AlgorithmConfiguration> createShadowAlgorithmConfigurations() {
  39. Map<String, AlgorithmConfiguration> result = new LinkedHashMap<>();
  40. Properties userIdInsertProps = new Properties();
  41. userIdInsertProps.setProperty("operation", "insert");
  42. userIdInsertProps.setProperty("column", "user_type");
  43. userIdInsertProps.setProperty("value", "1");
  44. result.put("user-id-insert-match-algorithm", new AlgorithmConfiguration("VALUE_MATCH", userIdInsertProps));
  45. return result;
  46. }
  47. }

Features Description of Shadow DB