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 shutdown(env);
63 }
64 }
65
66
67
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
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 }