001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hdfs.server.datanode.fsdataset.impl;
020
021import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATANODE_CACHE_REVOCATION_TIMEOUT_MS;
022import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATANODE_CACHE_REVOCATION_TIMEOUT_MS_DEFAULT;
023import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATANODE_CACHE_REVOCATION_POLLING_MS;
024import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATANODE_CACHE_REVOCATION_POLLING_MS_DEFAULT;
025
026import com.google.common.base.Preconditions;
027import com.google.common.util.concurrent.ThreadFactoryBuilder;
028
029import java.io.FileInputStream;
030import java.io.FileNotFoundException;
031import java.io.IOException;
032import java.util.ArrayList;
033import java.util.HashMap;
034import java.util.Iterator;
035import java.util.List;
036import java.util.Map.Entry;
037import java.util.concurrent.Executor;
038import java.util.concurrent.LinkedBlockingQueue;
039import java.util.concurrent.ThreadFactory;
040import java.util.concurrent.ThreadPoolExecutor;
041import java.util.concurrent.ScheduledThreadPoolExecutor;
042import java.util.concurrent.TimeUnit;
043import java.util.concurrent.atomic.AtomicLong;
044
045import org.apache.commons.io.IOUtils;
046import org.apache.commons.lang.time.DurationFormatUtils;
047import org.apache.hadoop.classification.InterfaceAudience;
048import org.apache.hadoop.classification.InterfaceStability;
049import org.apache.hadoop.fs.ChecksumException;
050import org.apache.hadoop.hdfs.ExtendedBlockId;
051import org.apache.hadoop.hdfs.DFSConfigKeys;
052import org.apache.hadoop.hdfs.protocol.BlockListAsLongs;
053import org.apache.hadoop.hdfs.protocol.ExtendedBlock;
054import org.apache.hadoop.hdfs.server.datanode.DatanodeUtil;
055import org.apache.hadoop.io.nativeio.NativeIO;
056import org.apache.hadoop.util.Time;
057import org.slf4j.Logger;
058import org.slf4j.LoggerFactory;
059
060/**
061 * Manages caching for an FsDatasetImpl by using the mmap(2) and mlock(2)
062 * system calls to lock blocks into memory. Block checksums are verified upon
063 * entry into the cache.
064 */
065@InterfaceAudience.Private
066@InterfaceStability.Unstable
067public class FsDatasetCache {
068  /**
069   * MappableBlocks that we know about.
070   */
071  private static final class Value {
072    final State state;
073    final MappableBlock mappableBlock;
074
075    Value(MappableBlock mappableBlock, State state) {
076      this.mappableBlock = mappableBlock;
077      this.state = state;
078    }
079  }
080
081  private enum State {
082    /**
083     * The MappableBlock is in the process of being cached.
084     */
085    CACHING,
086
087    /**
088     * The MappableBlock was in the process of being cached, but it was
089     * cancelled.  Only the FsDatasetCache#WorkerTask can remove cancelled
090     * MappableBlock objects.
091     */
092    CACHING_CANCELLED,
093
094    /**
095     * The MappableBlock is in the cache.
096     */
097    CACHED,
098
099    /**
100     * The MappableBlock is in the process of uncaching.
101     */
102    UNCACHING;
103
104    /**
105     * Whether we should advertise this block as cached to the NameNode and
106     * clients.
107     */
108    public boolean shouldAdvertise() {
109      return (this == CACHED);
110    }
111  }
112
113  private static final Logger LOG = LoggerFactory.getLogger(FsDatasetCache
114      .class);
115
116  /**
117   * Stores MappableBlock objects and the states they're in.
118   */
119  private final HashMap<ExtendedBlockId, Value> mappableBlockMap =
120      new HashMap<ExtendedBlockId, Value>();
121
122  private final AtomicLong numBlocksCached = new AtomicLong(0);
123
124  private final FsDatasetImpl dataset;
125
126  private final ThreadPoolExecutor uncachingExecutor;
127
128  private final ScheduledThreadPoolExecutor deferredUncachingExecutor;
129
130  private final long revocationMs;
131
132  private final long revocationPollingMs;
133
134  /**
135   * The approximate amount of cache space in use.
136   *
137   * This number is an overestimate, counting bytes that will be used only
138   * if pending caching operations succeed.  It does not take into account
139   * pending uncaching operations.
140   *
141   * This overestimate is more useful to the NameNode than an underestimate,
142   * since we don't want the NameNode to assign us more replicas than
143   * we can cache, because of the current batch of operations.
144   */
145  private final UsedBytesCount usedBytesCount;
146
147  public static class PageRounder {
148    private final long osPageSize =
149        NativeIO.POSIX.getCacheManipulator().getOperatingSystemPageSize();
150
151    /**
152     * Round up a number to the operating system page size.
153     */
154    public long round(long count) {
155      long newCount = 
156          (count + (osPageSize - 1)) / osPageSize;
157      return newCount * osPageSize;
158    }
159  }
160
161  private class UsedBytesCount {
162    private final AtomicLong usedBytes = new AtomicLong(0);
163    
164    private final PageRounder rounder = new PageRounder();
165
166    /**
167     * Try to reserve more bytes.
168     *
169     * @param count    The number of bytes to add.  We will round this
170     *                 up to the page size.
171     *
172     * @return         The new number of usedBytes if we succeeded;
173     *                 -1 if we failed.
174     */
175    long reserve(long count) {
176      count = rounder.round(count);
177      while (true) {
178        long cur = usedBytes.get();
179        long next = cur + count;
180        if (next > maxBytes) {
181          return -1;
182        }
183        if (usedBytes.compareAndSet(cur, next)) {
184          return next;
185        }
186      }
187    }
188    
189    /**
190     * Release some bytes that we're using.
191     *
192     * @param count    The number of bytes to release.  We will round this
193     *                 up to the page size.
194     *
195     * @return         The new number of usedBytes.
196     */
197    long release(long count) {
198      count = rounder.round(count);
199      return usedBytes.addAndGet(-count);
200    }
201    
202    long get() {
203      return usedBytes.get();
204    }
205  }
206
207  /**
208   * The total cache capacity in bytes.
209   */
210  private final long maxBytes;
211
212  /**
213   * Number of cache commands that could not be completed successfully
214   */
215  final AtomicLong numBlocksFailedToCache = new AtomicLong(0);
216  /**
217   * Number of uncache commands that could not be completed successfully
218   */
219  final AtomicLong numBlocksFailedToUncache = new AtomicLong(0);
220
221  public FsDatasetCache(FsDatasetImpl dataset) {
222    this.dataset = dataset;
223    this.maxBytes = dataset.datanode.getDnConf().getMaxLockedMemory();
224    ThreadFactory workerFactory = new ThreadFactoryBuilder()
225        .setDaemon(true)
226        .setNameFormat("FsDatasetCache-%d-" + dataset.toString())
227        .build();
228    this.usedBytesCount = new UsedBytesCount();
229    this.uncachingExecutor = new ThreadPoolExecutor(
230            0, 1,
231            60, TimeUnit.SECONDS,
232            new LinkedBlockingQueue<Runnable>(),
233            workerFactory);
234    this.uncachingExecutor.allowCoreThreadTimeOut(true);
235    this.deferredUncachingExecutor = new ScheduledThreadPoolExecutor(
236            1, workerFactory);
237    this.revocationMs = dataset.datanode.getConf().getLong(
238        DFS_DATANODE_CACHE_REVOCATION_TIMEOUT_MS,
239        DFS_DATANODE_CACHE_REVOCATION_TIMEOUT_MS_DEFAULT);
240    long confRevocationPollingMs = dataset.datanode.getConf().getLong(
241        DFS_DATANODE_CACHE_REVOCATION_POLLING_MS,
242        DFS_DATANODE_CACHE_REVOCATION_POLLING_MS_DEFAULT);
243    long minRevocationPollingMs = revocationMs / 2;
244    if (minRevocationPollingMs < confRevocationPollingMs) {
245      throw new RuntimeException("configured value " +
246              confRevocationPollingMs + "for " +
247              DFS_DATANODE_CACHE_REVOCATION_POLLING_MS +
248              " is too high.  It must not be more than half of the " +
249              "value of " +  DFS_DATANODE_CACHE_REVOCATION_TIMEOUT_MS +
250              ".  Reconfigure this to " + minRevocationPollingMs);
251    }
252    this.revocationPollingMs = confRevocationPollingMs;
253  }
254
255  /**
256   * @return List of cached blocks suitable for translation into a
257   * {@link BlockListAsLongs} for a cache report.
258   */
259  synchronized List<Long> getCachedBlocks(String bpid) {
260    List<Long> blocks = new ArrayList<Long>();
261    for (Iterator<Entry<ExtendedBlockId, Value>> iter =
262        mappableBlockMap.entrySet().iterator(); iter.hasNext(); ) {
263      Entry<ExtendedBlockId, Value> entry = iter.next();
264      if (entry.getKey().getBlockPoolId().equals(bpid)) {
265        if (entry.getValue().state.shouldAdvertise()) {
266          blocks.add(entry.getKey().getBlockId());
267        }
268      }
269    }
270    return blocks;
271  }
272
273  /**
274   * Attempt to begin caching a block.
275   */
276  synchronized void cacheBlock(long blockId, String bpid,
277      String blockFileName, long length, long genstamp,
278      Executor volumeExecutor) {
279    ExtendedBlockId key = new ExtendedBlockId(blockId, bpid);
280    Value prevValue = mappableBlockMap.get(key);
281    if (prevValue != null) {
282      LOG.debug("Block with id {}, pool {} already exists in the "
283              + "FsDatasetCache with state {}", blockId, bpid, prevValue.state
284      );
285      numBlocksFailedToCache.incrementAndGet();
286      return;
287    }
288    mappableBlockMap.put(key, new Value(null, State.CACHING));
289    volumeExecutor.execute(
290        new CachingTask(key, blockFileName, length, genstamp));
291    LOG.debug("Initiating caching for Block with id {}, pool {}", blockId,
292        bpid);
293  }
294
295  synchronized void uncacheBlock(String bpid, long blockId) {
296    ExtendedBlockId key = new ExtendedBlockId(blockId, bpid);
297    Value prevValue = mappableBlockMap.get(key);
298    boolean deferred = false;
299
300    if (!dataset.datanode.getShortCircuitRegistry().
301            processBlockMunlockRequest(key)) {
302      deferred = true;
303    }
304    if (prevValue == null) {
305      LOG.debug("Block with id {}, pool {} does not need to be uncached, "
306          + "because it is not currently in the mappableBlockMap.", blockId,
307          bpid);
308      numBlocksFailedToUncache.incrementAndGet();
309      return;
310    }
311    switch (prevValue.state) {
312    case CACHING:
313      LOG.debug("Cancelling caching for block with id {}, pool {}.", blockId,
314          bpid);
315      mappableBlockMap.put(key,
316          new Value(prevValue.mappableBlock, State.CACHING_CANCELLED));
317      break;
318    case CACHED:
319      mappableBlockMap.put(key,
320          new Value(prevValue.mappableBlock, State.UNCACHING));
321      if (deferred) {
322        if (LOG.isDebugEnabled()) {
323          LOG.debug("{} is anchored, and can't be uncached now.  Scheduling it " +
324                  "for uncaching in {} ",
325              key, DurationFormatUtils.formatDurationHMS(revocationPollingMs));
326        }
327        deferredUncachingExecutor.schedule(
328            new UncachingTask(key, revocationMs),
329            revocationPollingMs, TimeUnit.MILLISECONDS);
330      } else {
331        LOG.debug("{} has been scheduled for immediate uncaching.", key);
332        uncachingExecutor.execute(new UncachingTask(key, 0));
333      }
334      break;
335    default:
336      LOG.debug("Block with id {}, pool {} does not need to be uncached, "
337          + "because it is in state {}.", blockId, bpid, prevValue.state);
338      numBlocksFailedToUncache.incrementAndGet();
339      break;
340    }
341  }
342
343  /**
344   * Background worker that mmaps, mlocks, and checksums a block
345   */
346  private class CachingTask implements Runnable {
347    private final ExtendedBlockId key; 
348    private final String blockFileName;
349    private final long length;
350    private final long genstamp;
351
352    CachingTask(ExtendedBlockId key, String blockFileName, long length, long genstamp) {
353      this.key = key;
354      this.blockFileName = blockFileName;
355      this.length = length;
356      this.genstamp = genstamp;
357    }
358
359    @Override
360    public void run() {
361      boolean success = false;
362      FileInputStream blockIn = null, metaIn = null;
363      MappableBlock mappableBlock = null;
364      ExtendedBlock extBlk = new ExtendedBlock(key.getBlockPoolId(),
365          key.getBlockId(), length, genstamp);
366      long newUsedBytes = usedBytesCount.reserve(length);
367      boolean reservedBytes = false;
368      try {
369        if (newUsedBytes < 0) {
370          LOG.warn("Failed to cache " + key + ": could not reserve " + length +
371              " more bytes in the cache: " +
372              DFSConfigKeys.DFS_DATANODE_MAX_LOCKED_MEMORY_KEY +
373              " of " + maxBytes + " exceeded.");
374          return;
375        }
376        reservedBytes = true;
377        try {
378          blockIn = (FileInputStream)dataset.getBlockInputStream(extBlk, 0);
379          metaIn = DatanodeUtil.getMetaDataInputStream(extBlk, dataset);
380        } catch (ClassCastException e) {
381          LOG.warn("Failed to cache " + key +
382              ": Underlying blocks are not backed by files.", e);
383          return;
384        } catch (FileNotFoundException e) {
385          LOG.info("Failed to cache " + key + ": failed to find backing " +
386              "files.");
387          return;
388        } catch (IOException e) {
389          LOG.warn("Failed to cache " + key + ": failed to open file", e);
390          return;
391        }
392        try {
393          mappableBlock = MappableBlock.
394              load(length, blockIn, metaIn, blockFileName);
395        } catch (ChecksumException e) {
396          // Exception message is bogus since this wasn't caused by a file read
397          LOG.warn("Failed to cache " + key + ": checksum verification failed.");
398          return;
399        } catch (IOException e) {
400          LOG.warn("Failed to cache " + key, e);
401          return;
402        }
403        synchronized (FsDatasetCache.this) {
404          Value value = mappableBlockMap.get(key);
405          Preconditions.checkNotNull(value);
406          Preconditions.checkState(value.state == State.CACHING ||
407                                   value.state == State.CACHING_CANCELLED);
408          if (value.state == State.CACHING_CANCELLED) {
409            mappableBlockMap.remove(key);
410            LOG.warn("Caching of " + key + " was cancelled.");
411            return;
412          }
413          mappableBlockMap.put(key, new Value(mappableBlock, State.CACHED));
414        }
415        LOG.debug("Successfully cached {}.  We are now caching {} bytes in"
416            + " total.", key, newUsedBytes);
417        dataset.datanode.getShortCircuitRegistry().processBlockMlockEvent(key);
418        numBlocksCached.addAndGet(1);
419        dataset.datanode.getMetrics().incrBlocksCached(1);
420        success = true;
421      } finally {
422        IOUtils.closeQuietly(blockIn);
423        IOUtils.closeQuietly(metaIn);
424        if (!success) {
425          if (reservedBytes) {
426            usedBytesCount.release(length);
427          }
428          LOG.debug("Caching of {} was aborted.  We are now caching only {} "
429                  + "bytes in total.", key, usedBytesCount.get());
430          if (mappableBlock != null) {
431            mappableBlock.close();
432          }
433          numBlocksFailedToCache.incrementAndGet();
434
435          synchronized (FsDatasetCache.this) {
436            mappableBlockMap.remove(key);
437          }
438        }
439      }
440    }
441  }
442
443  private class UncachingTask implements Runnable {
444    private final ExtendedBlockId key; 
445    private final long revocationTimeMs;
446
447    UncachingTask(ExtendedBlockId key, long revocationDelayMs) {
448      this.key = key;
449      if (revocationDelayMs == 0) {
450        this.revocationTimeMs = 0;
451      } else {
452        this.revocationTimeMs = revocationDelayMs + Time.monotonicNow();
453      }
454    }
455
456    private boolean shouldDefer() {
457      /* If revocationTimeMs == 0, this is an immediate uncache request.
458       * No clients were anchored at the time we made the request. */
459      if (revocationTimeMs == 0) {
460        return false;
461      }
462      /* Let's check if any clients still have this block anchored. */
463      boolean anchored =
464        !dataset.datanode.getShortCircuitRegistry().
465            processBlockMunlockRequest(key);
466      if (!anchored) {
467        LOG.debug("Uncaching {} now that it is no longer in use " +
468            "by any clients.", key);
469        return false;
470      }
471      long delta = revocationTimeMs - Time.monotonicNow();
472      if (delta < 0) {
473        LOG.warn("Forcibly uncaching {} after {} " +
474            "because client(s) {} refused to stop using it.", key,
475            DurationFormatUtils.formatDurationHMS(revocationTimeMs),
476            dataset.datanode.getShortCircuitRegistry().getClientNames(key));
477        return false;
478      }
479      LOG.info("Replica {} still can't be uncached because some " +
480          "clients continue to use it.  Will wait for {}", key,
481          DurationFormatUtils.formatDurationHMS(delta));
482      return true;
483    }
484
485    @Override
486    public void run() {
487      Value value;
488
489      if (shouldDefer()) {
490        deferredUncachingExecutor.schedule(
491            this, revocationPollingMs, TimeUnit.MILLISECONDS);
492        return;
493      }
494
495      synchronized (FsDatasetCache.this) {
496        value = mappableBlockMap.get(key);
497      }
498      Preconditions.checkNotNull(value);
499      Preconditions.checkArgument(value.state == State.UNCACHING);
500
501      IOUtils.closeQuietly(value.mappableBlock);
502      synchronized (FsDatasetCache.this) {
503        mappableBlockMap.remove(key);
504      }
505      long newUsedBytes =
506          usedBytesCount.release(value.mappableBlock.getLength());
507      numBlocksCached.addAndGet(-1);
508      dataset.datanode.getMetrics().incrBlocksUncached(1);
509      if (revocationTimeMs != 0) {
510        LOG.debug("Uncaching of {} completed. usedBytes = {}",
511            key, newUsedBytes);
512      } else {
513        LOG.debug("Deferred uncaching of {} completed. usedBytes = {}",
514            key, newUsedBytes);
515      }
516    }
517  }
518
519  // Stats related methods for FSDatasetMBean
520
521  /**
522   * Get the approximate amount of cache space used.
523   */
524  public long getCacheUsed() {
525    return usedBytesCount.get();
526  }
527
528  /**
529   * Get the maximum amount of bytes we can cache.  This is a constant.
530   */
531  public long getCacheCapacity() {
532    return maxBytes;
533  }
534
535  public long getNumBlocksFailedToCache() {
536    return numBlocksFailedToCache.get();
537  }
538
539  public long getNumBlocksFailedToUncache() {
540    return numBlocksFailedToUncache.get();
541  }
542
543  public long getNumBlocksCached() {
544    return numBlocksCached.get();
545  }
546
547  public synchronized boolean isCached(String bpid, long blockId) {
548    ExtendedBlockId block = new ExtendedBlockId(blockId, bpid);
549    Value val = mappableBlockMap.get(block);
550    return (val != null) && val.state.shouldAdvertise();
551  }
552}