Search in sources :

Example 1 with RegistryCacheValue

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

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;
}
Also used : RegistryCacheValue(com.tencent.polaris.api.pojo.RegistryCacheValue)

Example 3 with RegistryCacheValue

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;
            }
        }
    }
}
Also used : InstanceByProto(com.tencent.polaris.client.pojo.InstanceByProto) RegistryCacheValue(com.tencent.polaris.api.pojo.RegistryCacheValue) InstanceLocalValue(com.tencent.polaris.api.pojo.InstanceLocalValue) InstanceProperty(com.tencent.polaris.api.plugin.registry.InstanceProperty) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 4 with RegistryCacheValue

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()));
            }
        }
    }
}
Also used : ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) AbstractResourceEventListener(com.tencent.polaris.api.plugin.registry.AbstractResourceEventListener) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto) ServicesByProto(com.tencent.polaris.client.pojo.ServicesByProto) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HeartbeatEvent(org.springframework.cloud.client.discovery.event.HeartbeatEvent) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AtomicLong(java.util.concurrent.atomic.AtomicLong) RegistryCacheValue(com.tencent.polaris.api.pojo.RegistryCacheValue) CollectionUtils(org.springframework.util.CollectionUtils) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) HeartbeatEvent(org.springframework.cloud.client.discovery.event.HeartbeatEvent) Sets(com.google.common.collect.Sets) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto) ServicesByProto(com.tencent.polaris.client.pojo.ServicesByProto)

Aggregations

RegistryCacheValue (com.tencent.polaris.api.pojo.RegistryCacheValue)4 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)3 Sets (com.google.common.collect.Sets)1 PolarisException (com.tencent.polaris.api.exception.PolarisException)1 AbstractResourceEventListener (com.tencent.polaris.api.plugin.registry.AbstractResourceEventListener)1 CachedStatus (com.tencent.polaris.api.plugin.registry.CacheHandler.CachedStatus)1 InstanceProperty (com.tencent.polaris.api.plugin.registry.InstanceProperty)1 ResourceEventListener (com.tencent.polaris.api.plugin.registry.ResourceEventListener)1 InstanceLocalValue (com.tencent.polaris.api.pojo.InstanceLocalValue)1 InstanceByProto (com.tencent.polaris.client.pojo.InstanceByProto)1 ServiceInstancesByProto (com.tencent.polaris.client.pojo.ServiceInstancesByProto)1 ServicesByProto (com.tencent.polaris.client.pojo.ServicesByProto)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1