Finish codes about sync dictionary of Application and ServiceName.
This commit is contained in:
parent
046102ef31
commit
be32ab2223
|
|
@ -4,6 +4,10 @@ import io.netty.util.internal.ConcurrentSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import org.skywalking.apm.network.proto.Application;
|
||||||
|
import org.skywalking.apm.network.proto.ApplicationMapping;
|
||||||
|
import org.skywalking.apm.network.proto.ApplicationRegisterServiceGrpc;
|
||||||
|
import org.skywalking.apm.network.proto.KeyWithIntegerValue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map of application id to application code, which is from the collector side.
|
* Map of application id to application code, which is from the collector side.
|
||||||
|
|
@ -13,15 +17,29 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
public enum ApplicationDictionary {
|
public enum ApplicationDictionary {
|
||||||
INSTANCE;
|
INSTANCE;
|
||||||
private Map<String, Integer> applicationDictionary = new ConcurrentHashMap<String, Integer>();
|
private Map<String, Integer> applicationDictionary = new ConcurrentHashMap<String, Integer>();
|
||||||
private Set<String> unRegisterApplication = new ConcurrentSet<String>();
|
private Set<String> unRegisterApplications = new ConcurrentSet<String>();
|
||||||
|
|
||||||
public PossibleFound find(String applicationCode) {
|
public PossibleFound find(String applicationCode) {
|
||||||
Integer applicationId = applicationDictionary.get(applicationCode);
|
Integer applicationId = applicationDictionary.get(applicationCode);
|
||||||
if (applicationId != null) {
|
if (applicationId != null) {
|
||||||
return new Found(applicationId);
|
return new Found(applicationId);
|
||||||
} else {
|
} else {
|
||||||
unRegisterApplication.add(applicationCode);
|
unRegisterApplications.add(applicationCode);
|
||||||
return new NotFound();
|
return new NotFound();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void syncRemoteDictionary(
|
||||||
|
ApplicationRegisterServiceGrpc.ApplicationRegisterServiceBlockingStub applicationRegisterServiceBlockingStub) {
|
||||||
|
if (unRegisterApplications.size() > 0) {
|
||||||
|
ApplicationMapping applicationMapping = applicationRegisterServiceBlockingStub.register(
|
||||||
|
Application.newBuilder().addAllApplicationCode(unRegisterApplications).build());
|
||||||
|
if (applicationMapping.getApplicationCount() > 0) {
|
||||||
|
for (KeyWithIntegerValue keyWithIntegerValue : applicationMapping.getApplicationList()) {
|
||||||
|
unRegisterApplications.remove(keyWithIntegerValue.getKey());
|
||||||
|
applicationDictionary.put(keyWithIntegerValue.getKey(), keyWithIntegerValue.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,11 @@ import io.netty.util.internal.ConcurrentSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import org.skywalking.apm.network.proto.ServiceNameCollection;
|
||||||
|
import org.skywalking.apm.network.proto.ServiceNameDiscoveryServiceGrpc;
|
||||||
|
import org.skywalking.apm.network.proto.ServiceNameElement;
|
||||||
|
import org.skywalking.apm.network.proto.ServiceNameMappingCollection;
|
||||||
|
import org.skywalking.apm.network.proto.ServiceNameMappingElement;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author wusheng
|
* @author wusheng
|
||||||
|
|
@ -11,7 +16,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
public enum OperationNameDictionary {
|
public enum OperationNameDictionary {
|
||||||
INSTANCE;
|
INSTANCE;
|
||||||
private Map<OperationNameKey, Integer> operationNameDictionary = new ConcurrentHashMap<OperationNameKey, Integer>();
|
private Map<OperationNameKey, Integer> operationNameDictionary = new ConcurrentHashMap<OperationNameKey, Integer>();
|
||||||
private Set<OperationNameKey> unRegisterOperationName = new ConcurrentSet<OperationNameKey>();
|
private Set<OperationNameKey> unRegisterOperationNames = new ConcurrentSet<OperationNameKey>();
|
||||||
|
|
||||||
public PossibleFound find(int applicationId, String operationName) {
|
public PossibleFound find(int applicationId, String operationName) {
|
||||||
OperationNameKey key = new OperationNameKey(applicationId, operationName);
|
OperationNameKey key = new OperationNameKey(applicationId, operationName);
|
||||||
|
|
@ -19,11 +24,35 @@ public enum OperationNameDictionary {
|
||||||
if (operationId != null) {
|
if (operationId != null) {
|
||||||
return new Found(applicationId);
|
return new Found(applicationId);
|
||||||
} else {
|
} else {
|
||||||
unRegisterOperationName.add(key);
|
unRegisterOperationNames.add(key);
|
||||||
return new NotFound();
|
return new NotFound();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void syncRemoteDictionary(
|
||||||
|
ServiceNameDiscoveryServiceGrpc.ServiceNameDiscoveryServiceBlockingStub serviceNameDiscoveryServiceBlockingStub) {
|
||||||
|
if (unRegisterOperationNames.size() > 0) {
|
||||||
|
ServiceNameCollection.Builder builder = ServiceNameCollection.newBuilder();
|
||||||
|
for (OperationNameKey operationNameKey : unRegisterOperationNames) {
|
||||||
|
ServiceNameElement serviceNameElement = ServiceNameElement.newBuilder()
|
||||||
|
.setApplicationId(operationNameKey.getApplicationId())
|
||||||
|
.setServiceName(operationNameKey.getOperationName())
|
||||||
|
.build();
|
||||||
|
builder.addElements(serviceNameElement);
|
||||||
|
}
|
||||||
|
ServiceNameMappingCollection serviceNameMappingCollection = serviceNameDiscoveryServiceBlockingStub.discovery(builder.build());
|
||||||
|
if (serviceNameMappingCollection.getElementsCount() > 0) {
|
||||||
|
for (ServiceNameMappingElement serviceNameMappingElement : serviceNameMappingCollection.getElementsList()) {
|
||||||
|
OperationNameKey key = new OperationNameKey(
|
||||||
|
serviceNameMappingElement.getElement().getApplicationId(),
|
||||||
|
serviceNameMappingElement.getElement().getServiceName());
|
||||||
|
unRegisterOperationNames.remove(key);
|
||||||
|
operationNameDictionary.put(key, serviceNameMappingElement.getServiceId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private class OperationNameKey {
|
private class OperationNameKey {
|
||||||
private int applicationId;
|
private int applicationId;
|
||||||
private String operationName;
|
private String operationName;
|
||||||
|
|
@ -33,6 +62,14 @@ public enum OperationNameDictionary {
|
||||||
this.operationName = operationName;
|
this.operationName = operationName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getApplicationId() {
|
||||||
|
return applicationId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOperationName() {
|
||||||
|
return operationName;
|
||||||
|
}
|
||||||
|
|
||||||
@Override public boolean equals(Object o) {
|
@Override public boolean equals(Object o) {
|
||||||
if (this == o)
|
if (this == o)
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,136 @@
|
||||||
|
package org.skywalking.apm.agent.core.remote;
|
||||||
|
|
||||||
|
import io.grpc.ManagedChannel;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import org.skywalking.apm.agent.core.boot.BootService;
|
||||||
|
import org.skywalking.apm.agent.core.boot.ServiceManager;
|
||||||
|
import org.skywalking.apm.agent.core.conf.Config;
|
||||||
|
import org.skywalking.apm.agent.core.conf.RemoteDownstreamConfig;
|
||||||
|
import org.skywalking.apm.agent.core.context.TracingContext;
|
||||||
|
import org.skywalking.apm.agent.core.context.TracingContextListener;
|
||||||
|
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
|
||||||
|
import org.skywalking.apm.agent.core.dictionary.ApplicationDictionary;
|
||||||
|
import org.skywalking.apm.agent.core.dictionary.DictionaryUtil;
|
||||||
|
import org.skywalking.apm.agent.core.dictionary.OperationNameDictionary;
|
||||||
|
import org.skywalking.apm.logging.ILog;
|
||||||
|
import org.skywalking.apm.logging.LogManager;
|
||||||
|
import org.skywalking.apm.network.proto.Application;
|
||||||
|
import org.skywalking.apm.network.proto.ApplicationInstance;
|
||||||
|
import org.skywalking.apm.network.proto.ApplicationInstanceHeartbeat;
|
||||||
|
import org.skywalking.apm.network.proto.ApplicationInstanceMapping;
|
||||||
|
import org.skywalking.apm.network.proto.ApplicationInstanceRecover;
|
||||||
|
import org.skywalking.apm.network.proto.ApplicationMapping;
|
||||||
|
import org.skywalking.apm.network.proto.ApplicationRegisterServiceGrpc;
|
||||||
|
import org.skywalking.apm.network.proto.InstanceDiscoveryServiceGrpc;
|
||||||
|
import org.skywalking.apm.network.proto.ServiceNameDiscoveryServiceGrpc;
|
||||||
|
|
||||||
|
import static org.skywalking.apm.agent.core.remote.GRPCChannelStatus.CONNECTED;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wusheng
|
||||||
|
*/
|
||||||
|
public class AppAndServiceRegisterClient implements BootService, GRPCChannelListener, Runnable, TracingContextListener {
|
||||||
|
private static final ILog logger = LogManager.getLogger(AppAndServiceRegisterClient.class);
|
||||||
|
|
||||||
|
private volatile GRPCChannelStatus status = GRPCChannelStatus.DISCONNECT;
|
||||||
|
private volatile ApplicationRegisterServiceGrpc.ApplicationRegisterServiceBlockingStub applicationRegisterServiceBlockingStub;
|
||||||
|
private volatile InstanceDiscoveryServiceGrpc.InstanceDiscoveryServiceBlockingStub instanceDiscoveryServiceBlockingStub;
|
||||||
|
private volatile ServiceNameDiscoveryServiceGrpc.ServiceNameDiscoveryServiceBlockingStub serviceNameDiscoveryServiceBlockingStub;
|
||||||
|
private volatile ScheduledFuture<?> applicationRegisterFuture;
|
||||||
|
private volatile boolean needRegisterRecover = false;
|
||||||
|
private volatile long lastSegmentTime = -1;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void statusChanged(GRPCChannelStatus status) {
|
||||||
|
if (CONNECTED.equals(status)) {
|
||||||
|
ManagedChannel channel = ServiceManager.INSTANCE.findService(GRPCChannelManager.class).getManagedChannel();
|
||||||
|
applicationRegisterServiceBlockingStub = ApplicationRegisterServiceGrpc.newBlockingStub(channel);
|
||||||
|
instanceDiscoveryServiceBlockingStub = InstanceDiscoveryServiceGrpc.newBlockingStub(channel);
|
||||||
|
if (RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID != DictionaryUtil.nullValue()) {
|
||||||
|
needRegisterRecover = true;
|
||||||
|
}
|
||||||
|
serviceNameDiscoveryServiceBlockingStub = ServiceNameDiscoveryServiceGrpc.newBlockingStub(channel);
|
||||||
|
} else {
|
||||||
|
applicationRegisterServiceBlockingStub = null;
|
||||||
|
instanceDiscoveryServiceBlockingStub = null;
|
||||||
|
serviceNameDiscoveryServiceBlockingStub = null;
|
||||||
|
}
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void beforeBoot() throws Throwable {
|
||||||
|
ServiceManager.INSTANCE.findService(GRPCChannelManager.class).addChannelListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void boot() throws Throwable {
|
||||||
|
applicationRegisterFuture = Executors
|
||||||
|
.newSingleThreadScheduledExecutor()
|
||||||
|
.scheduleAtFixedRate(this, 0, 10, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterBoot() throws Throwable {
|
||||||
|
TracingContext.ListenerManager.add(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (CONNECTED.equals(status)) {
|
||||||
|
try {
|
||||||
|
if (RemoteDownstreamConfig.Agent.APPLICATION_ID == DictionaryUtil.nullValue()) {
|
||||||
|
if (applicationRegisterServiceBlockingStub != null) {
|
||||||
|
ApplicationMapping applicationMapping = applicationRegisterServiceBlockingStub.register(
|
||||||
|
Application.newBuilder().addApplicationCode(Config.Agent.APPLICATION_CODE).build());
|
||||||
|
if (applicationMapping.getApplicationCount() > 0) {
|
||||||
|
RemoteDownstreamConfig.Agent.APPLICATION_ID = applicationMapping.getApplication(0).getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (instanceDiscoveryServiceBlockingStub != null) {
|
||||||
|
if (RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID == DictionaryUtil.nullValue()) {
|
||||||
|
|
||||||
|
ApplicationInstanceMapping instanceMapping = instanceDiscoveryServiceBlockingStub.register(ApplicationInstance.newBuilder()
|
||||||
|
.setApplicationId(RemoteDownstreamConfig.Agent.APPLICATION_ID)
|
||||||
|
.setRegisterTime(System.currentTimeMillis())
|
||||||
|
.build());
|
||||||
|
if (instanceMapping.getApplicationInstanceId() != DictionaryUtil.nullValue()) {
|
||||||
|
RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID
|
||||||
|
= instanceMapping.getApplicationInstanceId();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (needRegisterRecover) {
|
||||||
|
instanceDiscoveryServiceBlockingStub.registerRecover(ApplicationInstanceRecover.newBuilder()
|
||||||
|
.setApplicationId(RemoteDownstreamConfig.Agent.APPLICATION_ID)
|
||||||
|
.setApplicationInstanceId(RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID)
|
||||||
|
.setRegisterTime(System.currentTimeMillis())
|
||||||
|
.build());
|
||||||
|
} else {
|
||||||
|
if (lastSegmentTime - System.currentTimeMillis() > 60 * 1000) {
|
||||||
|
instanceDiscoveryServiceBlockingStub.heartbeat(ApplicationInstanceHeartbeat.newBuilder()
|
||||||
|
.setApplicationInstanceId(RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID)
|
||||||
|
.setHeartbeatTime(System.currentTimeMillis())
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ApplicationDictionary.INSTANCE.syncRemoteDictionary(applicationRegisterServiceBlockingStub);
|
||||||
|
OperationNameDictionary.INSTANCE.syncRemoteDictionary(serviceNameDiscoveryServiceBlockingStub);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Throwable t) {
|
||||||
|
logger.error(t, "AppAndServiceRegisterClient execute fail.");
|
||||||
|
ServiceManager.INSTANCE.findService(GRPCChannelManager.class).reportError(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterFinished(TraceSegment traceSegment) {
|
||||||
|
lastSegmentTime = System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,119 +0,0 @@
|
||||||
package org.skywalking.apm.agent.core.remote;
|
|
||||||
|
|
||||||
import io.grpc.ManagedChannel;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.ScheduledFuture;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import org.skywalking.apm.agent.core.boot.BootService;
|
|
||||||
import org.skywalking.apm.agent.core.boot.ServiceManager;
|
|
||||||
import org.skywalking.apm.agent.core.conf.Config;
|
|
||||||
import org.skywalking.apm.agent.core.conf.RemoteDownstreamConfig;
|
|
||||||
import org.skywalking.apm.agent.core.context.TracingContext;
|
|
||||||
import org.skywalking.apm.agent.core.context.TracingContextListener;
|
|
||||||
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
|
|
||||||
import org.skywalking.apm.agent.core.dictionary.DictionaryUtil;
|
|
||||||
import org.skywalking.apm.network.proto.Application;
|
|
||||||
import org.skywalking.apm.network.proto.ApplicationInstance;
|
|
||||||
import org.skywalking.apm.network.proto.ApplicationInstanceHeartbeat;
|
|
||||||
import org.skywalking.apm.network.proto.ApplicationInstanceMapping;
|
|
||||||
import org.skywalking.apm.network.proto.ApplicationInstanceRecover;
|
|
||||||
import org.skywalking.apm.network.proto.ApplicationMapping;
|
|
||||||
import org.skywalking.apm.network.proto.ApplicationRegisterServiceGrpc;
|
|
||||||
import org.skywalking.apm.network.proto.InstanceDiscoveryServiceGrpc;
|
|
||||||
|
|
||||||
import static org.skywalking.apm.agent.core.remote.GRPCChannelStatus.CONNECTED;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author wusheng
|
|
||||||
*/
|
|
||||||
public class ApplicationRegisterClient implements BootService, GRPCChannelListener, Runnable, TracingContextListener {
|
|
||||||
private volatile GRPCChannelStatus status = GRPCChannelStatus.DISCONNECT;
|
|
||||||
private volatile ApplicationRegisterServiceGrpc.ApplicationRegisterServiceBlockingStub applicationRegisterServiceBlockingStub;
|
|
||||||
private volatile InstanceDiscoveryServiceGrpc.InstanceDiscoveryServiceBlockingStub instanceDiscoveryServiceBlockingStub;
|
|
||||||
private volatile ScheduledFuture<?> applicationRegisterFuture;
|
|
||||||
private volatile boolean needRegisterRecover = false;
|
|
||||||
private volatile long lastSegmentTime = -1;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void statusChanged(GRPCChannelStatus status) {
|
|
||||||
if (CONNECTED.equals(status)) {
|
|
||||||
ManagedChannel channel = ServiceManager.INSTANCE.findService(GRPCChannelManager.class).getManagedChannel();
|
|
||||||
if (RemoteDownstreamConfig.Agent.APPLICATION_ID == DictionaryUtil.nullValue()) {
|
|
||||||
applicationRegisterServiceBlockingStub = ApplicationRegisterServiceGrpc.newBlockingStub(channel);
|
|
||||||
} else {
|
|
||||||
instanceDiscoveryServiceBlockingStub = InstanceDiscoveryServiceGrpc.newBlockingStub(channel);
|
|
||||||
if (RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID != DictionaryUtil.nullValue()) {
|
|
||||||
needRegisterRecover = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
applicationRegisterServiceBlockingStub = null;
|
|
||||||
}
|
|
||||||
this.status = status;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void beforeBoot() throws Throwable {
|
|
||||||
ServiceManager.INSTANCE.findService(GRPCChannelManager.class).addChannelListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void boot() throws Throwable {
|
|
||||||
applicationRegisterFuture = Executors
|
|
||||||
.newSingleThreadScheduledExecutor()
|
|
||||||
.scheduleAtFixedRate(this, 0, 10, TimeUnit.SECONDS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterBoot() throws Throwable {
|
|
||||||
TracingContext.ListenerManager.add(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (CONNECTED.equals(status)) {
|
|
||||||
if (RemoteDownstreamConfig.Agent.APPLICATION_ID == DictionaryUtil.nullValue()) {
|
|
||||||
if (applicationRegisterServiceBlockingStub != null) {
|
|
||||||
ApplicationMapping applicationMapping = applicationRegisterServiceBlockingStub.register(
|
|
||||||
Application.newBuilder().addApplicationCode(Config.Agent.APPLICATION_CODE).build());
|
|
||||||
if (applicationMapping.getApplicationCount() > 0) {
|
|
||||||
RemoteDownstreamConfig.Agent.APPLICATION_ID = applicationMapping.getApplication(0).getValue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID == DictionaryUtil.nullValue()) {
|
|
||||||
if (instanceDiscoveryServiceBlockingStub != null) {
|
|
||||||
ApplicationInstanceMapping instanceMapping = instanceDiscoveryServiceBlockingStub.register(ApplicationInstance.newBuilder()
|
|
||||||
.setApplicationId(RemoteDownstreamConfig.Agent.APPLICATION_ID)
|
|
||||||
.setRegisterTime(System.currentTimeMillis())
|
|
||||||
.build());
|
|
||||||
if (instanceMapping.getApplicationInstanceId() != DictionaryUtil.nullValue()) {
|
|
||||||
RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID
|
|
||||||
= instanceMapping.getApplicationInstanceId();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (needRegisterRecover) {
|
|
||||||
instanceDiscoveryServiceBlockingStub.registerRecover(ApplicationInstanceRecover.newBuilder()
|
|
||||||
.setApplicationId(RemoteDownstreamConfig.Agent.APPLICATION_ID)
|
|
||||||
.setApplicationInstanceId(RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID)
|
|
||||||
.setRegisterTime(System.currentTimeMillis())
|
|
||||||
.build());
|
|
||||||
} else {
|
|
||||||
if (lastSegmentTime - System.currentTimeMillis() > 60 * 1000) {
|
|
||||||
instanceDiscoveryServiceBlockingStub.heartbeat(ApplicationInstanceHeartbeat.newBuilder()
|
|
||||||
.setApplicationInstanceId(RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID)
|
|
||||||
.setHeartbeatTime(System.currentTimeMillis())
|
|
||||||
.build());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterFinished(TraceSegment traceSegment) {
|
|
||||||
lastSegmentTime = System.currentTimeMillis();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -10,6 +10,9 @@ import java.util.Collections;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import org.skywalking.apm.agent.core.boot.BootService;
|
import org.skywalking.apm.agent.core.boot.BootService;
|
||||||
import org.skywalking.apm.agent.core.conf.RemoteDownstreamConfig;
|
import org.skywalking.apm.agent.core.conf.RemoteDownstreamConfig;
|
||||||
import org.skywalking.apm.logging.ILog;
|
import org.skywalking.apm.logging.ILog;
|
||||||
|
|
@ -21,11 +24,12 @@ import org.skywalking.apm.logging.LogManager;
|
||||||
public class GRPCChannelManager implements BootService, Runnable {
|
public class GRPCChannelManager implements BootService, Runnable {
|
||||||
private static final ILog logger = LogManager.getLogger(DiscoveryRestServiceClient.class);
|
private static final ILog logger = LogManager.getLogger(DiscoveryRestServiceClient.class);
|
||||||
|
|
||||||
private volatile Thread channelManagerThread = null;
|
|
||||||
private volatile ManagedChannel managedChannel = null;
|
private volatile ManagedChannel managedChannel = null;
|
||||||
private volatile long nextStartTime = 0;
|
private volatile ScheduledFuture<?> connectCheckFuture;
|
||||||
|
private volatile boolean reconnect = true;
|
||||||
private Random random = new Random();
|
private Random random = new Random();
|
||||||
private List<GRPCChannelListener> listeners = Collections.synchronizedList(new LinkedList<GRPCChannelListener>());
|
private List<GRPCChannelListener> listeners = Collections.synchronizedList(new LinkedList<GRPCChannelListener>());
|
||||||
|
private final int retryCycle = 30;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeBoot() throws Throwable {
|
public void beforeBoot() throws Throwable {
|
||||||
|
|
@ -34,7 +38,9 @@ public class GRPCChannelManager implements BootService, Runnable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void boot() throws Throwable {
|
public void boot() throws Throwable {
|
||||||
this.connectInBackground(false);
|
connectCheckFuture = Executors
|
||||||
|
.newSingleThreadScheduledExecutor()
|
||||||
|
.scheduleAtFixedRate(this, 0, retryCycle, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -42,38 +48,9 @@ public class GRPCChannelManager implements BootService, Runnable {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void connectInBackground(boolean forceStart) {
|
|
||||||
if (channelManagerThread == null || !channelManagerThread.isAlive()) {
|
|
||||||
synchronized (this) {
|
|
||||||
if (forceStart) {
|
|
||||||
/**
|
|
||||||
* The startup has invoked in 30 seconds before, don't invoke again.
|
|
||||||
*/
|
|
||||||
if (System.currentTimeMillis() < nextStartTime) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resetNextStartTime();
|
|
||||||
if (channelManagerThread == null || !channelManagerThread.isAlive()) {
|
|
||||||
if (forceStart || managedChannel == null || managedChannel.isTerminated() || managedChannel.isShutdown()) {
|
|
||||||
if (managedChannel != null) {
|
|
||||||
managedChannel.shutdownNow();
|
|
||||||
notify(GRPCChannelStatus.DISCONNECT);
|
|
||||||
}
|
|
||||||
Thread channelManagerThread = new Thread(this, "ChannelManagerThread");
|
|
||||||
channelManagerThread.setDaemon(true);
|
|
||||||
channelManagerThread.start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (true) {
|
if (reconnect) {
|
||||||
resetNextStartTime();
|
|
||||||
|
|
||||||
if (RemoteDownstreamConfig.Collector.GRPC_SERVERS.size() > 0) {
|
if (RemoteDownstreamConfig.Collector.GRPC_SERVERS.size() > 0) {
|
||||||
int index = random.nextInt() % RemoteDownstreamConfig.Collector.GRPC_SERVERS.size();
|
int index = random.nextInt() % RemoteDownstreamConfig.Collector.GRPC_SERVERS.size();
|
||||||
String server = RemoteDownstreamConfig.Collector.GRPC_SERVERS.get(index);
|
String server = RemoteDownstreamConfig.Collector.GRPC_SERVERS.get(index);
|
||||||
|
|
@ -85,17 +62,15 @@ public class GRPCChannelManager implements BootService, Runnable {
|
||||||
.maxInboundMessageSize(1024 * 1024 * 50)
|
.maxInboundMessageSize(1024 * 1024 * 50)
|
||||||
.usePlaintext(true);
|
.usePlaintext(true);
|
||||||
managedChannel = channelBuilder.build();
|
managedChannel = channelBuilder.build();
|
||||||
|
reconnect = false;
|
||||||
notify(GRPCChannelStatus.CONNECTED);
|
notify(GRPCChannelStatus.CONNECTED);
|
||||||
break;
|
return;
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
logger.error(t, "Create channel to {} fail.", server);
|
logger.error(t, "Create channel to {} fail.", server);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resetNextStartTime();
|
logger.debug("Selected collector grpc service is not available. Wait {} seconds to retry", retryCycle);
|
||||||
int waitTime = 5 * 1000;
|
|
||||||
logger.debug("Selected collector grpc service is not available. Wait {} millis to try", waitTime);
|
|
||||||
try2Sleep(waitTime);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -114,13 +89,17 @@ public class GRPCChannelManager implements BootService, Runnable {
|
||||||
*/
|
*/
|
||||||
public void reportError(Throwable throwable) {
|
public void reportError(Throwable throwable) {
|
||||||
if (isNetworkError(throwable)) {
|
if (isNetworkError(throwable)) {
|
||||||
this.connectInBackground(true);
|
reconnect = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void notify(GRPCChannelStatus status) {
|
private void notify(GRPCChannelStatus status) {
|
||||||
for (GRPCChannelListener listener : listeners) {
|
for (GRPCChannelListener listener : listeners) {
|
||||||
listener.statusChanged(status);
|
try {
|
||||||
|
listener.statusChanged(status);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
logger.error(t, "Fail to notify {} about channel connected.", listener.getClass().getName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -146,21 +125,4 @@ public class GRPCChannelManager implements BootService, Runnable {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void resetNextStartTime() {
|
|
||||||
nextStartTime = System.currentTimeMillis() + 20 * 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Try to sleep, and ignore the {@link InterruptedException}
|
|
||||||
*
|
|
||||||
* @param millis the length of time to sleep in milliseconds
|
|
||||||
*/
|
|
||||||
private void try2Sleep(long millis) {
|
|
||||||
try {
|
|
||||||
Thread.sleep(millis);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,4 +4,4 @@ org.skywalking.apm.agent.core.remote.CollectorDiscoveryService
|
||||||
org.skywalking.apm.agent.core.sampling.SamplingService
|
org.skywalking.apm.agent.core.sampling.SamplingService
|
||||||
org.skywalking.apm.agent.core.remote.GRPCChannelManager
|
org.skywalking.apm.agent.core.remote.GRPCChannelManager
|
||||||
org.skywalking.apm.agent.core.jvm.JVMService
|
org.skywalking.apm.agent.core.jvm.JVMService
|
||||||
org.skywalking.apm.agent.core.remote.ApplicationRegisterClient
|
org.skywalking.apm.agent.core.remote.AppAndServiceRegisterClient
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue