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,
38 Configuration conf) {
39 this.rsServices = rsServices;
40 this.conf = conf;
41
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
67
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
89
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 }