/
ModCluster.java
283 lines (232 loc) · 8.23 KB
/
ModCluster.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed 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 io.undertow.server.handlers.proxy.mod_cluster;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import io.undertow.client.UndertowClient;
import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.ResponseCodeHandler;
import io.undertow.server.handlers.proxy.ProxyHandler;
import org.xnio.OptionMap;
import org.xnio.XnioWorker;
import org.xnio.ssl.XnioSsl;
/**
* @author Emanuel Muckenhuber
*/
public class ModCluster {
private static final HttpHandler NEXT_HANDLER = ResponseCodeHandler.HANDLE_404;
// Health check intervals
private final long healthCheckInterval;
private final long removeBrokenNodes;
private final NodeHealthChecker healthChecker;
// Proxy connection pool defaults
private final int maxConnections;
private final int cacheConnections;
private final int requestQueueSize;
private final boolean queueNewRequests;
private final int maxRequestTime;
private final long ttl;
private final boolean useAlias;
private final XnioWorker xnioWorker;
private final ModClusterContainer container;
private final int maxRetries;
private final String serverID = UUID.randomUUID().toString(); // TODO
ModCluster(Builder builder) {
this.xnioWorker = builder.xnioWorker;
this.maxConnections = builder.maxConnections;
this.cacheConnections = builder.cacheConnections;
this.requestQueueSize = builder.requestQueueSize;
this.queueNewRequests = builder.queueNewRequests;
this.healthCheckInterval = builder.healthCheckInterval;
this.removeBrokenNodes = builder.removeBrokenNodes;
this.healthChecker = builder.healthChecker;
this.maxRequestTime = builder.maxRequestTime;
this.ttl = builder.ttl;
this.useAlias = builder.useAlias;
this.maxRetries = builder.maxRetries;
this.container = new ModClusterContainer(this, builder.xnioSsl, builder.client, builder.clientOptions);
}
protected String getServerID() {
return serverID;
}
protected ModClusterContainer getContainer() {
return container;
}
public ModClusterController getController() {
return container;
}
public int getMaxConnections() {
return maxConnections;
}
public int getCacheConnections() {
return cacheConnections;
}
public int getRequestQueueSize() {
return requestQueueSize;
}
public boolean isQueueNewRequests() {
return queueNewRequests;
}
public long getHealthCheckInterval() {
return healthCheckInterval;
}
public long getRemoveBrokenNodes() {
return removeBrokenNodes;
}
public NodeHealthChecker getHealthChecker() {
return healthChecker;
}
public long getTtl() {
return ttl;
}
public boolean isUseAlias() {
return useAlias;
}
/**
* Get the handler proxying the requests.
*
* @return the proxy handler
*/
@Deprecated
public HttpHandler getProxyHandler() {
return createProxyHandler();
}
/**
* Get the handler proxying the requests.
*
* @return the proxy handler
*/
public HttpHandler createProxyHandler() {
return new ProxyHandler(container.getProxyClient(), maxRequestTime, NEXT_HANDLER, false, false, maxRetries);
}
/**
* Get the handler proxying the requests.
*
* @return the proxy handler
*/
public HttpHandler createProxyHandler(HttpHandler next) {
return new ProxyHandler(container.getProxyClient(), maxRequestTime, next, false, false, maxRetries);
}
/**
* Start
*/
public synchronized void start() {
}
/**
* Start advertising a mcmp handler.
*
* @param config the mcmp handler config
* @throws IOException
*/
public synchronized void advertise(MCMPConfig config) throws IOException {
final MCMPConfig.AdvertiseConfig advertiseConfig = config.getAdvertiseConfig();
if (advertiseConfig == null) {
throw new IllegalArgumentException("advertise not enabled");
}
MCMPAdvertiseTask.advertise(container, advertiseConfig, xnioWorker);
}
/**
* Stop
*/
public synchronized void stop() {
}
public static Builder builder(final XnioWorker worker) {
return builder(worker, UndertowClient.getInstance(), null);
}
public static Builder builder(final XnioWorker worker, final UndertowClient client) {
return builder(worker, client, null);
}
public static Builder builder(final XnioWorker worker, final UndertowClient client, final XnioSsl xnioSsl) {
return new Builder(worker, client, xnioSsl);
}
public static class Builder {
private final XnioSsl xnioSsl;
private final UndertowClient client;
private final XnioWorker xnioWorker;
// Fairly restrictive connection pool defaults
private int maxConnections = 16;
private int cacheConnections = 8;
private int requestQueueSize = 0;
private boolean queueNewRequests = false;
private int maxRequestTime = -1;
private long ttl;
private boolean useAlias = false;
private NodeHealthChecker healthChecker = NodeHealthChecker.NO_CHECK;
private long healthCheckInterval = TimeUnit.SECONDS.toMillis(10);
private long removeBrokenNodes = TimeUnit.MINUTES.toMillis(1);
public OptionMap clientOptions = OptionMap.EMPTY;
public int maxRetries;
private Builder(XnioWorker xnioWorker, UndertowClient client, XnioSsl xnioSsl) {
this.xnioSsl = xnioSsl;
this.client = client;
this.xnioWorker = xnioWorker;
}
public ModCluster build() {
return new ModCluster(this);
}
public Builder setMaxRequestTime(int maxRequestTime) {
this.maxRequestTime = maxRequestTime;
return this;
}
public Builder setHealthCheckInterval(long healthCheckInterval) {
this.healthCheckInterval = healthCheckInterval;
return this;
}
public Builder setRemoveBrokenNodes(long removeBrokenNodes) {
this.removeBrokenNodes = removeBrokenNodes;
return this;
}
public Builder setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections;
return this;
}
public Builder setCacheConnections(int cacheConnections) {
this.cacheConnections = cacheConnections;
return this;
}
public Builder setRequestQueueSize(int requestQueueSize) {
this.requestQueueSize = requestQueueSize;
return this;
}
public Builder setQueueNewRequests(boolean queueNewRequests) {
this.queueNewRequests = queueNewRequests;
return this;
}
public Builder setHealthChecker(NodeHealthChecker healthChecker) {
this.healthChecker = healthChecker;
return this;
}
public Builder setUseAlias(boolean useAlias) {
this.useAlias = useAlias;
return this;
}
public Builder setMaxRetries(int maxRetries) {
this.maxRetries = maxRetries;
return this;
}
public Builder setTtl(long ttl) {
this.ttl = ttl;
return this;
}
public Builder setClientOptions(OptionMap clientOptions) {
this.clientOptions = clientOptions;
return this;
}
}
}