Skip to content

Commit ff5face

Browse files
committed
HBASE-26192 Master UI hbck should provide a JSON formatted output option
1 parent 540fe8b commit ff5face

3 files changed

Lines changed: 386 additions & 0 deletions

File tree

hbase-server/src/main/java/org/apache/hadoop/hbase/master/HMaster.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
import org.apache.hadoop.hbase.master.cleaner.ReplicationBarrierCleaner;
134134
import org.apache.hadoop.hbase.master.cleaner.SnapshotCleanerChore;
135135
import org.apache.hadoop.hbase.master.http.MasterDumpServlet;
136+
import org.apache.hadoop.hbase.master.http.MasterHbckServlet;
136137
import org.apache.hadoop.hbase.master.http.MasterRedirectServlet;
137138
import org.apache.hadoop.hbase.master.http.MasterStatusServlet;
138139
import org.apache.hadoop.hbase.master.http.api_v1.ResourceConfigFactory;
@@ -707,6 +708,7 @@ protected MasterRpcServices createRpcServices() throws IOException {
707708
protected void configureInfoServer(InfoServer infoServer) {
708709
infoServer.addUnprivilegedServlet("master-status", "/master-status", MasterStatusServlet.class);
709710
infoServer.addUnprivilegedServlet("api_v1", "/api/v1/*", buildApiV1Servlet());
711+
infoServer.addUnprivilegedServlet("hbck", "/hbck", MasterHbckServlet.class);
710712

711713
infoServer.setAttribute(MASTER, this);
712714
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.master.http;
19+
20+
import java.io.IOException;
21+
import java.io.PrintWriter;
22+
import java.util.HashMap;
23+
import java.util.List;
24+
import java.util.Map;
25+
import javax.servlet.ServletException;
26+
import javax.servlet.http.HttpServlet;
27+
import javax.servlet.http.HttpServletRequest;
28+
import javax.servlet.http.HttpServletResponse;
29+
import org.apache.hadoop.fs.Path;
30+
import org.apache.hadoop.hbase.ServerName;
31+
import org.apache.hadoop.hbase.client.RegionInfo;
32+
import org.apache.hadoop.hbase.master.HMaster;
33+
import org.apache.hadoop.hbase.master.HbckChore;
34+
import org.apache.hadoop.hbase.master.http.gson.GsonFactory;
35+
import org.apache.hadoop.hbase.master.janitor.CatalogJanitor;
36+
import org.apache.hadoop.hbase.master.janitor.Report;
37+
import org.apache.hadoop.hbase.util.Pair;
38+
import org.apache.yetus.audience.InterfaceAudience;
39+
import org.slf4j.Logger;
40+
import org.slf4j.LoggerFactory;
41+
42+
import org.apache.hbase.thirdparty.com.google.gson.Gson;
43+
44+
@InterfaceAudience.Private
45+
public class MasterHbckServlet extends HttpServlet {
46+
47+
public static final String START_TIMESTAMP = "start_timestamp";
48+
public static final String END_TIMESTAMP = "end_timestamp";
49+
public static final String INCONSISTENT_REGIONS = "inconsistent_regions";
50+
public static final String ORPHAN_REGIONS_ON_RS = "orphan_regions_on_rs";
51+
public static final String ORPHAN_REGIONS_ON_FS = "orphan_regions_on_fs";
52+
public static final String HOLES = "holes";
53+
public static final String OVERLAPS = "overlaps";
54+
public static final String UNKNOWN_SERVERS = "unknown_servers";
55+
public static final String EMPTY_REGIONINFO = "empty_regioninfo";
56+
57+
private static final long serialVersionUID = 1L;
58+
private static final Logger LOG = LoggerFactory.getLogger(MasterHbckServlet.class);
59+
private static final Gson GSON = GsonFactory.buildGson();
60+
61+
@Override
62+
public void doGet(HttpServletRequest request, HttpServletResponse response)
63+
throws ServletException, IOException {
64+
final HMaster master = (HMaster) getServletContext().getAttribute(HMaster.MASTER);
65+
if (!master.isInitialized()) {
66+
LOG.warn("Master is not initialized yet");
67+
sendError(response, HttpServletResponse.SC_SERVICE_UNAVAILABLE,
68+
"master is not initialized yet");
69+
return;
70+
}
71+
final HbckChore hbckChore = master.getHbckChore();
72+
if (hbckChore == null || hbckChore.isDisabled()) {
73+
LOG.warn("Hbck chore is disabled");
74+
sendError(response, HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Hbck chore is disabled");
75+
return;
76+
}
77+
if (!Boolean.parseBoolean(request.getParameter("cache"))) {
78+
try {
79+
master.getMasterRpcServices().runHbckChore(null, null);
80+
} catch (org.apache.hbase.thirdparty.com.google.protobuf.ServiceException se) {
81+
LOG.warn("Failed generating a new hbck chore report; using cache", se);
82+
}
83+
try {
84+
master.getMasterRpcServices().runCatalogScan(null, null);
85+
} catch (org.apache.hbase.thirdparty.com.google.protobuf.ServiceException se) {
86+
LOG.warn("Failed generating a new catalogjanitor report; using cache", se);
87+
}
88+
}
89+
Map<String, Object> result = new HashMap<>();
90+
result.put(START_TIMESTAMP, hbckChore.getCheckingStartTimestamp());
91+
result.put(END_TIMESTAMP, hbckChore.getCheckingEndTimestamp());
92+
final Map<String, Pair<ServerName, List<ServerName>>> inconsistentRegions =
93+
hbckChore.getInconsistentRegions();
94+
if (inconsistentRegions != null && !inconsistentRegions.isEmpty()) {
95+
result.put(INCONSISTENT_REGIONS, inconsistentRegions);
96+
}
97+
final Map<String, ServerName> orphanRegionsOnRS = hbckChore.getOrphanRegionsOnRS();
98+
if (orphanRegionsOnRS != null && !orphanRegionsOnRS.isEmpty()) {
99+
result.put(ORPHAN_REGIONS_ON_RS, orphanRegionsOnRS);
100+
}
101+
final Map<String, Path> orphanRegionsOnFS = hbckChore.getOrphanRegionsOnFS();
102+
if (orphanRegionsOnFS != null && !orphanRegionsOnFS.isEmpty()) {
103+
result.put(ORPHAN_REGIONS_ON_FS, orphanRegionsOnFS);
104+
}
105+
final CatalogJanitor janitor = master.getCatalogJanitor();
106+
if (janitor != null) {
107+
final Report report = janitor.getLastReport();
108+
if (report != null && !report.isEmpty()) {
109+
List<Pair<RegionInfo, RegionInfo>> holes = report.getHoles();
110+
if (holes != null && !holes.isEmpty()) {
111+
result.put(HOLES, holes);
112+
}
113+
List<Pair<RegionInfo, RegionInfo>> overlaps = report.getOverlaps();
114+
if (overlaps != null && !overlaps.isEmpty()) {
115+
result.put(OVERLAPS, overlaps);
116+
}
117+
List<Pair<RegionInfo, ServerName>> unknownServers = report.getUnknownServers();
118+
if (unknownServers != null && !unknownServers.isEmpty()) {
119+
result.put(UNKNOWN_SERVERS, unknownServers);
120+
}
121+
List<byte[]> emptyRegionInfo = report.getEmptyRegionInfo();
122+
if (!emptyRegionInfo.isEmpty()) {
123+
result.put(EMPTY_REGIONINFO, emptyRegionInfo);
124+
}
125+
}
126+
}
127+
response.setContentType("application/json");
128+
PrintWriter out = response.getWriter();
129+
out.write(GSON.toJson(result));
130+
out.write('\n');
131+
}
132+
133+
private static void sendError(HttpServletResponse response, int code, String message)
134+
throws IOException {
135+
response.setContentType("application/json");
136+
Map<String, Object> result = new HashMap<>();
137+
result.put("error", message);
138+
response.setStatus(code);
139+
PrintWriter out = response.getWriter();
140+
out.write(GSON.toJson(result));
141+
out.write('\n');
142+
}
143+
144+
}

0 commit comments

Comments
 (0)