Search in sources :

Example 1 with ConfigFile

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();
        }
    }
}
Also used : ConfigFileResponse(com.tencent.polaris.api.plugin.configuration.ConfigFileResponse) ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata)

Example 2 with ConfigFile

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);
}
Also used : ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile)

Example 3 with ConfigFile

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;
}
Also used : ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile)

Example 4 with 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);
        }
    }
}
Also used : ConfigFileProto(com.tencent.polaris.client.pb.ConfigFileProto) ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile) Connection(com.tencent.polaris.plugins.connector.grpc.Connection) PolarisConfigGRPCGrpc(com.tencent.polaris.client.pb.PolarisConfigGRPCGrpc) RetriableException(com.tencent.polaris.api.exception.RetriableException)

Example 5 with ConfigFile

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;
}
Also used : ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile)

Aggregations

ConfigFile (com.tencent.polaris.api.plugin.configuration.ConfigFile)11 ConfigFileResponse (com.tencent.polaris.api.plugin.configuration.ConfigFileResponse)6 ConfigFileMetadata (com.tencent.polaris.configuration.api.core.ConfigFileMetadata)6 Test (org.junit.Test)4 RetriableException (com.tencent.polaris.api.exception.RetriableException)2 ErrorCode (com.tencent.polaris.api.exception.ErrorCode)1 ServerCodes (com.tencent.polaris.api.exception.ServerCodes)1 ConfigFileConnector (com.tencent.polaris.api.plugin.configuration.ConfigFileConnector)1 SDKContext (com.tencent.polaris.client.api.SDKContext)1 ConfigFileProto (com.tencent.polaris.client.pb.ConfigFileProto)1 PolarisConfigGRPCGrpc (com.tencent.polaris.client.pb.PolarisConfigGRPCGrpc)1 ConfigFileTestUtils (com.tencent.polaris.configuration.client.ConfigFileTestUtils)1 Connection (com.tencent.polaris.plugins.connector.grpc.Connection)1 Map (java.util.Map)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 RunWith (org.junit.runner.RunWith)1 ArgumentMatchers.anyList (org.mockito.ArgumentMatchers.anyList)1 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)1 Mock (org.mockito.Mock)1