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 096be158cd9 Refactor switch compute node state and remove lock state
(#31312)
096be158cd9 is described below
commit 096be158cd940ab56b6045020ba2cfb0410463eb
Author: Haoran Meng <[email protected]>
AuthorDate: Mon May 20 16:54:40 2024 +0800
Refactor switch compute node state and remove lock state (#31312)
---
.../infra/instance/ComputeNodeInstance.java | 10 +++----
.../infra/instance/InstanceContext.java | 13 +++++---
.../infra/state/instance/InstanceState.java | 19 +++++++++++-
.../infra/state/instance/InstanceStateContext.java | 10 +++----
.../infra/state/InstanceStateContextTest.java | 20 ++++---------
.../infra/state/instance/InstanceStateTest.java | 14 +++++----
.../compute/service/ComputeNodeStatusService.java | 3 +-
.../proxy/frontend/state/ProxyStateContext.java | 2 --
.../proxy/frontend/state/impl/LockProxyState.java | 35 ----------------------
9 files changed, 53 insertions(+), 73 deletions(-)
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstance.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstance.java
index 5e2d7796f0d..b04e29743da 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstance.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstance.java
@@ -48,13 +48,13 @@ public final class ComputeNodeInstance {
/**
* Switch state.
*
- * @param status status
+ * @param instanceState instance state
*/
- public void switchState(final String status) {
- if (InstanceState.CIRCUIT_BREAK.name().equals(status)) {
- state.switchToValidState(InstanceState.CIRCUIT_BREAK);
+ public void switchState(final InstanceState instanceState) {
+ if (InstanceState.CIRCUIT_BREAK == instanceState) {
+ state.switchState(instanceState);
} else {
- state.switchToInvalidState(InstanceState.CIRCUIT_BREAK);
+ state.recoverState(InstanceState.CIRCUIT_BREAK);
}
}
}
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceContext.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceContext.java
index ea7201190b0..6cb67d76aa4 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceContext.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceContext.java
@@ -26,6 +26,7 @@ import
org.apache.shardingsphere.infra.instance.metadata.InstanceType;
import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
import org.apache.shardingsphere.infra.instance.workerid.WorkerIdGenerator;
import org.apache.shardingsphere.infra.lock.LockContext;
+import org.apache.shardingsphere.infra.state.instance.InstanceState;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import java.util.Collection;
@@ -65,16 +66,20 @@ public final class InstanceContext {
* @param status status
*/
public void updateStatus(final String id, final String status) {
+ Optional<InstanceState> instanceState = InstanceState.get(status);
+ if (!instanceState.isPresent()) {
+ return;
+ }
if (instance.getMetaData().getId().equals(id)) {
- instance.switchState(status);
+ instance.switchState(instanceState.get());
}
- updateRelatedComputeNodeInstancesStatus(id, status);
+ updateRelatedComputeNodeInstancesStatus(id, instanceState.get());
}
- private void updateRelatedComputeNodeInstancesStatus(final String
instanceId, final String status) {
+ private void updateRelatedComputeNodeInstancesStatus(final String
instanceId, final InstanceState instanceState) {
for (ComputeNodeInstance each : allClusterComputeNodeInstances) {
if (each.getMetaData().getId().equals(instanceId)) {
- each.switchState(status);
+ each.switchState(instanceState);
}
}
}
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/state/instance/InstanceState.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/state/instance/InstanceState.java
index d03ccb48e3e..a6f4a30648b 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/state/instance/InstanceState.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/state/instance/InstanceState.java
@@ -17,10 +17,27 @@
package org.apache.shardingsphere.infra.state.instance;
+import java.util.Optional;
+
/**
* Instance state.
*/
public enum InstanceState {
- OK, CIRCUIT_BREAK, LOCK
+ OK, CIRCUIT_BREAK;
+
+ /**
+ * Get instance state enum by state.
+ *
+ * @param state state
+ * @return instance state enum
+ */
+ public static Optional<InstanceState> get(final String state) {
+ if (OK.name().equals(state)) {
+ return Optional.of(OK);
+ } else if (CIRCUIT_BREAK.name().equals(state)) {
+ return Optional.of(CIRCUIT_BREAK);
+ }
+ return Optional.empty();
+ }
}
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/state/instance/InstanceStateContext.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/state/instance/InstanceStateContext.java
index aeeb832c298..1f992f733c5 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/state/instance/InstanceStateContext.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/state/instance/InstanceStateContext.java
@@ -32,18 +32,18 @@ public final class InstanceStateContext {
/**
* Switch state.
*
- * @param state state
+ * @param state state to be switched
*/
- public void switchToValidState(final InstanceState state) {
+ public void switchState(final InstanceState state) {
currentState.push(state);
}
/**
- * Switch state.
+ * Recover state.
*
- * @param state state
+ * @param state state before being recovered
*/
- public void switchToInvalidState(final InstanceState state) {
+ public void recoverState(final InstanceState state) {
if (getCurrentState() == state) {
recoverState();
}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/state/InstanceStateContextTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/state/InstanceStateContextTest.java
index 15350297f53..14f666a03f8 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/state/InstanceStateContextTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/state/InstanceStateContextTest.java
@@ -29,24 +29,16 @@ class InstanceStateContextTest {
private final InstanceStateContext instanceStateContext = new
InstanceStateContext();
@Test
- void assertSwitchStateWithCircuitBreakOn() {
- instanceStateContext.switchToValidState(InstanceState.CIRCUIT_BREAK);
+ void assertSwitchState() {
+ instanceStateContext.switchState(InstanceState.CIRCUIT_BREAK);
assertThat(instanceStateContext.getCurrentState(),
is(InstanceState.CIRCUIT_BREAK));
- instanceStateContext.switchToInvalidState(InstanceState.CIRCUIT_BREAK);
}
@Test
- void assertSwitchStateWithCircuitBreakOff() {
- instanceStateContext.switchToInvalidState(InstanceState.CIRCUIT_BREAK);
- assertThat(instanceStateContext.getCurrentState(),
is(InstanceState.OK));
- }
-
- @Test
- void assertSwitchStateWithMultiState() {
- instanceStateContext.switchToValidState(InstanceState.CIRCUIT_BREAK);
- instanceStateContext.switchToValidState(InstanceState.LOCK);
- assertThat(instanceStateContext.getCurrentState(),
is(InstanceState.LOCK));
- instanceStateContext.switchToInvalidState(InstanceState.LOCK);
+ void assertRecoverState() {
+ instanceStateContext.switchState(InstanceState.CIRCUIT_BREAK);
assertThat(instanceStateContext.getCurrentState(),
is(InstanceState.CIRCUIT_BREAK));
+ instanceStateContext.recoverState(InstanceState.CIRCUIT_BREAK);
+ assertThat(instanceStateContext.getCurrentState(),
is(InstanceState.OK));
}
}
diff --git
a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/state/impl/LockProxyStateTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/state/instance/InstanceStateTest.java
similarity index 66%
rename from
proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/state/impl/LockProxyStateTest.java
rename to
infra/common/src/test/java/org/apache/shardingsphere/infra/state/instance/InstanceStateTest.java
index 7eddae246b2..46a2543c17d 100644
---
a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/state/impl/LockProxyStateTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/state/instance/InstanceStateTest.java
@@ -15,17 +15,19 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.frontend.state.impl;
+package org.apache.shardingsphere.infra.state.instance;
-import
org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
-class LockProxyStateTest {
+class InstanceStateTest {
@Test
- void assertExecute() {
- assertThrows(UnsupportedSQLOperationException.class, () -> new
LockProxyState().execute(null, null, null, null));
+ void assertGetInstanceState() {
+ assertTrue(InstanceState.get("OK").isPresent());
+ assertTrue(InstanceState.get("CIRCUIT_BREAK").isPresent());
+ assertFalse(InstanceState.get("TEST_STATE").isPresent());
}
}
diff --git
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/service/ComputeNodeStatusService.java
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/service/ComputeNodeStatusService.java
index b7bdfb18e83..103a51126e8 100644
---
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/service/ComputeNodeStatusService.java
+++
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/compute/service/ComputeNodeStatusService.java
@@ -27,6 +27,7 @@ import
org.apache.shardingsphere.infra.instance.metadata.InstanceMetaDataFactory
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
import org.apache.shardingsphere.infra.instance.yaml.YamlComputeNodeData;
import
org.apache.shardingsphere.infra.instance.yaml.YamlComputeNodeDataSwapper;
+import org.apache.shardingsphere.infra.state.instance.InstanceState;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.metadata.persist.node.ComputeNode;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
@@ -151,7 +152,7 @@ public final class ComputeNodeStatusService {
public ComputeNodeInstance loadComputeNodeInstance(final InstanceMetaData
instanceMetaData) {
ComputeNodeInstance result = new ComputeNodeInstance(instanceMetaData);
result.getLabels().addAll(loadInstanceLabels(instanceMetaData.getId()));
- result.switchState(loadInstanceStatus(instanceMetaData.getId()));
+
InstanceState.get(loadInstanceStatus(instanceMetaData.getId())).ifPresent(result::switchState);
loadInstanceWorkerId(instanceMetaData.getId()).ifPresent(result::setWorkerId);
return result;
}
diff --git
a/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/state/ProxyStateContext.java
b/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/state/ProxyStateContext.java
index c0470972b10..e35ebfb5eab 100644
---
a/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/state/ProxyStateContext.java
+++
b/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/state/ProxyStateContext.java
@@ -25,7 +25,6 @@ import
org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import
org.apache.shardingsphere.proxy.frontend.spi.DatabaseProtocolFrontendEngine;
import
org.apache.shardingsphere.proxy.frontend.state.impl.CircuitBreakProxyState;
-import org.apache.shardingsphere.proxy.frontend.state.impl.LockProxyState;
import org.apache.shardingsphere.proxy.frontend.state.impl.OKProxyState;
import java.util.Map;
@@ -41,7 +40,6 @@ public final class ProxyStateContext {
static {
STATES.put(InstanceState.OK, new OKProxyState());
- STATES.put(InstanceState.LOCK, new LockProxyState());
STATES.put(InstanceState.CIRCUIT_BREAK, new CircuitBreakProxyState());
}
diff --git
a/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/state/impl/LockProxyState.java
b/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/state/impl/LockProxyState.java
deleted file mode 100644
index 52b35695648..00000000000
---
a/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/state/impl/LockProxyState.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * 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.proxy.frontend.state.impl;
-
-import io.netty.channel.ChannelHandlerContext;
-import
org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
-import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
-import
org.apache.shardingsphere.proxy.frontend.spi.DatabaseProtocolFrontendEngine;
-import org.apache.shardingsphere.proxy.frontend.state.ProxyState;
-
-/**
- * Lock proxy state.
- */
-public final class LockProxyState implements ProxyState {
-
- @Override
- public void execute(final ChannelHandlerContext context, final Object
message, final DatabaseProtocolFrontendEngine databaseProtocolFrontendEngine,
final ConnectionSession connectionSession) {
- throw new UnsupportedSQLOperationException("LockProxyState");
- }
-}