Search in sources :

Example 1 with CachedStatus

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;
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) RegistryCacheValue(com.tencent.polaris.api.pojo.RegistryCacheValue) CachedStatus(com.tencent.polaris.api.plugin.registry.CacheHandler.CachedStatus) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ResourceEventListener(com.tencent.polaris.api.plugin.registry.ResourceEventListener)

Example 2 with CachedStatus

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;
}
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

CachedStatus (com.tencent.polaris.api.plugin.registry.CacheHandler.CachedStatus)2 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)2 PolarisException (com.tencent.polaris.api.exception.PolarisException)1 ResourceEventListener (com.tencent.polaris.api.plugin.registry.ResourceEventListener)1 RegistryCacheValue (com.tencent.polaris.api.pojo.RegistryCacheValue)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 Service (com.tencent.polaris.client.pb.ServiceProto.Service)1