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  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.classification.InterfaceAudience;
27  import org.apache.hadoop.fs.FileSystem;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.TableName;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.Server;
32  import org.apache.hadoop.hbase.backup.HFileArchiver;
33  import org.apache.hadoop.hbase.catalog.MetaEditor;
34  import org.apache.hadoop.hbase.executor.EventType;
35  import org.apache.hadoop.hbase.master.AssignmentManager;
36  import org.apache.hadoop.hbase.master.HMaster;
37  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
38  import org.apache.hadoop.hbase.master.MasterFileSystem;
39  import org.apache.hadoop.hbase.master.MasterServices;
40  import org.apache.hadoop.hbase.master.RegionStates;
41  import org.apache.hadoop.hbase.master.RegionState.State;
42  import org.apache.hadoop.hbase.util.Threads;
43  import org.apache.zookeeper.KeeperException;
44  
45  @InterfaceAudience.Private
46  public class DeleteTableHandler extends TableEventHandler {
47    private static final Log LOG = LogFactory.getLog(DeleteTableHandler.class);
48  
49    public DeleteTableHandler(TableName tableName, Server server,
50        final MasterServices masterServices) {
51      super(EventType.C_M_DELETE_TABLE, tableName, server, masterServices);
52    }
53  
54    @Override
55    protected void prepareWithTableLock() throws IOException {
56      // The next call fails if no such table.
57      getTableDescriptor();
58    }
59  
60    @Override
61    protected void handleTableOperation(List<HRegionInfo> regions)
62    throws IOException, KeeperException {
63      MasterCoprocessorHost cpHost = ((HMaster) this.server)
64          .getCoprocessorHost();
65      if (cpHost != null) {
66        cpHost.preDeleteTableHandler(this.tableName);
67      }
68  
69      // 1. Wait because of region in transition
70      AssignmentManager am = this.masterServices.getAssignmentManager();
71      RegionStates states = am.getRegionStates();
72      long waitTime = server.getConfiguration().
73        getLong("hbase.master.wait.on.region", 5 * 60 * 1000);
74      for (HRegionInfo region : regions) {
75        long done = System.currentTimeMillis() + waitTime;
76        while (System.currentTimeMillis() < done) {
77          if (states.isRegionInState(region, State.FAILED_OPEN)) {
78            am.regionOffline(region);
79          }
80          if (!states.isRegionInTransition(region)) break;
81          Threads.sleep(waitingTimeForEvents);
82          LOG.debug("Waiting on region to clear regions in transition; "
83            + am.getRegionStates().getRegionTransitionState(region));
84        }
85        if (states.isRegionInTransition(region)) {
86          throw new IOException("Waited hbase.master.wait.on.region (" +
87            waitTime + "ms) for region to leave region " +
88            region.getRegionNameAsString() + " in transitions");
89        }
90      }
91  
92      // 2. Remove regions from META
93      LOG.debug("Deleting regions from META");
94      MetaEditor.deleteRegions(this.server.getCatalogTracker(), regions);
95  
96      // 3. Move the table in /hbase/.tmp
97      MasterFileSystem mfs = this.masterServices.getMasterFileSystem();
98      Path tempTableDir = mfs.moveTableToTemp(tableName);
99  
100     try {
101       // 4. Delete regions from FS (temp directory)
102       FileSystem fs = mfs.getFileSystem();
103       for (HRegionInfo hri: regions) {
104         LOG.debug("Archiving region " + hri.getRegionNameAsString() + " from FS");
105         HFileArchiver.archiveRegion(fs, mfs.getRootDir(),
106             tempTableDir, new Path(tempTableDir, hri.getEncodedName()));
107       }
108 
109       // 5. Delete table from FS (temp directory)
110       if (!fs.delete(tempTableDir, true)) {
111         LOG.error("Couldn't delete " + tempTableDir);
112       }
113 
114       LOG.debug("Table '" + tableName + "' archived!");
115     } finally {
116       // 6. Update table descriptor cache
117       LOG.debug("Removing '" + tableName + "' descriptor.");
118       this.masterServices.getTableDescriptors().remove(tableName);
119 
120       // 7. Clean up regions of the table in RegionStates.
121       LOG.debug("Removing '" + tableName + "' from region states.");
122       states.tableDeleted(tableName);
123 
124       // 8. If entry for this table in zk, and up in AssignmentManager, remove it.
125       LOG.debug("Marking '" + tableName + "' as deleted.");
126       am.getZKTable().setDeletedTable(tableName);
127     }
128 
129     if (cpHost != null) {
130       cpHost.postDeleteTableHandler(this.tableName);
131     }
132   }
133 
134   @Override
135   protected void releaseTableLock() {
136     super.releaseTableLock();
137     try {
138       masterServices.getTableLockManager().tableDeleted(tableName);
139     } catch (IOException ex) {
140       LOG.warn("Received exception from TableLockManager.tableDeleted:", ex); //not critical
141     }
142   }
143 
144   @Override
145   public String toString() {
146     String name = "UnknownServerName";
147     if(server != null && server.getServerName() != null) {
148       name = server.getServerName().toString();
149     }
150     return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" + tableName;
151   }
152 }