Search in sources :

Example 31 with FileOutStream

use of alluxio.client.file.FileOutStream in project alluxio by Alluxio.

the class StreamCacheTest method expiration.

@Test
public void expiration() throws Exception {
    StreamCache streamCache = new StreamCache(0);
    FileInStream is = Mockito.mock(FileInStream.class);
    FileOutStream os = Mockito.mock(FileOutStream.class);
    streamCache.put(is);
    streamCache.put(os);
    Mockito.verify(is).close();
    Mockito.verify(os).close();
}
Also used : FileInStream(alluxio.client.file.FileInStream) FileOutStream(alluxio.client.file.FileOutStream) Test(org.junit.Test)

Example 32 with FileOutStream

use of alluxio.client.file.FileOutStream in project alluxio by Alluxio.

the class BlockServiceHandlerIntegrationTest method lockBlock.

// Tests that lock block returns the correct path
@Test
public void lockBlock() throws Exception {
    final int blockSize = (int) WORKER_CAPACITY_BYTES / 2;
    CreateFileOptions options = CreateFileOptions.defaults().setBlockSizeBytes(blockSize).setWriteType(WriteType.MUST_CACHE);
    FileOutStream out = mFileSystem.createFile(new AlluxioURI("/testFile"), options);
    URIStatus file = mFileSystem.getStatus(new AlluxioURI("/testFile"));
    final long blockId = BlockId.createBlockId(BlockId.getContainerId(file.getFileId()), 0);
    out.write(BufferUtils.getIncreasingByteArray(blockSize));
    out.close();
    String localPath = mBlockWorkerServiceHandler.lockBlock(blockId, SESSION_ID, new LockBlockTOptions()).getBlockPath();
    // The local path should exist
    Assert.assertNotNull(localPath);
    UnderFileSystem ufs = UnderFileSystem.Factory.get(localPath);
    byte[] data = new byte[blockSize];
    InputStream in = ufs.open(localPath);
    int bytesRead = in.read(data);
    // The data in the local file should equal the data we wrote earlier
    Assert.assertEquals(blockSize, bytesRead);
    Assert.assertTrue(BufferUtils.equalIncreasingByteArray(bytesRead, data));
    mBlockWorkerServiceHandler.unlockBlock(blockId, SESSION_ID);
}
Also used : CreateFileOptions(alluxio.client.file.options.CreateFileOptions) InputStream(java.io.InputStream) FileOutStream(alluxio.client.file.FileOutStream) URIStatus(alluxio.client.file.URIStatus) LockBlockTOptions(alluxio.thrift.LockBlockTOptions) UnderFileSystem(alluxio.underfs.UnderFileSystem) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 33 with FileOutStream

use of alluxio.client.file.FileOutStream in project alluxio by Alluxio.

the class CapacityUsageIntegrationTest method createAndWriteFile.

private void createAndWriteFile(AlluxioURI filePath, WriteType writeType, int len) throws IOException, AlluxioException {
    ByteBuffer buf = ByteBuffer.allocate(len);
    buf.order(ByteOrder.nativeOrder());
    for (int k = 0; k < len; k++) {
        buf.put((byte) k);
    }
    CreateFileOptions options = CreateFileOptions.defaults().setWriteType(writeType);
    FileOutStream os = mFileSystem.createFile(filePath, options);
    os.write(buf.array());
    os.close();
}
Also used : CreateFileOptions(alluxio.client.file.options.CreateFileOptions) FileOutStream(alluxio.client.file.FileOutStream) ByteBuffer(java.nio.ByteBuffer)

Example 34 with FileOutStream

use of alluxio.client.file.FileOutStream in project alluxio by Alluxio.

the class AbstractFileSystem method create.

/**
   * Attempts to create a file. Overwrite will not succeed if the path exists and is a folder.
   *
   * @param path path to create
   * @param permission permissions of the created file/folder
   * @param overwrite overwrite if file exists
   * @param bufferSize the size in bytes of the buffer to be used
   * @param replication under filesystem replication factor
   * @param blockSize block size in bytes
   * @param progress queryable progress
   * @return an {@link FSDataOutputStream} created at the indicated path of a file
   * @throws IOException if overwrite is not specified and the path already exists or if the path is
   *         a folder
   */
@Override
public FSDataOutputStream create(Path path, FsPermission permission, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {
    LOG.debug("create({}, {}, {}, {}, {}, {}, {})", path, permission, overwrite, bufferSize, replication, blockSize, progress);
    if (mStatistics != null) {
        mStatistics.incrementWriteOps(1);
    }
    AlluxioURI uri = new AlluxioURI(HadoopUtils.getPathWithoutScheme(path));
    CreateFileOptions options = CreateFileOptions.defaults().setBlockSizeBytes(blockSize).setMode(new Mode(permission.toShort()));
    FileOutStream outStream;
    try {
        outStream = mFileSystem.createFile(uri, options);
    } catch (AlluxioException e) {
        //now we should consider the override parameter
        try {
            if (mFileSystem.exists(uri)) {
                if (!overwrite) {
                    throw new IOException(ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(uri));
                }
                if (mFileSystem.getStatus(uri).isFolder()) {
                    throw new IOException(ExceptionMessage.FILE_CREATE_IS_DIRECTORY.getMessage(uri));
                }
                mFileSystem.delete(uri);
            }
            outStream = mFileSystem.createFile(uri, options);
        } catch (AlluxioException e2) {
            throw new IOException(e2);
        }
    }
    return new FSDataOutputStream(outStream, mStatistics);
}
Also used : CreateFileOptions(alluxio.client.file.options.CreateFileOptions) Mode(alluxio.security.authorization.Mode) FileOutStream(alluxio.client.file.FileOutStream) IOException(java.io.IOException) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) AlluxioURI(alluxio.AlluxioURI) AlluxioException(alluxio.exception.AlluxioException)

Example 35 with FileOutStream

use of alluxio.client.file.FileOutStream in project alluxio by Alluxio.

the class AlluxioFuseFileSystemTest method write.

@Test
public void write() throws Exception {
    FileOutStream fos = mock(FileOutStream.class);
    AlluxioURI anyURI = any();
    when(mFileSystem.createFile(anyURI)).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);
}
Also used : Runtime(jnr.ffi.Runtime) FileOutStream(alluxio.client.file.FileOutStream) Pointer(jnr.ffi.Pointer) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Aggregations

FileOutStream (alluxio.client.file.FileOutStream)39 AlluxioURI (alluxio.AlluxioURI)28 Test (org.junit.Test)25 CreateFileOptions (alluxio.client.file.options.CreateFileOptions)11 FileInStream (alluxio.client.file.FileInStream)9 URIStatus (alluxio.client.file.URIStatus)9 AlluxioException (alluxio.exception.AlluxioException)5 IOException (java.io.IOException)5 FileSystem (alluxio.client.file.FileSystem)4 ByteBuffer (java.nio.ByteBuffer)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 LocalFirstPolicy (alluxio.client.file.policy.LocalFirstPolicy)2 LineageFileSystem (alluxio.client.lineage.LineageFileSystem)2 LineageMasterClient (alluxio.client.lineage.LineageMasterClient)2 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)2 Closer (com.google.common.io.Closer)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ExpectedException (org.junit.rules.ExpectedException)2 OpenFileOptions (alluxio.client.file.options.OpenFileOptions)1