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

[#11256] Fix stream leak of FlowControl #11305

Merged
merged 1 commit into from
Jul 31, 2024
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 @@ -63,7 +63,7 @@ public GrpcSpanReceiverConfiguration() {

@Deprecated
@Configuration
@ConditionalOnProperty(name = "collector.receiver.grpc.span.stream.flow-control.type", havingValue = "legacy")
@ConditionalOnProperty(name = "collector.receiver.grpc.span.stream.flow-control.type", havingValue = "legacy", matchIfMissing = true)
public static class LegacySpanInterceptorConfiguration {
@Bean
public FactoryBean<ScheduledExecutorService> grpcSpanStreamScheduler(@Qualifier("grpcSpanStreamProperties")
Expand All @@ -89,7 +89,7 @@ public FactoryBean<ServerInterceptor> spanStreamExecutorInterceptor(@Qualifier("
}

@Configuration
@ConditionalOnProperty(name = "collector.receiver.grpc.span.stream.flow-control.type", havingValue = "rate-limit", matchIfMissing = true)
@ConditionalOnProperty(name = "collector.receiver.grpc.span.stream.flow-control.type", havingValue = "rate-limit")
public static class RateLimitServerInterceptorConfiguration {
@Bean
public Bandwidth spanBandwidth(@Value("${collector.receiver.grpc.span.stream.flow-control.rate-limit.capacity:5000}") long capacity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public GrpcStatReceiverConfiguration() {

@Deprecated
@Configuration
@ConditionalOnProperty(name = "collector.receiver.grpc.stat.stream.flow-control.type", havingValue = "legacy")
@ConditionalOnProperty(name = "collector.receiver.grpc.stat.stream.flow-control.type", havingValue = "legacy", matchIfMissing = true)
public static class LegacySpanInterceptorConfiguration {

@Bean
Expand Down Expand Up @@ -92,7 +92,7 @@ public FactoryBean<ServerInterceptor> statStreamExecutorInterceptor(@Qualifier("


@Configuration
@ConditionalOnProperty(name = "collector.receiver.grpc.stat.stream.flow-control.type", havingValue = "rate-limit", matchIfMissing = true)
@ConditionalOnProperty(name = "collector.receiver.grpc.stat.stream.flow-control.type", havingValue = "rate-limit")
public static class RateLimitServerInterceptorConfiguration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class GrpcStreamProperties {
@PositiveOrZero
private int callInitRequestCount = 1000;
@Positive
private int schedulerPeriodMillis = 64;
private int schedulerPeriodMillis = 1000;
private int schedulerRecoveryMessageCount = 10;

private long idleTimeout = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
this.serverCall.close(status, trailers);
}

@Override
public boolean isCancelled() {
return this.serverCall.isCancelled();

Check warning on line 56 in grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/DefaultServerCallWrapper.java

View check run for this annotation

Codecov / codecov/patch

grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/DefaultServerCallWrapper.java#L56

Added line #L56 was not covered by tests
}

@Override
public String toString() {
return "DefaultServerCallWrapper{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

import io.grpc.Metadata;
import io.grpc.Status;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Objects;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;

public class FlowControlRejectExecutionListener implements RejectedExecutionListener {
private final Logger logger = LogManager.getLogger(this.getClass());

private static final AtomicLongFieldUpdater<FlowControlRejectExecutionListener> REJECT =
AtomicLongFieldUpdater.newUpdater(FlowControlRejectExecutionListener.class, "rejectedExecutionCounter");

private static final Status STREAM_IDLE_TIMEOUT = Status.DEADLINE_EXCEEDED.withDescription("Stream idle timeout");

private final Logger logger = LogManager.getLogger(this.getClass());

private final String name;

private final AtomicLong rejectedExecutionCounter = new AtomicLong(0);
private volatile long rejectedExecutionCounter;

private final ServerCallWrapper serverCall;
private final long recoveryMessagesCount;

Expand All @@ -33,11 +38,19 @@

@Override
public void onRejectedExecution() {
this.rejectedExecutionCounter.incrementAndGet();
REJECT.incrementAndGet(this);

Check warning on line 41 in grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/FlowControlRejectExecutionListener.java

View check run for this annotation

Codecov / codecov/patch

grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/FlowControlRejectExecutionListener.java#L41

Added line #L41 was not covered by tests
}

@Override
public void onSchedule() {
if (logger.isTraceEnabled()) {
logger.trace("Stream state check {} agent:{}/{}", this.name, serverCall.getApplicationName(), serverCall.getAgentId());

Check warning on line 47 in grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/FlowControlRejectExecutionListener.java

View check run for this annotation

Codecov / codecov/patch

grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/FlowControlRejectExecutionListener.java#L47

Added line #L47 was not covered by tests
}
if (this.serverCall.isCancelled()) {
logger.info("Stream already cancelled:{} agent:{}/{}", this.name, serverCall.getApplicationName(), serverCall.getAgentId());
this.cancel();
return;

Check warning on line 52 in grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/FlowControlRejectExecutionListener.java

View check run for this annotation

Codecov / codecov/patch

grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/FlowControlRejectExecutionListener.java#L50-L52

Added lines #L50 - L52 were not covered by tests
}
if (!expireIdleTimeout()) {
reject();
}
Expand All @@ -55,17 +68,20 @@


private void reject() {
final long currentRejectCount = this.rejectedExecutionCounter.get();
final long currentRejectCount = getRejectedExecutionCount();
if (currentRejectCount > 0) {
final long recovery = Math.min(currentRejectCount, recoveryMessagesCount);
this.rejectedExecutionCounter.addAndGet(-recovery);
REJECT.addAndGet(this, -recovery);

Check warning on line 74 in grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/FlowControlRejectExecutionListener.java

View check run for this annotation

Codecov / codecov/patch

grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/FlowControlRejectExecutionListener.java#L74

Added line #L74 was not covered by tests
if (logger.isDebugEnabled()) {
logger.debug("flow-control request:{} {}/{}", recovery, serverCall.getApplicationName(), serverCall.getAgentId());

Check warning on line 76 in grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/FlowControlRejectExecutionListener.java

View check run for this annotation

Codecov / codecov/patch

grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/FlowControlRejectExecutionListener.java#L76

Added line #L76 was not covered by tests
}
serverCall.request((int) recovery);
}
}

@Override
public long getRejectedExecutionCount() {
return rejectedExecutionCounter.get();
return REJECT.get(this);
}

@Override
Expand Down Expand Up @@ -102,16 +118,19 @@


private void idleTimeout() {
logger.info("stream idle timeout applicationName:{} agentId:{} {}", this.name, serverCall.getApplicationName(), serverCall.getAgentId());
serverCall.cancel(STREAM_IDLE_TIMEOUT, new Metadata());
logger.info("Stream idle timeout agent:{}/{} {}", this.name, serverCall.getApplicationName(), serverCall.getAgentId());
try {
serverCall.cancel(STREAM_IDLE_TIMEOUT, new Metadata());
} catch (IllegalStateException ex) {
logger.warn("Failed to cancel stream. agent:{}/{} {}", serverCall.getApplicationName(), serverCall.getAgentId(), ex.getMessage());

Check warning on line 125 in grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/FlowControlRejectExecutionListener.java

View check run for this annotation

Codecov / codecov/patch

grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/FlowControlRejectExecutionListener.java#L124-L125

Added lines #L124 - L125 were not covered by tests
}
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("RejectedExecutionListener{");
sb.append("rejectedExecutionCounter=").append(rejectedExecutionCounter);
sb.append(", serverCall=").append(serverCall);
sb.append('}');
return sb.toString();
return "RejectedExecutionListener{" +

Check warning on line 131 in grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/FlowControlRejectExecutionListener.java

View check run for this annotation

Codecov / codecov/patch

grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/FlowControlRejectExecutionListener.java#L131

Added line #L131 was not covered by tests
"rejectedExecutionCounter=" + rejectedExecutionCounter +
", serverCall=" + serverCall +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface ServerCallWrapper {
SocketAddress getRemoteAddr();

void cancel(Status status, Metadata trailers);

boolean isCancelled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.grpc.Metadata;
import io.grpc.Status;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Objects;
import java.util.concurrent.Future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("StreamExecutorRejectedExecutionRequestScheduler{");
sb.append("scheduledExecutorService=").append(scheduledExecutor);
sb.append(", rejectedExecutionListenerFactory=").append(rejectedExecutionListenerFactory);
sb.append('}');
return sb.toString();
return "StreamExecutorRejectedExecutionRequestScheduler{" +

Check warning on line 69 in grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/StreamExecutorRejectedExecutionRequestScheduler.java

View check run for this annotation

Codecov / codecov/patch

grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/StreamExecutorRejectedExecutionRequestScheduler.java#L69

Added line #L69 was not covered by tests
"scheduledExecutorService=" + scheduledExecutor +
", rejectedExecutionListenerFactory=" + rejectedExecutionListenerFactory +
'}';
}

public static class Listener {
Expand Down Expand Up @@ -102,10 +101,9 @@

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Listener{");
sb.append("rejectedExecutionListener=").append(rejectedExecutionListener);
sb.append('}');
return sb.toString();
return "Listener{" +

Check warning on line 104 in grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/StreamExecutorRejectedExecutionRequestScheduler.java

View check run for this annotation

Codecov / codecov/patch

grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/StreamExecutorRejectedExecutionRequestScheduler.java#L104

Added line #L104 was not covered by tests
"rejectedExecutionListener=" + rejectedExecutionListener +
'}';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;

/**
* @author jaehong.kim
Expand Down Expand Up @@ -86,10 +85,11 @@
}
});
// scheduleListener.onMessage();
} catch (RejectedExecutionException ree) {
} catch (Throwable th) {

Check warning on line 88 in grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/StreamExecutorServerInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/StreamExecutorServerInterceptor.java#L88

Added line #L88 was not covered by tests
// Defense code, need log ?
scheduleListener.onRejectedExecution();
throttledLogger.info("Failed to request. Rejected execution, count={}", scheduleListener.getRejectedExecutionCount());
throttledLogger.info("Failed to request. Rejected execution, count={} {}/{}",
scheduleListener.getRejectedExecutionCount(), serverCall.getApplicationName(), serverCall.getAgentId());

Check warning on line 92 in grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/StreamExecutorServerInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc/src/main/java/com/navercorp/pinpoint/grpc/server/flowcontrol/StreamExecutorServerInterceptor.java#L91-L92

Added lines #L91 - L92 were not covered by tests
}
}

Expand Down