use of com.tencent.polaris.api.plugin.registry.CacheHandler.CachedStatus 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.plugin.registry.CacheHandler.CachedStatus 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;
}
Aggregations