Search in sources :

Example 11 with TimeService

use of org.infinispan.commons.time.TimeService in project keycloak by keycloak.

the class PersistenceManagerImpl method start.

@Override
@Start
public void start() {
    enabled = configuration.persistence().usingStores();
    if (!enabled)
        return;
    preloaded = false;
    segmentCount = configuration.clustering().hash().numSegments();
    isInvalidationCache = configuration.clustering().cacheMode().isInvalidation();
    long stamp = lock.writeLock();
    try {
        Completable storeStartup = Flowable.fromIterable(configuration.persistence().stores()).concatMapSingle(storeConfiguration -> {
            NonBlockingStore<?, ?> actualStore = storeFromConfiguration(storeConfiguration);
            NonBlockingStore<?, ?> nonBlockingStore;
            if (storeConfiguration.async().enabled()) {
                nonBlockingStore = new AsyncNonBlockingStore<>(actualStore);
            } else {
                nonBlockingStore = actualStore;
            }
            InitializationContextImpl ctx = new InitializationContextImpl(storeConfiguration, cache.wired(), keyPartitioner, persistenceMarshaller, timeService, byteBufferFactory, marshallableEntryFactory, nonBlockingExecutor, globalConfiguration, blockingManager, nonBlockingManager);
            CompletionStage<Void> stage = nonBlockingStore.start(ctx).whenComplete((ignore, t) -> {
                // On exception, just put a status with only the store - this way we can still invoke stop on it later
                if (t != null) {
                    stores.add(new StoreStatus(nonBlockingStore, null, null));
                }
            });
            return Completable.fromCompletionStage(stage).toSingle(() -> new StoreStatus(nonBlockingStore, storeConfiguration, updateCharacteristics(nonBlockingStore, nonBlockingStore.characteristics(), storeConfiguration)));
        }).doOnNext(stores::add).delay(status -> {
            if (status.config.purgeOnStartup()) {
                return Flowable.fromCompletable(Completable.fromCompletionStage(status.store.clear()));
            }
            return Flowable.empty();
        }).ignoreElements();
        long interval = configuration.persistence().availabilityInterval();
        if (interval > 0) {
            storeStartup = storeStartup.doOnComplete(() -> availabilityTask = nonBlockingManager.scheduleWithFixedDelay(this::pollStoreAvailability, interval, interval, MILLISECONDS));
        }
        // Blocks here waiting for stores and availability task to start if needed
        storeStartup.blockingAwait();
        allSegmentedOrShared = allStoresSegmentedOrShared();
    } catch (Throwable t) {
        log.debug("PersistenceManagerImpl encountered an exception during startup of stores", t);
        throw t;
    } finally {
        lock.unlockWrite(stamp);
    }
}
Also used : WriteCommand(org.infinispan.commands.write.WriteCommand) ComponentName(org.infinispan.factories.annotations.ComponentName) LogFactory(org.infinispan.util.logging.LogFactory) InvocationHelper(org.infinispan.cache.impl.InvocationHelper) GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) KnownComponentNames(org.infinispan.factories.KnownComponentNames) StoreUnavailableException(org.infinispan.persistence.spi.StoreUnavailableException) Scopes(org.infinispan.factories.scopes.Scopes) AdvancedCache(org.infinispan.AdvancedCache) TxInvocationContext(org.infinispan.context.impl.TxInvocationContext) InitializationContextImpl(org.infinispan.persistence.InitializationContextImpl) Transaction(javax.transaction.Transaction) MethodHandles(java.lang.invoke.MethodHandles) ByRef(org.infinispan.commons.util.ByRef) Set(java.util.Set) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) IntSet(org.infinispan.commons.util.IntSet) CompletionStage(java.util.concurrent.CompletionStage) ComposedSegmentedLoadWriteStore(org.infinispan.persistence.support.ComposedSegmentedLoadWriteStore) DelegatingNonBlockingStore(org.infinispan.persistence.support.DelegatingNonBlockingStore) InternalCacheValue(org.infinispan.container.entries.InternalCacheValue) DataConversion(org.infinispan.encoding.DataConversion) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) Single(io.reactivex.rxjava3.core.Single) TransactionalStoreInterceptor(org.infinispan.interceptors.impl.TransactionalStoreInterceptor) NonBlockingStoreAdapter(org.infinispan.persistence.support.NonBlockingStoreAdapter) EnumUtil(org.infinispan.commons.util.EnumUtil) InternalExpirationManager(org.infinispan.expiration.impl.InternalExpirationManager) CacheLoaderInterceptor(org.infinispan.interceptors.impl.CacheLoaderInterceptor) Start(org.infinispan.factories.annotations.Start) ArrayList(java.util.ArrayList) Maybe(io.reactivex.rxjava3.core.Maybe) StoreConfiguration(org.infinispan.configuration.cache.StoreConfiguration) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry) DataWriteCommand(org.infinispan.commands.write.DataWriteCommand) AsyncInterceptor(org.infinispan.interceptors.AsyncInterceptor) InternalEntryFactory(org.infinispan.container.impl.InternalEntryFactory) Properties(java.util.Properties) Executor(java.util.concurrent.Executor) Publisher(org.reactivestreams.Publisher) InvalidTransactionException(javax.transaction.InvalidTransactionException) IOException(java.io.IOException) CacheNotifier(org.infinispan.notifications.cachelistener.CacheNotifier) Inject(org.infinispan.factories.annotations.Inject) MVCCEntry(org.infinispan.container.entries.MVCCEntry) InvalidateCommand(org.infinispan.commands.write.InvalidateCommand) Function(io.reactivex.rxjava3.functions.Function) PersistenceMarshaller(org.infinispan.marshall.persistence.PersistenceMarshaller) SystemException(javax.transaction.SystemException) StampedLock(java.util.concurrent.locks.StampedLock) DistributionManager(org.infinispan.distribution.DistributionManager) AggregateCompletionStage(org.infinispan.util.concurrent.AggregateCompletionStage) IntSets(org.infinispan.commons.util.IntSets) MarshallableEntryFactory(org.infinispan.persistence.spi.MarshallableEntryFactory) Stop(org.infinispan.factories.annotations.Stop) InvocationContext(org.infinispan.context.InvocationContext) ComponentRef(org.infinispan.factories.impl.ComponentRef) SingleSegmentPublisher(org.infinispan.persistence.support.SingleSegmentPublisher) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) Lifecycle(org.infinispan.commons.api.Lifecycle) Scope(org.infinispan.factories.scopes.Scope) ByteBufferFactory(org.infinispan.commons.io.ByteBufferFactory) PERSISTENCE(org.infinispan.util.logging.Log.PERSISTENCE) CommandsFactory(org.infinispan.commands.CommandsFactory) AsyncNonBlockingStore(org.infinispan.persistence.async.AsyncNonBlockingStore) Predicate(java.util.function.Predicate) Collection(java.util.Collection) AbstractCacheTransaction(org.infinispan.transaction.impl.AbstractCacheTransaction) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) GuardedBy(net.jcip.annotations.GuardedBy) SegmentPublisherWrapper(org.infinispan.persistence.support.SegmentPublisherWrapper) PersistenceException(org.infinispan.persistence.spi.PersistenceException) LocalOnlyCacheLoader(org.infinispan.persistence.spi.LocalOnlyCacheLoader) CompletionStages(org.infinispan.util.concurrent.CompletionStages) CompletableFuture(java.util.concurrent.CompletableFuture) AbstractSegmentedStoreConfiguration(org.infinispan.configuration.cache.AbstractSegmentedStoreConfiguration) FlagBitSets(org.infinispan.context.impl.FlagBitSets) InternalMetadataImpl(org.infinispan.metadata.impl.InternalMetadataImpl) HashSet(java.util.HashSet) Characteristic(org.infinispan.persistence.spi.NonBlockingStore.Characteristic) BiPredicate(java.util.function.BiPredicate) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) Log(org.infinispan.util.logging.Log) CacheWriterInterceptor(org.infinispan.interceptors.impl.CacheWriterInterceptor) Flowable(io.reactivex.rxjava3.core.Flowable) Iterator(java.util.Iterator) NonBlockingStore(org.infinispan.persistence.spi.NonBlockingStore) Util(org.infinispan.commons.util.Util) NonBlockingManager(org.infinispan.util.concurrent.NonBlockingManager) Completable(io.reactivex.rxjava3.core.Completable) PutMapCommand(org.infinispan.commands.write.PutMapCommand) NotSupportedException(javax.transaction.NotSupportedException) PersistenceUtil(org.infinispan.persistence.internal.PersistenceUtil) Configuration(org.infinispan.configuration.cache.Configuration) BlockingManager(org.infinispan.util.concurrent.BlockingManager) TransactionManager(javax.transaction.TransactionManager) TimeService(org.infinispan.commons.time.TimeService) InputStream(java.io.InputStream) AsyncNonBlockingStore(org.infinispan.persistence.async.AsyncNonBlockingStore) Completable(io.reactivex.rxjava3.core.Completable) InitializationContextImpl(org.infinispan.persistence.InitializationContextImpl) DelegatingNonBlockingStore(org.infinispan.persistence.support.DelegatingNonBlockingStore) AsyncNonBlockingStore(org.infinispan.persistence.async.AsyncNonBlockingStore) NonBlockingStore(org.infinispan.persistence.spi.NonBlockingStore) CompletionStage(java.util.concurrent.CompletionStage) AggregateCompletionStage(org.infinispan.util.concurrent.AggregateCompletionStage) Start(org.infinispan.factories.annotations.Start)

