Search in sources :

Example 1 with DFSInputStream

use of org.apache.hadoop.hdfs.DFSInputStream in project hadoop by apache.

the class DFSClientCache method inputStreamLoader.

private CacheLoader<DFSInputStreamCaheKey, FSDataInputStream> inputStreamLoader() {
    return new CacheLoader<DFSInputStreamCaheKey, FSDataInputStream>() {

        @Override
        public FSDataInputStream load(DFSInputStreamCaheKey key) throws Exception {
            DFSClient client = getDfsClient(key.userId);
            DFSInputStream dis = client.open(key.inodePath);
            return client.createWrappedInputStream(dis);
        }
    };
}
Also used : DFSClient(org.apache.hadoop.hdfs.DFSClient) CacheLoader(com.google.common.cache.CacheLoader) DFSInputStream(org.apache.hadoop.hdfs.DFSInputStream)

Example 2 with DFSInputStream

use of org.apache.hadoop.hdfs.DFSInputStream in project SSM by Intel-bigdata.

the class ErasureCodingBase method convert.

protected void convert(HdfsFileStatus srcFileStatus) throws ActionException {
    DFSInputStream in = null;
    DFSOutputStream out = null;
    try {
        long blockSize = conf.getLong(DFSConfigKeys.DFS_BLOCK_SIZE_KEY, DFSConfigKeys.DFS_BLOCK_SIZE_DEFAULT);
        in = dfsClient.open(srcPath, bufferSize, true);
        short replication = (short) conf.getInt(DFSConfigKeys.DFS_REPLICATION_KEY, DFSConfigKeys.DFS_REPLICATION_DEFAULT);
        // use the same FsPermission as srcPath
        FsPermission permission = srcFileStatus.getPermission();
        out = dfsClient.create(ecTmpPath, permission, EnumSet.of(CreateFlag.CREATE), true, replication, blockSize, null, bufferSize, null, null, ecPolicyName);
        // Keep storage policy according with original file except UNDEF storage policy
        String storagePolicyName = dfsClient.getStoragePolicy(srcPath).getName();
        if (!storagePolicyName.equals("UNDEF")) {
            dfsClient.setStoragePolicy(ecTmpPath, storagePolicyName);
        }
        long bytesRemaining = srcFileStatus.getLen();
        byte[] buf = new byte[bufferSize];
        while (bytesRemaining > 0L) {
            int bytesToRead = (int) (bytesRemaining < (long) buf.length ? bytesRemaining : (long) buf.length);
            int bytesRead = in.read(buf, 0, bytesToRead);
            if (bytesRead == -1) {
                break;
            }
            out.write(buf, 0, bytesRead);
            bytesRemaining -= (long) bytesRead;
            this.progress = (float) (srcFileStatus.getLen() - bytesRemaining) / srcFileStatus.getLen();
        }
    } catch (Exception ex) {
        throw new ActionException(ex);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        } catch (IOException ex) {
            LOG.error("IOException occurred when closing DFSInputStream or DFSOutputStream!");
        }
    }
}
Also used : ActionException(org.smartdata.action.ActionException) DFSInputStream(org.apache.hadoop.hdfs.DFSInputStream) FsPermission(org.apache.hadoop.fs.permission.FsPermission) IOException(java.io.IOException) DFSOutputStream(org.apache.hadoop.hdfs.DFSOutputStream) IOException(java.io.IOException) ActionException(org.smartdata.action.ActionException)

Example 3 with DFSInputStream

use of org.apache.hadoop.hdfs.DFSInputStream in project SSM by Intel-bigdata.

the class TestCompressDecompress method testCompressedFileRandomRead.

