Search in sources :

Example 1 with Service

use of com.tencent.polaris.client.pb.ServiceProto.Service 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 Service

use of com.tencent.polaris.client.pb.ServiceProto.Service in project polaris-java by polarismesh.

the class CommonHandler method compareMessage.

/**
 * 比较消息
 *
 * @param eventType 类型
 * @param oldValue 旧值
 * @param discoverResponse proto应答
 * @param getRevision 获取版本号
 * @return 状态
 */
public static CachedStatus compareMessage(EventType eventType, RegistryCacheValue oldValue, DiscoverResponse discoverResponse, Function<DiscoverResponse, String> getRevision) {
    Service service = discoverResponse.getService();
    ServiceEventKey serviceEventKey = new ServiceEventKey(new ServiceKey(service.getNamespace().getValue(), service.getName().getValue()), eventType);
    if (discoverResponse.getCode().getValue() == ServerCodes.DATA_NO_CHANGE) {
        if (null == oldValue) {
            return CachedStatus.CacheEmptyButNoData;
        }
        return CachedStatus.CacheNotChanged;
    }
    String newRevision = getRevision.apply(discoverResponse);
    String oldRevision;
    boolean oldLoadedFromFile = false;
    CachedStatus cachedStatus;
    if (null == oldValue) {
        oldRevision = emptyReplaceHolder;
        cachedStatus = CachedStatus.CacheNotExists;
    } else {
        oldLoadedFromFile = oldValue.isLoadedFromFile();
        oldRevision = oldValue.getRevision();
        // revision 值,因此对于 SERVICES 类型的请求,默认直接强制更新 cache 数据
        if (discoverResponse.getType() == DiscoverResponseType.SERVICES) {
            cachedStatus = CachedStatus.CacheChanged;
        } else {
            cachedStatus = oldRevision.equals(newRevision) && !oldLoadedFromFile ? CachedStatus.CacheNotChanged : CachedStatus.CacheChanged;
        }
    }
    if (cachedStatus != CachedStatus.CacheNotChanged) {
        LOG.info("resource {} has updated, compare status {}, old revision is {}, old loadedFromFile is {}, " + "new revision is {}", serviceEventKey, cachedStatus, oldRevision, oldLoadedFromFile, newRevision);
    } else {
        LOG.debug("resource {} is not updated, compare status {}, old revision is {}, old loadedFromFile is {}, " + "new revision is {}", serviceEventKey, cachedStatus, oldRevision, oldLoadedFromFile, newRevision);
    }
    return cachedStatus;
}
Also used : CachedStatus(com.tencent.polaris.api.plugin.registry.CacheHandler.CachedStatus) Service(com.tencent.polaris.client.pb.ServiceProto.Service) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey)

Aggregations

Service (com.tencent.polaris.client.pb.ServiceProto.Service)2 PolarisException (com.tencent.polaris.api.exception.PolarisException)1 CachedStatus (com.tencent.polaris.api.plugin.registry.CacheHandler.CachedStatus)1 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)1 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)1 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)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 DestroyableServerConnector (com.tencent.polaris.plugins.connector.common.DestroyableServerConnector)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1