diff --git a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleServiceTest.java b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleServiceTest.java index b14d859f858c9..63a883710dd63 100644 --- a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleServiceTest.java +++ b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleServiceTest.java @@ -72,8 +72,8 @@ void assertStart() throws IOException { private ContextManager mockContextManager() { 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()); + new ComputeNodeInstance(mock(InstanceMetaData.class)), new ModeConfiguration("Standalone", null), new EventBusContext()); + computeNodeInstanceContext.init(new StandaloneWorkerIdGenerator(), mock(LockContext.class)); return new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class)); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java index 391c25814ad13..1af0543e4fad6 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java @@ -112,8 +112,9 @@ private ShardingRule createShardingRule() { nonCacheableTableSharding.setTableShardingStrategy(new StandardShardingStrategyConfiguration("id", "table-inline")); ruleConfig.getTables().add(nonCacheableTableSharding); ruleConfig.setShardingCache(new ShardingCacheConfiguration(100, new ShardingCacheOptionsConfiguration(true, 0, 0))); - return new ShardingRule(ruleConfig, Maps.of("ds_0", new MockedDataSource(), "ds_1", new MockedDataSource()), - new ComputeNodeInstanceContext(mock(ComputeNodeInstance.class), props -> 0, null, null, null), Collections.emptyList()); + ComputeNodeInstanceContext instanceContext = new ComputeNodeInstanceContext(mock(ComputeNodeInstance.class), null, null); + instanceContext.init(props -> 0, null); + return new ShardingRule(ruleConfig, Maps.of("ds_0", new MockedDataSource(), "ds_1", new MockedDataSource()), instanceContext, Collections.emptyList()); } private TimestampServiceRule createTimeServiceRule() { diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstanceContext.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstanceContext.java index 302bc0d81b2ff..47ed9440cd88c 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstanceContext.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstanceContext.java @@ -54,19 +54,12 @@ public final class ComputeNodeInstanceContext { private final Collection allClusterInstances = new CopyOnWriteArrayList<>(); - public ComputeNodeInstanceContext(final ComputeNodeInstance instance, final WorkerIdGenerator workerIdGenerator, - final ModeConfiguration modeConfig, final LockContext lockContext, final EventBusContext eventBusContext) { + public ComputeNodeInstanceContext(final ComputeNodeInstance instance, final ModeConfiguration modeConfig, final EventBusContext eventBusContext) { this.instance = instance; - this.workerIdGenerator.set(workerIdGenerator); this.modeConfiguration = modeConfig; - this.lockContext.set(lockContext); this.eventBusContext = eventBusContext; } - public ComputeNodeInstanceContext(final ComputeNodeInstance instance, final ModeConfiguration modeConfig, final EventBusContext eventBusContext) { - this(instance, null, modeConfig, null, eventBusContext); - } - /** * Initialize compute node instance context. * diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstanceContextTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstanceContextTest.java index dfffac5f40bae..a60e8fe7b92c1 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstanceContextTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstanceContextTest.java @@ -54,100 +54,105 @@ void assertInit() { @Test void assertUpdateStatusWithInvalidInstanceState() { InstanceMetaData instanceMetaData = mock(InstanceMetaData.class); - ComputeNodeInstanceContext context = new ComputeNodeInstanceContext( - new ComputeNodeInstance(instanceMetaData), mock(WorkerIdGenerator.class), mock(ModeConfiguration.class), mock(LockContext.class), new EventBusContext()); - context.updateStatus("id", "INVALID"); + ComputeNodeInstanceContext instanceContext = new ComputeNodeInstanceContext(new ComputeNodeInstance(instanceMetaData), mock(ModeConfiguration.class), new EventBusContext()); + instanceContext.init(mock(WorkerIdGenerator.class), mock(LockContext.class)); + instanceContext.updateStatus("id", "INVALID"); verify(instanceMetaData, times(0)).getId(); } @Test void assertUpdateStatusWithCurrentInstance() { InstanceMetaData instanceMetaData = new ProxyInstanceMetaData("foo_instance_id", 3306); - ComputeNodeInstanceContext context = new ComputeNodeInstanceContext( - new ComputeNodeInstance(instanceMetaData), mock(WorkerIdGenerator.class), mock(ModeConfiguration.class), mock(LockContext.class), new EventBusContext()); - context.addComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); - context.updateStatus("foo_instance_id", InstanceState.CIRCUIT_BREAK.name()); - assertThat(context.getInstance().getState().getCurrentState(), is(InstanceState.CIRCUIT_BREAK)); + ComputeNodeInstanceContext instanceContext = new ComputeNodeInstanceContext(new ComputeNodeInstance(instanceMetaData), mock(ModeConfiguration.class), new EventBusContext()); + instanceContext.init(mock(WorkerIdGenerator.class), mock(LockContext.class)); + instanceContext.addComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); + instanceContext.updateStatus("foo_instance_id", InstanceState.CIRCUIT_BREAK.name()); + assertThat(instanceContext.getInstance().getState().getCurrentState(), is(InstanceState.CIRCUIT_BREAK)); } @Test void assertUpdateStatusWithOtherInstance() { InstanceMetaData instanceMetaData = new ProxyInstanceMetaData("foo_instance_id", 3306); - ComputeNodeInstanceContext context = new ComputeNodeInstanceContext( - new ComputeNodeInstance(instanceMetaData), mock(WorkerIdGenerator.class), mock(ModeConfiguration.class), mock(LockContext.class), new EventBusContext()); - context.addComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); - context.updateStatus("bar_instance_id", InstanceState.CIRCUIT_BREAK.name()); - assertThat(context.getInstance().getState().getCurrentState(), is(InstanceState.OK)); + ComputeNodeInstanceContext instanceContext = new ComputeNodeInstanceContext(new ComputeNodeInstance(instanceMetaData), mock(ModeConfiguration.class), new EventBusContext()); + instanceContext.init(mock(WorkerIdGenerator.class), mock(LockContext.class)); + instanceContext.addComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); + instanceContext.updateStatus("bar_instance_id", InstanceState.CIRCUIT_BREAK.name()); + assertThat(instanceContext.getInstance().getState().getCurrentState(), is(InstanceState.OK)); } @Test void assertUpdateLabelsWithCurrentInstance() { InstanceMetaData instanceMetaData = new ProxyInstanceMetaData("foo_instance_id", 3306); - ComputeNodeInstanceContext context = new ComputeNodeInstanceContext( - new ComputeNodeInstance(instanceMetaData), mock(WorkerIdGenerator.class), mock(ModeConfiguration.class), mock(LockContext.class), new EventBusContext()); - context.updateLabels("foo_instance_id", Arrays.asList("label_1", "label_2")); - assertThat(context.getInstance().getLabels(), is(Arrays.asList("label_1", "label_2"))); + ComputeNodeInstanceContext instanceContext = new ComputeNodeInstanceContext(new ComputeNodeInstance(instanceMetaData), mock(ModeConfiguration.class), new EventBusContext()); + instanceContext.init(mock(WorkerIdGenerator.class), mock(LockContext.class)); + instanceContext.updateLabels("foo_instance_id", Arrays.asList("label_1", "label_2")); + assertThat(instanceContext.getInstance().getLabels(), is(Arrays.asList("label_1", "label_2"))); } @Test void assertUpdateLabelsWithOtherInstance() { InstanceMetaData instanceMetaData = new ProxyInstanceMetaData("foo_instance_id", 3306); - ComputeNodeInstanceContext context = new ComputeNodeInstanceContext( - new ComputeNodeInstance(instanceMetaData), mock(WorkerIdGenerator.class), mock(ModeConfiguration.class), mock(LockContext.class), new EventBusContext()); - context.addComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); - context.updateLabels("bar_instance_id", Arrays.asList("label_1", "label_2")); - assertTrue(context.getInstance().getLabels().isEmpty()); - assertThat(context.getAllClusterInstances().iterator().next().getLabels(), is(Arrays.asList("label_1", "label_2"))); + ComputeNodeInstanceContext instanceContext = new ComputeNodeInstanceContext(new ComputeNodeInstance(instanceMetaData), mock(ModeConfiguration.class), new EventBusContext()); + instanceContext.init(mock(WorkerIdGenerator.class), mock(LockContext.class)); + instanceContext.addComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); + instanceContext.updateLabels("bar_instance_id", Arrays.asList("label_1", "label_2")); + assertTrue(instanceContext.getInstance().getLabels().isEmpty()); + assertThat(instanceContext.getAllClusterInstances().iterator().next().getLabels(), is(Arrays.asList("label_1", "label_2"))); } @Test void assertUpdateWorkerIdWithCurrentInstance() { ComputeNodeInstance instance = new ComputeNodeInstance(new ProxyInstanceMetaData("foo_instance_id", 3306)); - ComputeNodeInstanceContext context = new ComputeNodeInstanceContext(instance, mock(WorkerIdGenerator.class), mock(ModeConfiguration.class), mock(LockContext.class), new EventBusContext()); - context.updateWorkerId("foo_instance_id", 10); - assertThat(context.getWorkerId(), is(10)); + ComputeNodeInstanceContext instanceContext = new ComputeNodeInstanceContext(instance, mock(ModeConfiguration.class), new EventBusContext()); + instanceContext.init(mock(WorkerIdGenerator.class), mock(LockContext.class)); + instanceContext.updateWorkerId("foo_instance_id", 10); + assertThat(instanceContext.getWorkerId(), is(10)); } @Test void assertUpdateWorkerIdWithOtherInstance() { ComputeNodeInstance instance = new ComputeNodeInstance(new ProxyInstanceMetaData("foo_instance_id", 3306)); - ComputeNodeInstanceContext context = new ComputeNodeInstanceContext(instance, mock(WorkerIdGenerator.class), mock(ModeConfiguration.class), mock(LockContext.class), new EventBusContext()); - context.addComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); - context.updateWorkerId("bar_instance_id", 10); - assertThat(context.getWorkerId(), is(-1)); - assertThat(context.getAllClusterInstances().iterator().next().getWorkerId(), is(10)); + ComputeNodeInstanceContext instanceContext = new ComputeNodeInstanceContext(instance, mock(ModeConfiguration.class), new EventBusContext()); + instanceContext.init(mock(WorkerIdGenerator.class), mock(LockContext.class)); + instanceContext.addComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); + instanceContext.updateWorkerId("bar_instance_id", 10); + assertThat(instanceContext.getWorkerId(), is(-1)); + assertThat(instanceContext.getAllClusterInstances().iterator().next().getWorkerId(), is(10)); } @Test void assertGenerateWorkerId() { - ComputeNodeInstanceContext context = new ComputeNodeInstanceContext( - new ComputeNodeInstance(mock(InstanceMetaData.class)), mock(WorkerIdGenerator.class), mock(ModeConfiguration.class), mock(LockContext.class), new EventBusContext()); - assertThat(context.generateWorkerId(new Properties()), is(0)); + ComputeNodeInstanceContext instanceContext = new ComputeNodeInstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), mock(ModeConfiguration.class), new EventBusContext()); + instanceContext.init(mock(WorkerIdGenerator.class), mock(LockContext.class)); + assertThat(instanceContext.generateWorkerId(new Properties()), is(0)); } @Test void assertAddComputeNodeInstance() { ComputeNodeInstance instance = new ComputeNodeInstance(new ProxyInstanceMetaData("foo_instance_id", 3306)); - ComputeNodeInstanceContext context = new ComputeNodeInstanceContext(instance, mock(WorkerIdGenerator.class), mock(ModeConfiguration.class), mock(LockContext.class), new EventBusContext()); - context.addComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); - assertFalse(context.getAllClusterInstances().isEmpty()); + ComputeNodeInstanceContext instanceContext = new ComputeNodeInstanceContext(instance, mock(ModeConfiguration.class), new EventBusContext()); + instanceContext.init(mock(WorkerIdGenerator.class), mock(LockContext.class)); + instanceContext.addComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); + assertFalse(instanceContext.getAllClusterInstances().isEmpty()); } @Test void assertDeleteComputeNodeInstance() { ComputeNodeInstance instance = new ComputeNodeInstance(new ProxyInstanceMetaData("foo_instance_id", 3306)); - ComputeNodeInstanceContext context = new ComputeNodeInstanceContext(instance, mock(WorkerIdGenerator.class), mock(ModeConfiguration.class), mock(LockContext.class), new EventBusContext()); - context.addComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); - context.deleteComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); - assertTrue(context.getAllClusterInstances().isEmpty()); + ComputeNodeInstanceContext instanceContext = new ComputeNodeInstanceContext(instance, mock(ModeConfiguration.class), new EventBusContext()); + instanceContext.init(mock(WorkerIdGenerator.class), mock(LockContext.class)); + instanceContext.addComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); + instanceContext.deleteComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); + assertTrue(instanceContext.getAllClusterInstances().isEmpty()); } @Test void assertGetComputeNodeInstanceById() { ComputeNodeInstance instance = new ComputeNodeInstance(new ProxyInstanceMetaData("foo_instance_id", 3306)); - ComputeNodeInstanceContext context = new ComputeNodeInstanceContext(instance, mock(WorkerIdGenerator.class), mock(ModeConfiguration.class), mock(LockContext.class), new EventBusContext()); - context.addComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); - Optional actual = context.getComputeNodeInstanceById("bar_instance_id"); + ComputeNodeInstanceContext instanceContext = new ComputeNodeInstanceContext(instance, mock(ModeConfiguration.class), new EventBusContext()); + instanceContext.init(mock(WorkerIdGenerator.class), mock(LockContext.class)); + instanceContext.addComputeNodeInstance(new ComputeNodeInstance(new ProxyInstanceMetaData("bar_instance_id", 3307))); + Optional actual = instanceContext.getComputeNodeInstanceById("bar_instance_id"); assertTrue(actual.isPresent()); assertThat(actual.get().getMetaData().getId(), is("bar_instance_id")); } diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java index f4229e584146e..627e268103922 100644 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java +++ b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java @@ -46,16 +46,12 @@ public ContextManager build(final ContextManagerBuilderParameter param, final Ev StandalonePersistRepository repository = TypedSPILoader.getService( StandalonePersistRepository.class, null == repositoryConfig ? null : repositoryConfig.getType(), null == repositoryConfig ? new Properties() : repositoryConfig.getProps()); MetaDataPersistService persistService = new MetaDataPersistService(repository); - ComputeNodeInstanceContext computeNodeInstanceContext = buildComputeNodeInstanceContext(param, eventBusContext); + ComputeNodeInstanceContext computeNodeInstanceContext = new ComputeNodeInstanceContext(new ComputeNodeInstance(param.getInstanceMetaData()), param.getModeConfiguration(), eventBusContext); + computeNodeInstanceContext.init(new StandaloneWorkerIdGenerator(), new GlobalLockContext(null)); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(persistService, param, computeNodeInstanceContext); return new ContextManager(metaDataContexts, computeNodeInstanceContext, repository); } - private ComputeNodeInstanceContext buildComputeNodeInstanceContext(final ContextManagerBuilderParameter param, final EventBusContext eventBusContext) { - return new ComputeNodeInstanceContext(new ComputeNodeInstance(param.getInstanceMetaData()), - new StandaloneWorkerIdGenerator(), param.getModeConfiguration(), new GlobalLockContext(null), eventBusContext); - } - @Override public String getType() { return "Standalone"; diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java index 2700c0fc35873..908fd91169ef7 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java @@ -168,8 +168,8 @@ private ContextManager mockContextManager() { new GlobalClockRule(new DefaultGlobalClockRuleConfigurationBuilder().build()))), new ConfigurationProperties(PropertiesBuilder.build(new Property(ConfigurationPropertyKey.SQL_SHOW.getKey(), "true"))))); ComputeNodeInstanceContext computeNodeInstanceContext = new ComputeNodeInstanceContext( - new ComputeNodeInstance(mock(InstanceMetaData.class)), new StandaloneWorkerIdGenerator(), new ModeConfiguration("Standalone", null), - mock(LockContext.class), new EventBusContext()); + new ComputeNodeInstance(mock(InstanceMetaData.class)), new ModeConfiguration("Standalone", null), new EventBusContext()); + computeNodeInstanceContext.init(new StandaloneWorkerIdGenerator(), mock(LockContext.class)); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); when(result.getComputeNodeInstanceContext()).thenReturn(computeNodeInstanceContext); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutorTest.java index 1c3d517bc2ade..0d0c811034f41 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutorTest.java @@ -109,7 +109,8 @@ private ContextManager mockContextManager() { MetaDataPersistService metaDataPersistService = mock(MetaDataPersistService.class, RETURNS_DEEP_STUBS); when(metaDataPersistService.getPropsService()).thenReturn(mock(PropertiesPersistService.class)); ComputeNodeInstanceContext computeNodeInstanceContext = new ComputeNodeInstanceContext( - new ComputeNodeInstance(mock(InstanceMetaData.class)), mock(WorkerIdGenerator.class), new ModeConfiguration("Standalone", null), mock(LockContext.class), new EventBusContext()); + new ComputeNodeInstance(mock(InstanceMetaData.class)), new ModeConfiguration("Standalone", null), new EventBusContext()); + computeNodeInstanceContext.init(mock(WorkerIdGenerator.class), mock(LockContext.class)); return new ContextManager(MetaDataContextsFactory.create(metaDataPersistService, new ShardingSphereMetaData()), computeNodeInstanceContext, mock(PersistRepository.class)); } }