This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 23c608e Add instance state type enum (#12537)
23c608e is described below
commit 23c608e5b9f7df0093d682daa93ca34b03596a43
Author: Haoran Meng <[email protected]>
AuthorDate: Sat Sep 18 11:29:46 2021 +0800
Add instance state type enum (#12537)
* Add instance state type enum
* Add instance state type enum
---
.../prometheus/collector/ProxyInfoCollector.java | 9 +++++----
.../shardingsphere/infra/state/StateContext.java | 8 ++++----
.../shardingsphere/infra/state/StateEvent.java | 2 +-
.../apache/shardingsphere/infra/state/StateType.java | 15 ++++-----------
.../shardingsphere/infra/state/StateContextTest.java | 20 ++++++++++----------
.../driver/state/DriverStateContext.java | 2 +-
.../watcher/ComputeNodeStateChangedWatcher.java | 4 ++--
.../proxy/frontend/state/ProxyStateContext.java | 9 +++++----
8 files changed, 32 insertions(+), 37 deletions(-)
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/ProxyInfoCollector.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/ProxyInfoCollector.java
index a30b91a..b0231df 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/ProxyInfoCollector.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/ProxyInfoCollector.java
@@ -22,6 +22,7 @@ import io.prometheus.client.GaugeMetricFamily;
import org.apache.shardingsphere.agent.metrics.api.constant.MetricIds;
import org.apache.shardingsphere.agent.metrics.api.util.MetricsUtil;
import
org.apache.shardingsphere.agent.metrics.prometheus.wrapper.PrometheusWrapperFactory;
+import org.apache.shardingsphere.infra.state.StateType;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import java.util.Collections;
@@ -41,11 +42,11 @@ public final class ProxyInfoCollector extends Collector {
private static final PrometheusWrapperFactory FACTORY = new
PrometheusWrapperFactory();
- private static final ConcurrentHashMap<String, Integer> PROXY_STATE_MAP =
new ConcurrentHashMap<>();
+ private static final ConcurrentHashMap<StateType, Integer> PROXY_STATE_MAP
= new ConcurrentHashMap<>();
static {
- PROXY_STATE_MAP.put("OK", 1);
- PROXY_STATE_MAP.put("CIRCUIT_BREAK", 2);
+ PROXY_STATE_MAP.put(StateType.OK, 1);
+ PROXY_STATE_MAP.put(StateType.CIRCUIT_BREAK, 2);
}
@Override
@@ -55,7 +56,7 @@ public final class ProxyInfoCollector extends Collector {
return result;
}
Optional<GaugeMetricFamily> proxyInfo =
FACTORY.createGaugeMetricFamily(MetricIds.PROXY_INFO);
- String currentState =
ProxyContext.getInstance().getStateContext().getCurrentState();
+ StateType currentState =
ProxyContext.getInstance().getStateContext().getCurrentState();
proxyInfo.ifPresent(m ->
m.addMetric(Collections.singletonList(PROXY_STATE),
PROXY_STATE_MAP.get(currentState)));
proxyInfo.ifPresent(result::add);
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateContext.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateContext.java
index 1beafe7..addff4a 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateContext.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateContext.java
@@ -30,7 +30,7 @@ import java.util.concurrent.ConcurrentLinkedDeque;
*/
public final class StateContext {
- private final Deque<String> currentState = new
ConcurrentLinkedDeque<>(Collections.singleton("OK"));
+ private final Deque<StateType> currentState = new
ConcurrentLinkedDeque<>(Collections.singleton(StateType.OK));
public StateContext() {
ShardingSphereEventBus.getInstance().register(this);
@@ -59,9 +59,9 @@ public final class StateContext {
/**
* Get current state.
*
- * @return current state
+ * @return current state type
*/
- public String getCurrentState() {
- return Optional.ofNullable(currentState.peek()).orElse("OK");
+ public StateType getCurrentState() {
+ return Optional.ofNullable(currentState.peek()).orElse(StateType.OK);
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateEvent.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateEvent.java
index 29153fd..d58e49a 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateEvent.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateEvent.java
@@ -27,7 +27,7 @@ import lombok.RequiredArgsConstructor;
@Getter
public final class StateEvent {
- private final String type;
+ private final StateType type;
private final boolean on;
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/node/ComputeNode.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateType.java
similarity index 72%
rename from
shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/node/ComputeNode.java
rename to
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateType.java
index 05a87ac..8c842f8 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/node/ComputeNode.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateType.java
@@ -15,19 +15,12 @@
* limitations under the License.
*/
-package
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.node;
-
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
+package org.apache.shardingsphere.infra.state;
/**
- * Compute node.
+ * State type.
*/
-@RequiredArgsConstructor
-@Getter
-public final class ComputeNode {
-
- private final String status;
+public enum StateType {
- private final String instanceId;
+ OK, LOCK, CIRCUIT_BREAK
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/state/StateContextTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/state/StateContextTest.java
index d534cb1..78f652b 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/state/StateContextTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/state/StateContextTest.java
@@ -28,23 +28,23 @@ public final class StateContextTest {
@Test
public void assertSwitchStateWithCircuitBreakOn() {
- stateContext.switchState(new StateEvent("CIRCUIT_BREAK", true));
- assertThat(stateContext.getCurrentState(), is("CIRCUIT_BREAK"));
- stateContext.switchState(new StateEvent("CIRCUIT_BREAK", false));
+ stateContext.switchState(new StateEvent(StateType.CIRCUIT_BREAK,
true));
+ assertThat(stateContext.getCurrentState(),
is(StateType.CIRCUIT_BREAK));
+ stateContext.switchState(new StateEvent(StateType.CIRCUIT_BREAK,
false));
}
@Test
public void assertSwitchStateWithCircuitBreakOff() {
- stateContext.switchState(new StateEvent("CIRCUIT_BREAK", false));
- assertThat(stateContext.getCurrentState(), is("OK"));
+ stateContext.switchState(new StateEvent(StateType.CIRCUIT_BREAK,
false));
+ assertThat(stateContext.getCurrentState(), is(StateType.OK));
}
@Test
public void assertSwitchStateWithMultiState() {
- stateContext.switchState(new StateEvent("CIRCUIT_BREAK", true));
- stateContext.switchState(new StateEvent("LOCK", true));
- assertThat(stateContext.getCurrentState(), is("LOCK"));
- stateContext.switchState(new StateEvent("LOCK", false));
- assertThat(stateContext.getCurrentState(), is("CIRCUIT_BREAK"));
+ stateContext.switchState(new StateEvent(StateType.CIRCUIT_BREAK,
true));
+ stateContext.switchState(new StateEvent(StateType.LOCK, true));
+ assertThat(stateContext.getCurrentState(), is(StateType.LOCK));
+ stateContext.switchState(new StateEvent(StateType.LOCK, false));
+ assertThat(stateContext.getCurrentState(),
is(StateType.CIRCUIT_BREAK));
}
}
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateContext.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateContext.java
index e6efa9c..c6fd37f 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateContext.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/state/DriverStateContext.java
@@ -57,6 +57,6 @@ public final class DriverStateContext {
* @return connection
*/
public static Connection getConnection(final String schemaName, final
Map<String, DataSource> dataSourceMap, final ContextManager contextManager,
final TransactionType transactionType) {
- return
STATES.get(contextManager.getMetaDataContexts().getStateContext().getCurrentState()).getConnection(schemaName,
dataSourceMap, contextManager, transactionType);
+ return
STATES.get(contextManager.getMetaDataContexts().getStateContext().getCurrentState().name()).getConnection(schemaName,
dataSourceMap, contextManager, transactionType);
}
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/watcher/ComputeNodeStateChangedWatcher.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/watcher/ComputeNodeStateChangedWatcher.java
index 93fd71a..d60dc90 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/watcher/ComputeNodeStateChangedWatcher.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/watcher/ComputeNodeStateChangedWatcher.java
@@ -18,6 +18,7 @@
package
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.watcher;
import org.apache.shardingsphere.infra.state.StateEvent;
+import org.apache.shardingsphere.infra.state.StateType;
import
org.apache.shardingsphere.mode.manager.cluster.coordinator.ClusterInstance;
import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher;
import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.ComputeNodeStatus;
@@ -47,8 +48,7 @@ public final class ComputeNodeStateChangedWatcher implements
GovernanceWatcher<S
@Override
public Optional<StateEvent> createGovernanceEvent(final DataChangedEvent
event) {
- // TODO use enum to instead of CIRCUIT_BREAK
- return isCircuitBreaker(event.getKey()) ? Optional.of(new
StateEvent("CIRCUIT_BREAK", Type.ADDED == event.getType())) : Optional.empty();
+ return isCircuitBreaker(event.getKey()) ? Optional.of(new
StateEvent(StateType.CIRCUIT_BREAK, Type.ADDED == event.getType())) :
Optional.empty();
}
private boolean isCircuitBreaker(final String dataChangedPath) {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/state/ProxyStateContext.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/state/ProxyStateContext.java
index 36a2451..238c602 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/state/ProxyStateContext.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/state/ProxyStateContext.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.proxy.frontend.state;
import io.netty.channel.ChannelHandlerContext;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.state.StateType;
import
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import
org.apache.shardingsphere.proxy.frontend.spi.DatabaseProtocolFrontendEngine;
@@ -36,12 +37,12 @@ import java.util.concurrent.ConcurrentHashMap;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ProxyStateContext {
- private static final Map<String, ProxyState> STATES = new
ConcurrentHashMap<>(3, 1);
+ private static final Map<StateType, ProxyState> STATES = new
ConcurrentHashMap<>(3, 1);
static {
- STATES.put("OK", new OKProxyState());
- STATES.put("LOCK", new LockProxyState());
- STATES.put("CIRCUIT_BREAK", new CircuitBreakProxyState());
+ STATES.put(StateType.OK, new OKProxyState());
+ STATES.put(StateType.LOCK, new LockProxyState());
+ STATES.put(StateType.CIRCUIT_BREAK, new CircuitBreakProxyState());
}
/**