Skip to content

Commit a29e2b1

Browse files
authored
HDDS-14023. Extract utility for resizing thread pool (#9387)
1 parent 2da0696 commit a29e2b1

3 files changed

Lines changed: 30 additions & 34 deletions

File tree

hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/statemachine/commandhandler/DeleteBlocksCommandHandler.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.apache.hadoop.hdds.protocol.proto.StorageContainerDatanodeProtocolProtos.SCMCommandProto;
4747
import org.apache.hadoop.hdds.scm.container.common.helpers.StorageContainerException;
4848
import org.apache.hadoop.hdds.upgrade.HDDSLayoutFeature;
49+
import org.apache.hadoop.hdds.utils.HddsServerUtil;
4950
import org.apache.hadoop.hdds.utils.db.BatchOperation;
5051
import org.apache.hadoop.hdds.utils.db.Table;
5152
import org.apache.hadoop.metrics2.lib.MetricsRegistry;
@@ -763,22 +764,6 @@ public ThreadPoolExecutor getExecutor() {
763764
}
764765

765766
public void setPoolSize(int size) {
766-
if (size <= 0) {
767-
throw new IllegalArgumentException("Pool size must be positive.");
768-
}
769-
770-
int currentCorePoolSize = executor.getCorePoolSize();
771-
772-
// In ThreadPoolExecutor, maximumPoolSize must always be greater than or
773-
// equal to the corePoolSize. We must make sure this invariant holds when
774-
// changing the pool size. Therefore, we take into account whether the
775-
// new size is greater or smaller than the current core pool size.
776-
if (size > currentCorePoolSize) {
777-
executor.setMaximumPoolSize(size);
778-
executor.setCorePoolSize(size);
779-
} else {
780-
executor.setCorePoolSize(size);
781-
executor.setMaximumPoolSize(size);
782-
}
767+
HddsServerUtil.setPoolSize(executor, size, LOG);
783768
}
784769
}

hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/replication/ReplicationServer.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.hadoop.hdds.security.SecurityConfig;
3636
import org.apache.hadoop.hdds.security.x509.certificate.client.CertificateClient;
3737
import org.apache.hadoop.hdds.tracing.GrpcServerInterceptor;
38+
import org.apache.hadoop.hdds.utils.HddsServerUtil;
3839
import org.apache.hadoop.ozone.OzoneConsts;
3940
import org.apache.hadoop.ozone.container.ozoneimpl.ContainerController;
4041
import org.apache.ratis.thirdparty.io.grpc.Server;
@@ -153,23 +154,7 @@ public int getPort() {
153154
}
154155

155156
public void setPoolSize(int size) {
156-
if (size <= 0) {
157-
throw new IllegalArgumentException("Pool size must be positive.");
158-
}
159-
160-
int currentCorePoolSize = executor.getCorePoolSize();
161-
162-
// In ThreadPoolExecutor, maximumPoolSize must always be greater than or
163-
// equal to the corePoolSize. We must make sure this invariant holds when
164-
// changing the pool size. Therefore, we take into account whether the
165-
// new size is greater or smaller than the current core pool size.
166-
if (size > currentCorePoolSize) {
167-
executor.setMaximumPoolSize(size);
168-
executor.setCorePoolSize(size);
169-
} else {
170-
executor.setCorePoolSize(size);
171-
executor.setMaximumPoolSize(size);
172-
}
157+
HddsServerUtil.setPoolSize(executor, size, LOG);
173158
}
174159

175160
@VisibleForTesting

hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/HddsServerUtil.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import java.util.Optional;
6363
import java.util.OptionalInt;
6464
import java.util.Set;
65+
import java.util.concurrent.ThreadPoolExecutor;
6566
import java.util.concurrent.TimeUnit;
6667
import java.util.stream.Collectors;
6768
import java.util.stream.Stream;
@@ -104,6 +105,7 @@
104105
import org.apache.hadoop.ozone.OzoneConsts;
105106
import org.apache.hadoop.security.UserGroupInformation;
106107
import org.apache.hadoop.util.ShutdownHookManager;
108+
import org.apache.ratis.util.Preconditions;
107109
import org.slf4j.Logger;
108110
import org.slf4j.LoggerFactory;
109111

@@ -730,4 +732,28 @@ private static String createStartupMessage(VersionInfo versionInfo,
730732
" conf = " + conf);
731733
}
732734

735+
public static void setPoolSize(ThreadPoolExecutor executor, int size, Logger logger) {
736+
Preconditions.assertTrue(size > 0, () -> "Pool size must be positive: " + size);
737+
738+
int currentCorePoolSize = executor.getCorePoolSize();
739+
740+
// In ThreadPoolExecutor, maximumPoolSize must always be greater than or
741+
// equal to the corePoolSize. We must make sure this invariant holds when
742+
// changing the pool size. Therefore, we take into account whether the
743+
// new size is greater or smaller than the current core pool size.
744+
String change = "unchanged";
745+
if (size > currentCorePoolSize) {
746+
change = "increased";
747+
executor.setMaximumPoolSize(size);
748+
executor.setCorePoolSize(size);
749+
} else if (size < currentCorePoolSize) {
750+
change = "decreased";
751+
executor.setCorePoolSize(size);
752+
executor.setMaximumPoolSize(size);
753+
}
754+
if (logger != null) {
755+
logger.info("pool size {} from {} to {}", change, currentCorePoolSize, size);
756+
}
757+
}
758+
733759
}

0 commit comments

Comments
 (0)