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  
20  package org.apache.hadoop.hbase.coprocessor;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.Comparator;
26  import java.util.HashSet;
27  import java.util.List;
28  import java.util.Set;
29  import java.util.TreeSet;
30  import java.util.UUID;
31  import java.util.concurrent.ConcurrentSkipListSet;
32  import java.util.concurrent.ExecutorService;
33  import java.util.concurrent.atomic.AtomicInteger;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.apache.hadoop.hbase.classification.InterfaceAudience;
38  import org.apache.hadoop.hbase.classification.InterfaceStability;
39  import org.apache.hadoop.conf.Configuration;
40  import org.apache.hadoop.fs.Path;
41  import org.apache.hadoop.hbase.Abortable;
42  import org.apache.hadoop.hbase.Coprocessor;
43  import org.apache.hadoop.hbase.CoprocessorEnvironment;
44  import org.apache.hadoop.hbase.DoNotRetryIOException;
45  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
46  import org.apache.hadoop.hbase.TableName;
47  import org.apache.hadoop.hbase.client.HTable;
48  import org.apache.hadoop.hbase.client.HTableInterface;
49  import org.apache.hadoop.hbase.client.HTableWrapper;
50  import org.apache.hadoop.hbase.util.Bytes;
51  import org.apache.hadoop.hbase.util.CoprocessorClassLoader;
52  import org.apache.hadoop.hbase.util.SortedList;
53  import org.apache.hadoop.hbase.util.VersionInfo;
54  
55  /**
56   * Provides the common setup framework and runtime services for coprocessor
57   * invocation from HBase services.
58   * @param <E> the specific environment extension that a concrete implementation
59   * provides
60   */
61  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
62  @InterfaceStability.Evolving
63  public abstract class CoprocessorHost<E extends CoprocessorEnvironment> {
64    public static final String REGION_COPROCESSOR_CONF_KEY =
65        "hbase.coprocessor.region.classes";
66    public static final String REGIONSERVER_COPROCESSOR_CONF_KEY =
67        "hbase.coprocessor.regionserver.classes";
68    public static final String USER_REGION_COPROCESSOR_CONF_KEY =
69        "hbase.coprocessor.user.region.classes";
70    public static final String MASTER_COPROCESSOR_CONF_KEY =
71        "hbase.coprocessor.master.classes";
72    public static final String WAL_COPROCESSOR_CONF_KEY =
73      "hbase.coprocessor.wal.classes";
74    public static final String ABORT_ON_ERROR_KEY = "hbase.coprocessor.abortonerror";
75    public static final boolean DEFAULT_ABORT_ON_ERROR = true;
76    public static final String COPROCESSORS_ENABLED_CONF_KEY = "hbase.coprocessor.enabled";
77    public static final boolean DEFAULT_COPROCESSORS_ENABLED = true;
78    public static final String USER_COPROCESSORS_ENABLED_CONF_KEY =
79      "hbase.coprocessor.user.enabled";
80    public static final boolean DEFAULT_USER_COPROCESSORS_ENABLED = true;
81  
82    private static final Log LOG = LogFactory.getLog(CoprocessorHost.class);
83    protected Abortable abortable;
84    /** Ordered set of loaded coprocessors with lock */
85    protected SortedList<E> coprocessors =
86        new SortedList<E>(new EnvironmentPriorityComparator());
87    protected Configuration conf;
88    // unique file prefix to use for local copies of jars when classloading
89    protected String pathPrefix;
90    protected AtomicInteger loadSequence = new AtomicInteger();
91  
92    public CoprocessorHost(Abortable abortable) {
93      this.abortable = abortable;
94      this.pathPrefix = UUID.randomUUID().toString();
95    }
96  
97    /**
98     * Not to be confused with the per-object _coprocessors_ (above),
99     * coprocessorNames is static and stores the set of all coprocessors ever
100    * loaded by any thread in this JVM. It is strictly additive: coprocessors are
101    * added to coprocessorNames, by loadInstance() but are never removed, since
102    * the intention is to preserve a history of all loaded coprocessors for
103    * diagnosis in case of server crash (HBASE-4014).
104    */
105   private static Set<String> coprocessorNames =
106       Collections.synchronizedSet(new HashSet<String>());
107 
108   public static Set<String> getLoadedCoprocessors() {
109     synchronized (coprocessorNames) {
110       return new HashSet(coprocessorNames);
111     }
112   }
113 
114   /**
115    * Used to create a parameter to the HServerLoad constructor so that
116    * HServerLoad can provide information about the coprocessors loaded by this
117    * regionserver.
118    * (HBASE-4070: Improve region server metrics to report loaded coprocessors
119    * to master).
120    */
121   public Set<String> getCoprocessors() {
122     Set<String> returnValue = new TreeSet<String>();
123     for (CoprocessorEnvironment e: coprocessors) {
124       returnValue.add(e.getInstance().getClass().getSimpleName());
125     }
126     return returnValue;
127   }
128 
129   /**
130    * Load system coprocessors once only. Read the class names from configuration.
131    * Called by constructor.
132    */
133   protected void loadSystemCoprocessors(Configuration conf, String confKey) {
134     boolean coprocessorsEnabled = conf.getBoolean(COPROCESSORS_ENABLED_CONF_KEY,
135       DEFAULT_COPROCESSORS_ENABLED);
136     if (!coprocessorsEnabled) {
137       return;
138     }
139 
140     Class<?> implClass = null;
141 
142     // load default coprocessors from configure file
143     String[] defaultCPClasses = conf.getStrings(confKey);
144     if (defaultCPClasses == null || defaultCPClasses.length == 0)
145       return;
146 
147     int priority = Coprocessor.PRIORITY_SYSTEM;
148     for (String className : defaultCPClasses) {
149       className = className.trim();
150       if (findCoprocessor(className) != null) {
151         // If already loaded will just continue
152         LOG.warn("Attempted duplicate loading of " + className + "; skipped");
153         continue;
154       }
155       ClassLoader cl = this.getClass().getClassLoader();
156       Thread.currentThread().setContextClassLoader(cl);
157       try {
158         implClass = cl.loadClass(className);
159         // Add coprocessors as we go to guard against case where a coprocessor is specified twice
160         // in the configuration
161         this.coprocessors.add(loadInstance(implClass, Coprocessor.PRIORITY_SYSTEM, conf));
162         LOG.info("System coprocessor " + className + " was loaded " +
163             "successfully with priority (" + priority++ + ").");
164       } catch (Throwable t) {
165         // We always abort if system coprocessors cannot be loaded
166         abortServer(className, t);
167       }
168     }
169   }
170 
171   /**
172    * Load a coprocessor implementation into the host
173    * @param path path to implementation jar
174    * @param className the main class name
175    * @param priority chaining priority
176    * @param conf configuration for coprocessor
177    * @throws java.io.IOException Exception
178    */
179   public E load(Path path, String className, int priority,
180       Configuration conf) throws IOException {
181     Class<?> implClass = null;
182     LOG.debug("Loading coprocessor class " + className + " with path " +
183         path + " and priority " + priority);
184 
185     ClassLoader cl = null;
186     if (path == null) {
187       try {
188         implClass = getClass().getClassLoader().loadClass(className);
189       } catch (ClassNotFoundException e) {
190         throw new IOException("No jar path specified for " + className);
191       }
192     } else {
193       cl = CoprocessorClassLoader.getClassLoader(
194         path, getClass().getClassLoader(), pathPrefix, conf);
195       try {
196         implClass = cl.loadClass(className);
197       } catch (ClassNotFoundException e) {
198         throw new IOException("Cannot load external coprocessor class " + className, e);
199       }
200     }
201 
202     //load custom code for coprocessor
203     Thread currentThread = Thread.currentThread();
204     ClassLoader hostClassLoader = currentThread.getContextClassLoader();
205     try{
206       // switch temporarily to the thread classloader for custom CP
207       currentThread.setContextClassLoader(cl);
208       E cpInstance = loadInstance(implClass, priority, conf);
209       return cpInstance;
210     } finally {
211       // restore the fresh (host) classloader
212       currentThread.setContextClassLoader(hostClassLoader);
213     }
214   }
215 
216   /**
217    * @param implClass Implementation class
218    * @param priority priority
219    * @param conf configuration
220    * @throws java.io.IOException Exception
221    */
222   public void load(Class<?> implClass, int priority, Configuration conf)
223       throws IOException {
224     E env = loadInstance(implClass, priority, conf);
225     coprocessors.add(env);
226   }
227 
228   /**
229    * @param implClass Implementation class
230    * @param priority priority
231    * @param conf configuration
232    * @throws java.io.IOException Exception
233    */
234   public E loadInstance(Class<?> implClass, int priority, Configuration conf)
235       throws IOException {
236     if (!Coprocessor.class.isAssignableFrom(implClass)) {
237       throw new IOException("Configured class " + implClass.getName() + " must implement "
238           + Coprocessor.class.getName() + " interface ");
239     }
240 
241     // create the instance
242     Coprocessor impl;
243     Object o = null;
244     try {
245       o = implClass.newInstance();
246       impl = (Coprocessor)o;
247     } catch (InstantiationException e) {
248       throw new IOException(e);
249     } catch (IllegalAccessException e) {
250       throw new IOException(e);
251     }
252     // create the environment
253     E env = createEnvironment(implClass, impl, priority, loadSequence.incrementAndGet(), conf);
254     if (env instanceof Environment) {
255       ((Environment)env).startup();
256     }
257     // HBASE-4014: maintain list of loaded coprocessors for later crash analysis
258     // if server (master or regionserver) aborts.
259     coprocessorNames.add(implClass.getName());
260     return env;
261   }
262 
263   /**
264    * Called when a new Coprocessor class is loaded
265    */
266   public abstract E createEnvironment(Class<?> implClass, Coprocessor instance,
267       int priority, int sequence, Configuration conf);
268 
269   public void shutdown(CoprocessorEnvironment e) {
270     if (e instanceof Environment) {
271       if (LOG.isDebugEnabled()) {
272         LOG.debug("Stop coprocessor " + e.getInstance().getClass().getName());
273       }
274       ((Environment)e).shutdown();
275     } else {
276       LOG.warn("Shutdown called on unknown environment: "+
277           e.getClass().getName());
278     }
279   }
280 
281   /**
282    * Find a coprocessor implementation by class name
283    * @param className the class name
284    * @return the coprocessor, or null if not found
285    */
286   public Coprocessor findCoprocessor(String className) {
287     for (E env: coprocessors) {
288       if (env.getInstance().getClass().getName().equals(className) ||
289           env.getInstance().getClass().getSimpleName().equals(className)) {
290         return env.getInstance();
291       }
292     }
293     return null;
294   }
295 
296   /**
297    * Find list of coprocessors that extend/implement the given class/interface
298    * @param cls the class/interface to look for
299    * @return the list of coprocessors, or null if not found
300    */
301   public <T extends Coprocessor> List<T> findCoprocessors(Class<T> cls) {
302     ArrayList<T> ret = new ArrayList<T>();
303 
304     for (E env: coprocessors) {
305       Coprocessor cp = env.getInstance();
306 
307       if(cp != null) {
308         if (cls.isAssignableFrom(cp.getClass())) {
309           ret.add((T)cp);
310         }
311       }
312     }
313     return ret;
314   }
315 
316   /**
317    * Find a coprocessor environment by class name
318    * @param className the class name
319    * @return the coprocessor, or null if not found
320    */
321   public CoprocessorEnvironment findCoprocessorEnvironment(String className) {
322     for (E env: coprocessors) {
323       if (env.getInstance().getClass().getName().equals(className) ||
324           env.getInstance().getClass().getSimpleName().equals(className)) {
325         return env;
326       }
327     }
328     return null;
329   }
330 
331   /**
332    * Retrieves the set of classloaders used to instantiate Coprocessor classes defined in external
333    * jar files.
334    * @return A set of ClassLoader instances
335    */
336   Set<ClassLoader> getExternalClassLoaders() {
337     Set<ClassLoader> externalClassLoaders = new HashSet<ClassLoader>();
338     final ClassLoader systemClassLoader = this.getClass().getClassLoader();
339     for (E env : coprocessors) {
340       ClassLoader cl = env.getInstance().getClass().getClassLoader();
341       if (cl != systemClassLoader ){
342         //do not include system classloader
343         externalClassLoaders.add(cl);
344       }
345     }
346     return externalClassLoaders;
347   }
348 
349   /**
350    * Environment priority comparator.
351    * Coprocessors are chained in sorted order.
352    */
353   static class EnvironmentPriorityComparator
354       implements Comparator<CoprocessorEnvironment> {
355     @Override
356     public int compare(final CoprocessorEnvironment env1,
357         final CoprocessorEnvironment env2) {
358       if (env1.getPriority() < env2.getPriority()) {
359         return -1;
360       } else if (env1.getPriority() > env2.getPriority()) {
361         return 1;
362       }
363       if (env1.getLoadSequence() < env2.getLoadSequence()) {
364         return -1;
365       } else if (env1.getLoadSequence() > env2.getLoadSequence()) {
366         return 1;
367       }
368       return 0;
369     }
370   }
371 
372   /**
373    * Encapsulation of the environment of each coprocessor
374    */
375   public static class Environment implements CoprocessorEnvironment {
376 
377     /** The coprocessor */
378     public Coprocessor impl;
379     /** Chaining priority */
380     protected int priority = Coprocessor.PRIORITY_USER;
381     /** Current coprocessor state */
382     Coprocessor.State state = Coprocessor.State.UNINSTALLED;
383     /** Accounting for tables opened by the coprocessor */
384     protected List<HTableInterface> openTables =
385       Collections.synchronizedList(new ArrayList<HTableInterface>());
386     private int seq;
387     private Configuration conf;
388     private ClassLoader classLoader;
389 
390     /**
391      * Constructor
392      * @param impl the coprocessor instance
393      * @param priority chaining priority
394      */
395     public Environment(final Coprocessor impl, final int priority,
396         final int seq, final Configuration conf) {
397       this.impl = impl;
398       this.classLoader = impl.getClass().getClassLoader();
399       this.priority = priority;
400       this.state = Coprocessor.State.INSTALLED;
401       this.seq = seq;
402       this.conf = conf;
403     }
404 
405     /** Initialize the environment */
406     public void startup() throws IOException {
407       if (state == Coprocessor.State.INSTALLED ||
408           state == Coprocessor.State.STOPPED) {
409         state = Coprocessor.State.STARTING;
410         Thread currentThread = Thread.currentThread();
411         ClassLoader hostClassLoader = currentThread.getContextClassLoader();
412         try {
413           currentThread.setContextClassLoader(this.getClassLoader());
414           impl.start(this);
415           state = Coprocessor.State.ACTIVE;
416         } finally {
417           currentThread.setContextClassLoader(hostClassLoader);
418         }
419       } else {
420         LOG.warn("Not starting coprocessor "+impl.getClass().getName()+
421             " because not inactive (state="+state.toString()+")");
422       }
423     }
424 
425     /** Clean up the environment */
426     protected void shutdown() {
427       if (state == Coprocessor.State.ACTIVE) {
428         state = Coprocessor.State.STOPPING;
429         Thread currentThread = Thread.currentThread();
430         ClassLoader hostClassLoader = currentThread.getContextClassLoader();
431         try {
432           currentThread.setContextClassLoader(this.getClassLoader());
433           impl.stop(this);
434           state = Coprocessor.State.STOPPED;
435         } catch (IOException ioe) {
436           LOG.error("Error stopping coprocessor "+impl.getClass().getName(), ioe);
437         } finally {
438           currentThread.setContextClassLoader(hostClassLoader);
439         }
440       } else {
441         LOG.warn("Not stopping coprocessor "+impl.getClass().getName()+
442             " because not active (state="+state.toString()+")");
443       }
444       synchronized (openTables) {
445         // clean up any table references
446         for (HTableInterface table: openTables) {
447           try {
448             ((HTableWrapper)table).internalClose();
449           } catch (IOException e) {
450             // nothing can be done here
451             LOG.warn("Failed to close " +
452                 Bytes.toStringBinary(table.getTableName()), e);
453           }
454         }
455       }
456     }
457 
458     @Override
459     public Coprocessor getInstance() {
460       return impl;
461     }
462 
463     @Override
464     public ClassLoader getClassLoader() {
465       return classLoader;
466     }
467 
468     @Override
469     public int getPriority() {
470       return priority;
471     }
472 
473     @Override
474     public int getLoadSequence() {
475       return seq;
476     }
477 
478     /** @return the coprocessor environment version */
479     @Override
480     public int getVersion() {
481       return Coprocessor.VERSION;
482     }
483 
484     /** @return the HBase release */
485     @Override
486     public String getHBaseVersion() {
487       return VersionInfo.getVersion();
488     }
489 
490     @Override
491     public Configuration getConfiguration() {
492       return conf;
493     }
494 
495     /**
496      * Open a table from within the Coprocessor environment
497      * @param tableName the table name
498      * @return an interface for manipulating the table
499      * @exception java.io.IOException Exception
500      */
501     @Override
502     public HTableInterface getTable(TableName tableName) throws IOException {
503       return this.getTable(tableName, HTable.getDefaultExecutor(getConfiguration()));
504     }
505 
506     /**
507      * Open a table from within the Coprocessor environment
508      * @param tableName the table name
509      * @return an interface for manipulating the table
510      * @exception java.io.IOException Exception
511      */
512     @Override
513     public HTableInterface getTable(TableName tableName, ExecutorService pool) throws IOException {
514       return HTableWrapper.createWrapper(openTables, tableName, this, pool);
515     }
516   }
517 
518   protected void abortServer(final CoprocessorEnvironment environment, final Throwable e) {
519     abortServer(environment.getInstance().getClass().getName(), e);
520   }
521 
522   protected void abortServer(final String coprocessorName, final Throwable e) {
523     String message = "The coprocessor " + coprocessorName + " threw " + e.toString();
524     LOG.error(message, e);
525     if (abortable != null) {
526       abortable.abort(message, e);
527     } else {
528       LOG.warn("No available Abortable, process was not aborted");
529     }
530   }
531 
532   /**
533    * This is used by coprocessor hooks which are declared to throw IOException
534    * (or its subtypes). For such hooks, we should handle throwable objects
535    * depending on the Throwable's type. Those which are instances of
536    * IOException should be passed on to the client. This is in conformance with
537    * the HBase idiom regarding IOException: that it represents a circumstance
538    * that should be passed along to the client for its own handling. For
539    * example, a coprocessor that implements access controls would throw a
540    * subclass of IOException, such as AccessDeniedException, in its preGet()
541    * method to prevent an unauthorized client's performing a Get on a particular
542    * table.
543    * @param env Coprocessor Environment
544    * @param e Throwable object thrown by coprocessor.
545    * @exception IOException Exception
546    */
547   protected void handleCoprocessorThrowable(final CoprocessorEnvironment env, final Throwable e)
548       throws IOException {
549     if (e instanceof IOException) {
550       throw (IOException)e;
551     }
552     // If we got here, e is not an IOException. A loaded coprocessor has a
553     // fatal bug, and the server (master or regionserver) should remove the
554     // faulty coprocessor from its set of active coprocessors. Setting
555     // 'hbase.coprocessor.abortonerror' to true will cause abortServer(),
556     // which may be useful in development and testing environments where
557     // 'failing fast' for error analysis is desired.
558     if (env.getConfiguration().getBoolean(ABORT_ON_ERROR_KEY, DEFAULT_ABORT_ON_ERROR)) {
559       // server is configured to abort.
560       abortServer(env, e);
561     } else {
562       LOG.error("Removing coprocessor '" + env.toString() + "' from " +
563           "environment because it threw:  " + e,e);
564       coprocessors.remove(env);
565       try {
566         shutdown(env);
567       } catch (Exception x) {
568         LOG.error("Uncaught exception when shutting down coprocessor '"
569             + env.toString() + "'", x);
570       }
571       throw new DoNotRetryIOException("Coprocessor: '" + env.toString() +
572           "' threw: '" + e + "' and has been removed from the active " +
573           "coprocessor set.", e);
574     }
575   }
576 
577   /**
578    * Used to gracefully handle fallback to deprecated methods when we
579    * evolve coprocessor APIs.
580    *
581    * When a particular Coprocessor API is updated to change methods, hosts can support fallback
582    * to the deprecated API by using this method to determine if an instance implements the new API.
583    * In the event that said support is partial, then in the face of a runtime issue that prevents
584    * proper operation {@link #legacyWarning(Class, String)} should be used to let operators know.
585    *
586    * For examples of this in action, see the implementation of
587    * <ul>
588    *   <li>{@link org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost}
589    *   <li>{@link org.apache.hadoop.hbase.regionserver.wal.WALCoprocessorHost}
590    * </ul>
591    *
592    * @param clazz Coprocessor you wish to evaluate
593    * @param methodName the name of the non-deprecated method version
594    * @param parameterTypes the Class of the non-deprecated method's arguments in the order they are
595    *     declared.
596    */
597   @InterfaceAudience.Private
598   protected static boolean useLegacyMethod(final Class<? extends Coprocessor> clazz,
599       final String methodName, final Class<?>... parameterTypes) {
600     boolean useLegacy;
601     // Use reflection to see if they implement the non-deprecated version
602     try {
603       clazz.getDeclaredMethod(methodName, parameterTypes);
604       LOG.debug("Found an implementation of '" + methodName + "' that uses updated method " +
605           "signature. Skipping legacy support for invocations in '" + clazz +"'.");
606       useLegacy = false;
607     } catch (NoSuchMethodException exception) {
608       useLegacy = true;
609     } catch (SecurityException exception) {
610       LOG.warn("The Security Manager denied our attempt to detect if the coprocessor '" + clazz +
611           "' requires legacy support; assuming it does. If you get later errors about legacy " +
612           "coprocessor use, consider updating your security policy to allow access to the package" +
613           " and declared members of your implementation.");
614       LOG.debug("Details of Security Manager rejection.", exception);
615       useLegacy = true;
616     }
617     return useLegacy;
618   }
619 
620   /**
621    * Used to limit legacy handling to once per Coprocessor class per classloader.
622    */
623   private static final Set<Class<? extends Coprocessor>> legacyWarning =
624       new ConcurrentSkipListSet<Class<? extends Coprocessor>>(
625           new Comparator<Class<? extends Coprocessor>>() {
626             @Override
627             public int compare(Class<? extends Coprocessor> c1, Class<? extends Coprocessor> c2) {
628               if (c1.equals(c2)) {
629                 return 0;
630               }
631               return c1.getName().compareTo(c2.getName());
632             }
633           });
634 
635   /**
636    * limits the amount of logging to once per coprocessor class.
637    * Used in concert with {@link #useLegacyMethod(Class, String, Class[])} when a runtime issue
638    * prevents properly supporting the legacy version of a coprocessor API.
639    * Since coprocessors can be in tight loops this serves to limit the amount of log spam we create.
640    */
641   @InterfaceAudience.Private
642   protected void legacyWarning(final Class<? extends Coprocessor> clazz, final String message) {
643     if(legacyWarning.add(clazz)) {
644       LOG.error("You have a legacy coprocessor loaded and there are events we can't map to the " +
645           " deprecated API. Your coprocessor will not see these events.  Please update '" + clazz +
646           "'. Details of the problem: " + message);
647     }
648   }
649 }