Search in sources :

Example 1 with CacheScope

use of alluxio.client.quota.CacheScope in project presto by prestodb.

the class PrestoCacheContext method build.

public static PrestoCacheContext build(String cacheIdentifier, HiveFileContext hiveFileContext, boolean cacheQuotaEnabled) {
    PrestoCacheContext context = new PrestoCacheContext(hiveFileContext);
    context.setCacheIdentifier(cacheIdentifier);
    if (cacheQuotaEnabled) {
        CacheScope scope = CacheScope.create(hiveFileContext.getCacheQuota().getIdentity());
        context.setCacheScope(scope);
        if (hiveFileContext.getCacheQuota().getQuota().isPresent()) {
            context.setCacheQuota(new CacheQuota(ImmutableMap.of(scope.level(), hiveFileContext.getCacheQuota().getQuota().get().toBytes())));
        } else {
            context.setCacheQuota(CacheQuota.UNLIMITED);
        }
    }
    return context;
}
Also used : CacheScope(alluxio.client.quota.CacheScope) CacheQuota(alluxio.client.quota.CacheQuota)

Example 2 with CacheScope

use of alluxio.client.quota.CacheScope in project alluxio by Alluxio.

the class QuotaMetaStore method removePage.

@Override
public PageInfo removePage(PageId pageId) throws PageNotFoundException {
    PageInfo pageInfo = super.removePage(pageId);
    for (CacheScope cacheScope = pageInfo.getScope(); cacheScope != CacheScope.GLOBAL; cacheScope = cacheScope.parent()) {
        mBytesInScope.computeIfPresent(cacheScope, (k, v) -> v - pageInfo.getPageSize());
        CacheEvictor evictor = mCacheEvictors.computeIfAbsent(cacheScope, k -> mSupplier.get());
        evictor.updateOnDelete(pageId);
    }
    return pageInfo;
}
Also used : CacheEvictor(alluxio.client.file.cache.evictor.CacheEvictor) CacheScope(alluxio.client.quota.CacheScope)

Example 3 with CacheScope

use of alluxio.client.quota.CacheScope in project alluxio by Alluxio.

the class QuotaMetaStore method getPageInfo.

@Override
public PageInfo getPageInfo(PageId pageId) throws PageNotFoundException {
    PageInfo pageInfo = super.getPageInfo(pageId);
    for (CacheScope cacheScope = pageInfo.getScope(); cacheScope != CacheScope.GLOBAL; cacheScope = cacheScope.parent()) {
        CacheEvictor evictor = mCacheEvictors.computeIfAbsent(cacheScope, k -> mSupplier.get());
        evictor.updateOnPut(pageId);
    }
    return pageInfo;
}
Also used : CacheEvictor(alluxio.client.file.cache.evictor.CacheEvictor) CacheScope(alluxio.client.quota.CacheScope)

Example 4 with CacheScope

use of alluxio.client.quota.CacheScope in project alluxio by Alluxio.

the class LocalCacheManager method putAttempt.

