Search in sources :

Example 1 with ClusterType

use of com.tencent.polaris.api.config.global.ClusterType in project polaris-java by polarismesh.

the class GrpcConnector method initActually.

private void initActually(InitContext ctx, ServerConnectorConfig connectorConfig) {
    readyFuture = new CompletableFuture<>();
    Map<ClusterType, CompletableFuture<String>> futures = new HashMap<>();
    futures.put(ClusterType.SERVICE_DISCOVER_CLUSTER, readyFuture);
    connectionManager = new ConnectionManager(ctx, connectorConfig, futures);
    connectionIdleTimeoutMs = connectorConfig.getConnectionIdleTimeout();
    messageTimeoutMs = connectorConfig.getMessageTimeout();
    sendDiscoverExecutor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory(getName() + "-send-discovery"), new CallerRunsPolicy());
    sendDiscoverExecutor.setMaximumPoolSize(1);
    buildInExecutor = new ScheduledThreadPoolExecutor(0, new NamedThreadFactory(getName() + "-builtin-discovery"), new CallerRunsPolicy());
    buildInExecutor.setMaximumPoolSize(1);
    streamClients.put(ClusterType.BUILTIN_CLUSTER, new AtomicReference<>());
    streamClients.put(ClusterType.SERVICE_DISCOVER_CLUSTER, new AtomicReference<>());
    updateServiceExecutor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory(getName() + "-update-service"));
    updateServiceExecutor.setMaximumPoolSize(1);
    initialized = true;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) HashMap(java.util.HashMap) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) NamedThreadFactory(com.tencent.polaris.client.util.NamedThreadFactory) CallerRunsPolicy(java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy) ClusterType(com.tencent.polaris.api.config.global.ClusterType)

Example 2 with ClusterType

use of com.tencent.polaris.api.config.global.ClusterType in project polaris-java by polarismesh.

the class GrpcConnector method submitServiceHandler.

@Override
protected void submitServiceHandler(ServiceUpdateTask updateTask, long delayMs) {
    ClusterType targetCluster = updateTask.getTargetClusterType();
    if (updateTask.setStatus(Status.READY, Status.RUNNING)) {
        if (targetCluster == ClusterType.BUILTIN_CLUSTER) {
            LOG.info("[ServerConnector]task for service {} has been scheduled builtin", updateTask);
            buildInExecutor.schedule(updateTask, delayMs, TimeUnit.MILLISECONDS);
        } else {
            LOG.debug("[ServerConnector]task for service {} has been scheduled discover", updateTask);
            sendDiscoverExecutor.schedule(updateTask, delayMs, TimeUnit.MILLISECONDS);
        }
    }
}
Also used : ClusterType(com.tencent.polaris.api.config.global.ClusterType)

Example 3 with ClusterType

use of com.tencent.polaris.api.config.global.ClusterType in project polaris-java by polarismesh.

the class GrpcServiceUpdateTask method execute.

public void execute(ServiceUpdateTask serviceUpdateTask) {
    if (getTaskType() == Type.FIRST) {
        LOG.info("[ServerConnector]start to run first task {}", serviceUpdateTask);
    } else {
        LOG.debug("[ServerConnector]start to run task {}", serviceUpdateTask);
    }
    GrpcConnector grpcConnector = (GrpcConnector) serverConnector;
    ConnectionManager connectionManager = grpcConnector.getConnectionManager();
    ClusterType clusterType = targetClusterType.get();
    boolean clusterReady = connectionManager.checkReady(clusterType);
    if (!clusterReady) {
        // 没有ready,就重试
        LOG.info("{} service is not ready", clusterType);
        grpcConnector.retryServiceUpdateTask(serviceUpdateTask);
        return;
    }
    if (grpcConnector.isDestroyed()) {
        LOG.info("{} grpc connection is destroyed", clusterType);
        grpcConnector.retryServiceUpdateTask(serviceUpdateTask);
        return;
    }
    AtomicReference<SpecStreamClient> streamClientAtomicReference = grpcConnector.getStreamClient(clusterType);
    SpecStreamClient specStreamClient = streamClientAtomicReference.get();
    boolean available = checkStreamClientAvailable(specStreamClient, serviceUpdateTask);
    if (!available) {
        LOG.debug("[ServerConnector]start to get connection for task {}", serviceUpdateTask);
        Connection connection = null;
        try {
            connection = connectionManager.getConnection(GrpcUtil.OP_KEY_DISCOVER, clusterType);
        } catch (PolarisException e) {
            LOG.error("[ServerConnector]fail to get connection to {}", clusterType, e);
        }
        if (null == connection) {
            LOG.error("[ServerConnector]get null connection for {}", serviceUpdateTask);
            grpcConnector.retryServiceUpdateTask(serviceUpdateTask);
            return;
        }
        specStreamClient = new SpecStreamClient(connection, grpcConnector.getConnectionIdleTimeoutMs(), serviceUpdateTask);
        streamClientAtomicReference.set(specStreamClient);
        LOG.info("[ServerConnector]success to create stream client for task {}", serviceUpdateTask);
    }
    msgSendTime.set(System.currentTimeMillis());
    totalRequests.addAndGet(1);
    specStreamClient.sendRequest(serviceUpdateTask);
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) ClusterType(com.tencent.polaris.api.config.global.ClusterType)

Example 4 with ClusterType

use of com.tencent.polaris.api.config.global.ClusterType in project polaris-java by polarismesh.

the class PolarisConfigFileConnector method init.

@Override
public void init(InitContext ctx) throws PolarisException {
    CompletableFuture<String> readyFuture = new CompletableFuture<>();
    Map<ClusterType, CompletableFuture<String>> futures = new HashMap<>();
    futures.put(ClusterType.SERVICE_DISCOVER_CLUSTER, readyFuture);
    connectionManager = new ConnectionManager(ctx, null, futures);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ConnectionManager(com.tencent.polaris.plugins.connector.grpc.ConnectionManager) HashMap(java.util.HashMap) ClusterType(com.tencent.polaris.api.config.global.ClusterType)

Aggregations

ClusterType (com.tencent.polaris.api.config.global.ClusterType)4 HashMap (java.util.HashMap)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 PolarisException (com.tencent.polaris.api.exception.PolarisException)1 NamedThreadFactory (com.tencent.polaris.client.util.NamedThreadFactory)1 ConnectionManager (com.tencent.polaris.plugins.connector.grpc.ConnectionManager)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 CallerRunsPolicy (java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy)1