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