Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 5787023

Browse files
committed
FABJ-378 Make excutor service configurable
Change-Id: Iff50ab585cf27b94b0e3e212e09bebb484471b16 Signed-off-by: rickr <cr22rc@gmail.com>
1 parent ce7fd3b commit 5787023

File tree

3 files changed

+100
-12
lines changed

3 files changed

+100
-12
lines changed

config.properties

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@
3737
#org.hyperledger.fabric.sdk.connections.ssl.sslProvider=openSSL
3838
## Default negotiation type for grpc ssl connections. (TLS, plainText)
3939
#org.hyperledger.fabric.sdk.connections.ssl.negotiationType=TLS
40+
## the number of threads to keep in the pool, even if they are idle, unless {@code allowCoreThreadTimeOut} is set
41+
#org.hyperledger.fabric.sdk.client.thread_executor_corepoolsize=0
42+
## maximumPoolSize the maximum number of threads to allow in the pool defautl is Max integer.
43+
#org.hyperledger.fabric.sdk.client.thread_executor_maximumpoolsize=2147483647
44+
## keepAliveTime when the number of threads is greater than
45+
## the core, this is the maximum time that excess idle threads
46+
## will wait for new tasks before terminating.
47+
#org.hyperledger.fabric.sdk.client.thread_executor_keepalivetime=60
48+
## the time unit for the {@code keepAliveTime} argument (SECONDS,MILLISECONDS,NANOSECDONS) see Java's TimeUnit
49+
#org.hyperledger.fabric.sdk.client.thread_executor_keepalivetimeunit=SECONDS
4050

4151
# System wide defaults for CryptoPrimitives objects. You can customize further by using the
4252
# CryptoPrimitives.setProperties() method.
@@ -55,3 +65,4 @@
5565
# The algorithm used to generate a signature. Valid values are listed in the JCA Standard Algorithm Name Documentation
5666
# e.g. http://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#Signature
5767
# org.hyperledger.fabric.sdk.crypto.default_signature_algorithm = SHA256withECDSA
68+

src/main/java/org/hyperledger/fabric/sdk/HFClient.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
import java.util.Set;
2929
import java.util.concurrent.ExecutorService;
3030
import java.util.concurrent.Executors;
31+
import java.util.concurrent.SynchronousQueue;
32+
import java.util.concurrent.ThreadFactory;
33+
import java.util.concurrent.ThreadPoolExecutor;
34+
import java.util.concurrent.TimeUnit;
3135
import java.util.logging.Level;
3236
import java.util.logging.Logger;
3337

@@ -39,15 +43,18 @@
3943
import org.hyperledger.fabric.sdk.exception.NetworkConfigurationException;
4044
import org.hyperledger.fabric.sdk.exception.ProposalException;
4145
import org.hyperledger.fabric.sdk.exception.TransactionException;
46+
import org.hyperledger.fabric.sdk.helper.Config;
4247
import org.hyperledger.fabric.sdk.helper.Utils;
4348
import org.hyperledger.fabric.sdk.security.CryptoSuite;
4449

4550
import static java.lang.String.format;
4651
import static org.hyperledger.fabric.sdk.User.userContextCheck;
4752

