goiri commented on a change in pull request #1832: HDFS-13989. RBF: Add FSCK to the Router URL: https://github.com/apache/hadoop/pull/1832#discussion_r376562407
########## File path: hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/RouterFsck.java ########## @@ -0,0 +1,158 @@ +/** + * 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.hdfs.server.federation.router; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.InetAddress; +import java.net.URL; +import java.net.URLConnection; +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.hdfs.server.federation.resolver.FederationNamenodeServiceState; +import org.apache.hadoop.hdfs.server.federation.store.MembershipStore; +import org.apache.hadoop.hdfs.server.federation.store.StateStoreService; +import org.apache.hadoop.hdfs.server.federation.store.protocol.GetNamenodeRegistrationsRequest; +import org.apache.hadoop.hdfs.server.federation.store.protocol.GetNamenodeRegistrationsResponse; +import org.apache.hadoop.hdfs.server.federation.store.records.MembershipState; +import org.apache.hadoop.security.UserGroupInformation; +import org.apache.hadoop.util.Time; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Wrapper for the Router to offer the Namenode FSCK. + */ [email protected] +public class RouterFsck { + + public static final Logger LOG = + LoggerFactory.getLogger(RouterFsck.class.getName()); + + private final Router router; + private final InetAddress remoteAddress; + private final PrintWriter out; + private final Map<String, String[]> pmap; + + public RouterFsck(Router router, Map<String, String[]> pmap, + PrintWriter out, InetAddress remoteAddress) { + this.router = router; + this.remoteAddress = remoteAddress; + this.out = out; + this.pmap = pmap; + } + + public void fsck() { + final long startTime = Time.monotonicNow(); + try { + String msg = "Federated FSCK started by " + + UserGroupInformation.getCurrentUser() + " from " + remoteAddress + + " at " + new Date(); + LOG.info(msg); + out.println(msg); + + // Check each Namenode in the federation + StateStoreService stateStore = router.getStateStore(); + MembershipStore membership = + stateStore.getRegisteredRecordStore(MembershipStore.class); + GetNamenodeRegistrationsRequest request = + GetNamenodeRegistrationsRequest.newInstance(); + GetNamenodeRegistrationsResponse response = + membership.getNamenodeRegistrations(request); + List<MembershipState> memberships = response.getNamenodeMemberships(); + Collections.sort(memberships); + for (MembershipState nn : memberships) { + if (nn.getState() == FederationNamenodeServiceState.ACTIVE) { + try { + String webAddress = nn.getWebAddress(); + out.write("Checking " + nn + " at " + webAddress + "\n"); + remoteFsck(nn); + } catch (IOException ioe) { + out.println("Cannot query " + nn + ": " + ioe.getMessage() + "\n"); + } + } + } + + out.println("Federated FSCK ended at " + new Date() + " in " + + (Time.monotonicNow() - startTime + " milliseconds")); + } catch (Exception e) { + String errMsg = "Fsck " + e.getMessage(); + LOG.warn(errMsg, e); + out.println("Federated FSCK ended at " + new Date() + " in " + + (Time.monotonicNow() - startTime + " milliseconds")); + out.println(e.getMessage()); + out.print("\n\n" + errMsg); + } finally { + out.close(); + } + } + + /** + * Perform FSCK in a remote Namenode. + * + * @param nn The state of the remote NameNode + * @throws IOException Failed to fsck in a remote NameNode + */ + private void remoteFsck(MembershipState nn) throws IOException { + final String scheme = nn.getWebScheme(); + final String webAddress = nn.getWebAddress(); + final String args = getURLArguments(pmap); + final URL url = new URL(scheme + "://" + webAddress + "/fsck?" + args); + + // Connect to the Namenode and output + final URLConnection conn = url.openConnection(); + try (InputStream is = conn.getInputStream(); + InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8); Review comment: Too long. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
