This is an automated email from the ASF dual-hosted git repository.
sunnianjun 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 d40ea0a41d5 Refactor WorkerIdAssignedException (#31317)
d40ea0a41d5 is described below
commit d40ea0a41d574947c91b37152f7003862137aa0c
Author: Liang Zhang <[email protected]>
AuthorDate: Mon May 20 23:34:23 2024 +0800
Refactor WorkerIdAssignedException (#31317)
---
.../user-manual/error-code/sql-error-code.cn.md | 2 +-
.../user-manual/error-code/sql-error-code.en.md | 2 +-
.../instance/workerid/WorkerIdAssignedException.java | 10 +++++-----
.../workerid/generator/ClusterWorkerIdGenerator.java | 20 ++++++++++++--------
.../generator/StandaloneWorkerIdGenerator.java | 5 +++--
5 files changed, 22 insertions(+), 17 deletions(-)
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 0a06fc1bcf1..408bd36cb14 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 7735b29c147..32b027a75d8 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 2f1142f5aec..7f92f0abf41 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 ee21caf4812..1610bec3602 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.exception.core.ShardingSpherePrecondition
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 final class ClusterWorkerIdGenerator implements
WorkerIdGenerator {
@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<Integer> loadExistedWorkerId() {
+ return
computeNodeStatusService.loadInstanceWorkerId(instanceMetaData.getId());
+ }
+
+ private int generateNewWorkerId() {
Optional<Integer> generatedWorkId;
do {
generatedWorkId = generateAvailableWorkerId();
@@ -74,7 +78,7 @@ public final class ClusterWorkerIdGenerator implements
WorkerIdGenerator {
private Optional<Integer> generateAvailableWorkerId() {
Collection<Integer> assignedWorkerIds =
computeNodeStatusService.getAssignedWorkerIds();
- ShardingSpherePreconditions.checkState(assignedWorkerIds.size() <=
1024, WorkIdAssignedException::new);
+ ShardingSpherePreconditions.checkState(assignedWorkerIds.size() <=
MAX_WORKER_ID + 1, WorkerIdAssignedException::new);
Collection<Integer> availableWorkerIds = new LinkedList<>();
for (int i = 0; i < 1024; i++) {
availableWorkerIds.add(i);
@@ -93,10 +97,10 @@ public final class ClusterWorkerIdGenerator implements
WorkerIdGenerator {
}
}
- 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 8af9297454a..fd3948a315b 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 final class StandaloneWorkerIdGenerator implements
WorkerIdGenerator {
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;
}
}