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.Cell;
34  import org.apache.hadoop.hbase.HConstants;
35  import org.apache.hadoop.hbase.KeyValue;
36  import org.apache.hadoop.hbase.KeyValueUtil;
37  import org.apache.hadoop.hbase.client.Scan;
38  import org.apache.hadoop.hbase.monitoring.MonitoredTask;
39  import org.apache.hadoop.hbase.regionserver.compactions.Compactor;
40  
41  /**
42   * Store flusher interface. Turns a snapshot of memstore into a set of store files (usually one).
43   * Custom implementation can be provided.
44   */
45  @InterfaceAudience.Private
46  abstract class StoreFlusher {
47    protected Configuration conf;
48    protected Store store;
49  
50    public StoreFlusher(Configuration conf, Store store) {
51      this.conf = conf;
52      this.store = store;
53    }
54  
55    /**
56     * Turns a snapshot of memstore into a set of store files.
57     * @param snapshot Memstore snapshot.
58     * @param cacheFlushSeqNum Log cache flush sequence number.
59     * @param snapshotTimeRangeTracker Time range tracker from the memstore
60     *                                 pertaining to the snapshot.
61     * @param flushedSize Out parameter for the size of the KVs flushed.
62     * @param status Task that represents the flush operation and may be updated with status.
63     * @return List of files written. Can be empty; must not be null.
64     */
65    public abstract List<Path> flushSnapshot(SortedSet<KeyValue> snapshot, long cacheFlushSeqNum,
66        TimeRangeTracker snapshotTimeRangeTracker, AtomicLong flushedSize, MonitoredTask status)
67        throws IOException;
68  
69    protected void finalizeWriter(StoreFile.Writer writer, long cacheFlushSeqNum,
70        MonitoredTask status) throws IOException {
71      // Write out the log sequence number that corresponds to this output
72      // hfile. Also write current time in metadata as minFlushTime.
73      // The hfile is current up to and including cacheFlushSeqNum.
74      status.setStatus("Flushing " + store + ": appending metadata");
75      writer.appendMetadata(cacheFlushSeqNum, false);
76      status.setStatus("Flushing " + store + ": closing flushed file");
77      writer.close();
78    }
79  
80    /** Calls coprocessor to create a flush scanner based on memstore scanner */
81    protected InternalScanner preCreateCoprocScanner(
82        KeyValueScanner memstoreScanner) throws IOException {
83      if (store.getCoprocessorHost() != null) {
84        return store.getCoprocessorHost().preFlushScannerOpen(store, memstoreScanner);
85      }
86      return null;
87    }
88  
89    /** Creates the default flush scanner based on memstore scanner */
90    protected InternalScanner createStoreScanner(long smallestReadPoint,
91        KeyValueScanner memstoreScanner) throws IOException {
92      Scan scan = new Scan();
93      scan.setMaxVersions(store.getScanInfo().getMaxVersions());
94      return new StoreScanner(store, store.getScanInfo(), scan,
95          Collections.singletonList(memstoreScanner), ScanType.COMPACT_RETAIN_DELETES,
96          smallestReadPoint, HConstants.OLDEST_TIMESTAMP);
97    }
98  
99    /**
100    * Calls coprocessor to create a scanner based on default flush scanner
101    * @return new or default scanner; if null, flush should not proceed.
102    */
103   protected  InternalScanner postCreateCoprocScanner(InternalScanner scanner)
104       throws IOException {
105     if (store.getCoprocessorHost() != null) {
106       return store.getCoprocessorHost().preFlush(store, scanner);
107     }
108     return scanner;
109   }
110 
111   /**
112    * Performs memstore flush, writing data from scanner into sink.
113    * @param scanner Scanner to get data from.
114    * @param sink Sink to write data to. Could be StoreFile.Writer.
115    * @param smallestReadPoint Smallest read point used for the flush.
116    * @return Bytes flushed.
117 s   */
118   protected long performFlush(InternalScanner scanner,
119       Compactor.CellSink sink, long smallestReadPoint) throws IOException {
120     int compactionKVMax =
121       conf.getInt(HConstants.COMPACTION_KV_MAX, HConstants.COMPACTION_KV_MAX_DEFAULT);
122     List<Cell> kvs = new ArrayList<Cell>();
123     boolean hasMore;
124     long flushed = 0;
125     do {
126       hasMore = scanner.next(kvs, compactionKVMax);
127       if (!kvs.isEmpty()) {
128         for (Cell c : kvs) {
129           // If we know that this KV is going to be included always, then let us
130           // set its memstoreTS to 0. This will help us save space when writing to
131           // disk.
132           KeyValue kv = KeyValueUtil.ensureKeyValue(c);
133           if (kv.getMvccVersion() <= smallestReadPoint) {
134             // let us not change the original KV. It could be in the memstore
135             // changing its memstoreTS could affect other threads/scanners.
136             kv = kv.shallowCopy();
137             kv.setMvccVersion(0);
138           }
139           sink.append(kv);
140           flushed += MemStore.heapSizeChange(kv, true);
141         }
142         kvs.clear();
143       }
144     } while (hasMore);
145     return flushed;
146   }
147 }