Search in sources :

Example 1 with WriteStableHow

use of org.apache.hadoop.nfs.nfs3.Nfs3Constant.WriteStableHow 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 2 with WriteStableHow

use of org.apache.hadoop.nfs.nfs3.Nfs3Constant.WriteStableHow in project hadoop by apache.

the class OpenFileCtx method processOverWrite.

/** Process an overwrite write request */
private void processOverWrite(DFSClient dfsClient, WRITE3Request request, Channel channel, int xid, IdMappingServiceProvider iug) {
    WccData wccData = new WccData(latestAttr.getWccAttr(), null);
    long offset = request.getOffset();
    int count = request.getCount();
    WriteStableHow stableHow = request.getStableHow();
    WRITE3Response response;
    long cachedOffset = nextOffset.get();
    if (offset + count > cachedOffset) {
        LOG.warn("Treat this jumbo write as a real random write, no support.");
        response = new WRITE3Response(Nfs3Status.NFS3ERR_INVAL, wccData, 0, WriteStableHow.UNSTABLE, Nfs3Constant.WRITE_COMMIT_VERF);
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Process perfectOverWrite");
        }
        // TODO: let executor handle perfect overwrite
        response = processPerfectOverWrite(dfsClient, offset, count, stableHow, request.getData().array(), Nfs3Utils.getFileIdPath(request.getHandle()), wccData, iug);
    }
    updateLastAccessTime();
    Nfs3Utils.writeChannel(channel, response.serialize(new XDR(), xid, new VerifierNone()), xid);
}
Also used : WccData(org.apache.hadoop.nfs.nfs3.response.WccData) WriteStableHow(org.apache.hadoop.nfs.nfs3.Nfs3Constant.WriteStableHow) XDR(org.apache.hadoop.oncrpc.XDR) VerifierNone(org.apache.hadoop.oncrpc.security.VerifierNone) WRITE3Response(org.apache.hadoop.nfs.nfs3.response.WRITE3Response)

Example 3 with WriteStableHow

use of org.apache.hadoop.nfs.nfs3.Nfs3Constant.WriteStableHow in project hadoop by apache.

the class OpenFileCtx method doSingleWrite.

private void doSingleWrite(final WriteCtx writeCtx) {
    Channel channel = writeCtx.getChannel();
    int xid = writeCtx.getXid();
    long offset = writeCtx.getOffset();
    int count = writeCtx.getCount();
    WriteStableHow stableHow = writeCtx.getStableHow();
    FileHandle handle = writeCtx.getHandle();
    if (LOG.isDebugEnabled()) {
        LOG.debug("do write, fileId: " + handle.getFileId() + " offset: " + offset + " length: " + count + " stableHow: " + stableHow.name());
    }
    try {
        // The write is not protected by lock. asyncState is used to make sure
        // there is one thread doing write back at any time    
        writeCtx.writeData(fos);
        RpcProgramNfs3.metrics.incrBytesWritten(writeCtx.getCount());
        long flushedOffset = getFlushedOffset();
        if (flushedOffset != (offset + count)) {
            throw new IOException("output stream is out of sync, pos=" + flushedOffset + " and nextOffset should be" + (offset + count));
        }
        // Reduce memory occupation size if request was allowed dumped
        if (writeCtx.getDataState() == WriteCtx.DataState.ALLOW_DUMP) {
            synchronized (writeCtx) {
                if (writeCtx.getDataState() == WriteCtx.DataState.ALLOW_DUMP) {
                    writeCtx.setDataState(WriteCtx.DataState.NO_DUMP);
                    updateNonSequentialWriteInMemory(-count);
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("After writing " + handle.getFileId() + " at offset " + offset + ", updated the memory count, new value: " + nonSequentialWriteInMemory.get());
                    }
                }
            }
        }
        if (!writeCtx.getReplied()) {
            if (stableHow != WriteStableHow.UNSTABLE) {
                LOG.info("Do sync for stable write: " + writeCtx);
                try {
                    if (stableHow == WriteStableHow.DATA_SYNC) {
                        fos.hsync();
                    } else {
                        Preconditions.checkState(stableHow == WriteStableHow.FILE_SYNC, "Unknown WriteStableHow: " + stableHow);
                        // Sync file data and length
                        fos.hsync(EnumSet.of(SyncFlag.UPDATE_LENGTH));
                    }
                } catch (IOException e) {
                    LOG.error("hsync failed with writeCtx: " + writeCtx, e);
                    throw e;
                }
            }
            WccAttr preOpAttr = latestAttr.getWccAttr();
            WccData fileWcc = new WccData(preOpAttr, latestAttr);
            if (writeCtx.getOriginalCount() != WriteCtx.INVALID_ORIGINAL_COUNT) {
                LOG.warn("Return original count: " + writeCtx.getOriginalCount() + " instead of real data count: " + count);
                count = writeCtx.getOriginalCount();
            }
            WRITE3Response response = new WRITE3Response(Nfs3Status.NFS3_OK, fileWcc, count, stableHow, Nfs3Constant.WRITE_COMMIT_VERF);
            RpcProgramNfs3.metrics.addWrite(Nfs3Utils.getElapsedTime(writeCtx.startTime));
            Nfs3Utils.writeChannel(channel, response.serialize(new XDR(), xid, new VerifierNone()), xid);
        }
        // Handle the waiting commits without holding any lock
        processCommits(writeCtx.getOffset() + writeCtx.getCount());
    } catch (IOException e) {
        LOG.error("Error writing to fileId " + handle.getFileId() + " at offset " + offset + " and length " + count, e);
        if (!writeCtx.getReplied()) {
            WRITE3Response response = new WRITE3Response(Nfs3Status.NFS3ERR_IO);
            Nfs3Utils.writeChannel(channel, response.serialize(new XDR(), xid, new VerifierNone()), xid);
        // Keep stream open. Either client retries or SteamMonitor closes it.
        }
        LOG.info("Clean up open file context for fileId: " + latestAttr.getFileId());
        cleanup();
    }
}
Also used : WccData(org.apache.hadoop.nfs.nfs3.response.WccData) WriteStableHow(org.apache.hadoop.nfs.nfs3.Nfs3Constant.WriteStableHow) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) Channel(org.jboss.netty.channel.Channel) 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)

