Esempio n. 1
0
        private BlockReaderLocal(BlockReaderLocal.Builder builder)
        {
            this.replica    = builder.replica;
            this.dataIn     = replica.GetDataStream().GetChannel();
            this.dataPos    = builder.dataPos;
            this.checksumIn = replica.GetMetaStream().GetChannel();
            BlockMetadataHeader header = builder.replica.GetMetaHeader();

            this.checksum       = header.GetChecksum();
            this.verifyChecksum = builder.verifyChecksum && (this.checksum.GetChecksumType().
                                                             id != DataChecksum.ChecksumNull);
            this.filename           = builder.filename;
            this.block              = builder.block;
            this.bytesPerChecksum   = checksum.GetBytesPerChecksum();
            this.checksumSize       = checksum.GetChecksumSize();
            this.maxAllocatedChunks = (bytesPerChecksum == 0) ? 0 : ((builder.bufferSize + bytesPerChecksum
                                                                      - 1) / bytesPerChecksum);
            // Calculate the effective maximum readahead.
            // We can't do more readahead than there is space in the buffer.
            int maxReadaheadChunks = (bytesPerChecksum == 0) ? 0 : ((Math.Min(builder.bufferSize
                                                                              , builder.maxReadahead) + bytesPerChecksum - 1) / bytesPerChecksum);

            if (maxReadaheadChunks == 0)
            {
                this.zeroReadaheadRequested = true;
                maxReadaheadChunks          = 1;
            }
            else
            {
                this.zeroReadaheadRequested = false;
            }
            this.maxReadaheadLength = maxReadaheadChunks * bytesPerChecksum;
            this.storageType        = builder.storageType;
        }
            public void Visit(int numOutstandingMmaps, IDictionary <ExtendedBlockId, ShortCircuitReplica
                                                                    > replicas, IDictionary <ExtendedBlockId, SecretManager.InvalidToken> failedLoads
                              , IDictionary <long, ShortCircuitReplica> evictable, IDictionary <long, ShortCircuitReplica
                                                                                                > evictableMmapped)
            {
                ShortCircuitReplica replica = replicas[new ExtendedBlockId(firstBlock.GetBlockId(
                                                                               ), firstBlock.GetBlockPoolId())];

                NUnit.Framework.Assert.IsNotNull(replica);
                NUnit.Framework.Assert.IsTrue(replica.HasMmap());
                NUnit.Framework.Assert.IsNull(replica.GetEvictableTimeNs());
            }
                public void Visit(int numOutstandingMmaps, IDictionary <ExtendedBlockId, ShortCircuitReplica
                                                                        > replicas, IDictionary <ExtendedBlockId, SecretManager.InvalidToken> failedLoads
                                  , IDictionary <long, ShortCircuitReplica> evictable, IDictionary <long, ShortCircuitReplica
                                                                                                    > evictableMmapped)
                {
                    NUnit.Framework.Assert.AreEqual(expectedOutstandingMmaps, numOutstandingMmaps);
                    ShortCircuitReplica replica = replicas[ExtendedBlockId.FromExtendedBlock(block)];

                    NUnit.Framework.Assert.IsNotNull(replica);
                    ShortCircuitShm.Slot slot = replica.GetSlot();
                    if ((expectedIsAnchorable != slot.IsAnchorable()) || (expectedIsAnchored != slot.
                                                                          IsAnchored()))
                    {
                        TestEnhancedByteBufferAccess.Log.Info("replica " + replica + " has isAnchorable = "
                                                              + slot.IsAnchorable() + ", isAnchored = " + slot.IsAnchored() + ".  Waiting for isAnchorable = "
                                                              + expectedIsAnchorable + ", isAnchored = " + expectedIsAnchored);
                        return;
                    }
                    result.SetValue(true);
                }
Esempio n. 4
0
 public virtual BlockReaderLocal.Builder SetShortCircuitReplica(ShortCircuitReplica
                                                                replica)
 {
     this.replica = replica;
     return(this);
 }
        /// <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();
                }
            }
        }
Esempio n. 6
0
        /// <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);
            }
            }
        }