Skip to content

Commit

Permalink
Add ModeContextManager interface and split ClusterModeContextManager …
Browse files Browse the repository at this point in the history
…and StandaloneModeContextManager (apache#23061)

* Add ModeContextManager and split ClusterModeContextManager and StandaloneModeContextManager

* Refactor alterRuleConfiguration logic

* Fix checkstyle

* Fix checkstyle

* Fix it

* Fix unit test

* Add synchronized

* Fix checkstyle
  • Loading branch information
zhaojinchao95 authored Dec 24, 2022
1 parent 4b5424d commit 34a5db9
Show file tree
Hide file tree
Showing 32 changed files with 535 additions and 263 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
Expand Down Expand Up @@ -53,8 +54,8 @@ public void close() {
public void assertStart() throws IOException {
MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData());
InstanceContext instanceContext = new InstanceContext(
new ComputeNodeInstance(mock(InstanceMetaData.class)), new StandaloneWorkerIdGenerator(), new ModeConfiguration("Standalone", null), mock(LockContext.class),
new EventBusContext());
new ComputeNodeInstance(mock(InstanceMetaData.class)), new StandaloneWorkerIdGenerator(), new ModeConfiguration("Standalone", null),
mock(ModeContextManager.class), mock(LockContext.class), new EventBusContext());
ProxyContext.init(new ContextManager(metaDataContexts, instanceContext));
pluginBootService.start(new PluginConfiguration("localhost", 8090, "", createProperties()), true);
new Socket().connect(new InetSocketAddress("localhost", 8090));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
Expand All @@ -41,8 +42,8 @@ public final class ProxyInfoCollectorTest extends ProxyContextRestorer {
public void assertCollect() {
MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData());
InstanceContext instanceContext = new InstanceContext(
new ComputeNodeInstance(mock(InstanceMetaData.class)), new StandaloneWorkerIdGenerator(), new ModeConfiguration("Standalone", null), mock(LockContext.class),
new EventBusContext());
new ComputeNodeInstance(mock(InstanceMetaData.class)), new StandaloneWorkerIdGenerator(), new ModeConfiguration("Standalone", null),
mock(ModeContextManager.class), mock(LockContext.class), new EventBusContext());
ProxyContext.init(new ContextManager(metaDataContexts, instanceContext));
assertFalse(new ProxyInfoCollector().collect().isEmpty());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.instance.InstanceContextAware;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.sharding.algorithm.keygen.fixture.FixedTimeService;
Expand Down Expand Up @@ -203,7 +204,7 @@ private void setSequence(final KeyGenerateAlgorithm algorithm, final Number valu
public void assertSetWorkerIdFailureWhenNegative() {
SnowflakeKeyGenerateAlgorithm algorithm = (SnowflakeKeyGenerateAlgorithm) KeyGenerateAlgorithmFactory.newInstance(new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
InstanceContext instanceContext = new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(-1),
new ModeConfiguration("Standalone", null), mock(LockContext.class), new EventBusContext());
new ModeConfiguration("Standalone", null), mock(ModeContextManager.class), mock(LockContext.class), new EventBusContext());
algorithm.setInstanceContext(instanceContext);
algorithm.generateKey();
}
Expand All @@ -219,7 +220,7 @@ public void assertSetMaxVibrationOffsetFailureWhenNegative() {
public void assertSetWorkerIdFailureWhenOutOfRange() {
SnowflakeKeyGenerateAlgorithm algorithm = (SnowflakeKeyGenerateAlgorithm) KeyGenerateAlgorithmFactory.newInstance(new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
InstanceContext instanceContext = new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(Integer.MIN_VALUE),
new ModeConfiguration("Standalone", null), mock(LockContext.class), new EventBusContext());
new ModeConfiguration("Standalone", null), mock(ModeContextManager.class), mock(LockContext.class), new EventBusContext());
algorithm.setInstanceContext(instanceContext);
algorithm.generateKey();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private ShardingRule prepareShardingRule() {
ShardingTableRuleConfiguration nonCacheableTableSharding = new ShardingTableRuleConfiguration("t_non_cacheable_table_sharding", "ds_0.t_non_cacheable_table_sharding_${0..1}");
nonCacheableTableSharding.setTableShardingStrategy(new StandardShardingStrategyConfiguration("id", "inline"));
ruleConfig.getTables().add(nonCacheableTableSharding);
return new ShardingRule(ruleConfig, Arrays.asList("ds_0", "ds_1"), new InstanceContext(mock(ComputeNodeInstance.class), props -> 0, null, null, null));
return new ShardingRule(ruleConfig, Arrays.asList("ds_0", "ds_1"), new InstanceContext(mock(ComputeNodeInstance.class), props -> 0, null, null, null, null));
}

private ShardingCacheRule prepareShardingCacheRule(final ShardingRule shardingRule) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.sharding.cosid.algorithm.keygen.fixture.WorkerIdGeneratorFixture;
Expand Down Expand Up @@ -60,7 +61,7 @@ public void assertGenerateKey() {
CosIdSnowflakeKeyGenerateAlgorithm algorithm = (CosIdSnowflakeKeyGenerateAlgorithm) KeyGenerateAlgorithmFactory.newInstance(
new AlgorithmConfiguration("COSID_SNOWFLAKE", new Properties()));
algorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(FIXTURE_WORKER_ID),
new ModeConfiguration("Standalone", null), mock(LockContext.class), eventBusContext));
new ModeConfiguration("Standalone", null), mock(ModeContextManager.class), mock(LockContext.class), eventBusContext));
long firstActualKey = (Long) algorithm.generateKey();
long secondActualKey = (Long) algorithm.generateKey();
SnowflakeIdState firstActualState = snowflakeIdStateParser.parse(firstActualKey);
Expand All @@ -76,7 +77,7 @@ public void assertGenerateKey() {
public void assertGenerateKeyModUniformity() {
CosIdSnowflakeKeyGenerateAlgorithm algorithm = (CosIdSnowflakeKeyGenerateAlgorithm) KeyGenerateAlgorithmFactory.newInstance(new AlgorithmConfiguration("COSID_SNOWFLAKE", new Properties()));
algorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(FIXTURE_WORKER_ID),
new ModeConfiguration("Standalone", null), mock(LockContext.class), eventBusContext));
new ModeConfiguration("Standalone", null), mock(ModeContextManager.class), mock(LockContext.class), eventBusContext));
int divisor = 4;
int total = 99999;
int avg = total / divisor;
Expand Down Expand Up @@ -121,7 +122,7 @@ public void assertGenerateKeyAsString() {
new AlgorithmConfiguration("COSID_SNOWFLAKE", props));
algorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)),
new WorkerIdGeneratorFixture(FIXTURE_WORKER_ID), new ModeConfiguration("Standalone", null),
mock(LockContext.class), eventBusContext));
mock(ModeContextManager.class), mock(LockContext.class), eventBusContext));
Comparable<?> actualKey = algorithm.generateKey();
assertThat(actualKey, instanceOf(String.class));
String actualStringKey = (String) actualKey;
Expand All @@ -142,7 +143,7 @@ public void assertGenerateKeyWhenNegative() {
CosIdSnowflakeKeyGenerateAlgorithm algorithm = (CosIdSnowflakeKeyGenerateAlgorithm) KeyGenerateAlgorithmFactory.newInstance(
new AlgorithmConfiguration("COSID_SNOWFLAKE", new Properties()));
algorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(-1),
new ModeConfiguration("Standalone", null), mock(LockContext.class), eventBusContext));
new ModeConfiguration("Standalone", null), mock(ModeContextManager.class), mock(LockContext.class), eventBusContext));
algorithm.generateKey();
}

