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  
20  package org.apache.hadoop.hbase.coprocessor;
21  
22  import java.io.IOException;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.fs.Path;
28  import org.apache.hadoop.hbase.Coprocessor;
29  import org.apache.hadoop.hbase.HBaseConfiguration;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.HColumnDescriptor;
32  import org.apache.hadoop.hbase.HRegionInfo;
33  import org.apache.hadoop.hbase.HTableDescriptor;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.client.Put;
36  import org.apache.hadoop.hbase.client.Durability;
37  import org.apache.hadoop.hbase.regionserver.HRegion;
38  import org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost;
39  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
40  import org.apache.hadoop.hbase.testclassification.SmallTests;
41  import org.apache.hadoop.hbase.util.Bytes;
42  import org.junit.experimental.categories.Category;
43  
44  @Category(SmallTests.class)
45  public class TestRegionObserverStacking extends TestCase {
46    private static HBaseTestingUtility TEST_UTIL
47      = new HBaseTestingUtility();
48    static final Path DIR = TEST_UTIL.getDataTestDir();
49  
50    public static class ObserverA extends BaseRegionObserver {
51      long id;
52      @Override
53      public void postPut(final ObserverContext<RegionCoprocessorEnvironment> c,
54          final Put put, final WALEdit edit,
55          final Durability durability)
56          throws IOException {
57        id = System.currentTimeMillis();
58        try {
59          Thread.sleep(10);
60        } catch (InterruptedException ex) {
61        }
62      }
63    }
64  
65    public static class ObserverB extends BaseRegionObserver {
66      long id;
67      @Override
68      public void postPut(final ObserverContext<RegionCoprocessorEnvironment> c,
69          final Put put, final WALEdit edit,
70          final Durability durability)
71          throws IOException {
72        id = System.currentTimeMillis();
73        try {
74          Thread.sleep(10);
75        } catch (InterruptedException ex) {
76        }
77      }
78    }
79  
80    public static class ObserverC extends BaseRegionObserver {
81      long id;
82  
83      @Override
84      public void postPut(final ObserverContext<RegionCoprocessorEnvironment> c,
85          final Put put, final WALEdit edit,
86          final Durability durability)
87          throws IOException {
88        id = System.currentTimeMillis();
89        try {
90          Thread.sleep(10);
91        } catch (InterruptedException ex) {
92        }
93      }
94    }
95  
96    HRegion initHRegion (byte [] tableName, String callingMethod,
97        Configuration conf, byte [] ... families) throws IOException {
98      HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
99      for(byte [] family : families) {
100       htd.addFamily(new HColumnDescriptor(family));
101     }
102     HRegionInfo info = new HRegionInfo(htd.getTableName(), null, null, false);
103     Path path = new Path(DIR + callingMethod);
104     HRegion r = HRegion.createHRegion(info, path, conf, htd);
105     // this following piece is a hack. currently a coprocessorHost
106     // is secretly loaded at OpenRegionHandler. we don't really
107     // start a region server here, so just manually create cphost
108     // and set it to region.
109     RegionCoprocessorHost host = new RegionCoprocessorHost(r, null, conf);
110     r.setCoprocessorHost(host);
111     return r;
112   }
113 
114   public void testRegionObserverStacking() throws Exception {
115     byte[] ROW = Bytes.toBytes("testRow");
116     byte[] TABLE = Bytes.toBytes(this.getClass().getSimpleName());
117     byte[] A = Bytes.toBytes("A");
118     byte[][] FAMILIES = new byte[][] { A } ;
119 
120     Configuration conf = HBaseConfiguration.create();
121     HRegion region = initHRegion(TABLE, getClass().getName(),
122       conf, FAMILIES);
123     RegionCoprocessorHost h = region.getCoprocessorHost();
124     h.load(ObserverA.class, Coprocessor.PRIORITY_HIGHEST, conf);
125     h.load(ObserverB.class, Coprocessor.PRIORITY_USER, conf);
126     h.load(ObserverC.class, Coprocessor.PRIORITY_LOWEST, conf);
127 
128     Put put = new Put(ROW);
129     put.add(A, A, A);
130     region.put(put);
131 
132     Coprocessor c = h.findCoprocessor(ObserverA.class.getName());
133     long idA = ((ObserverA)c).id;
134     c = h.findCoprocessor(ObserverB.class.getName());
135     long idB = ((ObserverB)c).id;
136     c = h.findCoprocessor(ObserverC.class.getName());
137     long idC = ((ObserverC)c).id;
138 
139     assertTrue(idA < idB);
140     assertTrue(idB < idC);
141   }
142 
143 }
144