Search in sources :

Example 1 with DiscoverResponse

use of com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse in project polaris-java by polarismesh.

the class CircuitBreakCacheHandler method messageToCacheValue.

@Override
public RegistryCacheValue messageToCacheValue(RegistryCacheValue oldValue, Object newValue, boolean isCacheLoaded) {
    DiscoverResponse discoverResponse = (DiscoverResponse) newValue;
    CircuitBreaker circuitBreaker = discoverResponse.getCircuitBreaker();
    String revision = "";
    if (null != circuitBreaker) {
        revision = circuitBreaker.getRevision().getValue();
    }
    return new ServiceRuleByProto(circuitBreaker, revision, isCacheLoaded, getTargetEventType());
}
Also used : CircuitBreaker(com.tencent.polaris.client.pb.CircuitBreakerProto.CircuitBreaker) DiscoverResponse(com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse) ServiceRuleByProto(com.tencent.polaris.client.pojo.ServiceRuleByProto)

Example 2 with DiscoverResponse

use of com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse 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 3 with DiscoverResponse

use of com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse in project polaris-java by polarismesh.

the class ServiceInstancesCacheHandler method messageToCacheValue.

@Override
public RegistryCacheValue messageToCacheValue(RegistryCacheValue oldValue, Object newValue, boolean isCacheLoaded) {
    DiscoverResponse discoverResponse = (DiscoverResponse) newValue;
    ServiceInstancesByProto oldServiceInstances = null;
    if (null != oldValue) {
        oldServiceInstances = (ServiceInstancesByProto) oldValue;
    }
    return new ServiceInstancesByProto(discoverResponse, oldServiceInstances, isCacheLoaded);
}
Also used : DiscoverResponse(com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto)

Example 4 with DiscoverResponse

use of com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse in project polaris-java by polarismesh.

the class RateLimitingCacheHandler method messageToCacheValue.

@Override
public RegistryCacheValue messageToCacheValue(RegistryCacheValue oldValue, Object newValue, boolean isCacheLoaded) {
    DiscoverResponse discoverResponse = (DiscoverResponse) newValue;
    RateLimit rateLimit = discoverResponse.getRateLimit();
    String revision = getRevision(discoverResponse);
    List<Rule> rulesList = rateLimit.getRulesList();
    // 需要做一次排序,PB中的数据不可变,需要单独构建一份
    List<Rule> sortedRules = new ArrayList<>(rulesList);
    sortedRules.sort((o1, o2) -> {
        if (o1.getPriority().getValue() != o2.getPriority().getValue()) {
            return o1.getPriority().getValue() - o2.getPriority().getValue();
        }
        return o1.getId().getValue().compareTo(o2.getId().getValue());
    });
    RateLimit newRateLimit = RateLimit.newBuilder().addAllRules(sortedRules).setRevision(StringValue.newBuilder().setValue(revision).build()).build();
    return new ServiceRuleByProto(newRateLimit, revision, isCacheLoaded, getTargetEventType());
}
Also used : RateLimit(com.tencent.polaris.client.pb.RateLimitProto.RateLimit) ArrayList(java.util.ArrayList) DiscoverResponse(com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse) Rule(com.tencent.polaris.client.pb.RateLimitProto.Rule) ServiceRuleByProto(com.tencent.polaris.client.pojo.ServiceRuleByProto)

Example 5 with DiscoverResponse

use of com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse in project polaris-java by polarismesh.

the class RoutingCacheHandler method messageToCacheValue.

@Override
public RegistryCacheValue messageToCacheValue(RegistryCacheValue oldValue, Object newValue, boolean isCacheLoaded) {
    DiscoverResponse discoverResponse = (DiscoverResponse) newValue;
    Routing routing = discoverResponse.getRouting();
    String revision = "";
    if (null != routing) {
        revision = routing.getRevision().getValue();
    }
    return new ServiceRuleByProto(routing, revision, isCacheLoaded, getTargetEventType());
}
Also used : Routing(com.tencent.polaris.client.pb.RoutingProto.Routing) DiscoverResponse(com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse) ServiceRuleByProto(com.tencent.polaris.client.pojo.ServiceRuleByProto)

Aggregations

DiscoverResponse (com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse)5 ServiceRuleByProto (com.tencent.polaris.client.pojo.ServiceRuleByProto)3 ArrayList (java.util.ArrayList)2 PolarisException (com.tencent.polaris.api.exception.PolarisException)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 CircuitBreaker (com.tencent.polaris.client.pb.CircuitBreakerProto.CircuitBreaker)1 RateLimit (com.tencent.polaris.client.pb.RateLimitProto.RateLimit)1 Rule (com.tencent.polaris.client.pb.RateLimitProto.Rule)1 Routing (com.tencent.polaris.client.pb.RoutingProto.Routing)1 Instance (com.tencent.polaris.client.pb.ServiceProto.Instance)1 Service (com.tencent.polaris.client.pb.ServiceProto.Service)1 ServiceInstancesByProto (com.tencent.polaris.client.pojo.ServiceInstancesByProto)1 DestroyableServerConnector (com.tencent.polaris.plugins.connector.common.DestroyableServerConnector)1 List (java.util.List)1