Expand All @@ -151,7 +152,7 @@ public void assertGenerateKeyWhenGreaterThen1023() {
CosIdSnowflakeKeyGenerateAlgorithm algorithm = (CosIdSnowflakeKeyGenerateAlgorithm) KeyGenerateAlgorithmFactory.newInstance(
new AlgorithmConfiguration("COSID_SNOWFLAKE", new Properties()));
algorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(1024),
new ModeConfiguration("Standalone", null), mock(LockContext.class), eventBusContext));
new ModeConfiguration("Standalone", null), mock(ModeContextManager.class), mock(LockContext.class), eventBusContext));
algorithm.generateKey();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
Expand Down Expand Up @@ -48,6 +49,8 @@ public final class InstanceContext {

private final ModeConfiguration modeConfiguration;

private final ModeContextManager modeContextManager;

private final LockContext lockContext;

private final EventBusContext eventBusContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.infra.instance.mode;

import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Map;
import java.util.Properties;

/**
* Mode context manager.
*/
public interface ModeContextManager {

/**
* Create database.
*
* @param databaseName database name
*/
void createDatabase(String databaseName);

/**
* Drop database.
*
* @param databaseName database name
*/
void dropDatabase(String databaseName);

/**
* Register storage units.
*
* @param databaseName database name
* @param toBeRegisterStorageUnitProps to be register storage unit props
* @throws SQLException SQL exception
*/
void registerStorageUnits(String databaseName, Map<String, DataSourceProperties> toBeRegisterStorageUnitProps) throws SQLException;

/**
* Alter storage units.
*
* @param databaseName database name
* @param toBeUpdatedStorageUnitProps to be updated storage unit props
* @throws SQLException SQL exception
*/
void alterStorageUnits(String databaseName, Map<String, DataSourceProperties> toBeUpdatedStorageUnitProps) throws SQLException;

/**
* Unregister storage units.
* @param databaseName database name
* @param toBeDroppedStorageUnitNames to be dropped storage unit names
* @throws SQLException SQL exception
*/
void unregisterStorageUnits(String databaseName, Collection<String> toBeDroppedStorageUnitNames) throws SQLException;

/**
* Alter rule configuration.
*
* @param databaseName database name
* @param ruleConfigs rule configs
*/
void alterRuleConfiguration(String databaseName, Collection<RuleConfiguration> ruleConfigs);

/**
* Alter global rule configuration.
*
* @param globalRuleConfigs global rule configs
*/
void alterGlobalRuleConfiguration(Collection<RuleConfiguration> globalRuleConfigs);

/**
* Alter properties.
*
* @param props pros
*/
void alterProperties(Properties props);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.instance.fixture.WorkerIdGeneratorFixture;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.state.StateType;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
Expand All @@ -43,6 +44,8 @@ public final class InstanceContextTest {

private final ModeConfiguration modeConfig = new ModeConfiguration("Standalone", null);

private final ModeContextManager modeContextManager = mock(ModeContextManager.class);

private final LockContext lockContext = mock(LockContext.class);

private final EventBusContext eventBusContext = new EventBusContext();
Expand All @@ -51,7 +54,8 @@ public final class InstanceContextTest {
public void assertUpdateInstanceStatus() {
InstanceMetaData instanceMetaData = mock(InstanceMetaData.class);
when(instanceMetaData.getId()).thenReturn("foo_instance_id");
InstanceContext context = new InstanceContext(new ComputeNodeInstance(instanceMetaData), new WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig, lockContext, eventBusContext);
InstanceContext context = new InstanceContext(new ComputeNodeInstance(instanceMetaData),
new WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig, modeContextManager, lockContext, eventBusContext);
StateType actual = context.getInstance().getState().getCurrentState();
assertThat(actual, is(StateType.OK));
context.updateInstanceStatus(instanceMetaData.getId(), StateType.CIRCUIT_BREAK.name());
Expand All @@ -66,22 +70,23 @@ public void assertUpdateInstanceStatus() {
public void assertGetWorkerId() {
ComputeNodeInstance computeNodeInstance = mock(ComputeNodeInstance.class);
when(computeNodeInstance.getWorkerId()).thenReturn(0);
InstanceContext context = new InstanceContext(computeNodeInstance, new WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig, lockContext, eventBusContext);
InstanceContext context = new InstanceContext(computeNodeInstance, new WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig, modeContextManager, lockContext, eventBusContext);
assertThat(context.getWorkerId(), is(0));
}

@Test
public void assertGenerateWorkerId() {
InstanceContext context = new InstanceContext(
new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig, lockContext, eventBusContext);
new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig, modeContextManager, lockContext, eventBusContext);
assertThat(context.generateWorkerId(new Properties()), is(Integer.MIN_VALUE));
}

@Test
public void assertUpdateLabel() {
InstanceMetaData instanceMetaData = mock(InstanceMetaData.class);
when(instanceMetaData.getId()).thenReturn("foo_instance_id");
InstanceContext context = new InstanceContext(new ComputeNodeInstance(instanceMetaData), new WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig, lockContext, eventBusContext);
InstanceContext context = new InstanceContext(new ComputeNodeInstance(instanceMetaData), new WorkerIdGeneratorFixture(Integer.MIN_VALUE),
modeConfig, modeContextManager, lockContext, eventBusContext);
Set<String> expected = new LinkedHashSet<>(Arrays.asList("label_1", "label_2"));
context.updateLabel("foo_instance_id", expected);
Collection<String> actual = context.getInstance().getLabels();
Expand All @@ -91,32 +96,32 @@ public void assertUpdateLabel() {
@Test
public void assertGetInstance() {
ComputeNodeInstance expected = new ComputeNodeInstance(mock(InstanceMetaData.class));
InstanceContext context = new InstanceContext(expected, new WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig, lockContext, eventBusContext);
InstanceContext context = new InstanceContext(expected, new WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig, modeContextManager, lockContext, eventBusContext);
ComputeNodeInstance actual = context.getInstance();
assertThat(actual, is(expected));
}

@Test
public void assertGetState() {
InstanceContext context = new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig,
lockContext, eventBusContext);
modeContextManager, lockContext, eventBusContext);
assertNotNull(context.getInstance().getState());
}

@Test
public void assertGetModeConfiguration() {
InstanceContext context = new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig,
lockContext, eventBusContext);
modeContextManager, lockContext, eventBusContext);
assertThat(context.getModeConfiguration(), is(modeConfig));
}

@Test
public void assertIsCluster() {
InstanceContext context = new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(Integer.MIN_VALUE), modeConfig,
lockContext, eventBusContext);
modeContextManager, lockContext, eventBusContext);
assertFalse(context.isCluster());
InstanceContext clusterContext = new InstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), new WorkerIdGeneratorFixture(Integer.MIN_VALUE),
new ModeConfiguration("Cluster", null), lockContext, eventBusContext);
new ModeConfiguration("Cluster", null), modeContextManager, lockContext, eventBusContext);
assertTrue(clusterContext.isCluster());
}
}
Loading

0 comments on commit 34a5db9

Please sign in to comment.