/// <summary>Read SASL message and negotiated cipher option from server.</summary>
 /// <param name="in">stream to read</param>
 /// <returns>
 /// SaslResponseWithNegotiatedCipherOption SASL message and
 /// negotiated cipher option
 /// </returns>
 /// <exception cref="System.IO.IOException">for any error</exception>
 public static SaslResponseWithNegotiatedCipherOption ReadSaslMessageAndNegotiatedCipherOption
     (InputStream @in)
 {
     DataTransferProtos.DataTransferEncryptorMessageProto proto = DataTransferProtos.DataTransferEncryptorMessageProto
                                                                  .ParseFrom(PBHelper.VintPrefixed(@in));
     if (proto.GetStatus() == DataTransferProtos.DataTransferEncryptorMessageProto.DataTransferEncryptorStatus
         .ErrorUnknownKey)
     {
         throw new InvalidEncryptionKeyException(proto.GetMessage());
     }
     else
     {
         if (proto.GetStatus() == DataTransferProtos.DataTransferEncryptorMessageProto.DataTransferEncryptorStatus
             .Error)
         {
             throw new IOException(proto.GetMessage());
         }
         else
         {
             byte[] response = proto.GetPayload().ToByteArray();
             IList <CipherOption> options = PBHelper.ConvertCipherOptionProtos(proto.GetCipherOptionList
                                                                                   ());
             CipherOption option = null;
             if (options != null && !options.IsEmpty())
             {
                 option = options[0];
             }
             return(new SaslResponseWithNegotiatedCipherOption(response, option));
         }
     }
 }
Exemple #2
0
        /// <summary>Receive OP_WRITE_BLOCK</summary>
        /// <exception cref="System.IO.IOException"/>
        private void OpWriteBlock(DataInputStream @in)
        {
            DataTransferProtos.OpWriteBlockProto proto = DataTransferProtos.OpWriteBlockProto
                                                         .ParseFrom(PBHelper.VintPrefixed(@in));
            DatanodeInfo[] targets    = PBHelper.Convert(proto.GetTargetsList());
            TraceScope     traceScope = DataTransferProtoUtil.ContinueTraceSpan(proto.GetHeader()
                                                                                , proto.GetType().Name);

            try
            {
                WriteBlock(PBHelper.Convert(proto.GetHeader().GetBaseHeader().GetBlock()), PBHelper
                           .ConvertStorageType(proto.GetStorageType()), PBHelper.Convert(proto.GetHeader().
                                                                                         GetBaseHeader().GetToken()), proto.GetHeader().GetClientName(), targets, PBHelper
                           .ConvertStorageTypes(proto.GetTargetStorageTypesList(), targets.Length), PBHelper
                           .Convert(proto.GetSource()), DataTransferProtoUtil.FromProto(proto.GetStage()),
                           proto.GetPipelineSize(), proto.GetMinBytesRcvd(), proto.GetMaxBytesRcvd(), proto
                           .GetLatestGenerationStamp(), DataTransferProtoUtil.FromProto(proto.GetRequestedChecksum
                                                                                            ()), (proto.HasCachingStrategy() ? GetCachingStrategy(proto.GetCachingStrategy()
                                                                                                                                                  ) : CachingStrategy.NewDefaultStrategy()), (proto.HasAllowLazyPersist() ? proto.
                                                                                                                                                                                              GetAllowLazyPersist() : false), (proto.HasPinning() ? proto.GetPinning() : false
                                                                                                                                                                                                                               ), (PBHelper.ConvertBooleanList(proto.GetTargetPinningsList())));
            }
            finally
            {
                if (traceScope != null)
                {
                    traceScope.Close();
                }
            }
        }
 /// <summary>Reads a SASL negotiation message and negotiation cipher options.</summary>
 /// <param name="in">stream to read</param>
 /// <param name="cipherOptions">list to store negotiation cipher options</param>
 /// <returns>byte[] SASL negotiation message</returns>
 /// <exception cref="System.IO.IOException">for any error</exception>
 public static byte[] ReadSaslMessageAndNegotiationCipherOptions(InputStream @in,
                                                                 IList <CipherOption> cipherOptions)
 {
     DataTransferProtos.DataTransferEncryptorMessageProto proto = DataTransferProtos.DataTransferEncryptorMessageProto
                                                                  .ParseFrom(PBHelper.VintPrefixed(@in));
     if (proto.GetStatus() == DataTransferProtos.DataTransferEncryptorMessageProto.DataTransferEncryptorStatus
         .ErrorUnknownKey)
     {
         throw new InvalidEncryptionKeyException(proto.GetMessage());
     }
     else
     {
         if (proto.GetStatus() == DataTransferProtos.DataTransferEncryptorMessageProto.DataTransferEncryptorStatus
             .Error)
         {
             throw new IOException(proto.GetMessage());
         }
         else
         {
             IList <HdfsProtos.CipherOptionProto> optionProtos = proto.GetCipherOptionList();
             if (optionProtos != null)
             {
                 foreach (HdfsProtos.CipherOptionProto optionProto in optionProtos)
                 {
                     cipherOptions.AddItem(PBHelper.Convert(optionProto));
                 }
             }
             return(proto.GetPayload().ToByteArray());
         }
     }
 }
