Search in sources :

Example 6 with WccData

use of org.apache.hadoop.nfs.nfs3.response.WccData in project hadoop by apache.

the class RpcProgramNfs3 method write.

@VisibleForTesting
WRITE3Response write(XDR xdr, Channel channel, int xid, SecurityHandler securityHandler, SocketAddress remoteAddress) {
    WRITE3Response response = new WRITE3Response(Nfs3Status.NFS3_OK);
    DFSClient dfsClient = clientCache.getDfsClient(securityHandler.getUser());
    if (dfsClient == null) {
        response.setStatus(Nfs3Status.NFS3ERR_SERVERFAULT);
        return response;
    }
    WRITE3Request request;
    try {
        request = WRITE3Request.deserialize(xdr);
    } catch (IOException e) {
        LOG.error("Invalid WRITE request");
        return new WRITE3Response(Nfs3Status.NFS3ERR_INVAL);
    }
    long offset = request.getOffset();
    int count = request.getCount();
    WriteStableHow stableHow = request.getStableHow();
    byte[] data = request.getData().array();
    if (data.length < count) {
        LOG.error("Invalid argument, data size is less than count in request");
        return new WRITE3Response(Nfs3Status.NFS3ERR_INVAL);
    }
    FileHandle handle = request.getHandle();
    if (LOG.isDebugEnabled()) {
        LOG.debug("NFS WRITE fileId: " + handle.getFileId() + " offset: " + offset + " length: " + count + " stableHow: " + stableHow.getValue() + " xid: " + xid + " client: " + remoteAddress);
    }
    Nfs3FileAttributes preOpAttr = null;
    try {
        preOpAttr = writeManager.getFileAttr(dfsClient, handle, iug);
        if (preOpAttr == null) {
            LOG.error("Can't get path for fileId: " + handle.getFileId());
            return new WRITE3Response(Nfs3Status.NFS3ERR_STALE);
        }
        if (!checkAccessPrivilege(remoteAddress, AccessPrivilege.READ_WRITE)) {
            return new WRITE3Response(Nfs3Status.NFS3ERR_ACCES, new WccData(Nfs3Utils.getWccAttr(preOpAttr), preOpAttr), 0, stableHow, Nfs3Constant.WRITE_COMMIT_VERF);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("requested offset=" + offset + " and current filesize=" + preOpAttr.getSize());
        }
        writeManager.handleWrite(dfsClient, request, channel, xid, preOpAttr);
    } catch (IOException e) {
        LOG.info("Error writing to fileId " + handle.getFileId() + " at offset " + offset + " and length " + data.length, e);
        // Try to return WccData
        Nfs3FileAttributes postOpAttr = null;
        try {
            postOpAttr = writeManager.getFileAttr(dfsClient, handle, iug);
        } catch (IOException e1) {
            LOG.info("Can't get postOpAttr for fileId: " + handle.getFileId(), e1);
        }
        WccAttr attr = preOpAttr == null ? null : Nfs3Utils.getWccAttr(preOpAttr);
        WccData fileWcc = new WccData(attr, postOpAttr);
        int status = mapErrorStatus(e);
        return new WRITE3Response(status, fileWcc, 0, request.getStableHow(), Nfs3Constant.WRITE_COMMIT_VERF);
    }
    return null;
}
Also used : DFSClient(org.apache.hadoop.hdfs.DFSClient) WccData(org.apache.hadoop.nfs.nfs3.response.WccData) WriteStableHow(org.apache.hadoop.nfs.nfs3.Nfs3Constant.WriteStableHow) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) Nfs3FileAttributes(org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes) WRITE3Request(org.apache.hadoop.nfs.nfs3.request.WRITE3Request) WccAttr(org.apache.hadoop.nfs.nfs3.response.WccAttr) IOException(java.io.IOException) WRITE3Response(org.apache.hadoop.nfs.nfs3.response.WRITE3Response) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with WccData

use of org.apache.hadoop.nfs.nfs3.response.WccData in project hadoop by apache.

the class RpcProgramNfs3 method symlink.

