Search in sources :

Example 1 with Comparator

use of org.apache.hadoop.io.BytesWritable.Comparator 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

IOException (java.io.IOException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)1 Comparator (org.apache.hadoop.io.BytesWritable.Comparator)1 Nfs3FileAttributes (org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes)1 WRITE3Response (org.apache.hadoop.nfs.nfs3.response.WRITE3Response)1