View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.master.handler;
20  
21  import java.io.IOException;
22  import java.io.InterruptedIOException;
23  import java.util.ArrayList;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.NavigableMap;
28  import java.util.Set;
29  import java.util.concurrent.locks.Lock;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.hadoop.classification.InterfaceAudience;
34  import org.apache.hadoop.hbase.HConstants;
35  import org.apache.hadoop.hbase.HRegionInfo;
36  import org.apache.hadoop.hbase.Server;
37  import org.apache.hadoop.hbase.ServerName;
38  import org.apache.hadoop.hbase.catalog.CatalogTracker;
39  import org.apache.hadoop.hbase.catalog.MetaReader;
40  import org.apache.hadoop.hbase.client.Result;
41  import org.apache.hadoop.hbase.executor.EventHandler;
42  import org.apache.hadoop.hbase.executor.EventType;
43  import org.apache.hadoop.hbase.master.AssignmentManager;
44  import org.apache.hadoop.hbase.master.DeadServer;
45  import org.apache.hadoop.hbase.master.MasterServices;
46  import org.apache.hadoop.hbase.master.RegionState;
47  import org.apache.hadoop.hbase.master.RegionState.State;
48  import org.apache.hadoop.hbase.master.RegionStates;
49  import org.apache.hadoop.hbase.master.ServerManager;
50  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
51  import org.apache.zookeeper.KeeperException;
52  
53  /**
54   * Process server shutdown.
55   * Server-to-handle must be already in the deadservers lists.  See
56   * {@link ServerManager#expireServer(ServerName)}
57   */
58  @InterfaceAudience.Private
59  public class ServerShutdownHandler extends EventHandler {
60    private static final Log LOG = LogFactory.getLog(ServerShutdownHandler.class);
61    protected final ServerName serverName;
62    protected final MasterServices services;
63    protected final DeadServer deadServers;
64    protected final boolean shouldSplitHlog; // whether to split HLog or not
65    protected final boolean distributedLogReplay;
66    protected final int regionAssignmentWaitTimeout;
67  
68    public ServerShutdownHandler(final Server server, final MasterServices services,
69        final DeadServer deadServers, final ServerName serverName,
70        final boolean shouldSplitHlog) {
71      this(server, services, deadServers, serverName, EventType.M_SERVER_SHUTDOWN,
72          shouldSplitHlog);
73    }
74  
75    ServerShutdownHandler(final Server server, final MasterServices services,
76        final DeadServer deadServers, final ServerName serverName, EventType type,
77        final boolean shouldSplitHlog) {
78      super(server, type);
79      this.serverName = serverName;
80      this.server = server;
81      this.services = services;
82      this.deadServers = deadServers;
83      if (!this.deadServers.isDeadServer(this.serverName)) {
84        LOG.warn(this.serverName + " is NOT in deadservers; it should be!");
85      }
86      this.shouldSplitHlog = shouldSplitHlog;
87      this.distributedLogReplay = server.getConfiguration().getBoolean(
88            HConstants.DISTRIBUTED_LOG_REPLAY_KEY, 
89            HConstants.DEFAULT_DISTRIBUTED_LOG_REPLAY_CONFIG);
90      this.regionAssignmentWaitTimeout = server.getConfiguration().getInt(
91        HConstants.LOG_REPLAY_WAIT_REGION_TIMEOUT, 15000);
92    }
93  
94    @Override
95    public String getInformativeName() {
96      if (serverName != null) {
97        return this.getClass().getSimpleName() + " for " + serverName;
98      } else {
99        return super.getInformativeName();
100     }
101   }
102 
103   /**
104    * @return True if the server we are processing was carrying <code>hbase:meta</code>
105    */
106   boolean isCarryingMeta() {
107     return false;
108   }
109 
110   @Override
111   public String toString() {
112     String name = "UnknownServerName";
113     if(server != null && server.getServerName() != null) {
114       name = server.getServerName().toString();
115     }
116     return getClass().getSimpleName() + "-" + name + "-" + getSeqid();
117   }
118 
119   @Override
120   public void process() throws IOException {
121     boolean hasLogReplayWork = false;
122     final ServerName serverName = this.serverName;
123     try {
124 
125       // We don't want worker thread in the MetaServerShutdownHandler
126       // executor pool to block by waiting availability of hbase:meta
127       // Otherwise, it could run into the following issue:
128       // 1. The current MetaServerShutdownHandler instance For RS1 waits for the hbase:meta
129       //    to come online.
130       // 2. The newly assigned hbase:meta region server RS2 was shutdown right after
131       //    it opens the hbase:meta region. So the MetaServerShutdownHandler
132       //    instance For RS1 will still be blocked.
133       // 3. The new instance of MetaServerShutdownHandler for RS2 is queued.
134       // 4. The newly assigned hbase:meta region server RS3 was shutdown right after
135       //    it opens the hbase:meta region. So the MetaServerShutdownHandler
136       //    instance For RS1 and RS2 will still be blocked.
137       // 5. The new instance of MetaServerShutdownHandler for RS3 is queued.
138       // 6. Repeat until we run out of MetaServerShutdownHandler worker threads
139       // The solution here is to resubmit a ServerShutdownHandler request to process
140       // user regions on that server so that MetaServerShutdownHandler
141       // executor pool is always available.
142       //
143       // If AssignmentManager hasn't finished rebuilding user regions,
144       // we are not ready to assign dead regions either. So we re-queue up
145       // the dead server for further processing too.
146       AssignmentManager am = services.getAssignmentManager();
147       if (isCarryingMeta() // hbase:meta
148           || !am.isFailoverCleanupDone()) {
149         this.services.getServerManager().processDeadServer(serverName, this.shouldSplitHlog);
150         return;
151       }
152 
153       // Wait on meta to come online; we need it to progress.
154       // TODO: Best way to hold strictly here?  We should build this retry logic
155       // into the MetaReader operations themselves.
156       // TODO: Is the reading of hbase:meta necessary when the Master has state of
157       // cluster in its head?  It should be possible to do without reading hbase:meta
158       // in all but one case. On split, the RS updates the hbase:meta
159       // table and THEN informs the master of the split via zk nodes in
160       // 'unassigned' dir.  Currently the RS puts ephemeral nodes into zk so if
161       // the regionserver dies, these nodes do not stick around and this server
162       // shutdown processing does fixup (see the fixupDaughters method below).
163       // If we wanted to skip the hbase:meta scan, we'd have to change at least the
164       // final SPLIT message to be permanent in zk so in here we'd know a SPLIT
165       // completed (zk is updated after edits to hbase:meta have gone in).  See
166       // {@link SplitTransaction}.  We'd also have to be figure another way for
167       // doing the below hbase:meta daughters fixup.
168       NavigableMap<HRegionInfo, Result> hris = null;
169       while (!this.server.isStopped()) {
170         try {
171           this.server.getCatalogTracker().waitForMeta();
172           hris = MetaReader.getServerUserRegions(this.server.getCatalogTracker(),
173             this.serverName);
174           break;
175         } catch (InterruptedException e) {
176           Thread.currentThread().interrupt();
177           throw new IOException("Interrupted", e);
178         } catch (IOException ioe) {
179           LOG.info("Received exception accessing hbase:meta during server shutdown of " +
180             serverName + ", retrying hbase:meta read", ioe);
181         }
182       }
183       if (this.server.isStopped()) {
184         throw new IOException("Server is stopped");
185       }
186 
187       try {
188         if (this.shouldSplitHlog) {
189           LOG.info("Splitting logs for " + serverName + " before assignment.");
190           if (this.distributedLogReplay) {
191             LOG.info("Mark regions in recovery before assignment.");
192             Set<ServerName> serverNames = new HashSet<ServerName>();
193             serverNames.add(serverName);
194             this.services.getMasterFileSystem().prepareLogReplay(serverNames);
195           } else {
196             this.services.getMasterFileSystem().splitLog(serverName);
197           }
198           am.getRegionStates().logSplit(serverName);
199         } else {
200           LOG.info("Skipping log splitting for " + serverName);
201         }
202       } catch (IOException ioe) {
203         resubmit(serverName, ioe);
204       }
205 
206       // Clean out anything in regions in transition.  Being conservative and
207       // doing after log splitting.  Could do some states before -- OPENING?
208       // OFFLINE? -- and then others after like CLOSING that depend on log
209       // splitting.
210       List<HRegionInfo> regionsInTransition = am.processServerShutdown(serverName);
211       LOG.info("Reassigning " + ((hris == null)? 0: hris.size()) +
212         " region(s) that " + (serverName == null? "null": serverName)  +
213         " was carrying (and " + regionsInTransition.size() +
214         " regions(s) that were opening on this server)");
215 
216       List<HRegionInfo> toAssignRegions = new ArrayList<HRegionInfo>();
217       toAssignRegions.addAll(regionsInTransition);
218 
219       // Iterate regions that were on this server and assign them
220       if (hris != null) {
221         RegionStates regionStates = am.getRegionStates();
222         for (Map.Entry<HRegionInfo, Result> e: hris.entrySet()) {
223           HRegionInfo hri = e.getKey();
224           if (regionsInTransition.contains(hri)) {
225             continue;
226           }
227           String encodedName = hri.getEncodedName();
228           Lock lock = am.acquireRegionLock(encodedName);
229           try {
230             RegionState rit = regionStates.getRegionTransitionState(hri);
231             if (processDeadRegion(hri, e.getValue(), am, server.getCatalogTracker())) {
232               ServerName addressFromAM = regionStates.getRegionServerOfRegion(hri);
233               if (addressFromAM != null && !addressFromAM.equals(this.serverName)) {
234                 // If this region is in transition on the dead server, it must be
235                 // opening or pending_open, which should have been covered by AM#processServerShutdown
236                 LOG.info("Skip assigning region " + hri.getRegionNameAsString()
237                   + " because it has been opened in " + addressFromAM.getServerName());
238                 continue;
239               }
240               if (rit != null) {
241                 if (rit.getServerName() != null && !rit.isOnServer(serverName)) {
242                   // Skip regions that are in transition on other server
243                   LOG.info("Skip assigning region in transition on other server" + rit);
244                   continue;
245                 }
246                 try{
247                   //clean zk node
248                   LOG.info("Reassigning region with rs = " + rit + " and deleting zk node if exists");
249                   ZKAssign.deleteNodeFailSilent(services.getZooKeeper(), hri);
250                   regionStates.updateRegionState(hri, State.OFFLINE);
251                 } catch (KeeperException ke) {
252                   this.server.abort("Unexpected ZK exception deleting unassigned node " + hri, ke);
253                   return;
254                 }
255               } else if (regionStates.isRegionInState(
256                   hri, State.SPLITTING_NEW, State.MERGING_NEW)) {
257                 regionStates.regionOffline(hri);
258               }
259               toAssignRegions.add(hri);
260             } else if (rit != null) {
261               if (rit.isPendingCloseOrClosing()
262                   && am.getZKTable().isDisablingOrDisabledTable(hri.getTable())) {
263                 // If the table was partially disabled and the RS went down, we should clear the RIT
264                 // and remove the node for the region.
265                 // The rit that we use may be stale in case the table was in DISABLING state
266                 // but though we did assign we will not be clearing the znode in CLOSING state.
267                 // Doing this will have no harm. See HBASE-5927
268                 regionStates.updateRegionState(hri, State.OFFLINE);
269                 am.deleteClosingOrClosedNode(hri, rit.getServerName());
270                 am.offlineDisabledRegion(hri);
271               } else {
272                 LOG.warn("THIS SHOULD NOT HAPPEN: unexpected region in transition "
273                   + rit + " not to be assigned by SSH of server " + serverName);
274               }
275             }
276           } finally {
277             lock.unlock();
278           }
279         }
280       }
281 
282       try {
283         am.assign(toAssignRegions);
284       } catch (InterruptedException ie) {
285         LOG.error("Caught " + ie + " during round-robin assignment");
286         throw new IOException(ie);
287       }
288 
289       if (this.shouldSplitHlog && this.distributedLogReplay) {
290         // wait for region assignment completes
291         for (HRegionInfo hri : toAssignRegions) {
292           try {
293             if (!am.waitOnRegionToClearRegionsInTransition(hri, regionAssignmentWaitTimeout)) {
294               // Wait here is to avoid log replay hits current dead server and incur a RPC timeout
295               // when replay happens before region assignment completes.
296               LOG.warn("Region " + hri.getEncodedName()
297                   + " didn't complete assignment in time");
298             }
299           } catch (InterruptedException ie) {
300             throw new InterruptedIOException("Caught " + ie
301                 + " during waitOnRegionToClearRegionsInTransition");
302           }
303         }
304         // submit logReplay work
305         this.services.getExecutorService().submit(
306           new LogReplayHandler(this.server, this.services, this.deadServers, this.serverName));
307         hasLogReplayWork = true;
308       }
309     } finally {
310       this.deadServers.finish(serverName);
311     }
312 
313     if (!hasLogReplayWork) {
314       LOG.info("Finished processing of shutdown of " + serverName);
315     }
316   }
317 
318   private void resubmit(final ServerName serverName, IOException ex) throws IOException {
319     // typecast to SSH so that we make sure that it is the SSH instance that
320     // gets submitted as opposed to MSSH or some other derived instance of SSH
321     this.services.getExecutorService().submit((ServerShutdownHandler) this);
322     this.deadServers.add(serverName);
323     throw new IOException("failed log splitting for " + serverName + ", will retry", ex);
324   }
325 
326   /**
327    * Process a dead region from a dead RS. Checks if the region is disabled or
328    * disabling or if the region has a partially completed split.
329    * @param hri
330    * @param result
331    * @param assignmentManager
332    * @param catalogTracker
333    * @return Returns true if specified region should be assigned, false if not.
334    * @throws IOException
335    */
336   public static boolean processDeadRegion(HRegionInfo hri, Result result,
337       AssignmentManager assignmentManager, CatalogTracker catalogTracker)
338   throws IOException {
339     boolean tablePresent = assignmentManager.getZKTable().isTablePresent(hri.getTable());
340     if (!tablePresent) {
341       LOG.info("The table " + hri.getTable()
342           + " was deleted.  Hence not proceeding.");
343       return false;
344     }
345     // If table is not disabled but the region is offlined,
346     boolean disabled = assignmentManager.getZKTable().isDisabledTable(hri.getTable());
347     if (disabled){
348       LOG.info("The table " + hri.getTable()
349           + " was disabled.  Hence not proceeding.");
350       return false;
351     }
352     if (hri.isOffline() && hri.isSplit()) {
353       //HBASE-7721: Split parent and daughters are inserted into hbase:meta as an atomic operation.
354       //If the meta scanner saw the parent split, then it should see the daughters as assigned
355       //to the dead server. We don't have to do anything.
356       return false;
357     }
358     boolean disabling = assignmentManager.getZKTable().isDisablingTable(hri.getTable());
359     if (disabling) {
360       LOG.info("The table " + hri.getTable()
361           + " is disabled.  Hence not assigning region" + hri.getEncodedName());
362       return false;
363     }
364     return true;
365   }
366 }