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.util.ArrayList;
23  import java.util.HashSet;
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.classification.InterfaceAudience;
32  import org.apache.hadoop.hbase.HConstants;
33  import org.apache.hadoop.hbase.HRegionInfo;
34  import org.apache.hadoop.hbase.Server;
35  import org.apache.hadoop.hbase.ServerName;
36  import org.apache.hadoop.hbase.catalog.CatalogTracker;
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.executor.EventType;
41  import org.apache.hadoop.hbase.master.AssignmentManager;
42  import org.apache.hadoop.hbase.master.DeadServer;
43  import org.apache.hadoop.hbase.master.MasterServices;
44  import org.apache.hadoop.hbase.master.RegionState;
45  import org.apache.hadoop.hbase.master.RegionStates;
46  import org.apache.hadoop.hbase.master.ServerManager;
47  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
48  import org.apache.zookeeper.KeeperException;
49  
50  /**
51   * Process server shutdown.
52   * Server-to-handle must be already in the deadservers lists.  See
53   * {@link ServerManager#expireServer(ServerName)}
54   */
55  @InterfaceAudience.Private
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    protected final boolean distributedLogReplay;
63    protected final int regionAssignmentWaitTimeout;
64  
65    public ServerShutdownHandler(final Server server, final MasterServices services,
66        final DeadServer deadServers, final ServerName serverName,
67        final boolean shouldSplitHlog) {
68      this(server, services, deadServers, serverName, EventType.M_SERVER_SHUTDOWN,
69          shouldSplitHlog);
70    }
71  
72    ServerShutdownHandler(final Server server, final MasterServices services,
73        final DeadServer deadServers, final ServerName serverName, EventType type,
74        final boolean shouldSplitHlog) {
75      super(server, type);
76      this.serverName = serverName;
77      this.server = server;
78      this.services = services;
79      this.deadServers = deadServers;
80      if (!this.deadServers.isDeadServer(this.serverName)) {
81        LOG.warn(this.serverName + " is NOT in deadservers; it should be!");
82      }
83      this.shouldSplitHlog = shouldSplitHlog;
84      this.distributedLogReplay = server.getConfiguration().getBoolean(
85            HConstants.DISTRIBUTED_LOG_REPLAY_KEY, 
86            HConstants.DEFAULT_DISTRIBUTED_LOG_REPLAY_CONFIG);
87      this.regionAssignmentWaitTimeout = server.getConfiguration().getInt(
88        HConstants.LOG_REPLAY_WAIT_REGION_TIMEOUT, 15000);
89    }
90  
91    @Override
92    public String getInformativeName() {
93      if (serverName != null) {
94        return this.getClass().getSimpleName() + " for " + serverName;
95      } else {
96        return super.getInformativeName();
97      }
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 
121       // We don't want worker thread in the MetaServerShutdownHandler
122       // executor pool to block by waiting availability of .META.
123       // Otherwise, it could run into the following issue:
124       // 1. The current MetaServerShutdownHandler instance For RS1 waits for the .META.
125       //    to come online.
126       // 2. The newly assigned .META. region server RS2 was shutdown right after
127       //    it opens the .META. region. So the MetaServerShutdownHandler
128       //    instance For RS1 will still be blocked.
129       // 3. The new instance of MetaServerShutdownHandler for RS2 is queued.
130       // 4. The newly assigned .META. region server RS3 was shutdown right after
131       //    it opens the .META. region. So the MetaServerShutdownHandler
132       //    instance For RS1 and RS2 will still be blocked.
133       // 5. The new instance of MetaServerShutdownHandler for RS3 is queued.
134       // 6. Repeat until we run out of MetaServerShutdownHandler worker threads
135       // The solution here is to resubmit a ServerShutdownHandler request to process
136       // user regions on that server so that MetaServerShutdownHandler
137       // executor pool is always available.
138       //
139       // If AssignmentManager hasn't finished rebuilding user regions,
140       // we are not ready to assign dead regions either. So we re-queue up
141       // the dead server for further processing too.
142       if (isCarryingMeta() // .META.
143           || !services.getAssignmentManager().isFailoverCleanupDone()) {
144         this.services.getServerManager().processDeadServer(serverName, this.shouldSplitHlog);
145         return;
146       }
147 
148       // Wait on meta to come online; we need it to progress.
149       // TODO: Best way to hold strictly here?  We should build this retry logic
150       // into the MetaReader operations themselves.
151       // TODO: Is the reading of .META. necessary when the Master has state of
152       // cluster in its head?  It should be possible to do without reading .META.
153       // in all but one case. On split, the RS updates the .META.
154       // table and THEN informs the master of the split via zk nodes in
155       // 'unassigned' dir.  Currently the RS puts ephemeral nodes into zk so if
156       // the regionserver dies, these nodes do not stick around and this server
157       // shutdown processing does fixup (see the fixupDaughters method below).
158       // If we wanted to skip the .META. scan, we'd have to change at least the
159       // final SPLIT message to be permanent in zk so in here we'd know a SPLIT
160       // completed (zk is updated after edits to .META. have gone in).  See
161       // {@link SplitTransaction}.  We'd also have to be figure another way for
162       // doing the below .META. daughters fixup.
163       NavigableMap<HRegionInfo, Result> hris = null;
164       while (!this.server.isStopped()) {
165         try {
166           this.server.getCatalogTracker().waitForMeta();
167           hris = MetaReader.getServerUserRegions(this.server.getCatalogTracker(),
168             this.serverName);
169           break;
170         } catch (InterruptedException e) {
171           Thread.currentThread().interrupt();
172           throw new IOException("Interrupted", e);
173         } catch (IOException ioe) {
174           LOG.info("Received exception accessing META during server shutdown of " +
175               serverName + ", retrying META read", ioe);
176         }
177       }
178       if (this.server.isStopped()) {
179         throw new IOException("Server is stopped");
180       }
181 
182       try {
183         if (this.shouldSplitHlog) {
184           LOG.info("Splitting logs for " + serverName + " before assignment.");
185           if(this.distributedLogReplay){
186             Set<ServerName> serverNames = new HashSet<ServerName>();
187             serverNames.add(serverName);
188             this.services.getMasterFileSystem().prepareLogReplay(serverNames);
189           } else {
190             this.services.getMasterFileSystem().splitLog(serverName);
191           }
192         } else {
193           LOG.info("Skipping log splitting for " + serverName);
194         }
195       } catch (IOException ioe) {
196         resubmit(serverName, ioe);
197       }
198 
199       // Clean out anything in regions in transition.  Being conservative and
200       // doing after log splitting.  Could do some states before -- OPENING?
201       // OFFLINE? -- and then others after like CLOSING that depend on log
202       // splitting.
203       AssignmentManager am = services.getAssignmentManager();
204       List<HRegionInfo> regionsInTransition = am.processServerShutdown(serverName);
205       LOG.info("Reassigning " + ((hris == null)? 0: hris.size()) +
206         " region(s) that " + (serverName == null? "null": serverName)  +
207         " was carrying (and " + regionsInTransition.size() +
208         " regions(s) that were opening on this server)");
209 
210       List<HRegionInfo> toAssignRegions = new ArrayList<HRegionInfo>();
211       toAssignRegions.addAll(regionsInTransition);
212 
213       // Iterate regions that were on this server and assign them
214       if (hris != null) {
215         RegionStates regionStates = am.getRegionStates();
216         for (Map.Entry<HRegionInfo, Result> e: hris.entrySet()) {
217           HRegionInfo hri = e.getKey();
218           if (regionsInTransition.contains(hri)) {
219             continue;
220           }
221           RegionState rit = regionStates.getRegionTransitionState(hri);
222           if (processDeadRegion(hri, e.getValue(), am, server.getCatalogTracker())) {
223             ServerName addressFromAM = regionStates.getRegionServerOfRegion(hri);
224             if (addressFromAM != null && !addressFromAM.equals(this.serverName)) {
225               // If this region is in transition on the dead server, it must be
226               // opening or pending_open, which should have been covered by AM#processServerShutdown
227               LOG.info("Skip assigning region " + hri.getRegionNameAsString()
228                 + " because it has been opened in " + addressFromAM.getServerName());
229               continue;
230             }
231             if (rit != null) {
232               if (!rit.isOnServer(serverName)
233                   || rit.isClosed() || rit.isOpened() || rit.isSplit()) {
234                 // Skip regions that are in transition on other server,
235                 // or in state closed/opened/split
236                 LOG.info("Skip assigning region " + rit);
237                 continue;
238               }
239               try{
240                 //clean zk node
241                 LOG.info("Reassigning region with rs = " + rit + " and deleting zk node if exists");
242                 ZKAssign.deleteNodeFailSilent(services.getZooKeeper(), hri);
243               } catch (KeeperException ke) {
244                 this.server.abort("Unexpected ZK exception deleting unassigned node " + hri, ke);
245                 return;
246               }
247             }
248             toAssignRegions.add(hri);
249           } else if (rit != null) {
250             if (rit.isSplitting() || rit.isSplit()) {
251               // This will happen when the RS went down and the call back for the SPLIITING or SPLIT
252               // has not yet happened for node Deleted event. In that case if the region was actually
253               // split
254               // but the RS had gone down before completing the split process then will not try to
255               // assign the parent region again. In that case we should make the region offline and
256               // also delete the region from RIT.
257               am.regionOffline(hri);
258             } else if ((rit.isClosing() || rit.isPendingClose())
259                 && am.getZKTable().isDisablingOrDisabledTable(hri.getTableNameAsString())) {
260               // If the table was partially disabled and the RS went down, we should clear the RIT
261               // and remove the node for the region.
262               // The rit that we use may be stale in case the table was in DISABLING state
263               // but though we did assign we will not be clearing the znode in CLOSING state.
264               // Doing this will have no harm. See HBASE-5927
265               am.deleteClosingOrClosedNode(hri);
266               am.regionOffline(hri);
267             } else {
268               LOG.warn("THIS SHOULD NOT HAPPEN: unexpected region in transition "
269                 + rit + " not to be assigned by SSH of server " + serverName);
270             }
271           }
272         }
273       }
274 
275       try {
276         am.assign(toAssignRegions);
277       } catch (InterruptedException ie) {
278         LOG.error("Caught " + ie + " during round-robin assignment");
279         throw new IOException(ie);
280       }
281 
282       try {
283         if (this.shouldSplitHlog && this.distributedLogReplay) {
284           // wait for region assignment completes
285           for (HRegionInfo hri : toAssignRegions) {
286             if (!am.waitOnRegionToClearRegionsInTransition(hri, regionAssignmentWaitTimeout)) {
287               throw new IOException("Region " + hri.getEncodedName()
288                   + " didn't complete assignment in time");
289             }
290           }
291           this.services.getMasterFileSystem().splitLog(serverName);
292         }
293       } catch (Exception ex) {
294         if (ex instanceof IOException) {
295           resubmit(serverName, (IOException)ex);
296         } else {
297           throw new IOException(ex);
298         }
299       }
300     } finally {
301       this.deadServers.finish(serverName);
302     }
303 
304     LOG.info("Finished processing of shutdown of " + serverName);
305   }
306 
307   private void resubmit(final ServerName serverName, IOException ex) throws IOException {
308     // typecast to SSH so that we make sure that it is the SSH instance that
309     // gets submitted as opposed to MSSH or some other derived instance of SSH
310     this.services.getExecutorService().submit((ServerShutdownHandler) this);
311     this.deadServers.add(serverName);
312     throw new IOException("failed log splitting for " + serverName + ", will retry", ex);
313   }
314 
315   /**
316    * Process a dead region from a dead RS. Checks if the region is disabled or
317    * disabling or if the region has a partially completed split.
318    * @param hri
319    * @param result
320    * @param assignmentManager
321    * @param catalogTracker
322    * @return Returns true if specified region should be assigned, false if not.
323    * @throws IOException
324    */
325   public static boolean processDeadRegion(HRegionInfo hri, Result result,
326       AssignmentManager assignmentManager, CatalogTracker catalogTracker)
327   throws IOException {
328     boolean tablePresent = assignmentManager.getZKTable().isTablePresent(
329         hri.getTableNameAsString());
330     if (!tablePresent) {
331       LOG.info("The table " + hri.getTableNameAsString()
332           + " was deleted.  Hence not proceeding.");
333       return false;
334     }
335     // If table is not disabled but the region is offlined,
336     boolean disabled = assignmentManager.getZKTable().isDisabledTable(
337         hri.getTableNameAsString());
338     if (disabled){
339       LOG.info("The table " + hri.getTableNameAsString()
340           + " was disabled.  Hence not proceeding.");
341       return false;
342     }
343     if (hri.isOffline() && hri.isSplit()) {
344       //HBASE-7721: Split parent and daughters are inserted into META as an atomic operation.
345       //If the meta scanner saw the parent split, then it should see the daughters as assigned
346       //to the dead server. We don't have to do anything.
347       return false;
348     }
349     boolean disabling = assignmentManager.getZKTable().isDisablingTable(
350         hri.getTableNameAsString());
351     if (disabling) {
352       LOG.info("The table " + hri.getTableNameAsString()
353           + " is disabled.  Hence not assigning region" + hri.getEncodedName());
354       return false;
355     }
356     return true;
357   }
358 }