forked from apache/eventmesh
/
EventMeshHTTPServer.java
260 lines (192 loc) · 11 KB
/
EventMeshHTTPServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.eventmesh.runtime.boot;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import com.google.common.eventbus.EventBus;
import org.apache.eventmesh.common.ThreadPoolFactory;
import org.apache.eventmesh.common.protocol.http.common.RequestCode;
import org.apache.eventmesh.runtime.common.ServiceState;
import org.apache.eventmesh.runtime.configuration.EventMeshHTTPConfiguration;
import org.apache.eventmesh.runtime.core.consumergroup.ConsumerGroupConf;
import org.apache.eventmesh.runtime.core.protocol.http.consumer.ConsumerManager;
import org.apache.eventmesh.runtime.core.protocol.http.processor.AdminMetricsProcessor;
import org.apache.eventmesh.runtime.core.protocol.http.processor.BatchSendMessageProcessor;
import org.apache.eventmesh.runtime.core.protocol.http.processor.BatchSendMessageV2Processor;
import org.apache.eventmesh.runtime.core.protocol.http.processor.HeartBeatProcessor;
import org.apache.eventmesh.runtime.core.protocol.http.processor.ReplyMessageProcessor;
import org.apache.eventmesh.runtime.core.protocol.http.processor.SendAsyncMessageProcessor;
import org.apache.eventmesh.runtime.core.protocol.http.processor.SendSyncMessageProcessor;
import org.apache.eventmesh.runtime.core.protocol.http.processor.SubscribeProcessor;
import org.apache.eventmesh.runtime.core.protocol.http.processor.UnSubscribeProcessor;
import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.Client;
import org.apache.eventmesh.runtime.core.protocol.http.producer.ProducerManager;
import org.apache.eventmesh.runtime.core.protocol.http.push.AbstractHTTPPushRequest;
import org.apache.eventmesh.runtime.core.protocol.http.retry.HttpRetryer;
import org.apache.eventmesh.runtime.metrics.http.HTTPMetricsServer;
public class EventMeshHTTPServer extends AbstractHTTPServer {
private EventMeshServer eventMeshServer;
public ServiceState serviceState;
private EventMeshHTTPConfiguration eventMeshHttpConfiguration;
public final ConcurrentHashMap<String /**group*/, ConsumerGroupConf> localConsumerGroupMapping = new ConcurrentHashMap<>();
public final ConcurrentHashMap<String /**group@topic*/, List<Client>> localClientInfoMapping = new ConcurrentHashMap<>();
public EventMeshHTTPServer(EventMeshServer eventMeshServer,
EventMeshHTTPConfiguration eventMeshHttpConfiguration) {
super(eventMeshHttpConfiguration.httpServerPort, eventMeshHttpConfiguration.eventMeshServerUseTls);
this.eventMeshServer = eventMeshServer;
this.eventMeshHttpConfiguration = eventMeshHttpConfiguration;
}
public EventMeshServer getEventMeshServer() {
return eventMeshServer;
}
public EventBus eventBus = new EventBus();
private ConsumerManager consumerManager;
private ProducerManager producerManager;
private HttpRetryer httpRetryer;
public ThreadPoolExecutor batchMsgExecutor;
public ThreadPoolExecutor sendMsgExecutor;
public ThreadPoolExecutor replyMsgExecutor;
public ThreadPoolExecutor pushMsgExecutor;
public ThreadPoolExecutor clientManageExecutor;
public ThreadPoolExecutor adminExecutor;
public void shutdownThreadPool() throws Exception {
batchMsgExecutor.shutdown();
adminExecutor.shutdown();
clientManageExecutor.shutdown();
sendMsgExecutor.shutdown();
pushMsgExecutor.shutdown();
replyMsgExecutor.shutdown();
}
public void initThreadPool() throws Exception {
BlockingQueue<Runnable> batchMsgThreadPoolQueue = new LinkedBlockingQueue<Runnable>(eventMeshHttpConfiguration.eventMeshServerBatchBlockQSize);
batchMsgExecutor = ThreadPoolFactory.createThreadPoolExecutor(eventMeshHttpConfiguration.eventMeshServerBatchMsgThreadNum,
eventMeshHttpConfiguration.eventMeshServerBatchMsgThreadNum, batchMsgThreadPoolQueue, "eventMesh-batchMsg-", true);
BlockingQueue<Runnable> sendMsgThreadPoolQueue = new LinkedBlockingQueue<Runnable>(eventMeshHttpConfiguration.eventMeshServerSendMsgBlockQSize);
sendMsgExecutor = ThreadPoolFactory.createThreadPoolExecutor(eventMeshHttpConfiguration.eventMeshServerSendMsgThreadNum,
eventMeshHttpConfiguration.eventMeshServerSendMsgThreadNum, sendMsgThreadPoolQueue, "eventMesh-sendMsg-", true);
BlockingQueue<Runnable> pushMsgThreadPoolQueue = new LinkedBlockingQueue<Runnable>(eventMeshHttpConfiguration.eventMeshServerPushMsgBlockQSize);
pushMsgExecutor = ThreadPoolFactory.createThreadPoolExecutor(eventMeshHttpConfiguration.eventMeshServerPushMsgThreadNum,
eventMeshHttpConfiguration.eventMeshServerPushMsgThreadNum, pushMsgThreadPoolQueue, "eventMesh-pushMsg-", true);
BlockingQueue<Runnable> clientManageThreadPoolQueue = new LinkedBlockingQueue<Runnable>(eventMeshHttpConfiguration.eventMeshServerClientManageBlockQSize);
clientManageExecutor = ThreadPoolFactory.createThreadPoolExecutor(eventMeshHttpConfiguration.eventMeshServerClientManageThreadNum,
eventMeshHttpConfiguration.eventMeshServerClientManageThreadNum, clientManageThreadPoolQueue, "eventMesh-clientManage-", true);
BlockingQueue<Runnable> adminThreadPoolQueue = new LinkedBlockingQueue<Runnable>(50);
adminExecutor = ThreadPoolFactory.createThreadPoolExecutor(eventMeshHttpConfiguration.eventMeshServerAdminThreadNum,
eventMeshHttpConfiguration.eventMeshServerAdminThreadNum, adminThreadPoolQueue, "eventMesh-admin-", true);
BlockingQueue<Runnable> replyMessageThreadPoolQueue = new LinkedBlockingQueue<Runnable>(100);
replyMsgExecutor = ThreadPoolFactory.createThreadPoolExecutor(eventMeshHttpConfiguration.eventMeshServerReplyMsgThreadNum,
eventMeshHttpConfiguration.eventMeshServerReplyMsgThreadNum, replyMessageThreadPoolQueue, "eventMesh-replyMsg-", true);
}
public ThreadPoolExecutor getBatchMsgExecutor() {
return batchMsgExecutor;
}
public ThreadPoolExecutor getSendMsgExecutor() {
return sendMsgExecutor;
}
public ThreadPoolExecutor getReplyMsgExecutor() {
return replyMsgExecutor;
}
public ThreadPoolExecutor getPushMsgExecutor() {
return pushMsgExecutor;
}
public ThreadPoolExecutor getClientManageExecutor() {
return clientManageExecutor;
}
public ThreadPoolExecutor getAdminExecutor() {
return adminExecutor;
}
public void init() throws Exception {
logger.info("==================EventMeshHTTPServer Initialing==================");
super.init("eventMesh-http");
initThreadPool();
metrics = new HTTPMetricsServer(this);
metrics.init();
consumerManager = new ConsumerManager(this);
consumerManager.init();
producerManager = new ProducerManager(this);
producerManager.init();
httpRetryer = new HttpRetryer(this);
httpRetryer.init();
registerHTTPRequestProcessor();
logger.info("--------------------------EventMeshHTTPServer inited");
}
@Override
public void start() throws Exception {
super.start();
metrics.start();
consumerManager.start();
producerManager.start();
httpRetryer.start();
logger.info("--------------------------EventMeshHTTPServer started");
}
@Override
public void shutdown() throws Exception {
super.shutdown();
metrics.shutdown();
consumerManager.shutdown();
shutdownThreadPool();
AbstractHTTPPushRequest.httpClientPool.shutdown();
producerManager.shutdown();
httpRetryer.shutdown();
logger.info("--------------------------EventMeshHTTPServer shutdown");
}
public void registerHTTPRequestProcessor() {
BatchSendMessageProcessor batchSendMessageProcessor = new BatchSendMessageProcessor(this);
registerProcessor(RequestCode.MSG_BATCH_SEND.getRequestCode(),
batchSendMessageProcessor, batchMsgExecutor);
BatchSendMessageV2Processor batchSendMessageV2Processor = new BatchSendMessageV2Processor(this);
registerProcessor(RequestCode.MSG_BATCH_SEND_V2.getRequestCode(),
batchSendMessageV2Processor, batchMsgExecutor);
SendSyncMessageProcessor sendSyncMessageProcessor = new SendSyncMessageProcessor(this);
registerProcessor(RequestCode.MSG_SEND_SYNC.getRequestCode(),
sendSyncMessageProcessor, sendMsgExecutor);
SendAsyncMessageProcessor sendAsyncMessageProcessor = new SendAsyncMessageProcessor(this);
registerProcessor(RequestCode.MSG_SEND_ASYNC.getRequestCode(),
sendAsyncMessageProcessor, sendMsgExecutor);
AdminMetricsProcessor adminMetricsProcessor = new AdminMetricsProcessor(this);
registerProcessor(RequestCode.ADMIN_METRICS.getRequestCode(), adminMetricsProcessor, adminExecutor);
HeartBeatProcessor heartProcessor = new HeartBeatProcessor(this);
registerProcessor(RequestCode.HEARTBEAT.getRequestCode(), heartProcessor, clientManageExecutor);
SubscribeProcessor subscribeProcessor = new SubscribeProcessor(this);
registerProcessor(RequestCode.SUBSCRIBE.getRequestCode(), subscribeProcessor, clientManageExecutor);
UnSubscribeProcessor unSubscribeProcessor = new UnSubscribeProcessor(this);
registerProcessor(RequestCode.UNSUBSCRIBE.getRequestCode(), unSubscribeProcessor, clientManageExecutor);
ReplyMessageProcessor replyMessageProcessor = new ReplyMessageProcessor(this);
registerProcessor(RequestCode.REPLY_MESSAGE.getRequestCode(), replyMessageProcessor, replyMsgExecutor);
}
public ConsumerManager getConsumerManager() {
return consumerManager;
}
public ProducerManager getProducerManager() {
return producerManager;
}
public ServiceState getServiceState() {
return serviceState;
}
public EventMeshHTTPConfiguration getEventMeshHttpConfiguration() {
return eventMeshHttpConfiguration;
}
public EventBus getEventBus() {
return eventBus;
}
public HttpRetryer getHttpRetryer() {
return httpRetryer;
}
}