001/* 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.coprocessor; 020 021import java.io.IOException; 022import java.util.ArrayList; 023import java.util.Collections; 024import java.util.List; 025 026import org.apache.hadoop.hbase.Cell; 027import org.apache.hadoop.hbase.CellUtil; 028import org.apache.hadoop.hbase.CoprocessorEnvironment; 029import org.apache.hadoop.hbase.client.Scan; 030import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ColumnAggregationProtos.ColumnAggregationService; 031import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ColumnAggregationProtos.SumRequest; 032import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ColumnAggregationProtos.SumResponse; 033import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils; 034import org.apache.hadoop.hbase.regionserver.InternalScanner; 035import org.apache.hadoop.hbase.util.Bytes; 036import org.slf4j.Logger; 037import org.slf4j.LoggerFactory; 038 039import com.google.protobuf.RpcCallback; 040import com.google.protobuf.RpcController; 041import com.google.protobuf.Service; 042 043/** 044 * The aggregation implementation at a region. 045 */ 046public class ColumnAggregationEndpoint extends ColumnAggregationService 047implements RegionCoprocessor { 048 private static final Logger LOG = LoggerFactory.getLogger(ColumnAggregationEndpoint.class); 049 private RegionCoprocessorEnvironment env = null; 050 051 @Override 052 public Iterable<Service> getServices() { 053 return Collections.singleton(this); 054 } 055 056 @Override 057 public void start(CoprocessorEnvironment env) throws IOException { 058 if (env instanceof RegionCoprocessorEnvironment) { 059 this.env = (RegionCoprocessorEnvironment)env; 060 return; 061 } 062 throw new CoprocessorException("Must be loaded on a table region!"); 063 } 064 065 @Override 066 public void stop(CoprocessorEnvironment env) throws IOException { 067 // Nothing to do. 068 } 069 070 @Override 071 public void sum(RpcController controller, SumRequest request, RpcCallback<SumResponse> done) { 072 // aggregate at each region 073 Scan scan = new Scan(); 074 // Family is required in pb. Qualifier is not. 075 byte [] family = request.getFamily().toByteArray(); 076 byte [] qualifier = request.hasQualifier()? request.getQualifier().toByteArray(): null; 077 if (request.hasQualifier()) { 078 scan.addColumn(family, qualifier); 079 } else { 080 scan.addFamily(family); 081 } 082 int sumResult = 0; 083 InternalScanner scanner = null; 084 try { 085 scanner = this.env.getRegion().getScanner(scan); 086 List<Cell> curVals = new ArrayList<>(); 087 boolean hasMore = false; 088 do { 089 curVals.clear(); 090 hasMore = scanner.next(curVals); 091 for (Cell kv : curVals) { 092 if (CellUtil.matchingQualifier(kv, qualifier)) { 093 sumResult += Bytes.toInt(kv.getValueArray(), kv.getValueOffset()); 094 } 095 } 096 } while (hasMore); 097 } catch (IOException e) { 098 CoprocessorRpcUtils.setControllerException(controller, e); 099 // Set result to -1 to indicate error. 100 sumResult = -1; 101 LOG.info("Setting sum result to -1 to indicate error", e); 102 } finally { 103 if (scanner != null) { 104 try { 105 scanner.close(); 106 } catch (IOException e) { 107 CoprocessorRpcUtils.setControllerException(controller, e); 108 sumResult = -1; 109 LOG.info("Setting sum result to -1 to indicate error", e); 110 } 111 } 112 } 113 LOG.info("Returning result " + sumResult); 114 done.run(SumResponse.newBuilder().setSum(sumResult).build()); 115 } 116}