Search in sources :

Example 1 with SubspaceProvider

use of com.apple.foundationdb.record.provider.foundationdb.SubspaceProvider in project fdb-record-layer by FoundationDB.

the class ReadVersionRecordStoreStateCache method get.

@Nonnull
@Override
public CompletableFuture<FDBRecordStoreStateCacheEntry> get(@Nonnull FDBRecordStore recordStore, @Nonnull FDBRecordStoreBase.StoreExistenceCheck existenceCheck) {
    final FDBRecordContext context = recordStore.getContext();
    validateContext(context);
    if (context.hasDirtyStoreState()) {
        // If a store info header has been modified during the course of this transaction's run,
        // don't go to the cache as its results may be stale.
        context.increment(FDBStoreTimer.Counts.STORE_STATE_CACHE_MISS);
        return FDBRecordStoreStateCacheEntry.load(recordStore, existenceCheck);
    } else {
        return context.getReadVersionAsync().thenCompose(readVersion -> {
            final SubspaceProvider subspaceProvider = recordStore.getSubspaceProvider();
            CompletableFuture<FDBRecordStoreStateCacheEntry> storeStateFuture = cache.orElseGet(Pair.of(subspaceProvider, readVersion), () -> {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug(KeyValueLogMessage.of("store state cache miss", subspaceProvider.logKey(), subspaceProvider.toString(context), LogMessageKeys.READ_VERSION, readVersion));
                }
                context.increment(FDBStoreTimer.Counts.STORE_STATE_CACHE_MISS);
                return FDBRecordStoreStateCacheEntry.load(recordStore, existenceCheck);
            }).thenCompose(storeState -> storeState.handleCachedState(context, existenceCheck).thenApply(vignore -> storeState));
            if (context.getTimer() != null && MoreAsyncUtil.isCompletedNormally(storeStateFuture)) {
                context.increment(FDBStoreTimer.Counts.STORE_STATE_CACHE_HIT);
            }
            return storeStateFuture;
        });
    }
}
Also used : MoreAsyncUtil(com.apple.foundationdb.async.MoreAsyncUtil) Logger(org.slf4j.Logger) LogMessageKeys(com.apple.foundationdb.record.logging.LogMessageKeys) SubspaceProvider(com.apple.foundationdb.record.provider.foundationdb.SubspaceProvider) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) LoggerFactory(org.slf4j.LoggerFactory) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) CompletableFuture(java.util.concurrent.CompletableFuture) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) RecordCoreArgumentException(com.apple.foundationdb.record.RecordCoreArgumentException) AsyncLoadingCache(com.apple.foundationdb.record.AsyncLoadingCache) KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) FDBRecordStore(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStore) Pair(org.apache.commons.lang3.tuple.Pair) API(com.apple.foundationdb.annotation.API) Nonnull(javax.annotation.Nonnull) FDBRecordStoreBase(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreBase) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) SubspaceProvider(com.apple.foundationdb.record.provider.foundationdb.SubspaceProvider) Nonnull(javax.annotation.Nonnull)

Example 2 with SubspaceProvider

use of com.apple.foundationdb.record.provider.foundationdb.SubspaceProvider in project fdb-record-layer by FoundationDB.

the class MetaDataVersionStampStoreStateCache method get.

@Nonnull
@Override
public CompletableFuture<FDBRecordStoreStateCacheEntry> get(@Nonnull FDBRecordStore recordStore, @Nonnull FDBRecordStoreBase.StoreExistenceCheck existenceCheck) {
    final FDBRecordContext context = recordStore.getContext();
    validateContext(context);
    if (context.hasDirtyStoreState()) {
        recordStore.increment(FDBStoreTimer.Counts.STORE_STATE_CACHE_MISS);
        return FDBRecordStoreStateCacheEntry.load(recordStore, existenceCheck);
    }
    final SubspaceProvider subspaceProvider = recordStore.getSubspaceProvider();
    final FDBRecordStoreStateCacheEntry existingEntry = cache.getIfPresent(subspaceProvider);
    if (existingEntry == null) {
        recordStore.increment(FDBStoreTimer.Counts.STORE_STATE_CACHE_MISS);
        return FDBRecordStoreStateCacheEntry.load(recordStore, existenceCheck).whenComplete((cacheEntry, err) -> {
            if (err == null && cacheEntry.getRecordStoreState().getStoreHeader().getCacheable()) {
                addToCache(subspaceProvider, cacheEntry);
            }
        });
    } else {
        return recordStore.getContext().getMetaDataVersionStampAsync(IsolationLevel.SNAPSHOT).thenCompose(metaDataVersionStamp -> {
            if (metaDataVersionStamp == null || existingEntry.getMetaDataVersionStamp() == null || ByteArrayUtil.compareUnsigned(metaDataVersionStamp, existingEntry.getMetaDataVersionStamp()) != 0) {
                recordStore.increment(FDBStoreTimer.Counts.STORE_STATE_CACHE_MISS);
                return FDBRecordStoreStateCacheEntry.load(recordStore, existenceCheck).whenComplete((cacheEntry, err) -> {
                    if (err == null && metaDataVersionStamp != null) {
                        if (cacheEntry.getRecordStoreState().getStoreHeader().getCacheable()) {
                            addToCache(subspaceProvider, cacheEntry);
                        } else {
                            invalidateOlderEntry(subspaceProvider, metaDataVersionStamp);
                        }
                    }
                });
            } else {
                recordStore.increment(FDBStoreTimer.Counts.STORE_STATE_CACHE_HIT);
                return existingEntry.handleCachedState(context, existenceCheck).thenApply(ignore -> existingEntry);
            }
        });
    }
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) SubspaceProvider(com.apple.foundationdb.record.provider.foundationdb.SubspaceProvider) Nonnull(javax.annotation.Nonnull)

Aggregations

FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)2 SubspaceProvider (com.apple.foundationdb.record.provider.foundationdb.SubspaceProvider)2 Nonnull (javax.annotation.Nonnull)2 API (com.apple.foundationdb.annotation.API)1 MoreAsyncUtil (com.apple.foundationdb.async.MoreAsyncUtil)1 AsyncLoadingCache (com.apple.foundationdb.record.AsyncLoadingCache)1 RecordCoreArgumentException (com.apple.foundationdb.record.RecordCoreArgumentException)1 KeyValueLogMessage (com.apple.foundationdb.record.logging.KeyValueLogMessage)1 LogMessageKeys (com.apple.foundationdb.record.logging.LogMessageKeys)1 FDBDatabase (com.apple.foundationdb.record.provider.foundationdb.FDBDatabase)1 FDBRecordStore (com.apple.foundationdb.record.provider.foundationdb.FDBRecordStore)1 FDBRecordStoreBase (com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreBase)1 FDBStoreTimer (com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Pair (org.apache.commons.lang3.tuple.Pair)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1