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  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.fs.Path;
28  import org.apache.hadoop.hbase.Cell;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.hbase.client.Scan;
32  import org.apache.hadoop.hbase.monitoring.MonitoredTask;
33  import org.apache.hadoop.hbase.regionserver.InternalScanner.NextState;
34  import org.apache.hadoop.hbase.regionserver.compactions.Compactor;
35  
36  /**
37   * Store flusher interface. Turns a snapshot of memstore into a set of store files (usually one).
38   * Custom implementation can be provided.
39   */
40  @InterfaceAudience.Private
41  abstract class StoreFlusher {
42    protected Configuration conf;
43    protected Store store;
44  
45    public StoreFlusher(Configuration conf, Store store) {
46      this.conf = conf;
47      this.store = store;
48    }
49  
50    /**
51     * Turns a snapshot of memstore into a set of store files.
52     * @param snapshot Memstore snapshot.
53     * @param cacheFlushSeqNum Log cache flush sequence number.
54     * @param status Task that represents the flush operation and may be updated with status.
55     * @return List of files written. Can be empty; must not be null.
56     */
57    public abstract List<Path> flushSnapshot(MemStoreSnapshot snapshot, long cacheFlushSeqNum,
58        MonitoredTask status) throws IOException;
59  
60    protected void finalizeWriter(StoreFile.Writer writer, long cacheFlushSeqNum,
61        MonitoredTask status) throws IOException {
62      // Write out the log sequence number that corresponds to this output
63      // hfile. Also write current time in metadata as minFlushTime.
64      // The hfile is current up to and including cacheFlushSeqNum.
65      status.setStatus("Flushing " + store + ": appending metadata");
66      writer.appendMetadata(cacheFlushSeqNum, false);
67      status.setStatus("Flushing " + store + ": closing flushed file");
68      writer.close();
69    }
70  
71  
72    /**
73     * Creates the scanner for flushing snapshot. Also calls coprocessors.
74     * @param snapshotScanner
75     * @param smallestReadPoint
76     * @return The scanner; null if coprocessor is canceling the flush.
77     */
78    protected InternalScanner createScanner(KeyValueScanner snapshotScanner,
79        long smallestReadPoint) throws IOException {
80      InternalScanner scanner = null;
81      if (store.getCoprocessorHost() != null) {
82        scanner = store.getCoprocessorHost().preFlushScannerOpen(store, snapshotScanner);
83      }
84      if (scanner == null) {
85        Scan scan = new Scan();
86        scan.setMaxVersions(store.getScanInfo().getMaxVersions());
87        scanner = new StoreScanner(store, store.getScanInfo(), scan,
88            Collections.singletonList(snapshotScanner), ScanType.COMPACT_RETAIN_DELETES,
89            smallestReadPoint, HConstants.OLDEST_TIMESTAMP);
90      }
91      assert scanner != null;
92      if (store.getCoprocessorHost() != null) {
93        try {
94          return store.getCoprocessorHost().preFlush(store, scanner);
95        } catch (IOException ioe) {
96          scanner.close();
97          throw ioe;
98        }
99      }
100     return scanner;
101   }
102 
103   /**
104    * Performs memstore flush, writing data from scanner into sink.
105    * @param scanner Scanner to get data from.
106    * @param sink Sink to write data to. Could be StoreFile.Writer.
107    * @param smallestReadPoint Smallest read point used for the flush.
108    */
109   protected void performFlush(InternalScanner scanner,
110       Compactor.CellSink sink, long smallestReadPoint) throws IOException {
111     int compactionKVMax =
112       conf.getInt(HConstants.COMPACTION_KV_MAX, HConstants.COMPACTION_KV_MAX_DEFAULT);
113     List<Cell> kvs = new ArrayList<Cell>();
114     boolean hasMore;
115     do {
116       hasMore = NextState.hasMoreValues(scanner.next(kvs, compactionKVMax));
117       if (!kvs.isEmpty()) {
118         for (Cell c : kvs) {
119           // If we know that this KV is going to be included always, then let us
120           // set its memstoreTS to 0. This will help us save space when writing to
121           // disk.
122           sink.append(c);
123         }
124         kvs.clear();
125       }
126     } while (hasMore);
127   }
128 }