use of com.tencent.polaris.api.exception.PolarisException in project polaris-java by polarismesh.
the class GrpcConnector method registerInstance.
@Override
public CommonProviderResponse registerInstance(CommonProviderRequest req) throws PolarisException {
checkDestroyed();
Connection connection = null;
ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
try {
waitDiscoverReady();
connection = connectionManager.getConnection(GrpcUtil.OP_KEY_REGISTER_INSTANCE, ClusterType.SERVICE_DISCOVER_CLUSTER);
req.setTargetServer(connectionToTargetNode(connection));
PolarisGRPCGrpc.PolarisGRPCBlockingStub stub = PolarisGRPCGrpc.newBlockingStub(connection.getChannel());
GrpcUtil.attachRequestHeader(stub, GrpcUtil.nextInstanceRegisterReqId());
ResponseProto.Response registerInstanceResponse = stub.registerInstance(buildRegisterInstanceRequest(req));
GrpcUtil.checkResponse(registerInstanceResponse);
if (!registerInstanceResponse.hasInstance()) {
throw new PolarisException(ErrorCode.SERVER_USER_ERROR, "invalid register response: missing instance");
}
CommonProviderResponse resp = new CommonProviderResponse();
resp.setInstanceID(registerInstanceResponse.getInstance().getId().getValue());
resp.setExists(registerInstanceResponse.getCode().getValue() == ServerCodes.EXISTED_RESOURCE);
return resp;
} catch (Throwable t) {
if (t instanceof PolarisException) {
throw t;
}
if (null != connection) {
connection.reportFail();
}
throw new RetriableException(ErrorCode.NETWORK_ERROR, String.format("fail to register host %s:%d service %s", req.getHost(), req.getPort(), serviceKey), t);
} finally {
if (null != connection) {
connection.release(GrpcUtil.OP_KEY_REGISTER_INSTANCE);
}
}
}
use of com.tencent.polaris.api.exception.PolarisException 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.exception.PolarisException in project polaris-java by polarismesh.
the class ErrRateCircuitBreaker method init.
@Override
public void init(InitContext ctx) throws PolarisException {
CircuitBreakerConfig circuitBreakerConfig = ctx.getConfig().getConsumer().getCircuitBreaker();
OutlierDetectionConfig outlierDetection = ctx.getConfig().getConsumer().getOutlierDetection();
metricWindowMs = circuitBreakerConfig.getCheckPeriod();
HalfOpenConfig halfOpenConfig = new HalfOpenConfig(circuitBreakerConfig, outlierDetection);
Config cfg = circuitBreakerConfig.getPluginConfig(getName(), Config.class);
if (cfg == null) {
throw new PolarisException(ErrorCode.INVALID_CONFIG, String.format("plugin %s config is missing", getName()));
}
ConfigSet<Config> configSet = new ConfigSet<>(StatusDimension.Level.SERVICE, false, halfOpenConfig, cfg);
create = new Function<Integer, Object>() {
@Override
public Object apply(Integer integer) {
return new ErrRateCounter(metricWindowName, configSet.getPlugConfig(), getBucketIntervalMs());
}
};
configGroup = new ConfigGroup<>(configSet);
stateMachine = new StateMachineImpl(configGroup, id, this, metricWindowMs);
flowControlParam = new DefaultFlowControlParam(ctx.getConfig().getGlobal().getAPI());
}
use of com.tencent.polaris.api.exception.PolarisException in project polaris-java by polarismesh.
the class SDKContext method initContextByConfig.
/**
* 通过配置对象初始化SDK上下文
*
* @param config 配置对象
* @return SDK上下文
* @throws PolarisException 初始化过程的异常
*/
public static SDKContext initContextByConfig(Configuration config) throws PolarisException {
try {
((ConfigurationImpl) config).setDefault();
config.verify();
} catch (IllegalArgumentException e) {
throw new PolarisException(ErrorCode.INVALID_CONFIG, "fail to verify configuration", e);
}
ServiceLoader<TypeProvider> providers = ServiceLoader.load(TypeProvider.class);
List<PluginType> types = new ArrayList<>();
for (TypeProvider provider : providers) {
types.addAll(provider.getTypes());
}
PluginManager manager = new PluginManager(types);
ValueContext valueContext = new ValueContext();
valueContext.setHost(parseHost(config));
valueContext.setServerConnectorProtocol(parseServerConnectorProtocol(config));
SDKContext initContext = new SDKContext(config, manager, valueContext);
try {
manager.initPlugins(initContext);
} catch (Throwable e) {
manager.destroyPlugins();
if (e instanceof PolarisException) {
throw e;
}
throw new PolarisException(ErrorCode.PLUGIN_ERROR, "plugin error", e);
}
return initContext;
}
use of com.tencent.polaris.api.exception.PolarisException in project polaris-java by polarismesh.
the class PluginConfigImpl method mutableSetPluginConfig.
private Map<?, ?> mutableSetPluginConfig(String pluginName, Verifier config) throws PolarisException {
Map<?, ?> configMap;
try {
configMap = ConfigUtils.objectToMap(config);
} catch (Exception e) {
throw new PolarisException(ErrorCode.INVALID_CONFIG, String.format("fail to marshal plugin config for %s", pluginName), e);
}
plugin.put(pluginName, configMap);
return configMap;
}
Aggregations