use of com.tencent.polaris.api.plugin.configuration.ConfigFile in project polaris-java by polarismesh.
the class DefaultConfigFileLongPollingService method doLongPolling.
private void doLongPolling() {
while (!Thread.currentThread().isInterrupted()) {
try {
List<ConfigFile> watchConfigFiles = assembleWatchConfigFiles();
LOGGER.info("[Config] do long polling. config file size = {}, delay time = {}", watchConfigFiles.size(), retryPolicy.getCurrentDelayTime());
ConfigFileResponse response = configFileConnector.watchConfigFiles(watchConfigFiles);
retryPolicy.success();
int responseCode = response.getCode();
// 感知到配置文件发布事件
if (responseCode == ServerCodes.EXECUTE_SUCCESS && response.getConfigFile() != null) {
ConfigFile changedConfigFile = response.getConfigFile();
ConfigFileMetadata metadata = new DefaultConfigFileMetadata(changedConfigFile.getNamespace(), changedConfigFile.getFileGroup(), changedConfigFile.getFileName());
long newNotifiedVersion = changedConfigFile.getVersion();
long oldNotifiedVersion = notifiedVersion.get(metadata);
long maxVersion = Math.max(newNotifiedVersion, oldNotifiedVersion);
// 更新版本号
notifiedVersion.put(metadata, maxVersion);
LOGGER.info("[Config] received change event by long polling. file = {}, new version = {}, old version = {}", metadata, newNotifiedVersion, oldNotifiedVersion);
// 通知 RemoteConfigFileRepo 拉取最新的配置文件
RemoteConfigFileRepo remoteConfigFileRepo = configFilePool.get(metadata);
remoteConfigFileRepo.onLongPollNotified(maxVersion);
continue;
}
// 没有变更
if (responseCode == ServerCodes.DATA_NO_CHANGE) {
LOGGER.info("[Config] long polling result: data no change");
continue;
}
// 预期之外的状态码,退避重试
LOGGER.error("[Config] long polling result with unexpect code. code = {}", responseCode);
retryPolicy.fail();
retryPolicy.executeDelay();
} catch (Throwable t) {
LOGGER.error("[Config] long polling failed.", t);
retryPolicy.fail();
retryPolicy.executeDelay();
}
}
}
use of com.tencent.polaris.api.plugin.configuration.ConfigFile in project polaris-java by polarismesh.
the class RemoteConfigFileRepo method addToLongPollingPool.
private void addToLongPollingPool(ConfigFileLongPollingService configFileLongPollingService, ConfigFileMetadata configFileMetadata) {
ConfigFile configFile = remoteConfigFile.get();
// 理论上,加到长轮询任务之前会同步一次配置文件,但是可能会同步失败。
if (configFile == null) {
configFile = new ConfigFile(configFileMetadata.getNamespace(), configFileMetadata.getFileGroup(), configFileMetadata.getFileName());
// 初始版本号为0
configFile.setVersion(INIT_VERSION);
}
configFileLongPollingService.addConfigFile(this);
}
use of com.tencent.polaris.api.plugin.configuration.ConfigFile in project polaris-java by polarismesh.
the class RemoteConfigFileRepo method deepCloneConfigFile.
private ConfigFile deepCloneConfigFile(ConfigFile sourceConfigFile) {
ConfigFile configFile = new ConfigFile(sourceConfigFile.getNamespace(), sourceConfigFile.getFileGroup(), sourceConfigFile.getFileName());
configFile.setContent(sourceConfigFile.getContent());
configFile.setVersion(sourceConfigFile.getVersion());
configFile.setMd5(sourceConfigFile.getMd5());
return configFile;
}
use of com.tencent.polaris.api.plugin.configuration.ConfigFile in project polaris-java by polarismesh.
the class PolarisConfigFileConnector method watchConfigFiles.
@Override
public ConfigFileResponse watchConfigFiles(List<ConfigFile> configFiles) {
Connection connection = null;
try {
connection = connectionManager.getConnection(OP_KEY_GET_CONFIG_FILE, ClusterType.SERVICE_DISCOVER_CLUSTER);
// grpc 调用
PolarisConfigGRPCGrpc.PolarisConfigGRPCBlockingStub stub = PolarisConfigGRPCGrpc.newBlockingStub(connection.getChannel());
// 附加通用 header
GrpcUtil.attachRequestHeader(stub, GrpcUtil.nextInstanceRegisterReqId());
// 执行调用
List<ConfigFileProto.ConfigFileDTO> dtos = Lists.newLinkedList();
for (ConfigFile configFile : configFiles) {
dtos.add(transfer2DTO(configFile));
}
ConfigFileProto.WatchConfigFileRequest request = ConfigFileProto.WatchConfigFileRequest.newBuilder().addAllWatchFiles(dtos).build();
ConfigFileProto.ConfigFileResponse response = stub.watchConfigFiles(request);
return handleResponse(response);
} catch (Throwable t) {
// 网络访问异常
if (connection != null) {
connection.reportFail();
}
throw new RetriableException(ErrorCode.NETWORK_ERROR, "[Config] failed to watch config file", t);
} finally {
if (connection != null) {
connection.release(OP_KEY_GET_CONFIG_FILE);
}
}
}
use of com.tencent.polaris.api.plugin.configuration.ConfigFile in project polaris-java by polarismesh.
the class PolarisConfigFileConnector method transferFromDTO.
private ConfigFile transferFromDTO(ConfigFileProto.ConfigFileDTO configFileDTO) {
if (configFileDTO == null) {
return null;
}
ConfigFile configFile = new ConfigFile(configFileDTO.getNamespace().getValue(), configFileDTO.getGroup().getValue(), configFileDTO.getFileName().getValue());
configFile.setContent(configFileDTO.getContent().getValue());
configFile.setMd5(configFileDTO.getMd5().getValue());
configFile.setVersion(configFileDTO.getVersion().getValue());
return configFile;
}
Aggregations