@VisibleForTesting
SYMLINK3Response symlink(XDR xdr, SecurityHandler securityHandler, SocketAddress remoteAddress) {
    SYMLINK3Response response = new SYMLINK3Response(Nfs3Status.NFS3_OK);
    if (!checkAccessPrivilege(remoteAddress, AccessPrivilege.READ_WRITE)) {
        response.setStatus(Nfs3Status.NFS3ERR_ACCES);
        return response;
    }
    DFSClient dfsClient = clientCache.getDfsClient(securityHandler.getUser());
    if (dfsClient == null) {
        response.setStatus(Nfs3Status.NFS3ERR_SERVERFAULT);
        return response;
    }
    SYMLINK3Request request;
    try {
        request = SYMLINK3Request.deserialize(xdr);
    } catch (IOException e) {
        LOG.error("Invalid SYMLINK request");
        response.setStatus(Nfs3Status.NFS3ERR_INVAL);
        return response;
    }
    FileHandle dirHandle = request.getHandle();
    String name = request.getName();
    String symData = request.getSymData();
    String linkDirIdPath = Nfs3Utils.getFileIdPath(dirHandle);
    // Don't do any name check to source path, just leave it to HDFS
    String linkIdPath = linkDirIdPath + "/" + name;
    if (LOG.isDebugEnabled()) {
        LOG.debug("NFS SYMLINK, target: " + symData + " link: " + linkIdPath + " client: " + remoteAddress);
    }
    try {
        WccData dirWcc = response.getDirWcc();
        WccAttr preOpAttr = Nfs3Utils.getWccAttr(dfsClient, linkDirIdPath);
        dirWcc.setPreOpAttr(preOpAttr);
        dfsClient.createSymlink(symData, linkIdPath, false);
        // Set symlink attr is considered as to change the attr of the target
        // file. So no need to set symlink attr here after it's created.
        HdfsFileStatus linkstat = dfsClient.getFileLinkInfo(linkIdPath);
        Nfs3FileAttributes objAttr = Nfs3Utils.getNfs3FileAttrFromFileStatus(linkstat, iug);
        dirWcc.setPostOpAttr(Nfs3Utils.getFileAttr(dfsClient, linkDirIdPath, iug));
        return new SYMLINK3Response(Nfs3Status.NFS3_OK, new FileHandle(objAttr.getFileId()), objAttr, dirWcc);
    } catch (IOException e) {
        LOG.warn("Exception: " + e);
        int status = mapErrorStatus(e);
        response.setStatus(status);
        return response;
    }
}
Also used : DFSClient(org.apache.hadoop.hdfs.DFSClient) WccData(org.apache.hadoop.nfs.nfs3.response.WccData) SYMLINK3Request(org.apache.hadoop.nfs.nfs3.request.SYMLINK3Request) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) HdfsFileStatus(org.apache.hadoop.hdfs.protocol.HdfsFileStatus) Nfs3FileAttributes(org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes) WccAttr(org.apache.hadoop.nfs.nfs3.response.WccAttr) IOException(java.io.IOException) SYMLINK3Response(org.apache.hadoop.nfs.nfs3.response.SYMLINK3Response) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with WccData

use of org.apache.hadoop.nfs.nfs3.response.WccData in project hadoop by apache.

the class WriteManager method handleWrite.