Example 12 with TimeService

use of org.infinispan.commons.time.TimeService in project infinispan by infinispan.

the class AbstractClusterMBeanTest method createManager.

private void createManager(String jmxDomain) {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.clustering().cacheMode(CacheMode.REPL_SYNC).statistics().enable();
    GlobalConfigurationBuilder gcb = GlobalConfigurationBuilder.defaultClusteredBuilder();
    gcb.cacheContainer().statistics(true).metrics().accurateSize(true).metrics().accurateSize(true).jmx().enabled(true).domain(jmxDomain).mBeanServerLookup(mBeanServerLookup).serialization().addContextInitializer(TestDataSCI.INSTANCE);
    gcb.addModule(TestGlobalConfigurationBuilder.class).testGlobalComponent(TimeService.class.getName(), timeService);
    addClusterEnabledCacheManager(gcb, cb);
}
Also used : TestGlobalConfigurationBuilder(org.infinispan.commands.module.TestGlobalConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) TestGlobalConfigurationBuilder(org.infinispan.commands.module.TestGlobalConfigurationBuilder) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) ControlledTimeService(org.infinispan.commons.time.ControlledTimeService) TimeService(org.infinispan.commons.time.TimeService) TestGlobalConfigurationBuilder(org.infinispan.commands.module.TestGlobalConfigurationBuilder)

