Skip to content

Commit

Permalink
Unify constructor of ComputeNodeInstanceContext
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Dec 17, 2024
1 parent 1d8282f commit 1d66c04
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,12 @@ public final class ComputeNodeInstanceContext {

private final Collection<ComputeNodeInstance> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComputeNodeInstance> 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<ComputeNodeInstance> actual = instanceContext.getComputeNodeInstanceById("bar_instance_id");
assertTrue(actual.isPresent());
assertThat(actual.get().getMetaData().getId(), is("bar_instance_id"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 1d66c04

Please sign in to comment.