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.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.HRegionInfo;
32  import org.apache.hadoop.hbase.HTableDescriptor;
33  import org.apache.hadoop.hbase.Server;
34  import org.apache.hadoop.hbase.MetaTableAccessor;
35  import org.apache.hadoop.hbase.client.HTable;
36  import org.apache.hadoop.hbase.client.Result;
37  import org.apache.hadoop.hbase.client.ResultScanner;
38  import org.apache.hadoop.hbase.client.Scan;
39  import org.apache.hadoop.hbase.executor.EventType;
40  import org.apache.hadoop.hbase.master.HMaster;
41  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
42  import org.apache.hadoop.hbase.master.MasterFileSystem;
43  import org.apache.hadoop.hbase.master.MasterServices;
44  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
45  import org.apache.hadoop.hbase.util.Bytes;
46  
47  @InterfaceAudience.Private
48  public class ModifyTableHandler extends TableEventHandler {
49    private static final Log LOG = LogFactory.getLog(ModifyTableHandler.class);
50  
51    private final HTableDescriptor htd;
52  
53    public ModifyTableHandler(final TableName tableName,
54        final HTableDescriptor htd, final Server server,
55        final MasterServices masterServices) {
56      super(EventType.C_M_MODIFY_TABLE, tableName, server, masterServices);
57      // This is the new schema we are going to write out as this modification.
58      this.htd = htd;
59    }
60  
61    @Override
62    protected void prepareWithTableLock() throws IOException {
63      super.prepareWithTableLock();
64      // Check operation is possible on the table in its current state
65      // Also checks whether the table exists
66      if (masterServices.getAssignmentManager().getTableStateManager()
67          .isTableState(this.htd.getTableName(), ZooKeeperProtos.Table.State.ENABLED)
68          && this.htd.getRegionReplication() != getTableDescriptor().getRegionReplication()) {
69        throw new IOException("REGION_REPLICATION change is not supported for enabled tables");
70      }
71    }
72  
73    @Override
74    protected void handleTableOperation(List<HRegionInfo> hris)
75    throws IOException {
76      MasterCoprocessorHost cpHost = ((HMaster) this.server).getMasterCoprocessorHost();
77      if (cpHost != null) {
78        cpHost.preModifyTableHandler(this.tableName, this.htd);
79      }
80      // Update descriptor
81      HTableDescriptor oldHtd = getTableDescriptor();
82      this.masterServices.getTableDescriptors().add(this.htd);
83      deleteFamilyFromFS(hris, oldHtd.getFamiliesKeys());
84      removeReplicaColumnsIfNeeded(this.htd.getRegionReplication(), oldHtd.getRegionReplication(),
85          htd.getTableName());
86      if (cpHost != null) {
87        cpHost.postModifyTableHandler(this.tableName, this.htd);
88      }
89    }
90  
91    private void removeReplicaColumnsIfNeeded(int newReplicaCount, int oldReplicaCount,
92        TableName table) throws IOException {
93      if (newReplicaCount >= oldReplicaCount) return;
94      Set<byte[]> tableRows = new HashSet<byte[]>();
95      Scan scan = MetaTableAccessor.getScanForTableName(table);
96      scan.addColumn(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
97      HTable htable = null;
98      try {
99        htable = new HTable(masterServices.getConfiguration(), TableName.META_TABLE_NAME);
100       ResultScanner resScanner = htable.getScanner(scan);
101       for (Result result : resScanner) {
102         tableRows.add(result.getRow());
103       }
104       MetaTableAccessor.removeRegionReplicasFromMeta(tableRows, newReplicaCount,
105           oldReplicaCount - newReplicaCount, masterServices.getShortCircuitConnection());
106     } finally {
107       if (htable != null) {
108         htable.close();
109       }
110     }
111   }
112 
113   /**
114    * Removes from hdfs the families that are not longer present in the new table descriptor.
115    */
116   private void deleteFamilyFromFS(final List<HRegionInfo> hris, final Set<byte[]> oldFamilies) {
117     try {
118       Set<byte[]> newFamilies = this.htd.getFamiliesKeys();
119       MasterFileSystem mfs = this.masterServices.getMasterFileSystem();
120       for (byte[] familyName: oldFamilies) {
121         if (!newFamilies.contains(familyName)) {
122           LOG.debug("Removing family=" + Bytes.toString(familyName) +
123                     " from table=" + this.tableName);
124           for (HRegionInfo hri: hris) {
125             // Delete the family directory in FS for all the regions one by one
126             mfs.deleteFamilyFromFS(hri, familyName);
127           }
128         }
129       }
130     } catch (IOException e) {
131       LOG.warn("Unable to remove on-disk directories for the removed families", e);
132     }
133   }
134 
135   @Override
136   public String toString() {
137     String name = "UnknownServerName";
138     if(server != null && server.getServerName() != null) {
139       name = server.getServerName().toString();
140     }
141     return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" +
142       tableName;
143   }
144 }