Search in sources :

Example 1 with CacheHandler

use of com.tencent.polaris.api.plugin.registry.CacheHandler 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 CacheHandler

use of com.tencent.polaris.api.plugin.registry.CacheHandler in project polaris-java by polarismesh.

the class InMemoryRegistry method loadFileCache.

private void loadFileCache(String persistPath) {
    LOG.info("start to load local cache files from {}", persistPath);
    Map<ServiceEventKey, Message> loadCachedServices = messagePersistHandler.loadPersistedServices(ResponseProto.DiscoverResponse.getDefaultInstance());
    for (Map.Entry<ServiceEventKey, Message> entry : loadCachedServices.entrySet()) {
        ServiceEventKey svcEventKey = entry.getKey();
        Message message = entry.getValue();
        if (null == message) {
            LOG.warn("load local cache, response is null, service event:{}", svcEventKey);
            continue;
        }
        CacheHandler cacheHandler = cacheHandlers.get(svcEventKey.getEventType());
        if (null == cacheHandler) {
            LOG.warn("[LocalRegistry]resource type {} not registered, ignore the file", svcEventKey.getEventType());
            continue;
        }
        CacheObject cacheObject = new CacheObject(cacheHandler, svcEventKey, this, message);
        resourceMap.put(svcEventKey, cacheObject);
    }
    LOG.info("loaded {} services from local cache", loadCachedServices.size());
}
Also used : Message(com.google.protobuf.Message) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) CacheHandler(com.tencent.polaris.api.plugin.registry.CacheHandler) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 3 with CacheHandler

use of com.tencent.polaris.api.plugin.registry.CacheHandler in project polaris-java by polarismesh.

the class InMemoryRegistry method loadRemoteValue.

/**
 * 加载资源
 *
 * @param svcEventKey 服务资源名
 * @param notifier    通知器
 * @throws PolarisException 异常
 */
private void loadRemoteValue(ServiceEventKey svcEventKey, EventCompleteNotifier notifier) throws PolarisException {
    checkDestroyed();
    CacheHandler handler = cacheHandlers.get(svcEventKey.getEventType());
    if (null == handler) {
        throw new PolarisException(ErrorCode.INTERNAL_ERROR, String.format("[LocalRegistry] unRegistered resource type %s", svcEventKey.getEventType()));
    }
    CacheObject cacheObject = resourceMap.computeIfAbsent(svcEventKey, serviceEventKey -> new CacheObject(handler, svcEventKey, InMemoryRegistry.this));
    // 添加监听器
    cacheObject.addNotifier(notifier);
    // 触发往serverConnector注册
    if (cacheObject.startRegister()) {
        LOG.info("[LocalRegistry]start to register service handler for {}", svcEventKey);
        try {
            connector.registerServiceHandler(enhanceServiceEventHandler(new ServiceEventHandler(svcEventKey, cacheObject)));
        } catch (Throwable e) {
            PolarisException polarisException;
            if (e instanceof PolarisException) {
                polarisException = (PolarisException) e;
            } else {
                polarisException = new PolarisException(ErrorCode.INTERNAL_ERROR, String.format("exception occurs while registering service handler for %s", svcEventKey));
            }
            cacheObject.resumeUnRegistered(polarisException);
            throw polarisException;
        }
        if (svcEventKey.getEventType() == EventType.INSTANCE) {
            // 注册了监听后,认为是被用户需要的服务,加入serviceSet
            services.put(svcEventKey.getServiceKey(), true);
        }
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) CacheHandler(com.tencent.polaris.api.plugin.registry.CacheHandler) ServiceEventHandler(com.tencent.polaris.api.plugin.server.ServiceEventHandler)

Aggregations

CacheHandler (com.tencent.polaris.api.plugin.registry.CacheHandler)3 PolarisException (com.tencent.polaris.api.exception.PolarisException)2 Message (com.google.protobuf.Message)1 ServerServiceInfo (com.tencent.polaris.api.plugin.compose.ServerServiceInfo)1 ServiceEventHandler (com.tencent.polaris.api.plugin.server.ServiceEventHandler)1 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)1 NamedThreadFactory (com.tencent.polaris.client.util.NamedThreadFactory)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1