Skip to content

Commit

Permalink
Format java files to suit checkstyle rules.
Browse files Browse the repository at this point in the history
Was accomplished with a combination of a hacked google-java-formatter (ignoring imports), the netbeans formatter, and manual edits.

ref: #198
  • Loading branch information
michaelsembwever authored and adejanovski committed Sep 26, 2017
1 parent 9f2b7bb commit ff6b3ab
Show file tree
Hide file tree
Showing 100 changed files with 6,422 additions and 5,048 deletions.
4 changes: 2 additions & 2 deletions src/server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<artifactId>mockito-core</artifactId>
<version>2.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion src/server/src/checkstyle/java.header
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* 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 a
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
Expand Down
31 changes: 23 additions & 8 deletions src/server/src/main/java/com/spotify/reaper/AppContext.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
/*
* 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 com.spotify.reaper;

import com.codahale.metrics.MetricRegistry;
import com.spotify.reaper.cassandra.JmxConnectionFactory;
import com.spotify.reaper.service.RepairManager;
import com.spotify.reaper.storage.IStorage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

import com.codahale.metrics.MetricRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Single class to hold all application global interfacing objects,
* and app global options.
* Single class to hold all application global interfacing objects, and app global options.
*/
public final class AppContext {

public static final UUID REAPER_INSTANCE_ID = UUID.randomUUID();
public static final String REAPER_INSTANCE_ADDRESS = initialiseInstanceAddress();

private static final String DEFAULT_INSTANCE_ADDRESS = "127.0.0.1";
private static final Logger LOG = LoggerFactory.getLogger(AppContext.class);

public static final String REAPER_INSTANCE_ADDRESS = initialiseInstanceAddress();
public final AtomicBoolean isRunning = new AtomicBoolean(true);
public IStorage storage;
public RepairManager repairManager;
Expand All @@ -34,9 +49,9 @@ private static String initialiseInstanceAddress() {
String reaperInstanceAddress;
try {
reaperInstanceAddress = InetAddress.getLocalHost().getHostAddress();
} catch(UnknownHostException e) {
} catch (UnknownHostException e) {
LOG.warn("Cannot get instance address", e);
reaperInstanceAddress = DEFAULT_INSTANCE_ADDRESS;
reaperInstanceAddress = DEFAULT_INSTANCE_ADDRESS;
}
return reaperInstanceAddress;
}
Expand Down
137 changes: 68 additions & 69 deletions src/server/src/main/java/com/spotify/reaper/ReaperApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.spotify.reaper;

import java.util.Arrays;
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 io.dropwizard.configuration.EnvironmentVariableSubstitutor;
import io.dropwizard.configuration.SubstitutingSourceProvider;
import org.apache.cassandra.repair.RepairParallelism;
import org.eclipse.jetty.servlets.CrossOriginFilter;
import org.flywaydb.core.Flyway;
import org.joda.time.DateTimeZone;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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;
package com.spotify.reaper;

import com.spotify.reaper.ReaperApplicationConfiguration.JmxCredentials;
import com.spotify.reaper.cassandra.JmxConnectionFactory;
Expand All @@ -54,24 +30,43 @@
import com.spotify.reaper.storage.MemoryStorage;
import com.spotify.reaper.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.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 org.skife.jdbi.v2.DBI;

import io.dropwizard.configuration.EnvironmentVariableSubstitutor;
import io.dropwizard.configuration.SubstitutingSourceProvider;
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.servlets.CrossOriginFilter;
import org.flywaydb.core.Flyway;
import org.joda.time.DateTimeZone;
import org.skife.jdbi.v2.DBI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.Signal;
import sun.misc.SignalHandler;

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() {
Expand All @@ -90,17 +85,16 @@ public ReaperApplication(AppContext 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.
* 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) {
Expand All @@ -109,7 +103,7 @@ public void initialize(Bootstrap<ReaperApplicationConfiguration> bootstrap) {

// enable using environment variables in yml files
final SubstitutingSourceProvider envSourceProvider = new SubstitutingSourceProvider(
bootstrap.getConfigurationSourceProvider(), new EnvironmentVariableSubstitutor(false));
bootstrap.getConfigurationSourceProvider(), new EnvironmentVariableSubstitutor(false));
bootstrap.setConfigurationSourceProvider(envSourceProvider);
}

Expand All @@ -126,16 +120,19 @@ public void run(ReaperApplicationConfiguration config, Environment environment)
context.metricRegistry = environment.metrics();
CollectorRegistry.defaultRegistry.register(new DropwizardExports(environment.metrics()));

environment.admin()
environment
.admin()
.addServlet("prometheusMetrics", new MetricsServlet(CollectorRegistry.defaultRegistry))
.addMapping("/prometheusMetrics");

LOG.info("initializing runner thread pool with {} threads", config.getRepairRunThreadCount());
context.repairManager = new RepairManager();
context.repairManager.initializeThreadPool(
config.getRepairRunThreadCount(),
config.getHangingRepairTimeoutMins(), TimeUnit.MINUTES,
config.getRepairManagerSchedulingIntervalSeconds(), TimeUnit.SECONDS);
config.getHangingRepairTimeoutMins(),
TimeUnit.MINUTES,
config.getRepairManagerSchedulingIntervalSeconds(),
TimeUnit.SECONDS);

if (context.storage == null) {
LOG.info("initializing storage of type: {}", config.getStorageType());
Expand All @@ -157,7 +154,7 @@ public void run(ReaperApplicationConfiguration config, Environment environment)
context.jmxConnectionFactory.setJmxPorts(jmxPorts);
}

if(config.useAddressTranslator()) {
if (config.useAddressTranslator()) {
context.jmxConnectionFactory.setAddressTranslator(new EC2MultiRegionAddressTranslator());
}

Expand All @@ -170,8 +167,8 @@ public void run(ReaperApplicationConfiguration config, Environment environment)

// 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);
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");
Expand Down Expand Up @@ -205,45 +202,45 @@ public void run(ReaperApplicationConfiguration config, Environment environment)
}

LOG.info("resuming pending repair runs");

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(context);
} catch (ReaperException e) {
LOG.error("Couldn't resume running repair runs", e);
}
}, 0, 10, TimeUnit.SECONDS);
}
else {
}
},
0,
10,
TimeUnit.SECONDS);
} else {
// Storage is different than Cassandra, assuming we have a single instance
context.repairManager.resumeRunningRepairRuns(context);
}

}