Exemple #4
0
        /// <summary>
        /// Receive
        /// <see cref="OP.RequestShortCircuitShm"/>
        ///
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        private void OpRequestShortCircuitShm(DataInputStream @in)
        {
            DataTransferProtos.ShortCircuitShmRequestProto proto = DataTransferProtos.ShortCircuitShmRequestProto
                                                                   .ParseFrom(PBHelper.VintPrefixed(@in));
            TraceScope traceScope = DataTransferProtoUtil.ContinueTraceSpan(proto.GetTraceInfo
                                                                                (), proto.GetType().Name);

            try
            {
                RequestShortCircuitShm(proto.GetClientName());
            }
            finally
            {
                if (traceScope != null)
                {
                    traceScope.Close();
                }
            }
        }
Exemple #5
0
        /// <summary>Receive OP_BLOCK_CHECKSUM</summary>
        /// <exception cref="System.IO.IOException"/>
        private void OpBlockChecksum(DataInputStream @in)
        {
            DataTransferProtos.OpBlockChecksumProto proto = DataTransferProtos.OpBlockChecksumProto
                                                            .ParseFrom(PBHelper.VintPrefixed(@in));
            TraceScope traceScope = DataTransferProtoUtil.ContinueTraceSpan(proto.GetHeader()
                                                                            , proto.GetType().Name);

            try
            {
                BlockChecksum(PBHelper.Convert(proto.GetHeader().GetBlock()), PBHelper.Convert(proto
                                                                                               .GetHeader().GetToken()));
            }
            finally
            {
                if (traceScope != null)
                {
                    traceScope.Close();
                }
            }
        }
        /// <summary>Create a new BlockReader specifically to satisfy a read.</summary>
        /// <remarks>
        /// Create a new BlockReader specifically to satisfy a read.
        /// This method also sends the OP_READ_BLOCK request.
        /// </remarks>
        /// <param name="file">File location</param>
        /// <param name="block">The block object</param>
        /// <param name="blockToken">The block token for security</param>
        /// <param name="startOffset">The read offset, relative to block head</param>
        /// <param name="len">The number of bytes to read</param>
        /// <param name="bufferSize">The IO buffer size (not the client buffer size)</param>
        /// <param name="verifyChecksum">Whether to verify checksum</param>
        /// <param name="clientName">Client name</param>
        /// <returns>New BlockReader instance, or null on error.</returns>
        /// <exception cref="System.IO.IOException"/>
        public static Org.Apache.Hadoop.Hdfs.RemoteBlockReader NewBlockReader(string file
                                                                              , ExtendedBlock block, Org.Apache.Hadoop.Security.Token.Token <BlockTokenIdentifier
                                                                                                                                             > blockToken, long startOffset, long len, int bufferSize, bool verifyChecksum, string
                                                                              clientName, Peer peer, DatanodeID datanodeID, PeerCache peerCache, CachingStrategy
                                                                              cachingStrategy)
        {
            // in and out will be closed when sock is closed (by the caller)
            DataOutputStream @out = new DataOutputStream(new BufferedOutputStream(peer.GetOutputStream
                                                                                      ()));

            new Sender(@out).ReadBlock(block, blockToken, clientName, startOffset, len, verifyChecksum
                                       , cachingStrategy);
            //
            // Get bytes in block, set streams
            //
            DataInputStream @in = new DataInputStream(new BufferedInputStream(peer.GetInputStream
                                                                                  (), bufferSize));

            DataTransferProtos.BlockOpResponseProto status = DataTransferProtos.BlockOpResponseProto
                                                             .ParseFrom(PBHelper.VintPrefixed(@in));
            RemoteBlockReader2.CheckSuccess(status, peer, block, file);
            DataTransferProtos.ReadOpChecksumInfoProto checksumInfo = status.GetReadOpChecksumInfo
                                                                          ();
            DataChecksum checksum = DataTransferProtoUtil.FromProto(checksumInfo.GetChecksum(
                                                                        ));
            //Warning when we get CHECKSUM_NULL?
            // Read the first chunk offset.
            long firstChunkOffset = checksumInfo.GetChunkOffset();

            if (firstChunkOffset < 0 || firstChunkOffset > startOffset || firstChunkOffset <=
                (startOffset - checksum.GetBytesPerChecksum()))
            {
                throw new IOException("BlockReader: error in first chunk offset (" + firstChunkOffset
                                      + ") startOffset is " + startOffset + " for file " + file);
            }
            return(new Org.Apache.Hadoop.Hdfs.RemoteBlockReader(file, block.GetBlockPoolId(),
                                                                block.GetBlockId(), @in, checksum, verifyChecksum, startOffset, firstChunkOffset
                                                                , len, peer, datanodeID, peerCache));
        }
 /// <summary>Reads a SASL negotiation message.</summary>
 /// <param name="in">stream to read</param>
 /// <returns>bytes of SASL negotiation messsage</returns>
 /// <exception cref="System.IO.IOException">for any error</exception>
 public static byte[] ReadSaslMessage(InputStream @in)
 {
     DataTransferProtos.DataTransferEncryptorMessageProto proto = DataTransferProtos.DataTransferEncryptorMessageProto
                                                                  .ParseFrom(PBHelper.VintPrefixed(@in));
     if (proto.GetStatus() == DataTransferProtos.DataTransferEncryptorMessageProto.DataTransferEncryptorStatus
         .ErrorUnknownKey)
     {
         throw new InvalidEncryptionKeyException(proto.GetMessage());
     }
     else
     {
         if (proto.GetStatus() == DataTransferProtos.DataTransferEncryptorMessageProto.DataTransferEncryptorStatus
             .Error)
         {
             throw new IOException(proto.GetMessage());
         }
         else
         {
             return(proto.GetPayload().ToByteArray());
         }
     }
 }
