Search in sources :

Example 16 with ServiceEventKey

use of com.tencent.polaris.api.pojo.ServiceEventKey in project spring-cloud-tencent by Tencent.

the class PolarisServiceStatusChangeListenerTest method testOnResourceUpdated.

@Test
public void testOnResourceUpdated() {
    PolarisServiceStatusChangeListener polarisServiceStatusChangeListener = new PolarisServiceStatusChangeListener();
    polarisServiceStatusChangeListener.setApplicationEventPublisher(publisher);
    // Service update event
    ServiceEventKey serviceUpdateEventKey = new ServiceEventKey(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER), ServiceEventKey.EventType.SERVICE);
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setNamespace(NAMESPACE_TEST);
    serviceInfo.setService(SERVICE_PROVIDER);
    // Need update
    ServicesByProto oldServices = new ServicesByProto(Collections.emptyList());
    ServicesByProto newServices = new ServicesByProto(Collections.singletonList(serviceInfo));
    polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldServices, newServices);
    verify(publisher, times(1)).publishEvent(any(ApplicationEvent.class));
    // No need update
    oldServices = new ServicesByProto(Collections.singletonList(serviceInfo));
    newServices = new ServicesByProto(Collections.singletonList(serviceInfo));
    polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldServices, newServices);
    verify(publisher, times(1)).publishEvent(any(ApplicationEvent.class));
    // Instance update event
    ServiceEventKey instanceUpdateEventKey = new ServiceEventKey(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER), ServiceEventKey.EventType.INSTANCE);
    DefaultInstance instance = new DefaultInstance();
    instance.setNamespace(NAMESPACE_TEST);
    instance.setService(SERVICE_PROVIDER);
    instance.setHost(HOST);
    instance.setPort(PORT);
    try {
        Field instances = ServiceInstancesByProto.class.getDeclaredField("instances");
        instances.setAccessible(true);
        // Need update
        ServiceInstancesByProto oldInstances = new ServiceInstancesByProto();
        instances.set(oldInstances, Collections.emptyList());
        ServiceInstancesByProto newInstances = new ServiceInstancesByProto();
        instances.set(newInstances, Collections.singletonList(instance));
        polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldInstances, newInstances);
        verify(publisher, times(2)).publishEvent(any(ApplicationEvent.class));
        // No need update
        oldInstances = new ServiceInstancesByProto();
        instances.set(oldInstances, Collections.singletonList(instance));
        newInstances = new ServiceInstancesByProto();
        instances.set(newInstances, Collections.singletonList(instance));
        polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldInstances, newInstances);
        verify(publisher, times(2)).publishEvent(any(ApplicationEvent.class));
    } catch (NoSuchFieldException | IllegalAccessException e) {
        Assertions.fail("Exception encountered.", e);
    }
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) Field(java.lang.reflect.Field) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) ApplicationEvent(org.springframework.context.ApplicationEvent) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto) ServicesByProto(com.tencent.polaris.client.pojo.ServicesByProto) Test(org.junit.Test)

Example 17 with ServiceEventKey

use of com.tencent.polaris.api.pojo.ServiceEventKey in project polaris-java by polarismesh.

the class SpecStreamClient method checkAvailable.

/**
 * checkAvailable
 *
 * @param serviceUpdateTask 更新任务
 * @return 是否可用
 */
public boolean checkAvailable(ServiceUpdateTask serviceUpdateTask) {
    if (isEndStream()) {
        return false;
    }
    synchronized (clientLock) {
        if (isEndStream()) {
            return false;
        }
        if (closeExpireStream()) {
            return false;
        }
        ServiceEventKey serviceEventKey = serviceUpdateTask.getServiceEventKey();
        ServiceUpdateTask lastUpdateTask = pendingTask.get(serviceEventKey);
        if (null != lastUpdateTask) {
            LOG.warn("[ServerConnector]pending task {} has been overwritten", lastUpdateTask);
        }
        pendingTask.put(serviceEventKey, serviceUpdateTask);
    }
    return true;
}
Also used : ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ServiceUpdateTask(com.tencent.polaris.plugins.connector.common.ServiceUpdateTask)

Example 18 with ServiceEventKey

use of com.tencent.polaris.api.pojo.ServiceEventKey in project polaris-java by polarismesh.

the class SpecStreamClient method onNext.

/**
 * 正常回调
 *
 * @param response 应答说
 */
