-
Notifications
You must be signed in to change notification settings - Fork 74
/
MetricsServiceLifecycle.java
452 lines (399 loc) · 17.5 KB
/
MetricsServiceLifecycle.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* Copyright 2014-2016 Red Hat, Inc. and/or its affiliates
* and other 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 org.hawkular.metrics.api.jaxrs;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.hawkular.metrics.api.jaxrs.config.ConfigurationKey.CASSANDRA_CQL_PORT;
import static org.hawkular.metrics.api.jaxrs.config.ConfigurationKey.CASSANDRA_KEYSPACE;
import static org.hawkular.metrics.api.jaxrs.config.ConfigurationKey.CASSANDRA_MAX_CONN_HOST;
import static org.hawkular.metrics.api.jaxrs.config.ConfigurationKey.CASSANDRA_MAX_REQUEST_CONN;
import static org.hawkular.metrics.api.jaxrs.config.ConfigurationKey.CASSANDRA_NODES;
import static org.hawkular.metrics.api.jaxrs.config.ConfigurationKey.CASSANDRA_RESETDB;
import static org.hawkular.metrics.api.jaxrs.config.ConfigurationKey.CASSANDRA_USESSL;
import static org.hawkular.metrics.api.jaxrs.config.ConfigurationKey.DEFAULT_TTL;
import static org.hawkular.metrics.api.jaxrs.config.ConfigurationKey.DISABLE_METRICS_JMX;
import static org.hawkular.metrics.api.jaxrs.config.ConfigurationKey.USE_VIRTUAL_CLOCK;
import static org.hawkular.metrics.api.jaxrs.config.ConfigurationKey.WAIT_FOR_SERVICE;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Event;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.net.ssl.SSLContext;
import org.hawkular.metrics.api.jaxrs.config.Configurable;
import org.hawkular.metrics.api.jaxrs.config.ConfigurationProperty;
import org.hawkular.metrics.api.jaxrs.log.RestLogger;
import org.hawkular.metrics.api.jaxrs.log.RestLogging;
import org.hawkular.metrics.api.jaxrs.util.Eager;
import org.hawkular.metrics.api.jaxrs.util.MetricRegistryProvider;
import org.hawkular.metrics.core.service.DataAccess;
import org.hawkular.metrics.core.service.DataAccessImpl;
import org.hawkular.metrics.core.service.MetricsService;
import org.hawkular.metrics.core.service.MetricsServiceImpl;
import org.hawkular.metrics.schema.SchemaService;
import org.hawkular.metrics.sysconfig.ConfigurationService;
import org.hawkular.metrics.tasks.api.Task2;
import org.hawkular.metrics.tasks.api.TaskScheduler;
import org.hawkular.rx.cassandra.driver.RxSessionImpl;
import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Configuration;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.HostDistance;
import com.datastax.driver.core.JdkSSLOptions;
import com.datastax.driver.core.PoolingOptions;
import com.datastax.driver.core.SSLOptions;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.policies.LoadBalancingPolicy;
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.Uninterruptibles;
import rx.Subscription;
import rx.functions.Action1;
/**
* Bean created on startup to manage the lifecycle of the {@link MetricsService} instance shared in application scope.
*
* @author John Sanda
* @author Thomas Segismont
*/
@ApplicationScoped
@Eager
public class MetricsServiceLifecycle {
private static final RestLogger log = RestLogging.getRestLogger(MetricsServiceLifecycle.class);
/**
* @see #getState()
*/
public enum State {
STARTING, STARTED, STOPPING, STOPPED, FAILED
}
private MetricsServiceImpl metricsService;
private TaskScheduler taskScheduler;
private final ScheduledExecutorService lifecycleExecutor;
@Inject
@Configurable
@ConfigurationProperty(CASSANDRA_CQL_PORT)
private String cqlPort;
@Inject
@Configurable
@ConfigurationProperty(CASSANDRA_NODES)
private String nodes;
@Inject
@Configurable
@ConfigurationProperty(CASSANDRA_KEYSPACE)
private String keyspace;
@Inject
@Configurable
@ConfigurationProperty(CASSANDRA_RESETDB)
private String resetDb;
@Inject
@Configurable
@ConfigurationProperty(CASSANDRA_MAX_CONN_HOST)
private String maxConnectionsPerHost;
@Inject
@Configurable
@ConfigurationProperty(CASSANDRA_MAX_REQUEST_CONN)
private String maxRequestsPerConnection;
@Inject
@Configurable
@ConfigurationProperty(WAIT_FOR_SERVICE)
private String waitForService;
@Inject
@Configurable
@ConfigurationProperty(USE_VIRTUAL_CLOCK)
private String useVirtualClock;
@Inject
@Configurable
@ConfigurationProperty(CASSANDRA_USESSL)
private String cassandraUseSSL;
@Inject
@Configurable
@ConfigurationProperty(DEFAULT_TTL)
private String defaultTTL;
@Inject
@Configurable
@ConfigurationProperty(DISABLE_METRICS_JMX)
private String disableMetricsJmxReporting;
@Inject
@ServiceReady
Event<ServiceReadyEvent> metricsServiceReady;
private volatile State state;
private int connectionAttempts;
private Session session;
private JmxReporter jmxReporter;
private DataAccess dataAcces;
private Map<? super Action1<Task2>, Subscription> jobs = new HashMap<>();
MetricsServiceLifecycle() {
ThreadFactory threadFactory = r -> {
Thread thread = Executors.defaultThreadFactory().newThread(r);
thread.setName(MetricsService.class.getSimpleName().toLowerCase(Locale.ROOT) + "-lifecycle-thread");
return thread;
};
// All lifecycle operations will be executed on a single thread to avoid synchronization issues
lifecycleExecutor = Executors.newSingleThreadScheduledExecutor(threadFactory);
state = State.STARTING;
}
/**
* Returns the lifecycle state of the {@link MetricsService} shared in application scope.
*
* @return lifecycle state of the shared {@link MetricsService}
*/
public State getState() {
return state;
}
@PostConstruct
void init() {
lifecycleExecutor.submit(this::startMetricsService);
if (Boolean.parseBoolean(waitForService)
// "hawkular-metrics.backend" is not a real Metrics configuration parameter (there's a single
// MetricsService implementation, which is backed by Cassandra).
// But it's been used historically to wait for the service to be available before completing the deployment.
// Therefore, we still use it here for backward compatibililty.
// TODO remove when Hawkular build has been updated to use the eager startup flag
|| "embedded_cassandra".equals(System.getProperty("hawkular.backend"))) {
long start = System.nanoTime();
while (state == State.STARTING
// Give up after a minute. The deployment won't be failed and we'll continue to try to start the
// service in the background.
&& NANOSECONDS.convert(1, MINUTES) > System.nanoTime() - start) {
Uninterruptibles.sleepUninterruptibly(1, SECONDS);
}
}
}
private void startMetricsService() {
if (state != State.STARTING) {
return;
}
log.infoInitializing();
connectionAttempts++;
try {
session = createSession();
} catch (Exception t) {
Throwable rootCause = Throwables.getRootCause(t);
log.warnCouldNotConnectToCassandra(rootCause.getLocalizedMessage());
// cycle between original and more wait time - avoid waiting huge amounts of time
long delay = 1L + ((connectionAttempts - 1L) % 4L);
log.warnRetryingConnectingToCassandra(connectionAttempts, delay);
lifecycleExecutor.schedule(this::startMetricsService, delay, SECONDS);
return;
}
try {
// When this class was first introduced, I said that the schema management
// should stay in MetricsServiceImpl, and now I am putting it back here. OK, so
// I deserve some criticism; however, I still think it should be done that way.
// I made this change temporarily because the schema for metrics and for the
// task scheduling service are declared and created in the same place. That
// will change at some point though because the task scheduling service will
// probably move to the hawkular-commons repo.
initSchema();
dataAcces = new DataAccessImpl(session);
initTaskScheduler();
ConfigurationService configurationService = new ConfigurationService();
configurationService.init(new RxSessionImpl(session));
metricsService = new MetricsServiceImpl();
metricsService.setDataAccess(dataAcces);
metricsService.setTaskScheduler(taskScheduler);
metricsService.setConfigurationService(configurationService);
metricsService.setDefaultTTL(getDefaultTTL());
MetricRegistry metricRegistry = MetricRegistryProvider.INSTANCE.getMetricRegistry();
if (!Boolean.parseBoolean(disableMetricsJmxReporting)) {
jmxReporter = JmxReporter.forRegistry(metricRegistry).inDomain("hawkular.metrics").build();
jmxReporter.start();
}
metricsService.startUp(session, keyspace, false, false, metricRegistry);
initJobs();
metricsServiceReady.fire(new ServiceReadyEvent(metricsService.insertedDataEvents()));
Configuration configuration = session.getCluster().getConfiguration();
LoadBalancingPolicy loadBalancingPolicy = configuration.getPolicies().getLoadBalancingPolicy();
PoolingOptions poolingOptions = configuration.getPoolingOptions();
lifecycleExecutor.scheduleAtFixedRate(() -> {
if (log.isDebugEnabled()) {
Session.State state = session.getState();
for (Host host : state.getConnectedHosts()) {
HostDistance distance = loadBalancingPolicy.distance(host);
int connections = state.getOpenConnections(host);
int inFlightQueries = state.getInFlightQueries(host);
log.debugf("%s connections=%d, current load=%d, max load=%d%n", host, connections,
inFlightQueries, connections * poolingOptions.getMaxRequestsPerConnection(distance));
}
}
}, 5, 5, TimeUnit.SECONDS);
state = State.STARTED;
log.infoServiceStarted();
} catch (Exception e) {
log.fatalCannotConnectToCassandra(e);
state = State.FAILED;
} finally {
if (state != State.STARTED && metricsService != null) {
try {
metricsService.shutdown();
} catch (Exception e) {
log.errorCouldNotCloseServiceInstance(e);
}
}
}
}
private Session createSession() {
Cluster.Builder clusterBuilder = new Cluster.Builder();
int port;
try {
port = Integer.parseInt(cqlPort);
} catch (NumberFormatException nfe) {
String defaultPort = CASSANDRA_CQL_PORT.defaultValue();
log.warnInvalidCqlPort(cqlPort, defaultPort);
port = Integer.parseInt(defaultPort);
}
clusterBuilder.withPort(port);
Arrays.stream(nodes.split(",")).forEach(clusterBuilder::addContactPoint);
if (Boolean.parseBoolean(cassandraUseSSL)) {
SSLOptions sslOptions = null;
try {
String[] defaultCipherSuites = { "TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_256_CBC_SHA" };
sslOptions = JdkSSLOptions.builder().withSSLContext(SSLContext.getDefault())
.withCipherSuites(defaultCipherSuites).build();
clusterBuilder.withSSL(sslOptions);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SSL support is required but is not available in the JVM.", e);
}
}
if (Boolean.parseBoolean(disableMetricsJmxReporting)) {
clusterBuilder.withoutJMXReporting();
}
int newMaxConnections;
try {
newMaxConnections = Integer.parseInt(maxConnectionsPerHost);
} catch (NumberFormatException nfe) {
String defaultMaxConnections = CASSANDRA_MAX_CONN_HOST.defaultValue();
log.warnInvalidMaxConnections(maxConnectionsPerHost, defaultMaxConnections);
newMaxConnections = Integer.parseInt(defaultMaxConnections);
}
int newMaxRequests;
try {
newMaxRequests = Integer.parseInt(maxRequestsPerConnection);
} catch (NumberFormatException nfe) {
String defaultMaxRequests = CASSANDRA_MAX_REQUEST_CONN.defaultValue();
log.warnInvalidMaxRequests(maxRequestsPerConnection, defaultMaxRequests);
newMaxRequests = Integer.parseInt(defaultMaxRequests);
}
clusterBuilder.withPoolingOptions(new PoolingOptions()
.setMaxConnectionsPerHost(HostDistance.LOCAL, newMaxConnections)
.setMaxConnectionsPerHost(HostDistance.REMOTE, newMaxConnections)
.setMaxRequestsPerConnection(HostDistance.LOCAL, newMaxRequests)
.setMaxRequestsPerConnection(HostDistance.REMOTE, newMaxRequests)
);
Cluster cluster = clusterBuilder.build();
cluster.init();
Session createdSession = null;
try {
createdSession = cluster.connect("system");
return createdSession;
} finally {
if (createdSession == null) {
cluster.close();
}
}
}
private void initSchema() {
SchemaService schemaService = new SchemaService();
schemaService.run(session, keyspace, Boolean.parseBoolean(resetDb));
session.execute("USE " + keyspace);
}
private void initTaskScheduler() {
// taskScheduler = new TaskSchedulerImpl(new RxSessionImpl(session), new Queries(session));
// if (Boolean.valueOf(useVirtualClock.toLowerCase())) {
// TestScheduler scheduler = Schedulers.test();
// scheduler.advanceTimeTo(System.currentTimeMillis(), MILLISECONDS);
// AbstractTrigger.now = scheduler::now;
// ((TaskSchedulerImpl) taskScheduler).setTickScheduler(scheduler);
//
// }
// taskScheduler.start();
}
private int getDefaultTTL() {
try {
return Integer.parseInt(defaultTTL);
} catch (NumberFormatException e) {
log.warnInvalidDefaultTTL(defaultTTL, DEFAULT_TTL.defaultValue());
return Integer.parseInt(DEFAULT_TTL.defaultValue());
}
}
private void initJobs() {
// GenerateRate generateRates = new GenerateRate(metricsService);
// CreateTenants createTenants = new CreateTenants(metricsService, dataAcces);
//
// jobs.put(generateRates, taskScheduler.getTasks().filter(task -> task.getName().equals(GenerateRate.TASK_NAME))
// .subscribe(generateRates));
// jobs.put(createTenants, taskScheduler.getTasks().filter(task -> task.getName().equals(CreateTenants.TASK_NAME))
// .subscribe(createTenants));
}
/**
* @return a {@link MetricsService} instance to share in application scope
*/
@Produces
@ApplicationScoped
public MetricsService getMetricsService() {
return metricsService;
}
@Produces
@ApplicationScoped
public TaskScheduler getTaskScheduler() {
return taskScheduler;
}
@PreDestroy
void destroy() {
Future<?> stopFuture = lifecycleExecutor.submit(this::stopMetricsService);
try {
Futures.get(stopFuture, 1, MINUTES, Exception.class);
} catch (Exception e) {
log.errorShutdownProblem(e);
}
lifecycleExecutor.shutdown();
}
private void stopMetricsService() {
state = State.STOPPING;
try {
if (metricsService != null) {
metricsService.shutdown();
}
if (taskScheduler != null) {
taskScheduler.shutdown();
}
jobs.values().forEach(Subscription::unsubscribe);
if (session != null) {
session.close();
session.getCluster().close();
}
if (jmxReporter != null) {
jmxReporter.stop();
}
} finally {
state = State.STOPPED;
}
}
}