Example 4 with WriteStableHow

use of org.apache.hadoop.nfs.nfs3.Nfs3Constant.WriteStableHow in project hadoop by apache.

the class WRITE3Response method deserialize.

public static WRITE3Response deserialize(XDR xdr) {
    int status = xdr.readInt();
    WccData fileWcc = WccData.deserialize(xdr);
    int count = 0;
    WriteStableHow stableHow = null;
    long verifier = 0;
    if (status == Nfs3Status.NFS3_OK) {
        count = xdr.readInt();
        int how = xdr.readInt();
        stableHow = WriteStableHow.values()[how];
        verifier = xdr.readHyper();
    }
    return new WRITE3Response(status, fileWcc, count, stableHow, verifier);
}
Also used : WriteStableHow(org.apache.hadoop.nfs.nfs3.Nfs3Constant.WriteStableHow)

Example 5 with WriteStableHow

use of org.apache.hadoop.nfs.nfs3.Nfs3Constant.WriteStableHow in project hadoop by apache.

the class WRITE3Request method deserialize.

public static WRITE3Request deserialize(XDR xdr) throws IOException {
    FileHandle handle = readHandle(xdr);
    long offset = xdr.readHyper();
    int count = xdr.readInt();
    WriteStableHow stableHow = WriteStableHow.fromValue(xdr.readInt());
    ByteBuffer data = ByteBuffer.wrap(xdr.readFixedOpaque(xdr.readInt()));
    return new WRITE3Request(handle, offset, count, stableHow, data);
}
Also used : FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) WriteStableHow(org.apache.hadoop.nfs.nfs3.Nfs3Constant.WriteStableHow) ByteBuffer(java.nio.ByteBuffer)

Aggregations

WriteStableHow (org.apache.hadoop.nfs.nfs3.Nfs3Constant.WriteStableHow)6 WRITE3Response (org.apache.hadoop.nfs.nfs3.response.WRITE3Response)5 WccData (org.apache.hadoop.nfs.nfs3.response.WccData)4 IOException (java.io.IOException)3 FileHandle (org.apache.hadoop.nfs.nfs3.FileHandle)3 WccAttr (org.apache.hadoop.nfs.nfs3.response.WccAttr)3 XDR (org.apache.hadoop.oncrpc.XDR)3 VerifierNone (org.apache.hadoop.oncrpc.security.VerifierNone)3 Nfs3FileAttributes (org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)1 DFSClient (org.apache.hadoop.hdfs.DFSClient)1 Comparator (org.apache.hadoop.io.BytesWritable.Comparator)1 WRITE3Request (org.apache.hadoop.nfs.nfs3.request.WRITE3Request)1 Channel (org.jboss.netty.channel.Channel)1