// @Test(timeout = 90000)
// public void testCompressEmptyFile() throws Exception {
// waitTillSSMExitSafeMode();
// 
// // initDB();
// String fileName = "/ssm/compression/file2";
// prepareFile(fileName, 0);
// MetaStore metaStore = ssm.getMetaStore();
// 
// int bufSize = 1024 * 1024;
// CmdletManager cmdletManager = ssm.getCmdletManager();
// long cmdId = cmdletManager.submitCmdlet("compress -file " + fileName
// + " -bufSize " + bufSize + " -compressImpl " + compressionImpl);
// 
// waitTillActionDone(cmdId);
// FileState fileState = metaStore.getFileState(fileName);
// while (!fileState.getFileType().equals(FileState.FileType.COMPRESSION)) {
// Thread.sleep(200);
// fileState = metaStore.getFileState(fileName);
// }
// 
// // metastore  test
// //    Assert.assertEquals(FileState.FileType.COMPRESSION, fileState.getFileType());
// Assert.assertEquals(FileState.FileStage.DONE, fileState.getFileStage());
// Assert.assertTrue(fileState instanceof CompressionFileState);
// CompressionFileState compressionFileState = (CompressionFileState) fileState;
// Assert.assertEquals(fileName, compressionFileState.getPath());
// Assert.assertEquals(bufSize, compressionFileState.getBufferSize());
// Assert.assertEquals(compressionImpl, compressionFileState.getCompressionImpl());
// Assert.assertEquals(0, compressionFileState.getOriginalLength());
// Assert.assertEquals(0, compressionFileState.getCompressedLength());
// 
// // File length test
// Assert.assertEquals(0, dfsClient.getFileInfo(fileName).getLen());
// }
@Test
public void testCompressedFileRandomRead() throws Exception {
    // if (!loadedNative()) {
    // return;
    // }
    waitTillSSMExitSafeMode();
    // initDB();
    int arraySize = 1024 * 1024 * 8;
    String fileName = "/ssm/compression/file3";
    byte[] bytes = prepareFile(fileName, arraySize);
    int bufSize = 1024 * 1024;
    CmdletManager cmdletManager = ssm.getCmdletManager();
    long cmdId = cmdletManager.submitCmdlet("compress -file " + fileName + " -bufSize " + bufSize + " -codec " + codec);
    waitTillActionDone(cmdId);
    // Test random read
    Random rnd = new Random(System.currentTimeMillis());
    DFSInputStream dfsInputStream = smartDFSClient.open(fileName);
    int randomReadSize = 500;
    byte[] randomReadBuffer = new byte[randomReadSize];
    for (int i = 0; i < 5; i++) {
        int pos = rnd.nextInt(arraySize - 500);
        byte[] subBytes = Arrays.copyOfRange(bytes, pos, pos + 500);
        dfsInputStream.seek(pos);
        Assert.assertEquals(pos, dfsInputStream.getPos());
        int off = 0;
        while (off < randomReadSize) {
            int len = dfsInputStream.read(randomReadBuffer, off, randomReadSize - off);
            off += len;
        }
        Assert.assertArrayEquals(subBytes, randomReadBuffer);
        Assert.assertEquals(pos + 500, dfsInputStream.getPos());
    }
}
Also used : Random(java.util.Random) CmdletManager(org.smartdata.server.engine.CmdletManager) DFSInputStream(org.apache.hadoop.hdfs.DFSInputStream) Test(org.junit.Test)

Example 4 with DFSInputStream

use of org.apache.hadoop.hdfs.DFSInputStream in project SSM by Intel-bigdata.

the class SmartDFSClient method open.

@Override
public DFSInputStream open(String src, int buffersize, boolean verifyChecksum) throws IOException {
    DFSInputStream is = super.open(src, buffersize, verifyChecksum);
    if (is.getFileLength() == 0) {
        is.close();
        FileState fileState = getFileState(src);
        if (fileState.getFileStage().equals(FileState.FileStage.PROCESSING)) {
            throw new IOException("Cannot open " + src + " when it is under PROCESSING to " + fileState.getFileType());
        }
        is = SmartInputStreamFactory.create(this, src, verifyChecksum, fileState);
    } else {
        is.close();
        FileState fileState = getFileState(src);
        if (fileState.getFileStage().equals(FileState.FileStage.PROCESSING)) {
            throw new IOException("Cannot open " + src + " when it is under PROCESSING to " + fileState.getFileType());
        }
        is = SmartInputStreamFactory.create(this, src, verifyChecksum, fileState);
    }
    reportFileAccessEvent(src);
    return is;
}
Also used : NormalFileState(org.smartdata.model.NormalFileState) CompactFileState(org.smartdata.model.CompactFileState) FileState(org.smartdata.model.FileState) CompressionFileState(org.smartdata.model.CompressionFileState) DFSInputStream(org.apache.hadoop.hdfs.DFSInputStream) IOException(java.io.IOException)

