Search in sources :

Example 1 with ByteBufferFactory

use of org.infinispan.commons.io.ByteBufferFactory in project infinispan by infinispan.

the class SingleFileStore method readFromDisk.

private MarshallableEntry<K, V> readFromDisk(FileEntry fe, Object key, boolean loadValue, boolean loadMetadata) {
    org.infinispan.commons.io.ByteBuffer valueBb = null;
    // If we only require the key, then no need to read disk
    if (!loadValue && !loadMetadata) {
        try {
            return entryFactory.create(key);
        } finally {
            fe.unlock();
        }
    }
    final byte[] data;
    try {
        // load serialized data from disk
        data = new byte[fe.keyLen + fe.dataLen + (loadMetadata ? fe.metadataLen + fe.internalMetadataLen : 0)];
        // The entry lock will prevent clear() from truncating the file at this point
        channel.read(ByteBuffer.wrap(data), fe.offset + KEY_POS_LATEST);
    } catch (Exception e) {
        throw new PersistenceException(e);
    } finally {
        // No need to keep the lock for deserialization.
        // FileEntry is immutable, so its members can't be changed by another thread.
        fe.unlock();
    }
    if (log.isTraceEnabled())
        log.tracef("Read entry %s at %d:%d", key, fe.offset, fe.actualSize());
    ByteBufferFactory factory = ctx.getByteBufferFactory();
    org.infinispan.commons.io.ByteBuffer keyBb = factory.newByteBuffer(data, 0, fe.keyLen);
    if (loadValue) {
        valueBb = factory.newByteBuffer(data, fe.keyLen, fe.dataLen);
    }
    if (loadMetadata) {
        long created = -1;
        long lastUsed = -1;
        org.infinispan.commons.io.ByteBuffer metadataBb = null;
        org.infinispan.commons.io.ByteBuffer internalMetadataBb = null;
        int offset = fe.keyLen + fe.dataLen;
        if (fe.metadataLen > 0) {
            int metaLength = fe.metadataLen - TIMESTAMP_BYTES;
            metadataBb = factory.newByteBuffer(data, offset, metaLength);
            offset += metaLength;
            ByteBuffer buffer = ByteBuffer.wrap(data, offset, TIMESTAMP_BYTES);
            if (fe.expiryTime > 0) {
                offset += TIMESTAMP_BYTES;
                created = buffer.getLong();
                lastUsed = buffer.getLong();
            }
        }
        if (fe.internalMetadataLen > 0) {
            internalMetadataBb = factory.newByteBuffer(data, offset, fe.internalMetadataLen);
        }
        return entryFactory.create(keyBb, valueBb, metadataBb, internalMetadataBb, created, lastUsed);
    }
    return entryFactory.create(keyBb, valueBb);
}
Also used : PersistenceException(org.infinispan.persistence.spi.PersistenceException) ByteBuffer(java.nio.ByteBuffer) PersistenceException(org.infinispan.persistence.spi.PersistenceException) IOException(java.io.IOException) ByteBufferFactory(org.infinispan.commons.io.ByteBufferFactory)

Example 2 with ByteBufferFactory

use of org.infinispan.commons.io.ByteBufferFactory 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 3 with ByteBufferFactory

use of org.infinispan.commons.io.ByteBufferFactory in project datawave by NationalSecurityAgency.

the class AccumuloCacheStore method decodeEntry.

private MarshalledEntry<K, V> decodeEntry(Map.Entry<Key, Value> entry, Object key, boolean loadValue, boolean loadMetadata) {
    if (entry != null) {
        ByteBufferFactory bbFactory = ctx.getByteBufferFactory();
        ByteBuffer buffer = ByteBuffer.wrap(entry.getValue().get(), 0, entry.getValue().getSize());
        int valueSize = buffer.getInt();
        int metadataSize = buffer.getInt();
        org.infinispan.commons.io.ByteBuffer valueBytes = null;
        if (loadValue) {
            valueBytes = bbFactory.newByteBuffer(entry.getValue().get(), buffer.position(), valueSize);
        }
        org.infinispan.commons.io.ByteBuffer metadataBytes = null;
        if (metadataSize > 0 && loadMetadata) {
            // Skip over value if we didn't read it in.
            if (!loadValue)
                buffer.position(buffer.position() + valueSize);
            metadataBytes = bbFactory.newByteBuffer(entry.getValue().get(), buffer.position() + valueSize, metadataSize);
        }
        // noinspection unchecked
        return ctx.getMarshalledEntryFactory().newMarshalledEntry(key, valueBytes, metadataBytes);
    } else {
        return null;
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) ByteBufferFactory(org.infinispan.commons.io.ByteBufferFactory)

Aggregations

ByteBufferFactory (org.infinispan.commons.io.ByteBufferFactory)3 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 PersistenceException (org.infinispan.persistence.spi.PersistenceException)2 Completable (io.reactivex.rxjava3.core.Completable)1 Flowable (io.reactivex.rxjava3.core.Flowable)1 Maybe (io.reactivex.rxjava3.core.Maybe)1 Single (io.reactivex.rxjava3.core.Single)1 Function (io.reactivex.rxjava3.functions.Function)1 InputStream (java.io.InputStream)1 MethodHandles (java.lang.invoke.MethodHandles)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Objects (java.util.Objects)1 Properties (java.util.Properties)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1