This is an automated email from the ASF dual-hosted git repository.
szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git
The following commit(s) were added to refs/heads/master by this push:
new c760804071 HDDS-11083. Avoid duplicate creation of
RunningDatanodeState (#6886)
c760804071 is described below
commit c7608040718ed666834dcc0a172b8365877cdceb
Author: jianghuazhu <[email protected]>
AuthorDate: Thu Jul 25 16:17:48 2024 +0800
HDDS-11083. Avoid duplicate creation of RunningDatanodeState (#6886)
---
.../common/statemachine/StateContext.java | 18 ++++-
.../container/common/states/DatanodeState.java | 6 ++
.../states/datanode/RunningDatanodeState.java | 90 +++++++---------------
3 files changed, 49 insertions(+), 65 deletions(-)
diff --git
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/statemachine/StateContext.java
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/statemachine/StateContext.java
index 712b3f0f23..93a4590597 100644
---
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/statemachine/StateContext.java
+++
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/statemachine/StateContext.java
@@ -152,6 +152,8 @@ public class StateContext {
private final String threadNamePrefix;
+ private RunningDatanodeState runningDatanodeState;
+
/**
* Constructs a StateContext.
*
@@ -612,9 +614,11 @@ public class StateContext {
parentDatanodeStateMachine.getConnectionManager(),
this);
case RUNNING:
- return new RunningDatanodeState(this.conf,
- parentDatanodeStateMachine.getConnectionManager(),
- this);
+ if (runningDatanodeState == null) {
+ runningDatanodeState = new RunningDatanodeState(this.conf,
+ parentDatanodeStateMachine.getConnectionManager(), this);
+ }
+ return runningDatanodeState;
case SHUTDOWN:
return null;
default:
@@ -654,7 +658,11 @@ public class StateContext {
// Adding not null check, in a case where datanode is still starting up,
but
// we called stop DatanodeStateMachine, this sets state to SHUTDOWN, and
// there is a chance of getting task as null.
- if (task != null) {
+ if (task == null) {
+ return;
+ }
+
+ try {
if (this.isEntering()) {
task.onEnter();
}
@@ -691,6 +699,8 @@ public class StateContext {
// that we can terminate the datanode.
setShutdownOnError();
}
+ } finally {
+ task.clear();
}
}
diff --git
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/states/DatanodeState.java
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/states/DatanodeState.java
index 25be207dcd..f057a852bc 100644
---
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/states/DatanodeState.java
+++
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/states/DatanodeState.java
@@ -55,4 +55,10 @@ public interface DatanodeState<T> {
T await(long time, TimeUnit timeUnit)
throws InterruptedException, ExecutionException, TimeoutException;
+ /**
+ * Clean up some resources.
+ */
+ @SuppressWarnings("checkstyle:WhitespaceAround")
+ default void clear(){}
+
}
diff --git
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/states/datanode/RunningDatanodeState.java
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/states/datanode/RunningDatanodeState.java
index 4e5b64c27e..c4d507668d 100644
---
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/states/datanode/RunningDatanodeState.java
+++
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/states/datanode/RunningDatanodeState.java
@@ -31,11 +31,8 @@ import org.apache.hadoop.util.Time;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.EnumMap;
-import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
-import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
@@ -54,9 +51,6 @@ public class RunningDatanodeState implements DatanodeState {
private final ConfigurationSource conf;
private final StateContext context;
private CompletionService<EndPointStates> ecs;
- /** Cache the end point task per end point per end point state. */
- private Map<EndpointStateMachine, Map<EndPointStates,
- Callable<EndPointStates>>> endpointTasks;
public RunningDatanodeState(ConfigurationSource conf,
SCMConnectionManager connectionManager,
@@ -64,55 +58,6 @@ public class RunningDatanodeState implements DatanodeState {
this.connectionManager = connectionManager;
this.conf = conf;
this.context = context;
- initEndPointTask();
- }
-
- /**
- * Initialize end point tasks corresponding to each end point,
- * each end point state.
- */
- private void initEndPointTask() {
- endpointTasks = new HashMap<>();
- for (EndpointStateMachine endpoint : connectionManager.getValues()) {
- EnumMap<EndPointStates, Callable<EndPointStates>> endpointTaskForState =
- new EnumMap<>(EndPointStates.class);
-
- for (EndPointStates state : EndPointStates.values()) {
- Callable<EndPointStates> endPointTask = null;
- switch (state) {
- case GETVERSION:
- endPointTask = new VersionEndpointTask(endpoint, conf,
- context.getParent().getContainer());
- break;
- case REGISTER:
- endPointTask = RegisterEndpointTask.newBuilder()
- .setConfig(conf)
- .setEndpointStateMachine(endpoint)
- .setContext(context)
- .setDatanodeDetails(context.getParent().getDatanodeDetails())
- .setOzoneContainer(context.getParent().getContainer())
- .build();
- break;
- case HEARTBEAT:
- endPointTask = HeartbeatEndpointTask.newBuilder()
- .setConfig(conf)
- .setEndpointStateMachine(endpoint)
- .setDatanodeDetails(context
- .getParent()
- .getDatanodeDetails())
- .setContext(context)
- .build();
- break;
- default:
- break;
- }
-
- if (endPointTask != null) {
- endpointTaskForState.put(state, endPointTask);
- }
- }
- endpointTasks.put(endpoint, endpointTaskForState);
- }
}
/**
@@ -140,7 +85,7 @@ public class RunningDatanodeState implements DatanodeState {
public void execute(ExecutorService executor) {
ecs = new ExecutorCompletionService<>(executor);
for (EndpointStateMachine endpoint : connectionManager.getValues()) {
- Callable<EndPointStates> endpointTask = getEndPointTask(endpoint);
+ Callable<EndPointStates> endpointTask = buildEndPointTask(endpoint);
if (endpointTask != null) {
// Just do a timely wait. A slow EndpointStateMachine won't occupy
// the thread in executor from DatanodeStateMachine for a long time,
@@ -171,12 +116,30 @@ public class RunningDatanodeState implements
DatanodeState {
this.ecs = e;
}
- private Callable<EndPointStates> getEndPointTask(
+ @SuppressWarnings("checkstyle:Indentation")
+ private Callable<EndPointStates> buildEndPointTask(
EndpointStateMachine endpoint) {
- if (endpointTasks.containsKey(endpoint)) {
- return endpointTasks.get(endpoint).get(endpoint.getState());
- } else {
- throw new IllegalArgumentException("Illegal endpoint: " + endpoint);
+ switch (endpoint.getState()) {
+ case GETVERSION:
+ return new VersionEndpointTask(endpoint, conf,
+ context.getParent().getContainer());
+ case REGISTER:
+ return RegisterEndpointTask.newBuilder()
+ .setConfig(conf)
+ .setEndpointStateMachine(endpoint)
+ .setContext(context)
+ .setDatanodeDetails(context.getParent().getDatanodeDetails())
+ .setOzoneContainer(context.getParent().getContainer())
+ .build();
+ case HEARTBEAT:
+ return HeartbeatEndpointTask.newBuilder()
+ .setConfig(conf)
+ .setEndpointStateMachine(endpoint)
+ .setDatanodeDetails(context.getParent().getDatanodeDetails())
+ .setContext(context)
+ .build();
+ default:
+ return null;
}
}
@@ -238,4 +201,9 @@ public class RunningDatanodeState implements DatanodeState {
}
return computeNextContainerState(results);
}
+
+ @Override
+ public void clear() {
+ ecs = null;
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]