View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.master.handler;
21  
22  import java.io.IOException;
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.HRegionInfo;
28  import org.apache.hadoop.hbase.Server;
29  import org.apache.hadoop.hbase.catalog.MetaReader;
30  import org.apache.hadoop.hbase.executor.EventHandler;
31  import org.apache.hadoop.hbase.master.MasterServices;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.apache.zookeeper.KeeperException;
34  
35  /**
36   * Base class for performing operations against tables.
37   * Checks on whether the process can go forward are done in constructor rather
38   * than later on in {@link #process()}.  The idea is to fail fast rather than
39   * later down in an async invocation of {@link #process()} (which currently has
40   * no means of reporting back issues once started).
41   */
42  public abstract class TableEventHandler extends EventHandler {
43    private static final Log LOG = LogFactory.getLog(TableEventHandler.class);
44    protected final MasterServices masterServices;
45    protected final byte [] tableName;
46    protected final String tableNameStr;
47  
48    public TableEventHandler(EventType eventType, byte [] tableName, Server server,
49        MasterServices masterServices)
50    throws IOException {
51      super(server, eventType);
52      this.masterServices = masterServices;
53      this.tableName = tableName;
54      this.masterServices.checkTableModifiable(tableName);
55      this.tableNameStr = Bytes.toString(this.tableName);
56    }
57  
58    @Override
59    public void process() {
60      try {
61        LOG.info("Handling table operation " + eventType + " on table " +
62            Bytes.toString(tableName));
63        List<HRegionInfo> hris =
64          MetaReader.getTableRegions(this.server.getCatalogTracker(),
65            tableName);
66        handleTableOperation(hris);
67      } catch (IOException e) {
68        LOG.error("Error manipulating table " + Bytes.toString(tableName), e);
69      } catch (KeeperException e) {
70        LOG.error("Error manipulating table " + Bytes.toString(tableName), e);
71      }
72    }
73  
74    protected abstract void handleTableOperation(List<HRegionInfo> regions)
75    throws IOException, KeeperException;
76  }