Search in sources :

Example 1 with MockRateLimiter

use of com.google.common.util.concurrent.MockRateLimiter in project alluxio by Alluxio.

the class FileDataManagerTest method persistFileRateLimiting.

/**
   * Tests the rate limiting functionality for asynchronous persistence.
   */
@Test
public void persistFileRateLimiting() throws Exception {
    Configuration.set(PropertyKey.WORKER_FILE_PERSIST_RATE_LIMIT_ENABLED, "true");
    Configuration.set(PropertyKey.WORKER_FILE_PERSIST_RATE_LIMIT, "100");
    mMockRateLimiter = new MockRateLimiter(Configuration.getBytes(PropertyKey.WORKER_FILE_PERSIST_RATE_LIMIT));
    mManager = new FileDataManager(mBlockWorker, mMockRateLimiter.getGuavaRateLimiter());
    long fileId = 1;
    List<Long> blockIds = Lists.newArrayList(1L, 2L, 3L);
    FileInfo fileInfo = new FileInfo();
    fileInfo.setPath("test");
    Mockito.when(mBlockWorker.getFileInfo(fileId)).thenReturn(fileInfo);
    BlockReader reader = Mockito.mock(BlockReader.class);
    for (long blockId : blockIds) {
        Mockito.when(mBlockWorker.lockBlock(Sessions.CHECKPOINT_SESSION_ID, blockId)).thenReturn(blockId);
        Mockito.when(mBlockWorker.readBlockRemote(Sessions.CHECKPOINT_SESSION_ID, blockId, blockId)).thenReturn(reader);
        BlockMeta mockedBlockMeta = PowerMockito.mock(BlockMeta.class);
        Mockito.when(mockedBlockMeta.getBlockSize()).thenReturn(100L);
        Mockito.when(mBlockWorker.getBlockMeta(Sessions.CHECKPOINT_SESSION_ID, blockId, blockId)).thenReturn(mockedBlockMeta);
    }
    String ufsRoot = Configuration.get(PropertyKey.UNDERFS_ADDRESS);
    Mockito.when(mUfs.isDirectory(ufsRoot)).thenReturn(true);
    OutputStream outputStream = Mockito.mock(OutputStream.class);
    // mock BufferUtils
    PowerMockito.mockStatic(BufferUtils.class);
    String dstPath = PathUtils.concatPath(ufsRoot, fileInfo.getPath());
    fileInfo.setUfsPath(dstPath);
    Mockito.when(mUfs.create(dstPath)).thenReturn(outputStream);
    Mockito.when(mUfs.create(Mockito.anyString(), Mockito.any(CreateOptions.class))).thenReturn(outputStream);
    Mockito.when(mMockFileSystem.getStatus(Mockito.any(AlluxioURI.class))).thenReturn(new URIStatus(fileInfo));
    mManager.lockBlocks(fileId, blockIds);
    mManager.persistFile(fileId, blockIds);
    List<String> expectedEvents = Lists.newArrayList("R0.00", "R1.00", "R1.00");
    assertEquals(expectedEvents, mMockRateLimiter.readEventsAndClear());
    // Simulate waiting for 1 second.
    mMockRateLimiter.sleepMillis(1000);
    mManager.lockBlocks(fileId, blockIds);
    mManager.persistFile(fileId, blockIds);
    // The first write will go through immediately without throttling.
    expectedEvents = Lists.newArrayList("U1.00", "R0.00", "R1.00", "R1.00");
    assertEquals(expectedEvents, mMockRateLimiter.readEventsAndClear());
    // Repeat persistence without sleeping.
    mManager.lockBlocks(fileId, blockIds);
    mManager.persistFile(fileId, blockIds);
    expectedEvents = Lists.newArrayList("R1.00", "R1.00", "R1.00");
    assertEquals(expectedEvents, mMockRateLimiter.readEventsAndClear());
}
Also used : MockRateLimiter(com.google.common.util.concurrent.MockRateLimiter) BlockReader(alluxio.worker.block.io.BlockReader) OutputStream(java.io.OutputStream) URIStatus(alluxio.client.file.URIStatus) CreateOptions(alluxio.underfs.options.CreateOptions) FileInfo(alluxio.wire.FileInfo) BlockMeta(alluxio.worker.block.meta.BlockMeta) AlluxioURI(alluxio.AlluxioURI) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with MockRateLimiter

use of com.google.common.util.concurrent.MockRateLimiter in project alluxio by Alluxio.

the class FileDataManagerTest method before.

@Before
public void before() throws Exception {
    mUfs = Mockito.mock(UnderFileSystem.class);
    mBlockWorker = Mockito.mock(BlockWorker.class);
    mMockRateLimiter = new MockRateLimiter(Configuration.getBytes(PropertyKey.WORKER_FILE_PERSIST_RATE_LIMIT));
    mManager = new FileDataManager(mBlockWorker, mMockRateLimiter.getGuavaRateLimiter());
    mMockFileSystem = PowerMockito.mock(FileSystem.class);
    PowerMockito.mockStatic(FileSystem.Factory.class);
    Mockito.when(FileSystem.Factory.get()).thenReturn(mMockFileSystem);
    Mockito.when(mUfs.isDirectory(Mockito.anyString())).thenReturn(true);
    mUfsFactory = Mockito.mock(UnderFileSystemFactory.class);
    Mockito.when(mUfsFactory.supportsPath(Mockito.anyString())).thenReturn(true);
    Mockito.when(mUfsFactory.create(Mockito.anyString(), Mockito.anyObject())).thenReturn(mUfs);
    UnderFileSystemRegistry.register(mUfsFactory);
}
Also used : MockRateLimiter(com.google.common.util.concurrent.MockRateLimiter) FileSystem(alluxio.client.file.FileSystem) UnderFileSystem(alluxio.underfs.UnderFileSystem) UnderFileSystem(alluxio.underfs.UnderFileSystem) UnderFileSystemFactory(alluxio.underfs.UnderFileSystemFactory) BlockWorker(alluxio.worker.block.BlockWorker) Before(org.junit.Before)

Aggregations

MockRateLimiter (com.google.common.util.concurrent.MockRateLimiter)2 AlluxioURI (alluxio.AlluxioURI)1 FileSystem (alluxio.client.file.FileSystem)1 URIStatus (alluxio.client.file.URIStatus)1 UnderFileSystem (alluxio.underfs.UnderFileSystem)1 UnderFileSystemFactory (alluxio.underfs.UnderFileSystemFactory)1 CreateOptions (alluxio.underfs.options.CreateOptions)1 FileInfo (alluxio.wire.FileInfo)1 BlockWorker (alluxio.worker.block.BlockWorker)1 BlockReader (alluxio.worker.block.io.BlockReader)1 BlockMeta (alluxio.worker.block.meta.BlockMeta)1 OutputStream (java.io.OutputStream)1 Before (org.junit.Before)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1