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.master.http;
019
020import static org.apache.hadoop.hbase.client.hamcrest.BytesMatchers.bytesAsStringBinary;
021import static org.hamcrest.MatcherAssert.assertThat;
022import static org.hamcrest.Matchers.allOf;
023import static org.hamcrest.Matchers.contains;
024import static org.hamcrest.Matchers.equalTo;
025import static org.hamcrest.Matchers.hasProperty;
026import static org.hamcrest.Matchers.startsWith;
027import static org.mockito.Mockito.mock;
028import static org.mockito.Mockito.when;
029import java.util.List;
030import java.util.concurrent.CompletableFuture;
031import javax.servlet.http.HttpServletRequest;
032import org.apache.hadoop.hbase.ClearUserNamespacesAndTablesRule;
033import org.apache.hadoop.hbase.ConnectionRule;
034import org.apache.hadoop.hbase.HBaseClassTestRule;
035import org.apache.hadoop.hbase.MiniClusterRule;
036import org.apache.hadoop.hbase.NamespaceDescriptor;
037import org.apache.hadoop.hbase.TableName;
038import org.apache.hadoop.hbase.client.AsyncAdmin;
039import org.apache.hadoop.hbase.client.AsyncConnection;
040import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
041import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
042import org.apache.hadoop.hbase.client.TableDescriptor;
043import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
044import org.apache.hadoop.hbase.master.RegionState;
045import org.apache.hadoop.hbase.testclassification.MasterTests;
046import org.apache.hadoop.hbase.testclassification.MediumTests;
047import org.apache.hadoop.hbase.util.RegionSplitter;
048import org.junit.Before;
049import org.junit.ClassRule;
050import org.junit.Rule;
051import org.junit.Test;
052import org.junit.experimental.categories.Category;
053import org.junit.rules.RuleChain;
054import org.junit.rules.TestName;
055import org.junit.rules.TestRule;
056import org.apache.hbase.thirdparty.org.apache.commons.collections4.IterableUtils;
057
058/**
059 * Cluster-backed correctness tests for the functionality provided by {@link MetaBrowser}.
060 */
061@Category({ MasterTests.class, MediumTests.class})
062public class TestMetaBrowser {
063
064  @ClassRule
065  public static final HBaseClassTestRule testRule =
066    HBaseClassTestRule.forClass(TestMetaBrowser.class);
067  @ClassRule
068  public static final MiniClusterRule miniClusterRule = MiniClusterRule.newBuilder().build();
069
070  private final ConnectionRule connectionRule =
071    new ConnectionRule(miniClusterRule::createConnection);
072  private final ClearUserNamespacesAndTablesRule clearUserNamespacesAndTablesRule =
073    new ClearUserNamespacesAndTablesRule(connectionRule::getConnection);
074
075  @Rule
076  public TestRule rule = RuleChain.outerRule(connectionRule)
077    .around(clearUserNamespacesAndTablesRule);
078
079  @Rule
080  public TestName testNameRule = new TestName();
081
082  private AsyncConnection connection;
083  private AsyncAdmin admin;
084
085  @Before
086  public void before() {
087    connection = connectionRule.getConnection();
088    admin = connection.getAdmin();
089  }
090
091  @Test
092  public void noFilters() {
093    final String namespaceName = testNameRule.getMethodName();
094    final TableName a = TableName.valueOf("a");
095    final TableName b = TableName.valueOf(namespaceName, "b");
096
097    CompletableFuture.allOf(
098      createTable(a),
099      createNamespace(namespaceName).thenCompose(_void -> createTable(b, 2)))
100      .join();
101
102    final HttpServletRequest request = new MockRequestBuilder().build();
103    final List<RegionReplicaInfo> rows;
104    try (final MetaBrowser.Results results = new MetaBrowser(connection, request).getResults()) {
105      rows = IterableUtils.toList(results);
106    }
107    assertThat(rows, contains(
108      hasProperty("row", bytesAsStringBinary(startsWith(a + ",,"))),
109      hasProperty("row", bytesAsStringBinary(startsWith(b + ",,"))),
110      hasProperty("row", bytesAsStringBinary(startsWith(b + ",80000000")))));
111  }
112
113  @Test
114  public void limit() {
115    final String tableName = testNameRule.getMethodName();
116    createTable(TableName.valueOf(tableName), 8).join();
117
118    final HttpServletRequest request = new MockRequestBuilder()
119      .setLimit(5)
120      .build();
121    final List<RegionReplicaInfo> rows;
122    try (final MetaBrowser.Results results = new MetaBrowser(connection, request).getResults()) {
123      rows = IterableUtils.toList(results);
124    }
125    assertThat(rows, contains(
126      hasProperty("row", bytesAsStringBinary(startsWith(tableName + ",,"))),
127      hasProperty("row", bytesAsStringBinary(startsWith(tableName + ",20000000"))),
128      hasProperty("row", bytesAsStringBinary(startsWith(tableName + ",40000000"))),
129      hasProperty("row", bytesAsStringBinary(startsWith(tableName + ",60000000"))),
130      hasProperty("row", bytesAsStringBinary(startsWith(tableName + ",80000000")))));
131  }
132
133  @Test
134  public void regionStateFilter() {
135    final String namespaceName = testNameRule.getMethodName();
136    final TableName foo = TableName.valueOf(namespaceName, "foo");
137    final TableName bar = TableName.valueOf(namespaceName, "bar");
138
139    createNamespace(namespaceName)
140      .thenCompose(_void1 -> CompletableFuture.allOf(
141        createTable(foo, 2).thenCompose(_void2 -> admin.disableTable(foo)),
142        createTable(bar, 2)))
143      .join();
144
145    final HttpServletRequest request = new MockRequestBuilder()
146      .setLimit(10_000)
147      .setRegionState(RegionState.State.OPEN)
148      .setTable(namespaceName)
149      .build();
150    final List<RegionReplicaInfo> rows;
151    try (final MetaBrowser.Results results = new MetaBrowser(connection, request).getResults()) {
152      rows = IterableUtils.toList(results);
153    }
154    assertThat(rows, contains(
155      hasProperty("row", bytesAsStringBinary(startsWith(bar.toString() + ",,"))),
156      hasProperty("row", bytesAsStringBinary(startsWith(bar.toString() + ",80000000")))));
157  }
158
159  @Test
160  public void scanTableFilter() {
161    final String namespaceName = testNameRule.getMethodName();
162    final TableName a = TableName.valueOf("a");
163    final TableName b = TableName.valueOf(namespaceName, "b");
164
165    CompletableFuture.allOf(
166      createTable(a),
167      createNamespace(namespaceName).thenCompose(_void -> createTable(b, 2)))
168      .join();
169
170    final HttpServletRequest request = new MockRequestBuilder()
171      .setTable(namespaceName)
172      .build();
173    final List<RegionReplicaInfo> rows;
174    try (final MetaBrowser.Results results = new MetaBrowser(connection, request).getResults()) {
175      rows = IterableUtils.toList(results);
176    }
177    assertThat(rows, contains(
178      hasProperty("row", bytesAsStringBinary(startsWith(b + ",,"))),
179      hasProperty("row", bytesAsStringBinary(startsWith(b + ",80000000")))));
180  }
181
182  @Test
183  public void paginateWithReplicas() {
184    final String namespaceName = testNameRule.getMethodName();
185    final TableName a = TableName.valueOf("a");
186    final TableName b = TableName.valueOf(namespaceName, "b");
187
188    CompletableFuture.allOf(
189      createTableWithReplicas(a, 2),
190      createNamespace(namespaceName).thenCompose(_void -> createTable(b, 2)))
191      .join();
192
193    final HttpServletRequest request1 = new MockRequestBuilder()
194      .setLimit(2)
195      .build();
196    final List<RegionReplicaInfo> rows1;
197    try (final MetaBrowser.Results results = new MetaBrowser(connection, request1).getResults()) {
198      rows1 = IterableUtils.toList(results);
199    }
200    assertThat(rows1, contains(
201      allOf(
202        hasProperty("regionName", bytesAsStringBinary(startsWith(a + ",,"))),
203        hasProperty("replicaId", equalTo(0))),
204      allOf(
205        hasProperty("regionName", bytesAsStringBinary(startsWith(a + ",,"))),
206        hasProperty("replicaId", equalTo(1)))));
207
208    final HttpServletRequest request2 = new MockRequestBuilder()
209      .setLimit(2)
210      .setStart(MetaBrowser.buildStartParamFrom(rows1.get(rows1.size() - 1).getRow()))
211      .build();
212    final List<RegionReplicaInfo> rows2;
213    try (final MetaBrowser.Results results = new MetaBrowser(connection, request2).getResults()) {
214      rows2 = IterableUtils.toList(results);
215    }
216    assertThat(rows2, contains(
217      hasProperty("row", bytesAsStringBinary(startsWith(b + ",,"))),
218      hasProperty("row", bytesAsStringBinary(startsWith(b + ",80000000")))));
219  }
220
221  @Test
222  public void paginateWithTableFilter() {
223    final String namespaceName = testNameRule.getMethodName();
224    final TableName a = TableName.valueOf("a");
225    final TableName b = TableName.valueOf(namespaceName, "b");
226
227    CompletableFuture.allOf(
228      createTable(a),
229      createNamespace(namespaceName).thenCompose(_void -> createTable(b, 5)))
230      .join();
231
232    final HttpServletRequest request1 = new MockRequestBuilder()
233      .setLimit(2)
234      .setTable(namespaceName)
235      .build();
236    final List<RegionReplicaInfo> rows1;
237    try (final MetaBrowser.Results results = new MetaBrowser(connection, request1).getResults()) {
238      rows1 = IterableUtils.toList(results);
239    }
240    assertThat(rows1, contains(
241      hasProperty("row", bytesAsStringBinary(startsWith(b + ",,"))),
242      hasProperty("row", bytesAsStringBinary(startsWith(b + ",33333333")))));
243
244    final HttpServletRequest request2 = new MockRequestBuilder()
245      .setLimit(2)
246      .setTable(namespaceName)
247      .setStart(MetaBrowser.buildStartParamFrom(rows1.get(rows1.size() - 1).getRow()))
248      .build();
249    final List<RegionReplicaInfo> rows2;
250    try (final MetaBrowser.Results results = new MetaBrowser(connection, request2).getResults()) {
251      rows2 = IterableUtils.toList(results);
252    }
253    assertThat(rows2, contains(
254      hasProperty("row", bytesAsStringBinary(startsWith(b + ",66666666"))),
255      hasProperty("row", bytesAsStringBinary(startsWith(b + ",99999999")))));
256
257    final HttpServletRequest request3 = new MockRequestBuilder()
258      .setLimit(2)
259      .setTable(namespaceName)
260      .setStart(MetaBrowser.buildStartParamFrom(rows2.get(rows2.size() - 1).getRow()))
261      .build();
262    final List<RegionReplicaInfo> rows3;
263    try (final MetaBrowser.Results results = new MetaBrowser(connection, request3).getResults()) {
264      rows3 = IterableUtils.toList(results);
265    }
266    assertThat(rows3, contains(
267      hasProperty("row", bytesAsStringBinary(startsWith(b + ",cccccccc")))));
268  }
269
270  private ColumnFamilyDescriptor columnFamilyDescriptor() {
271    return ColumnFamilyDescriptorBuilder.of("f1");
272  }
273
274  private TableDescriptor tableDescriptor(final TableName tableName) {
275    return TableDescriptorBuilder.newBuilder(tableName)
276      .setColumnFamily(columnFamilyDescriptor())
277      .build();
278  }
279
280  private TableDescriptor tableDescriptor(final TableName tableName, final int replicaCount) {
281    return TableDescriptorBuilder.newBuilder(tableName)
282      .setRegionReplication(replicaCount)
283      .setColumnFamily(columnFamilyDescriptor())
284      .build();
285  }
286
287  private CompletableFuture<Void> createTable(final TableName tableName) {
288    return admin.createTable(tableDescriptor(tableName));
289  }
290
291  private CompletableFuture<Void> createTable(final TableName tableName, final int splitCount) {
292    return admin.createTable(
293      tableDescriptor(tableName),
294      new RegionSplitter.HexStringSplit().split(splitCount));
295  }
296
297  private CompletableFuture<Void> createTableWithReplicas(final TableName tableName,
298    final int replicaCount) {
299    return admin.createTable(tableDescriptor(tableName, replicaCount));
300  }
301
302  private CompletableFuture<Void> createNamespace(final String namespace) {
303    final NamespaceDescriptor descriptor = NamespaceDescriptor.create(namespace).build();
304    return admin.createNamespace(descriptor);
305  }
306
307  /**
308   * Helper for mocking an {@link HttpServletRequest} relevant to the test.
309   */
310  static class MockRequestBuilder {
311
312    private String limit = null;
313    private String regionState = null;
314    private String start = null;
315    private String table = null;
316
317    public MockRequestBuilder setLimit(final int value) {
318      this.limit = Integer.toString(value);
319      return this;
320    }
321
322    public MockRequestBuilder setLimit(final String value) {
323      this.limit = value;
324      return this;
325    }
326
327    public MockRequestBuilder setRegionState(final RegionState.State value) {
328      this.regionState = value.toString();
329      return this;
330    }
331
332    public MockRequestBuilder setRegionState(final String value) {
333      this.regionState = value;
334      return this;
335    }
336
337    public MockRequestBuilder setStart(final String value) {
338      this.start = value;
339      return this;
340    }
341
342    public MockRequestBuilder setTable(final String value) {
343      this.table = value;
344      return this;
345    }
346
347    public HttpServletRequest build() {
348      final HttpServletRequest request = mock(HttpServletRequest.class);
349      when(request.getRequestURI()).thenReturn("/table.jsp");
350      when(request.getParameter("name")).thenReturn("hbase%3Ameta");
351
352      when(request.getParameter("scan_limit")).thenReturn(limit);
353      when(request.getParameter("scan_region_state")).thenReturn(regionState);
354      when(request.getParameter("scan_start")).thenReturn(start);
355      when(request.getParameter("scan_table")).thenReturn(table);
356
357      return request;
358    }
359  }
360}