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

[#9482] Await termination of grpc server #9483

Merged
merged 1 commit into from
Dec 12, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class GrpcPropertiesServerOptionBuilder {
private static final String HANDSHAKE_TIMEOUT = ".handshake_timeout_millis";
private static final String MAX_INBOUND_MESSAGE_SIZE = ".inbound_message_size_max";
private static final String RECEIVE_BUFFER_SIZE = ".receive_buffer_size";
private static final String GRPC_MAX_TERM_WAIT_TIME_MILLIS = ".grpc_max_term_wait_time_millis";
private static final String CHANNEL_TYPE = ".channel-type";

private final ServerOption.Builder builder = ServerOption.newBuilder();
Expand Down Expand Up @@ -81,11 +82,14 @@ public void setFlowControlWindowSizeInit(DataSize flowControlWindowStr) {
builder.setFlowControlWindow((int) flowControlWindowStr.toBytes());
}


public void setReceiveBufferSize(DataSize receiveBufferSize) {
public void setReceiveBufferSize(DataSize receiveBufferSize) {
builder.setReceiveBufferSize((int) receiveBufferSize.toBytes());
}

public void setGrpcMaxTermWaitTimeMillis(long grpcMaxTermWaitTimeMillis) {
builder.setGrpcMaxTermWaitTimeMillis(grpcMaxTermWaitTimeMillis);
}

public void setChannelType(String channelTypeEnum) {
Objects.requireNonNull(channelTypeEnum, "channelTypeEnum");
builder.setChannelTypeEnum(channelTypeEnum);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import io.grpc.ServerInterceptor;
import io.grpc.ServerServiceDefinition;
import io.grpc.ServerTransportFilter;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
Expand All @@ -49,6 +49,7 @@
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

/**
* @author Taejin Koo
Expand Down Expand Up @@ -159,15 +160,40 @@ private void addService() {
}
}

private void shutdownServer() {
if (server == null || server.isTerminated()) {
return;
}

final long maxWaitTime = serverOption.getGrpcMaxTermWaitTimeMillis();

server.shutdown();
if (awaitServerTermination(maxWaitTime)) {
return;
}

server.shutdownNow();
awaitServerTermination(1000);
}

private boolean awaitServerTermination(long maxWaitTimeMillis) {
try {
return server.awaitTermination(maxWaitTimeMillis, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
logger.warn("awaitServerTermination({}ms) was interrupted", maxWaitTimeMillis, e);
Thread.currentThread().interrupt();
}

return false;
}

@Override
public void destroy() throws Exception {
if (logger.isInfoEnabled()) {
logger.info("Destroy {} server {}", this.beanName, this.server);
}

if (this.server != null) {
this.server.shutdown();
}
shutdownServer();

for (Object bindableService : serviceList) {
if (bindableService instanceof Closeable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ collector.receiver.grpc.agent.flow-control_window_size_init=1MB
collector.receiver.grpc.agent.header_list_size_max=1KB
collector.receiver.grpc.agent.inbound_message_size_max=4MB
collector.receiver.grpc.agent.receive_buffer_size=64KB
collector.receiver.grpc.agent.grpc_max_term_wait_time_millis=3000
## AUTO, NIO, EPOLL
collector.receiver.grpc.agent.channel-type=AUTO

Expand All @@ -26,6 +27,7 @@ collector.receiver.grpc.stat.flow-control_window_size_init=1MB
collector.receiver.grpc.stat.header_list_size_max=1KB
collector.receiver.grpc.stat.inbound_message_size_max=4MB
collector.receiver.grpc.stat.receive_buffer_size=64KB
collector.receiver.grpc.stat.grpc_max_term_wait_time_millis=3000
## AUTO, NIO, EPOLL
collector.receiver.grpc.stat.channel-type=AUTO

Expand All @@ -41,5 +43,6 @@ collector.receiver.grpc.span.flow-control_window_size_init=1MB
collector.receiver.grpc.span.header_list_size_max=1KB
collector.receiver.grpc.span.inbound_message_size_max=4MB
collector.receiver.grpc.span.receive_buffer_size=64KB
collector.receiver.grpc.span.grpc_max_term_wait_time_millis=3000
## AUTO, NIO, EPOLL
collector.receiver.grpc.span.channel-type=AUTO
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class ServerOption {
public static final long DEFAULT_HANDSHAKE_TIMEOUT = TimeUnit.SECONDS.toMillis(120);
public static final int DEFAULT_RECEIVE_BUFFER_SIZE = 64 * 1024;

public static final long DEFAULT_GRPC_MAX_TERM_WAIT_TIME_MILLIS = 3000;

public static final String DEFAULT_CHANNEL_TYPE = ChannelTypeEnum.AUTO.name();

// Sets a custom keepalive time, the delay time for sending next keepalive ping.
Expand Down Expand Up @@ -76,11 +78,13 @@ public class ServerOption {
// ChannelOption
private final int receiveBufferSize;

private final long grpcMaxTermWaitTimeMillis;

public final ChannelTypeEnum channelTypeEnum;

ServerOption(long keepAliveTime, long keepAliveTimeout, long permitKeepAliveTime, long maxConnectionIdle,
int maxConcurrentCallsPerConnection, int maxInboundMessageSize, int maxHeaderListSize,
long handshakeTimeout, int flowControlWindow, int receiveBufferSize,
long handshakeTimeout, int flowControlWindow, int receiveBufferSize, long grpcMaxTermWaitTimeMillis,
ChannelTypeEnum channelTypeEnum) {
this.keepAliveTime = keepAliveTime;
this.keepAliveTimeout = keepAliveTimeout;
Expand All @@ -92,6 +96,7 @@ public class ServerOption {
this.handshakeTimeout = handshakeTimeout;
this.flowControlWindow = flowControlWindow;
this.receiveBufferSize = receiveBufferSize;
this.grpcMaxTermWaitTimeMillis = grpcMaxTermWaitTimeMillis;
this.channelTypeEnum = Objects.requireNonNull(channelTypeEnum, "channelTypeEnum");
}

Expand Down Expand Up @@ -147,6 +152,10 @@ public int getReceiveBufferSize() {
return receiveBufferSize;
}

public long getGrpcMaxTermWaitTimeMillis() {
return grpcMaxTermWaitTimeMillis;
}

public ChannelTypeEnum getChannelTypeEnum() {
return channelTypeEnum;
}
Expand All @@ -157,23 +166,21 @@ public static Builder newBuilder() {

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("ServerOption{");
sb.append("keepAliveTime=").append(keepAliveTime);
sb.append(", keepAliveTimeout=").append(keepAliveTimeout);
sb.append(", permitKeepAliveTime=").append(permitKeepAliveTime);
sb.append(", permitKeepAliveWithoutCalls=").append(permitKeepAliveWithoutCalls);
sb.append(", maxConnectionIdle=").append(maxConnectionIdle);
sb.append(", maxConnectionAge=").append(DEFAULT_MAX_CONNECTION_AGE);
sb.append(", maxConnectionAgeGrace=").append(DEFAULT_MAX_CONNECTION_AGE_GRACE);
sb.append(", maxConcurrentCallsPerConnection=").append(maxConcurrentCallsPerConnection);
sb.append(", maxInboundMessageSize=").append(maxInboundMessageSize);
sb.append(", maxHeaderListSize=").append(maxHeaderListSize);
sb.append(", handshakeTimeout=").append(handshakeTimeout);
sb.append(", flowControlWindow=").append(flowControlWindow);
sb.append(", receiveBufferSize=").append(receiveBufferSize);
sb.append(", channelTypeEnum=").append(channelTypeEnum);
sb.append('}');
return sb.toString();
return "ServerOption{" +
"keepAliveTime=" + keepAliveTime +
", keepAliveTimeout=" + keepAliveTimeout +
", permitKeepAliveTime=" + permitKeepAliveTime +
", permitKeepAliveWithoutCalls=" + permitKeepAliveWithoutCalls +
", maxConnectionIdle=" + maxConnectionIdle +
", maxConcurrentCallsPerConnection=" + maxConcurrentCallsPerConnection +
", maxInboundMessageSize=" + maxInboundMessageSize +
", maxHeaderListSize=" + maxHeaderListSize +
", handshakeTimeout=" + handshakeTimeout +
", flowControlWindow=" + flowControlWindow +
", receiveBufferSize=" + receiveBufferSize +
", grpcMaxTermWaitTimeMillis=" + grpcMaxTermWaitTimeMillis +
", channelTypeEnum=" + channelTypeEnum +
'}';
}

public static class Builder {
Expand All @@ -200,6 +207,8 @@ public static class Builder {

private int receiveBufferSize = DEFAULT_RECEIVE_BUFFER_SIZE;

private long grpcMaxTermWaitTimeMillis = DEFAULT_GRPC_MAX_TERM_WAIT_TIME_MILLIS;

private ChannelTypeEnum channelTypeEnum = ChannelTypeEnum.valueOf(DEFAULT_CHANNEL_TYPE);

private Builder() {
Expand All @@ -208,7 +217,7 @@ private Builder() {
public ServerOption build() {
final ServerOption serverOption = new ServerOption(keepAliveTime, keepAliveTimeout, permitKeepAliveTime,
maxConnectionIdle, maxConcurrentCallsPerConnection, maxInboundMessageSize,
maxHeaderListSize, handshakeTimeout, flowControlWindow, receiveBufferSize, channelTypeEnum);
maxHeaderListSize, handshakeTimeout, flowControlWindow, receiveBufferSize, grpcMaxTermWaitTimeMillis, channelTypeEnum);
return serverOption;
}

Expand Down Expand Up @@ -262,27 +271,32 @@ public void setReceiveBufferSize(int receiveBufferSize) {
this.receiveBufferSize = receiveBufferSize;
}

public void setGrpcMaxTermWaitTimeMillis(long grpcMaxTermWaitTimeMillis) {
Assert.isTrue(grpcMaxTermWaitTimeMillis > 0, "grpcMaxTermWaitTimeMillis " + grpcMaxTermWaitTimeMillis + " must be positive");
this.grpcMaxTermWaitTimeMillis = grpcMaxTermWaitTimeMillis;
}

public void setChannelTypeEnum(String channelTypeEnum) {
Objects.requireNonNull(channelTypeEnum, "channelTypeEnum");
this.channelTypeEnum = ChannelTypeEnum.valueOf(channelTypeEnum);
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Builder{");
sb.append("keepAliveTime=").append(keepAliveTime);
sb.append(", keepAliveTimeout=").append(keepAliveTimeout);
sb.append(", permitKeepAliveTime=").append(permitKeepAliveTime);
sb.append(", maxConnectionIdle=").append(maxConnectionIdle);
sb.append(", maxConcurrentCallsPerConnection=").append(maxConcurrentCallsPerConnection);
sb.append(", maxInboundMessageSize=").append(maxInboundMessageSize);
sb.append(", maxHeaderListSize=").append(maxHeaderListSize);
sb.append(", handshakeTimeout=").append(handshakeTimeout);
sb.append(", flowControlWindow=").append(flowControlWindow);
sb.append(", receiveBufferSize=").append(receiveBufferSize);
sb.append(", channelTypeEnum=").append(channelTypeEnum);
sb.append('}');
return sb.toString();
return "Builder{" +
"keepAliveTime=" + keepAliveTime +
", keepAliveTimeout=" + keepAliveTimeout +
", permitKeepAliveTime=" + permitKeepAliveTime +
", maxConnectionIdle=" + maxConnectionIdle +
", maxConcurrentCallsPerConnection=" + maxConcurrentCallsPerConnection +
", maxInboundMessageSize=" + maxInboundMessageSize +
", maxHeaderListSize=" + maxHeaderListSize +
", handshakeTimeout=" + handshakeTimeout +
", flowControlWindow=" + flowControlWindow +
", receiveBufferSize=" + receiveBufferSize +
", grpcMaxTermWaitTimeMillis=" + grpcMaxTermWaitTimeMillis +
", channelTypeEnum=" + channelTypeEnum +
'}';
}
}
}