Skip to content

Commit e255a2b

Browse files
authored
Fix sonar issue for JdbcUrlAppender (#33265)
* Fix sonar issue for JdbcUrlAppender * Refactor ComputeNodeInstanceContextTest * Refactor ComputeNodeInstanceContextTest * Refactor ComputeNodeInstanceContext * Add more test cases on ComputeNodeInstanceContext * Add more test cases on ComputeNodeInstanceContext * Add more test cases on ComputeNodeInstanceContext * Add more test cases on ComputeNodeInstanceContext * Add more test cases on ComputeNodeInstanceContext * Add more test cases on ComputeNodeInstanceContext * Add more test cases on ComputeNodeInstanceContext * Add more test cases on ComputeNodeInstanceContext * Add more test cases on ComputeNodeInstanceContext * Add more test cases on ComputeNodeInstanceContext * Add more test cases on ComputeNodeInstanceContext
1 parent 550b90b commit e255a2b

File tree

6 files changed

+127
-121
lines changed

6 files changed

+127
-121
lines changed

infra/common/src/main/java/org/apache/shardingsphere/infra/instance/ComputeNodeInstanceContext.java

Lines changed: 33 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,17 @@
1717

1818
package org.apache.shardingsphere.infra.instance;
1919

20-
import com.google.common.base.Preconditions;
2120
import lombok.AccessLevel;
2221
import lombok.Getter;
2322
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
24-
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
25-
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
23+
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
2624
import org.apache.shardingsphere.infra.instance.workerid.WorkerIdGenerator;
2725
import org.apache.shardingsphere.infra.lock.LockContext;
2826
import org.apache.shardingsphere.infra.state.instance.InstanceState;
2927
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
3028

3129
import javax.annotation.concurrent.ThreadSafe;
3230
import java.util.Collection;
33-
import java.util.LinkedHashMap;
34-
import java.util.Map;
3531
import java.util.Optional;
3632
import java.util.Properties;
3733
import java.util.concurrent.CopyOnWriteArrayList;
@@ -51,17 +47,15 @@ public final class ComputeNodeInstanceContext {
5147

5248
private final ModeConfiguration modeConfiguration;
5349

54-
@SuppressWarnings("rawtypes")
5550
@Getter(AccessLevel.NONE)
56-
private final AtomicReference<LockContext> lockContext = new AtomicReference<>();
51+
private final AtomicReference<LockContext<?>> lockContext = new AtomicReference<>();
5752

5853
private final EventBusContext eventBusContext;
5954

6055
private final Collection<ComputeNodeInstance> allClusterInstances = new CopyOnWriteArrayList<>();
6156

62-
@SuppressWarnings("rawtypes")
6357
public ComputeNodeInstanceContext(final ComputeNodeInstance instance, final WorkerIdGenerator workerIdGenerator,
64-
final ModeConfiguration modeConfig, final LockContext lockContext, final EventBusContext eventBusContext) {
58+
final ModeConfiguration modeConfig, final LockContext<?> lockContext, final EventBusContext eventBusContext) {
6559
this.instance = instance;
6660
this.workerIdGenerator.set(workerIdGenerator);
6761
this.modeConfiguration = modeConfig;
@@ -79,86 +73,76 @@ public ComputeNodeInstanceContext(final ComputeNodeInstance instance, final Mode
7973
* @param workerIdGenerator worker id generator
8074
* @param lockContext lock context
8175
*/
82-
@SuppressWarnings("rawtypes")
83-
public void init(final WorkerIdGenerator workerIdGenerator, final LockContext lockContext) {
76+
public void init(final WorkerIdGenerator workerIdGenerator, final LockContext<?> lockContext) {
8477
this.workerIdGenerator.set(workerIdGenerator);
8578
this.lockContext.set(lockContext);
8679
}
8780

8881
/**
8982
* Update instance status.
9083
*
91-
* @param id instance ID
84+
* @param instanceId instance ID
9285
* @param status status
9386
*/
94-
public void updateStatus(final String id, final String status) {
87+
public void updateStatus(final String instanceId, final String status) {
9588
Optional<InstanceState> instanceState = InstanceState.get(status);
9689
if (!instanceState.isPresent()) {
9790
return;
9891
}
99-
if (instance.getMetaData().getId().equals(id)) {
92+
if (instance.getMetaData().getId().equals(instanceId)) {
10093
instance.switchState(instanceState.get());
10194
}
102-
updateRelatedComputeNodeInstancesStatus(id, instanceState.get());
103-
}
104-
105-
private void updateRelatedComputeNodeInstancesStatus(final String instanceId, final InstanceState instanceState) {
106-
for (ComputeNodeInstance each : allClusterInstances) {
107-
if (each.getMetaData().getId().equals(instanceId)) {
108-
each.switchState(instanceState);
109-
}
110-
}
95+
allClusterInstances.stream().filter(each -> each.getMetaData().getId().equals(instanceId)).forEach(each -> each.switchState(instanceState.get()));
11196
}
11297

11398
/**
114-
* Update instance worker id.
99+
* Update instance labels.
115100
*
116-
* @param instanceId instance id
117-
* @param workerId worker id
101+
* @param instanceId instance ID
102+
* @param labels labels
118103
*/
119-
public void updateWorkerId(final String instanceId, final Integer workerId) {
104+
public void updateLabels(final String instanceId, final Collection<String> labels) {
120105
if (instance.getMetaData().getId().equals(instanceId)) {
121-
instance.setWorkerId(workerId);
106+
updateLabels(instance, labels);
122107
}
123-
allClusterInstances.stream().filter(each -> each.getMetaData().getId().equals(instanceId)).forEach(each -> each.setWorkerId(workerId));
108+
allClusterInstances.stream().filter(each -> each.getMetaData().getId().equals(instanceId)).forEach(each -> updateLabels(each, labels));
109+
}
110+
111+
private void updateLabels(final ComputeNodeInstance computeNodeInstance, final Collection<String> labels) {
112+
computeNodeInstance.getLabels().clear();
113+
computeNodeInstance.getLabels().addAll(labels);
124114
}
125115

126116
/**
127-
* Update instance label.
117+
* Update instance worker ID.
128118
*
129-
* @param instanceId instance id
130-
* @param labels collection of label
119+
* @param instanceId instance ID
120+
* @param workerId worker ID
131121
*/
132-
public void updateLabel(final String instanceId, final Collection<String> labels) {
122+
public void updateWorkerId(final String instanceId, final Integer workerId) {
133123
if (instance.getMetaData().getId().equals(instanceId)) {
134-
instance.getLabels().clear();
135-
instance.getLabels().addAll(labels);
136-
}
137-
for (ComputeNodeInstance each : allClusterInstances) {
138-
if (each.getMetaData().getId().equals(instanceId)) {
139-
each.getLabels().clear();
140-
each.getLabels().addAll(labels);
141-
}
124+
instance.setWorkerId(workerId);
142125
}
126+
allClusterInstances.stream().filter(each -> each.getMetaData().getId().equals(instanceId)).forEach(each -> each.setWorkerId(workerId));
143127
}
144128

145129
/**
146-
* Get worker id.
130+
* Get worker ID.
147131
*
148-
* @return worker id
132+
* @return worker ID
149133
*/
150134
public int getWorkerId() {
151135
return instance.getWorkerId();
152136
}
153137

154138
/**
155-
* Generate worker id.
139+
* Generate worker ID.
156140
*
157141
* @param props properties
158-
* @return worker id
142+
* @return worker ID
159143
*/
160144
public int generateWorkerId(final Properties props) {
161-
Preconditions.checkArgument(workerIdGenerator.get() != null, "Worker id generator is not initialized.");
145+
ShardingSpherePreconditions.checkNotNull(workerIdGenerator.get(), () -> new IllegalArgumentException("Worker id generator is not initialized."));
162146
int result = workerIdGenerator.get().generate(props);
163147
instance.setWorkerId(result);
164148
return result;
@@ -184,26 +168,9 @@ public void deleteComputeNodeInstance(final ComputeNodeInstance instance) {
184168
}
185169

186170
/**
187-
* Get compute node instances by instance type and labels.
188-
*
189-
* @param instanceType instance type
190-
* @param labels collection of contained label
191-
* @return compute node instances
192-
*/
193-
public Map<String, InstanceMetaData> getAllClusterInstances(final InstanceType instanceType, final Collection<String> labels) {
194-
Map<String, InstanceMetaData> result = new LinkedHashMap<>(allClusterInstances.size(), 1F);
195-
for (ComputeNodeInstance each : allClusterInstances) {
196-
if (each.getMetaData().getType() == instanceType && labels.stream().anyMatch(each.getLabels()::contains)) {
197-
result.put(each.getMetaData().getId(), each.getMetaData());
198-
}
199-
}
200-
return result;
201-
}
202-
203-
/**
204-
* Get compute node instance by instance id.
171+
* Get compute node instance.
205172
*
206-
* @param instanceId instance id
173+
* @param instanceId instance ID
207174
* @return compute node instance
208175
*/
209176
public Optional<ComputeNodeInstance> getComputeNodeInstanceById(final String instanceId) {
@@ -216,8 +183,7 @@ public Optional<ComputeNodeInstance> getComputeNodeInstanceById(final String ins
216183
* @return lock context
217184
* @throws IllegalStateException if lock context is not initialized
218185
*/
219-
@SuppressWarnings("rawtypes")
220-
public LockContext getLockContext() throws IllegalStateException {
186+
public LockContext<?> getLockContext() throws IllegalStateException {
221187
return Optional.ofNullable(lockContext.get()).orElseThrow(() -> new IllegalStateException("Lock context is not initialized."));
222188
}
223189
}

0 commit comments

Comments
 (0)