Example 5 with DFSInputStream

use of org.apache.hadoop.hdfs.DFSInputStream in project SSM by Intel-bigdata.

the class SmallFileUncompactAction method execute.

@Override
protected void execute() throws Exception {
    // Set hdfs client by DFSClient rather than SmartDFSClient
    this.setDfsClient(HadoopUtil.getDFSClient(HadoopUtil.getNameNodeUri(conf), conf));
    // Get small file list
    if (smallFiles == null || smallFiles.isEmpty()) {
        throw new IllegalArgumentException(String.format("Invalid small files: %s.", smallFiles));
    }
    ArrayList<String> smallFileList = new Gson().fromJson(smallFiles, new TypeToken<ArrayList<String>>() {
    }.getType());
    if (smallFileList == null || smallFileList.isEmpty()) {
        throw new IllegalArgumentException(String.format("Invalid small files: %s.", smallFiles));
    }
    // Get container file path
    if (containerFile == null || containerFile.isEmpty()) {
        throw new IllegalArgumentException(String.format("Invalid container file: %s.", containerFile));
    }
    appendLog(String.format("Action starts at %s : uncompact small files.", Utils.getFormatedCurrentTime()));
    for (String smallFile : smallFileList) {
        if ((smallFile != null) && !smallFile.isEmpty() && dfsClient.exists(smallFile)) {
            DFSInputStream in = null;
            OutputStream out = null;
            try {
                // Get compact input stream
                in = smartDFSClient.open(smallFile);
                // Save original metadata of small file and delete original small file
                HdfsFileStatus fileStatus = dfsClient.getFileInfo(smallFile);
                Map<String, byte[]> xAttr = dfsClient.getXAttrs(smallFile);
                dfsClient.delete(smallFile, false);
                // Create new small file
                out = dfsClient.create(smallFile, true);
                // Copy contents to original small file
                IOUtils.copyBytes(in, out, 4096);
                // Reset file meta data
                resetFileMeta(smallFile, fileStatus, xAttr);
                // Set status and update log
                this.status = (smallFileList.indexOf(smallFile) + 1.0f) / smallFileList.size();
                appendLog(String.format("Uncompact %s successfully.", smallFile));
            } finally {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            }
        }
    }
    dfsClient.delete(containerFile, false);
    appendLog(String.format("Uncompact all the small files of %s successfully.", containerFile));
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) OutputStream(java.io.OutputStream) HdfsFileStatus(org.apache.hadoop.hdfs.protocol.HdfsFileStatus) Gson(com.google.gson.Gson) DFSInputStream(org.apache.hadoop.hdfs.DFSInputStream)

Aggregations

DFSInputStream (org.apache.hadoop.hdfs.DFSInputStream)16 IOException (java.io.IOException)7 Test (org.junit.Test)6 Path (org.apache.hadoop.fs.Path)4 CompressionFileState (org.smartdata.model.CompressionFileState)4 HdfsFileStatus (org.apache.hadoop.hdfs.protocol.HdfsFileStatus)3 ActionException (org.smartdata.action.ActionException)3 SmartDFSClient (org.smartdata.hdfs.client.SmartDFSClient)3 FileState (org.smartdata.model.FileState)3 Gson (com.google.gson.Gson)2 OutputStream (java.io.OutputStream)2 DFSStripedInputStream (org.apache.hadoop.hdfs.DFSStripedInputStream)2 SmartConf (org.smartdata.conf.SmartConf)2 CompactFileState (org.smartdata.model.CompactFileState)2 NormalFileState (org.smartdata.model.NormalFileState)2 CmdletManager (org.smartdata.server.engine.CmdletManager)2 CacheLoader (com.google.common.cache.CacheLoader)1 TypeToken (com.google.gson.reflect.TypeToken)1 FilterInputStream (java.io.FilterInputStream)1 Field (java.lang.reflect.Field)1