Ejemplo n.º 1
0
 /// <summary>
 /// Manage the OS buffer cache by performing read-ahead
 /// and drop-behind.
 /// </summary>
 /// <exception cref="System.IO.IOException"/>
 private void ManageOsCache()
 {
     // We can't manage the cache for this block if we don't have a file
     // descriptor to work with.
     if (blockInFd == null)
     {
         return;
     }
     // Perform readahead if necessary
     if ((readaheadLength > 0) && (datanode.readaheadPool != null) && (alwaysReadahead ||
                                                                       IsLongRead()))
     {
         curReadahead = datanode.readaheadPool.ReadaheadStream(clientTraceFmt, blockInFd,
                                                               offset, readaheadLength, long.MaxValue, curReadahead);
     }
     // Drop what we've just read from cache, since we aren't
     // likely to need it again
     if (dropCacheBehindAllReads || (dropCacheBehindLargeReads && IsLongRead()))
     {
         long nextCacheDropOffset = lastCacheDropOffset + CacheDropIntervalBytes;
         if (offset >= nextCacheDropOffset)
         {
             long dropLength = offset - lastCacheDropOffset;
             NativeIO.POSIX.GetCacheManipulator().PosixFadviseIfPossible(block.GetBlockName(),
                                                                         blockInFd, lastCacheDropOffset, dropLength, NativeIO.POSIX.PosixFadvDontneed);
             lastCacheDropOffset = offset;
         }
     }
 }
Ejemplo n.º 2
0
 private void DoReadahead()
 {
     if (raPool != null && inFd != null && readahead)
     {
         curReadahead = raPool.ReadaheadStream("ifile", inFd, currentOffset, readaheadLength
                                               , dataLength, curReadahead);
     }
 }
Ejemplo n.º 3
0
 /// <exception cref="System.Exception"/>
 public override object NextChunk()
 {
     if (manageOsCache && readaheadPool != null)
     {
         readaheadRequest = readaheadPool.ReadaheadStream(identifier, fd, GetCurrentOffset
                                                              (), readaheadLength, GetEndOffset(), readaheadRequest);
     }
     return(base.NextChunk());
 }
Ejemplo n.º 4
0
 /// <exception cref="System.IO.IOException"/>
 public override long TransferTo(WritableByteChannel target, long position)
 {
     if (manageOsCache && readaheadPool != null)
     {
         readaheadRequest = readaheadPool.ReadaheadStream(identifier, fd, GetPosition() +
                                                          position, readaheadLength, GetPosition() + GetCount(), readaheadRequest);
     }
     if (this.shuffleTransferToAllowed)
     {
         return(base.TransferTo(target, position));
     }
     else
     {
         return(CustomShuffleTransfer(target, position));
     }
 }
Ejemplo n.º 5
0
        /// <summary>Issue a request to readahead on the given file descriptor.</summary>
        /// <param name="identifier">
        /// a textual identifier that will be used in error
        /// messages (e.g. the file name)
        /// </param>
        /// <param name="fd">the file descriptor to read ahead</param>
        /// <param name="curPos">the current offset at which reads are being issued</param>
        /// <param name="readaheadLength">the configured length to read ahead</param>
        /// <param name="maxOffsetToRead">
        /// the maximum offset that will be readahead
        /// (useful if, for example, only some segment of the file is
        /// requested by the user). Pass
        /// <see cref="Long.MAX_VALUE"/>
        /// to allow
        /// readahead to the end of the file.
        /// </param>
        /// <param name="lastReadahead">
        /// the result returned by the previous invocation
        /// of this function on this file descriptor, or null if this is
        /// the first call
        /// </param>
        /// <returns>
        /// an object representing this outstanding request, or null
        /// if no readahead was performed
        /// </returns>
        public virtual ReadaheadPool.ReadaheadRequest ReadaheadStream(string identifier,
                                                                      FileDescriptor fd, long curPos, long readaheadLength, long maxOffsetToRead, ReadaheadPool.ReadaheadRequest
                                                                      lastReadahead)
        {
            Preconditions.CheckArgument(curPos <= maxOffsetToRead, "Readahead position %s higher than maxOffsetToRead %s"
                                        , curPos, maxOffsetToRead);
            if (readaheadLength <= 0)
            {
                return(null);
            }
            long lastOffset = long.MinValue;

            if (lastReadahead != null)
            {
                lastOffset = lastReadahead.GetOffset();
            }
            // trigger each readahead when we have reached the halfway mark
            // in the previous readahead. This gives the system time
            // to satisfy the readahead before we start reading the data.
            long nextOffset = lastOffset + readaheadLength / 2;

            if (curPos >= nextOffset)
            {
                // cancel any currently pending readahead, to avoid
                // piling things up in the queue. Each reader should have at most
                // one outstanding request in the queue.
                if (lastReadahead != null)
                {
                    lastReadahead.Cancel();
                    lastReadahead = null;
                }
                long length = Math.Min(readaheadLength, maxOffsetToRead - curPos);
                if (length <= 0)
                {
                    // we've reached the end of the stream
                    return(null);
                }
                return(SubmitReadahead(identifier, fd, curPos, length));
            }
            else
            {
                return(lastReadahead);
            }
        }