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  package org.apache.hadoop.hbase.regionserver.compactions;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.classification.InterfaceAudience;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.KeyValue;
33  import org.apache.hadoop.hbase.client.Scan;
34  import org.apache.hadoop.hbase.io.CellOutputStream;
35  import org.apache.hadoop.hbase.io.compress.Compression;
36  import org.apache.hadoop.hbase.io.hfile.HFileWriterV2;
37  import org.apache.hadoop.hbase.regionserver.HStore;
38  import org.apache.hadoop.hbase.regionserver.InternalScanner;
39  import org.apache.hadoop.hbase.regionserver.MultiVersionConsistencyControl;
40  import org.apache.hadoop.hbase.regionserver.ScanType;
41  import org.apache.hadoop.hbase.regionserver.Store;
42  import org.apache.hadoop.hbase.regionserver.StoreFile;
43  import org.apache.hadoop.hbase.regionserver.StoreFileScanner;
44  import org.apache.hadoop.hbase.regionserver.StoreScanner;
45  import org.apache.hadoop.hbase.util.Bytes;
46  import org.apache.hadoop.util.StringUtils;
47  
48  /**
49   * A compactor is a compaction algorithm associated a given policy. Base class also contains
50   * reusable parts for implementing compactors (what is common and what isn't is evolving).
51   */
52  @InterfaceAudience.Private
53  public abstract class Compactor {
54    private static final Log LOG = LogFactory.getLog(Compactor.class);
55    protected CompactionProgress progress;
56    protected Configuration conf;
57    protected Store store;
58  
59    private int compactionKVMax;
60    protected Compression.Algorithm compactionCompression;
61  
62    //TODO: depending on Store is not good but, realistically, all compactors currently do.
63    Compactor(final Configuration conf, final Store store) {
64      this.conf = conf;
65      this.store = store;
66      this.compactionKVMax = this.conf.getInt(HConstants.COMPACTION_KV_MAX, 10);
67      this.compactionCompression = (this.store.getFamily() == null) ?
68          Compression.Algorithm.NONE : this.store.getFamily().getCompactionCompression();
69    }
70  
71    /**
72     * TODO: Replace this with {@link CellOutputStream} when StoreFile.Writer uses cells.
73     */
74    public interface CellSink {
75      void append(KeyValue kv) throws IOException;
76    }
77  
78    /**
79     * Do a minor/major compaction on an explicit set of storefiles from a Store.
80     * @param request the requested compaction
81     * @return Product of compaction or an empty list if all cells expired or deleted and nothing made
82     *         it through the compaction.
83     * @throws IOException
84     */
85    public abstract List<Path> compact(final CompactionRequest request) throws IOException;
86  
87    /**
88     * Compact a list of files for testing. Creates a fake {@link CompactionRequest} to pass to
89     * {@link #compact(CompactionRequest)};
90     * @param filesToCompact the files to compact. These are used as the compactionSelection for the
91     *          generated {@link CompactionRequest}.
92     * @param isMajor true to major compact (prune all deletes, max versions, etc)
93     * @return Product of compaction or an empty list if all cells expired or deleted and nothing made
94     *         it through the compaction.
95     * @throws IOException
96     */
97    public List<Path> compactForTesting(final Collection<StoreFile> filesToCompact, boolean isMajor)
98        throws IOException {
99      CompactionRequest cr = new CompactionRequest(filesToCompact);
100     cr.setIsMajor(isMajor);
101     return this.compact(cr);
102   }
103 
104   public CompactionProgress getProgress() {
105     return this.progress;
106   }
107 
108   /** The sole reason this class exists is that java has no ref/out/pointer parameters. */
109   protected static class FileDetails {
110     /** Maximum key count after compaction (for blooms) */
111     public long maxKeyCount = 0;
112     /** Earliest put timestamp if major compaction */
113     public long earliestPutTs = HConstants.LATEST_TIMESTAMP;
114     /** The last key in the files we're compacting. */
115     public long maxSeqId = 0;
116     /** Latest memstore read point found in any of the involved files */
117     public long maxMVCCReadpoint = 0;
118   }
119 
120   protected FileDetails getFileDetails(
121       Collection<StoreFile> filesToCompact, boolean calculatePutTs) throws IOException {
122     FileDetails fd = new FileDetails();
123 
124     for (StoreFile file : filesToCompact) {
125       long seqNum = file.getMaxSequenceId();
126       fd.maxSeqId = Math.max(fd.maxSeqId, seqNum);
127       StoreFile.Reader r = file.getReader();
128       if (r == null) {
129         LOG.warn("Null reader for " + file.getPath());
130         continue;
131       }
132       // NOTE: getFilterEntries could cause under-sized blooms if the user
133       // switches bloom type (e.g. from ROW to ROWCOL)
134       long keyCount = (r.getBloomFilterType() == store.getFamily().getBloomFilterType())
135           ? r.getFilterEntries() : r.getEntries();
136       fd.maxKeyCount += keyCount;
137       // calculate the latest MVCC readpoint in any of the involved store files
138       Map<byte[], byte[]> fileInfo = r.loadFileInfo();
139       byte tmp[] = fileInfo.get(HFileWriterV2.MAX_MEMSTORE_TS_KEY);
140       if (tmp != null) {
141         fd.maxMVCCReadpoint = Math.max(fd.maxMVCCReadpoint, Bytes.toLong(tmp));
142       }
143       // If required, calculate the earliest put timestamp of all involved storefiles.
144       // This is used to remove family delete marker during compaction.
145       long earliestPutTs = 0;
146       if (calculatePutTs) {
147         tmp = fileInfo.get(StoreFile.EARLIEST_PUT_TS);
148         if (tmp == null) {
149           // There's a file with no information, must be an old one
150           // assume we have very old puts
151           fd.earliestPutTs = earliestPutTs = HConstants.OLDEST_TIMESTAMP;
152         } else {
153           earliestPutTs = Bytes.toLong(tmp);
154           fd.earliestPutTs = Math.min(fd.earliestPutTs, earliestPutTs);
155         }
156       }
157       if (LOG.isDebugEnabled()) {
158         LOG.debug("Compacting " + file +
159           ", keycount=" + keyCount +
160           ", bloomtype=" + r.getBloomFilterType().toString() +
161           ", size=" + StringUtils.humanReadableInt(r.length()) +
162           ", encoding=" + r.getHFileReader().getEncodingOnDisk() +
163           ", seqNum=" + seqNum +
164           (calculatePutTs ? ", earliestPutTs=" + earliestPutTs: ""));
165       }
166     }
167     return fd;
168   }
169 
170   protected List<StoreFileScanner> createFileScanners(
171       final Collection<StoreFile> filesToCompact) throws IOException {
172     return StoreFileScanner.getScannersForStoreFiles(filesToCompact, false, false, true);
173   }
174 
175   protected long setSmallestReadPoint() {
176     long smallestReadPoint = store.getSmallestReadPoint();
177     MultiVersionConsistencyControl.setThreadReadPoint(smallestReadPoint);
178     return smallestReadPoint;
179   }
180 
181   protected InternalScanner preCreateCoprocScanner(final CompactionRequest request,
182       ScanType scanType, long earliestPutTs,  List<StoreFileScanner> scanners) throws IOException {
183     if (store.getCoprocessorHost() == null) return null;
184     return store.getCoprocessorHost()
185         .preCompactScannerOpen(store, scanners, scanType, earliestPutTs, request);
186   }
187 
188   protected InternalScanner postCreateCoprocScanner(final CompactionRequest request,
189       ScanType scanType, InternalScanner scanner) throws IOException {
190     if (store.getCoprocessorHost() == null) return scanner;
191     return store.getCoprocessorHost().preCompact(store, scanner, scanType, request);
192   }
193 
194   @SuppressWarnings("deprecation")
195   protected boolean performCompaction(InternalScanner scanner,
196       CellSink writer, long smallestReadPoint) throws IOException {
197     int bytesWritten = 0;
198     // Since scanner.next() can return 'false' but still be delivering data,
199     // we have to use a do/while loop.
200     List<KeyValue> kvs = new ArrayList<KeyValue>();
201     // Limit to "hbase.hstore.compaction.kv.max" (default 10) to avoid OOME
202     int closeCheckInterval = HStore.getCloseCheckInterval();
203     boolean hasMore;
204     do {
205       hasMore = scanner.next(kvs, compactionKVMax);
206       // output to writer:
207       for (KeyValue kv : kvs) {
208         if (kv.getMvccVersion() <= smallestReadPoint) {
209           kv.setMvccVersion(0);
210         }
211         writer.append(kv);
212         ++progress.currentCompactedKVs;
213 
214         // check periodically to see if a system stop is requested
215         if (closeCheckInterval > 0) {
216           bytesWritten += kv.getLength();
217           if (bytesWritten > closeCheckInterval) {
218             bytesWritten = 0;
219             if (!store.areWritesEnabled()) {
220               progress.cancel();
221               return false;
222             }
223           }
224         }
225       }
226       kvs.clear();
227     } while (hasMore);
228     progress.complete();
229     return true;
230   }
231 
232   protected void abortWriter(final StoreFile.Writer writer) throws IOException {
233     writer.close();
234     store.getFileSystem().delete(writer.getPath(), false);
235   }
236 
237   /**
238    * @param scanners Store file scanners.
239    * @param scanType Scan type.
240    * @param smallestReadPoint Smallest MVCC read point.
241    * @param earliestPutTs Earliest put across all files.
242    * @return A compaction scanner.
243    */
244   protected InternalScanner createScanner(Store store, List<StoreFileScanner> scanners,
245       ScanType scanType, long smallestReadPoint, long earliestPutTs) throws IOException {
246     Scan scan = new Scan();
247     scan.setMaxVersions(store.getFamily().getMaxVersions());
248     return new StoreScanner(store, store.getScanInfo(), scan, scanners,
249         scanType, smallestReadPoint, earliestPutTs);
250   }
251 }