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  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,
38        Configuration conf) {
39      this.rsServices = rsServices;
40      this.conf = conf;
41      // load system default cp's from configuration.
42      loadSystemCoprocessors(conf, REGIONSERVER_COPROCESSOR_CONF_KEY);
43    }
44  
45    @Override
46    public RegionServerEnvironment createEnvironment(Class<?> implClass,
47        Coprocessor instance, int priority, int sequence, Configuration conf) {
48      return new RegionServerEnvironment(implClass, instance, priority,
49        sequence, conf, this.rsServices);
50    }
51  
52    public void preStop(String message) throws IOException {
53      ObserverContext<RegionServerCoprocessorEnvironment> ctx = null;
54      for (RegionServerEnvironment env : coprocessors) {
55        if (env.getInstance() instanceof RegionServerObserver) {
56          ctx = ObserverContext.createAndPrepare(env, ctx);
57          ((RegionServerObserver) env.getInstance()).preStopRegionServer(ctx);
58          if (ctx.shouldComplete()) {
59            break;
60          }
61        }
62      }
63    }
64  
65    /**
66     * Coprocessor environment extension providing access to region server
67     * related services.
68     */
69    static class RegionServerEnvironment extends CoprocessorHost.Environment
70        implements RegionServerCoprocessorEnvironment {
71  
72      private RegionServerServices regionServerServices;
73  
74      public RegionServerEnvironment(final Class<?> implClass,
75          final Coprocessor impl, final int priority, final int seq,
76          final Configuration conf, 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
89     * order.
90     */
91    static class EnvironmentPriorityComparator implements
92        Comparator<CoprocessorEnvironment> {
93      public int compare(final CoprocessorEnvironment env1,
94          final CoprocessorEnvironment env2) {
95        if (env1.getPriority() < env2.getPriority()) {
96          return -1;
97        } else if (env1.getPriority() > env2.getPriority()) {
98          return 1;
99        }
100       if (env1.getLoadSequence() < env2.getLoadSequence()) {
101         return -1;
102       } else if (env1.getLoadSequence() > env2.getLoadSequence()) {
103         return 1;
104       }
105       return 0;
106     }
107   }
108 }