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.util.List;
23  import java.util.concurrent.ExecutorService;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.HRegionInfo;
30  import org.apache.hadoop.hbase.Server;
31  import org.apache.hadoop.hbase.TableNotEnabledException;
32  import org.apache.hadoop.hbase.TableNotFoundException;
33  import org.apache.hadoop.hbase.MetaTableAccessor;
34  import org.apache.hadoop.hbase.client.TableState;
35  import org.apache.hadoop.hbase.constraint.ConstraintException;
36  import org.apache.hadoop.hbase.executor.EventHandler;
37  import org.apache.hadoop.hbase.executor.EventType;
38  import org.apache.hadoop.hbase.master.AssignmentManager;
39  import org.apache.hadoop.hbase.master.BulkAssigner;
40  import org.apache.hadoop.hbase.master.HMaster;
41  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
42  import org.apache.hadoop.hbase.master.RegionState;
43  import org.apache.hadoop.hbase.master.RegionStates;
44  import org.apache.hadoop.hbase.master.TableLockManager;
45  import org.apache.hadoop.hbase.master.TableLockManager.TableLock;
46  import org.htrace.Trace;
47  
48  /**
49   * Handler to run disable of a table.
50   */
51  @InterfaceAudience.Private
52  public class DisableTableHandler extends EventHandler {
53    private static final Log LOG = LogFactory.getLog(DisableTableHandler.class);
54    private final TableName tableName;
55    private final AssignmentManager assignmentManager;
56    private final TableLockManager tableLockManager;
57    private final boolean skipTableStateCheck;
58    private TableLock tableLock;
59  
60    public DisableTableHandler(Server server, TableName tableName,
61        AssignmentManager assignmentManager, TableLockManager tableLockManager,
62        boolean skipTableStateCheck) {
63      super(server, EventType.C_M_DISABLE_TABLE);
64      this.tableName = tableName;
65      this.assignmentManager = assignmentManager;
66      this.tableLockManager = tableLockManager;
67      this.skipTableStateCheck = skipTableStateCheck;
68    }
69  
70    public DisableTableHandler prepare()
71        throws TableNotFoundException, TableNotEnabledException, IOException {
72      if(tableName.equals(TableName.META_TABLE_NAME)) {
73        throw new ConstraintException("Cannot disable catalog table");
74      }
75      //acquire the table write lock, blocking
76      this.tableLock = this.tableLockManager.writeLock(tableName,
77          EventType.C_M_DISABLE_TABLE.toString());
78      this.tableLock.acquire();
79  
80      boolean success = false;
81      try {
82        // Check if table exists
83        if (!MetaTableAccessor.tableExists(this.server.getConnection(), tableName)) {
84          throw new TableNotFoundException(tableName);
85        }
86  
87        // There could be multiple client requests trying to disable or enable
88        // the table at the same time. Ensure only the first request is honored
89        // After that, no other requests can be accepted until the table reaches
90        // DISABLED or ENABLED.
91        //TODO: reevaluate this since we have table locks now
92        if (!skipTableStateCheck) {
93          if (!this.assignmentManager.getTableStateManager().setTableStateIfInStates(
94            this.tableName, TableState.State.DISABLING,
95            TableState.State.ENABLED)) {
96            LOG.info("Table " + tableName + " isn't enabled; skipping disable");
97            throw new TableNotEnabledException(this.tableName);
98          }
99        }
100       success = true;
101     } finally {
102       if (!success) {
103         releaseTableLock();
104       }
105     }
106 
107     return this;
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         tableName;
118   }
119 
120   @Override
121   public void process() {
122     try {
123       LOG.info("Attempting to disable table " + this.tableName);
124       MasterCoprocessorHost cpHost = ((HMaster) this.server)
125           .getMasterCoprocessorHost();
126       if (cpHost != null) {
127         cpHost.preDisableTableHandler(this.tableName);
128       }
129       handleDisableTable();
130       if (cpHost != null) {
131         cpHost.postDisableTableHandler(this.tableName);
132       }
133     } catch (IOException e) {
134       LOG.error("Error trying to disable table " + this.tableName, e);
135     } finally {
136       releaseTableLock();
137     }
138   }
139 
140   private void releaseTableLock() {
141     if (this.tableLock != null) {
142       try {
143         this.tableLock.release();
144       } catch (IOException ex) {
145         LOG.warn("Could not release the table lock", ex);
146       }
147     }
148   }
149 
150   private void handleDisableTable() throws IOException {
151     // Set table disabling flag up in zk.
152     this.assignmentManager.getTableStateManager().setTableState(this.tableName,
153       TableState.State.DISABLING);
154     boolean done = false;
155     while (true) {
156       // Get list of online regions that are of this table.  Regions that are
157       // already closed will not be included in this list; i.e. the returned
158       // list is not ALL regions in a table, its all online regions according
159       // to the in-memory state on this master.
160       final List<HRegionInfo> regions = this.assignmentManager
161         .getRegionStates().getRegionsOfTable(tableName);
162       if (regions.size() == 0) {
163         done = true;
164         break;
165       }
166       LOG.info("Offlining " + regions.size() + " regions.");
167       BulkDisabler bd = new BulkDisabler(this.server, regions);
168       try {
169         if (bd.bulkAssign()) {
170           done = true;
171           break;
172         }
173       } catch (InterruptedException e) {
174         LOG.warn("Disable was interrupted");
175         // Preserve the interrupt.
176         Thread.currentThread().interrupt();
177         break;
178       }
179     }
180     // Flip the table to disabled if success.
181     if (done) this.assignmentManager.getTableStateManager().setTableState(this.tableName,
182       TableState.State.DISABLED);
183     LOG.info("Disabled table, " + this.tableName + ", is done=" + done);
184   }
185 
186   /**
187    * Run bulk disable.
188    */
189   class BulkDisabler extends BulkAssigner {
190     private final List<HRegionInfo> regions;
191 
192     BulkDisabler(final Server server, final List<HRegionInfo> regions) {
193       super(server);
194       this.regions = regions;
195     }
196 
197     @Override
198     protected void populatePool(ExecutorService pool) {
199       RegionStates regionStates = assignmentManager.getRegionStates();
200       for (HRegionInfo region: regions) {
201         if (regionStates.isRegionInTransition(region)
202             && !regionStates.isRegionInState(region, RegionState.State.FAILED_CLOSE)) {
203           continue;
204         }
205         final HRegionInfo hri = region;
206         pool.execute(Trace.wrap("DisableTableHandler.BulkDisabler",new Runnable() {
207           public void run() {
208             assignmentManager.unassign(hri);
209           }
210         }));
211       }
212     }
213 
214     @Override
215     protected boolean waitUntilDone(long timeout)
216     throws InterruptedException {
217       long startTime = System.currentTimeMillis();
218       long remaining = timeout;
219       List<HRegionInfo> regions = null;
220       while (!server.isStopped() && remaining > 0) {
221         Thread.sleep(waitingTimeForEvents);
222         regions = assignmentManager.getRegionStates().getRegionsOfTable(tableName);
223         LOG.debug("Disable waiting until done; " + remaining + " ms remaining; " + regions);
224         if (regions.isEmpty()) break;
225         remaining = timeout - (System.currentTimeMillis() - startTime);
226       }
227       return regions != null && regions.isEmpty();
228     }
229   }
230 }