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