4853
public class HFClient {
54+
private static final Config config = Config.getConfig();
4955

5056
private CryptoSuite cryptoSuite;
57+
protected final ExecutorService executorService;
5158

5259
static {
5360

@@ -59,11 +66,6 @@ public class HFClient {
5966
}
6067
}
6168

62-
private final ExecutorService executorService = Executors.newCachedThreadPool(r -> {
63-
Thread t = Executors.defaultThreadFactory().newThread(r);
64-
t.setDaemon(true);
65-
return t;
66-
});
6769

6870
ExecutorService getExecutorService() {
6971
return executorService;
@@ -79,8 +81,24 @@ public User getUserContext() {
7981

8082
private User userContext;
8183

84+
protected final ThreadFactory threadFactory = Executors.defaultThreadFactory();
85+
86+
private static final int CLIENT_THREAD_EXECUTOR_COREPOOLSIZE = config.getClientThreadExecutorCorePoolSize();
87+
private static final int CLIENT_THREAD_EXECUTOR_MAXIMUMPOOLSIZE = config.getClientThreadExecutorMaxiumPoolSize();
88+
private static final long CLIENT_THREAD_EXECUTOR_KEEPALIVETIME = config.getClientThreadExecutorKeepAliveTime();
89+
private static final TimeUnit CLIENT_THREAD_EXECUTOR_KEEPALIVETIMEUNIT = config.getClientThreadExecutorKeepAliveTimeUnit();
90+
8291
private HFClient() {
8392

93+
executorService = new ThreadPoolExecutor(CLIENT_THREAD_EXECUTOR_COREPOOLSIZE, CLIENT_THREAD_EXECUTOR_MAXIMUMPOOLSIZE,
94+
CLIENT_THREAD_EXECUTOR_KEEPALIVETIME, CLIENT_THREAD_EXECUTOR_KEEPALIVETIMEUNIT,
95+
new SynchronousQueue<Runnable>(),
96+
r -> {
97+
Thread t = threadFactory.newThread(r);
98+
t.setDaemon(true);
99+
return t;
100+
});
101+
84102
}
85103

86104
public CryptoSuite getCryptoSuite() {
@@ -120,7 +138,7 @@ public static HFClient createNewInstance() {
120138
* Configures a channel based on information loaded from a Network Config file.
121139
* Note that it is up to the caller to initialize the returned channel.
122140
*
123-
* @param channelName The name of the channel to be configured
141+
* @param channelName The name of the channel to be configured
124142
* @param networkConfig The network configuration to use to configure the channel
125143
* @return The configured channel, or null if the channel is not defined in the configuration
126144
* @throws InvalidArgumentException
@@ -144,7 +162,6 @@ public Channel loadChannelFromConfig(String channelName, NetworkConfig networkCo
144162
return networkConfig.loadChannel(this, channelName);
145163
}
146164

147-
148165
/**
149166
* newChannel - already configured channel.
150167
*
@@ -188,7 +205,7 @@ public Channel newChannel(String name) throws InvalidArgumentException {
188205
*/
189206

190207
public Channel newChannel(String name, Orderer orderer, ChannelConfiguration channelConfiguration,
191-
byte[]... channelConfigurationSignatures) throws TransactionException, InvalidArgumentException {
208+
byte[]... channelConfigurationSignatures) throws TransactionException, InvalidArgumentException {
192209

193210
clientCheck();
194211
if (Utils.isNullOrEmpty(name)) {
@@ -620,7 +637,7 @@ public byte[] getChannelConfigurationSignature(ChannelConfiguration channelConfi
620637
*/
621638

622639
public byte[] getUpdateChannelConfigurationSignature(UpdateChannelConfiguration updateChannelConfiguration,
623-
User signer) throws InvalidArgumentException {
640+
User signer) throws InvalidArgumentException {
624641

625642
clientCheck();
626643

@@ -640,7 +657,7 @@ public byte[] getUpdateChannelConfigurationSignature(UpdateChannelConfiguration
640657
*/
641658

642659
public Collection<ProposalResponse> sendInstallProposal(InstallProposalRequest installProposalRequest,
643-
Collection<Peer> peers) throws ProposalException, InvalidArgumentException {
660+
Collection<Peer> peers) throws ProposalException, InvalidArgumentException {
644661

645662
clientCheck();
646663

@@ -651,7 +668,6 @@ public Collection<ProposalResponse> sendInstallProposal(InstallProposalRequest i
651668

652669
}
653670

654-
655671
private void clientCheck() throws InvalidArgumentException {
656672

657673
if (null == cryptoSuite) {

src/main/java/org/hyperledger/fabric/sdk/helper/Config.java

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.HashMap;
2121
import java.util.Map;
2222
import java.util.Properties;
23+
import java.util.concurrent.TimeUnit;
2324

2425
import org.apache.commons.logging.Log;
2526
import org.apache.commons.logging.LogFactory;
@@ -83,6 +84,15 @@ public class Config {
8384
public static final String CONN_SSL_PROVIDER = "org.hyperledger.fabric.sdk.connections.ssl.sslProvider";
8485
public static final String CONN_SSL_NEGTYPE = "org.hyperledger.fabric.sdk.connections.ssl.negotiationType";
8586

87+
/**
88+
* Default HFClient thread executor settings.
89+
*/
90+
91+
public static final String CLIENT_THREAD_EXECUTOR_COREPOOLSIZE = "org.hyperledger.fabric.sdk.client.thread_executor_corepoolsize";
92+
public static final String CLIENT_THREAD_EXECUTOR_MAXIMUMPOOLSIZE = "org.hyperledger.fabric.sdk.client.thread_executor_maximumpoolsize";
93+
public static final String CLIENT_THREAD_EXECUTOR_KEEPALIVETIME = "org.hyperledger.fabric.sdk.client.thread_executor_keepalivetime";
94+
public static final String CLIENT_THREAD_EXECUTOR_KEEPALIVETIMEUNIT = "org.hyperledger.fabric.sdk.client.thread_executor_keepalivetimeunit";
95+
8696
/**
8797
* Miscellaneous settings
8898
**/
@@ -150,6 +160,15 @@ private Config() {
150160
defaultProperty(CONN_SSL_PROVIDER, "openSSL");
151161
defaultProperty(CONN_SSL_NEGTYPE, "TLS");
152162

163+
/**
164+
* Default HFClient thread executor settings.
165+
*/
166+
167+
defaultProperty(CLIENT_THREAD_EXECUTOR_COREPOOLSIZE, "0");
168+
defaultProperty(CLIENT_THREAD_EXECUTOR_MAXIMUMPOOLSIZE, "" + Integer.MAX_VALUE);
169+
defaultProperty(CLIENT_THREAD_EXECUTOR_KEEPALIVETIME, "" + "60");
170+
defaultProperty(CLIENT_THREAD_EXECUTOR_KEEPALIVETIMEUNIT, "SECONDS");
171+
153172
/**
154173
* Logging settings
155174
**/
@@ -167,7 +186,6 @@ private Config() {
167186
defaultProperty(SERVICE_DISCOVER_FREQ_SECONDS, "120");
168187
defaultProperty(SERVICE_DISCOVER_WAIT_TIME, "5000");
169188

170-
171189
final String inLogLevel = sdkProperties.getProperty(LOGGERLEVEL);
172190

173191
if (null != inLogLevel) {
@@ -528,4 +546,47 @@ public DiagnosticFileDumper getDiagnosticFileDumper() {
528546
public long getTransactionListenerCleanUpTimeout() {
529547
return Long.parseLong(getProperty(TRANSACTION_CLEANUP_UP_TIMEOUT_WAIT_TIME));
530548
}
549+
550+
/**
551+
* The number of threads to keep in the pool, even if they are idle, unless {@code allowCoreThreadTimeOut} is set
552+
*
553+
* @return The number of threads to keep in the pool, even if they are idle, unless {@code allowCoreThreadTimeOut} is set
554+
*/
555+
556+
public int getClientThreadExecutorCorePoolSize() {
557+
return Integer.parseInt(getProperty(CLIENT_THREAD_EXECUTOR_COREPOOLSIZE));
558+
}
559+
560+
/**
561+
* maximumPoolSize the maximum number of threads to allow in the pool
562+
*
563+
* @return maximumPoolSize the maximum number of threads to allow in the pool
564+
*/
565+
public int getClientThreadExecutorMaxiumPoolSize() {
566+
return Integer.parseInt(getProperty(CLIENT_THREAD_EXECUTOR_MAXIMUMPOOLSIZE));
567+
}
568+
569+
/**
570+
* keepAliveTime when the number of threads is greater than
571+
* the core, this is the maximum time that excess idle threads
572+
* will wait for new tasks before terminating.
573+
*
574+
* @return The keep alive time.
575+
*/
576+
577+
public long getClientThreadExecutorKeepAliveTime() {
578+
return Long.parseLong(getProperty(CLIENT_THREAD_EXECUTOR_KEEPALIVETIME));
579+
}
580+
581+
/**
582+
* the time unit for the argument
583+
*
584+
* @return
585+
*/
586+
587+
public TimeUnit getClientThreadExecutorKeepAliveTimeUnit() {
588+
589+
return TimeUnit.valueOf(getProperty(CLIENT_THREAD_EXECUTOR_KEEPALIVETIMEUNIT));
590+
}
591+
531592
}

0 commit comments

Comments
 (0)