void handleWrite(DFSClient dfsClient, WRITE3Request request, Channel channel, int xid, Nfs3FileAttributes preOpAttr) throws IOException {
    int count = request.getCount();
    byte[] data = request.getData().array();
    if (data.length < count) {
        WRITE3Response response = new WRITE3Response(Nfs3Status.NFS3ERR_INVAL);
        Nfs3Utils.writeChannel(channel, response.serialize(new XDR(), xid, new VerifierNone()), xid);
        return;
    }
    FileHandle handle = request.getHandle();
    if (LOG.isDebugEnabled()) {
        LOG.debug("handleWrite " + request);
    }
    // Check if there is a stream to write
    FileHandle fileHandle = request.getHandle();
    OpenFileCtx openFileCtx = fileContextCache.get(fileHandle);
    if (openFileCtx == null) {
        LOG.info("No opened stream for fileId: " + fileHandle.getFileId());
        String fileIdPath = Nfs3Utils.getFileIdPath(fileHandle.getFileId());
        HdfsDataOutputStream fos = null;
        Nfs3FileAttributes latestAttr = null;
        try {
            int bufferSize = config.getInt(CommonConfigurationKeysPublic.IO_FILE_BUFFER_SIZE_KEY, CommonConfigurationKeysPublic.IO_FILE_BUFFER_SIZE_DEFAULT);
            fos = dfsClient.append(fileIdPath, bufferSize, EnumSet.of(CreateFlag.APPEND), null, null);
            latestAttr = Nfs3Utils.getFileAttr(dfsClient, fileIdPath, iug);
        } catch (RemoteException e) {
            IOException io = e.unwrapRemoteException();
            if (io instanceof AlreadyBeingCreatedException) {
                LOG.warn("Can't append file: " + fileIdPath + ". Possibly the file is being closed. Drop the request: " + request + ", wait for the client to retry...");
                return;
            }
            throw e;
        } catch (IOException e) {
            LOG.error("Can't append to file: " + fileIdPath, e);
            if (fos != null) {
                fos.close();
            }
            WccData fileWcc = new WccData(Nfs3Utils.getWccAttr(preOpAttr), preOpAttr);
            WRITE3Response response = new WRITE3Response(Nfs3Status.NFS3ERR_IO, fileWcc, count, request.getStableHow(), Nfs3Constant.WRITE_COMMIT_VERF);
            Nfs3Utils.writeChannel(channel, response.serialize(new XDR(), xid, new VerifierNone()), xid);
            return;
        }
        // Add open stream
        String writeDumpDir = config.get(NfsConfigKeys.DFS_NFS_FILE_DUMP_DIR_KEY, NfsConfigKeys.DFS_NFS_FILE_DUMP_DIR_DEFAULT);
        openFileCtx = new OpenFileCtx(fos, latestAttr, writeDumpDir + "/" + fileHandle.getFileId(), dfsClient, iug, aixCompatMode, config);
        if (!addOpenFileStream(fileHandle, openFileCtx)) {
            LOG.info("Can't add new stream. Close it. Tell client to retry.");
            try {
                fos.close();
            } catch (IOException e) {
                LOG.error("Can't close stream for fileId: " + handle.getFileId(), e);
            }
            // Notify client to retry
            WccData fileWcc = new WccData(latestAttr.getWccAttr(), latestAttr);
            WRITE3Response response = new WRITE3Response(Nfs3Status.NFS3ERR_JUKEBOX, fileWcc, 0, request.getStableHow(), Nfs3Constant.WRITE_COMMIT_VERF);
            Nfs3Utils.writeChannel(channel, response.serialize(new XDR(), xid, new VerifierNone()), xid);
            return;
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Opened stream for appending file: " + fileHandle.getFileId());
        }
    }
    // Add write into the async job queue
    openFileCtx.receivedNewWrite(dfsClient, request, channel, xid, asyncDataService, iug);
    return;
}
Also used : WccData(org.apache.hadoop.nfs.nfs3.response.WccData) AlreadyBeingCreatedException(org.apache.hadoop.hdfs.protocol.AlreadyBeingCreatedException) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) XDR(org.apache.hadoop.oncrpc.XDR) Nfs3FileAttributes(org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes) IOException(java.io.IOException) VerifierNone(org.apache.hadoop.oncrpc.security.VerifierNone) HdfsDataOutputStream(org.apache.hadoop.hdfs.client.HdfsDataOutputStream) WRITE3Response(org.apache.hadoop.nfs.nfs3.response.WRITE3Response) RemoteException(org.apache.hadoop.ipc.RemoteException)

Example 9 with WccData

use of org.apache.hadoop.nfs.nfs3.response.WccData in project hadoop by apache.

the class WriteManager method handleCommit.

void handleCommit(DFSClient dfsClient, FileHandle fileHandle, long commitOffset, Channel channel, int xid, Nfs3FileAttributes preOpAttr) {
    long startTime = System.nanoTime();
    int status;
    OpenFileCtx openFileCtx = fileContextCache.get(fileHandle);
    if (openFileCtx == null) {
        LOG.info("No opened stream for fileId: " + fileHandle.getFileId() + " commitOffset=" + commitOffset + ". Return success in this case.");
        status = Nfs3Status.NFS3_OK;
    } else {
        COMMIT_STATUS ret = openFileCtx.checkCommit(dfsClient, commitOffset, channel, xid, preOpAttr, false);
        switch(ret) {
            case COMMIT_FINISHED:
            case COMMIT_INACTIVE_CTX:
                status = Nfs3Status.NFS3_OK;
                break;
            case COMMIT_INACTIVE_WITH_PENDING_WRITE:
            case COMMIT_ERROR:
                status = Nfs3Status.NFS3ERR_IO;
                break;
            case COMMIT_WAIT:
                // Do nothing. Commit is async now.
                return;
            case COMMIT_SPECIAL_WAIT:
                status = Nfs3Status.NFS3ERR_JUKEBOX;
                break;
            case COMMIT_SPECIAL_SUCCESS:
                status = Nfs3Status.NFS3_OK;
                break;
            default:
                LOG.error("Should not get commit return code: " + ret.name());
                throw new RuntimeException("Should not get commit return code: " + ret.name());
        }
    }
    // Send out the response
    Nfs3FileAttributes postOpAttr = null;
    try {
        postOpAttr = getFileAttr(dfsClient, new FileHandle(preOpAttr.getFileId()), iug);
    } catch (IOException e1) {
        LOG.info("Can't get postOpAttr for fileId: " + preOpAttr.getFileId(), e1);
    }
    WccData fileWcc = new WccData(Nfs3Utils.getWccAttr(preOpAttr), postOpAttr);
    COMMIT3Response response = new COMMIT3Response(status, fileWcc, Nfs3Constant.WRITE_COMMIT_VERF);
    RpcProgramNfs3.metrics.addCommit(Nfs3Utils.getElapsedTime(startTime));
    Nfs3Utils.writeChannelCommit(channel, response.serialize(new XDR(), xid, new VerifierNone()), xid);
}
Also used : WccData(org.apache.hadoop.nfs.nfs3.response.WccData) COMMIT3Response(org.apache.hadoop.nfs.nfs3.response.COMMIT3Response) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) Nfs3FileAttributes(org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes) XDR(org.apache.hadoop.oncrpc.XDR) VerifierNone(org.apache.hadoop.oncrpc.security.VerifierNone) IOException(java.io.IOException) COMMIT_STATUS(org.apache.hadoop.hdfs.nfs.nfs3.OpenFileCtx.COMMIT_STATUS)