private IStorage initializeStorage(ReaperApplicationConfiguration config,
Environment environment) throws ReaperException {
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())) {
} else if ("cassandra".equalsIgnoreCase(config.getStorageType())) {
storage = new CassandraStorage(config, environment);
} else if ("database".equalsIgnoreCase(config.getStorageType())) {
// create DBI instance
DBI jdbi;
final DBIFactory factory = new DBIFactory();
jdbi = factory.build(environment, config.getDataSourceFactory(), "postgresql");

// instanciate store
storage = new PostgresStorage(jdbi);
initDatabase(config);



} else {
LOG.error("invalid storageType: {}", config.getStorageType());
throw new ReaperException("invalid storage type: " + config.getStorageType());
Expand All @@ -253,7 +250,7 @@ private IStorage initializeStorage(ReaperApplicationConfiguration config,
}

private void checkConfiguration(ReaperApplicationConfiguration config) {
LOG.debug("repairIntensity: {}",config.getRepairIntensity());
LOG.debug("repairIntensity: {}", config.getRepairIntensity());
LOG.debug("incrementalRepair: {}", config.getIncrementalRepair());
LOG.debug("repairRunThreadCount: {}", config.getRepairRunThreadCount());
LOG.debug("segmentCount: {}", config.getSegmentCount());
Expand All @@ -268,25 +265,27 @@ void reloadConfiguration() {
}

private void addSignalHandlers() {
if(!System.getProperty("os.name").toLowerCase().contains("win")) {
LOG.debug("adding signal handler for SIGHUP");
Signal.handle(new Signal("HUP"), new SignalHandler() {
@Override
public void handle(Signal signal) {
LOG.info("received SIGHUP signal: {}", signal);
reloadConfiguration();
}
});
}
if (!System.getProperty("os.name").toLowerCase().contains("win")) {
LOG.debug("adding signal handler for SIGHUP");
Signal.handle(new Signal("HUP"), new SignalHandler() {
@Override
public void handle(Signal signal) {
LOG.info("received SIGHUP signal: {}", signal);
reloadConfiguration();
}
});
}
}

private void initDatabase(ReaperApplicationConfiguration config) throws ReaperException {
Flyway flyway = new Flyway();
flyway.setDataSource(config.getDataSourceFactory().getUrl(), config.getDataSourceFactory().getUser(), config.getDataSourceFactory().getPassword());
if("org.h2.Driver".equals(config.getDataSourceFactory().getDriverClass())) {
flyway.setDataSource(
config.getDataSourceFactory().getUrl(),
config.getDataSourceFactory().getUser(),
config.getDataSourceFactory().getPassword());
if ("org.h2.Driver".equals(config.getDataSourceFactory().getDriverClass())) {
flyway.setLocations("/db/h2");
}
else {
} else {
flyway.setLocations("/db/postgres");
}
flyway.setBaselineOnMigrate(true);
Expand Down
Loading

0 comments on commit ff6b3ab

Please sign in to comment.