|
| 1 | +/* |
| 2 | + * Copyright 2015 NAVER Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.navercorp.pinpoint.common.hbase; |
| 18 | + |
| 19 | +import com.navercorp.pinpoint.common.util.StopWatch; |
| 20 | +import com.sematext.hbase.wd.AbstractRowKeyDistributor; |
| 21 | +import org.apache.hadoop.hbase.client.HTableInterface; |
| 22 | +import org.apache.hadoop.hbase.client.ResultScanner; |
| 23 | +import org.apache.hadoop.hbase.client.Scan; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | +import org.springframework.data.hadoop.hbase.ResultsExtractor; |
| 27 | +import org.springframework.data.hadoop.hbase.TableCallback; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author HyunGil Jeong |
| 33 | + */ |
| 34 | +public abstract class AbstractDistributedScannerTableCallback<T> implements TableCallback<T> { |
| 35 | + |
| 36 | + protected final Logger logger = LoggerFactory.getLogger(getClass()); |
| 37 | + protected final boolean debugEnabled = this.logger.isDebugEnabled(); |
| 38 | + |
| 39 | + protected final Scan scan; |
| 40 | + protected final AbstractRowKeyDistributor rowKeyDistributor; |
| 41 | + protected final ResultsExtractor<T> resultsExtractor; |
| 42 | + |
| 43 | + protected AbstractDistributedScannerTableCallback(Scan scan, AbstractRowKeyDistributor rowKeyDistributor, ResultsExtractor<T> resultsExtractor) { |
| 44 | + this.scan = scan; |
| 45 | + this.rowKeyDistributor = rowKeyDistributor; |
| 46 | + this.resultsExtractor = resultsExtractor; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public T doInTable(HTableInterface table) throws Throwable { |
| 51 | + StopWatch watch = null; |
| 52 | + if (debugEnabled) { |
| 53 | + watch = new StopWatch(); |
| 54 | + watch.start(); |
| 55 | + } |
| 56 | + final ResultScanner[] splitScanners = splitScanners(table); |
| 57 | + final ResultScanner scanner = createResultScanner(splitScanners); |
| 58 | + if (debugEnabled) { |
| 59 | + logger.debug("DistributedScanner createTime:{}", watch.stop()); |
| 60 | + watch.start(); |
| 61 | + } |
| 62 | + try { |
| 63 | + return resultsExtractor.extractData(scanner); |
| 64 | + } finally { |
| 65 | + scanner.close(); |
| 66 | + if (debugEnabled) { |
| 67 | + logger.debug("DistributedScanner scanTime:{}", watch.stop()); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + protected abstract ResultScanner createResultScanner(ResultScanner[] scanners) throws IOException; |
| 73 | + |
| 74 | + private ResultScanner[] splitScanners(HTableInterface htable) throws IOException { |
| 75 | + Scan[] scans = this.rowKeyDistributor.getDistributedScans(this.scan); |
| 76 | + final int length = scans.length; |
| 77 | + for(int i = 0; i < length; i++) { |
| 78 | + Scan scan = scans[i]; |
| 79 | + // other properties are already set upon construction |
| 80 | + scan.setId(this.scan.getId() + "-" + i); |
| 81 | + } |
| 82 | + |
| 83 | + ResultScanner[] scanners = new ResultScanner[length]; |
| 84 | + boolean success = false; |
| 85 | + try { |
| 86 | + for (int i = 0; i < length; i++) { |
| 87 | + scanners[i] = htable.getScanner(scans[i]); |
| 88 | + } |
| 89 | + success = true; |
| 90 | + } finally { |
| 91 | + if (!success) { |
| 92 | + closeScanner(scanners); |
| 93 | + } |
| 94 | + } |
| 95 | + return scanners; |
| 96 | + } |
| 97 | + |
| 98 | + private void closeScanner(ResultScanner[] scannerList ) { |
| 99 | + for (ResultScanner scanner : scannerList) { |
| 100 | + if (scanner != null) { |
| 101 | + try { |
| 102 | + scanner.close(); |
| 103 | + } catch (Exception e) { |
| 104 | + logger.warn("Scanner.close() error Caused:{}", e.getMessage(), e); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments