Search in sources :

Example 1 with NamedThreadFactory

use of com.tencent.polaris.client.util.NamedThreadFactory in project polaris-java by polarismesh.

the class InMemoryRegistry method init.

@Override
public void init(InitContext ctx) throws PolarisException {
    // 获取系统服务配置
    Collection<ServerServiceInfo> serverServices = ctx.getServerServices();
    for (ServerServiceInfo serverServiceInfo : serverServices) {
        if (serverServiceInfo.getClusterType() == ClusterType.SERVICE_DISCOVER_CLUSTER) {
            hasDiscoverCluster = true;
        }
        serverServiceMap.put(serverServiceInfo.getServiceKey(), serverServiceInfo);
    }
    // 加载cacheHandler
    ServiceLoader<CacheHandler> handlers = ServiceLoader.load(CacheHandler.class);
    for (CacheHandler handler : handlers) {
        cacheHandlers.put(handler.getTargetEventType(), handler);
    }
    // Load server connector.
    connector = (ServerConnector) ctx.getPlugins().getPlugin(PluginTypes.SERVER_CONNECTOR.getBaseType(), ctx.getValueContext().getServerConnectorProtocol());
    // 构建基础属性
    String persistDir = ctx.getConfig().getConsumer().getLocalCache().getPersistDir();
    int maxReadRetry = ctx.getConfig().getConsumer().getLocalCache().getPersistMaxReadRetry();
    int maxWriteRetry = ctx.getConfig().getConsumer().getLocalCache().getPersistMaxWriteRetry();
    long retryIntervalMs = ctx.getConfig().getConsumer().getLocalCache().getPersistRetryInterval();
    this.serviceRefreshIntervalMs = ctx.getConfig().getConsumer().getLocalCache().getServiceRefreshInterval();
    boolean configPersistEnable = ctx.getConfig().getConsumer().getLocalCache().isPersistEnable();
    persistEnable = configPersistEnable && StringUtils.isNotBlank(persistDir);
    // 启动本地缓存
    if (persistEnable) {
        messagePersistHandler = new MessagePersistHandler(persistDir, maxWriteRetry, maxReadRetry, retryIntervalMs);
        try {
            messagePersistHandler.init();
        } catch (IOException e) {
            throw new PolarisException(ErrorCode.PLUGIN_ERROR, String.format("plugin %s init failed", getName()), e);
        }
        loadFileCache(persistDir);
    }
    NamedThreadFactory namedThreadFactory = new NamedThreadFactory(getName());
    serviceExpireTimeMs = ctx.getConfig().getConsumer().getLocalCache().getServiceExpireTime();
    persistExecutor = Executors.newSingleThreadExecutor(namedThreadFactory);
    expireExecutor = Executors.newSingleThreadScheduledExecutor(namedThreadFactory);
    if (hasDiscoverCluster) {
        serverServicesDiscoverExecutor = new ThreadPoolExecutor(0, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), namedThreadFactory);
    }
}
Also used : NamedThreadFactory(com.tencent.polaris.client.util.NamedThreadFactory) IOException(java.io.IOException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ServerServiceInfo(com.tencent.polaris.api.plugin.compose.ServerServiceInfo) PolarisException(com.tencent.polaris.api.exception.PolarisException) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) CacheHandler(com.tencent.polaris.api.plugin.registry.CacheHandler)

Example 2 with NamedThreadFactory

use of com.tencent.polaris.client.util.NamedThreadFactory 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 3 with NamedThreadFactory

use of com.tencent.polaris.client.util.NamedThreadFactory in project polaris-java by polarismesh.

the class CompositeConnector method init.

@Override
public void init(InitContext ctx) throws PolarisException {
    if (getName().equals(ctx.getValueContext().getServerConnectorProtocol())) {
        if (CollectionUtils.isEmpty(serverConnectors)) {
            serverConnectors = new ArrayList<>();
        }
        List<ServerConnectorConfigImpl> serverConnectorConfigs = ctx.getConfig().getGlobal().getServerConnectors();
        for (ServerConnectorConfigImpl serverConnectorConfig : serverConnectorConfigs) {
            DestroyableServerConnector serverConnector = (DestroyableServerConnector) ctx.getPlugins().getPlugin(PluginTypes.SERVER_CONNECTOR.getBaseType(), serverConnectorConfig.getProtocol());
            serverConnector.init(ctx);
            serverConnectors.add(serverConnector);
        }
        sendDiscoverExecutor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory(getName() + "-send-discovery"), new CallerRunsPolicy());
        sendDiscoverExecutor.setMaximumPoolSize(1);
        updateServiceExecutor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory(getName() + "-update-service"));
        updateServiceExecutor.setMaximumPoolSize(1);
        initialized = true;
    }
}
Also used : ServerConnectorConfigImpl(com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) NamedThreadFactory(com.tencent.polaris.client.util.NamedThreadFactory) CallerRunsPolicy(java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy) DestroyableServerConnector(com.tencent.polaris.plugins.connector.common.DestroyableServerConnector)

Aggregations

NamedThreadFactory (com.tencent.polaris.client.util.NamedThreadFactory)3 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)2 CallerRunsPolicy (java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy)2 ClusterType (com.tencent.polaris.api.config.global.ClusterType)1 PolarisException (com.tencent.polaris.api.exception.PolarisException)1 ServerServiceInfo (com.tencent.polaris.api.plugin.compose.ServerServiceInfo)1 CacheHandler (com.tencent.polaris.api.plugin.registry.CacheHandler)1 ServerConnectorConfigImpl (com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl)1 DestroyableServerConnector (com.tencent.polaris.plugins.connector.common.DestroyableServerConnector)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1