Example 10 with WccData

use of org.apache.hadoop.nfs.nfs3.response.WccData in project hadoop by apache.

the class OpenFileCtx method cleanup.

synchronized void cleanup() {
    if (!activeState) {
        LOG.info("Current OpenFileCtx is already inactive, no need to cleanup.");
        return;
    }
    activeState = false;
    // stop the dump thread
    if (dumpThread != null && dumpThread.isAlive()) {
        dumpThread.interrupt();
        try {
            dumpThread.join(3000);
        } catch (InterruptedException ignored) {
        }
    }
    // Close stream
    try {
        if (fos != null) {
            fos.close();
        }
    } catch (IOException e) {
        LOG.info("Can't close stream for fileId: " + latestAttr.getFileId() + ", error: " + e);
    }
    // Reply error for pending writes
    LOG.info("There are " + pendingWrites.size() + " pending writes.");
    WccAttr preOpAttr = latestAttr.getWccAttr();
    while (!pendingWrites.isEmpty()) {
        OffsetRange key = pendingWrites.firstKey();
        LOG.info("Fail pending write: " + key.toString() + ", nextOffset=" + nextOffset.get());
        WriteCtx writeCtx = pendingWrites.remove(key);
        if (!writeCtx.getReplied()) {
            WccData fileWcc = new WccData(preOpAttr, latestAttr);
            WRITE3Response response = new WRITE3Response(Nfs3Status.NFS3ERR_IO, fileWcc, 0, writeCtx.getStableHow(), Nfs3Constant.WRITE_COMMIT_VERF);
            Nfs3Utils.writeChannel(writeCtx.getChannel(), response.serialize(new XDR(), writeCtx.getXid(), new VerifierNone()), writeCtx.getXid());
        }
    }
    // Cleanup dump file
    if (dumpOut != null) {
        try {
            dumpOut.close();
        } catch (IOException e) {
            LOG.error("Failed to close outputstream of dump file" + dumpFilePath, e);
        }
        File dumpFile = new File(dumpFilePath);
        if (dumpFile.exists() && !dumpFile.delete()) {
            LOG.error("Failed to delete dumpfile: " + dumpFile);
        }
    }
    if (raf != null) {
        try {
            raf.close();
        } catch (IOException e) {
            LOG.error("Got exception when closing input stream of dump file.", e);
        }
    }
}
Also used : WccData(org.apache.hadoop.nfs.nfs3.response.WccData) XDR(org.apache.hadoop.oncrpc.XDR) VerifierNone(org.apache.hadoop.oncrpc.security.VerifierNone) WccAttr(org.apache.hadoop.nfs.nfs3.response.WccAttr) IOException(java.io.IOException) WRITE3Response(org.apache.hadoop.nfs.nfs3.response.WRITE3Response) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

Nfs3FileAttributes (org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes)17 WccData (org.apache.hadoop.nfs.nfs3.response.WccData)17 FileHandle (org.apache.hadoop.nfs.nfs3.FileHandle)16 IOException (java.io.IOException)15 VisibleForTesting (com.google.common.annotations.VisibleForTesting)9 DFSClient (org.apache.hadoop.hdfs.DFSClient)9 WRITE3Response (org.apache.hadoop.nfs.nfs3.response.WRITE3Response)8 XDR (org.apache.hadoop.oncrpc.XDR)8 VerifierNone (org.apache.hadoop.oncrpc.security.VerifierNone)8 WccAttr (org.apache.hadoop.nfs.nfs3.response.WccAttr)6 WriteStableHow (org.apache.hadoop.nfs.nfs3.Nfs3Constant.WriteStableHow)5 HdfsFileStatus (org.apache.hadoop.hdfs.protocol.HdfsFileStatus)3 COMMIT3Response (org.apache.hadoop.nfs.nfs3.response.COMMIT3Response)3 ClosedChannelException (java.nio.channels.ClosedChannelException)2 FsPermission (org.apache.hadoop.fs.permission.FsPermission)2 HdfsDataOutputStream (org.apache.hadoop.hdfs.client.HdfsDataOutputStream)2 SetAttr3 (org.apache.hadoop.nfs.nfs3.request.SetAttr3)2 File (java.io.File)1 RandomAccessFile (java.io.RandomAccessFile)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1