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);
}
}
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());
}
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);
}
}
}
Aggregations