use of com.tencent.polaris.api.pojo.ServiceEventKey.EventType in project polaris-java by polarismesh.
the class SpecStreamClient method validMessage.
/**
* 检查该应答是否合法,不合法则走异常流程
*
* @param response 服务端应答
* @return 合法返回true,否则返回false
*/
private ValidResult validMessage(ResponseProto.DiscoverResponse response) {
ErrorCode errorCode = ErrorCode.Success;
if (response.hasCode()) {
errorCode = ServerCodes.convertServerErrorToRpcError(response.getCode().getValue());
}
ServiceProto.Service service;
if (CollectionUtils.isNotEmpty(response.getServicesList())) {
service = response.getServicesList().get(0);
} else {
service = response.getService();
}
if (null == service || StringUtils.isEmpty(service.getNamespace().getValue()) || StringUtils.isEmpty(service.getName().getValue())) {
return new ValidResult(null, ErrorCode.INVALID_SERVER_RESPONSE, "service is empty, response text is " + response.toString());
}
EventType eventType = GrpcUtil.buildEventType(response.getType());
if (eventType == EventType.UNKNOWN) {
return new ValidResult(null, ErrorCode.INVALID_SERVER_RESPONSE, "invalid event type " + response.getType());
}
ServiceEventKey serviceEventKey = new ServiceEventKey(new ServiceKey(service.getNamespace().getValue(), service.getName().getValue()), eventType);
if (errorCode == ErrorCode.SERVER_ERROR) {
// 返回了500错误
return new ValidResult(serviceEventKey, errorCode, "invalid event type " + response.getType());
}
return new ValidResult(serviceEventKey, ErrorCode.Success, "");
}
use of com.tencent.polaris.api.pojo.ServiceEventKey.EventType 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();
}
}
Aggregations