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.List;
024import java.util.Set;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.yetus.audience.InterfaceStability;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031/**
032 * Provides the common setup framework and runtime services for globally barriered procedure
033 * invocation from HBase services.
034 * @param <E> the specific procedure management extension that a concrete implementation provides
035 */
036@InterfaceAudience.Private
037@InterfaceStability.Evolving
038public abstract class ProcedureManagerHost<E extends ProcedureManager> {
039
040  public static final String REGIONSERVER_PROCEDURE_CONF_KEY =
041    "hbase.procedure.regionserver.classes";
042  public static final String MASTER_PROCEDURE_CONF_KEY = "hbase.procedure.master.classes";
043
044  private static final Logger LOG = LoggerFactory.getLogger(ProcedureManagerHost.class);
045
046  protected Set<E> procedures = new HashSet<>();
047
048  /**
049   * Load system procedures. Read the class names from configuration. Called by constructor.
050   */
051  protected void loadUserProcedures(Configuration conf, String confKey) {
052    Class<?> implClass = null;
053
054    // load default procedures from configure file
055    String[] defaultProcClasses = conf.getStrings(confKey);
056    if (defaultProcClasses == null || defaultProcClasses.length == 0) return;
057
058    List<E> configured = new ArrayList<>();
059    for (String className : defaultProcClasses) {
060      className = className.trim();
061      ClassLoader cl = this.getClass().getClassLoader();
062      Thread.currentThread().setContextClassLoader(cl);
063      try {
064        implClass = cl.loadClass(className);
065        configured.add(loadInstance(implClass));
066        LOG.info("User procedure " + className + " was loaded successfully.");
067      } catch (ClassNotFoundException e) {
068        LOG.warn("Class " + className + " cannot be found. " + e.getMessage());
069      } catch (IOException e) {
070        LOG.warn("Load procedure " + className + " failed. " + e.getMessage());
071      }
072    }
073
074    // add entire set to the collection
075    procedures.addAll(configured);
076  }
077
078  @SuppressWarnings("unchecked")
079  public E loadInstance(Class<?> implClass) throws IOException {
080    // create the instance
081    E impl;
082    Object o = null;
083    try {
084      o = implClass.getDeclaredConstructor().newInstance();
085      impl = (E) o;
086    } catch (Exception e) {
087      throw new IOException(e);
088    }
089
090    return impl;
091  }
092
093  // Register a procedure manager object
094  public void register(E obj) {
095    procedures.add(obj);
096  }
097
098  public Set<E> getProcedureManagers() {
099    Set<E> returnValue = new HashSet<>();
100    for (E e : procedures) {
101      returnValue.add(e);
102    }
103    return returnValue;
104  }
105
106  public abstract void loadProcedures(Configuration conf);
107}