View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.master.handler;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.NavigableMap;
27  import java.util.Set;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.HRegionInfo;
33  import org.apache.hadoop.hbase.Server;
34  import org.apache.hadoop.hbase.ServerName;
35  import org.apache.hadoop.hbase.catalog.CatalogTracker;
36  import org.apache.hadoop.hbase.catalog.MetaEditor;
37  import org.apache.hadoop.hbase.catalog.MetaReader;
38  import org.apache.hadoop.hbase.client.Result;
39  import org.apache.hadoop.hbase.executor.EventHandler;
40  import org.apache.hadoop.hbase.master.AssignmentManager;
41  import org.apache.hadoop.hbase.master.AssignmentManager.RegionState;
42  import org.apache.hadoop.hbase.master.DeadServer;
43  import org.apache.hadoop.hbase.master.MasterServices;
44  import org.apache.hadoop.hbase.master.ServerManager;
45  import org.apache.hadoop.hbase.util.Bytes;
46  import org.apache.hadoop.hbase.util.Pair;
47  import org.apache.hadoop.hbase.util.Threads;
48  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
49  import org.apache.zookeeper.KeeperException;
50  
51  /**
52   * Process server shutdown.
53   * Server-to-handle must be already in the deadservers lists.  See
54   * {@link ServerManager#expireServer(ServerName)}
55   */
56  public class ServerShutdownHandler extends EventHandler {
57    private static final Log LOG = LogFactory.getLog(ServerShutdownHandler.class);
58    protected final ServerName serverName;
59    protected final MasterServices services;
60    protected final DeadServer deadServers;
61    protected final boolean shouldSplitHlog; // whether to split HLog or not
62  
63    public ServerShutdownHandler(final Server server, final MasterServices services,
64        final DeadServer deadServers, final ServerName serverName,
65        final boolean shouldSplitHlog) {
66      this(server, services, deadServers, serverName, EventType.M_SERVER_SHUTDOWN,
67          shouldSplitHlog);
68    }
69  
70    ServerShutdownHandler(final Server server, final MasterServices services,
71        final DeadServer deadServers, final ServerName serverName, EventType type,
72        final boolean shouldSplitHlog) {
73      super(server, type);
74      this.serverName = serverName;
75      this.server = server;
76      this.services = services;
77      this.deadServers = deadServers;
78      if (!this.deadServers.contains(this.serverName)) {
79        LOG.warn(this.serverName + " is NOT in deadservers; it should be!");
80      }
81      this.shouldSplitHlog = shouldSplitHlog;
82    }
83  
84    @Override
85    public String getInformativeName() {
86      if (serverName != null) {
87        return this.getClass().getSimpleName() + " for " + serverName;
88      } else {
89        return super.getInformativeName();
90      }
91    }
92    
93    /**
94     * @return True if the server we are processing was carrying <code>-ROOT-</code>
95     */
96    boolean isCarryingRoot() {
97      return false;
98    }
99  
100   /**
101    * @return True if the server we are processing was carrying <code>.META.</code>
102    */
103   boolean isCarryingMeta() {
104     return false;
105   }
106 
107   @Override
108   public String toString() {
109     String name = "UnknownServerName";
110     if(server != null && server.getServerName() != null) {
111       name = server.getServerName().toString();
112     }
113     return getClass().getSimpleName() + "-" + name + "-" + getSeqid();
114   }
115 
116   @Override
117   public void process() throws IOException {
118     final ServerName serverName = this.serverName;
119     try {
120       if (this.server.isStopped()) {
121         throw new IOException("Server is stopped");
122       }
123 
124       try {
125         if (this.shouldSplitHlog) {
126           LOG.info("Splitting logs for " + serverName);
127           this.services.getMasterFileSystem().splitLog(serverName);
128         } else {
129           LOG.info("Skipping log splitting for " + serverName);
130         }
131       } catch (IOException ioe) {
132         //typecast to SSH so that we make sure that it is the SSH instance that
133         //gets submitted as opposed to MSSH or some other derived instance of SSH
134         this.services.getExecutorService().submit((ServerShutdownHandler)this);
135         this.deadServers.add(serverName);
136         throw new IOException("failed log splitting for " +
137           serverName + ", will retry", ioe);
138       }
139 
140       // We don't want worker thread in the MetaServerShutdownHandler
141       // executor pool to block by waiting availability of -ROOT-
142       // and .META. server. Otherwise, it could run into the following issue:
143       // 1. The current MetaServerShutdownHandler instance For RS1 waits for the .META.
144       //    to come online.
145       // 2. The newly assigned .META. region server RS2 was shutdown right after
146       //    it opens the .META. region. So the MetaServerShutdownHandler
147       //    instance For RS1 will still be blocked.
148       // 3. The new instance of MetaServerShutdownHandler for RS2 is queued.
149       // 4. The newly assigned .META. region server RS3 was shutdown right after
150       //    it opens the .META. region. So the MetaServerShutdownHandler
151       //    instance For RS1 and RS2 will still be blocked.
152       // 5. The new instance of MetaServerShutdownHandler for RS3 is queued.
153       // 6. Repeat until we run out of MetaServerShutdownHandler worker threads
154       // The solution here is to resubmit a ServerShutdownHandler request to process
155       // user regions on that server so that MetaServerShutdownHandler
156       // executor pool is always available.
157       if (isCarryingRoot() || isCarryingMeta()) { // -ROOT- or .META.
158         this.services.getExecutorService().submit(new ServerShutdownHandler(
159           this.server, this.services, this.deadServers, serverName, false));
160         this.deadServers.add(serverName);
161         return;
162       }
163 
164 
165       // Wait on meta to come online; we need it to progress.
166       // TODO: Best way to hold strictly here?  We should build this retry logic
167       // into the MetaReader operations themselves.
168       // TODO: Is the reading of .META. necessary when the Master has state of
169       // cluster in its head?  It should be possible to do without reading .META.
170       // in all but one case. On split, the RS updates the .META.
171       // table and THEN informs the master of the split via zk nodes in
172       // 'unassigned' dir.  Currently the RS puts ephemeral nodes into zk so if
173       // the regionserver dies, these nodes do not stick around and this server
174       // shutdown processing does fixup (see the fixupDaughters method below).
175       // If we wanted to skip the .META. scan, we'd have to change at least the
176       // final SPLIT message to be permanent in zk so in here we'd know a SPLIT
177       // completed (zk is updated after edits to .META. have gone in).  See
178       // {@link SplitTransaction}.  We'd also have to be figure another way for
179       // doing the below .META. daughters fixup.
180       NavigableMap<HRegionInfo, Result> hris = null;
181       while (!this.server.isStopped()) {
182         try {
183           this.server.getCatalogTracker().waitForMeta();
184           // Skip getting user regions if the server is stopped.
185           if (!this.server.isStopped()) {
186             hris = MetaReader.getServerUserRegions(this.server.getCatalogTracker(),
187                 this.serverName);
188           }
189           break;
190         } catch (InterruptedException e) {
191           Thread.currentThread().interrupt();
192           throw new IOException("Interrupted", e);
193         } catch (IOException ioe) {
194           LOG.info("Received exception accessing META during server shutdown of " +
195               serverName + ", retrying META read", ioe);
196         }
197       }
198 
199       // Returns set of regions that had regionplans against the downed server and a list of
200       // the intersection of regions-in-transition and regions that were on the server that died.
201       Pair<Set<HRegionInfo>, List<RegionState>> p = this.services.getAssignmentManager()
202           .processServerShutdown(this.serverName);
203       Set<HRegionInfo> ritsGoingToServer = p.getFirst();
204       List<RegionState> ritsOnServer = p.getSecond();
205 
206       List<HRegionInfo> regionsToAssign = getRegionsToAssign(hris, ritsOnServer, ritsGoingToServer);
207       for (HRegionInfo hri : ritsGoingToServer) {
208         if (!this.services.getAssignmentManager().isRegionAssigned(hri)) {
209           if (!regionsToAssign.contains(hri)) {
210             regionsToAssign.add(hri);
211             RegionState rit =
212                 services.getAssignmentManager().getRegionsInTransition().get(hri.getEncodedName());
213             removeRITsOfRregionInDisablingOrDisabledTables(regionsToAssign, rit,
214               services.getAssignmentManager(), hri);
215           }
216         }
217       }
218 
219       // re-assign regions
220       for (HRegionInfo hri : regionsToAssign) {
221         this.services.getAssignmentManager().assign(hri, true);
222       }
223       LOG.info(regionsToAssign.size() + " regions which were planned to open on " + this.serverName
224           + " have been re-assigned.");
225     } finally {
226       this.deadServers.finish(serverName);
227     }
228     LOG.info("Finished processing of shutdown of " + serverName);
229   }
230 
231   /**
232    * Figure what to assign from the dead server considering state of RIT and whats up in .META.
233    * @param metaHRIs Regions that .META. says were assigned to the dead server
234    * @param ritsOnServer Regions that were in transition, and on the dead server.
235    * @param ritsGoingToServer Regions that were in transition to the dead server.
236    * @return List of regions to assign or null if aborting.
237    * @throws IOException
238    */
239   private List<HRegionInfo> getRegionsToAssign(final NavigableMap<HRegionInfo, Result> metaHRIs,
240       final List<RegionState> ritsOnServer, Set<HRegionInfo> ritsGoingToServer) throws IOException {
241     List<HRegionInfo> toAssign = new ArrayList<HRegionInfo>();
242     // If no regions on the server, then nothing to assign (Regions that were currently being
243     // assigned will be retried over in the AM#assign method).
244     if (metaHRIs == null || metaHRIs.isEmpty()) return toAssign;
245     // Remove regions that we do not want to reassign such as regions that are
246     // OFFLINE. If region is OFFLINE against this server, its probably being assigned over
247     // in the single region assign method in AM; do not assign it here too. TODO: VERIFY!!!
248     // TODO: Currently OFFLINE is too messy. Its done on single assign but bulk done when bulk
249     // assigning and then there is special handling when master joins a cluster.
250     //
251     // If split, the zk callback will have offlined. Daughters will be in the
252     // list of hris we got from scanning the .META. These should be reassigned. Not the parent.
253     for (RegionState rs : ritsOnServer) {
254       if (!rs.isClosing() && !rs.isPendingClose() && !rs.isSplitting()) {
255         LOG.debug("Removed " + rs.getRegion().getRegionNameAsString()
256             + " from list of regions to assign because region state: " + rs.getState());
257         metaHRIs.remove(rs.getRegion());
258       }
259     }
260 
261     AssignmentManager assignmentManager = this.services.getAssignmentManager();
262     for (Map.Entry<HRegionInfo, Result> e : metaHRIs.entrySet()) {
263       RegionState rit =
264           assignmentManager.getRegionsInTransition().get(e.getKey().getEncodedName());
265 
266       if (processDeadRegion(e.getKey(), e.getValue(), assignmentManager,
267         this.server.getCatalogTracker())) {
268         ServerName addressFromAM = assignmentManager.getRegionServerOfRegion(e.getKey());
269         if (rit != null && !rit.isClosing() && !rit.isPendingClose() && !rit.isSplitting()
270             && !ritsGoingToServer.contains(e.getKey())) {
271           // Skip regions that were in transition unless CLOSING or
272           // PENDING_CLOSE
273           LOG.info("Skip assigning region " + rit.toString());
274         } else if (addressFromAM != null && !addressFromAM.equals(this.serverName)) {
275           LOG.debug("Skip assigning region " + e.getKey().getRegionNameAsString()
276               + " because it has been opened in " + addressFromAM.getServerName());
277           ritsGoingToServer.remove(e.getKey());
278         } else {
279           if (rit != null) {
280             // clean zk node
281             try {
282               LOG.info("Reassigning region with rs =" + rit + " and deleting zk node if exists");
283               ZKAssign.deleteNodeFailSilent(services.getZooKeeper(), e.getKey());
284             } catch (KeeperException ke) {
285               this.server.abort("Unexpected ZK exception deleting unassigned node " + e.getKey(),
286                 ke);
287               return null;
288             }
289           }
290           toAssign.add(e.getKey());
291         }
292       } else if (rit != null && (rit.isSplitting() || rit.isSplit())) {
293         // This will happen when the RS went down and the call back for the SPLIITING or SPLIT
294         // has not yet happened for node Deleted event. In that case if the region was actually
295         // split but the RS had gone down before completing the split process then will not try
296         // to assign the parent region again. In that case we should make the region offline
297         // and also delete the region from RIT.
298         HRegionInfo region = rit.getRegion();
299         AssignmentManager am = assignmentManager;
300         am.regionOffline(region);
301         ritsGoingToServer.remove(region);
302       }
303       // If the table was partially disabled and the RS went down, we should clear the RIT
304       // and remove the node for the region. The rit that we use may be stale in case the table
305       // was in DISABLING state but though we did assign we will not be clearing the znode in
306       // CLOSING state. Doing this will have no harm. The rit can be null if region server went
307       // down during master startup. In that case If any znodes' exists for partially disabled 
308       // table regions deleting them during startup only. See HBASE-8127. 
309       removeRITsOfRregionInDisablingOrDisabledTables(toAssign, rit, assignmentManager, e.getKey());
310     }
311 
312     return toAssign;
313   }
314 
315   private void removeRITsOfRregionInDisablingOrDisabledTables(List<HRegionInfo> toAssign,
316       RegionState rit, AssignmentManager assignmentManager, HRegionInfo hri) {
317 
318     if (!assignmentManager.getZKTable().isDisablingOrDisabledTable(hri.getTableNameAsString())) {
319       return;
320     }
321 
322     // To avoid region assignment if table is in disabling or disabled state.
323     toAssign.remove(hri);
324 
325     if (rit != null) {
326       assignmentManager.deleteNodeAndOfflineRegion(hri);
327     }
328   }
329 
330   /**
331    * Process a dead region from a dead RS. Checks if the region is disabled or
332    * disabling or if the region has a partially completed split.
333    * @param hri
334    * @param result
335    * @param assignmentManager
336    * @param catalogTracker
337    * @return Returns true if specified region should be assigned, false if not.
338    * @throws IOException
339    */
340   public static boolean processDeadRegion(HRegionInfo hri, Result result,
341       AssignmentManager assignmentManager, CatalogTracker catalogTracker)
342   throws IOException {
343     boolean tablePresent = assignmentManager.getZKTable().isTablePresent(
344         hri.getTableNameAsString());
345     if (!tablePresent) {
346       LOG.info("The table " + hri.getTableNameAsString()
347           + " was deleted.  Hence not proceeding.");
348       return false;
349     }
350     // If table is not disabled but the region is offlined,
351     boolean disabled = assignmentManager.getZKTable().isDisabledTable(
352         hri.getTableNameAsString());
353     if (disabled){
354       LOG.info("The table " + hri.getTableNameAsString()
355           + " was disabled.  Hence not proceeding.");
356       return false;
357     }
358     if (hri.isOffline() && hri.isSplit()) {
359       LOG.debug("Offlined and split region " + hri.getRegionNameAsString() +
360         "; checking daughter presence");
361       if (MetaReader.getRegion(catalogTracker, hri.getRegionName()) == null) {
362         return false;
363       }
364       fixupDaughters(result, assignmentManager, catalogTracker);
365       return false;
366     }
367     boolean disabling = assignmentManager.getZKTable().isDisablingTable(
368         hri.getTableNameAsString());
369     if (disabling) {
370       LOG.info("The table " + hri.getTableNameAsString()
371           + " is disabled.  Hence not assigning region" + hri.getEncodedName());
372       return false;
373     }
374     return true;
375   }
376 
377   /**
378    * Check that daughter regions are up in .META. and if not, add them.
379    * @param hris All regions for this server in meta.
380    * @param result The contents of the parent row in .META.
381    * @return the number of daughters missing and fixed
382    * @throws IOException
383    */
384   public static int fixupDaughters(final Result result,
385       final AssignmentManager assignmentManager,
386       final CatalogTracker catalogTracker)
387   throws IOException {
388     int fixedA = fixupDaughter(result, HConstants.SPLITA_QUALIFIER,
389       assignmentManager, catalogTracker);
390     int fixedB = fixupDaughter(result, HConstants.SPLITB_QUALIFIER,
391       assignmentManager, catalogTracker);
392     return fixedA + fixedB;
393   }
394 
395   /**
396    * Check individual daughter is up in .META.; fixup if its not.
397    * @param result The contents of the parent row in .META.
398    * @param qualifier Which daughter to check for.
399    * @return 1 if the daughter is missing and fixed. Otherwise 0
400    * @throws IOException
401    */
402   static int fixupDaughter(final Result result, final byte [] qualifier,
403       final AssignmentManager assignmentManager,
404       final CatalogTracker catalogTracker)
405   throws IOException {
406     HRegionInfo daughter =
407       MetaReader.parseHRegionInfoFromCatalogResult(result, qualifier);
408     if (daughter == null) return 0;
409     if (isDaughterMissing(catalogTracker, daughter)) {
410       LOG.info("Fixup; missing daughter " + daughter.getRegionNameAsString());
411       MetaEditor.addDaughter(catalogTracker, daughter, null);
412 
413       // TODO: Log WARN if the regiondir does not exist in the fs.  If its not
414       // there then something wonky about the split -- things will keep going
415       // but could be missing references to parent region.
416 
417       // And assign it.
418       assignmentManager.assign(daughter, true);
419       return 1;
420     } else {
421       LOG.debug("Daughter " + daughter.getRegionNameAsString() + " present");
422     }
423     return 0;
424   }
425 
426   /**
427    * Look for presence of the daughter OR of a split of the daughter in .META.
428    * Daughter could have been split over on regionserver before a run of the
429    * catalogJanitor had chance to clear reference from parent.
430    * @param daughter Daughter region to search for.
431    * @throws IOException 
432    */
433   private static boolean isDaughterMissing(final CatalogTracker catalogTracker,
434       final HRegionInfo daughter) throws IOException {
435     FindDaughterVisitor visitor = new FindDaughterVisitor(daughter);
436     // Start the scan at what should be the daughter's row in the .META.
437     // We will either 1., find the daughter or some derivative split of the
438     // daughter (will have same table name and start row at least but will sort
439     // after because has larger regionid -- the regionid is timestamp of region
440     // creation), OR, we will not find anything with same table name and start
441     // row.  If the latter, then assume daughter missing and do fixup.
442     byte [] startrow = daughter.getRegionName();
443     MetaReader.fullScan(catalogTracker, visitor, startrow);
444     return !visitor.foundDaughter();
445   }
446 
447   /**
448    * Looks for daughter.  Sets a flag if daughter or some progeny of daughter
449    * is found up in <code>.META.</code>.
450    */
451   static class FindDaughterVisitor implements MetaReader.Visitor {
452     private final HRegionInfo daughter;
453     private boolean found = false;
454 
455     FindDaughterVisitor(final HRegionInfo daughter) {
456       this.daughter = daughter;
457     }
458 
459     /**
460      * @return True if we found a daughter region during our visiting.
461      */
462     boolean foundDaughter() {
463       return this.found;
464     }
465 
466     @Override
467     public boolean visit(Result r) throws IOException {
468       HRegionInfo hri =
469         MetaReader.parseHRegionInfoFromCatalogResult(r, HConstants.REGIONINFO_QUALIFIER);
470       if (hri == null) {
471         LOG.warn("No serialized HRegionInfo in " + r);
472         return true;
473       }
474       byte [] value = r.getValue(HConstants.CATALOG_FAMILY,
475           HConstants.SERVER_QUALIFIER);
476       // See if daughter is assigned to some server
477       if (value == null) return false;
478 
479       // Now see if we have gone beyond the daughter's startrow.
480       if (!Bytes.equals(daughter.getTableName(),
481           hri.getTableName())) {
482         // We fell into another table.  Stop scanning.
483         return false;
484       }
485       // If our start rows do not compare, move on.
486       if (!Bytes.equals(daughter.getStartKey(), hri.getStartKey())) {
487         return false;
488       }
489       // Else, table name and start rows compare.  It means that the daughter
490       // or some derivative split of the daughter is up in .META.  Daughter
491       // exists.
492       this.found = true;
493       return false;
494     }
495   }
496 }