Search in sources :

Example 1 with DestroyableServerConnector

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;
}
Also used : CommonProviderResponse(com.tencent.polaris.api.plugin.server.CommonProviderResponse) DestroyableServerConnector(com.tencent.polaris.plugins.connector.common.DestroyableServerConnector)

Example 2 with DestroyableServerConnector

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;
}
Also used : ReportClientResponse(com.tencent.polaris.api.plugin.server.ReportClientResponse) DestroyableServerConnector(com.tencent.polaris.plugins.connector.common.DestroyableServerConnector)

Example 3 with DestroyableServerConnector

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);
}
Also used : DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) Instance(com.tencent.polaris.client.pb.ServiceProto.Instance) ArrayList(java.util.ArrayList) Service(com.tencent.polaris.client.pb.ServiceProto.Service) DiscoverResponse(com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse) ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) PolarisException(com.tencent.polaris.api.exception.PolarisException) Services(com.tencent.polaris.api.pojo.Services) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) ArrayList(java.util.ArrayList) List(java.util.List) DestroyableServerConnector(com.tencent.polaris.plugins.connector.common.DestroyableServerConnector)

Example 4 with DestroyableServerConnector

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);
        }
    }
}
Also used : DestroyableServerConnector(com.tencent.polaris.plugins.connector.common.DestroyableServerConnector) GrpcServiceUpdateTask(com.tencent.polaris.plugins.connector.grpc.GrpcServiceUpdateTask)

Example 5 with DestroyableServerConnector

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;
    }
}
Also used : ServerConnectorConfigImpl(com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) NamedThreadFactory(com.tencent.polaris.client.util.NamedThreadFactory) CallerRunsPolicy(java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy) DestroyableServerConnector(com.tencent.polaris.plugins.connector.common.DestroyableServerConnector)

Aggregations

DestroyableServerConnector (com.tencent.polaris.plugins.connector.common.DestroyableServerConnector)5 PolarisException (com.tencent.polaris.api.exception.PolarisException)1 CommonProviderResponse (com.tencent.polaris.api.plugin.server.CommonProviderResponse)1 ReportClientResponse (com.tencent.polaris.api.plugin.server.ReportClientResponse)1 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)1 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)1 Services (com.tencent.polaris.api.pojo.Services)1 DiscoverResponse (com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse)1 Instance (com.tencent.polaris.client.pb.ServiceProto.Instance)1 Service (com.tencent.polaris.client.pb.ServiceProto.Service)1 NamedThreadFactory (com.tencent.polaris.client.util.NamedThreadFactory)1 ServerConnectorConfigImpl (com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl)1 GrpcServiceUpdateTask (com.tencent.polaris.plugins.connector.grpc.GrpcServiceUpdateTask)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 CallerRunsPolicy (java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy)1