Search in sources :

Example 6 with VerifierNone

use of org.apache.hadoop.oncrpc.security.VerifierNone 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 7 with VerifierNone

use of org.apache.hadoop.oncrpc.security.VerifierNone 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)

Example 8 with VerifierNone

use of org.apache.hadoop.oncrpc.security.VerifierNone 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 9 with VerifierNone

use of org.apache.hadoop.oncrpc.security.VerifierNone 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 10 with VerifierNone

use of org.apache.hadoop.oncrpc.security.VerifierNone in project hadoop by apache.

the class TestRpcReply method testRpcReply.

@Test
public void testRpcReply() {
    RpcReply reply = new RpcReply(0, ReplyState.MSG_ACCEPTED, new VerifierNone()) {

        @Override
        public XDR write(XDR xdr) {
            return null;
        }
    };
    Assert.assertEquals(0, reply.getXid());
    Assert.assertEquals(RpcMessage.Type.RPC_REPLY, reply.getMessageType());
    Assert.assertEquals(ReplyState.MSG_ACCEPTED, reply.getState());
}
Also used : VerifierNone(org.apache.hadoop.oncrpc.security.VerifierNone) Test(org.junit.Test)

Aggregations

VerifierNone (org.apache.hadoop.oncrpc.security.VerifierNone)21 XDR (org.apache.hadoop.oncrpc.XDR)15 WccData (org.apache.hadoop.nfs.nfs3.response.WccData)8 WRITE3Response (org.apache.hadoop.nfs.nfs3.response.WRITE3Response)6 IOException (java.io.IOException)5 CredentialsNone (org.apache.hadoop.oncrpc.security.CredentialsNone)5 Test (org.junit.Test)5 FileHandle (org.apache.hadoop.nfs.nfs3.FileHandle)4 RpcCall (org.apache.hadoop.oncrpc.RpcCall)4 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)4 WriteStableHow (org.apache.hadoop.nfs.nfs3.Nfs3Constant.WriteStableHow)3 WccAttr (org.apache.hadoop.nfs.nfs3.response.WccAttr)3 RpcResponse (org.apache.hadoop.oncrpc.RpcResponse)3 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 Nfs3FileAttributes (org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes)2 COMMIT3Response (org.apache.hadoop.nfs.nfs3.response.COMMIT3Response)2 Credentials (org.apache.hadoop.oncrpc.security.Credentials)2 Verifier (org.apache.hadoop.oncrpc.security.Verifier)2 File (java.io.File)1