public static void SetupCluster() { if (DomainSocket.GetLoadingFailureReason() != null) { return; } sockDir = new TemporarySocketDirectory(); HdfsConfiguration conf = new HdfsConfiguration(); conf.Set(DFSConfigKeys.DfsDomainSocketPathKey, new FilePath(sockDir.GetDir(), "TestParallelShortCircuitReadUnCached._PORT.sock" ).GetAbsolutePath()); conf.SetBoolean(DFSConfigKeys.DfsClientReadShortcircuitKey, true); // Enabling data transfer encryption should have no effect when using // short-circuit local reads. This is a regression test for HDFS-5353. conf.SetBoolean(DFSConfigKeys.DfsEncryptDataTransferKey, true); conf.SetBoolean(DFSConfigKeys.DfsBlockAccessTokenEnableKey, true); conf.SetBoolean(DFSConfigKeys.DfsClientReadShortcircuitSkipChecksumKey, false); conf.SetBoolean(DFSConfigKeys.DfsClientDomainSocketDataTraffic, true); // We want to test reading from stale sockets. conf.SetInt(DFSConfigKeys.DfsDatanodeSocketReuseKeepaliveKey, 1); conf.SetLong(DFSConfigKeys.DfsClientSocketCacheExpiryMsecKey, 5 * 60 * 1000); conf.SetInt(DFSConfigKeys.DfsClientSocketCacheCapacityKey, 32); // Avoid using the FileInputStreamCache. conf.SetInt(DFSConfigKeys.DfsClientReadShortcircuitStreamsCacheSizeKey, 0); DomainSocket.DisableBindPathValidation(); DFSInputStream.tcpReadsDisabledForTesting = true; SetupCluster(1, conf); }
/// <exception cref="System.IO.IOException"/> private static DomainPeer GetDomainPeerToDn(Configuration conf) { DomainSocket sock = DomainSocket.Connect(conf.Get(DFSConfigKeys.DfsDomainSocketPathKey )); return(new DomainPeer(sock)); }
public virtual void TestBothOldAndNewShortCircuitConfigured() { short ReplFactor = 1; int FileLength = 512; Assume.AssumeTrue(null == DomainSocket.GetLoadingFailureReason()); TemporarySocketDirectory socketDir = new TemporarySocketDirectory(); HdfsConfiguration conf = GetConfiguration(socketDir); MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build(); cluster.WaitActive(); socketDir.Close(); FileSystem fs = cluster.GetFileSystem(); Path path = new Path("/foo"); byte[] orig = new byte[FileLength]; for (int i = 0; i < orig.Length; i++) { orig[i] = unchecked ((byte)(i % 10)); } FSDataOutputStream fos = fs.Create(path, (short)1); fos.Write(orig); fos.Close(); DFSTestUtil.WaitReplication(fs, path, ReplFactor); FSDataInputStream fis = cluster.GetFileSystem().Open(path); byte[] buf = new byte[FileLength]; IOUtils.ReadFully(fis, buf, 0, FileLength); fis.Close(); Assert.AssertArrayEquals(orig, buf); Arrays.Equals(orig, buf); cluster.Shutdown(); }
public virtual DomainSocket CreateSocket(DomainSocketFactory.PathInfo info, int socketTimeout ) { Preconditions.CheckArgument(info.GetPathState() != DomainSocketFactory.PathState. Unusable); bool success = false; DomainSocket sock = null; try { sock = DomainSocket.Connect(info.GetPath()); sock.SetAttribute(DomainSocket.ReceiveTimeout, socketTimeout); success = true; } catch (IOException e) { Log.Warn("error creating DomainSocket", e); } finally { // fall through if (!success) { if (sock != null) { IOUtils.CloseQuietly(sock); } pathMap.Put(info.GetPath(), DomainSocketFactory.PathState.Unusable); sock = null; } } return(sock); }
public virtual void TestShortCircuitReadBlockFileCorruption() { Assume.AssumeThat(DomainSocket.GetLoadingFailureReason(), CoreMatchers.EqualTo(null )); StartUpCluster(true, 1 + EvictionLowWatermark, true, false); DoShortCircuitReadBlockFileCorruptionTest(); }
public static void Init() { sockDir = new TemporarySocketDirectory(); DomainSocket.DisableBindPathValidation(); prevCacheManipulator = NativeIO.POSIX.GetCacheManipulator(); NativeIO.POSIX.SetCacheManipulator(new _CacheManipulator_94()); }
public DomainPeer(DomainSocket socket) { this.socket = socket; this.@out = socket.GetOutputStream(); this.@in = socket.GetInputStream(); this.channel = socket.GetChannel(); }
public static void TeardownCluster() { if (DomainSocket.GetLoadingFailureReason() != null) { return; } sockDir.Close(); TestParallelReadUtil.TeardownCluster(); }
public virtual bool Handle(DomainSocket sock) { lock (registry) { lock (this) { registry.RemoveShm(this); } } return(true); }
public static void SetupCluster() { DFSInputStream.tcpReadsDisabledForTesting = true; HdfsConfiguration conf = new HdfsConfiguration(); conf.Set(DFSConfigKeys.DfsDomainSocketPathKey, string.Empty); conf.SetBoolean(DFSConfigKeys.DfsClientUseLegacyBlockreaderlocal, true); conf.SetBoolean(DFSConfigKeys.DfsClientDomainSocketDataTraffic, false); conf.SetBoolean(DFSConfigKeys.DfsClientReadShortcircuitKey, true); conf.SetBoolean(DFSConfigKeys.DfsClientReadShortcircuitSkipChecksumKey, false); conf.Set(DFSConfigKeys.DfsBlockLocalPathAccessUserKey, UserGroupInformation.GetCurrentUser ().GetShortUserName()); DomainSocket.DisableBindPathValidation(); SetupCluster(1, conf); }
public static void SetupCluster() { if (DomainSocket.GetLoadingFailureReason() != null) { return; } DFSInputStream.tcpReadsDisabledForTesting = true; sockDir = new TemporarySocketDirectory(); HdfsConfiguration conf = new HdfsConfiguration(); conf.Set(DFSConfigKeys.DfsDomainSocketPathKey, new FilePath(sockDir.GetDir(), "TestParallelLocalRead.%d.sock" ).GetAbsolutePath()); conf.SetBoolean(DFSConfigKeys.DfsClientReadShortcircuitKey, true); conf.SetBoolean(DFSConfigKeys.DfsClientReadShortcircuitSkipChecksumKey, false); DomainSocket.DisableBindPathValidation(); SetupCluster(1, conf); }
/// <summary>Handle a DFSClient request to create a new memory segment.</summary> /// <param name="clientName">Client name as reported by the client.</param> /// <param name="sock"> /// The DomainSocket to associate with this memory /// segment. When this socket is closed, or the /// other side writes anything to the socket, the /// segment will be closed. This can happen at any /// time, including right after this function returns. /// </param> /// <returns> /// A NewShmInfo object. The caller must close the /// NewShmInfo object once they are done with it. /// </returns> /// <exception cref="System.IO.IOException">If the new memory segment could not be created. /// </exception> public virtual ShortCircuitRegistry.NewShmInfo CreateNewMemorySegment(string clientName , DomainSocket sock) { ShortCircuitRegistry.NewShmInfo info = null; ShortCircuitRegistry.RegisteredShm shm = null; ShortCircuitShm.ShmId shmId = null; lock (this) { if (!enabled) { if (Log.IsTraceEnabled()) { Log.Trace("createNewMemorySegment: ShortCircuitRegistry is " + "not enabled."); } throw new NotSupportedException(); } FileInputStream fis = null; try { do { shmId = ShortCircuitShm.ShmId.CreateRandom(); }while (segments.Contains(shmId)); fis = shmFactory.CreateDescriptor(clientName, ShmLength); shm = new ShortCircuitRegistry.RegisteredShm(clientName, shmId, fis, this); } finally { if (shm == null) { IOUtils.CloseQuietly(fis); } } info = new ShortCircuitRegistry.NewShmInfo(shmId, fis); segments[shmId] = shm; } // Drop the registry lock to prevent deadlock. // After this point, RegisteredShm#handle may be called at any time. watcher.Add(sock, shm); if (Log.IsTraceEnabled()) { Log.Trace("createNewMemorySegment: created " + info.shmId); } return(info); }
public DomainSocketFactory(DFSClient.Conf conf) { string feature; if (conf.IsShortCircuitLocalReads() && (!conf.IsUseLegacyBlockReaderLocal())) { feature = "The short-circuit local reads feature"; } else { if (conf.IsDomainSocketDataTraffic()) { feature = "UNIX domain socket data traffic"; } else { feature = null; } } if (feature == null) { PerformanceAdvisory.Log.Debug("Both short-circuit local reads and UNIX domain socket are disabled." ); } else { if (conf.GetDomainSocketPath().IsEmpty()) { throw new HadoopIllegalArgumentException(feature + " is enabled but " + DFSConfigKeys .DfsDomainSocketPathKey + " is not set."); } else { if (DomainSocket.GetLoadingFailureReason() != null) { Log.Warn(feature + " cannot be used because " + DomainSocket.GetLoadingFailureReason ()); } else { Log.Debug(feature + " is enabled."); } } } }
private static Configuration CreateShortCircuitConf(string testName, TemporarySocketDirectory sockDir) { Configuration conf = new Configuration(); conf.Set(DFSConfigKeys.DfsClientContext, testName); conf.SetLong(DFSConfigKeys.DfsBlockSizeKey, 4096); conf.Set(DFSConfigKeys.DfsDomainSocketPathKey, new FilePath(sockDir.GetDir(), testName ).GetAbsolutePath()); conf.SetBoolean(DFSConfigKeys.DfsClientReadShortcircuitKey, true); conf.SetBoolean(DFSConfigKeys.DfsClientReadShortcircuitSkipChecksumKey, false); conf.SetBoolean(DFSConfigKeys.DfsClientDomainSocketDataTraffic, false); DFSInputStream.tcpReadsDisabledForTesting = true; DomainSocket.DisableBindPathValidation(); Assume.AssumeThat(DomainSocket.GetLoadingFailureReason(), CoreMatchers.EqualTo(null )); return(conf); }
/// <exception cref="System.IO.IOException"/> public virtual void TestSkipWithVerifyChecksum() { int size = blockSize; Configuration conf = new Configuration(); conf.SetBoolean(DFSConfigKeys.DfsClientReadShortcircuitKey, true); conf.SetBoolean(DFSConfigKeys.DfsClientReadShortcircuitSkipChecksumKey, false); conf.Set(DFSConfigKeys.DfsDomainSocketPathKey, "/tmp/testSkipWithVerifyChecksum._PORT" ); DomainSocket.DisableBindPathValidation(); MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Format( true).Build(); FileSystem fs = cluster.GetFileSystem(); try { // check that / exists Path path = new Path("/"); NUnit.Framework.Assert.IsTrue("/ should be a directory", fs.GetFileStatus(path).IsDirectory () == true); byte[] fileData = AppendTestUtil.RandomBytes(seed, size * 3); // create a new file in home directory. Do not close it. Path file1 = new Path("filelocal.dat"); FSDataOutputStream stm = CreateFile(fs, file1, 1); // write to file stm.Write(fileData); stm.Close(); // now test the skip function FSDataInputStream instm = fs.Open(file1); byte[] actual = new byte[fileData.Length]; // read something from the block first, otherwise BlockReaderLocal.skip() // will not be invoked int nread = instm.Read(actual, 0, 3); long skipped = 2 * size + 3; instm.Seek(skipped); nread = instm.Read(actual, (int)(skipped + nread), 3); instm.Close(); } finally { fs.Close(); cluster.Shutdown(); } }
/// <summary> /// Handle the closure of the UNIX domain socket associated with this shared /// memory segment by marking this segment as stale. /// </summary> /// <remarks> /// Handle the closure of the UNIX domain socket associated with this shared /// memory segment by marking this segment as stale. /// If there are no slots associated with this shared memory segment, it will /// be freed immediately in this function. /// </remarks> public virtual bool Handle(DomainSocket sock) { manager.UnregisterShm(GetShmId()); lock (this) { Preconditions.CheckState(!disconnected); disconnected = true; bool hadSlots = false; for (IEnumerator <ShortCircuitShm.Slot> iter = SlotIterator(); iter.HasNext();) { ShortCircuitShm.Slot slot = iter.Next(); slot.MakeInvalid(); hadSlots = true; } if (!hadSlots) { Free(); } } return(true); }
/// <summary>Get information about a domain socket path.</summary> /// <param name="addr">The inet address to use.</param> /// <param name="conf">The client configuration.</param> /// <returns>Information about the socket path.</returns> public virtual DomainSocketFactory.PathInfo GetPathInfo(IPEndPoint addr, DFSClient.Conf conf) { // If there is no domain socket path configured, we can't use domain // sockets. if (conf.GetDomainSocketPath().IsEmpty()) { return(DomainSocketFactory.PathInfo.NotConfigured); } // If we can't do anything with the domain socket, don't create it. if (!conf.IsDomainSocketDataTraffic() && (!conf.IsShortCircuitLocalReads() || conf .IsUseLegacyBlockReaderLocal())) { return(DomainSocketFactory.PathInfo.NotConfigured); } // If the DomainSocket code is not loaded, we can't create // DomainSocket objects. if (DomainSocket.GetLoadingFailureReason() != null) { return(DomainSocketFactory.PathInfo.NotConfigured); } // UNIX domain sockets can only be used to talk to local peers if (!DFSClient.IsLocalAddress(addr)) { return(DomainSocketFactory.PathInfo.NotConfigured); } string escapedPath = DomainSocket.GetEffectivePath(conf.GetDomainSocketPath(), addr .Port); DomainSocketFactory.PathState status = pathMap.GetIfPresent(escapedPath); if (status == null) { return(new DomainSocketFactory.PathInfo(escapedPath, DomainSocketFactory.PathState .Valid)); } else { return(new DomainSocketFactory.PathInfo(escapedPath, status)); } }
/// <summary>Get the next DomainPeer-- either from the cache or by creating it.</summary> /// <returns>the next DomainPeer, or null if we could not construct one.</returns> private BlockReaderFactory.BlockReaderPeer NextDomainPeer() { if (remainingCacheTries > 0) { Peer peer = clientContext.GetPeerCache().Get(datanode, true); if (peer != null) { if (Log.IsTraceEnabled()) { Log.Trace("nextDomainPeer: reusing existing peer " + peer); } return(new BlockReaderFactory.BlockReaderPeer(peer, true)); } } DomainSocket sock = clientContext.GetDomainSocketFactory().CreateSocket(pathInfo, conf.socketTimeout); if (sock == null) { return(null); } return(new BlockReaderFactory.BlockReaderPeer(new DomainPeer(sock), false)); }
/// <exception cref="System.IO.IOException"/> /// <exception cref="Sharpen.SocketTimeoutException"/> public virtual Peer Accept() { DomainSocket connSock = sock.Accept(); Peer peer = null; bool success = false; try { peer = new DomainPeer(connSock); success = true; return(peer); } finally { if (!success) { if (peer != null) { peer.Close(); } connSock.Close(); } } }
/// <exception cref="System.IO.IOException"/> public DomainPeerServer(string path, int port) : this(DomainSocket.BindAndListen(DomainSocket.GetEffectivePath(path, port))) { }
public static void Init() { DomainSocket.DisableBindPathValidation(); }
public virtual void Before() { Assume.AssumeThat(DomainSocket.GetLoadingFailureReason(), CoreMatchers.EqualTo(null )); }
internal DomainPeerServer(DomainSocket sock) { this.sock = sock; }
public static void Init() { sockDir = new TemporarySocketDirectory(); DomainSocket.DisableBindPathValidation(); }
/// <summary>Request file descriptors from a DomainPeer.</summary> /// <param name="peer">The peer to use for communication.</param> /// <param name="slot"> /// If non-null, the shared memory slot to associate with the /// new ShortCircuitReplica. /// </param> /// <returns> /// A ShortCircuitReplica object if we could communicate with the /// datanode; null, otherwise. /// </returns> /// <exception cref="System.IO.IOException"> /// If we encountered an I/O exception while communicating /// with the datanode. /// </exception> private ShortCircuitReplicaInfo RequestFileDescriptors(DomainPeer peer, ShortCircuitShm.Slot slot) { ShortCircuitCache cache = clientContext.GetShortCircuitCache(); DataOutputStream @out = new DataOutputStream(new BufferedOutputStream(peer.GetOutputStream ())); ShortCircuitShm.SlotId slotId = slot == null ? null : slot.GetSlotId(); new Sender(@out).RequestShortCircuitFds(block, token, slotId, 1, failureInjector. GetSupportsReceiptVerification()); DataInputStream @in = new DataInputStream(peer.GetInputStream()); DataTransferProtos.BlockOpResponseProto resp = DataTransferProtos.BlockOpResponseProto .ParseFrom(PBHelper.VintPrefixed(@in)); DomainSocket sock = peer.GetDomainSocket(); failureInjector.InjectRequestFileDescriptorsFailure(); switch (resp.GetStatus()) { case DataTransferProtos.Status.Success: { byte[] buf = new byte[1]; FileInputStream[] fis = new FileInputStream[2]; sock.RecvFileInputStreams(fis, buf, 0, buf.Length); ShortCircuitReplica replica = null; try { ExtendedBlockId key = new ExtendedBlockId(block.GetBlockId(), block.GetBlockPoolId ()); if (buf[0] == DataTransferProtos.ShortCircuitFdResponse.UseReceiptVerification.GetNumber ()) { Log.Trace("Sending receipt verification byte for slot " + slot); sock.GetOutputStream().Write(0); } replica = new ShortCircuitReplica(key, fis[0], fis[1], cache, Time.MonotonicNow() , slot); return(new ShortCircuitReplicaInfo(replica)); } catch (IOException e) { // This indicates an error reading from disk, or a format error. Since // it's not a socket communication problem, we return null rather than // throwing an exception. Log.Warn(this + ": error creating ShortCircuitReplica.", e); return(null); } finally { if (replica == null) { IOUtils.Cleanup(DFSClient.Log, fis[0], fis[1]); } } goto case DataTransferProtos.Status.ErrorUnsupported; } case DataTransferProtos.Status.ErrorUnsupported: { if (!resp.HasShortCircuitAccessVersion()) { Log.Warn("short-circuit read access is disabled for " + "DataNode " + datanode + ". reason: " + resp.GetMessage()); clientContext.GetDomainSocketFactory().DisableShortCircuitForPath(pathInfo.GetPath ()); } else { Log.Warn("short-circuit read access for the file " + fileName + " is disabled for DataNode " + datanode + ". reason: " + resp.GetMessage()); } return(null); } case DataTransferProtos.Status.ErrorAccessToken: { string msg = "access control error while " + "attempting to set up short-circuit access to " + fileName + resp.GetMessage(); if (Log.IsDebugEnabled()) { Log.Debug(this + ":" + msg); } return(new ShortCircuitReplicaInfo(new SecretManager.InvalidToken(msg))); } default: { Log.Warn(this + ": unknown response code " + resp.GetStatus() + " while attempting to set up short-circuit access. " + resp.GetMessage()); clientContext.GetDomainSocketFactory().DisableShortCircuitForPath(pathInfo.GetPath ()); return(null); } } }
public virtual void Init() { DomainSocket.DisableBindPathValidation(); Assume.AssumeThat(DomainSocket.GetLoadingFailureReason(), CoreMatchers.EqualTo(null )); }
/// <summary>Ask the DataNode for a new shared memory segment.</summary> /// <remarks> /// Ask the DataNode for a new shared memory segment. This function must be /// called with the manager lock held. We will release the lock while /// communicating with the DataNode. /// </remarks> /// <param name="clientName">The current client name.</param> /// <param name="peer">The peer to use to talk to the DataNode.</param> /// <returns> /// Null if the DataNode does not support shared memory /// segments, or experienced an error creating the /// shm. The shared memory segment itself on success. /// </returns> /// <exception cref="System.IO.IOException"> /// If there was an error communicating over the socket. /// We will not throw an IOException unless the socket /// itself (or the network) is the problem. /// </exception> private DfsClientShm RequestNewShm(string clientName, DomainPeer peer) { DataOutputStream @out = new DataOutputStream(new BufferedOutputStream(peer.GetOutputStream ())); new Sender(@out).RequestShortCircuitShm(clientName); DataTransferProtos.ShortCircuitShmResponseProto resp = DataTransferProtos.ShortCircuitShmResponseProto .ParseFrom(PBHelper.VintPrefixed(peer.GetInputStream())); string error = resp.HasError() ? resp.GetError() : "(unknown)"; switch (resp.GetStatus()) { case DataTransferProtos.Status.Success: { DomainSocket sock = peer.GetDomainSocket(); byte[] buf = new byte[1]; FileInputStream[] fis = new FileInputStream[1]; if (sock.RecvFileInputStreams(fis, buf, 0, buf.Length) < 0) { throw new EOFException("got EOF while trying to transfer the " + "file descriptor for the shared memory segment." ); } if (fis[0] == null) { throw new IOException("the datanode " + this.datanode + " failed to " + "pass a file descriptor for the shared memory segment." ); } try { DfsClientShm shm = new DfsClientShm(PBHelper.Convert(resp.GetId()), fis[0], this, peer); if (DfsClientShmManager.Log.IsTraceEnabled()) { DfsClientShmManager.Log.Trace(this + ": createNewShm: created " + shm); } return(shm); } finally { IOUtils.Cleanup(DfsClientShmManager.Log, fis[0]); } goto case DataTransferProtos.Status.ErrorUnsupported; } case DataTransferProtos.Status.ErrorUnsupported: { // The DataNode just does not support short-circuit shared memory // access, and we should stop asking. DfsClientShmManager.Log.Info(this + ": datanode does not support short-circuit " + "shared memory access: " + error); this.disabled = true; return(null); } default: { // The datanode experienced some kind of unexpected error when trying to // create the short-circuit shared memory segment. DfsClientShmManager.Log.Warn(this + ": error requesting short-circuit shared memory " + "access: " + error); return(null); } } }
public static void SetupCluster() { DFSInputStream.tcpReadsDisabledForTesting = true; DomainSocket.DisableBindPathValidation(); }
/// <exception cref="System.Exception"/> private void TestStatistics(bool isShortCircuit) { Assume.AssumeTrue(DomainSocket.GetLoadingFailureReason() == null); HdfsConfiguration conf = new HdfsConfiguration(); TemporarySocketDirectory sockDir = null; if (isShortCircuit) { DFSInputStream.tcpReadsDisabledForTesting = true; sockDir = new TemporarySocketDirectory(); conf.Set(DFSConfigKeys.DfsDomainSocketPathKey, new FilePath(sockDir.GetDir(), "TestStatisticsForLocalRead.%d.sock" ).GetAbsolutePath()); conf.SetBoolean(DFSConfigKeys.DfsClientReadShortcircuitKey, true); DomainSocket.DisableBindPathValidation(); } else { conf.SetBoolean(DFSConfigKeys.DfsClientReadShortcircuitKey, false); } MiniDFSCluster cluster = null; Path TestPath = new Path("/a"); long RandomSeed = 4567L; FSDataInputStream fsIn = null; byte[] original = new byte[TestBlockReaderLocal.BlockReaderLocalTest.TestLength]; FileSystem fs = null; try { cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build(); cluster.WaitActive(); fs = cluster.GetFileSystem(); DFSTestUtil.CreateFile(fs, TestPath, TestBlockReaderLocal.BlockReaderLocalTest.TestLength , (short)1, RandomSeed); try { DFSTestUtil.WaitReplication(fs, TestPath, (short)1); } catch (Exception e) { NUnit.Framework.Assert.Fail("unexpected InterruptedException during " + "waitReplication: " + e); } catch (TimeoutException e) { NUnit.Framework.Assert.Fail("unexpected TimeoutException during " + "waitReplication: " + e); } fsIn = fs.Open(TestPath); IOUtils.ReadFully(fsIn, original, 0, TestBlockReaderLocal.BlockReaderLocalTest.TestLength ); HdfsDataInputStream dfsIn = (HdfsDataInputStream)fsIn; NUnit.Framework.Assert.AreEqual(TestBlockReaderLocal.BlockReaderLocalTest.TestLength , dfsIn.GetReadStatistics().GetTotalBytesRead()); NUnit.Framework.Assert.AreEqual(TestBlockReaderLocal.BlockReaderLocalTest.TestLength , dfsIn.GetReadStatistics().GetTotalLocalBytesRead()); if (isShortCircuit) { NUnit.Framework.Assert.AreEqual(TestBlockReaderLocal.BlockReaderLocalTest.TestLength , dfsIn.GetReadStatistics().GetTotalShortCircuitBytesRead()); } else { NUnit.Framework.Assert.AreEqual(0, dfsIn.GetReadStatistics().GetTotalShortCircuitBytesRead ()); } fsIn.Close(); fsIn = null; } finally { DFSInputStream.tcpReadsDisabledForTesting = false; if (fsIn != null) { fsIn.Close(); } if (fs != null) { fs.Close(); } if (cluster != null) { cluster.Shutdown(); } if (sockDir != null) { sockDir.Close(); } } }
/// <exception cref="System.IO.IOException"/> public virtual void RunBlockReaderLocalTest(TestBlockReaderLocal.BlockReaderLocalTest test, bool checksum, long readahead) { Assume.AssumeThat(DomainSocket.GetLoadingFailureReason(), CoreMatchers.EqualTo(null )); MiniDFSCluster cluster = null; HdfsConfiguration conf = new HdfsConfiguration(); conf.SetBoolean(DFSConfigKeys.DfsClientReadShortcircuitSkipChecksumKey, !checksum ); conf.SetLong(DFSConfigKeys.DfsBytesPerChecksumKey, TestBlockReaderLocal.BlockReaderLocalTest .BytesPerChecksum); conf.Set(DFSConfigKeys.DfsChecksumTypeKey, "CRC32C"); conf.SetLong(DFSConfigKeys.DfsClientCacheReadahead, readahead); test.SetConfiguration(conf); FileInputStream dataIn = null; FileInputStream metaIn = null; Path TestPath = new Path("/a"); long RandomSeed = 4567L; BlockReaderLocal blockReaderLocal = null; FSDataInputStream fsIn = null; byte[] original = new byte[TestBlockReaderLocal.BlockReaderLocalTest.TestLength]; FileSystem fs = null; ShortCircuitShm shm = null; RandomAccessFile raf = null; try { cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build(); cluster.WaitActive(); fs = cluster.GetFileSystem(); DFSTestUtil.CreateFile(fs, TestPath, TestBlockReaderLocal.BlockReaderLocalTest.TestLength , (short)1, RandomSeed); try { DFSTestUtil.WaitReplication(fs, TestPath, (short)1); } catch (Exception e) { NUnit.Framework.Assert.Fail("unexpected InterruptedException during " + "waitReplication: " + e); } catch (TimeoutException e) { NUnit.Framework.Assert.Fail("unexpected TimeoutException during " + "waitReplication: " + e); } fsIn = fs.Open(TestPath); IOUtils.ReadFully(fsIn, original, 0, TestBlockReaderLocal.BlockReaderLocalTest.TestLength ); fsIn.Close(); fsIn = null; ExtendedBlock block = DFSTestUtil.GetFirstBlock(fs, TestPath); FilePath dataFile = cluster.GetBlockFile(0, block); FilePath metaFile = cluster.GetBlockMetadataFile(0, block); ShortCircuitCache shortCircuitCache = ClientContext.GetFromConf(conf).GetShortCircuitCache (); cluster.Shutdown(); cluster = null; test.Setup(dataFile, checksum); FileInputStream[] streams = new FileInputStream[] { new FileInputStream(dataFile) , new FileInputStream(metaFile) }; dataIn = streams[0]; metaIn = streams[1]; ExtendedBlockId key = new ExtendedBlockId(block.GetBlockId(), block.GetBlockPoolId ()); raf = new RandomAccessFile(new FilePath(sockDir.GetDir().GetAbsolutePath(), UUID. RandomUUID().ToString()), "rw"); raf.SetLength(8192); FileInputStream shmStream = new FileInputStream(raf.GetFD()); shm = new ShortCircuitShm(ShortCircuitShm.ShmId.CreateRandom(), shmStream); ShortCircuitReplica replica = new ShortCircuitReplica(key, dataIn, metaIn, shortCircuitCache , Time.Now(), shm.AllocAndRegisterSlot(ExtendedBlockId.FromExtendedBlock(block)) ); blockReaderLocal = new BlockReaderLocal.Builder(new DFSClient.Conf(conf)).SetFilename (TestPath.GetName()).SetBlock(block).SetShortCircuitReplica(replica).SetCachingStrategy (new CachingStrategy(false, readahead)).SetVerifyChecksum(checksum).Build(); dataIn = null; metaIn = null; test.DoTest(blockReaderLocal, original); // BlockReaderLocal should not alter the file position. NUnit.Framework.Assert.AreEqual(0, streams[0].GetChannel().Position()); NUnit.Framework.Assert.AreEqual(0, streams[1].GetChannel().Position()); } finally { if (fsIn != null) { fsIn.Close(); } if (fs != null) { fs.Close(); } if (cluster != null) { cluster.Shutdown(); } if (dataIn != null) { dataIn.Close(); } if (metaIn != null) { metaIn.Close(); } if (blockReaderLocal != null) { blockReaderLocal.Close(); } if (shm != null) { shm.Free(); } if (raf != null) { raf.Close(); } } }