Search in sources :

Example 1 with PageNotFoundException

use of alluxio.exception.PageNotFoundException in project alluxio by Alluxio.

the class RocksPageStore method delete.

@Override
public void delete(PageId pageId) throws PageNotFoundException {
    try {
        byte[] key = getKeyFromPageId(pageId);
        mDb.delete(key);
    } catch (RocksDBException e) {
        throw new PageNotFoundException("Failed to remove page", e);
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) PageNotFoundException(alluxio.exception.PageNotFoundException)

Example 2 with PageNotFoundException

use of alluxio.exception.PageNotFoundException in project alluxio by Alluxio.

the class LocalPageStore method delete.

@Override
public void delete(PageId pageId) throws IOException, PageNotFoundException {
    Path p = getFilePath(pageId);
    if (!Files.exists(p)) {
        throw new PageNotFoundException(p.toString());
    }
    Files.delete(p);
    // Cleaning up parent directory may lead to a race condition if one thread is removing a page as
    // well as its parent dir corresponding to the fileId, while another thread is adding
    // a different page from the same file in the same directory.
    // Note that, because (1) the chance of this type of racing is really low and
    // (2) even a race happens, the only penalty is an extra cache put failure;
    // whereas without the cleanup, there can be an unbounded amount of empty directories
    // uncleaned which takes an unbounded amount of space possibly.
    // We have seen the overhead goes up to a few hundred GBs due to inode storage overhead
    // TODO(binfan): remove the coupled fileId/pagIdex encoding with storage path, so the total
    // number of directories can be bounded.
    Path parent = Preconditions.checkNotNull(p.getParent(), "parent of cache file should not be null");
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(parent)) {
        if (!stream.iterator().hasNext()) {
            Files.delete(parent);
        }
    }
}
Also used : Path(java.nio.file.Path) PageNotFoundException(alluxio.exception.PageNotFoundException)

Example 3 with PageNotFoundException

use of alluxio.exception.PageNotFoundException 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 4 with PageNotFoundException

use of alluxio.exception.PageNotFoundException in project alluxio by Alluxio.

the class LocalCacheManager method get.

@Override
public int get(PageId pageId, int pageOffset, int bytesToRead, byte[] buffer, int offsetInBuffer, CacheContext cacheContext) {
    Preconditions.checkArgument(pageOffset <= mPageSize, "Read exceeds page boundary: offset=%s size=%s", pageOffset, mPageSize);
    Preconditions.checkArgument(bytesToRead <= buffer.length - offsetInBuffer, "buffer does not have enough space: bufferLength=%s offsetInBuffer=%s bytesToRead=%s", buffer.length, offsetInBuffer, bytesToRead);
    LOG.debug("get({},pageOffset={}) enters", pageId, pageOffset);
    if (mState.get() == NOT_IN_USE) {
        Metrics.GET_NOT_READY_ERRORS.inc();
        Metrics.GET_ERRORS.inc();
        return -1;
    }
    ReadWriteLock pageLock = getPageLock(pageId);
    try (LockResource r = new LockResource(pageLock.readLock())) {
        try (LockResource r2 = new LockResource(mMetaLock.readLock())) {
            // check if page exists and refresh LRU items
            mMetaStore.getPageInfo(pageId);
        } catch (PageNotFoundException e) {
            LOG.debug("get({},pageOffset={}) fails due to page not found", pageId, pageOffset);
            return 0;
        }
        int bytesRead = getPage(pageId, pageOffset, bytesToRead, buffer, offsetInBuffer);
        if (bytesRead <= 0) {
            Metrics.GET_ERRORS.inc();
            Metrics.GET_STORE_READ_ERRORS.inc();
            // something is wrong to read this page, let's remove it from meta store
            try (LockResource r2 = new LockResource(mMetaLock.writeLock())) {
                mMetaStore.removePage(pageId);
            } catch (PageNotFoundException e) {
                // best effort to remove this page from meta store and ignore the exception
                Metrics.CLEANUP_GET_ERRORS.inc();
            }
            return -1;
        }
        LOG.debug("get({},pageOffset={}) exits", pageId, pageOffset);
        return bytesRead;
    }
}
Also used : PageNotFoundException(alluxio.exception.PageNotFoundException) LockResource(alluxio.resource.LockResource) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 5 with PageNotFoundException

use of alluxio.exception.PageNotFoundException in project alluxio by Alluxio.

the class PageStoreTest method helloWorldTest.

@Test
public void helloWorldTest() throws Exception {
    String msg = "Hello, World!";
    byte[] msgBytes = msg.getBytes();
    PageId id = new PageId("0", 0);
    mPageStore.put(id, msgBytes);
    byte[] buf = new byte[1024];
    assertEquals(msgBytes.length, mPageStore.get(id, buf));
    assertArrayEquals(msgBytes, Arrays.copyOfRange(buf, 0, msgBytes.length));
    mPageStore.delete(id);
    try {
        mPageStore.get(id, buf);
        fail();
    } catch (PageNotFoundException e) {
    // Test completed successfully;
    }
}
Also used : PageId(alluxio.client.file.cache.PageId) PageNotFoundException(alluxio.exception.PageNotFoundException) Test(org.junit.Test)

Aggregations

PageNotFoundException (alluxio.exception.PageNotFoundException)10 PageId (alluxio.client.file.cache.PageId)3 LockResource (alluxio.resource.LockResource)3 IOException (java.io.IOException)3 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)3 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)3 Path (java.nio.file.Path)2 RocksDBException (org.rocksdb.RocksDBException)2 CacheScope (alluxio.client.quota.CacheScope)1 ResourceExhaustedException (alluxio.exception.status.ResourceExhaustedException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 Test (org.junit.Test)1