1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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 }
62 }
63
64
65
66
67 static class RegionServerEnvironment extends CoprocessorHost.Environment implements
68 RegionServerCoprocessorEnvironment {
69
70 private RegionServerServices regionServerServices;
71
72 public RegionServerEnvironment(final Class<?> implClass, final Coprocessor impl,
73 final int priority, final int seq, final Configuration conf,
74 final RegionServerServices services) {
75 super(impl, priority, seq, conf);
76 this.regionServerServices = services;
77 }
78
79 @Override
80 public RegionServerServices getRegionServerServices() {
81 return regionServerServices;
82 }
83 }
84
85
86
87
88 static class EnvironmentPriorityComparator implements Comparator<CoprocessorEnvironment> {
89 public int compare(final CoprocessorEnvironment env1, final CoprocessorEnvironment env2) {
90 if (env1.getPriority() < env2.getPriority()) {
91 return -1;
92 } else if (env1.getPriority() > env2.getPriority()) {
93 return 1;
94 }
95 if (env1.getLoadSequence() < env2.getLoadSequence()) {
96 return -1;
97 } else if (env1.getLoadSequence() > env2.getLoadSequence()) {
98 return 1;
99 }
100 return 0;
101 }
102 }
103
104 }