Search in sources :

Example 1 with Runtime

use of jnr.ffi.Runtime in project alluxio by Alluxio.

the class AlluxioFuseFileSystemTest method allocateNativeFileInfo.

// Allocate native memory for a FuseFileInfo data struct and return its pointer
private FuseFileInfo allocateNativeFileInfo() {
    final Runtime runtime = Runtime.getSystemRuntime();
    final Pointer pt = runtime.getMemoryManager().allocateTemporary(36, true);
    return FuseFileInfo.of(pt);
}
Also used : Runtime(jnr.ffi.Runtime) Pointer(jnr.ffi.Pointer)

Example 2 with Runtime

use of jnr.ffi.Runtime in project alluxio by Alluxio.

the class AlluxioFuseFileSystemTest method statfs.

@Test
public void statfs() throws Exception {
    Runtime runtime = Runtime.getSystemRuntime();
    Pointer pointer = runtime.getMemoryManager().allocateTemporary(4 * Constants.KB, true);
    Statvfs stbuf = Statvfs.of(pointer);
    int blockSize = 4 * Constants.KB;
    int totalBlocks = 4;
    int freeBlocks = 3;
    BlockMasterClient blockMasterClient = PowerMockito.mock(BlockMasterClient.class);
    PowerMockito.mockStatic(BlockMasterClient.Factory.class);
    when(BlockMasterClient.Factory.create(any())).thenReturn(blockMasterClient);
    BlockMasterInfo blockMasterInfo = new BlockMasterInfo();
    blockMasterInfo.setCapacityBytes(totalBlocks * blockSize);
    blockMasterInfo.setFreeBytes(freeBlocks * blockSize);
    when(blockMasterClient.getBlockMasterInfo(any())).thenReturn(blockMasterInfo);
    assertEquals(0, mFuseFs.statfs("/", stbuf));
    assertEquals(blockSize, stbuf.f_bsize.intValue());
    assertEquals(blockSize, stbuf.f_frsize.intValue());
    assertEquals(totalBlocks, stbuf.f_blocks.longValue());
    assertEquals(freeBlocks, stbuf.f_bfree.longValue());
    assertEquals(freeBlocks, stbuf.f_bavail.longValue());
    assertEquals(AlluxioFuseFileSystem.UNKNOWN_INODES, stbuf.f_files.intValue());
    assertEquals(AlluxioFuseFileSystem.UNKNOWN_INODES, stbuf.f_ffree.intValue());
    assertEquals(AlluxioFuseFileSystem.UNKNOWN_INODES, stbuf.f_favail.intValue());
    assertEquals(AlluxioFuseFileSystem.MAX_NAME_LENGTH, stbuf.f_namemax.intValue());
}
Also used : BlockMasterInfo(alluxio.wire.BlockMasterInfo) Runtime(jnr.ffi.Runtime) BlockMasterClient(alluxio.client.block.BlockMasterClient) Pointer(jnr.ffi.Pointer) Statvfs(ru.serce.jnrfuse.struct.Statvfs) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with Runtime

use of jnr.ffi.Runtime in project alluxio by Alluxio.

the class FuseFileInfo method of.

public static FuseFileInfo of(ByteBuffer buffer) {
    Runtime runtime = Runtime.getSystemRuntime();
    FuseFileInfo fi = new FuseFileInfo(runtime, buffer);
    fi.useMemory(jnr.ffi.Pointer.wrap(runtime, buffer));
    return fi;
}
Also used : Runtime(jnr.ffi.Runtime)

Example 4 with Runtime

use of jnr.ffi.Runtime in project alluxio by Alluxio.

the class AlluxioFuseFileSystemTest method write.

@Test
public void write() throws Exception {
    FileOutStream fos = mock(FileOutStream.class);
    AlluxioURI anyURI = any();
    CreateFilePOptions options = any();
    when(mFileSystem.createFile(anyURI, options)).thenReturn(fos);
    // open a file
    mFileInfo.flags.set(O_WRONLY.intValue());
    mFuseFs.create("/foo/bar", 0, mFileInfo);
    // prepare something to write into it
    Runtime r = Runtime.getSystemRuntime();
    Pointer ptr = r.getMemoryManager().allocateTemporary(4, true);
    byte[] expected = { 42, -128, 1, 3 };
    ptr.put(0, expected, 0, 4);
    mFuseFs.write("/foo/bar", ptr, 4, 0, mFileInfo);
    verify(fos).write(expected);
    // the second write is no-op because the writes must be sequential and overwriting is supported
    mFuseFs.write("/foo/bar", ptr, 4, 0, mFileInfo);
    verify(fos, times(1)).write(expected);
}
Also used : Runtime(jnr.ffi.Runtime) FileOutStream(alluxio.client.file.FileOutStream) CreateFilePOptions(alluxio.grpc.CreateFilePOptions) Pointer(jnr.ffi.Pointer) AlluxioURI(alluxio.AlluxioURI) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with Runtime

use of jnr.ffi.Runtime in project alluxio by Alluxio.

the class AlluxioFuseFileSystemTest method readOffset2.

