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);
}
}
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;
}
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();
}
}
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;
}
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;
}
Aggregations