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.rest;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.IOException;
27  import java.net.InetSocketAddress;
28  import java.util.ArrayList;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  import javax.xml.bind.JAXBContext;
33  import javax.xml.bind.JAXBException;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.apache.hadoop.hbase.HBaseTestingUtility;
38  import org.apache.hadoop.hbase.HColumnDescriptor;
39  import org.apache.hadoop.hbase.HRegionInfo;
40  import org.apache.hadoop.hbase.HRegionLocation;
41  import org.apache.hadoop.hbase.HTableDescriptor;
42  import org.apache.hadoop.hbase.KeyValue;
43  import org.apache.hadoop.hbase.ServerName;
44  import org.apache.hadoop.hbase.TableName;
45  import org.apache.hadoop.hbase.Waiter;
46  import org.apache.hadoop.hbase.client.Admin;
47  import org.apache.hadoop.hbase.client.Connection;
48  import org.apache.hadoop.hbase.client.Durability;
49  import org.apache.hadoop.hbase.client.Put;
50  import org.apache.hadoop.hbase.client.RegionLocator;
51  import org.apache.hadoop.hbase.client.Table;
52  import org.apache.hadoop.hbase.rest.client.Client;
53  import org.apache.hadoop.hbase.rest.client.Cluster;
54  import org.apache.hadoop.hbase.rest.client.Response;
55  import org.apache.hadoop.hbase.rest.model.TableInfoModel;
56  import org.apache.hadoop.hbase.rest.model.TableListModel;
57  import org.apache.hadoop.hbase.rest.model.TableModel;
58  import org.apache.hadoop.hbase.rest.model.TableRegionModel;
59  import org.apache.hadoop.hbase.testclassification.MediumTests;
60  import org.apache.hadoop.hbase.util.Bytes;
61  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
62  import org.junit.AfterClass;
63  import org.junit.BeforeClass;
64  import org.junit.Test;
65  import org.junit.experimental.categories.Category;
66  
67  @Category(MediumTests.class)
68  public class TestTableResource {
69    private static final Log LOG = LogFactory.getLog(TestTableResource.class);
70  
71    private static TableName TABLE = TableName.valueOf("TestTableResource");
72    private static String COLUMN_FAMILY = "test";
73    private static String COLUMN = COLUMN_FAMILY + ":qualifier";
74    private static List<HRegionLocation> regionMap;
75  
76    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
77    private static final HBaseRESTTestingUtility REST_TEST_UTIL =
78      new HBaseRESTTestingUtility();
79    private static Client client;
80    private static JAXBContext context;
81  
82    @BeforeClass
83    public static void setUpBeforeClass() throws Exception {
84      TEST_UTIL.startMiniCluster(3);
85      REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
86      client = new Client(new Cluster().add("localhost",
87        REST_TEST_UTIL.getServletPort()));
88      context = JAXBContext.newInstance(
89          TableModel.class,
90          TableInfoModel.class,
91          TableListModel.class,
92          TableRegionModel.class);
93      Admin admin = TEST_UTIL.getHBaseAdmin();
94      if (admin.tableExists(TABLE)) {
95        return;
96      }
97      HTableDescriptor htd = new HTableDescriptor(TABLE);
98      htd.addFamily(new HColumnDescriptor(COLUMN_FAMILY));
99      admin.createTable(htd);
100     byte[] k = new byte[3];
101     byte [][] famAndQf = KeyValue.parseColumn(Bytes.toBytes(COLUMN));
102     List<Put> puts = new ArrayList<>();
103     for (byte b1 = 'a'; b1 < 'z'; b1++) {
104       for (byte b2 = 'a'; b2 < 'z'; b2++) {
105         for (byte b3 = 'a'; b3 < 'z'; b3++) {
106           k[0] = b1;
107           k[1] = b2;
108           k[2] = b3;
109           Put put = new Put(k);
110           put.setDurability(Durability.SKIP_WAL);
111           put.add(famAndQf[0], famAndQf[1], k);
112           puts.add(put);
113         }
114       }
115     }
116     Connection connection = TEST_UTIL.getConnection();
117     
118     Table table =  connection.getTable(TABLE);
119     table.put(puts);
120     table.close();
121     // get the initial layout (should just be one region)
122     
123     final RegionLocator regionLocator = connection.getRegionLocator(TABLE);
124     List<HRegionLocation> m = regionLocator.getAllRegionLocations();
125     assertEquals(m.size(), 1);
126     // tell the master to split the table
127     admin.split(TABLE);
128 
129     // give some time for the split to happen
130     TEST_UTIL.waitFor(15 * 1000, 250, new Waiter.Predicate<IOException>() {
131       @Override
132       public boolean evaluate() throws IOException {
133         List<HRegionLocation> regionLocations = regionLocator.getAllRegionLocations();
134         return regionLocations != null &&
135             regionLocations.size() == 2 && regionLocations.get(0).getServerName() != null &&
136             regionLocations.get(1).getServerName() != null;
137       }
138     });
139     m = regionLocator.getAllRegionLocations();
140 
141     // should have two regions now
142     assertEquals(2, m.size());
143     regionMap = m;
144     LOG.info("regions: " + regionMap);
145     regionLocator.close();
146   }
147 
148   @AfterClass
149   public static void tearDownAfterClass() throws Exception {
150     REST_TEST_UTIL.shutdownServletContainer();
151     TEST_UTIL.shutdownMiniCluster();
152   }
153 
154   private static void checkTableList(TableListModel model) {
155     boolean found = false;
156     Iterator<TableModel> tables = model.getTables().iterator();
157     assertTrue(tables.hasNext());
158     while (tables.hasNext()) {
159       TableModel table = tables.next();
160       if (table.getName().equals(TABLE.getNameAsString())) {
161         found = true;
162         break;
163       }
164     }
165     assertTrue(found);
166   }
167 
168   void checkTableInfo(TableInfoModel model) {
169     assertEquals(model.getName(), TABLE.getNameAsString());
170     Iterator<TableRegionModel> regions = model.getRegions().iterator();
171     assertTrue(regions.hasNext());
172     while (regions.hasNext()) {
173       TableRegionModel region = regions.next();
174       boolean found = false;
175       for (HRegionLocation e: regionMap) {
176         HRegionInfo hri = e.getRegionInfo();
177         String hriRegionName = hri.getRegionNameAsString();
178         String regionName = region.getName();
179         if (hriRegionName.equals(regionName)) {
180           found = true;
181           byte[] startKey = hri.getStartKey();
182           byte[] endKey = hri.getEndKey();
183           ServerName serverName = e.getServerName();
184           InetSocketAddress sa =
185               new InetSocketAddress(serverName.getHostname(), serverName.getPort());
186           String location = sa.getHostName() + ":" +
187             Integer.valueOf(sa.getPort());
188           assertEquals(hri.getRegionId(), region.getId());
189           assertTrue(Bytes.equals(startKey, region.getStartKey()));
190           assertTrue(Bytes.equals(endKey, region.getEndKey()));
191           assertEquals(location, region.getLocation());
192           break;
193         }
194       }
195       assertTrue(found);
196     }
197   }
198 
199   @Test
200   public void testTableListText() throws IOException {
201     Response response = client.get("/", Constants.MIMETYPE_TEXT);
202     assertEquals(200, response.getCode());
203     assertEquals(Constants.MIMETYPE_TEXT, response.getHeader("content-type"));
204   }
205 
206   @Test
207   public void testTableListXML() throws IOException, JAXBException {
208     Response response = client.get("/", Constants.MIMETYPE_XML);
209     assertEquals(200, response.getCode());
210     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
211     TableListModel model = (TableListModel)
212       context.createUnmarshaller()
213         .unmarshal(new ByteArrayInputStream(response.getBody()));
214     checkTableList(model);
215   }
216 
217   @Test
218   public void testTableListJSON() throws IOException {
219     Response response = client.get("/", Constants.MIMETYPE_JSON);
220     assertEquals(200, response.getCode());
221     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
222   }
223 
224   @Test
225   public void testTableListPB() throws IOException, JAXBException {
226     Response response = client.get("/", Constants.MIMETYPE_PROTOBUF);
227     assertEquals(200, response.getCode());
228     assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
229     TableListModel model = new TableListModel();
230     model.getObjectFromMessage(response.getBody());
231     checkTableList(model);
232     response = client.get("/", Constants.MIMETYPE_PROTOBUF_IETF);
233     assertEquals(200, response.getCode());
234     assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
235     model = new TableListModel();
236     model.getObjectFromMessage(response.getBody());
237     checkTableList(model);
238   }
239 
240   @Test
241   public void testTableInfoText() throws IOException {
242     Response response = client.get("/" + TABLE + "/regions", Constants.MIMETYPE_TEXT);
243     assertEquals(200, response.getCode());
244     assertEquals(Constants.MIMETYPE_TEXT, response.getHeader("content-type"));
245   }
246 
247   @Test
248   public void testTableInfoXML() throws IOException, JAXBException {
249     Response response = client.get("/" + TABLE + "/regions",  Constants.MIMETYPE_XML);
250     assertEquals(200, response.getCode());
251     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
252     TableInfoModel model = (TableInfoModel)
253       context.createUnmarshaller()
254         .unmarshal(new ByteArrayInputStream(response.getBody()));
255     checkTableInfo(model);
256   }
257 
258   @Test
259   public void testTableInfoJSON() throws IOException {
260     Response response = client.get("/" + TABLE + "/regions", Constants.MIMETYPE_JSON);
261     assertEquals(200, response.getCode());
262     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
263   }
264 
265   @Test
266   public void testTableInfoPB() throws IOException, JAXBException {
267     Response response = client.get("/" + TABLE + "/regions", Constants.MIMETYPE_PROTOBUF);
268     assertEquals(200, response.getCode());
269     assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
270     TableInfoModel model = new TableInfoModel();
271     model.getObjectFromMessage(response.getBody());
272     checkTableInfo(model);
273     response = client.get("/" + TABLE + "/regions", Constants.MIMETYPE_PROTOBUF_IETF);
274     assertEquals(200, response.getCode());
275     assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
276     model = new TableInfoModel();
277     model.getObjectFromMessage(response.getBody());
278     checkTableInfo(model);
279   }
280 
281 }