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 static org.junit.Assert.assertEquals;
021
022import com.google.protobuf.ByteString;
023import com.google.protobuf.ServiceException;
024import java.io.IOException;
025import java.util.Map;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.HColumnDescriptor;
029import org.apache.hadoop.hbase.HTableDescriptor;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.Admin;
032import org.apache.hadoop.hbase.client.Put;
033import org.apache.hadoop.hbase.client.Table;
034import org.apache.hadoop.hbase.client.coprocessor.Batch;
035import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ColumnAggregationProtos;
036import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils;
037import org.apache.hadoop.hbase.testclassification.CoprocessorTests;
038import org.apache.hadoop.hbase.testclassification.MediumTests;
039import org.apache.hadoop.hbase.util.Bytes;
040import org.junit.AfterClass;
041import org.junit.BeforeClass;
042import org.junit.ClassRule;
043import org.junit.Rule;
044import org.junit.Test;
045import org.junit.experimental.categories.Category;
046import org.junit.rules.TestName;
047
048@Category({CoprocessorTests.class, MediumTests.class})
049public class TestCoprocessorTableEndpoint {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053      HBaseClassTestRule.forClass(TestCoprocessorTableEndpoint.class);
054
055  private static final byte[] TEST_FAMILY = Bytes.toBytes("TestFamily");
056  private static final byte[] TEST_QUALIFIER = Bytes.toBytes("TestQualifier");
057  private static final byte[] ROW = Bytes.toBytes("testRow");
058  private static final int ROWSIZE = 20;
059  private static final int rowSeperator1 = 5;
060  private static final int rowSeperator2 = 12;
061  private static final byte[][] ROWS = makeN(ROW, ROWSIZE);
062
063  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
064
065  @Rule
066  public TestName name = new TestName();
067
068  @BeforeClass
069  public static void setupBeforeClass() throws Exception {
070    TEST_UTIL.startMiniCluster(2);
071  }
072
073  @AfterClass
074  public static void tearDownAfterClass() throws Exception {
075    TEST_UTIL.shutdownMiniCluster();
076  }
077
078  @Test
079  public void testCoprocessorTableEndpoint() throws Throwable {
080    final TableName tableName = TableName.valueOf(name.getMethodName());
081
082    HTableDescriptor desc = new HTableDescriptor(tableName);
083    desc.addFamily(new HColumnDescriptor(TEST_FAMILY));
084    desc.addCoprocessor(org.apache.hadoop.hbase.coprocessor.ColumnAggregationEndpoint.class.getName());
085
086    createTable(desc);
087    verifyTable(tableName);
088  }
089
090  @Test
091  public void testDynamicCoprocessorTableEndpoint() throws Throwable {
092    final TableName tableName = TableName.valueOf(name.getMethodName());
093
094    HTableDescriptor desc = new HTableDescriptor(tableName);
095    desc.addFamily(new HColumnDescriptor(TEST_FAMILY));
096
097    createTable(desc);
098
099    desc.addCoprocessor(org.apache.hadoop.hbase.coprocessor.ColumnAggregationEndpoint.class.getName());
100    updateTable(desc);
101
102    verifyTable(tableName);
103  }
104
105  private static byte[][] makeN(byte[] base, int n) {
106    byte[][] ret = new byte[n][];
107    for (int i = 0; i < n; i++) {
108      ret[i] = Bytes.add(base, Bytes.toBytes(String.format("%02d", i)));
109    }
110    return ret;
111  }
112
113  private static Map<byte [], Long> sum(final Table table, final byte [] family,
114    final byte [] qualifier, final byte [] start, final byte [] end)
115      throws ServiceException, Throwable {
116  return table.coprocessorService(ColumnAggregationProtos.ColumnAggregationService.class,
117      start, end,
118    new Batch.Call<ColumnAggregationProtos.ColumnAggregationService, Long>() {
119      @Override
120      public Long call(ColumnAggregationProtos.ColumnAggregationService instance)
121      throws IOException {
122        CoprocessorRpcUtils.BlockingRpcCallback<ColumnAggregationProtos.SumResponse> rpcCallback =
123            new CoprocessorRpcUtils.BlockingRpcCallback<>();
124        ColumnAggregationProtos.SumRequest.Builder builder =
125          ColumnAggregationProtos.SumRequest.newBuilder();
126        builder.setFamily(ByteString.copyFrom(family));
127        if (qualifier != null && qualifier.length > 0) {
128          builder.setQualifier(ByteString.copyFrom(qualifier));
129        }
130        instance.sum(null, builder.build(), rpcCallback);
131        return rpcCallback.get().getSum();
132      }
133    });
134  }
135
136  private static final void createTable(HTableDescriptor desc) throws Exception {
137    Admin admin = TEST_UTIL.getAdmin();
138    admin.createTable(desc, new byte[][]{ROWS[rowSeperator1], ROWS[rowSeperator2]});
139    TEST_UTIL.waitUntilAllRegionsAssigned(desc.getTableName());
140    Table table = TEST_UTIL.getConnection().getTable(desc.getTableName());
141    try {
142      for (int i = 0; i < ROWSIZE; i++) {
143        Put put = new Put(ROWS[i]);
144        put.addColumn(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes(i));
145        table.put(put);
146      }
147    } finally {
148      table.close();
149    }
150  }
151
152  private static void updateTable(HTableDescriptor desc) throws Exception {
153    Admin admin = TEST_UTIL.getAdmin();
154    admin.disableTable(desc.getTableName());
155    admin.modifyTable(desc.getTableName(), desc);
156    admin.enableTable(desc.getTableName());
157  }
158
159  private static final void verifyTable(TableName tableName) throws Throwable {
160    Table table = TEST_UTIL.getConnection().getTable(tableName);
161    try {
162      Map<byte[], Long> results = sum(table, TEST_FAMILY, TEST_QUALIFIER, ROWS[0],
163        ROWS[ROWS.length-1]);
164      int sumResult = 0;
165      int expectedResult = 0;
166      for (Map.Entry<byte[], Long> e : results.entrySet()) {
167        sumResult += e.getValue();
168      }
169      for (int i = 0; i < ROWSIZE; i++) {
170        expectedResult += i;
171      }
172      assertEquals("Invalid result", expectedResult, sumResult);
173
174      // scan: for region 2 and region 3
175      results.clear();
176      results = sum(table, TEST_FAMILY, TEST_QUALIFIER, ROWS[rowSeperator1], ROWS[ROWS.length-1]);
177      sumResult = 0;
178      expectedResult = 0;
179      for (Map.Entry<byte[], Long> e : results.entrySet()) {
180        sumResult += e.getValue();
181      }
182      for (int i = rowSeperator1; i < ROWSIZE; i++) {
183        expectedResult += i;
184      }
185      assertEquals("Invalid result", expectedResult, sumResult);
186    } finally {
187      table.close();
188    }
189  }
190}