internal Value(MappableBlock mappableBlock, FsDatasetCache.State state) { this.mappableBlock = mappableBlock; this.state = state; }
public virtual void Run() { bool success = false; FileInputStream blockIn = null; FileInputStream metaIn = null; MappableBlock mappableBlock = null; ExtendedBlock extBlk = new ExtendedBlock(this.key.GetBlockPoolId(), this.key.GetBlockId (), this.length, this.genstamp); long newUsedBytes = this._enclosing.usedBytesCount.Reserve(this.length); bool reservedBytes = false; try { if (newUsedBytes < 0) { FsDatasetCache.Log.Warn("Failed to cache " + this.key + ": could not reserve " + this.length + " more bytes in the cache: " + DFSConfigKeys.DfsDatanodeMaxLockedMemoryKey + " of " + this._enclosing.maxBytes + " exceeded."); return; } reservedBytes = true; try { blockIn = (FileInputStream)this._enclosing.dataset.GetBlockInputStream(extBlk, 0); metaIn = DatanodeUtil.GetMetaDataInputStream(extBlk, this._enclosing.dataset); } catch (InvalidCastException e) { FsDatasetCache.Log.Warn("Failed to cache " + this.key + ": Underlying blocks are not backed by files." , e); return; } catch (FileNotFoundException) { FsDatasetCache.Log.Info("Failed to cache " + this.key + ": failed to find backing " + "files."); return; } catch (IOException e) { FsDatasetCache.Log.Warn("Failed to cache " + this.key + ": failed to open file", e); return; } try { mappableBlock = MappableBlock.Load(this.length, blockIn, metaIn, this.blockFileName ); } catch (ChecksumException) { // Exception message is bogus since this wasn't caused by a file read FsDatasetCache.Log.Warn("Failed to cache " + this.key + ": checksum verification failed." ); return; } catch (IOException e) { FsDatasetCache.Log.Warn("Failed to cache " + this.key, e); return; } lock (this._enclosing) { FsDatasetCache.Value value = this._enclosing.mappableBlockMap[this.key]; Preconditions.CheckNotNull(value); Preconditions.CheckState(value.state == FsDatasetCache.State.Caching || value.state == FsDatasetCache.State.CachingCancelled); if (value.state == FsDatasetCache.State.CachingCancelled) { Sharpen.Collections.Remove(this._enclosing.mappableBlockMap, this.key); FsDatasetCache.Log.Warn("Caching of " + this.key + " was cancelled."); return; } this._enclosing.mappableBlockMap[this.key] = new FsDatasetCache.Value(mappableBlock , FsDatasetCache.State.Cached); } FsDatasetCache.Log.Debug("Successfully cached {}. We are now caching {} bytes in" + " total.", this.key, newUsedBytes); this._enclosing.dataset.datanode.GetShortCircuitRegistry().ProcessBlockMlockEvent (this.key); this._enclosing.numBlocksCached.AddAndGet(1); this._enclosing.dataset.datanode.GetMetrics().IncrBlocksCached(1); success = true; } finally { IOUtils.CloseQuietly(blockIn); IOUtils.CloseQuietly(metaIn); if (!success) { if (reservedBytes) { this._enclosing.usedBytesCount.Release(this.length); } FsDatasetCache.Log.Debug("Caching of {} was aborted. We are now caching only {} " + "bytes in total.", this.key, this._enclosing.usedBytesCount.Get()); if (mappableBlock != null) { mappableBlock.Close(); } this._enclosing.numBlocksFailedToCache.IncrementAndGet(); lock (this._enclosing) { Sharpen.Collections.Remove(this._enclosing.mappableBlockMap, this.key); } } } }