ndimiduk commented on a change in pull request #1020: HBASE-23653 Expose 
content of meta table in web ui
URL: https://github.com/apache/hbase/pull/1020#discussion_r366467988
 
 

 ##########
 File path: 
hbase-server/src/main/java/org/apache/hadoop/hbase/master/webapp/MetaBrowser.java
 ##########
 @@ -0,0 +1,378 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.master.webapp;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.stream.StreamSupport;
+import javax.servlet.http.HttpServletRequest;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.commons.lang3.builder.ToStringStyle;
+import org.apache.hadoop.hbase.CompareOperator;
+import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.client.AdvancedScanResultConsumer;
+import org.apache.hadoop.hbase.client.AsyncConnection;
+import org.apache.hadoop.hbase.client.AsyncTable;
+import org.apache.hadoop.hbase.client.ResultScanner;
+import org.apache.hadoop.hbase.client.Scan;
+import org.apache.hadoop.hbase.filter.Filter;
+import org.apache.hadoop.hbase.filter.FilterList;
+import org.apache.hadoop.hbase.filter.PrefixFilter;
+import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
+import org.apache.hadoop.hbase.master.RegionState;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.yetus.audience.InterfaceAudience;
+import org.apache.hbase.thirdparty.com.google.common.collect.Iterators;
+import 
org.apache.hbase.thirdparty.io.netty.handler.codec.http.QueryStringEncoder;
+
+/**
+ * A support class for the "Meta Entries" section in
+ * {@code resources/hbase-webapps/master/table.jsp}.
+ */
+@InterfaceAudience.Private
+public class MetaBrowser {
+  public static final String NAME_PARAM = "name";
+  public static final String SCAN_LIMIT_PARAM = "scan_limit";
+  public static final String SCAN_REGION_STATE_PARAM = "scan_region_state";
+  public static final String SCAN_START_PARAM = "scan_start";
+  public static final String SCAN_TABLE_PARAM = "scan_table";
+
+  public static final int SCAN_LIMIT_DEFAULT = 10;
+  public static final int SCAN_LIMIT_MAX = 10_000;
+
+  private final AsyncConnection connection;
+  private final HttpServletRequest request;
+  private final List<String> errorMessages;
+  private final String name;
+  private final Integer scanLimit;
+  private final RegionState.State scanRegionState;
+  private final byte[] scanStart;
+  private final TableName scanTable;
+
+  public MetaBrowser(final AsyncConnection connection, final 
HttpServletRequest request) {
+    this.connection = connection;
+    this.request = request;
+    this.errorMessages = new LinkedList<>();
+    this.name = resolveName(request);
+    this.scanLimit = resolveScanLimit(request);
+    this.scanRegionState = resolveScanRegionState(request);
+    this.scanStart = resolveScanStart(request);
+    this.scanTable = resolveScanTable(request);
+  }
+
+  public List<String> getErrorMessages() {
+    return errorMessages;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public Integer getScanLimit() {
+    return scanLimit;
+  }
+
+  public byte[] getScanStart() {
+    return scanStart;
+  }
+
+  public RegionState.State getScanRegionState() {
+    return scanRegionState;
+  }
+
+  public TableName getScanTable() {
+    return scanTable;
+  }
+
+  public Results getResults() {
+    final AsyncTable<AdvancedScanResultConsumer> asyncTable =
+      connection.getTable(TableName.META_TABLE_NAME);
+    return new Results(asyncTable.getScanner(buildScan()));
+  }
+
+  @Override
+  public String toString() {
+    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
+      .append("scanStart", scanStart)
+      .append("scanLimit", scanLimit)
+      .append("scanTable", scanTable)
+      .append("scanRegionState", scanRegionState)
+      .toString();
+  }
+
+  private static String resolveName(final HttpServletRequest request) {
+    return resolveRequestParameter(request, NAME_PARAM);
+  }
+
+  private Integer resolveScanLimit(final HttpServletRequest request) {
+    final String requestValueStr = resolveRequestParameter(request, 
SCAN_LIMIT_PARAM);
+    if (StringUtils.isBlank(requestValueStr)) {
+      return null;
+    }
+
+    final Integer requestValue = tryParseInt(requestValueStr);
+    if (requestValue == null) {
+      errorMessages.add(buildScanLimitMalformedErrorMessage(requestValueStr));
+      return null;
+    }
+    if (requestValue <= 0) {
+      errorMessages.add(buildScanLimitLTEZero(requestValue));
+      return SCAN_LIMIT_DEFAULT;
+    }
+
+    final int truncatedValue = Math.min(requestValue, SCAN_LIMIT_MAX);
+    if (requestValue != truncatedValue) {
+      errorMessages.add(buildScanLimitExceededErrorMessage(requestValue));
+    }
+    return truncatedValue;
+  }
+
+  private RegionState.State resolveScanRegionState(final HttpServletRequest 
request) {
+    final String requestValueStr = resolveRequestParameter(request, 
SCAN_REGION_STATE_PARAM);
+    if (requestValueStr == null) {
+      return null;
+    }
+    final RegionState.State requestValue = tryValueOf(RegionState.State.class, 
requestValueStr);
+    if (requestValue == null) {
+      
errorMessages.add(buildScanRegionStateMalformedErrorMessage(requestValueStr));
+      return null;
+    }
+    return requestValue;
+  }
+
+  private static byte[] resolveScanStart(final HttpServletRequest request) {
+    final String requestValue = resolveRequestParameter(request, 
SCAN_START_PARAM);
+    if (requestValue == null) {
+      return null;
+    }
+    return Bytes.toBytesBinary(requestValue);
+  }
+
+  private static TableName resolveScanTable(final HttpServletRequest request) {
+    final String requestValue = resolveRequestParameter(request, 
SCAN_TABLE_PARAM);
+    if (requestValue == null) {
+      return null;
+    }
+    return TableName.valueOf(requestValue);
+  }
+
+  private static String resolveRequestParameter(final HttpServletRequest 
request,
+    final String param) {
+    if (request == null) {
+      return null;
+    }
+    final String requestValueStrEnc = request.getParameter(param);
+    if (StringUtils.isBlank(requestValueStrEnc)) {
+      return null;
+    }
+    return urlDecode(requestValueStrEnc);
+  }
+
+  private static Filter buildTableFilter(final TableName tableName) {
+    return new PrefixFilter(tableName.toBytes());
+  }
+
+  private static Filter buildScanRegionStateFilter(final RegionState.State 
state) {
+    return new SingleColumnValueFilter(
+      HConstants.CATALOG_FAMILY,
+      HConstants.STATE_QUALIFIER,
+      CompareOperator.EQUAL,
+      // use the same serialization strategy as found in 
MetaTableAccessor#addRegionStateToPut
+      Bytes.toBytes(state.name()));
+  }
+
+  private Filter buildScanFilter() {
+    if (scanTable == null && scanRegionState == null) {
+      return null;
+    }
+
+    final List<Filter> filters = new ArrayList<>(2);
+    if (scanTable != null) {
+      filters.add(buildTableFilter(scanTable));
+    }
+    if (scanRegionState != null) {
+      filters.add(buildScanRegionStateFilter(scanRegionState));
+    }
+    if (filters.size() == 1) {
+      return filters.get(0);
+    }
+    return new FilterList(FilterList.Operator.MUST_PASS_ALL, filters);
+  }
+
+  private Scan buildScan() {
+    final Scan metaScan = new Scan()
+      .addFamily(HConstants.CATALOG_FAMILY)
+      .readVersions(1)
+      .setLimit((scanLimit != null ? scanLimit : SCAN_LIMIT_DEFAULT) + 1);
 
 Review comment:
   Rgr.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to