/
ReaperApplication.java
378 lines (324 loc) · 15.4 KB
/
ReaperApplication.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
/*
* 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.cassandrareaper;
import io.cassandrareaper.ReaperApplicationConfiguration.DatacenterAvailability;
import io.cassandrareaper.ReaperApplicationConfiguration.JmxCredentials;
import io.cassandrareaper.jmx.JmxConnectionFactory;
import io.cassandrareaper.jmx.JmxConnectionsInitializer;
import io.cassandrareaper.resources.ClusterResource;
import io.cassandrareaper.resources.PingResource;
import io.cassandrareaper.resources.ReaperHealthCheck;
import io.cassandrareaper.resources.RepairRunResource;
import io.cassandrareaper.resources.RepairScheduleResource;
import io.cassandrareaper.resources.SnapshotResource;
import io.cassandrareaper.resources.auth.LoginResource;
import io.cassandrareaper.resources.auth.ShiroExceptionMapper;
import io.cassandrareaper.service.AutoSchedulingManager;
import io.cassandrareaper.service.RepairManager;
import io.cassandrareaper.service.SchedulingManager;
import io.cassandrareaper.service.SnapshotManager;
import io.cassandrareaper.storage.CassandraStorage;
import io.cassandrareaper.storage.IDistributedStorage;
import io.cassandrareaper.storage.IStorage;
import io.cassandrareaper.storage.MemoryStorage;
import io.cassandrareaper.storage.PostgresStorage;
import java.util.EnumSet;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.datastax.driver.core.policies.EC2MultiRegionAddressTranslator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import io.dropwizard.Application;
import io.dropwizard.assets.AssetsBundle;
import io.dropwizard.configuration.EnvironmentVariableSubstitutor;
import io.dropwizard.configuration.SubstitutingSourceProvider;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.jdbi.DBIFactory;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.dropwizard.DropwizardExports;
import io.prometheus.client.exporter.MetricsServlet;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlets.CrossOriginFilter;
import org.flywaydb.core.Flyway;
import org.joda.time.DateTimeZone;
import org.secnod.dropwizard.shiro.ShiroBundle;
import org.secnod.dropwizard.shiro.ShiroConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.Signal;
public final class ReaperApplication extends Application<ReaperApplicationConfiguration> {
private static final Logger LOG = LoggerFactory.getLogger(ReaperApplication.class);
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private final AppContext context;
public ReaperApplication() {
super();
LOG.info("default ReaperApplication constructor called");
this.context = new AppContext();
}
@VisibleForTesting
public ReaperApplication(AppContext context) {
super();
LOG.info("ReaperApplication constructor called with custom AppContext");
this.context = context;
}
public static void main(String[] args) throws Exception {
new ReaperApplication().run(args);
}
@Override
public String getName() {
return "cassandra-reaper";
}
/**
* Before a Dropwizard application can provide the command-line interface, parse a configuration file, or run as a
* server, it must first go through a bootstrapping phase. You can add Bundles, Commands, or register Jackson modules
* to allow you to include custom types as part of your configuration class.
*/
@Override
public void initialize(Bootstrap<ReaperApplicationConfiguration> bootstrap) {
bootstrap.addBundle(new AssetsBundle("/assets/", "/webui", "index.html"));
bootstrap.getObjectMapper().registerModule(new JavaTimeModule());
// enable using environment variables in yml files
final SubstitutingSourceProvider envSourceProvider = new SubstitutingSourceProvider(
bootstrap.getConfigurationSourceProvider(), new EnvironmentVariableSubstitutor(false));
bootstrap.setConfigurationSourceProvider(envSourceProvider);
bootstrap.addBundle(
new ShiroBundle<ReaperApplicationConfiguration>() {
@Override
public void run(ReaperApplicationConfiguration configuration, Environment environment) {
if (configuration.isAccessControlEnabled()) {
super.run(configuration, environment);
}
}
@Override
protected ShiroConfiguration narrow(ReaperApplicationConfiguration configuration) {
return configuration.getAccessControl().getShiroConfiguration();
}
});
bootstrap.setConfigurationSourceProvider(
new SubstitutingSourceProvider(
bootstrap.getConfigurationSourceProvider(), new EnvironmentVariableSubstitutor()));
bootstrap.getObjectMapper().registerModule(new JavaTimeModule());
}
@Override
public void run(ReaperApplicationConfiguration config, Environment environment) throws Exception {
// Using UTC times everywhere as default. Affects only Yoda time.
DateTimeZone.setDefault(DateTimeZone.UTC);
checkConfiguration(config);
context.config = config;
addSignalHandlers(); // SIGHUP, etc.
context.metricRegistry = environment.metrics();
CollectorRegistry.defaultRegistry.register(new DropwizardExports(environment.metrics()));
environment
.admin()
.addServlet("prometheusMetrics", new MetricsServlet(CollectorRegistry.defaultRegistry))
.addMapping("/prometheusMetrics");
context.snapshotManager = SnapshotManager.create(context);
LOG.info("initializing runner thread pool with {} threads", config.getRepairRunThreadCount());
context.repairManager = RepairManager.create(context);
context.repairManager.initializeThreadPool(
config.getRepairRunThreadCount(),
config.getHangingRepairTimeoutMins(),
TimeUnit.MINUTES,
config.getRepairManagerSchedulingIntervalSeconds(),
TimeUnit.SECONDS);
if (context.storage == null) {
LOG.info("initializing storage of type: {}", config.getStorageType());
context.storage = initializeStorage(config, environment);
} else {
LOG.info("storage already given in context, not initializing a new one");
}
if (context.jmxConnectionFactory == null) {
LOG.info("no JMX connection factory given in context, creating default");
context.jmxConnectionFactory = new JmxConnectionFactory(context.metricRegistry);
// read jmx host/port mapping from config and provide to jmx con.factory
Map<String, Integer> jmxPorts = config.getJmxPorts();
if (jmxPorts != null) {
LOG.debug("using JMX ports mapping: {}", jmxPorts);
context.jmxConnectionFactory.setJmxPorts(jmxPorts);
}
if (config.useAddressTranslator()) {
context.jmxConnectionFactory.setAddressTranslator(new EC2MultiRegionAddressTranslator());
}
}
JmxCredentials jmxAuth = config.getJmxAuth();
if (jmxAuth != null) {
LOG.debug("using specified JMX credentials for authentication");
context.jmxConnectionFactory.setJmxAuth(jmxAuth);
}
Map<String, JmxCredentials> jmxCredentials = config.getJmxCredentials();
if (jmxCredentials != null) {
LOG.debug("using specified JMX credentials per cluster for authentication");
context.jmxConnectionFactory.setJmxCredentials(jmxCredentials);
}
// Enable cross-origin requests for using external GUI applications.
if (config.isEnableCrossOrigin() || System.getProperty("enableCrossOrigin") != null) {
final FilterRegistration.Dynamic cors
= environment.servlets().addFilter("crossOriginRequests", CrossOriginFilter.class);
cors.setInitParameter("allowedOrigins", "*");
cors.setInitParameter("allowedHeaders", "X-Requested-With,Content-Type,Accept,Origin");
cors.setInitParameter("allowedMethods", "OPTIONS,GET,PUT,POST,DELETE,HEAD");
cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
}
LOG.info("creating and registering health checks");
// Notice that health checks are registered under the admin application on /healthcheck
final ReaperHealthCheck healthCheck = new ReaperHealthCheck(context);
environment.healthChecks().register("reaper", healthCheck);
LOG.info("creating resources and registering endpoints");
final PingResource pingResource = new PingResource(healthCheck);
environment.jersey().register(pingResource);
final ClusterResource addClusterResource = new ClusterResource(context);
environment.jersey().register(addClusterResource);
final RepairRunResource addRepairRunResource = new RepairRunResource(context);
environment.jersey().register(addRepairRunResource);
final RepairScheduleResource addRepairScheduleResource = new RepairScheduleResource(context);
environment.jersey().register(addRepairScheduleResource);
final SnapshotResource snapshotResource = new SnapshotResource(context);
environment.jersey().register(snapshotResource);
if (config.isAccessControlEnabled()) {
SessionHandler sessionHandler = new SessionHandler();
sessionHandler
.getSessionManager()
.setMaxInactiveInterval((int) config.getAccessControl().getSessionTimeout().getSeconds());
environment.getApplicationContext().setSessionHandler(sessionHandler);
environment.servlets().setSessionHandler(sessionHandler);
environment.jersey().register(new ShiroExceptionMapper());
environment.jersey().register(new LoginResource(context));
}
Thread.sleep(1000);
SchedulingManager.start(context);
if (config.hasAutoSchedulingEnabled()) {
LOG.debug("using specified configuration for auto scheduling: {}", config.getAutoScheduling());
AutoSchedulingManager.start(context);
}
initializeJmxSeedsForAllClusters();
LOG.info("resuming pending repair runs");
Preconditions.checkState(
context.storage instanceof IDistributedStorage
|| DatacenterAvailability.EACH != context.config.getDatacenterAvailability(),
"Cassandra backend storage is the only one allowing EACH datacenter availability modes.");
if (context.storage instanceof IDistributedStorage) {
// Allowing multiple Reaper instances to work concurrently requires
// us to poll the database for running repairs regularly
// only with Cassandra storage
scheduler.scheduleWithFixedDelay(
() -> {
try {
context.repairManager.resumeRunningRepairRuns();
} catch (ReaperException e) {
LOG.error("Couldn't resume running repair runs", e);
}
},
0,
10,
TimeUnit.SECONDS);
} else {
// Storage is different than Cassandra, assuming we have a single instance
context.repairManager.resumeRunningRepairRuns();
}
LOG.info("Initialization complete!");
LOG.warn("Reaper is ready to get things done!");
}
private IStorage initializeStorage(ReaperApplicationConfiguration config, Environment environment)
throws ReaperException {
IStorage storage;
if ("memory".equalsIgnoreCase(config.getStorageType())) {
storage = new MemoryStorage();
} else if ("cassandra".equalsIgnoreCase(config.getStorageType())) {
storage = new CassandraStorage(config, environment);
} else if ("postgres".equalsIgnoreCase(config.getStorageType())
|| "h2".equalsIgnoreCase(config.getStorageType())
|| "database".equalsIgnoreCase(config.getStorageType())) {
// create DBI instance
final DBIFactory factory = new DBIFactory();
// instanciate store
storage = new PostgresStorage(factory.build(environment, config.getDataSourceFactory(), "postgresql"));
initDatabase(config);
} else {
LOG.error("invalid storageType: {}", config.getStorageType());
throw new ReaperException("invalid storage type: " + config.getStorageType());
}
Preconditions.checkState(storage.isStorageConnected(), "Failed to connect storage");
return storage;
}
private void checkConfiguration(ReaperApplicationConfiguration config) {
LOG.debug("repairIntensity: {}", config.getRepairIntensity());
LOG.debug("incrementalRepair: {}", config.getIncrementalRepair());
LOG.debug("repairRunThreadCount: {}", config.getRepairRunThreadCount());
LOG.debug("segmentCount: {}", config.getSegmentCount());
LOG.debug("repairParallelism: {}", config.getRepairParallelism());
LOG.debug("hangingRepairTimeoutMins: {}", config.getHangingRepairTimeoutMins());
LOG.debug("jmxPorts: {}", config.getJmxPorts());
}
void reloadConfiguration() {
// TODO: reload configuration, but how?
LOG.warn("SIGHUP signal dropped, missing implementation for configuration reload");
}
private void addSignalHandlers() {
if (!System.getProperty("os.name").toLowerCase().contains("win")) {
LOG.debug("adding signal handler for SIGHUP");
Signal.handle(new Signal("HUP"), signal -> {
LOG.info("received SIGHUP signal: {}", signal);
reloadConfiguration();
});
}
}
private void initDatabase(ReaperApplicationConfiguration config) throws ReaperException {
Flyway flyway = new Flyway();
DataSourceFactory dsfactory = config.getDataSourceFactory();
flyway.setDataSource(
dsfactory.getUrl(),
dsfactory.getUser(),
dsfactory.getPassword());
if ("database".equals(config.getStorageType())) {
LOG.warn("!!!!!!!!!! USAGE 'database' AS STORAGE TYPE IS NOW DEPRECATED !!!!!!!!!!!!!!");
LOG.warn("!!!!!!!!!! PLEASE USE EITHER 'postgres' OR 'h2' FROM NOW ON !!!!!!!!!!!!!!");
if (config.getDataSourceFactory().getUrl().contains("h2")) {
flyway.setLocations("/db/h2");
} else {
flyway.setLocations("/db/postgres");
}
} else {
flyway.setLocations("/db/".concat(config.getStorageType().toLowerCase()));
}
flyway.setBaselineOnMigrate(true);
flyway.repair();
flyway.migrate();
}
private void initializeJmxSeedsForAllClusters() {
LOG.info("Initializing JMX seed list for all clusters...");
try (JmxConnectionsInitializer jmxConnectionsIntializer = JmxConnectionsInitializer.create(context);
Timer.Context cxt = context
.metricRegistry
.timer(MetricRegistry.name(JmxConnectionFactory.class, "jmxConnectionsIntializer"))
.time()) {
context
.storage
.getClusters()
.parallelStream()
.forEach(cluster -> jmxConnectionsIntializer.on(cluster));
LOG.info("Initialized JMX seed list for all clusters.");
} catch (RuntimeException e) {
LOG.error("Failed initializing JMX seed list", e);
}
}
}