public static void WriteChannelCommit(Org.Jboss.Netty.Channel.Channel channel, XDR @out, int xid) { if (RpcProgramNfs3.Log.IsDebugEnabled()) { RpcProgramNfs3.Log.Debug("Commit done:" + xid); } ChannelBuffer outBuf = XDR.WriteMessageTcp(@out, true); channel.Write(outBuf); }
/// <exception cref="System.Exception"/> private void Commit(string fileName, int len) { HdfsFileStatus status = nn.GetRpcServer().GetFileInfo(fileName); long dirId = status.GetFileId(); FileHandle handle = new FileHandle(dirId); XDR xdr_req = new XDR(); COMMIT3Request req = new COMMIT3Request(handle, 0, len); req.Serialize(xdr_req); Org.Jboss.Netty.Channel.Channel ch = Org.Mockito.Mockito.Mock <Org.Jboss.Netty.Channel.Channel >(); COMMIT3Response response2 = nfsd.Commit(xdr_req.AsReadOnlyWrap(), ch, 1, securityHandler , new IPEndPoint("localhost", 1234)); NUnit.Framework.Assert.AreEqual("Incorrect COMMIT3Response:", null, response2); }
/// <summary>Send a write response to the netty network socket channel</summary> public static void WriteChannel(Org.Jboss.Netty.Channel.Channel channel, XDR @out , int xid) { if (channel == null) { RpcProgramNfs3.Log.Info("Null channel should only happen in tests. Do nothing."); return; } if (RpcProgramNfs3.Log.IsDebugEnabled()) { RpcProgramNfs3.Log.Debug(WriteRpcEnd + xid); } ChannelBuffer outBuf = XDR.WriteMessageTcp(@out, true); channel.Write(outBuf); }
internal WriteCtx(FileHandle handle, long offset, int count, int originalCount, Nfs3Constant.WriteStableHow stableHow, ByteBuffer data, Org.Jboss.Netty.Channel.Channel channel, int xid, bool replied, WriteCtx.DataState dataState) { this.handle = handle; this.offset = offset; this.count = count; this.originalCount = originalCount; this.stableHow = stableHow; this.data = data; this.channel = channel; this.xid = xid; this.replied = replied; this.dataState = dataState; raf = null; this.startTime = Runtime.NanoTime(); }
/// <exception cref="System.Exception"/> public virtual void TestCommit() { HdfsFileStatus status = nn.GetRpcServer().GetFileInfo("/tmp/bar"); long dirId = status.GetFileId(); FileHandle handle = new FileHandle(dirId); XDR xdr_req = new XDR(); COMMIT3Request req = new COMMIT3Request(handle, 0, 5); req.Serialize(xdr_req); Org.Jboss.Netty.Channel.Channel ch = Org.Mockito.Mockito.Mock <Org.Jboss.Netty.Channel.Channel >(); // Attempt by an unpriviledged user should fail. COMMIT3Response response1 = nfsd.Commit(xdr_req.AsReadOnlyWrap(), ch, 1, securityHandlerUnpriviledged , new IPEndPoint("localhost", 1234)); NUnit.Framework.Assert.AreEqual("Incorrect return code:", Nfs3Status.Nfs3errAcces , response1.GetStatus()); // Attempt by a priviledged user should pass. COMMIT3Response response2 = nfsd.Commit(xdr_req.AsReadOnlyWrap(), ch, 1, securityHandler , new IPEndPoint("localhost", 1234)); NUnit.Framework.Assert.AreEqual("Incorrect COMMIT3Response:", null, response2); }
internal virtual void HandleCommit(DFSClient dfsClient, FileHandle fileHandle, long commitOffset, Org.Jboss.Netty.Channel.Channel channel, int xid, Nfs3FileAttributes preOpAttr) { long startTime = Runtime.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.Nfs3Ok; } else { OpenFileCtx.COMMIT_STATUS ret = openFileCtx.CheckCommit(dfsClient, commitOffset, channel, xid, preOpAttr, false); switch (ret) { case OpenFileCtx.COMMIT_STATUS.CommitFinished: case OpenFileCtx.COMMIT_STATUS.CommitInactiveCtx: { status = Nfs3Status.Nfs3Ok; break; } case OpenFileCtx.COMMIT_STATUS.CommitInactiveWithPendingWrite: case OpenFileCtx.COMMIT_STATUS.CommitError: { status = Nfs3Status.Nfs3errIo; break; } case OpenFileCtx.COMMIT_STATUS.CommitWait: { // Do nothing. Commit is async now. return; } case OpenFileCtx.COMMIT_STATUS.CommitSpecialWait: { status = Nfs3Status.Nfs3errJukebox; break; } case OpenFileCtx.COMMIT_STATUS.CommitSpecialSuccess: { status = Nfs3Status.Nfs3Ok; break; } default: { Log.Error("Should not get commit return code: " + ret.ToString()); throw new RuntimeException("Should not get commit return code: " + ret.ToString() ); } } } // 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.WriteCommitVerf ); RpcProgramNfs3.metrics.AddCommit(Nfs3Utils.GetElapsedTime(startTime)); Nfs3Utils.WriteChannelCommit(channel, response.Serialize(new XDR(), xid, new VerifierNone ()), xid); }
/// <exception cref="System.IO.IOException"/> internal virtual void HandleWrite(DFSClient dfsClient, WRITE3Request request, Org.Jboss.Netty.Channel.Channel channel, int xid, Nfs3FileAttributes preOpAttr) { int count = request.GetCount(); byte[] data = ((byte[])request.GetData().Array()); if (data.Length < count) { WRITE3Response response = new WRITE3Response(Nfs3Status.Nfs3errInval); 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.IoFileBufferSizeKey, CommonConfigurationKeysPublic.IoFileBufferSizeDefault); 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 is 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; } 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.Nfs3errIo, fileWcc, count , request.GetStableHow(), Nfs3Constant.WriteCommitVerf); Nfs3Utils.WriteChannel(channel, response.Serialize(new XDR(), xid, new VerifierNone ()), xid); return; } // Add open stream string writeDumpDir = config.Get(NfsConfigKeys.DfsNfsFileDumpDirKey, NfsConfigKeys .DfsNfsFileDumpDirDefault); 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.Nfs3errJukebox, fileWcc, 0, request.GetStableHow(), Nfs3Constant.WriteCommitVerf); 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; }
public virtual void TestCheckCommit() { // Validate all the commit check return codes OpenFileCtx.COMMIT_STATUS, which // includes COMMIT_FINISHED, COMMIT_WAIT, COMMIT_INACTIVE_CTX, // COMMIT_INACTIVE_WITH_PENDING_WRITE, COMMIT_ERROR, and COMMIT_DO_SYNC. DFSClient dfsClient = Org.Mockito.Mockito.Mock <DFSClient>(); Nfs3FileAttributes attr = new Nfs3FileAttributes(); HdfsDataOutputStream fos = Org.Mockito.Mockito.Mock <HdfsDataOutputStream>(); Org.Mockito.Mockito.When(fos.GetPos()).ThenReturn((long)0); NfsConfiguration conf = new NfsConfiguration(); conf.SetBoolean(NfsConfigKeys.LargeFileUpload, false); OpenFileCtx ctx = new OpenFileCtx(fos, attr, "/dumpFilePath", dfsClient, new ShellBasedIdMapping (conf), false, conf); OpenFileCtx.COMMIT_STATUS ret; // Test inactive open file context ctx.SetActiveStatusForTest(false); Org.Jboss.Netty.Channel.Channel ch = Org.Mockito.Mockito.Mock <Org.Jboss.Netty.Channel.Channel >(); ret = ctx.CheckCommit(dfsClient, 0, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitInactiveCtx); ctx.GetPendingWritesForTest()[new OffsetRange(5, 10)] = new WriteCtx(null, 0, 0, 0, null, null, null, 0, false, null); ret = ctx.CheckCommit(dfsClient, 0, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitInactiveWithPendingWrite ); // Test request with non zero commit offset ctx.SetActiveStatusForTest(true); Org.Mockito.Mockito.When(fos.GetPos()).ThenReturn((long)10); ctx.SetNextOffsetForTest(10); OpenFileCtx.COMMIT_STATUS status = ctx.CheckCommitInternal(5, null, 1, attr, false ); NUnit.Framework.Assert.IsTrue(status == OpenFileCtx.COMMIT_STATUS.CommitDoSync); // Do_SYNC state will be updated to FINISHED after data sync ret = ctx.CheckCommit(dfsClient, 5, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitFinished); status = ctx.CheckCommitInternal(10, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(status == OpenFileCtx.COMMIT_STATUS.CommitDoSync); ret = ctx.CheckCommit(dfsClient, 10, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitFinished); ConcurrentNavigableMap <long, OpenFileCtx.CommitCtx> commits = ctx.GetPendingCommitsForTest (); NUnit.Framework.Assert.IsTrue(commits.Count == 0); ret = ctx.CheckCommit(dfsClient, 11, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitWait); NUnit.Framework.Assert.IsTrue(commits.Count == 1); long key = commits.FirstKey(); NUnit.Framework.Assert.IsTrue(key == 11); // Test request with zero commit offset Sharpen.Collections.Remove(commits, System.Convert.ToInt64(11)); // There is one pending write [5,10] ret = ctx.CheckCommit(dfsClient, 0, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitWait); NUnit.Framework.Assert.IsTrue(commits.Count == 1); key = commits.FirstKey(); NUnit.Framework.Assert.IsTrue(key == 9); // Empty pending writes Sharpen.Collections.Remove(ctx.GetPendingWritesForTest(), new OffsetRange(5, 10)); ret = ctx.CheckCommit(dfsClient, 0, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitFinished); }
public virtual void TestCheckCommitFromReadLargeFileUpload() { // Validate all the commit check return codes OpenFileCtx.COMMIT_STATUS with large file upload option DFSClient dfsClient = Org.Mockito.Mockito.Mock <DFSClient>(); Nfs3FileAttributes attr = new Nfs3FileAttributes(); HdfsDataOutputStream fos = Org.Mockito.Mockito.Mock <HdfsDataOutputStream>(); Org.Mockito.Mockito.When(fos.GetPos()).ThenReturn((long)0); NfsConfiguration config = new NfsConfiguration(); config.SetBoolean(NfsConfigKeys.LargeFileUpload, true); OpenFileCtx ctx = new OpenFileCtx(fos, attr, "/dumpFilePath", dfsClient, new ShellBasedIdMapping (config), false, config); FileHandle h = new FileHandle(1); // fake handle for "/dumpFilePath" OpenFileCtx.COMMIT_STATUS ret; WriteManager wm = new WriteManager(new ShellBasedIdMapping(config), config, false ); NUnit.Framework.Assert.IsTrue(wm.AddOpenFileStream(h, ctx)); // Test inactive open file context ctx.SetActiveStatusForTest(false); Org.Jboss.Netty.Channel.Channel ch = Org.Mockito.Mockito.Mock <Org.Jboss.Netty.Channel.Channel >(); ret = ctx.CheckCommit(dfsClient, 0, ch, 1, attr, true); NUnit.Framework.Assert.AreEqual(OpenFileCtx.COMMIT_STATUS.CommitInactiveCtx, ret); NUnit.Framework.Assert.AreEqual(Nfs3Status.Nfs3Ok, wm.CommitBeforeRead(dfsClient, h, 0)); ctx.GetPendingWritesForTest()[new OffsetRange(10, 15)] = new WriteCtx(null, 0, 0, 0, null, null, null, 0, false, null); ret = ctx.CheckCommit(dfsClient, 0, ch, 1, attr, true); NUnit.Framework.Assert.AreEqual(OpenFileCtx.COMMIT_STATUS.CommitInactiveWithPendingWrite , ret); NUnit.Framework.Assert.AreEqual(Nfs3Status.Nfs3errIo, wm.CommitBeforeRead(dfsClient , h, 0)); // Test request with non zero commit offset ctx.SetActiveStatusForTest(true); Org.Mockito.Mockito.When(fos.GetPos()).ThenReturn((long)6); ctx.SetNextOffsetForTest((long)10); OpenFileCtx.COMMIT_STATUS status = ctx.CheckCommitInternal(5, ch, 1, attr, false); NUnit.Framework.Assert.AreEqual(OpenFileCtx.COMMIT_STATUS.CommitDoSync, status); // Do_SYNC state will be updated to FINISHED after data sync ret = ctx.CheckCommit(dfsClient, 5, ch, 1, attr, true); NUnit.Framework.Assert.AreEqual(OpenFileCtx.COMMIT_STATUS.CommitFinished, ret); NUnit.Framework.Assert.AreEqual(Nfs3Status.Nfs3Ok, wm.CommitBeforeRead(dfsClient, h, 5)); // Test request with sequential writes status = ctx.CheckCommitInternal(9, ch, 1, attr, true); NUnit.Framework.Assert.IsTrue(status == OpenFileCtx.COMMIT_STATUS.CommitSpecialWait ); ret = ctx.CheckCommit(dfsClient, 9, ch, 1, attr, true); NUnit.Framework.Assert.AreEqual(OpenFileCtx.COMMIT_STATUS.CommitSpecialWait, ret); NUnit.Framework.Assert.AreEqual(Nfs3Status.Nfs3errJukebox, wm.CommitBeforeRead(dfsClient , h, 9)); // Test request with non-sequential writes ConcurrentNavigableMap <long, OpenFileCtx.CommitCtx> commits = ctx.GetPendingCommitsForTest (); NUnit.Framework.Assert.IsTrue(commits.Count == 0); ret = ctx.CheckCommit(dfsClient, 16, ch, 1, attr, true); NUnit.Framework.Assert.AreEqual(OpenFileCtx.COMMIT_STATUS.CommitSpecialSuccess, ret ); NUnit.Framework.Assert.AreEqual(0, commits.Count); // commit triggered by read doesn't wait NUnit.Framework.Assert.AreEqual(Nfs3Status.Nfs3Ok, wm.CommitBeforeRead(dfsClient, h, 16)); // Test request with zero commit offset // There is one pending write [10,15] ret = ctx.CheckCommit(dfsClient, 0, ch, 1, attr, true); NUnit.Framework.Assert.AreEqual(OpenFileCtx.COMMIT_STATUS.CommitSpecialWait, ret); NUnit.Framework.Assert.AreEqual(0, commits.Count); NUnit.Framework.Assert.AreEqual(Nfs3Status.Nfs3errJukebox, wm.CommitBeforeRead(dfsClient , h, 0)); // Empty pending writes Sharpen.Collections.Remove(ctx.GetPendingWritesForTest(), new OffsetRange(10, 15) ); ret = ctx.CheckCommit(dfsClient, 0, ch, 1, attr, true); NUnit.Framework.Assert.AreEqual(OpenFileCtx.COMMIT_STATUS.CommitSpecialWait, ret); NUnit.Framework.Assert.AreEqual(Nfs3Status.Nfs3errJukebox, wm.CommitBeforeRead(dfsClient , h, 0)); }
public virtual void TestCheckCommitLargeFileUpload() { // Validate all the commit check return codes OpenFileCtx.COMMIT_STATUS with // large file upload option. DFSClient dfsClient = Org.Mockito.Mockito.Mock <DFSClient>(); Nfs3FileAttributes attr = new Nfs3FileAttributes(); HdfsDataOutputStream fos = Org.Mockito.Mockito.Mock <HdfsDataOutputStream>(); Org.Mockito.Mockito.When(fos.GetPos()).ThenReturn((long)0); NfsConfiguration conf = new NfsConfiguration(); conf.SetBoolean(NfsConfigKeys.LargeFileUpload, true); OpenFileCtx ctx = new OpenFileCtx(fos, attr, "/dumpFilePath", dfsClient, new ShellBasedIdMapping (conf), false, conf); OpenFileCtx.COMMIT_STATUS ret; // Test inactive open file context ctx.SetActiveStatusForTest(false); Org.Jboss.Netty.Channel.Channel ch = Org.Mockito.Mockito.Mock <Org.Jboss.Netty.Channel.Channel >(); ret = ctx.CheckCommit(dfsClient, 0, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitInactiveCtx); ctx.GetPendingWritesForTest()[new OffsetRange(10, 15)] = new WriteCtx(null, 0, 0, 0, null, null, null, 0, false, null); ret = ctx.CheckCommit(dfsClient, 0, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitInactiveWithPendingWrite ); // Test request with non zero commit offset ctx.SetActiveStatusForTest(true); Org.Mockito.Mockito.When(fos.GetPos()).ThenReturn((long)8); ctx.SetNextOffsetForTest(10); OpenFileCtx.COMMIT_STATUS status = ctx.CheckCommitInternal(5, null, 1, attr, false ); NUnit.Framework.Assert.IsTrue(status == OpenFileCtx.COMMIT_STATUS.CommitDoSync); // Do_SYNC state will be updated to FINISHED after data sync ret = ctx.CheckCommit(dfsClient, 5, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitFinished); // Test commit sequential writes status = ctx.CheckCommitInternal(10, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(status == OpenFileCtx.COMMIT_STATUS.CommitSpecialWait ); ret = ctx.CheckCommit(dfsClient, 10, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitSpecialWait); // Test commit non-sequential writes ConcurrentNavigableMap <long, OpenFileCtx.CommitCtx> commits = ctx.GetPendingCommitsForTest (); NUnit.Framework.Assert.IsTrue(commits.Count == 1); ret = ctx.CheckCommit(dfsClient, 16, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitSpecialSuccess ); NUnit.Framework.Assert.IsTrue(commits.Count == 1); // Test request with zero commit offset Sharpen.Collections.Remove(commits, System.Convert.ToInt64(10)); // There is one pending write [10,15] ret = ctx.CheckCommitInternal(0, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitSpecialWait); ret = ctx.CheckCommitInternal(9, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitSpecialWait); NUnit.Framework.Assert.IsTrue(commits.Count == 2); // Empty pending writes. nextOffset=10, flushed pos=8 Sharpen.Collections.Remove(ctx.GetPendingWritesForTest(), new OffsetRange(10, 15) ); ret = ctx.CheckCommit(dfsClient, 0, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitSpecialWait); // Empty pending writes ctx.SetNextOffsetForTest((long)8); // flushed pos = 8 ret = ctx.CheckCommit(dfsClient, 0, ch, 1, attr, false); NUnit.Framework.Assert.IsTrue(ret == OpenFileCtx.COMMIT_STATUS.CommitFinished); }