Skip to content

Commit

Permalink
Fix sonar issue for JdbcUrlAppender (#33265)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
terrymanu authored Oct 15, 2024
1 parent 550b90b commit e255a2b
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,17 @@

package org.apache.shardingsphere.infra.instance;

import com.google.common.base.Preconditions;
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
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 javax.annotation.concurrent.ThreadSafe;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.CopyOnWriteArrayList;
Expand All @@ -51,17 +47,15 @@ public final class ComputeNodeInstanceContext {

private final ModeConfiguration modeConfiguration;

@SuppressWarnings("rawtypes")
@Getter(AccessLevel.NONE)
private final AtomicReference<LockContext> lockContext = new AtomicReference<>();
private final AtomicReference<LockContext<?>> lockContext = new AtomicReference<>();

private final EventBusContext eventBusContext;

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

@SuppressWarnings("rawtypes")
public ComputeNodeInstanceContext(final ComputeNodeInstance instance, final WorkerIdGenerator workerIdGenerator,
final ModeConfiguration modeConfig, final LockContext lockContext, final EventBusContext eventBusContext) {
final ModeConfiguration modeConfig, final LockContext<?> lockContext, final EventBusContext eventBusContext) {
this.instance = instance;
this.workerIdGenerator.set(workerIdGenerator);
this.modeConfiguration = modeConfig;
Expand All @@ -79,86 +73,76 @@ public ComputeNodeInstanceContext(final ComputeNodeInstance instance, final Mode
* @param workerIdGenerator worker id generator
* @param lockContext lock context
*/
@SuppressWarnings("rawtypes")
public void init(final WorkerIdGenerator workerIdGenerator, final LockContext lockContext) {
public void init(final WorkerIdGenerator workerIdGenerator, final LockContext<?> lockContext) {
this.workerIdGenerator.set(workerIdGenerator);
this.lockContext.set(lockContext);
}

/**
* Update instance status.
*
* @param id instance ID
* @param instanceId instance ID
* @param status status
*/
public void updateStatus(final String id, final String status) {
public void updateStatus(final String instanceId, final String status) {
Optional<InstanceState> instanceState = InstanceState.get(status);
if (!instanceState.isPresent()) {
return;
}
if (instance.getMetaData().getId().equals(id)) {
if (instance.getMetaData().getId().equals(instanceId)) {
instance.switchState(instanceState.get());
}
updateRelatedComputeNodeInstancesStatus(id, instanceState.get());
}

private void updateRelatedComputeNodeInstancesStatus(final String instanceId, final InstanceState instanceState) {
for (ComputeNodeInstance each : allClusterInstances) {
if (each.getMetaData().getId().equals(instanceId)) {
each.switchState(instanceState);
}
}
allClusterInstances.stream().filter(each -> each.getMetaData().getId().equals(instanceId)).forEach(each -> each.switchState(instanceState.get()));
}

/**
* Update instance worker id.
* Update instance labels.
*
* @param instanceId instance id
* @param workerId worker id
* @param instanceId instance ID
* @param labels labels
*/
public void updateWorkerId(final String instanceId, final Integer workerId) {
public void updateLabels(final String instanceId, final Collection<String> labels) {
if (instance.getMetaData().getId().equals(instanceId)) {
instance.setWorkerId(workerId);
updateLabels(instance, labels);
}
allClusterInstances.stream().filter(each -> each.getMetaData().getId().equals(instanceId)).forEach(each -> each.setWorkerId(workerId));
allClusterInstances.stream().filter(each -> each.getMetaData().getId().equals(instanceId)).forEach(each -> updateLabels(each, labels));
}

private void updateLabels(final ComputeNodeInstance computeNodeInstance, final Collection<String> labels) {
computeNodeInstance.getLabels().clear();
computeNodeInstance.getLabels().addAll(labels);
}

/**
* Update instance label.
* Update instance worker ID.
*
* @param instanceId instance id
* @param labels collection of label
* @param instanceId instance ID
* @param workerId worker ID
*/
public void updateLabel(final String instanceId, final Collection<String> labels) {
public void updateWorkerId(final String instanceId, final Integer workerId) {
if (instance.getMetaData().getId().equals(instanceId)) {
instance.getLabels().clear();
instance.getLabels().addAll(labels);
}
for (ComputeNodeInstance each : allClusterInstances) {
if (each.getMetaData().getId().equals(instanceId)) {
each.getLabels().clear();
each.getLabels().addAll(labels);
}
instance.setWorkerId(workerId);
}
allClusterInstances.stream().filter(each -> each.getMetaData().getId().equals(instanceId)).forEach(each -> each.setWorkerId(workerId));
}

/**
* Get worker id.
* Get worker ID.
*
* @return worker id
* @return worker ID
*/
public int getWorkerId() {
return instance.getWorkerId();
}

/**
* Generate worker id.
* Generate worker ID.
*
* @param props properties
* @return worker id
* @return worker ID
*/
public int generateWorkerId(final Properties props) {
Preconditions.checkArgument(workerIdGenerator.get() != null, "Worker id generator is not initialized.");
ShardingSpherePreconditions.checkNotNull(workerIdGenerator.get(), () -> new IllegalArgumentException("Worker id generator is not initialized."));
int result = workerIdGenerator.get().generate(props);
instance.setWorkerId(result);
return result;
Expand All @@ -184,26 +168,9 @@ public void deleteComputeNodeInstance(final ComputeNodeInstance instance) {
}

/**
* Get compute node instances by instance type and labels.
*
* @param instanceType instance type
* @param labels collection of contained label
* @return compute node instances
*/
public Map<String, InstanceMetaData> getAllClusterInstances(final InstanceType instanceType, final Collection<String> labels) {
Map<String, InstanceMetaData> result = new LinkedHashMap<>(allClusterInstances.size(), 1F);
for (ComputeNodeInstance each : allClusterInstances) {
if (each.getMetaData().getType() == instanceType && labels.stream().anyMatch(each.getLabels()::contains)) {
result.put(each.getMetaData().getId(), each.getMetaData());
}
}
return result;
}

/**
* Get compute node instance by instance id.
* Get compute node instance.
*
* @param instanceId instance id
* @param instanceId instance ID
* @return compute node instance
*/
public Optional<ComputeNodeInstance> getComputeNodeInstanceById(final String instanceId) {
Expand All @@ -216,8 +183,7 @@ public Optional<ComputeNodeInstance> getComputeNodeInstanceById(final String ins
* @return lock context
* @throws IllegalStateException if lock context is not initialized
*/
@SuppressWarnings("rawtypes")
public LockContext getLockContext() throws IllegalStateException {
public LockContext<?> getLockContext() throws IllegalStateException {
return Optional.ofNullable(lockContext.get()).orElseThrow(() -> new IllegalStateException("Lock context is not initialized."));
}
}
Loading

0 comments on commit e255a2b

Please sign in to comment.