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);
}
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);
}
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;
}
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;
}
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);
}
}
Aggregations