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.hbase.HRegionInfo;
28  import org.apache.hadoop.hbase.Server;
29  import org.apache.hadoop.hbase.catalog.MetaEditor;
30  import org.apache.hadoop.hbase.master.AssignmentManager;
31  import org.apache.hadoop.hbase.master.MasterServices;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.apache.hadoop.hbase.util.Threads;
34  import org.apache.zookeeper.KeeperException;
35  
36  public class DeleteTableHandler extends TableEventHandler {
37    private static final Log LOG = LogFactory.getLog(DeleteTableHandler.class);
38  
39    public DeleteTableHandler(byte [] tableName, Server server,
40        final MasterServices masterServices)
41    throws IOException {
42      super(EventType.C_M_DELETE_TABLE, tableName, server, masterServices);
43      // The next call fails if no such table.
44      getTableDescriptor();
45    }
46  
47    @Override
48    protected void handleTableOperation(List<HRegionInfo> regions)
49    throws IOException, KeeperException {
50      AssignmentManager am = this.masterServices.getAssignmentManager();
51      long waitTime = server.getConfiguration().
52        getLong("hbase.master.wait.on.region", 5 * 60 * 1000);
53      for (HRegionInfo region : regions) {
54        long done = System.currentTimeMillis() + waitTime;
55        while (System.currentTimeMillis() < done) {
56          AssignmentManager.RegionState rs = am.isRegionInTransition(region);
57          if (rs == null) break;
58          Threads.sleep(waitingTimeForEvents);
59          LOG.debug("Waiting on  region to clear regions in transition; " + rs);
60        }
61        if (am.isRegionInTransition(region) != null) {
62          throw new IOException("Waited hbase.master.wait.on.region (" +
63            waitTime + "ms) for region to leave region " +
64            region.getRegionNameAsString() + " in transitions");
65        }
66        LOG.debug("Deleting region " + region.getRegionNameAsString() +
67          " from META and FS");
68        // Remove region from META
69        MetaEditor.deleteRegion(this.server.getCatalogTracker(), region);
70        // Delete region from FS
71        this.masterServices.getMasterFileSystem().deleteRegion(region);
72      }
73      // Delete table from FS
74      this.masterServices.getMasterFileSystem().deleteTable(tableName);
75      // Update table descriptor cache
76      this.masterServices.getTableDescriptors().remove(Bytes.toString(tableName));
77  
78      // If entry for this table in zk, and up in AssignmentManager, remove it.
79      // Call to undisableTable does this. TODO: Make a more formal purge table.
80      am.getZKTable().setEnabledTable(Bytes.toString(tableName));
81    }
82    
83    @Override
84    public String toString() {
85      String name = "UnknownServerName";
86      if(server != null && server.getServerName() != null) {
87        name = server.getServerName().toString();
88      }
89      return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" + tableNameStr;
90    }
91  }