From d40ea0a41d574947c91b37152f7003862137aa0c Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Mon, 20 May 2024 23:34:23 +0800 Subject: [PATCH] Refactor WorkerIdAssignedException (#31317) --- .../error-code/sql-error-code.cn.md | 2 +- .../error-code/sql-error-code.en.md | 2 +- .../workerid/WorkerIdAssignedException.java | 10 +++++----- .../generator/ClusterWorkerIdGenerator.java | 20 +++++++++++-------- .../StandaloneWorkerIdGenerator.java | 5 +++-- 5 files changed, 22 insertions(+), 17 deletions(-) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/exception/WorkIdAssignedException.java => infra/common/src/main/java/org/apache/shardingsphere/infra/instance/workerid/WorkerIdAssignedException.java (75%) diff --git a/docs/document/content/user-manual/error-code/sql-error-code.cn.md b/docs/document/content/user-manual/error-code/sql-error-code.cn.md index 0a06fc1bcf14f..408bd36cb141f 100644 --- a/docs/document/content/user-manual/error-code/sql-error-code.cn.md +++ b/docs/document/content/user-manual/error-code/sql-error-code.cn.md @@ -103,7 +103,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供, | Vendor Code | SQL State | 错误信息 | |-------------|-----------|---------------------------------------------------------------| | 17000 | 44000 | Mode must be 'cluster'. | -| 17001 | HY000 | Work ID assigned failed, which can not exceed 1024. | +| 17001 | HY000 | Worker ID assigned failed, which should be in [0, %s). | | 17010 | HY000 | Cluster persist repository error, reason is: %s | | 17020 | HY000 | The cluster status is %s, can not support SQL statement '%s'. | | 17030 | HY000 | Cluster is already locked. | diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md b/docs/document/content/user-manual/error-code/sql-error-code.en.md index 7735b29c1472b..32b027a75d8cd 100644 --- a/docs/document/content/user-manual/error-code/sql-error-code.en.md +++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md @@ -103,7 +103,7 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi | Vendor Code | SQL State | Reason | |-------------|-----------|---------------------------------------------------------------| | 17000 | 44000 | Mode must be 'cluster'. | -| 17001 | HY000 | Work ID assigned failed, which can not exceed 1024. | +| 17001 | HY000 | Worker ID assigned failed, which should be in [0, %s). | | 17010 | HY000 | Cluster persist repository error, reason is: %s | | 17020 | HY000 | The cluster status is %s, can not support SQL statement '%s'. | | 17030 | HY000 | Cluster is already locked. | diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/exception/WorkIdAssignedException.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/workerid/WorkerIdAssignedException.java similarity index 75% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/exception/WorkIdAssignedException.java rename to infra/common/src/main/java/org/apache/shardingsphere/infra/instance/workerid/WorkerIdAssignedException.java index 2f1142f5aecdc..7f92f0abf4116 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/exception/WorkIdAssignedException.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/workerid/WorkerIdAssignedException.java @@ -15,19 +15,19 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.workerid.exception; +package org.apache.shardingsphere.infra.instance.workerid; import org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState; import org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.ClusterSQLException; /** - * Work id assigned exception. + * Worker ID assigned exception. */ -public final class WorkIdAssignedException extends ClusterSQLException { +public final class WorkerIdAssignedException extends ClusterSQLException { private static final long serialVersionUID = 4782736481041926266L; - public WorkIdAssignedException() { - super(XOpenSQLState.GENERAL_ERROR, 1, "Work ID assigned failed, which can not exceed 1024."); + public WorkerIdAssignedException() { + super(XOpenSQLState.GENERAL_ERROR, 1, "Worker ID assigned failed, which should be in [0, %s).", WorkerIdGenerator.MAX_WORKER_ID + 1); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/generator/ClusterWorkerIdGenerator.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/generator/ClusterWorkerIdGenerator.java index ee21caf48126d..1610bec360218 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/generator/ClusterWorkerIdGenerator.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/workerid/generator/ClusterWorkerIdGenerator.java @@ -23,7 +23,7 @@ import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; import org.apache.shardingsphere.infra.instance.workerid.WorkerIdGenerator; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service.ComputeNodeStatusService; -import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.workerid.exception.WorkIdAssignedException; +import org.apache.shardingsphere.infra.instance.workerid.WorkerIdAssignedException; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.workerid.node.WorkerIdNode; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; import org.apache.shardingsphere.mode.repository.cluster.exception.ClusterPersistRepositoryException; @@ -57,12 +57,16 @@ public ClusterWorkerIdGenerator(final ClusterPersistRepository repository, final @Override public int generate(final Properties props) { - int result = computeNodeStatusService.loadInstanceWorkerId(instanceMetaData.getId()).orElseGet(this::reGenerate); - checkIneffectiveConfiguration(result, props); + int result = loadExistedWorkerId().orElseGet(this::generateNewWorkerId); + logWarning(result, props); return result; } - private int reGenerate() { + private Optional loadExistedWorkerId() { + return computeNodeStatusService.loadInstanceWorkerId(instanceMetaData.getId()); + } + + private int generateNewWorkerId() { Optional generatedWorkId; do { generatedWorkId = generateAvailableWorkerId(); @@ -74,7 +78,7 @@ private int reGenerate() { private Optional generateAvailableWorkerId() { Collection assignedWorkerIds = computeNodeStatusService.getAssignedWorkerIds(); - ShardingSpherePreconditions.checkState(assignedWorkerIds.size() <= 1024, WorkIdAssignedException::new); + ShardingSpherePreconditions.checkState(assignedWorkerIds.size() <= MAX_WORKER_ID + 1, WorkerIdAssignedException::new); Collection availableWorkerIds = new LinkedList<>(); for (int i = 0; i < 1024; i++) { availableWorkerIds.add(i); @@ -93,10 +97,10 @@ private Optional generateAvailableWorkerId() { } } - private void checkIneffectiveConfiguration(final int generatedWorkerId, final Properties props) { - if (!isWarned.get() && null != props && props.containsKey(WORKER_ID_KEY)) { + private void logWarning(final int generatedWorkerId, final Properties props) { + if (!isWarned.get() && props.containsKey(WORKER_ID_KEY)) { isWarned.set(true); - log.warn("No need to configured {} in cluster mode, system assigned {} was {}", WORKER_ID_KEY, WORKER_ID_KEY, generatedWorkerId); + log.warn("It is unnecessary to configure {} in cluster mode, system assigned {} was {}", WORKER_ID_KEY, WORKER_ID_KEY, generatedWorkerId); } } } diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/workerid/generator/StandaloneWorkerIdGenerator.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/workerid/generator/StandaloneWorkerIdGenerator.java index 8af9297454a67..fd3948a315b6e 100644 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/workerid/generator/StandaloneWorkerIdGenerator.java +++ b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/workerid/generator/StandaloneWorkerIdGenerator.java @@ -17,7 +17,8 @@ package org.apache.shardingsphere.mode.manager.standalone.workerid.generator; -import com.google.common.base.Preconditions; +import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.instance.workerid.WorkerIdAssignedException; import org.apache.shardingsphere.infra.instance.workerid.WorkerIdGenerator; import java.util.Properties; @@ -35,7 +36,7 @@ public int generate(final Properties props) { return DEFAULT_WORKER_ID; } int result = Integer.parseInt(props.get(WORKER_ID_KEY).toString()); - Preconditions.checkState(result <= MAX_WORKER_ID, "%s can not exceed %s", WORKER_ID_KEY, MAX_WORKER_ID); + ShardingSpherePreconditions.checkState(result >= 0 && result <= MAX_WORKER_ID, WorkerIdAssignedException::new); return result; } }