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