private void createRemoteValueWrapperInterceptor(ComponentRegistry cr, Configuration cfg) { RemoteValueWrapperInterceptor wrapperInterceptor = cr.getComponent(RemoteValueWrapperInterceptor.class); if (wrapperInterceptor == null) { wrapperInterceptor = new RemoteValueWrapperInterceptor(); // Interceptor registration not needed, core configuration handling // already does it for all custom interceptors - UNLESS the InterceptorChain already exists in // the component registry! AsyncInterceptorChain ic = cr.getComponent(AsyncInterceptorChain.class); ConfigurationBuilder builder = new ConfigurationBuilder().read(cfg); InterceptorConfigurationBuilder interceptorBuilder = builder.customInterceptors().addInterceptor(); interceptorBuilder.interceptor(wrapperInterceptor); if (cfg.invocationBatching().enabled()) { if (ic != null) ic.addInterceptorAfter(wrapperInterceptor, BatchingInterceptor.class); interceptorBuilder.after(BatchingInterceptor.class); } else { if (ic != null) ic.addInterceptorAfter(wrapperInterceptor, InvocationContextInterceptor.class); interceptorBuilder.after(InvocationContextInterceptor.class); } if (ic != null) { cr.registerComponent(wrapperInterceptor, RemoteValueWrapperInterceptor.class); } cfg.customInterceptors().interceptors(builder.build().customInterceptors().interceptors()); } }
public static void replicateCommand(Cache cache, VisitableCommand command) throws Throwable { ComponentRegistry cr = extractComponentRegistry(cache); InterceptorChain ic = cr.getComponent(InterceptorChain.class); InvocationContextContainer icc = cr.getComponent(InvocationContextContainer.class); InvocationContext ctxt = icc.createInvocationContext(true, -1); ic.invoke(ctxt, command); }
@Override public CompletableFuture<Object> invokeAsync() throws Throwable { GlobalComponentRegistry globalComponentRegistry = cacheManager.getGlobalComponentRegistry(); ComponentRegistry cacheComponentRegistry = globalComponentRegistry.getNamedComponentRegistry(cacheName); String name = cacheName.toString(); if (cacheComponentRegistry != null) { cacheComponentRegistry.getComponent(PersistenceManager.class).setClearOnStop(true); cacheComponentRegistry.getComponent(CacheJmxRegistration.class).setUnregisterCacheMBean(true); cacheComponentRegistry.getComponent(PassivationManager.class).skipPassivationOnStop(true); Cache<?, ?> cache = cacheManager.getCache(name, false); if (cache != null) { cache.stop(); } } globalComponentRegistry.removeCache(name); // Remove cache configuration and remove it from the computed cache name list globalComponentRegistry.getComponent(ConfigurationManager.class).removeConfiguration(name); // Remove cache from dependency graph //noinspection unchecked globalComponentRegistry .getComponent(DependencyGraph.class, CACHE_DEPENDENCY_GRAPH) .remove(cacheName); return CompletableFutures.completedNull(); }
@Override public void cacheStarted(ComponentRegistry cr, String cacheName) { Configuration configuration = cr.getComponent(Configuration.class); boolean indexingEnabled = configuration.indexing().enabled(); if (!indexingEnabled) { if (verifyChainContainsQueryInterceptor(cr)) { throw new IllegalStateException( "It was NOT expected to find the Query interceptor registered in the InterceptorChain as indexing was disabled, but it was found"); } return; } if (!verifyChainContainsQueryInterceptor(cr)) { throw new IllegalStateException( "It was expected to find the Query interceptor registered in the InterceptorChain but it wasn't found"); } // initializing the query module command initializer. // we can t inject Cache and CacheManager with @inject in there Cache<?, ?> cache = cr.getComponent(Cache.class); CommandInitializer initializer = cr.getComponent(CommandInitializer.class); EmbeddedCacheManager cacheManager = cr.getGlobalComponentRegistry().getComponent(EmbeddedCacheManager.class); initializer.setCache(cache, cacheManager); QueryBox queryBox = new QueryBox(); queryBox.setCache(cache.getAdvancedCache()); cr.registerComponent(queryBox, QueryBox.class); // Register query mbeans registerQueryMBeans(cache.getAdvancedCache(), cr, cacheName); }
public static SearchFactoryIntegrator extractSearchFactory(Cache cache) { ComponentRegistry componentRegistry = cache.getAdvancedCache().getComponentRegistry(); SearchFactoryIntegrator component = componentRegistry.getComponent(SearchFactoryIntegrator.class); Assert.assertNotNull(component); return component; }
protected static GMUVersionGenerator getGMUVersionGenerator( GlobalComponentRegistry globalComponentRegistry, String cacheName) { ComponentRegistry componentRegistry = globalComponentRegistry.getNamedComponentRegistry(cacheName); VersionGenerator versionGenerator = componentRegistry.getComponent(VersionGenerator.class); return toGMUVersionGenerator(versionGenerator); }
private void replaceLockManager(ComponentRegistry componentRegistry) { LockManager oldLockManager = componentRegistry.getComponent(LockManager.class); LockManager newLockManager = new ExtendedStatisticLockManager(oldLockManager, cacheStatisticManager, timeService); log.replaceComponent("LockManager", oldLockManager, newLockManager); componentRegistry.registerComponent(newLockManager, LockManager.class); }
/** * Replaces a component in a running cache * * @param cache cache in which to replace component * @param componentType component type of which to replace * @param replacementComponent new instance * @param rewire if true, ComponentRegistry.rewire() is called after replacing. * @return the original component that was replaced */ public static <T> T replaceComponent( Cache<?, ?> cache, Class<T> componentType, T replacementComponent, boolean rewire) { ComponentRegistry cr = extractComponentRegistry(cache); T old = cr.getComponent(componentType); cr.registerComponent(replacementComponent, componentType); if (rewire) cr.rewire(); return old; }
protected void assertIndexingKnows(BasicCache<?, ?> cache, Class<?>... types) { ComponentRegistry cr = ((Cache) cache).getAdvancedCache().getComponentRegistry(); SearchIntegrator searchIntegrator = cr.getComponent(SearchIntegrator.class); assertNotNull(searchIntegrator); HashSet<Class<?>> expectedTypes = new HashSet<Class<?>>(Arrays.asList(types)); HashSet<Class<?>> indexedTypes = new HashSet<Class<?>>(searchIntegrator.getIndexedTypes()); assertEquals(expectedTypes, indexedTypes); }
@Override public void cacheStopped(ComponentRegistry cr, String cacheName) { SearchFactoryIntegrator searchFactoryIntegrator = searchFactoriesToShutdown.remove(cacheName); if (searchFactoryIntegrator != null) { searchFactoryIntegrator.close(); } Configuration cfg = cr.getComponent(Configuration.class); removeQueryInterceptorFromConfiguration(cfg); }
@Override public void cacheStarted(ComponentRegistry cr, String cacheName) { InternalCacheRegistry icr = cr.getGlobalComponentRegistry().getComponent(InternalCacheRegistry.class); if (!icr.isInternalCache(cacheName)) { Configuration cfg = cr.getComponent(Configuration.class); boolean isIndexed = cfg.indexing().index().isEnabled(); boolean isCompatMode = cfg.compatibility().enabled(); if (isIndexed && !isCompatMode) { if (!verifyChainContainsRemoteValueWrapperInterceptor(cr)) { throw new IllegalStateException( "It was expected to find the RemoteValueWrapperInterceptor registered in the InterceptorChain but it wasn't found"); } } else if (verifyChainContainsRemoteValueWrapperInterceptor(cr)) { throw new IllegalStateException( "It was NOT expected to find the RemoteValueWrapperInterceptor registered in the InterceptorChain as indexing was disabled, but it was found"); } ProtobufMetadataManagerImpl protobufMetadataManager = (ProtobufMetadataManagerImpl) cr.getGlobalComponentRegistry().getComponent(ProtobufMetadataManager.class); SerializationContext serCtx = protobufMetadataManager.getSerializationContext(); cr.registerComponent(new ProtobufMatcher(serCtx), ProtobufMatcher.class); if (isCompatMode) { SearchIntegrator searchFactory = cr.getComponent(SearchIntegrator.class); CompatibilityReflectionMatcher compatibilityReflectionMatcher; if (searchFactory == null) { compatibilityReflectionMatcher = new CompatibilityReflectionMatcher(serCtx); } else { compatibilityReflectionMatcher = new CompatibilityReflectionMatcher(serCtx, searchFactory); } cr.registerComponent(compatibilityReflectionMatcher, CompatibilityReflectionMatcher.class); } AdvancedCache<?, ?> cache = cr.getComponent(Cache.class).getAdvancedCache(); RemoteQueryEngine remoteQueryEngine = new RemoteQueryEngine(cache, isIndexed, isCompatMode, serCtx); cr.registerComponent(remoteQueryEngine, RemoteQueryEngine.class); } }
/** * Replaces the existing interceptor chain in the cache wih one represented by the interceptor * passed in. This utility updates dependencies on all components that rely on the interceptor * chain as well. * * @param cache cache that needs to be altered * @param interceptor the first interceptor in the new chain. */ public static void replaceInterceptorChain(Cache cache, CommandInterceptor interceptor) { ComponentRegistry cr = extractComponentRegistry(cache); // make sure all interceptors here are wired. CommandInterceptor i = interceptor; do { cr.wireDependencies(i); } while ((i = i.getNext()) != null); InterceptorChain inch = cr.getComponent(InterceptorChain.class); inch.setFirstInChain(interceptor); }
/** * Verifies if the indexing interceptor is aware of a specific list of types. * * @param cache the cache containing the indexes * @param types vararg listing the types the indexing engine should know */ private void assertIndexingKnows(Cache<Object, Object> cache, Class<?>... types) { ComponentRegistry cr = cache.getAdvancedCache().getComponentRegistry(); SearchIntegrator searchIntegrator = cr.getComponent(SearchIntegrator.class); assertNotNull(searchIntegrator); Map<Class<?>, EntityIndexBinding> indexBindingForEntity = searchIntegrator.unwrap(ExtendedSearchIntegrator.class).getIndexBindings(); assertNotNull(indexBindingForEntity); Set<Class<?>> keySet = indexBindingForEntity.keySet(); assertEquals(types.length, keySet.size()); assertTrue(keySet.containsAll(Arrays.asList(types))); }
private void replaceRpcManager(ComponentRegistry componentRegistry) { RpcManager oldRpcManager = componentRegistry.getComponent(RpcManager.class); if (oldRpcManager == null) { // local mode return; } RpcManager newRpcManager = new ExtendedStatisticRpcManager(oldRpcManager, cacheStatisticManager, timeService); log.replaceComponent("RpcManager", oldRpcManager, newRpcManager); componentRegistry.registerComponent(newRpcManager, RpcManager.class); this.rpcManager = newRpcManager; }
/** * Replaces an existing interceptor of the given type in the interceptor chain with a new * interceptor instance passed as parameter. * * @param replacingInterceptor the interceptor to add to the interceptor chain * @param toBeReplacedInterceptorType the type of interceptor that should be swapped with the new * one * @return true if the interceptor was replaced */ public static boolean replaceInterceptor( Cache cache, CommandInterceptor replacingInterceptor, Class<? extends CommandInterceptor> toBeReplacedInterceptorType) { ComponentRegistry cr = extractComponentRegistry(cache); // make sure all interceptors here are wired. CommandInterceptor i = replacingInterceptor; do { cr.wireDependencies(i); } while ((i = i.getNext()) != null); InterceptorChain inch = cr.getComponent(InterceptorChain.class); return inch.replaceInterceptor(replacingInterceptor, toBeReplacedInterceptorType); }
protected AbstractCacheStream( Address localAddress, boolean parallel, DistributionManager dm, Supplier<CacheStream<CacheEntry>> supplier, ClusterStreamManager<Object> csm, boolean includeLoader, int distributedBatchSize, Executor executor, ComponentRegistry registry) { this.localAddress = localAddress; this.parallel = parallel; this.dm = dm; this.supplier = supplier; this.csm = csm; this.includeLoader = includeLoader; this.distributedBatchSize = distributedBatchSize; this.executor = executor; this.registry = registry; this.partition = registry.getComponent(PartitionHandlingManager.class); keyEquivalence = registry.getComponent(Configuration.class).dataContainer().keyEquivalence(); intermediateOperations = new ArrayDeque<>(); }
@Override protected void start() { super.start(); log.startStreamSummaryInterceptor(); streamSummaryContainer = StreamSummaryContainer.getOrCreateStreamLibContainer(cache); streamSummaryContainer.setEnabled(true); distributionManager = cache.getAdvancedCache().getDistributionManager(); ComponentRegistry componentRegistry = cache.getAdvancedCache().getComponentRegistry(); LockManager oldLockManager = componentRegistry.getComponent(LockManager.class); LockManager newLockManager = new TopKeyLockManager(oldLockManager, streamSummaryContainer); log.replaceComponent("LockManager", oldLockManager, newLockManager); componentRegistry.registerComponent(newLockManager, LockManager.class); componentRegistry.rewire(); }
private void createQueryInterceptorIfNeeded( ComponentRegistry cr, Configuration cfg, SearchFactoryIntegrator searchFactory) { QueryInterceptor queryInterceptor = cr.getComponent(QueryInterceptor.class); if (queryInterceptor == null) { queryInterceptor = buildQueryInterceptor(cfg, searchFactory); // Interceptor registration not needed, core configuration handling // already does it for all custom interceptors - UNLESS the InterceptorChain already exists in // the component registry! InterceptorChain ic = cr.getComponent(InterceptorChain.class); ConfigurationBuilder builder = new ConfigurationBuilder().read(cfg); InterceptorConfigurationBuilder interceptorBuilder = builder.customInterceptors().addInterceptor(); interceptorBuilder.interceptor(queryInterceptor); if (!cfg.transaction().transactionMode().isTransactional()) { if (ic != null) ic.addInterceptorAfter(queryInterceptor, NonTransactionalLockingInterceptor.class); interceptorBuilder.after(NonTransactionalLockingInterceptor.class); } else if (cfg.transaction().lockingMode() == LockingMode.OPTIMISTIC) { if (ic != null) ic.addInterceptorAfter(queryInterceptor, OptimisticLockingInterceptor.class); interceptorBuilder.after(OptimisticLockingInterceptor.class); } else { if (ic != null) ic.addInterceptorAfter(queryInterceptor, PessimisticLockingInterceptor.class); interceptorBuilder.after(PessimisticLockingInterceptor.class); } if (ic != null) { cr.registerComponent(queryInterceptor, QueryInterceptor.class); cr.registerComponent(queryInterceptor, queryInterceptor.getClass().getName(), true); } cfg.customInterceptors().interceptors(builder.build().customInterceptors().interceptors()); } }
@Override public void initialize( String indexName, Properties properties, Similarity similarity, WorkerBuildContext buildContext) { super.initialize(indexName, properties, similarity, buildContext); ServiceManager serviceManager = buildContext.getServiceManager(); ComponentRegistryService componentRegistryService = serviceManager.requestService(ComponentRegistryService.class); ComponentRegistry componentRegistry = componentRegistryService.getComponentRegistry(); Cache cache = componentRegistry.getComponent(Cache.class); cache.addListener(new TopologyChangeListener()); closeIndexWriter(); }
@Override public void cacheStopping(ComponentRegistry cr, String cacheName) { // TODO move this to cacheStopped event (won't work right now as the ComponentRegistry is half // empty at that point: ISPN-1006) Object searchFactoryIntegrator = cr.getComponent(SearchFactoryIntegrator.class); if (searchFactoryIntegrator != null && searchFactoryIntegrator != REMOVED_REGISTRY_COMPONENT) { searchFactoriesToShutdown.put(cacheName, (SearchFactoryIntegrator) searchFactoryIntegrator); // free some memory by de-registering the SearchFactory cr.registerComponent(REMOVED_REGISTRY_COMPONENT, SearchFactoryIntegrator.class); } // Unregister MBeans if (mbeanServer != null) { String queryMBeanFilter = jmxDomain + ":" + getQueryGroupName(cacheName) + ",*"; JmxUtil.unregisterMBeans(queryMBeanFilter, mbeanServer); } }
private Properties addProgrammaticMappings(Properties indexingProperties, ComponentRegistry cr) { Iterator<ProgrammaticSearchMappingProvider> providers = ServiceLoader.load(ProgrammaticSearchMappingProvider.class).iterator(); if (providers.hasNext()) { SearchMapping mapping = (SearchMapping) indexingProperties.get(Environment.MODEL_MAPPING); if (mapping == null) { mapping = new SearchMapping(); Properties amendedProperties = new Properties(); amendedProperties.putAll(indexingProperties); amendedProperties.put(Environment.MODEL_MAPPING, mapping); indexingProperties = amendedProperties; } Cache cache = cr.getComponent(Cache.class); while (providers.hasNext()) { ProgrammaticSearchMappingProvider provider = providers.next(); provider.defineMappings(cache, mapping); } } return indexingProperties; }
private SearchFactoryIntegrator getSearchFactory( Properties indexingProperties, ComponentRegistry cr) { Object component = cr.getComponent(SearchFactoryIntegrator.class); SearchFactoryIntegrator searchFactory = null; if (component instanceof SearchFactoryIntegrator) { // could be the placeholder Object REMOVED_REGISTRY_COMPONENT searchFactory = (SearchFactoryIntegrator) component; } // defend against multiple initialization: if (searchFactory == null) { GlobalComponentRegistry globalComponentRegistry = cr.getGlobalComponentRegistry(); EmbeddedCacheManager uninitializedCacheManager = globalComponentRegistry.getComponent(EmbeddedCacheManager.class); indexingProperties = addProgrammaticMappings(indexingProperties, cr); // Set up the search factory for Hibernate Search first. SearchConfiguration config = new SearchableCacheConfiguration( new Class[0], indexingProperties, uninitializedCacheManager, cr); searchFactory = new SearchFactoryBuilder().configuration(config).buildSearchFactory(); cr.registerComponent(searchFactory, SearchFactoryIntegrator.class); } return searchFactory; }
private boolean verifyChainContainsRemoteValueWrapperInterceptor(ComponentRegistry cr) { AsyncInterceptorChain interceptorChain = cr.getComponent(AsyncInterceptorChain.class); return interceptorChain != null && interceptorChain.containsInterceptorType(RemoteValueWrapperInterceptor.class, true); }
@Override public void cacheStopped(ComponentRegistry cr, String cacheName) { Configuration cfg = cr.getComponent(Configuration.class); removeRemoteIndexingInterceptorFromConfig(cfg); }
@Override public void handleInjection(ComponentRegistry registry) { super.handleInjection(registry); cache = registry.getComponent(Cache.class); }
private boolean verifyChainContainsQueryInterceptor(ComponentRegistry cr) { InterceptorChain interceptorChain = cr.getComponent(InterceptorChain.class); return interceptorChain.containsInterceptorType(QueryInterceptor.class, true); }
/** Extracts a component of a given type from the cache's internal component registry */ public static <T> T extractComponent(Cache cache, Class<T> componentType) { ComponentRegistry cr = extractComponentRegistry(cache); return cr.getComponent(componentType); }
private PersistenceManager getPersistenceManager(Cache<Object, Object> cache) { ComponentRegistry cr = cache.getAdvancedCache().getComponentRegistry(); return cr.getComponent(PersistenceManager.class); }
public static AbstractDelegatingMarshaller extractCacheMarshaller(Cache cache) { ComponentRegistry cr = (ComponentRegistry) extractField(cache, "componentRegistry"); StreamingMarshaller marshaller = cr.getComponent(StreamingMarshaller.class, KnownComponentNames.CACHE_MARSHALLER); return (AbstractDelegatingMarshaller) marshaller; }