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.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.fs.FileSystem;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.HBaseFileSystem;
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.master.AssignmentManager;
35  import org.apache.hadoop.hbase.master.MasterFileSystem;
36  import org.apache.hadoop.hbase.master.MasterServices;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.apache.hadoop.hbase.util.Threads;
39  import org.apache.zookeeper.KeeperException;
40  
41  public class DeleteTableHandler extends TableEventHandler {
42    private static final Log LOG = LogFactory.getLog(DeleteTableHandler.class);
43  
44    public DeleteTableHandler(byte [] tableName, Server server,
45        final MasterServices masterServices)
46    throws IOException {
47      super(EventType.C_M_DELETE_TABLE, tableName, server, masterServices);
48      // The next call fails if no such table.
49      getTableDescriptor();
50    }
51  
52    @Override
53    protected void handleTableOperation(List<HRegionInfo> regions)
54    throws IOException, KeeperException {
55      // 1. Wait because of region in transition
56      AssignmentManager am = this.masterServices.getAssignmentManager();
57      long waitTime = server.getConfiguration().
58        getLong("hbase.master.wait.on.region", 5 * 60 * 1000);
59      for (HRegionInfo region : regions) {
60        long done = System.currentTimeMillis() + waitTime;
61        while (System.currentTimeMillis() < done) {
62          AssignmentManager.RegionState rs = am.isRegionInTransition(region);
63          if (rs == null) break;
64          Threads.sleep(waitingTimeForEvents);
65          LOG.debug("Waiting on  region to clear regions in transition; " + rs);
66        }
67        if (am.isRegionInTransition(region) != null) {
68          throw new IOException("Waited hbase.master.wait.on.region (" +
69            waitTime + "ms) for region to leave region " +
70            region.getRegionNameAsString() + " in transitions");
71        }
72      }
73  
74      // 2. Remove regions from META
75      LOG.debug("Deleting regions from META");
76      MetaEditor.deleteRegions(this.server.getCatalogTracker(), regions);
77  
78      // 3. Move the table in /hbase/.tmp
79      LOG.debug("Moving table directory to a temp directory");
80      MasterFileSystem mfs = this.masterServices.getMasterFileSystem();
81      Path tempTableDir = mfs.moveTableToTemp(tableName);
82  
83      try {
84        // 4. Delete regions from FS (temp directory)
85        FileSystem fs = mfs.getFileSystem();
86        for (HRegionInfo hri: regions) {
87          LOG.debug("Archiving region " + hri.getRegionNameAsString() + " from FS");
88          HFileArchiver.archiveRegion(fs, mfs.getRootDir(),
89              tempTableDir, new Path(tempTableDir, hri.getEncodedName()));
90        }
91  
92        // 5. Delete table from FS (temp directory)
93        if (!HBaseFileSystem.deleteDirFromFileSystem(fs, tempTableDir)) {
94          LOG.error("Couldn't delete " + tempTableDir);
95        }
96      } finally {
97        // 6. Update table descriptor cache
98        this.masterServices.getTableDescriptors().remove(Bytes.toString(tableName));
99  
100       // 7. If entry for this table in zk, and up in AssignmentManager, remove it.
101       am.getZKTable().setDeletedTable(Bytes.toString(tableName));
102     }
103   }
104 
105   @Override
106   public String toString() {
107     String name = "UnknownServerName";
108     if(server != null && server.getServerName() != null) {
109       name = server.getServerName().toString();
110     }
111     return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" + tableNameStr;
112   }
113 }