Exemple #8
0
        /// <summary>
        /// Receive
        /// <see cref="OP.RequestShortCircuitFds"/>
        ///
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        private void OpRequestShortCircuitFds(DataInputStream @in)
        {
            DataTransferProtos.OpRequestShortCircuitAccessProto proto = DataTransferProtos.OpRequestShortCircuitAccessProto
                                                                        .ParseFrom(PBHelper.VintPrefixed(@in));
            ShortCircuitShm.SlotId slotId = (proto.HasSlotId()) ? PBHelper.Convert(proto.GetSlotId
                                                                                       ()) : null;
            TraceScope traceScope = DataTransferProtoUtil.ContinueTraceSpan(proto.GetHeader()
                                                                            , proto.GetType().Name);

            try
            {
                RequestShortCircuitFds(PBHelper.Convert(proto.GetHeader().GetBlock()), PBHelper.Convert
                                           (proto.GetHeader().GetToken()), slotId, proto.GetMaxVersion(), proto.GetSupportsReceiptVerification
                                           ());
            }
            finally
            {
                if (traceScope != null)
                {
                    traceScope.Close();
                }
            }
        }
Exemple #9
0
        /// <summary>
        /// Receive
        /// <see cref="OP.TransferBlock"/>
        ///
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        private void OpTransferBlock(DataInputStream @in)
        {
            DataTransferProtos.OpTransferBlockProto proto = DataTransferProtos.OpTransferBlockProto
                                                            .ParseFrom(PBHelper.VintPrefixed(@in));
            DatanodeInfo[] targets    = PBHelper.Convert(proto.GetTargetsList());
            TraceScope     traceScope = DataTransferProtoUtil.ContinueTraceSpan(proto.GetHeader()
                                                                                , proto.GetType().Name);

            try
            {
                TransferBlock(PBHelper.Convert(proto.GetHeader().GetBaseHeader().GetBlock()), PBHelper
                              .Convert(proto.GetHeader().GetBaseHeader().GetToken()), proto.GetHeader().GetClientName
                                  (), targets, PBHelper.ConvertStorageTypes(proto.GetTargetStorageTypesList(), targets
                                                                            .Length));
            }
            finally
            {
                if (traceScope != null)
                {
                    traceScope.Close();
                }
            }
        }
