use of com.tencent.polaris.api.pojo.RegistryCacheValue in project polaris-java by polarismesh.
the class CacheObject method onEventUpdate.
@Override
public boolean onEventUpdate(ServerEvent event) {
ServiceEventKey serviceEventKey = event.getServiceEventKey();
PolarisException error = event.getError();
remoteUpdated.set(true);
boolean svcDeleted = false;
Collection<ResourceEventListener> resourceEventListeners = registry.getResourceEventListeners();
if (null != error) {
// 收取消息有出错
RegistryCacheValue registryCacheValue = loadValue(false);
// 没有服务信息直接删除
if (error.getCode() == ErrorCode.SERVICE_NOT_FOUND) {
if (deleted.compareAndSet(false, true)) {
registry.removeCache(serviceEventKey);
for (ResourceEventListener listener : resourceEventListeners) {
listener.onResourceDeleted(svcEventKey, registryCacheValue);
}
svcDeleted = true;
}
} else {
LOG.error(String.format("received error notify for service %s", serviceEventKey), error);
}
} else {
Object message = event.getValue();
RegistryCacheValue cachedValue = value.get();
CachedStatus cachedStatus = cacheHandler.compareMessage(cachedValue, message);
if (cachedStatus == CachedStatus.CacheChanged || cachedStatus == CachedStatus.CacheNotExists) {
LOG.info("OnServiceUpdate: cache {} is pending to update", svcEventKey);
this.registry.saveMessageToFile(serviceEventKey, (Message) message);
RegistryCacheValue newCachedValue = cacheHandler.messageToCacheValue(cachedValue, message, false);
setValue(newCachedValue);
if (cachedStatus == CachedStatus.CacheChanged) {
for (ResourceEventListener listener : resourceEventListeners) {
listener.onResourceUpdated(svcEventKey, cachedValue, newCachedValue);
}
}
} else if (cachedStatus == CachedStatus.CacheEmptyButNoData) {
LOG.error("OnServiceUpdate: {} is empty, but discover returns no data", svcEventKey);
}
boolean newRemoteCache = null == cachedValue || cachedValue.isLoadedFromFile();
if (newRemoteCache && serviceEventKey.getEventType() == EventType.INSTANCE) {
// 设置就绪状态
registry.setServerServiceReady(serviceEventKey);
}
}
synchronized (lock) {
if (error != null && ErrorCode.SERVICE_NOT_FOUND.equals(error.getCode())) {
notifyEvent(null);
}
notifyEvent(error);
}
return svcDeleted;
}
use of com.tencent.polaris.api.pojo.RegistryCacheValue in project polaris-java by polarismesh.
the class InMemoryRegistry method getResource.
private RegistryCacheValue getResource(ServiceEventKey svcEventKey, boolean includeCache, boolean internalRequest) {
CacheObject cacheObject = resourceMap.get(svcEventKey);
if (null == cacheObject) {
return null;
}
RegistryCacheValue registryCacheValue = cacheObject.loadValue(!internalRequest);
if (null == registryCacheValue) {
return null;
}
if (cacheObject.isRemoteUpdated() || includeCache) {
return registryCacheValue;
}
return null;
}
use of com.tencent.polaris.api.pojo.RegistryCacheValue in project polaris-java by polarismesh.
the class InMemoryRegistry method updateInstances.
@Override
public void updateInstances(ServiceUpdateRequest request) {
Collection<InstanceProperty> instanceProperties = request.getProperties();
if (CollectionUtils.isEmpty(instanceProperties)) {
return;
}
RegistryCacheValue cacheValue = getResource(new ServiceEventKey(request.getServiceKey(), EventType.INSTANCE), true, true);
if (null == cacheValue) {
// 服务不存在,忽略
return;
}
for (InstanceProperty instanceProperty : instanceProperties) {
InstanceByProto instance = (InstanceByProto) instanceProperty.getInstance();
InstanceLocalValue instanceLocalValue = instance.getInstanceLocalValue();
Map<String, Object> properties = instanceProperty.getProperties();
LOG.info("update instance properties for instance {}, properties {}", instance.getId(), properties);
for (Map.Entry<String, Object> entry : properties.entrySet()) {
switch(entry.getKey()) {
case InstanceProperty.PROPERTY_CIRCUIT_BREAKER_STATUS:
onCircuitBreakStatus(entry.getValue(), instanceLocalValue, instance);
break;
case InstanceProperty.PROPERTY_DETECT_RESULT:
instanceLocalValue.setDetectResult((DetectResult) entry.getValue());
break;
default:
break;
}
}
}
}
use of com.tencent.polaris.api.pojo.RegistryCacheValue in project spring-cloud-tencent by Tencent.
the class PolarisServiceStatusChangeListener method onResourceUpdated.
@Override
public void onResourceUpdated(ServiceEventKey svcEventKey, RegistryCacheValue oldValue, RegistryCacheValue newValue) {
if (newValue.getEventType() == ServiceEventKey.EventType.SERVICE) {
if (oldValue instanceof ServicesByProto && newValue instanceof ServicesByProto) {
LOG.debug("receive service={} change event", svcEventKey);
Set<String> oldServiceInfoSet = ((ServicesByProto) oldValue).getServices().stream().map(i -> i.getNamespace() + "::" + i.getService()).collect(Collectors.toSet());
Set<String> newServiceInfoSet = ((ServicesByProto) newValue).getServices().stream().map(i -> i.getNamespace() + "::" + i.getService()).collect(Collectors.toSet());
Sets.SetView<String> addServiceInfoSetView = Sets.difference(newServiceInfoSet, oldServiceInfoSet);
Sets.SetView<String> deleteServiceInfoSetView = Sets.difference(oldServiceInfoSet, newServiceInfoSet);
if (addServiceInfoSetView.isEmpty() && deleteServiceInfoSetView.isEmpty()) {
return;
}
LOG.info("Service status is update. Add service of {}. Delete service of {}", addServiceInfoSetView, deleteServiceInfoSetView);
// Trigger reload of gateway route cache.
this.publisher.publishEvent(new HeartbeatEvent(this, INDEX.getAndIncrement()));
}
} else if (newValue.getEventType() == ServiceEventKey.EventType.INSTANCE) {
if (oldValue instanceof ServiceInstancesByProto && newValue instanceof ServiceInstancesByProto) {
LOG.debug("receive service instances={} change event", svcEventKey);
ServiceInstancesByProto oldIns = (ServiceInstancesByProto) oldValue;
ServiceInstancesByProto newIns = (ServiceInstancesByProto) newValue;
if ((CollectionUtils.isEmpty(oldIns.getInstances()) && !CollectionUtils.isEmpty(newIns.getInstances())) || (!CollectionUtils.isEmpty(oldIns.getInstances()) && CollectionUtils.isEmpty(newIns.getInstances()))) {
LOG.info("Service status of {} is update.", newIns.getService());
// Trigger reload of gateway route cache.
this.publisher.publishEvent(new HeartbeatEvent(this, INDEX.getAndIncrement()));
}
}
}
}
Aggregations