/// <exception cref="System.Exception"/> public virtual void TestShmBasedStaleness() { BlockReaderTestUtil.EnableShortCircuitShmTracing(); TemporarySocketDirectory sockDir = new TemporarySocketDirectory(); Configuration conf = CreateShortCircuitConf("testShmBasedStaleness", sockDir); MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build(); cluster.WaitActive(); DistributedFileSystem fs = cluster.GetFileSystem(); ShortCircuitCache cache = fs.GetClient().GetClientContext().GetShortCircuitCache( ); string TestFile = "/test_file"; int TestFileLen = 8193; int Seed = unchecked ((int)(0xFADED)); DFSTestUtil.CreateFile(fs, new Path(TestFile), TestFileLen, (short)1, Seed); FSDataInputStream fis = fs.Open(new Path(TestFile)); int first = fis.Read(); ExtendedBlock block = DFSTestUtil.GetFirstBlock(fs, new Path(TestFile)); NUnit.Framework.Assert.IsTrue(first != -1); cache.Accept(new _CacheVisitor_502(block)); // Stop the Namenode. This will close the socket keeping the client's // shared memory segment alive, and make it stale. cluster.GetDataNodes()[0].Shutdown(); cache.Accept(new _CacheVisitor_518(block)); cluster.Shutdown(); sockDir.Close(); }
/// <exception cref="System.Exception"/> private void WaitForReplicaAnchorStatus(ShortCircuitCache cache, ExtendedBlock block , bool expectedIsAnchorable, bool expectedIsAnchored, int expectedOutstandingMmaps ) { GenericTestUtils.WaitFor(new _Supplier_683(cache, expectedOutstandingMmaps, block , expectedIsAnchorable, expectedIsAnchored), 10, 60000); }
internal SimpleReplicaCreator(int blockId, ShortCircuitCache cache, TestShortCircuitCache.TestFileDescriptorPair pair) { this.blockId = blockId; this.cache = cache; this.pair = pair; }
/// <exception cref="System.Exception"/> public virtual void TestExpiry() { ShortCircuitCache cache = new ShortCircuitCache(2, 1, 1, 10000000, 1, 10000000, 0 ); TestShortCircuitCache.TestFileDescriptorPair pair = new TestShortCircuitCache.TestFileDescriptorPair (); ShortCircuitReplicaInfo replicaInfo1 = cache.FetchOrCreate(new ExtendedBlockId(123 , "test_bp1"), new TestShortCircuitCache.SimpleReplicaCreator(123, cache, pair)); Preconditions.CheckNotNull(replicaInfo1.GetReplica()); Preconditions.CheckState(replicaInfo1.GetInvalidTokenException() == null); pair.CompareWith(replicaInfo1.GetReplica().GetDataStream(), replicaInfo1.GetReplica ().GetMetaStream()); replicaInfo1.GetReplica().Unref(); MutableBoolean triedToCreate = new MutableBoolean(false); do { Sharpen.Thread.Sleep(10); ShortCircuitReplicaInfo replicaInfo2 = cache.FetchOrCreate(new ExtendedBlockId(123 , "test_bp1"), new _ShortCircuitReplicaCreator_229(triedToCreate)); if ((replicaInfo2 != null) && (replicaInfo2.GetReplica() != null)) { replicaInfo2.GetReplica().Unref(); } }while (triedToCreate.IsFalse()); cache.Close(); }
public virtual void TestShortCircuitCacheShutdown() { TemporarySocketDirectory sockDir = new TemporarySocketDirectory(); Configuration conf = CreateShortCircuitConf("testShortCircuitCacheShutdown", sockDir ); conf.Set(DFSConfigKeys.DfsClientContext, "testShortCircuitCacheShutdown"); Configuration serverConf = new Configuration(conf); DFSInputStream.tcpReadsDisabledForTesting = true; MiniDFSCluster cluster = new MiniDFSCluster.Builder(serverConf).NumDataNodes(1).Build (); cluster.WaitActive(); DistributedFileSystem fs = (DistributedFileSystem)FileSystem.Get(cluster.GetURI(0 ), conf); string TestFile = "/test_file"; int TestFileLen = 4000; int Seed = unchecked ((int)(0xFADEC)); DFSTestUtil.CreateFile(fs, new Path(TestFile), TestFileLen, (short)1, Seed); byte[] contents = DFSTestUtil.ReadFileBuffer(fs, new Path(TestFile)); byte[] expected = DFSTestUtil.CalculateFileContentsFromSeed(Seed, TestFileLen); NUnit.Framework.Assert.IsTrue(Arrays.Equals(contents, expected)); ShortCircuitCache cache = fs.dfs.GetClientContext().GetShortCircuitCache(); cache.Close(); NUnit.Framework.Assert.IsTrue(cache.GetDfsClientShmManager().GetDomainSocketWatcher ().IsClosed()); cluster.Shutdown(); sockDir.Close(); }
public _Supplier_593(TestShortCircuitCache _enclosing, ShortCircuitCache cache, DatanodeInfo datanode) { this._enclosing = _enclosing; this.cache = cache; this.datanode = datanode; this.done = new MutableBoolean(true); }
public _ShortCircuitReplicaCreator_330(ExtendedBlockId key, TestShortCircuitCache.TestFileDescriptorPair [] pairs, int iVal, ShortCircuitCache cache, long HourInMs) { this.key = key; this.pairs = pairs; this.iVal = iVal; this.cache = cache; this.HourInMs = HourInMs; }
public _Supplier_683(ShortCircuitCache cache, int expectedOutstandingMmaps, ExtendedBlock block, bool expectedIsAnchorable, bool expectedIsAnchored) { this.cache = cache; this.expectedOutstandingMmaps = expectedOutstandingMmaps; this.block = block; this.expectedIsAnchorable = expectedIsAnchorable; this.expectedIsAnchored = expectedIsAnchored; }
/// <exception cref="System.Exception"/> public virtual void TestEviction() { ShortCircuitCache cache = new ShortCircuitCache(2, 10000000, 1, 10000000, 1, 10000 , 0); TestShortCircuitCache.TestFileDescriptorPair[] pairs = new TestShortCircuitCache.TestFileDescriptorPair [] { new TestShortCircuitCache.TestFileDescriptorPair(), new TestShortCircuitCache.TestFileDescriptorPair (), new TestShortCircuitCache.TestFileDescriptorPair() }; ShortCircuitReplicaInfo[] replicaInfos = new ShortCircuitReplicaInfo[] { null, null , null }; for (int i = 0; i < pairs.Length; i++) { replicaInfos[i] = cache.FetchOrCreate(new ExtendedBlockId(i, "test_bp1"), new TestShortCircuitCache.SimpleReplicaCreator (i, cache, pairs[i])); Preconditions.CheckNotNull(replicaInfos[i].GetReplica()); Preconditions.CheckState(replicaInfos[i].GetInvalidTokenException() == null); pairs[i].CompareWith(replicaInfos[i].GetReplica().GetDataStream(), replicaInfos[i ].GetReplica().GetMetaStream()); } // At this point, we have 3 replicas in use. // Let's close them all. for (int i_1 = 0; i_1 < pairs.Length; i_1++) { replicaInfos[i_1].GetReplica().Unref(); } // The last two replicas should still be cached. for (int i_2 = 1; i_2 < pairs.Length; i_2++) { int iVal = i_2; replicaInfos[i_2] = cache.FetchOrCreate(new ExtendedBlockId(i_2, "test_bp1"), new _ShortCircuitReplicaCreator_277(iVal)); Preconditions.CheckNotNull(replicaInfos[i_2].GetReplica()); Preconditions.CheckState(replicaInfos[i_2].GetInvalidTokenException() == null); pairs[i_2].CompareWith(replicaInfos[i_2].GetReplica().GetDataStream(), replicaInfos [i_2].GetReplica().GetMetaStream()); } // The first (oldest) replica should not be cached. MutableBoolean calledCreate = new MutableBoolean(false); replicaInfos[0] = cache.FetchOrCreate(new ExtendedBlockId(0, "test_bp1"), new _ShortCircuitReplicaCreator_293 (calledCreate)); Preconditions.CheckState(replicaInfos[0].GetReplica() == null); NUnit.Framework.Assert.IsTrue(calledCreate.IsTrue()); // Clean up for (int i_3 = 1; i_3 < pairs.Length; i_3++) { replicaInfos[i_3].GetReplica().Unref(); } for (int i_4 = 0; i_4 < pairs.Length; i_4++) { pairs[i_4].Close(); } cache.Close(); }
private ClientContext(string name, DFSClient.Conf conf) { this.name = name; this.confString = ConfAsString(conf); this.shortCircuitCache = new ShortCircuitCache(conf.shortCircuitStreamsCacheSize, conf.shortCircuitStreamsCacheExpiryMs, conf.shortCircuitMmapCacheSize, conf.shortCircuitMmapCacheExpiryMs , conf.shortCircuitMmapCacheRetryTimeout, conf.shortCircuitCacheStaleThresholdMs , conf.shortCircuitSharedMemoryWatcherInterruptCheckMs); this.peerCache = new PeerCache(conf.socketCacheCapacity, conf.socketCacheExpiry); this.keyProviderCache = new KeyProviderCache(conf.keyProviderCacheExpiryMs); this.useLegacyBlockReaderLocal = conf.useLegacyBlockReaderLocal; this.domainSocketFactory = new DomainSocketFactory(conf); this.byteArrayManager = ByteArrayManager.NewInstance(conf.writeByteArrayManagerConf ); }
/// <exception cref="Org.Apache.Hadoop.Security.Token.SecretManager.InvalidToken"/> private BlockReader GetBlockReaderLocal() { if (Log.IsTraceEnabled()) { Log.Trace(this + ": trying to construct a BlockReaderLocal " + "for short-circuit reads." ); } if (pathInfo == null) { pathInfo = clientContext.GetDomainSocketFactory().GetPathInfo(inetSocketAddress, conf); } if (!pathInfo.GetPathState().GetUsableForShortCircuit()) { PerformanceAdvisory.Log.Debug(this + ": " + pathInfo + " is not " + "usable for short circuit; giving up on BlockReaderLocal." ); return(null); } ShortCircuitCache cache = clientContext.GetShortCircuitCache(); ExtendedBlockId key = new ExtendedBlockId(block.GetBlockId(), block.GetBlockPoolId ()); ShortCircuitReplicaInfo info = cache.FetchOrCreate(key, this); SecretManager.InvalidToken exc = info.GetInvalidTokenException(); if (exc != null) { if (Log.IsTraceEnabled()) { Log.Trace(this + ": got InvalidToken exception while trying to " + "construct BlockReaderLocal via " + pathInfo.GetPath()); } throw exc; } if (info.GetReplica() == null) { if (Log.IsTraceEnabled()) { PerformanceAdvisory.Log.Debug(this + ": failed to get " + "ShortCircuitReplica. Cannot construct " + "BlockReaderLocal via " + pathInfo.GetPath()); } return(null); } return(new BlockReaderLocal.Builder(conf).SetFilename(fileName).SetBlock(block).SetStartOffset (startOffset).SetShortCircuitReplica(info.GetReplica()).SetVerifyChecksum(verifyChecksum ).SetCachingStrategy(cachingStrategy).SetStorageType(storageType).Build()); }
/// <exception cref="System.IO.IOException"/> public ShortCircuitReplica(ExtendedBlockId key, FileInputStream dataStream, FileInputStream metaStream, ShortCircuitCache cache, long creationTimeMs, ShortCircuitShm.Slot slot) { this.key = key; this.dataStream = dataStream; this.metaStream = metaStream; this.metaHeader = BlockMetadataHeader.PreadHeader(metaStream.GetChannel()); if (metaHeader.GetVersion() != 1) { throw new IOException("invalid metadata header version " + metaHeader.GetVersion( ) + ". Can only handle version 1."); } this.cache = cache; this.creationTimeMs = creationTimeMs; this.slot = slot; }
/// <summary>Test unlinking a file whose blocks we are caching in the DFSClient.</summary> /// <remarks> /// Test unlinking a file whose blocks we are caching in the DFSClient. /// The DataNode will notify the DFSClient that the replica is stale via the /// ShortCircuitShm. /// </remarks> /// <exception cref="System.Exception"/> public virtual void TestUnlinkingReplicasInFileDescriptorCache() { BlockReaderTestUtil.EnableShortCircuitShmTracing(); TemporarySocketDirectory sockDir = new TemporarySocketDirectory(); Configuration conf = CreateShortCircuitConf("testUnlinkingReplicasInFileDescriptorCache" , sockDir); // We don't want the CacheCleaner to time out short-circuit shared memory // segments during the test, so set the timeout really high. conf.SetLong(DFSConfigKeys.DfsClientReadShortcircuitStreamsCacheExpiryMsKey, 1000000000L ); MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build(); cluster.WaitActive(); DistributedFileSystem fs = cluster.GetFileSystem(); ShortCircuitCache cache = fs.GetClient().GetClientContext().GetShortCircuitCache( ); cache.GetDfsClientShmManager().Visit(new _Visitor_556()); // The ClientShmManager starts off empty. Path TestPath = new Path("/test_file"); int TestFileLen = 8193; int Seed = unchecked ((int)(0xFADE0)); DFSTestUtil.CreateFile(fs, TestPath, TestFileLen, (short)1, Seed); byte[] contents = DFSTestUtil.ReadFileBuffer(fs, TestPath); byte[] expected = DFSTestUtil.CalculateFileContentsFromSeed(Seed, TestFileLen); NUnit.Framework.Assert.IsTrue(Arrays.Equals(contents, expected)); // Loading this file brought the ShortCircuitReplica into our local // replica cache. DatanodeInfo datanode = new DatanodeInfo(cluster.GetDataNodes()[0].GetDatanodeId( )); cache.GetDfsClientShmManager().Visit(new _Visitor_577(datanode)); // Remove the file whose blocks we just read. fs.Delete(TestPath, false); // Wait for the replica to be purged from the DFSClient's cache. GenericTestUtils.WaitFor(new _Supplier_593(this, cache, datanode), 10, 60000); // Check that all slots have been invalidated. cluster.Shutdown(); sockDir.Close(); }
/// <exception cref="System.Exception"/> public virtual void TestTimeBasedStaleness() { // Set up the cache with a short staleness time. ShortCircuitCache cache = new ShortCircuitCache(2, 10000000, 1, 10000000, 1, 10, 0); TestShortCircuitCache.TestFileDescriptorPair[] pairs = new TestShortCircuitCache.TestFileDescriptorPair [] { new TestShortCircuitCache.TestFileDescriptorPair(), new TestShortCircuitCache.TestFileDescriptorPair () }; ShortCircuitReplicaInfo[] replicaInfos = new ShortCircuitReplicaInfo[] { null, null }; long HourInMs = 60 * 60 * 1000; for (int i = 0; i < pairs.Length; i++) { int iVal = i; ExtendedBlockId key = new ExtendedBlockId(i, "test_bp1"); replicaInfos[i] = cache.FetchOrCreate(key, new _ShortCircuitReplicaCreator_330(key , pairs, iVal, cache, HourInMs)); Preconditions.CheckNotNull(replicaInfos[i].GetReplica()); Preconditions.CheckState(replicaInfos[i].GetInvalidTokenException() == null); pairs[i].CompareWith(replicaInfos[i].GetReplica().GetDataStream(), replicaInfos[i ].GetReplica().GetMetaStream()); } // Keep trying to getOrCreate block 0 until it goes stale (and we must re-create.) GenericTestUtils.WaitFor(new _Supplier_351(cache), 500, 60000); // Make sure that second replica did not go stale. ShortCircuitReplicaInfo info = cache.FetchOrCreate(new ExtendedBlockId(1, "test_bp1" ), new _ShortCircuitReplicaCreator_371()); info.GetReplica().Unref(); // Clean up for (int i_1 = 1; i_1 < pairs.Length; i_1++) { replicaInfos[i_1].GetReplica().Unref(); } cache.Close(); }
/// <exception cref="System.Exception"/> public virtual void TestAllocShm() { BlockReaderTestUtil.EnableShortCircuitShmTracing(); TemporarySocketDirectory sockDir = new TemporarySocketDirectory(); Configuration conf = CreateShortCircuitConf("testAllocShm", sockDir); MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build(); cluster.WaitActive(); DistributedFileSystem fs = cluster.GetFileSystem(); ShortCircuitCache cache = fs.GetClient().GetClientContext().GetShortCircuitCache( ); cache.GetDfsClientShmManager().Visit(new _Visitor_423()); // The ClientShmManager starts off empty DomainPeer peer = GetDomainPeerToDn(conf); MutableBoolean usedPeer = new MutableBoolean(false); ExtendedBlockId blockId = new ExtendedBlockId(123, "xyz"); DatanodeInfo datanode = new DatanodeInfo(cluster.GetDataNodes()[0].GetDatanodeId( )); // Allocating the first shm slot requires using up a peer. ShortCircuitShm.Slot slot = cache.AllocShmSlot(datanode, peer, usedPeer, blockId, "testAllocShm_client"); NUnit.Framework.Assert.IsNotNull(slot); NUnit.Framework.Assert.IsTrue(usedPeer.BooleanValue()); cache.GetDfsClientShmManager().Visit(new _Visitor_441(datanode)); // The ClientShmManager starts off empty cache.ScheduleSlotReleaser(slot); // Wait for the slot to be released, and the shared memory area to be // closed. Since we didn't register this shared memory segment on the // server, it will also be a test of how well the server deals with // bogus client behavior. GenericTestUtils.WaitFor(new _Supplier_458(cache, datanode), 10, 60000); cluster.Shutdown(); sockDir.Close(); }
/// <exception cref="System.Exception"/> public virtual void TestAddAndRetrieve() { ShortCircuitCache cache = new ShortCircuitCache(10, 10000000, 10, 10000000, 1, 10000 , 0); TestShortCircuitCache.TestFileDescriptorPair pair = new TestShortCircuitCache.TestFileDescriptorPair (); ShortCircuitReplicaInfo replicaInfo1 = cache.FetchOrCreate(new ExtendedBlockId(123 , "test_bp1"), new TestShortCircuitCache.SimpleReplicaCreator(123, cache, pair)); Preconditions.CheckNotNull(replicaInfo1.GetReplica()); Preconditions.CheckState(replicaInfo1.GetInvalidTokenException() == null); pair.CompareWith(replicaInfo1.GetReplica().GetDataStream(), replicaInfo1.GetReplica ().GetMetaStream()); ShortCircuitReplicaInfo replicaInfo2 = cache.FetchOrCreate(new ExtendedBlockId(123 , "test_bp1"), new _ShortCircuitReplicaCreator_175()); Preconditions.CheckNotNull(replicaInfo2.GetReplica()); Preconditions.CheckState(replicaInfo2.GetInvalidTokenException() == null); Preconditions.CheckState(replicaInfo1 == replicaInfo2); pair.CompareWith(replicaInfo2.GetReplica().GetDataStream(), replicaInfo2.GetReplica ().GetMetaStream()); replicaInfo1.GetReplica().Unref(); replicaInfo2.GetReplica().Unref(); // Even after the reference count falls to 0, we still keep the replica // around for a while (we have configured the expiry period to be really, // really long here) ShortCircuitReplicaInfo replicaInfo3 = cache.FetchOrCreate(new ExtendedBlockId(123 , "test_bp1"), new _ShortCircuitReplicaCreator_195()); Preconditions.CheckNotNull(replicaInfo3.GetReplica()); Preconditions.CheckState(replicaInfo3.GetInvalidTokenException() == null); replicaInfo3.GetReplica().Unref(); pair.Close(); cache.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(); } } }
/// <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 TestZeroCopyMmapCache() { HdfsConfiguration conf = InitZeroCopyTest(); MiniDFSCluster cluster = null; Path TestPath = new Path("/a"); int TestFileLength = 5 * BlockSize; int RandomSeed = 23453; string Context = "testZeroCopyMmapCacheContext"; FSDataInputStream fsIn = null; ByteBuffer[] results = new ByteBuffer[] { null, null, null, null }; DistributedFileSystem fs = null; conf.Set(DFSConfigKeys.DfsClientContext, Context); cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build(); cluster.WaitActive(); fs = cluster.GetFileSystem(); DFSTestUtil.CreateFile(fs, TestPath, TestFileLength, (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); byte[] original = new byte[TestFileLength]; IOUtils.ReadFully(fsIn, original, 0, TestFileLength); fsIn.Close(); fsIn = fs.Open(TestPath); ShortCircuitCache cache = ClientContext.Get(Context, new DFSClient.Conf(conf)).GetShortCircuitCache (); cache.Accept(new TestEnhancedByteBufferAccess.CountingVisitor(0, 5, 5, 0)); results[0] = fsIn.Read(null, BlockSize, EnumSet.Of(ReadOption.SkipChecksums)); fsIn.Seek(0); results[1] = fsIn.Read(null, BlockSize, EnumSet.Of(ReadOption.SkipChecksums)); // The mmap should be of the first block of the file. ExtendedBlock firstBlock = DFSTestUtil.GetFirstBlock(fs, TestPath); cache.Accept(new _CacheVisitor_373(firstBlock)); // The replica should not yet be evictable, since we have it open. // Read more blocks. results[2] = fsIn.Read(null, BlockSize, EnumSet.Of(ReadOption.SkipChecksums)); results[3] = fsIn.Read(null, BlockSize, EnumSet.Of(ReadOption.SkipChecksums)); // we should have 3 mmaps, 1 evictable cache.Accept(new TestEnhancedByteBufferAccess.CountingVisitor(3, 5, 2, 0)); // After we close the cursors, the mmaps should be evictable for // a brief period of time. Then, they should be closed (we're // using a very quick timeout) foreach (ByteBuffer buffer in results) { if (buffer != null) { fsIn.ReleaseBuffer(buffer); } } fsIn.Close(); GenericTestUtils.WaitFor(new _Supplier_407(cache), 10, 60000); cache.Accept(new TestEnhancedByteBufferAccess.CountingVisitor(0, -1, -1, -1)); fs.Close(); cluster.Shutdown(); }
/// <summary> /// Test that we can zero-copy read cached data even without disabling /// checksums. /// </summary> /// <exception cref="System.Exception"/> public virtual void TestZeroCopyReadOfCachedData() { BlockReaderTestUtil.EnableShortCircuitShmTracing(); BlockReaderTestUtil.EnableBlockReaderFactoryTracing(); BlockReaderTestUtil.EnableHdfsCachingTracing(); int TestFileLength = BlockSize; Path TestPath = new Path("/a"); int RandomSeed = 23453; HdfsConfiguration conf = InitZeroCopyTest(); conf.SetBoolean(DFSConfigKeys.DfsClientReadShortcircuitSkipChecksumKey, false); string Context = "testZeroCopyReadOfCachedData"; conf.Set(DFSConfigKeys.DfsClientContext, Context); conf.SetLong(DFSConfigKeys.DfsDatanodeMaxLockedMemoryKey, DFSTestUtil.RoundUpToMultiple (TestFileLength, (int)NativeIO.POSIX.GetCacheManipulator().GetOperatingSystemPageSize ())); MiniDFSCluster cluster = null; ByteBuffer result = null; ByteBuffer result2 = null; cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build(); cluster.WaitActive(); FsDatasetSpi <object> fsd = cluster.GetDataNodes()[0].GetFSDataset(); DistributedFileSystem fs = cluster.GetFileSystem(); DFSTestUtil.CreateFile(fs, TestPath, TestFileLength, (short)1, RandomSeed); DFSTestUtil.WaitReplication(fs, TestPath, (short)1); byte[] original = DFSTestUtil.CalculateFileContentsFromSeed(RandomSeed, TestFileLength ); // Prior to caching, the file can't be read via zero-copy FSDataInputStream fsIn = fs.Open(TestPath); try { result = fsIn.Read(null, TestFileLength / 2, EnumSet.NoneOf <ReadOption>()); NUnit.Framework.Assert.Fail("expected UnsupportedOperationException"); } catch (NotSupportedException) { } // expected // Cache the file fs.AddCachePool(new CachePoolInfo("pool1")); long directiveId = fs.AddCacheDirective(new CacheDirectiveInfo.Builder().SetPath( TestPath).SetReplication((short)1).SetPool("pool1").Build()); int numBlocks = (int)Math.Ceil((double)TestFileLength / BlockSize); DFSTestUtil.VerifyExpectedCacheUsage(DFSTestUtil.RoundUpToMultiple(TestFileLength , BlockSize), numBlocks, cluster.GetDataNodes()[0].GetFSDataset()); try { result = fsIn.Read(null, TestFileLength, EnumSet.NoneOf <ReadOption>()); } catch (NotSupportedException) { NUnit.Framework.Assert.Fail("expected to be able to read cached file via zero-copy" ); } Assert.AssertArrayEquals(Arrays.CopyOfRange(original, 0, BlockSize), ByteBufferToArray (result)); // Test that files opened after the cache operation has finished // still get the benefits of zero-copy (regression test for HDFS-6086) FSDataInputStream fsIn2 = fs.Open(TestPath); try { result2 = fsIn2.Read(null, TestFileLength, EnumSet.NoneOf <ReadOption>()); } catch (NotSupportedException) { NUnit.Framework.Assert.Fail("expected to be able to read cached file via zero-copy" ); } Assert.AssertArrayEquals(Arrays.CopyOfRange(original, 0, BlockSize), ByteBufferToArray (result2)); fsIn2.ReleaseBuffer(result2); fsIn2.Close(); // check that the replica is anchored ExtendedBlock firstBlock = DFSTestUtil.GetFirstBlock(fs, TestPath); ShortCircuitCache cache = ClientContext.Get(Context, new DFSClient.Conf(conf)).GetShortCircuitCache (); WaitForReplicaAnchorStatus(cache, firstBlock, true, true, 1); // Uncache the replica fs.RemoveCacheDirective(directiveId); WaitForReplicaAnchorStatus(cache, firstBlock, false, true, 1); fsIn.ReleaseBuffer(result); WaitForReplicaAnchorStatus(cache, firstBlock, false, false, 1); DFSTestUtil.VerifyExpectedCacheUsage(0, 0, fsd); fsIn.Close(); fs.Close(); cluster.Shutdown(); }
public _Supplier_458(ShortCircuitCache cache, DatanodeInfo datanode) { this.cache = cache; this.datanode = datanode; }
public _Supplier_351(ShortCircuitCache cache) { this.cache = cache; }
/// <exception cref="System.Exception"/> public virtual void TestCreateAndDestroy() { ShortCircuitCache cache = new ShortCircuitCache(10, 1, 10, 1, 1, 10000, 0); cache.Close(); }
/// <summary>Fetch a pair of short-circuit block descriptors from a local DataNode.</summary> /// <returns> /// Null if we could not communicate with the datanode, /// a new ShortCircuitReplicaInfo object otherwise. /// ShortCircuitReplicaInfo objects may contain either an InvalidToken /// exception, or a ShortCircuitReplica object ready to use. /// </returns> public virtual ShortCircuitReplicaInfo CreateShortCircuitReplicaInfo() { if (createShortCircuitReplicaInfoCallback != null) { ShortCircuitReplicaInfo info = createShortCircuitReplicaInfoCallback.CreateShortCircuitReplicaInfo (); if (info != null) { return(info); } } if (Log.IsTraceEnabled()) { Log.Trace(this + ": trying to create ShortCircuitReplicaInfo."); } BlockReaderFactory.BlockReaderPeer curPeer; while (true) { curPeer = NextDomainPeer(); if (curPeer == null) { break; } if (curPeer.fromCache) { remainingCacheTries--; } DomainPeer peer = (DomainPeer)curPeer.peer; ShortCircuitShm.Slot slot = null; ShortCircuitCache cache = clientContext.GetShortCircuitCache(); try { MutableBoolean usedPeer = new MutableBoolean(false); slot = cache.AllocShmSlot(datanode, peer, usedPeer, new ExtendedBlockId(block.GetBlockId (), block.GetBlockPoolId()), clientName); if (usedPeer.BooleanValue()) { if (Log.IsTraceEnabled()) { Log.Trace(this + ": allocShmSlot used up our previous socket " + peer.GetDomainSocket () + ". Allocating a new one..."); } curPeer = NextDomainPeer(); if (curPeer == null) { break; } peer = (DomainPeer)curPeer.peer; } ShortCircuitReplicaInfo info = RequestFileDescriptors(peer, slot); clientContext.GetPeerCache().Put(datanode, peer); return(info); } catch (IOException e) { if (slot != null) { cache.FreeSlot(slot); } if (curPeer.fromCache) { // Handle an I/O error we got when using a cached socket. // These are considered less serious, because the socket may be stale. if (Log.IsDebugEnabled()) { Log.Debug(this + ": closing stale domain peer " + peer, e); } IOUtils.Cleanup(Log, peer); } else { // Handle an I/O error we got when using a newly created socket. // We temporarily disable the domain socket path for a few minutes in // this case, to prevent wasting more time on it. Log.Warn(this + ": I/O error requesting file descriptors. " + "Disabling domain socket " + peer.GetDomainSocket(), e); IOUtils.Cleanup(Log, peer); clientContext.GetDomainSocketFactory().DisableDomainSocketPath(pathInfo.GetPath() ); return(null); } } } return(null); }