Search in sources :

Example 6 with WriteStableHow

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

the class OpenFileCtx method receivedNewWriteInternal.

private void receivedNewWriteInternal(DFSClient dfsClient, WRITE3Request request, Channel channel, int xid, AsyncDataService asyncDataService, IdMappingServiceProvider iug) {
    WriteStableHow stableHow = request.getStableHow();
    WccAttr preOpAttr = latestAttr.getWccAttr();
    int count = request.getCount();
    WriteCtx writeCtx = addWritesToCache(request, channel, xid);
    if (writeCtx == null) {
        // offset < nextOffset
        processOverWrite(dfsClient, request, channel, xid, iug);
    } else {
        // The write is added to pendingWrites.
        // Check and start writing back if necessary
        boolean startWriting = checkAndStartWrite(asyncDataService, writeCtx);
        if (!startWriting) {
            // offset > nextOffset. check if we need to dump data
            waitForDump();
            // for unstable non-sequential write
            if (stableHow != WriteStableHow.UNSTABLE) {
                LOG.info("Have to change stable write to unstable write: " + request.getStableHow());
                stableHow = WriteStableHow.UNSTABLE;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("UNSTABLE write request, send response for offset: " + writeCtx.getOffset());
            }
            WccData fileWcc = new WccData(preOpAttr, latestAttr);
            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);
            writeCtx.setReplied(true);
        }
    }
}
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) WccAttr(org.apache.hadoop.nfs.nfs3.response.WccAttr) WRITE3Response(org.apache.hadoop.nfs.nfs3.response.WRITE3Response)

Example 7 with WriteStableHow

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

the class OpenFileCtx method processPerfectOverWrite.

/**
   * Honor 2 kinds of overwrites: 1). support some application like touch(write
   * the same content back to change mtime), 2) client somehow sends the same
   * write again in a different RPC.
   */
private WRITE3Response processPerfectOverWrite(DFSClient dfsClient, long offset, int count, WriteStableHow stableHow, byte[] data, String path, WccData wccData, IdMappingServiceProvider iug) {
    WRITE3Response response;
    // Read the content back
    byte[] readbuffer = new byte[count];
    int readCount = 0;
    FSDataInputStream fis = null;
    try {
        // Sync file data and length to avoid partial read failure
        fos.hsync(EnumSet.of(SyncFlag.UPDATE_LENGTH));
    } catch (ClosedChannelException closedException) {
        LOG.info("The FSDataOutputStream has been closed. " + "Continue processing the perfect overwrite.");
    } catch (IOException e) {
        LOG.info("hsync failed when processing possible perfect overwrite, path=" + path + " error: " + e);
        return new WRITE3Response(Nfs3Status.NFS3ERR_IO, wccData, 0, stableHow, Nfs3Constant.WRITE_COMMIT_VERF);
    }
    try {
        fis = dfsClient.createWrappedInputStream(dfsClient.open(path));
        readCount = fis.read(offset, readbuffer, 0, count);
        if (readCount < count) {
            LOG.error("Can't read back " + count + " bytes, partial read size: " + readCount);
            return new WRITE3Response(Nfs3Status.NFS3ERR_IO, wccData, 0, stableHow, Nfs3Constant.WRITE_COMMIT_VERF);
        }
    } catch (IOException e) {
        LOG.info("Read failed when processing possible perfect overwrite, path=" + path, e);
        return new WRITE3Response(Nfs3Status.NFS3ERR_IO, wccData, 0, stableHow, Nfs3Constant.WRITE_COMMIT_VERF);
    } finally {
        IOUtils.cleanup(LOG, fis);
    }
    // Compare with the request
    Comparator comparator = new Comparator();
    if (comparator.compare(readbuffer, 0, readCount, data, 0, count) != 0) {
        LOG.info("Perfect overwrite has different content");
        response = new WRITE3Response(Nfs3Status.NFS3ERR_INVAL, wccData, 0, stableHow, Nfs3Constant.WRITE_COMMIT_VERF);
    } else {
        LOG.info("Perfect overwrite has same content," + " updating the mtime, then return success");
        Nfs3FileAttributes postOpAttr = null;
        try {
            dfsClient.setTimes(path, Time.monotonicNow(), -1);
            postOpAttr = Nfs3Utils.getFileAttr(dfsClient, path, iug);
        } catch (IOException e) {
            LOG.info("Got error when processing perfect overwrite, path=" + path + " error: " + e);
            return new WRITE3Response(Nfs3Status.NFS3ERR_IO, wccData, 0, stableHow, Nfs3Constant.WRITE_COMMIT_VERF);
        }
        wccData.setPostOpAttr(postOpAttr);
        response = new WRITE3Response(Nfs3Status.NFS3_OK, wccData, count, stableHow, Nfs3Constant.WRITE_COMMIT_VERF);
    }
    return response;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) Nfs3FileAttributes(org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) IOException(java.io.IOException) WRITE3Response(org.apache.hadoop.nfs.nfs3.response.WRITE3Response) Comparator(org.apache.hadoop.io.BytesWritable.Comparator)

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