View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.SortedSet;
26  import java.util.concurrent.atomic.AtomicLong;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.classification.InterfaceAudience;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.fs.Path;
33  import org.apache.hadoop.hbase.HConstants;
34  import org.apache.hadoop.hbase.KeyValue;
35  import org.apache.hadoop.hbase.client.Scan;
36  import org.apache.hadoop.hbase.monitoring.MonitoredTask;
37  import org.apache.hadoop.hbase.regionserver.compactions.Compactor;
38  
39  /**
40   * Store flusher interface. Turns a snapshot of memstore into a set of store files (usually one).
41   * Custom implementation can be provided.
42   */
43  @InterfaceAudience.Private
44  abstract class StoreFlusher {
45    protected Configuration conf;
46    protected Store store;
47  
48    public StoreFlusher(Configuration conf, Store store) {
49      this.conf = conf;
50      this.store = store;
51    }
52  
53    /**
54     * Turns a snapshot of memstore into a set of store files.
55     * @param snapshot Memstore snapshot.
56     * @param cacheFlushSeqNum Log cache flush sequence number.
57     * @param snapshotTimeRangeTracker Time range tracker from the memstore
58     *                                 pertaining to the snapshot.
59     * @param flushedSize Out parameter for the size of the KVs flushed.
60     * @param status Task that represents the flush operation and may be updated with status.
61     * @return List of files written. Can be empty; must not be null.
62     */
63    public abstract List<Path> flushSnapshot(SortedSet<KeyValue> snapshot, long cacheFlushSeqNum,
64        TimeRangeTracker snapshotTimeRangeTracker, AtomicLong flushedSize, MonitoredTask status)
65        throws IOException;
66  
67    protected void finalizeWriter(StoreFile.Writer writer, long cacheFlushSeqNum,
68        MonitoredTask status) throws IOException {
69      // Write out the log sequence number that corresponds to this output
70      // hfile. Also write current time in metadata as minFlushTime.
71      // The hfile is current up to and including cacheFlushSeqNum.
72      status.setStatus("Flushing " + store + ": appending metadata");
73      writer.appendMetadata(cacheFlushSeqNum, false);
74      status.setStatus("Flushing " + store + ": closing flushed file");
75      writer.close();
76    }
77  
78    /** Calls coprocessor to create a flush scanner based on memstore scanner */
79    protected InternalScanner preCreateCoprocScanner(
80        KeyValueScanner memstoreScanner) throws IOException {
81      if (store.getCoprocessorHost() != null) {
82        return store.getCoprocessorHost().preFlushScannerOpen(store, memstoreScanner);
83      }
84      return null;
85    }
86  
87    /** Creates the default flush scanner based on memstore scanner */
88    protected InternalScanner createStoreScanner(long smallestReadPoint,
89        KeyValueScanner memstoreScanner) throws IOException {
90      Scan scan = new Scan();
91      scan.setMaxVersions(store.getScanInfo().getMaxVersions());
92      return new StoreScanner(store, store.getScanInfo(), scan,
93          Collections.singletonList(memstoreScanner), ScanType.COMPACT_RETAIN_DELETES,
94          smallestReadPoint, HConstants.OLDEST_TIMESTAMP);
95    }
96  
97    /**
98     * Calls coprocessor to create a scanner based on default flush scanner
99     * @return new or default scanner; if null, flush should not proceed.
100    */
101   protected  InternalScanner postCreateCoprocScanner(InternalScanner scanner)
102       throws IOException {
103     if (store.getCoprocessorHost() != null) {
104       return store.getCoprocessorHost().preFlush(store, scanner);
105     }
106     return scanner;
107   }
108 
109   /**
110    * Performs memstore flush, writing data from scanner into sink.
111    * @param scanner Scanner to get data from.
112    * @param sink Sink to write data to. Could be StoreFile.Writer.
113    * @param smallestReadPoint Smallest read point used for the flush.
114    * @return Bytes flushed.
115 s   */
116   protected long performFlush(InternalScanner scanner,
117       Compactor.CellSink sink, long smallestReadPoint) throws IOException {
118     int compactionKVMax = conf.getInt(HConstants.COMPACTION_KV_MAX, 10);
119     List<KeyValue> kvs = new ArrayList<KeyValue>();
120     boolean hasMore;
121     long flushed = 0;
122     do {
123       hasMore = scanner.next(kvs, compactionKVMax);
124       if (!kvs.isEmpty()) {
125         for (KeyValue kv : kvs) {
126           // If we know that this KV is going to be included always, then let us
127           // set its memstoreTS to 0. This will help us save space when writing to
128           // disk.
129           if (kv.getMvccVersion() <= smallestReadPoint) {
130             // let us not change the original KV. It could be in the memstore
131             // changing its memstoreTS could affect other threads/scanners.
132             kv = kv.shallowCopy();
133             kv.setMvccVersion(0);
134           }
135           sink.append(kv);
136           flushed += MemStore.heapSizeChange(kv, true);
137         }
138         kvs.clear();
139       }
140     } while (hasMore);
141     return flushed;
142   }
143 }