Example 13 with TimeService

use of org.infinispan.commons.time.TimeService in project infinispan by infinispan.

the class PersistenceMockUtil method mockCache.

private static Cache mockCache(String nodeName, Configuration configuration, TimeService timeService, ClassAllowList allowList, ScheduledExecutorService timeoutScheduledExecutor) {
    String cacheName = "mock-cache";
    AdvancedCache cache = mock(AdvancedCache.class, RETURNS_DEEP_STUBS);
    GlobalConfiguration gc = new GlobalConfigurationBuilder().transport().nodeName(nodeName).build();
    Set<String> cachesSet = new HashSet<>();
    EmbeddedCacheManager cm = mock(EmbeddedCacheManager.class);
    when(cm.getCacheManagerConfiguration()).thenReturn(gc);
    when(cm.getClassAllowList()).thenReturn(new ClassAllowList());
    GlobalComponentRegistry gcr = new GlobalComponentRegistry(gc, cm, cachesSet, TestModuleRepository.defaultModuleRepository(), mock(ConfigurationManager.class));
    BasicComponentRegistry gbcr = gcr.getComponent(BasicComponentRegistry.class);
    gbcr.replaceComponent(TimeService.class.getName(), timeService, true);
    gbcr.replaceComponent(KnownComponentNames.TIMEOUT_SCHEDULE_EXECUTOR, timeoutScheduledExecutor, false);
    ComponentRegistry registry = new ComponentRegistry(cacheName, configuration, cache, gcr, configuration.getClass().getClassLoader());
    when(cache.getCacheManager().getGlobalComponentRegistry()).thenReturn(gcr);
    when(cache.getClassLoader()).thenReturn(PersistenceMockUtil.class.getClassLoader());
    when(cache.getCacheManager().getCacheManagerConfiguration()).thenReturn(gc);
    when(cache.getCacheManager().getClassAllowList()).thenReturn(allowList);
    when(cache.getName()).thenReturn(cacheName);
    when(cache.getAdvancedCache()).thenReturn(cache);
    when(cache.getComponentRegistry()).thenReturn(registry);
    when(cache.getStatus()).thenReturn(ComponentStatus.RUNNING);
    when(cache.getCacheConfiguration()).thenReturn(configuration);
    return cache;
}
Also used : GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) TimeService(org.infinispan.commons.time.TimeService) GlobalComponentRegistry(org.infinispan.factories.GlobalComponentRegistry) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) BasicComponentRegistry(org.infinispan.factories.impl.BasicComponentRegistry) ComponentRegistry(org.infinispan.factories.ComponentRegistry) BasicComponentRegistry(org.infinispan.factories.impl.BasicComponentRegistry) GlobalComponentRegistry(org.infinispan.factories.GlobalComponentRegistry) ClassAllowList(org.infinispan.commons.configuration.ClassAllowList) AdvancedCache(org.infinispan.AdvancedCache) ConfigurationManager(org.infinispan.configuration.ConfigurationManager) HashSet(java.util.HashSet)

