Skip to content

Commit

Permalink
HWKMETRICS-99: allow Hawkular metrics to start and then wait for a Ca…
Browse files Browse the repository at this point in the history
…ssandra cluster to become available rather than just failing.
  • Loading branch information
mwringe committed May 21, 2015
1 parent 50a0fb8 commit 6763aa6
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
import javax.enterprise.inject.Produces;
import javax.inject.Inject;

import com.datastax.driver.core.Session;
import com.google.common.util.concurrent.FutureCallback;
import org.hawkular.metrics.api.jaxrs.config.Configurable;
import org.hawkular.metrics.api.jaxrs.config.ConfigurationProperty;
import org.hawkular.metrics.api.jaxrs.util.Eager;
import org.hawkular.metrics.core.api.MetricsService;
import org.hawkular.metrics.core.impl.HawkularMetrics;
import org.hawkular.metrics.core.impl.cassandra.CassandraSession;
import org.hawkular.metrics.core.impl.cassandra.MetricsServiceCassandra;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -70,14 +73,28 @@ void init() {
@Produces
public MetricsService getMetricsService() {
if (metricsService == null) {
HawkularMetrics.Builder metricsServiceBuilder = new HawkularMetrics.Builder();
metricsService = new MetricsServiceCassandra();
Map<String, String> options = new HashMap<>();
options.put("cqlport", cqlPort);
options.put("nodes", nodes);
options.put("keyspace", keyspace);

metricsServiceBuilder.withOptions(options);
metricsService = metricsServiceBuilder.build();
CassandraSession.Builder cassandraSessionBuilder = new CassandraSession.Builder();
cassandraSessionBuilder.withOptions(options);

cassandraSessionBuilder.withInitializationCallback(new FutureCallback<Session>() {
@Override
public void onSuccess(Session session) {
metricsService.startUp(session);
}

@Override
public void onFailure(Throwable t) {
throw new RuntimeException("Error trying to get the Cassandra Session", t);
}
});

cassandraSessionBuilder.build();
}

return metricsService;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2014-2015 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.filter;

import org.hawkular.metrics.api.jaxrs.ApiError;
import org.hawkular.metrics.core.api.MetricsService;

import javax.inject.Inject;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import java.io.IOException;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;

/**
* Created by mwringe on 20/05/15.
*/
@Provider
public class CassandraAvailabilityFilter implements ContainerRequestFilter {

private static final String NO_CASSANDRA_SESSION_MSG = "Service unavailable while initializing.";

@Inject
private MetricsService metricsService;


@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
if (!metricsService.isStarted()) {
// Fail since the Cassandra cluster is not yet up yet.
Response response = Response.status(Response.Status.SERVICE_UNAVAILABLE)
.type(APPLICATION_JSON_TYPE)
.entity(new ApiError(NO_CASSANDRA_SESSION_MSG))
.build();
containerRequestContext.abortWith(response);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,14 @@ public interface MetricsService {
// creating tenants.
String DEFAULT_TENANT_ID = "test";

/** called to start the service up if needed
* @param params from e.g. servlet context */
void startUp(Map<String, String> params);

/**
* Startup with a given cassandra session
* @param session
*/
void startUp(Session session);

boolean isStarted();

void shutdown();

/**
Expand Down

This file was deleted.

0 comments on commit 6763aa6

Please sign in to comment.