View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.coprocessor;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  
26  import org.apache.hadoop.hbase.TableName;
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.HColumnDescriptor;
29  import org.apache.hadoop.hbase.HTableDescriptor;
30  import org.apache.hadoop.hbase.MediumTests;
31  import org.apache.hadoop.hbase.client.Durability;
32  import org.apache.hadoop.hbase.client.HBaseAdmin;
33  import org.apache.hadoop.hbase.client.HTable;
34  import org.apache.hadoop.hbase.client.HTableInterface;
35  import org.apache.hadoop.hbase.client.Put;
36  import org.apache.hadoop.hbase.client.Result;
37  import org.apache.hadoop.hbase.client.ResultScanner;
38  import org.apache.hadoop.hbase.client.Scan;
39  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
40  import org.junit.AfterClass;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  /**
45   * Test that a coprocessor can open a connection and write to another table, inside a hook.
46   */
47  @Category(MediumTests.class)
48  public class TestOpenTableInCoprocessor {
49  
50    private static final TableName otherTable =
51        TableName.valueOf("otherTable");
52    private static final byte[] family = new byte[] { 'f' };
53  
54    private static boolean completed = false;
55  
56    /**
57     * Custom coprocessor that just copies the write to another table.
58     */
59    public static class SendToOtherTableCoprocessor extends BaseRegionObserver {
60  
61      @Override
62      public void prePut(ObserverContext<RegionCoprocessorEnvironment> e, Put put, WALEdit edit,
63          final Durability durability) throws IOException {
64        HTableInterface table = e.getEnvironment().getTable(otherTable);
65        Put p = new Put(new byte[] { 'a' });
66        p.add(family, null, new byte[] { 'a' });
67        table.put(put);
68        table.flushCommits();
69        completed = true;
70        table.close();
71      }
72  
73    }
74  
75    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
76  
77    @AfterClass
78    public static void cleanup() throws Exception {
79      UTIL.getHBaseAdmin().close();
80    }
81  
82    @Test
83    public void testCoprocessorCanCreateConnectionToRemoteTable() throws Throwable {
84      HTableDescriptor primary = new HTableDescriptor(TableName.valueOf("primary"));
85      primary.addFamily(new HColumnDescriptor(family));
86      // add our coprocessor
87      primary.addCoprocessor(SendToOtherTableCoprocessor.class.getName());
88  
89      HTableDescriptor other = new HTableDescriptor(otherTable);
90      other.addFamily(new HColumnDescriptor(family));
91      UTIL.startMiniCluster();
92  
93      HBaseAdmin admin = UTIL.getHBaseAdmin();
94      admin.createTable(primary);
95      admin.createTable(other);
96      admin.close();
97  
98      HTable table = new HTable(UTIL.getConfiguration(), "primary");
99      Put p = new Put(new byte[] { 'a' });
100     p.add(family, null, new byte[] { 'a' });
101     table.put(p);
102     table.flushCommits();
103     table.close();
104 
105     HTable target = new HTable(UTIL.getConfiguration(), otherTable);
106     assertTrue("Didn't complete update to target table!", completed);
107     assertEquals("Didn't find inserted row", 1, getKeyValueCount(target));
108     target.close();
109 
110     UTIL.shutdownMiniCluster();
111   }
112 
113   /**
114    * Count the number of keyvalue in the table. Scans all possible versions
115    * @param table table to scan
116    * @return number of keyvalues over all rows in the table
117    * @throws IOException
118    */
119   private int getKeyValueCount(HTable table) throws IOException {
120     Scan scan = new Scan();
121     scan.setMaxVersions(Integer.MAX_VALUE - 1);
122 
123     ResultScanner results = table.getScanner(scan);
124     int count = 0;
125     for (Result res : results) {
126       count += res.list().size();
127       System.out.println(count + ") " + res);
128     }
129     results.close();
130 
131     return count;
132   }
133 }