View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.io.IOException;
22  import java.util.Comparator;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.Coprocessor;
26  import org.apache.hadoop.hbase.CoprocessorEnvironment;
27  import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
28  import org.apache.hadoop.hbase.coprocessor.ObserverContext;
29  import org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessorEnvironment;
30  import org.apache.hadoop.hbase.coprocessor.RegionServerObserver;
31  
32  public class RegionServerCoprocessorHost extends
33      CoprocessorHost<RegionServerCoprocessorHost.RegionServerEnvironment> {
34  
35    private RegionServerServices rsServices;
36  
37    public RegionServerCoprocessorHost(RegionServerServices rsServices, Configuration conf) {
38      this.rsServices = rsServices;
39      this.conf = conf;
40      // load system default cp's from configuration.
41      loadSystemCoprocessors(conf, REGIONSERVER_COPROCESSOR_CONF_KEY);
42    }
43  
44    @Override
45    public RegionServerEnvironment createEnvironment(Class<?> implClass, Coprocessor instance,
46        int priority, int sequence, Configuration conf) {
47      return new RegionServerEnvironment(implClass, instance, priority, sequence, conf,
48          this.rsServices);
49    }
50  
51    public void preStop(String message) throws IOException {
52      ObserverContext<RegionServerCoprocessorEnvironment> ctx = null;
53      for (RegionServerEnvironment env : coprocessors) {
54        if (env.getInstance() instanceof RegionServerObserver) {
55          ctx = ObserverContext.createAndPrepare(env, ctx);
56          ((RegionServerObserver) env.getInstance()).preStopRegionServer(ctx);
57          if (ctx.shouldComplete()) {
58            break;
59          }
60        }
61        // invoke coprocessor stop method
62        shutdown(env);
63      }
64    }
65  
66    /**
67     * Coprocessor environment extension providing access to region server related services.
68     */
69    static class RegionServerEnvironment extends CoprocessorHost.Environment implements
70        RegionServerCoprocessorEnvironment {
71  
72      private RegionServerServices regionServerServices;
73  
74      public RegionServerEnvironment(final Class<?> implClass, final Coprocessor impl,
75          final int priority, final int seq, final Configuration conf,
76          final RegionServerServices services) {
77        super(impl, priority, seq, conf);
78        this.regionServerServices = services;
79      }
80  
81      @Override
82      public RegionServerServices getRegionServerServices() {
83        return regionServerServices;
84      }
85    }
86  
87    /**
88     * Environment priority comparator. Coprocessors are chained in sorted order.
89     */
90    static class EnvironmentPriorityComparator implements Comparator<CoprocessorEnvironment> {
91      public int compare(final CoprocessorEnvironment env1, final CoprocessorEnvironment env2) {
92        if (env1.getPriority() < env2.getPriority()) {
93          return -1;
94        } else if (env1.getPriority() > env2.getPriority()) {
95          return 1;
96        }
97        if (env1.getLoadSequence() < env2.getLoadSequence()) {
98          return -1;
99        } else if (env1.getLoadSequence() > env2.getLoadSequence()) {
100         return 1;
101       }
102       return 0;
103     }
104   }
105 
106 }