use of com.tencent.polaris.plugins.connector.common.DestroyableServerConnector in project polaris-java by polarismesh.
the class CompositeConnector method registerInstance.
@Override
public CommonProviderResponse registerInstance(CommonProviderRequest req) throws PolarisException {
checkDestroyed();
CommonProviderResponse response = null;
for (DestroyableServerConnector sc : serverConnectors) {
CommonProviderResponse temp = sc.registerInstance(req);
if (DefaultPlugins.SERVER_CONNECTOR_GRPC.equals(sc.getName())) {
response = temp;
}
}
return response;
}
use of com.tencent.polaris.plugins.connector.common.DestroyableServerConnector in project polaris-java by polarismesh.
the class CompositeConnector method reportClient.
@Override
public ReportClientResponse reportClient(ReportClientRequest req) throws PolarisException {
checkDestroyed();
ReportClientResponse response = null;
for (DestroyableServerConnector sc : serverConnectors) {
ReportClientResponse temp = sc.reportClient(req);
if (DefaultPlugins.SERVER_CONNECTOR_GRPC.equals(sc.getName())) {
response = temp;
}
}
return response;
}
use of com.tencent.polaris.plugins.connector.common.DestroyableServerConnector in project polaris-java by polarismesh.
the class CompositeServiceUpdateTask method notifyServerEvent.
@Override
public boolean notifyServerEvent(ServerEvent serverEvent) {
taskStatus.compareAndSet(Status.RUNNING, Status.READY);
lastUpdateTime.set(System.currentTimeMillis());
try {
if (serverEvent.getValue() instanceof DiscoverResponse) {
DiscoverResponse discoverResponse = (DiscoverResponse) serverEvent.getValue();
DiscoverResponse.Builder newDiscoverResponseBuilder = DiscoverResponse.newBuilder().mergeFrom(discoverResponse);
CompositeConnector connector = (CompositeConnector) serverConnector;
if (EventType.INSTANCE.equals(serviceEventKey.getEventType())) {
// Get instance information list except polaris.
List<DefaultInstance> extendInstanceList = new ArrayList<>();
for (DestroyableServerConnector sc : connector.getServerConnectors()) {
if (!DefaultPlugins.SERVER_CONNECTOR_GRPC.equals(sc.getName())) {
List<DefaultInstance> instanceList = sc.syncGetServiceInstances(this);
if (extendInstanceList.isEmpty()) {
extendInstanceList.addAll(instanceList);
} else {
// TODO 多数据源合并去重
}
}
}
// Merge instance information list
List<Instance> polarisInstanceList = discoverResponse.getInstancesList();
for (DefaultInstance i : extendInstanceList) {
boolean needAdd = true;
for (Instance j : polarisInstanceList) {
if (i.getHost().equals(j.getHost().getValue()) && i.getPort() == j.getPort().getValue()) {
needAdd = false;
break;
}
}
if (needAdd) {
Instance.Builder instanceBuilder = Instance.newBuilder().setNamespace(StringValue.of(serviceEventKey.getNamespace())).setService(StringValue.of(i.getService())).setHost(StringValue.of(i.getHost())).setPort(UInt32Value.of(i.getPort())).setHealthy(BoolValue.of(true));
if (StringUtils.isNotBlank(i.getId())) {
instanceBuilder.setId(StringValue.of(i.getId()));
}
newDiscoverResponseBuilder.addInstances(instanceBuilder.build());
}
}
if (!newDiscoverResponseBuilder.getInstancesList().isEmpty()) {
serverEvent.setError(null);
}
} else if (EventType.SERVICE.equals(serviceEventKey.getEventType())) {
// Get instance information list except polaris.
List<ServiceInfo> extendServiceList = new ArrayList<>();
for (DestroyableServerConnector sc : connector.getServerConnectors()) {
if (!DefaultPlugins.SERVER_CONNECTOR_GRPC.equals(sc.getName())) {
Services services = sc.syncGetServices(this);
if (extendServiceList.isEmpty()) {
extendServiceList.addAll(services.getServices());
} else {
// TODO 多数据源合并去重
}
}
}
// Merge service information list
List<Service> polarisServiceList = discoverResponse.getServicesList();
for (ServiceInfo i : extendServiceList) {
boolean needAdd = true;
for (Service j : polarisServiceList) {
if (i.getService().equals(j.getName().getValue())) {
needAdd = false;
break;
}
}
if (needAdd) {
Service service = Service.newBuilder().setNamespace(StringValue.of(serviceEventKey.getNamespace())).setName(StringValue.of(i.getService())).build();
newDiscoverResponseBuilder.addServices(service);
}
}
if (!newDiscoverResponseBuilder.getServicesList().isEmpty()) {
serverEvent.setError(null);
}
}
serverEvent.setValue(newDiscoverResponseBuilder.build());
}
} catch (PolarisException e) {
LOG.error("Merge other server response failed.", e);
serverEvent.setError(e);
} catch (Throwable throwable) {
LOG.error("Merge other server response failed.", throwable);
serverEvent.setError(new PolarisException(ErrorCode.INTERNAL_ERROR));
}
if (null == serverEvent.getError()) {
successUpdates.addAndGet(1);
}
return getEventHandler().onEventUpdate(serverEvent);
}
use of com.tencent.polaris.plugins.connector.common.DestroyableServerConnector in project polaris-java by polarismesh.
the class CompositeServiceUpdateTask method execute.
@Override
protected void execute() {
CompositeConnector connector = (CompositeConnector) serverConnector;
for (DestroyableServerConnector sc : connector.getServerConnectors()) {
if (DefaultPlugins.SERVER_CONNECTOR_GRPC.equals(sc.getName())) {
GrpcServiceUpdateTask grpcServiceUpdateTask = new GrpcServiceUpdateTask(serviceEventHandler, sc);
grpcServiceUpdateTask.execute(this);
}
}
}
use of com.tencent.polaris.plugins.connector.common.DestroyableServerConnector in project polaris-java by polarismesh.
the class CompositeConnector method init.
@Override
public void init(InitContext ctx) throws PolarisException {
if (getName().equals(ctx.getValueContext().getServerConnectorProtocol())) {
if (CollectionUtils.isEmpty(serverConnectors)) {
serverConnectors = new ArrayList<>();
}
List<ServerConnectorConfigImpl> serverConnectorConfigs = ctx.getConfig().getGlobal().getServerConnectors();
for (ServerConnectorConfigImpl serverConnectorConfig : serverConnectorConfigs) {
DestroyableServerConnector serverConnector = (DestroyableServerConnector) ctx.getPlugins().getPlugin(PluginTypes.SERVER_CONNECTOR.getBaseType(), serverConnectorConfig.getProtocol());
serverConnector.init(ctx);
serverConnectors.add(serverConnector);
}
sendDiscoverExecutor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory(getName() + "-send-discovery"), new CallerRunsPolicy());
sendDiscoverExecutor.setMaximumPoolSize(1);
updateServiceExecutor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory(getName() + "-update-service"));
updateServiceExecutor.setMaximumPoolSize(1);
initialized = true;
}
}
Aggregations