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.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.TableName;
26  import org.apache.hadoop.hbase.HRegionInfo;
27  import org.apache.hadoop.hbase.HTableDescriptor;
28  import org.apache.hadoop.hbase.Server;
29  import org.apache.hadoop.hbase.executor.EventType;
30  import org.apache.hadoop.hbase.InvalidFamilyOperationException;
31  import org.apache.hadoop.hbase.master.HMaster;
32  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
33  import org.apache.hadoop.hbase.master.MasterFileSystem;
34  import org.apache.hadoop.hbase.master.MasterServices;
35  import org.apache.hadoop.hbase.util.Bytes;
36  
37  /**
38   * Handles adding a new family to an existing table.
39   */
40  @InterfaceAudience.Private
41  public class TableDeleteFamilyHandler extends TableEventHandler {
42  
43    private byte [] familyName;
44  
45    public TableDeleteFamilyHandler(TableName tableName, byte [] familyName,
46        Server server, final MasterServices masterServices) throws IOException {
47      super(EventType.C_M_DELETE_FAMILY, tableName, server, masterServices);
48      this.familyName = familyName;
49    }
50  
51    @Override
52    protected void prepareWithTableLock() throws IOException {
53      super.prepareWithTableLock();
54      HTableDescriptor htd = getTableDescriptor();
55      this.familyName = hasColumnFamily(htd, familyName);
56  
57      if (htd.getColumnFamilies().length == 1) {
58        throw new InvalidFamilyOperationException("Family '" + Bytes.toString(familyName)
59          + "' is the only column family in the table, so it cannot be deleted");
60      }
61    }
62  
63    @Override
64    protected void handleTableOperation(List<HRegionInfo> hris) throws IOException {
65      MasterCoprocessorHost cpHost = ((HMaster) this.server)
66          .getCoprocessorHost();
67      if (cpHost != null) {
68        cpHost.preDeleteColumnHandler(this.tableName, this.familyName);
69      }
70      // Update table descriptor
71      this.masterServices.getMasterFileSystem().deleteColumn(tableName, familyName);
72      // Remove the column family from the file system
73      MasterFileSystem mfs = this.masterServices.getMasterFileSystem();
74      for (HRegionInfo hri : hris) {
75        // Delete the family directory in FS for all the regions one by one
76        mfs.deleteFamilyFromFS(hri, familyName);
77      }
78      if (cpHost != null) {
79        cpHost.postDeleteColumnHandler(this.tableName, this.familyName);
80      }
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      String family = "UnknownFamily";
90      if(familyName != null) {
91        family = Bytes.toString(familyName);
92      }
93      return getClass().getSimpleName() + "-" + name + "-" + getSeqid() +
94          "-" + tableName + "-" + family;
95    }
96  }