001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.procedure;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.HashSet;
023import java.util.Set;
024import java.util.List;
025
026import org.apache.hadoop.conf.Configuration;
027import org.apache.yetus.audience.InterfaceAudience;
028import org.apache.yetus.audience.InterfaceStability;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * Provides the common setup framework and runtime services for globally
034 * barriered procedure invocation from HBase services.
035 * @param <E> the specific procedure management extension that a concrete
036 * implementation provides
037 */
038@InterfaceAudience.Private
039@InterfaceStability.Evolving
040public abstract class ProcedureManagerHost<E extends ProcedureManager> {
041
042  public static final String REGIONSERVER_PROCEDURE_CONF_KEY =
043      "hbase.procedure.regionserver.classes";
044  public static final String MASTER_PROCEDURE_CONF_KEY =
045      "hbase.procedure.master.classes";
046
047  private static final Logger LOG = LoggerFactory.getLogger(ProcedureManagerHost.class);
048
049  protected Set<E> procedures = new HashSet<>();
050
051  /**
052   * Load system procedures. Read the class names from configuration.
053   * Called by constructor.
054   */
055  protected void loadUserProcedures(Configuration conf, String confKey) {
056    Class<?> implClass = null;
057
058    // load default procedures from configure file
059    String[] defaultProcClasses = conf.getStrings(confKey);
060    if (defaultProcClasses == null || defaultProcClasses.length == 0)
061      return;
062
063    List<E> configured = new ArrayList<>();
064    for (String className : defaultProcClasses) {
065      className = className.trim();
066      ClassLoader cl = this.getClass().getClassLoader();
067      Thread.currentThread().setContextClassLoader(cl);
068      try {
069        implClass = cl.loadClass(className);
070        configured.add(loadInstance(implClass));
071        LOG.info("User procedure " + className + " was loaded successfully.");
072      } catch (ClassNotFoundException e) {
073        LOG.warn("Class " + className + " cannot be found. " +
074            e.getMessage());
075      } catch (IOException e) {
076        LOG.warn("Load procedure " + className + " failed. " +
077            e.getMessage());
078      }
079    }
080
081    // add entire set to the collection
082    procedures.addAll(configured);
083  }
084
085  @SuppressWarnings("unchecked")
086  public E loadInstance(Class<?> implClass) throws IOException {
087    // create the instance
088    E impl;
089    Object o = null;
090    try {
091      o = implClass.getDeclaredConstructor().newInstance();
092      impl = (E)o;
093    } catch (Exception e) {
094      throw new IOException(e);
095    }
096
097    return impl;
098  }
099
100  // Register a procedure manager object
101  public void register(E obj) {
102    procedures.add(obj);
103  }
104
105  public Set<E> getProcedureManagers() {
106    Set<E> returnValue = new HashSet<>();
107    for (E e: procedures) {
108      returnValue.add(e);
109    }
110    return returnValue;
111  }
112
113  public abstract void loadProcedures(Configuration conf);
114}