1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.namequeues;
21
22 import com.lmax.disruptor.EventHandler;
23 import com.lmax.disruptor.RingBuffer;
24 import java.lang.reflect.InvocationTargetException;
25 import java.util.HashMap;
26 import java.util.Map;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.hbase.namequeues.request.NamedQueueGetRequest;
30 import org.apache.hadoop.hbase.namequeues.response.NamedQueueGetResponse;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37
38
39 @InterfaceAudience.Private
40 class LogEventHandler implements EventHandler<RingBufferEnvelope> {
41
42 private static final Logger LOG = LoggerFactory.getLogger(LogEventHandler.class);
43
44
45
46
47
48
49 private final Map<NamedQueuePayload.NamedQueueEvent, NamedQueueService> namedQueueServices =
50 new HashMap<>();
51
52 private static final String NAMED_QUEUE_PROVIDER_CLASSES = "hbase.namedqueue.provider.classes";
53
54 LogEventHandler(final Configuration conf) {
55 for (String implName : conf.getStringCollection(NAMED_QUEUE_PROVIDER_CLASSES)) {
56 Class<?> clz;
57 try {
58 clz = Class.forName(implName);
59 } catch (ClassNotFoundException e) {
60 LOG.warn("Failed to find NamedQueueService implementor class {}", implName, e);
61 continue;
62 }
63
64 if (!NamedQueueService.class.isAssignableFrom(clz)) {
65 LOG.warn("Class {} is not implementor of NamedQueueService.", clz);
66 continue;
67 }
68
69
70 try {
71 NamedQueueService namedQueueService =
72 (NamedQueueService) clz.getConstructor(Configuration.class).newInstance(conf);
73 namedQueueServices.put(namedQueueService.getEvent(), namedQueueService);
74 } catch (InstantiationException | IllegalAccessException | NoSuchMethodException
75 | InvocationTargetException e) {
76 LOG.warn("Unable to instantiate/add NamedQueueService implementor {} to service map.",
77 clz);
78 }
79 }
80 }
81
82
83
84
85
86
87
88
89
90
91
92 @Override
93 public void onEvent(RingBufferEnvelope event, long sequence, boolean endOfBatch) {
94 final NamedQueuePayload namedQueuePayload = event.getPayload();
95
96 namedQueueServices.get(namedQueuePayload.getNamedQueueEvent())
97 .consumeEventFromDisruptor(namedQueuePayload);
98 }
99
100
101
102
103
104
105
106 boolean clearNamedQueue(NamedQueuePayload.NamedQueueEvent namedQueueEvent) {
107 return namedQueueServices.get(namedQueueEvent).clearNamedQueue();
108 }
109
110
111
112
113
114 void persistAll(NamedQueuePayload.NamedQueueEvent namedQueueEvent) {
115 namedQueueServices.get(namedQueueEvent).persistAll();
116 }
117
118
119
120
121
122
123
124 NamedQueueGetResponse getNamedQueueRecords(NamedQueueGetRequest request) {
125 return namedQueueServices.get(request.getNamedQueueEvent()).getNamedQueueRecords(request);
126 }
127
128 }