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