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.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.HColumnDescriptor;
26  import org.apache.hadoop.hbase.HRegionInfo;
27  import org.apache.hadoop.hbase.HTableDescriptor;
28  import org.apache.hadoop.hbase.exceptions.InvalidFamilyOperationException;
29  import org.apache.hadoop.hbase.Server;
30  import org.apache.hadoop.hbase.executor.EventType;
31  import org.apache.hadoop.hbase.master.HMaster;
32  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
33  import org.apache.hadoop.hbase.master.MasterServices;
34  
35  /**
36   * Handles adding a new family to an existing table.
37   */
38  @InterfaceAudience.Private
39  public class TableAddFamilyHandler extends TableEventHandler {
40  
41    private final HColumnDescriptor familyDesc;
42  
43    public TableAddFamilyHandler(byte[] tableName, HColumnDescriptor familyDesc,
44        Server server, final MasterServices masterServices) {
45      super(EventType.C_M_ADD_FAMILY, tableName, server, masterServices);
46      this.familyDesc = familyDesc;
47    }
48  
49    @Override
50    protected void prepareWithTableLock() throws IOException {
51      super.prepareWithTableLock();
52      HTableDescriptor htd = getTableDescriptor();
53      if (htd.hasFamily(familyDesc.getName())) {
54        throw new InvalidFamilyOperationException("Family '" +
55          familyDesc.getNameAsString() + "' already exists so cannot be added");
56      }
57    }
58  
59    @Override
60    protected void handleTableOperation(List<HRegionInfo> hris)
61    throws IOException {
62      MasterCoprocessorHost cpHost = ((HMaster) this.server)
63          .getCoprocessorHost();
64      if(cpHost != null){
65        cpHost.preAddColumnHandler(this.tableName, this.familyDesc);
66      }
67      // Update table descriptor
68      this.masterServices.getMasterFileSystem().addColumn(tableName, familyDesc);
69      if(cpHost != null){
70        cpHost.postAddColumnHandler(this.tableName, this.familyDesc);
71      }
72    }
73  
74    @Override
75    public String toString() {
76      String name = "UnknownServerName";
77      if(server != null && server.getServerName() != null) {
78        name = server.getServerName().toString();
79      }
80      String family = "UnknownFamily";
81      if(familyDesc != null) {
82        family = familyDesc.getNameAsString();
83      }
84      return getClass().getSimpleName() + "-" + name + "-" +
85                                getSeqid() + "-" + tableNameStr + "-" + family;
86    }
87  
88  }