Exemple #10
0
        /// <summary>Receive OP_READ_BLOCK</summary>
        /// <exception cref="System.IO.IOException"/>
        private void OpReadBlock()
        {
            DataTransferProtos.OpReadBlockProto proto = DataTransferProtos.OpReadBlockProto.ParseFrom
                                                            (PBHelper.VintPrefixed(@in));
            TraceScope traceScope = DataTransferProtoUtil.ContinueTraceSpan(proto.GetHeader()
                                                                            , proto.GetType().Name);

            try
            {
                ReadBlock(PBHelper.Convert(proto.GetHeader().GetBaseHeader().GetBlock()), PBHelper
                          .Convert(proto.GetHeader().GetBaseHeader().GetToken()), proto.GetHeader().GetClientName
                              (), proto.GetOffset(), proto.GetLen(), proto.GetSendChecksums(), (proto.HasCachingStrategy
                                                                                                    () ? GetCachingStrategy(proto.GetCachingStrategy()) : CachingStrategy.NewDefaultStrategy
                                                                                                    ()));
            }
            finally
            {
                if (traceScope != null)
                {
                    traceScope.Close();
                }
            }
        }
Exemple #11
0
        /// <summary>
        /// Receive
        /// <see cref="OP.ReleaseShortCircuitFds"/>
        ///
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        private void OpReleaseShortCircuitFds(DataInputStream @in)
        {
            DataTransferProtos.ReleaseShortCircuitAccessRequestProto proto = DataTransferProtos.ReleaseShortCircuitAccessRequestProto
                                                                             .ParseFrom(PBHelper.VintPrefixed(@in));
            TraceScope traceScope = DataTransferProtoUtil.ContinueTraceSpan(proto.GetTraceInfo
                                                                                (), proto.GetType().Name);

            try
            {
                ReleaseShortCircuitFds(PBHelper.Convert(proto.GetSlotId()));
            }
            finally
            {
                if (traceScope != null)
                {
                    traceScope.Close();
                }
            }
        }
            /// <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);
                }
                }
            }
        /// <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);
            }
            }
        }
Exemple #14
0
 /// <summary>Writable interface</summary>
 /// <exception cref="System.IO.IOException"/>
 public virtual void ReadFields(InputStream @in)
 {
     proto = DataTransferProtos.PipelineAckProto.ParseFrom(PBHelper.VintPrefixed(@in));
 }