Search in sources :

Example 1 with DefaultInstance

use of com.tencent.polaris.api.pojo.DefaultInstance 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 2 with DefaultInstance

use of com.tencent.polaris.api.pojo.DefaultInstance in project spring-cloud-tencent by Tencent.

the class PolarisLoadBalancerCompositeRuleTest method assembleServiceInstances.

private ServiceInstances assembleServiceInstances() {
    ServiceKey serviceKey = new ServiceKey(testNamespace, testCalleeService);
    List<Instance> instances = new LinkedList<>();
    instances.add(new DefaultInstance());
    instances.add(new DefaultInstance());
    instances.add(new DefaultInstance());
    instances.add(new DefaultInstance());
    instances.add(new DefaultInstance());
    return new DefaultServiceInstances(serviceKey, instances);
}
Also used : DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) Instance(com.tencent.polaris.api.pojo.Instance) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) LinkedList(java.util.LinkedList)

Example 3 with DefaultInstance

use of com.tencent.polaris.api.pojo.DefaultInstance in project spring-cloud-tencent by Tencent.

the class PolarisLoadBalancer method getExtendDiscoveryServiceInstances.

private ServiceInstances getExtendDiscoveryServiceInstances() {
    List<Server> allServers = super.getAllServers();
    if (CollectionUtils.isEmpty(allServers)) {
        return null;
    }
    ServiceInstances serviceInstances;
    if (StringUtils.isBlank(name)) {
        throw new IllegalStateException("PolarisLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute");
    }
    ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, name);
    List<Instance> instances = new ArrayList<>(8);
    for (Server server : allServers) {
        DefaultInstance instance = new DefaultInstance();
        instance.setNamespace(MetadataContext.LOCAL_NAMESPACE);
        instance.setService(name);
        instance.setHealthy(server.isAlive());
        instance.setProtocol(server.getScheme());
        instance.setId(server.getId());
        instance.setHost(server.getHost());
        instance.setPort(server.getPort());
        instance.setZone(server.getZone());
        instance.setWeight(100);
        instances.add(instance);
    }
    serviceInstances = new DefaultServiceInstances(serviceKey, instances);
    return serviceInstances;
}
Also used : DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) Server(com.netflix.loadbalancer.Server) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) Instance(com.tencent.polaris.api.pojo.Instance) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) ArrayList(java.util.ArrayList) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances)

Example 4 with DefaultInstance

use of com.tencent.polaris.api.pojo.DefaultInstance in project spring-cloud-tencent by Tencent.

the class PolarisLoadBalancerCompositeRuleTest method assembleServers.

private List<Server> assembleServers() {
    ServiceInstances serviceInstances = assembleServiceInstances();
    List<Server> servers = new LinkedList<>();
    servers.add(new PolarisServer(serviceInstances, new DefaultInstance()));
    servers.add(new PolarisServer(serviceInstances, new DefaultInstance()));
    servers.add(new PolarisServer(serviceInstances, new DefaultInstance()));
    servers.add(new PolarisServer(serviceInstances, new DefaultInstance()));
    return servers;
}
Also used : DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) Server(com.netflix.loadbalancer.Server) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) LinkedList(java.util.LinkedList)

Example 5 with DefaultInstance

use of com.tencent.polaris.api.pojo.DefaultInstance in project spring-cloud-tencent by Tencent.

the class PolarisServiceStatusChangeListenerTest method testOnResourceUpdated.

@Test
public void testOnResourceUpdated() {
    PolarisServiceStatusChangeListener polarisServiceStatusChangeListener = new PolarisServiceStatusChangeListener();
    polarisServiceStatusChangeListener.setApplicationEventPublisher(publisher);
    // Service update event
    ServiceEventKey serviceUpdateEventKey = new ServiceEventKey(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER), ServiceEventKey.EventType.SERVICE);
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setNamespace(NAMESPACE_TEST);
    serviceInfo.setService(SERVICE_PROVIDER);
    // Need update
    ServicesByProto oldServices = new ServicesByProto(Collections.emptyList());
    ServicesByProto newServices = new ServicesByProto(Collections.singletonList(serviceInfo));
    polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldServices, newServices);
    verify(publisher, times(1)).publishEvent(any(ApplicationEvent.class));
    // No need update
    oldServices = new ServicesByProto(Collections.singletonList(serviceInfo));
    newServices = new ServicesByProto(Collections.singletonList(serviceInfo));
    polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldServices, newServices);
    verify(publisher, times(1)).publishEvent(any(ApplicationEvent.class));
    // Instance update event
    ServiceEventKey instanceUpdateEventKey = new ServiceEventKey(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER), ServiceEventKey.EventType.INSTANCE);
    DefaultInstance instance = new DefaultInstance();
    instance.setNamespace(NAMESPACE_TEST);
    instance.setService(SERVICE_PROVIDER);
    instance.setHost(HOST);
    instance.setPort(PORT);
    try {
        Field instances = ServiceInstancesByProto.class.getDeclaredField("instances");
        instances.setAccessible(true);
        // Need update
        ServiceInstancesByProto oldInstances = new ServiceInstancesByProto();
        instances.set(oldInstances, Collections.emptyList());
        ServiceInstancesByProto newInstances = new ServiceInstancesByProto();
        instances.set(newInstances, Collections.singletonList(instance));
        polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldInstances, newInstances);
        verify(publisher, times(2)).publishEvent(any(ApplicationEvent.class));
        // No need update
        oldInstances = new ServiceInstancesByProto();
        instances.set(oldInstances, Collections.singletonList(instance));
        newInstances = new ServiceInstancesByProto();
        instances.set(newInstances, Collections.singletonList(instance));
        polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldInstances, newInstances);
        verify(publisher, times(2)).publishEvent(any(ApplicationEvent.class));
    } catch (NoSuchFieldException | IllegalAccessException e) {
        Assertions.fail("Exception encountered.", e);
    }
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) Field(java.lang.reflect.Field) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) ApplicationEvent(org.springframework.context.ApplicationEvent) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto) ServicesByProto(com.tencent.polaris.client.pojo.ServicesByProto) Test(org.junit.Test)

Aggregations

DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)7 DefaultServiceInstances (com.tencent.polaris.api.pojo.DefaultServiceInstances)4 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)4 ArrayList (java.util.ArrayList)4 Server (com.netflix.loadbalancer.Server)3 Instance (com.tencent.polaris.api.pojo.Instance)3 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)3 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)3 PolarisServer (com.tencent.cloud.common.pojo.PolarisServer)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 ConsulException (com.ecwid.consul.ConsulException)1 QueryParams (com.ecwid.consul.v1.QueryParams)1 HealthServicesRequest (com.ecwid.consul.v1.health.HealthServicesRequest)1 HealthService (com.ecwid.consul.v1.health.model.HealthService)1 PolarisServer (com.tencent.cloud.polaris.pojo.PolarisServer)1 PolarisException (com.tencent.polaris.api.exception.PolarisException)1 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)1 Services (com.tencent.polaris.api.pojo.Services)1 DiscoverResponse (com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse)1