Example 14 with TimeService

use of org.infinispan.commons.time.TimeService in project infinispan by infinispan.

the class TestInfinispanRegionFactory method createCacheManager.

@Override
protected EmbeddedCacheManager createCacheManager(Properties properties, ServiceRegistry serviceRegistry) {
    // If the cache manager has been provided by calling setCacheManager, don't create a new one
    EmbeddedCacheManager cacheManager = getCacheManager();
    if (cacheManager != null) {
        return cacheManager;
    } else if (providedManager != null) {
        cacheManager = providedManager;
    } else {
        ConfigurationBuilderHolder holder = DefaultCacheManagerProvider.loadConfiguration(serviceRegistry, properties);
        configurationHook.amendConfiguration(holder);
        cacheManager = new DefaultCacheManager(holder, true);
    }
    if (afterManagerCreated != null) {
        afterManagerCreated.accept(cacheManager);
    }
    if (timeService != null) {
        BasicComponentRegistry basicComponentRegistry = cacheManager.getGlobalComponentRegistry().getComponent(BasicComponentRegistry.class);
        basicComponentRegistry.replaceComponent(TimeService.class.getName(), timeService, false);
        basicComponentRegistry.rewire();
    }
    return cacheManager;
}
Also used : DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) ConfigurationBuilderHolder(org.infinispan.configuration.parsing.ConfigurationBuilderHolder) BasicComponentRegistry(org.infinispan.factories.impl.BasicComponentRegistry) TimeService(org.infinispan.commons.time.TimeService) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager)

Example 15 with TimeService

use of org.infinispan.commons.time.TimeService in project infinispan by infinispan.

the class EmbeddedTimeServiceTest method testExpired.

public void testExpired() {
    TimeService timeService = new EmbeddedTimeService() {

        @Override
        public long time() {
            return 10;
        }
    };
    assertTrue(timeService.isTimeExpired(-1));
    assertTrue(timeService.isTimeExpired(0));
    assertTrue(timeService.isTimeExpired(9));
    assertTrue(timeService.isTimeExpired(10));
    assertFalse(timeService.isTimeExpired(11));
}
Also used : TimeService(org.infinispan.commons.time.TimeService)

Aggregations

TimeService (org.infinispan.commons.time.TimeService)18 ControlledTimeService (org.infinispan.util.ControlledTimeService)4 HashSet (java.util.HashSet)3 AdvancedCache (org.infinispan.AdvancedCache)3 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)3 EmbeddedTimeService (org.infinispan.util.EmbeddedTimeService)3 Log (org.infinispan.util.logging.Log)3 LogFactory (org.infinispan.util.logging.LogFactory)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionStage (java.util.concurrent.CompletionStage)2 TimeUnit (java.util.concurrent.TimeUnit)2 Configuration (org.infinispan.configuration.cache.Configuration)2 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)2 DataContainer (org.infinispan.container.DataContainer)2 KeyPartitioner (org.infinispan.distribution.ch.KeyPartitioner)2 DataConversion (org.infinispan.encoding.DataConversion)2