public void onNext(ResponseProto.DiscoverResponse response) {
    lastRecvTimeMs.set(System.currentTimeMillis());
    ValidResult validResult = validMessage(response);
    if (validResult.errorCode != ErrorCode.Success) {
        exceptionCallback(validResult);
        return;
    }
    ServiceProto.Service service = response.getService();
    ServiceKey serviceKey = new ServiceKey(service.getNamespace().getValue(), service.getName().getValue());
    EventType eventType = GrpcUtil.buildEventType(response.getType());
    ServiceEventKey serviceEventKey = new ServiceEventKey(serviceKey, eventType);
    ServiceUpdateTask updateTask;
    synchronized (clientLock) {
        updateTask = pendingTask.remove(serviceEventKey);
    }
    if (null == updateTask) {
        LOG.error("[ServerConnector]callback not found for:{}", TextFormat.shortDebugString(service));
        return;
    }
    if (updateTask.getTaskType() == Type.FIRST) {
        LOG.info("[ServerConnector]receive response for {}", serviceEventKey);
    } else {
        LOG.debug("[ServerConnector]receive response for {}", serviceEventKey);
    }
    PolarisException error;
    if (!response.hasCode() || response.getCode().getValue() == ServerCodes.EXECUTE_SUCCESS) {
        error = null;
    } else {
        int respCode = response.getCode().getValue();
        String info = response.getInfo().getValue();
        error = ServerErrorResponseException.build(respCode, String.format("[ServerConnector]code %d, fail to query service %s from server(%s): %s", respCode, serviceKey, connection.getConnID(), info));
    }
    boolean svcDeleted = updateTask.notifyServerEvent(new ServerEvent(serviceEventKey, response, error));
    if (!svcDeleted) {
        updateTask.addUpdateTaskSet();
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) ServerEvent(com.tencent.polaris.api.plugin.server.ServerEvent) EventType(com.tencent.polaris.api.pojo.ServiceEventKey.EventType) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ServiceUpdateTask(com.tencent.polaris.plugins.connector.common.ServiceUpdateTask) ServiceProto(com.tencent.polaris.client.pb.ServiceProto)

Example 19 with ServiceEventKey

use of com.tencent.polaris.api.pojo.ServiceEventKey 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)

Example 20 with ServiceEventKey

use of com.tencent.polaris.api.pojo.ServiceEventKey in project polaris-java by polarismesh.

the class GetResourcesInvoker method init.

/**
 * 初始化invoker
 *
 * @param paramProvider
 * @return 等待的数量
 * @throws PolarisException
 */
private int init(ServiceEventKeysProvider paramProvider) throws PolarisException {
    LocalRegistry localRegistry = extensions.getLocalRegistry();
    int callbacks = 0;
    if (!CollectionUtils.isEmpty(paramProvider.getSvcEventKeys())) {
        for (ServiceEventKey svcEventKey : paramProvider.getSvcEventKeys()) {
            listeningServices.add(svcEventKey);
            callbacks = processSvcEventKey(localRegistry, callbacks, svcEventKey);
        }
    }
    if (null != paramProvider.getSvcEventKey()) {
        listeningServices.add(paramProvider.getSvcEventKey());
        callbacks = processSvcEventKey(localRegistry, callbacks, paramProvider.getSvcEventKey());
    }
    return callbacks;
}
Also used : ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) LocalRegistry(com.tencent.polaris.api.plugin.registry.LocalRegistry)

Aggregations

ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)23 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)11 DefaultServiceEventKeysProvider (com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider)5 HashMap (java.util.HashMap)5 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)4 ResourcesResponse (com.tencent.polaris.client.flow.ResourcesResponse)4 ServiceInstancesByProto (com.tencent.polaris.client.pojo.ServiceInstancesByProto)4 PolarisException (com.tencent.polaris.api.exception.PolarisException)3 ResourceFilter (com.tencent.polaris.api.plugin.registry.ResourceFilter)3 RegistryCacheValue (com.tencent.polaris.api.pojo.RegistryCacheValue)3 Message (com.google.protobuf.Message)2 DefaultRouterChainGroup (com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup)2 CachedStatus (com.tencent.polaris.api.plugin.registry.CacheHandler.CachedStatus)2 RouteInfo (com.tencent.polaris.api.plugin.route.RouteInfo)2 ServiceRouter (com.tencent.polaris.api.plugin.route.ServiceRouter)2 Instance (com.tencent.polaris.api.pojo.Instance)2 EventType (com.tencent.polaris.api.pojo.ServiceEventKey.EventType)2 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)2 ServiceRule (com.tencent.polaris.api.pojo.ServiceRule)2 ServiceProto (com.tencent.polaris.client.pb.ServiceProto)2