use of com.tencent.polaris.api.pojo.ServiceEventKey in project polaris-java by polarismesh.
the class MessagePersistHandler method fileNameToServiceKey.
private static ServiceEventKey fileNameToServiceKey(String fileName) {
fileName = fileName.substring(0, fileName.length() - CACHE_SUFFIX.length());
String[] pieces = fileName.split("#");
try {
String namespace = URLDecoder.decode(pieces[1], "UTF-8");
String service = URLDecoder.decode(pieces[2], "UTF-8");
String eventTypeStr = URLDecoder.decode(pieces[3], "UTF-8");
ServiceEventKey.EventType eventType = ServiceEventKey.EventType.valueOf(eventTypeStr.toUpperCase());
return new ServiceEventKey(new ServiceKey(namespace, service), eventType);
} catch (UnsupportedEncodingException e) {
throw new AssertionError("UTF-8 is unknown");
// or 'throw new AssertionError("Impossible things are happening today. " +
// "Consider buying a lottery ticket!!");'
}
}
use of com.tencent.polaris.api.pojo.ServiceEventKey in project polaris-java by polarismesh.
the class SpecStreamClient method sendRequest.
/**
* 发送请求
*
* @param serviceUpdateTask 服务更新任务
*/
public void sendRequest(ServiceUpdateTask serviceUpdateTask) {
ServiceEventKey serviceEventKey = serviceUpdateTask.getServiceEventKey();
ServiceProto.Service.Builder builder = ServiceProto.Service.newBuilder();
builder.setName(StringValue.newBuilder().setValue(serviceEventKey.getServiceKey().getService()).build());
builder.setNamespace(StringValue.newBuilder().setValue(serviceEventKey.getServiceKey().getNamespace()).build());
RequestProto.DiscoverRequest.Builder req = RequestProto.DiscoverRequest.newBuilder();
// switch
req.setType(GrpcUtil.buildDiscoverRequestType(serviceEventKey.getEventType()));
req.setService(builder);
if (serviceUpdateTask.getTaskType() == Type.FIRST) {
LOG.info("[ServerConnector]send request(id={}) to {} for service {}", reqId, connection.getConnID(), serviceEventKey);
} else {
LOG.debug("[ServerConnector]send request(id={}) to {} for service {}", reqId, connection.getConnID(), serviceEventKey);
}
discoverClient.onNext(req.build());
}
use of com.tencent.polaris.api.pojo.ServiceEventKey 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 in project polaris-java by polarismesh.
the class SpecStreamClient method exceptionCallback.
/**
* 异常回调
*
* @param validResult 异常
*/
public void exceptionCallback(ValidResult validResult) {
this.closeStream(false);
LOG.error("[ServerConnector]exceptionCallback: errCode {}, info {}, serviceEventKey {}", validResult.getErrorCode(), validResult.getMessage(), validResult.getServiceEventKey());
// report down
connection.reportFail();
List<ServiceUpdateTask> notifyTasks = new ArrayList<>();
synchronized (clientLock) {
ServiceEventKey serviceEventKey = validResult.getServiceEventKey();
if (null == serviceEventKey) {
if (CollectionUtils.isNotEmpty(pendingTask.values())) {
notifyTasks.addAll(pendingTask.values());
pendingTask.clear();
}
} else {
ServiceUpdateTask task = pendingTask.remove(serviceEventKey);
if (null != task) {
notifyTasks.add(task);
}
}
}
for (ServiceUpdateTask notifyTask : notifyTasks) {
notifyTask.retry();
}
}
use of com.tencent.polaris.api.pojo.ServiceEventKey in project polaris-java by polarismesh.
the class ChangeStateUtils method getInstance.
/**
* 获取实例ID
*
* @param instanceGauge 实例统计数据
* @param localRegistry 本地缓存插件
* @return ID
*/
public static InstanceByProto getInstance(InstanceGauge instanceGauge, LocalRegistry localRegistry) {
ServiceEventKey serviceEventKey = new ServiceEventKey(new ServiceKey(instanceGauge.getNamespace(), instanceGauge.getService()), EventType.INSTANCE);
ResourceFilter resourceFilter = new ResourceFilter(serviceEventKey, true, true);
ServiceInstances instances = localRegistry.getInstances(resourceFilter);
if (!instances.isInitialized()) {
return null;
}
ServiceInstancesByProto serviceInstancesByProto = (ServiceInstancesByProto) instances;
Instance instance = instanceGauge.getInstance();
if (instance instanceof InstanceByProto) {
return (InstanceByProto) instance;
}
InstanceByProto instanceByProto;
String instanceId = instanceGauge.getInstanceId();
if (StringUtils.isNotBlank(instanceId)) {
instanceByProto = serviceInstancesByProto.getInstance(instanceId);
} else {
Node node = new Node(instanceGauge.getHost(), instanceGauge.getPort());
instanceByProto = serviceInstancesByProto.getInstanceByNode(node);
}
if (null != instanceByProto) {
instanceGauge.setInstance(instanceByProto);
}
return instanceByProto;
}
Aggregations