Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[close #380] make gRPC idle timeout configurable #379

Merged
merged 2 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/org/tikv/common/ConfigUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ConfigUtils {
public static final String TIKV_GRPC_MAX_FRAME_SIZE = "tikv.grpc.max_frame_size";
public static final String TIKV_GRPC_KEEPALIVE_TIME = "tikv.grpc.keepalive_time";
public static final String TIKV_GRPC_KEEPALIVE_TIMEOUT = "tikv.grpc.keepalive_timeout";
public static final String TIKV_GRPC_IDLE_TIMEOUT = "tikv.grpc.idle_timeout";

public static final String TIKV_INDEX_SCAN_BATCH_SIZE = "tikv.index.scan_batch_size";
public static final String TIKV_INDEX_SCAN_CONCURRENCY = "tikv.index.scan_concurrency";
Expand Down Expand Up @@ -171,6 +172,7 @@ public class ConfigUtils {

public static final int DEF_TIKV_GRPC_KEEPALIVE_TIME = 10;
public static final int DEF_TIKV_GRPC_KEEPALIVE_TIMEOUT = 3;
public static final int DEF_TIKV_GRPC_IDLE_TIMEOUT = 60;
public static final boolean DEF_TIKV_TLS_ENABLE = false;
public static final boolean DEF_TIFLASH_ENABLE = false;

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/tikv/common/TiConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ private static void loadFromDefaultProperties() {
setIfMissing(TIKV_RAWKV_DEFAULT_BACKOFF_IN_MS, DEF_TIKV_RAWKV_DEFAULT_BACKOFF_IN_MS);
setIfMissing(TIKV_GRPC_KEEPALIVE_TIME, DEF_TIKV_GRPC_KEEPALIVE_TIME);
setIfMissing(TIKV_GRPC_KEEPALIVE_TIMEOUT, DEF_TIKV_GRPC_KEEPALIVE_TIMEOUT);
setIfMissing(TIKV_GRPC_IDLE_TIMEOUT, DEF_TIKV_GRPC_IDLE_TIMEOUT);
setIfMissing(TIKV_TLS_ENABLE, DEF_TIKV_TLS_ENABLE);
setIfMissing(TIFLASH_ENABLE, DEF_TIFLASH_ENABLE);
setIfMissing(TIKV_RAWKV_READ_TIMEOUT_IN_MS, DEF_TIKV_RAWKV_READ_TIMEOUT_IN_MS);
Expand Down Expand Up @@ -373,6 +374,7 @@ private static ReplicaRead getReplicaRead(String key) {

private int keepaliveTime = getInt(TIKV_GRPC_KEEPALIVE_TIME);
private int keepaliveTimeout = getInt(TIKV_GRPC_KEEPALIVE_TIMEOUT);
private int idleTimeout = getInt(TIKV_GRPC_IDLE_TIMEOUT);

private boolean circuitBreakEnable = getBoolean(TiKV_CIRCUIT_BREAK_ENABLE);
private int circuitBreakAvailabilityWindowInSeconds =
Expand Down Expand Up @@ -782,6 +784,14 @@ public void setKeepaliveTimeout(int timeout) {
this.keepaliveTimeout = timeout;
}

public int getIdleTimeout() {
return idleTimeout;
}

public void setIdleTimeout(int timeout) {
this.idleTimeout = timeout;
}

public boolean isTiFlashEnabled() {
return tiFlashEnable;
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/tikv/common/TiSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@ public TiSession(TiConfiguration conf) {
conf.getMaxFrameSize(),
conf.getKeepaliveTime(),
conf.getKeepaliveTimeout(),
conf.getIdleTimeout(),
conf.getTrustCertCollectionFile(),
conf.getKeyCertChainFile(),
conf.getKeyFile())
: new ChannelFactory(
conf.getMaxFrameSize(), conf.getKeepaliveTime(), conf.getKeepaliveTimeout());
conf.getMaxFrameSize(),
conf.getKeepaliveTime(),
conf.getKeepaliveTimeout(),
conf.getIdleTimeout());

this.client = PDClient.createRaw(conf, channelFactory);
this.enableGrpcForward = conf.getEnableGrpcForward();
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/org/tikv/common/util/ChannelFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,31 @@ public class ChannelFactory implements AutoCloseable {
private final int maxFrameSize;
private final int keepaliveTime;
private final int keepaliveTimeout;
private final int idleTimeout;
private final ConcurrentHashMap<String, ManagedChannel> connPool = new ConcurrentHashMap<>();
private final SslContextBuilder sslContextBuilder;

public ChannelFactory(int maxFrameSize, int keepaliveTime, int keepaliveTimeout) {
public ChannelFactory(
int maxFrameSize, int keepaliveTime, int keepaliveTimeout, int idleTimeout) {
this.maxFrameSize = maxFrameSize;
this.keepaliveTime = keepaliveTime;
this.keepaliveTimeout = keepaliveTimeout;
this.idleTimeout = idleTimeout;
this.sslContextBuilder = null;
}

public ChannelFactory(
int maxFrameSize,
int keepaliveTime,
int keepaliveTimeout,
int idleTimeout,
String trustCertCollectionFilePath,
String keyCertChainFilePath,
String keyFilePath) {
this.maxFrameSize = maxFrameSize;
this.keepaliveTime = keepaliveTime;
this.keepaliveTimeout = keepaliveTimeout;
this.idleTimeout = idleTimeout;
this.sslContextBuilder =
getSslContextBuilder(trustCertCollectionFilePath, keyCertChainFilePath, keyFilePath);
}
Expand Down Expand Up @@ -97,7 +102,7 @@ public ManagedChannel getChannel(String addressStr, HostMapping hostMapping) {
.keepAliveTime(keepaliveTime, TimeUnit.SECONDS)
.keepAliveTimeout(keepaliveTimeout, TimeUnit.SECONDS)
.keepAliveWithoutCalls(true)
.idleTimeout(60, TimeUnit.SECONDS);
.idleTimeout(idleTimeout, TimeUnit.SECONDS);

if (sslContextBuilder == null) {
return builder.usePlaintext().build();
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/tikv/common/TiConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,15 @@ public void tiFlashDefaultValueTest() {
TiConfiguration conf = TiConfiguration.createRawDefault();
assertFalse(conf.isTiFlashEnabled());
}

@Test
public void testGrpcIdleTimeoutValue() {
TiConfiguration conf = TiConfiguration.createDefault();
// default value
assertEquals(TiConfiguration.getInt(ConfigUtils.TIKV_GRPC_IDLE_TIMEOUT), conf.getIdleTimeout());
// new value
int newValue = 100000;
conf.setIdleTimeout(newValue);
assertEquals(newValue, conf.getIdleTimeout());
}
}