@Test
public void readOffset2() throws Exception {
    // mocks set-up
    AlluxioURI expectedPath = BASE_EXPECTED_URI.join("/foo/bar");
    setUpOpenMock(expectedPath);
    FileInStream fakeInStream = mock(FileInStream.class);
    when(fakeInStream.read(any(byte[].class), anyInt(), anyInt())).then((Answer<Integer>) invocationOnMock -> {
        byte[] myDest = (byte[]) invocationOnMock.getArguments()[0];
        for (byte i = 0; i < (int) invocationOnMock.getArgument(2); i++) {
            myDest[i] = i;
        }
        return myDest.length;
    });
    AtomicInteger callCounter = new AtomicInteger();
    when(fakeInStream.remaining()).then((Answer<Long>) invocationOnMock -> {
        if (callCounter.getAndIncrement() == 0) {
            return 4L;
        } else {
            return 3L;
        }
    });
    when(mFileSystem.openFile(expectedPath)).thenReturn(fakeInStream);
    mFileInfo.flags.set(O_RDONLY.intValue());
    // prepare something to read to it
    Runtime r = Runtime.getSystemRuntime();
    Pointer ptr = r.getMemoryManager().allocateTemporary(4, true);
    // actual test
    mFuseFs.open("/foo/bar", mFileInfo);
    mFuseFs.read("/foo/bar", ptr, 4, 4, mFileInfo);
    final byte[] dst = new byte[0];
    ptr.get(0, dst, 0, 0);
    final byte[] expected = new byte[0];
    assertArrayEquals("Source and dst data should be equal", expected, dst);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CreateFilePOptions(alluxio.grpc.CreateFilePOptions) LoadingCache(com.google.common.cache.LoadingCache) FileStat(ru.serce.jnrfuse.struct.FileStat) BlockMasterClient(alluxio.client.block.BlockMasterClient) PropertyKey(alluxio.conf.PropertyKey) Mockito.doThrow(org.mockito.Mockito.doThrow) FileSystem(alluxio.client.file.FileSystem) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Mockito.atLeast(org.mockito.Mockito.atLeast) Statvfs(ru.serce.jnrfuse.struct.Statvfs) ImmutableMap(com.google.common.collect.ImmutableMap) BlockMasterInfo(alluxio.wire.BlockMasterInfo) Mockito.doNothing(org.mockito.Mockito.doNothing) O_WRONLY(jnr.constants.platform.OpenFlags.O_WRONLY) List(java.util.List) O_RDONLY(jnr.constants.platform.OpenFlags.O_RDONLY) Pointer(jnr.ffi.Pointer) ConfigurationTestUtils(alluxio.ConfigurationTestUtils) Mockito.atMost(org.mockito.Mockito.atMost) FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) InstancedConfiguration(alluxio.conf.InstancedConfiguration) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CreateDirectoryPOptions(alluxio.grpc.CreateDirectoryPOptions) RunWith(org.junit.runner.RunWith) SetAttributePOptions(alluxio.grpc.SetAttributePOptions) Runtime(jnr.ffi.Runtime) FileOutStream(alluxio.client.file.FileOutStream) Mode(alluxio.security.authorization.Mode) Answer(org.mockito.stubbing.Answer) ConfigurationRule(alluxio.ConfigurationRule) Constants(alluxio.Constants) ErrorCodes(ru.serce.jnrfuse.ErrorCodes) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) AlluxioURI(alluxio.AlluxioURI) FileInStream(alluxio.client.file.FileInStream) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Assume(org.junit.Assume) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) PowerMockito(org.powermock.api.mockito.PowerMockito) Before(org.junit.Before) FuseFileInfo(ru.serce.jnrfuse.struct.FuseFileInfo) FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) FileIncompleteException(alluxio.exception.FileIncompleteException) Mockito.never(org.mockito.Mockito.never) URIStatus(alluxio.client.file.URIStatus) Rule(org.junit.Rule) FileInfo(alluxio.wire.FileInfo) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Runtime(jnr.ffi.Runtime) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileInStream(alluxio.client.file.FileInStream) Pointer(jnr.ffi.Pointer) AlluxioURI(alluxio.AlluxioURI) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Runtime (jnr.ffi.Runtime)10 Pointer (jnr.ffi.Pointer)6 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 AlluxioURI (alluxio.AlluxioURI)4 BlockMasterClient (alluxio.client.block.BlockMasterClient)4 FileOutStream (alluxio.client.file.FileOutStream)4 CreateFilePOptions (alluxio.grpc.CreateFilePOptions)4 BlockMasterInfo (alluxio.wire.BlockMasterInfo)4 ConfigurationRule (alluxio.ConfigurationRule)3 ConfigurationTestUtils (alluxio.ConfigurationTestUtils)3 Constants (alluxio.Constants)3 FileInStream (alluxio.client.file.FileInStream)3 FileSystem (alluxio.client.file.FileSystem)3 URIStatus (alluxio.client.file.URIStatus)3 InstancedConfiguration (alluxio.conf.InstancedConfiguration)3 PropertyKey (alluxio.conf.PropertyKey)3 FileAlreadyExistsException (alluxio.exception.FileAlreadyExistsException)3 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)3 FileIncompleteException (alluxio.exception.FileIncompleteException)3