/// <summary>close opened files.</summary> /// <exception cref="System.IO.IOException"/> public virtual void Close() { if (blockInFd != null && ((dropCacheBehindAllReads) || (dropCacheBehindLargeReads && IsLongRead()))) { try { NativeIO.POSIX.GetCacheManipulator().PosixFadviseIfPossible(block.GetBlockName(), blockInFd, lastCacheDropOffset, offset - lastCacheDropOffset, NativeIO.POSIX.PosixFadvDontneed ); } catch (Exception e) { Log.Warn("Unable to drop cache on file close", e); } } if (curReadahead != null) { curReadahead.Cancel(); } IOException ioe = null; if (checksumIn != null) { try { checksumIn.Close(); } catch (IOException e) { // close checksum file ioe = e; } checksumIn = null; } if (blockIn != null) { try { blockIn.Close(); } catch (IOException e) { // close data file ioe = e; } blockIn = null; blockInFd = null; } if (volumeRef != null) { IOUtils.Cleanup(null, volumeRef); volumeRef = null; } // throw IOException if there is any if (ioe != null) { throw ioe; } }
public override void ReleaseExternalResources() { if (readaheadRequest != null) { readaheadRequest.Cancel(); } base.ReleaseExternalResources(); }
/// <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); } }
/// <exception cref="System.Exception"/> public override void Close() { if (readaheadRequest != null) { readaheadRequest.Cancel(); } if (manageOsCache && GetEndOffset() - GetStartOffset() > 0) { try { NativeIO.POSIX.GetCacheManipulator().PosixFadviseIfPossible(identifier, fd, GetStartOffset (), GetEndOffset() - GetStartOffset(), NativeIO.POSIX.PosixFadvDontneed); } catch (Exception t) { Log.Warn("Failed to manage OS cache for " + identifier, t); } } base.Close(); }
/// <summary>Close the input stream.</summary> /// <remarks> /// Close the input stream. Note that we need to read to the end of the /// stream to validate the checksum. /// </remarks> /// <exception cref="System.IO.IOException"/> public override void Close() { if (curReadahead != null) { curReadahead.Cancel(); } if (currentOffset < dataLength) { byte[] t = new byte[Math.Min((int)(int.MaxValue & (dataLength - currentOffset)), 32 * 1024)]; while (currentOffset < dataLength) { int n = Read(t, 0, t.Length); if (0 == n) { throw new EOFException("Could not validate checksum"); } } } @in.Close(); }