Skip to content

Commit

Permalink
[pinpoint-apm#7375] Refactor grpc-stream
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Dec 4, 2020
1 parent 4dadb4c commit eabce0b
Show file tree
Hide file tree
Showing 28 changed files with 993 additions and 188 deletions.
10 changes: 10 additions & 0 deletions agent/src/main/resources/profiles/local/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
<AppenderRef ref="rollingFile"/>
</Logger>

<Logger name="io.grpc.Context" level="INFO" additivity="false">
<AppenderRef ref="console"/>
<AppenderRef ref="rollingFile"/>
</Logger>
<Logger name="io.grpc.netty.Utils" level="INFO" additivity="false">
<AppenderRef ref="console"/>
<AppenderRef ref="rollingFile"/>
</Logger>


<Root level="INFO">
<AppenderRef ref="console"/>
<AppenderRef ref="rollingFile"/>
Expand Down
9 changes: 9 additions & 0 deletions agent/src/main/resources/profiles/release/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
<AppenderRef ref="rollingFile"/>
</Logger>

<Logger name="io.grpc.Context" level="INFO" additivity="false">
<AppenderRef ref="console"/>
<AppenderRef ref="rollingFile"/>
</Logger>
<Logger name="io.grpc.netty.Utils" level="INFO" additivity="false">
<AppenderRef ref="console"/>
<AppenderRef ref="rollingFile"/>
</Logger>

<Root level="INFO">
<AppenderRef ref="console"/>
<AppenderRef ref="rollingFile"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.navercorp.pinpoint.grpc.MessageFormatUtils;
import com.navercorp.pinpoint.grpc.StatusError;
import com.navercorp.pinpoint.grpc.StatusErrors;
import com.navercorp.pinpoint.grpc.server.ServerContext;
import com.navercorp.pinpoint.grpc.trace.PAgentStat;
import com.navercorp.pinpoint.grpc.trace.PAgentStatBatch;
import com.navercorp.pinpoint.grpc.trace.PStatMessage;
Expand Down Expand Up @@ -93,6 +94,9 @@ public void onError(Throwable throwable) {

@Override
public void onCompleted() {
com.navercorp.pinpoint.grpc.Header header = ServerContext.getAgentInfo();
logger.info("onCompleted {}", header);

responseObserver.onNext(Empty.newBuilder().build());
responseObserver.onCompleted();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@
import com.navercorp.pinpoint.grpc.trace.PSpanEvent;
import com.navercorp.pinpoint.grpc.trace.PSpanMessage;
import com.navercorp.pinpoint.grpc.trace.SpanGrpc;
import com.navercorp.pinpoint.profiler.sender.grpc.DefaultStreamEventListener;
import com.navercorp.pinpoint.profiler.sender.grpc.ReconnectExecutor;
import com.navercorp.pinpoint.profiler.sender.grpc.Reconnector;
import com.navercorp.pinpoint.profiler.sender.grpc.ResponseStreamObserver;
import com.navercorp.pinpoint.profiler.sender.grpc.SpanGrpcDataSender;
import com.navercorp.pinpoint.profiler.sender.grpc.StreamEventListener;
import com.navercorp.pinpoint.profiler.sender.grpc.StreamId;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ManagedChannel;
import io.grpc.MethodDescriptor;
import io.grpc.stub.ClientCallStreamObserver;
import io.grpc.stub.StreamObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -135,7 +138,24 @@ private StreamObserver<PSpanMessage> newSpanStream() {
System.out.println("NEW SpanStream");
System.out.println("###");
StreamId spanId = StreamId.newStreamId("SpanStream");
ResponseStreamObserver<PSpanMessage, Empty> responseStreamObserver = new ResponseStreamObserver<PSpanMessage, Empty>(spanId, spanStreamReconnector);
StreamEventListener<PSpanMessage> listener = new StreamEventListener<PSpanMessage>() {

@Override
public void start(ClientCallStreamObserver<PSpanMessage> requestStream) {
spanStreamReconnector.reset();
}

@Override
public void onError(Throwable t) {
spanStreamReconnector.reconnect();
}

@Override
public void onCompleted() {
spanStreamReconnector.reconnect();
}
};
ResponseStreamObserver<PSpanMessage, Empty> responseStreamObserver = new ResponseStreamObserver<PSpanMessage, Empty>(listener);
return spanStub.sendSpan(responseStreamObserver);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,32 @@
package com.navercorp.pinpoint.grpc.client.interceptor;

import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.grpc.logging.ThrottledLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicLong;

/**
* @author Woonduk Kang(emeroad)
*/
public class LoggingDiscardEventListener<ReqT> implements DiscardEventListener<ReqT> {
private final Logger logger;
public class LoggingDiscardEventListener<ReqT> implements DiscardEventListener<ReqT> {
private final ThrottledLogger logger;

private final long rateLimitCount;
private final AtomicLong discardCounter = new AtomicLong();

public LoggingDiscardEventListener(String loggerName, long rateLimitCount) {
Assert.requireNonNull(loggerName, "loggerName");
this.logger = LoggerFactory.getLogger(loggerName);
this.rateLimitCount = rateLimitCount;
Logger log = LoggerFactory.getLogger(loggerName);
this.logger = ThrottledLogger.getLogger(log, rateLimitCount);
}

@Override
public void onDiscard(ReqT message, String cause) {
final long beforeDiscardCount = this.discardCounter.getAndIncrement();
if ((beforeDiscardCount % this.rateLimitCount) == 0) {
logDiscardMessage(message, cause, beforeDiscardCount+1);
}
logDiscardMessage(message, cause);
}

private void logDiscardMessage(ReqT message, String cause, long discardCount) {
logger.info("Discard {} message, {}. discardCount:{}", getMessageType(message), cause, discardCount);
private void logDiscardMessage(ReqT message, String cause) {
if (logger.isInfoEnabled()) {
logger.info("Discard {} message, {}. discardCount:{}", getMessageType(message), cause, logger.getCounter() + 1);
}
}

@Override
Expand All @@ -62,6 +58,6 @@ private String getMessageType(ReqT message) {
}

public long getDiscardCount() {
return discardCounter.get();
return logger.getCounter();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/*
* Copyright 2020 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.grpc.logging;

import com.navercorp.pinpoint.common.util.Assert;
import org.slf4j.Logger;

import java.util.concurrent.atomic.AtomicLong;

/**
* @author Woonduk Kang(emeroad)
*/
public class ThrottledLogger {
private final Logger logger;
private final long ratio;
private final AtomicLong counter = new AtomicLong();

public static ThrottledLogger getLogger(Logger logger, long ratio) {
Assert.requireNonNull(logger, "logger");
return new ThrottledLogger(logger, ratio);
}

private ThrottledLogger(Logger logger, long ratio) {
this.logger = Assert.requireNonNull(logger, "logger");
this.ratio = ratio;
}

private boolean checkLogCounter() {
if (counter.getAndIncrement() % ratio == 0) {
return true;
}
return false;
}

public long getCounter() {
return counter.get();
}

public boolean isInfoEnabled() {
return logger.isInfoEnabled();
}

public void info(String msg) {
if (!logger.isInfoEnabled()) {
return;
}
if (!checkLogCounter()) {
return;
}
logger.info(msg);
}

public void info(String msg, Object arg) {
if (!logger.isInfoEnabled()) {
return;
}
if (!checkLogCounter()) {
return;
}
logger.info(msg, arg);
}

public void info(String msg, Object arg1, Object arg2) {
if (!logger.isInfoEnabled()) {
return;
}
if (!checkLogCounter()) {
return;
}
logger.info(msg, arg1, arg2);
}

public void info(String msg, Throwable t) {
if (!logger.isInfoEnabled()) {
return;
}
if (!checkLogCounter()) {
return;
}
logger.info(msg, t);
}

public void info(String msg, Object... args) {
if (!logger.isInfoEnabled()) {
return;
}
if (!checkLogCounter()) {
return;
}
logger.info(msg, args);
}

// debug -------------

public boolean isDebugEnabled() {
return logger.isDebugEnabled();
}

public void debug(String msg) {
if (!logger.isDebugEnabled()) {
return;
}
if (!checkLogCounter()) {
return;
}
logger.debug(msg);
}

public void debug(String msg, Object arg) {
if (!logger.isDebugEnabled()) {
return;
}
if (!checkLogCounter()) {
return;
}
logger.debug(msg, arg);
}

public void debug(String msg, Object arg1, Object arg2) {
if (!logger.isDebugEnabled()) {
return;
}
if (!checkLogCounter()) {
return;
}
logger.debug(msg, arg1, arg2);
}

public void debug(String msg, Throwable t) {
if (!logger.isDebugEnabled()) {
return;
}
if (!checkLogCounter()) {
return;
}
logger.debug(msg, t);
}

public void debug(String msg, Object... args) {
if (!logger.isDebugEnabled()) {
return;
}
if (!checkLogCounter()) {
return;
}
logger.debug(msg, args);
}

// warn -------------

public boolean isWarnEnabled() {
return logger.isWarnEnabled();
}

public void warn(String msg) {
if (!logger.isWarnEnabled()) {
return;
}
if (!checkLogCounter()) {
return;
}
logger.warn(msg);
}

public void warn(String msg, Object arg) {
if (!logger.isWarnEnabled()) {
return;
}
if (!checkLogCounter()) {
return;
}
logger.warn(msg, arg);
}

public void warn(String msg, Object arg1, Object arg2) {
if (!logger.isWarnEnabled()) {
return;
}
if (!checkLogCounter()) {
return;
}
logger.warn(msg, arg1, arg2);
}

public void warn(String msg, Throwable t) {
if (!logger.isWarnEnabled()) {
return;
}
if (!checkLogCounter()) {
return;
}
logger.warn(msg, t);
}

public void warn(String msg, Object... args) {
if (!logger.isWarnEnabled()) {
return;
}
if (!checkLogCounter()) {
return;
}
logger.warn(msg, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void run() {
} catch (RejectedExecutionException ree) {
// Defense code, need log ?
scheduleListener.onRejectedExecution();
// logger.warn("Failed to request. Rejected execution, count={}", scheduleListener.getRejectedExecutionCount());
logger.warn("Failed to request. Rejected execution, count={}", scheduleListener.getRejectedExecutionCount());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ protected ChannelFactoryBuilder newChannelFactoryBuilder() {

final ClientInterceptor unaryCallDeadlineInterceptor = new UnaryCallDeadlineInterceptor(grpcTransportConfig.getSpanRequestTimeout());
channelFactoryBuilder.addClientInterceptor(unaryCallDeadlineInterceptor);
final ClientInterceptor discardClientInterceptor = newDiscardClientInterceptor();
channelFactoryBuilder.addClientInterceptor(discardClientInterceptor);
// final ClientInterceptor discardClientInterceptor = newDiscardClientInterceptor();
// channelFactoryBuilder.addClientInterceptor(discardClientInterceptor);

channelFactoryBuilder.setExecutorQueueSize(channelExecutorQueueSize);
channelFactoryBuilder.setClientOption(clientOption);
Expand Down
Loading

0 comments on commit eabce0b

Please sign in to comment.