private PutResult putAttempt(PageId pageId, byte[] page, CacheContext cacheContext, boolean forcedToEvict) {
    LOG.debug("putInternal({},{} bytes) enters", pageId, page.length);
    PageInfo victimPageInfo = null;
    CacheScope scopeToEvict;
    ReadWriteLock pageLock = getPageLock(pageId);
    try (LockResource r = new LockResource(pageLock.writeLock())) {
        try (LockResource r2 = new LockResource(mMetaLock.writeLock())) {
            if (mMetaStore.hasPage(pageId)) {
                LOG.debug("{} is already inserted before", pageId);
                // TODO(binfan): we should return more informative result in the future
                return PutResult.OK;
            }
            scopeToEvict = checkScopeToEvict(page.length, cacheContext.getCacheScope(), cacheContext.getCacheQuota(), forcedToEvict);
            if (scopeToEvict == null) {
                mMetaStore.addPage(pageId, new PageInfo(pageId, page.length, cacheContext.getCacheScope()));
            } else {
                if (mQuotaEnabled) {
                    victimPageInfo = ((QuotaMetaStore) mMetaStore).evict(scopeToEvict);
                } else {
                    victimPageInfo = mMetaStore.evict();
                }
                if (victimPageInfo == null) {
                    LOG.error("Unable to find page to evict: space used {}, page length {}, cache size {}", mMetaStore.bytes(), page.length, mCacheSize);
                    Metrics.PUT_EVICTION_ERRORS.inc();
                    return PutResult.OTHER;
                }
            }
        }
        if (scopeToEvict == null) {
            try {
                mPageStore.put(pageId, page);
                // Bytes written to the cache
                MetricsSystem.meter(MetricKey.CLIENT_CACHE_BYTES_WRITTEN_CACHE.getName()).mark(page.length);
                return PutResult.OK;
            } catch (ResourceExhaustedException e) {
                undoAddPage(pageId);
                LOG.error("Failed to add page {} to pageStore", pageId, e);
                Metrics.PUT_STORE_WRITE_NO_SPACE_ERRORS.inc();
                return PutResult.NO_SPACE_LEFT;
            } catch (IOException e) {
                undoAddPage(pageId);
                LOG.error("Failed to add page {} to pageStore", pageId, e);
                Metrics.PUT_STORE_WRITE_ERRORS.inc();
                return PutResult.OTHER;
            }
        }
    }
    Pair<ReadWriteLock, ReadWriteLock> pageLockPair = getPageLockPair(pageId, victimPageInfo.getPageId());
    try (LockResource r1 = new LockResource(pageLockPair.getFirst().writeLock());
        LockResource r2 = new LockResource(pageLockPair.getSecond().writeLock())) {
        // metalock. Evictor will be updated inside metastore.
        try (LockResource r3 = new LockResource(mMetaLock.writeLock())) {
            if (mMetaStore.hasPage(pageId)) {
                return PutResult.OK;
            }
            try {
                mMetaStore.removePage(victimPageInfo.getPageId());
            } catch (PageNotFoundException e) {
                LOG.debug("Page {} is unavailable to evict, likely due to a benign race", victimPageInfo.getPageId());
                return PutResult.BENIGN_RACING;
            }
            // Check if we are able to insert page after evicting victim page
            scopeToEvict = checkScopeToEvict(page.length, cacheContext.getCacheScope(), cacheContext.getCacheQuota(), false);
            if (scopeToEvict == null) {
                mMetaStore.addPage(pageId, new PageInfo(pageId, page.length, cacheContext.getCacheScope()));
            }
        }
        // phase2: remove victim and add new page in pagestore
        // Regardless of enoughSpace, delete the victim as it has been removed from the metastore
        PageId victim = victimPageInfo.getPageId();
        try {
            mPageStore.delete(victim);
            // Bytes evicted from the cache
            MetricsSystem.meter(MetricKey.CLIENT_CACHE_BYTES_EVICTED.getName()).mark(victimPageInfo.getPageSize());
            // Errors when adding pages
            MetricsSystem.meter(MetricKey.CLIENT_CACHE_PAGES_EVICTED.getName()).mark();
        } catch (IOException | PageNotFoundException e) {
            if (scopeToEvict == null) {
                // Failed to evict page, remove new page from metastore as there will not be enough space
                undoAddPage(pageId);
            }
            LOG.error("Failed to delete page {} from pageStore", pageId, e);
            Metrics.PUT_STORE_DELETE_ERRORS.inc();
            return PutResult.OTHER;
        }
        if (scopeToEvict != null) {
            return PutResult.INSUFFICIENT_SPACE_EVICTED;
        }
        try {
            mPageStore.put(pageId, page);
            // Bytes written to the cache
            MetricsSystem.meter(MetricKey.CLIENT_CACHE_BYTES_WRITTEN_CACHE.getName()).mark(page.length);
            return PutResult.OK;
        } catch (ResourceExhaustedException e) {
            undoAddPage(pageId);
            LOG.error("Failed to add page {} to pageStore", pageId, e);
            Metrics.PUT_STORE_WRITE_NO_SPACE_ERRORS.inc();
            return PutResult.NO_SPACE_LEFT;
        } catch (IOException e) {
            // Failed to add page, remove new page from metastoree
            undoAddPage(pageId);
            LOG.error("Failed to add page {} to pageStore", pageId, e);
            Metrics.PUT_STORE_WRITE_ERRORS.inc();
            return PutResult.OTHER;
        }
    }
}
Also used : ResourceExhaustedException(alluxio.exception.status.ResourceExhaustedException) PageNotFoundException(alluxio.exception.PageNotFoundException) LockResource(alluxio.resource.LockResource) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) IOException(java.io.IOException) CacheScope(alluxio.client.quota.CacheScope)

Example 5 with CacheScope

use of alluxio.client.quota.CacheScope in project alluxio by Alluxio.

the class LocalCacheManagerTest method putWithQuotaEviction.

@Test
public void putWithQuotaEviction() throws Exception {
    mConf.set(PropertyKey.USER_CLIENT_CACHE_QUOTA_ENABLED, true);
    CacheScope partitionCacheScope = CacheScope.create("schema.table.partition");
    CacheScope tableCacheScope = CacheScope.create("schema.table");
    CacheScope schemaCacheScope = CacheScope.create("schema");
    CacheScope[] quotaCacheScopes = { partitionCacheScope, tableCacheScope, schemaCacheScope, CacheScope.GLOBAL };
    for (CacheScope cacheScope : quotaCacheScopes) {
        mMetaStore = new QuotaMetaStore(mConf);
        mPageStore = PageStore.create(PageStoreOptions.create(mConf));
        mCacheManager = createLocalCacheManager(mConf, mMetaStore, mPageStore);
        CacheQuota quota = new CacheQuota(ImmutableMap.of(cacheScope.level(), (long) PAGE1.length + PAGE2.length - 1));
        CacheContext context = CacheContext.defaults().setCacheScope(partitionCacheScope).setCacheQuota(quota);
        assertTrue(mCacheManager.put(PAGE_ID1, PAGE1, context));
        assertEquals(PAGE1.length, mCacheManager.get(PAGE_ID1, PAGE1.length, mBuf, 0));
        assertTrue(mCacheManager.put(PAGE_ID2, PAGE2, context));
        assertEquals(0, mCacheManager.get(PAGE_ID1, PAGE1.length, mBuf, 0));
        assertEquals(PAGE2.length, mCacheManager.get(PAGE_ID2, PAGE2.length, mBuf, 0));
    }
}
Also used : CacheContext(alluxio.client.file.CacheContext) CacheScope(alluxio.client.quota.CacheScope) CacheQuota(alluxio.client.quota.CacheQuota) Test(org.junit.Test)

Aggregations

CacheScope (alluxio.client.quota.CacheScope)14 CacheQuota (alluxio.client.quota.CacheQuota)9 Test (org.junit.Test)9 CacheContext (alluxio.client.file.CacheContext)8 CacheEvictor (alluxio.client.file.cache.evictor.CacheEvictor)3 PageNotFoundException (alluxio.exception.PageNotFoundException)1 ResourceExhaustedException (alluxio.exception.status.ResourceExhaustedException)1 LockResource (alluxio.resource.LockResource)1 IOException (java.io.IOException)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1