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