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  import java.util.Set;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.HRegionInfo;
29  import org.apache.hadoop.hbase.HTableDescriptor;
30  import org.apache.hadoop.hbase.Server;
31  import org.apache.hadoop.hbase.executor.EventType;
32  import org.apache.hadoop.hbase.master.HMaster;
33  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
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  
38  @InterfaceAudience.Private
39  public class ModifyTableHandler extends TableEventHandler {
40    private static final Log LOG = LogFactory.getLog(ModifyTableHandler.class);
41  
42    private final HTableDescriptor htd;
43  
44    public ModifyTableHandler(final byte [] tableName,
45        final HTableDescriptor htd, final Server server,
46        final MasterServices masterServices) {
47      super(EventType.C_M_MODIFY_TABLE, tableName, server, masterServices);
48      // This is the new schema we are going to write out as this modification.
49      this.htd = htd;
50    }
51  
52    @Override
53    protected void prepareWithTableLock() throws IOException {
54      super.prepareWithTableLock();
55      // Check table exists.
56      getTableDescriptor();
57    }
58  
59    @Override
60    protected void handleTableOperation(List<HRegionInfo> hris)
61    throws IOException {
62      MasterCoprocessorHost cpHost = ((HMaster) this.server).getCoprocessorHost();
63      if (cpHost != null) {
64        cpHost.preModifyTableHandler(this.tableName, this.htd);
65      }
66      // Update descriptor
67      HTableDescriptor oldHtd = getTableDescriptor();
68      this.masterServices.getTableDescriptors().add(this.htd);
69      deleteFamilyFromFS(hris, oldHtd.getFamiliesKeys());
70      if (cpHost != null) {
71        cpHost.postModifyTableHandler(this.tableName, this.htd);
72      }
73    }
74  
75    /**
76     * Removes from hdfs the families that are not longer present in the new table descriptor.
77     */
78    private void deleteFamilyFromFS(final List<HRegionInfo> hris, final Set<byte[]> oldFamilies) {
79      try {
80        Set<byte[]> newFamilies = this.htd.getFamiliesKeys();
81        MasterFileSystem mfs = this.masterServices.getMasterFileSystem();
82        for (byte[] familyName: oldFamilies) {
83          if (!newFamilies.contains(familyName)) {
84            LOG.debug("Removing family=" + Bytes.toString(familyName) +
85                      " from table=" + this.tableName);
86            for (HRegionInfo hri: hris) {
87              // Delete the family directory in FS for all the regions one by one
88              mfs.deleteFamilyFromFS(hri, familyName);
89            }
90          }
91        }
92      } catch (IOException e) {
93        LOG.warn("Unable to remove on-disk directories for the removed families", e);
94      }
95    }
96  
97    @Override
98    public String toString() {
99      String name = "UnknownServerName";
100     if(server != null && server.getServerName() != null) {
101       name = server.getServerName().toString();
102     }
103     return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" +
104       tableNameStr;
105   }
106 }