Skip to content

Commit

Permalink
[HWKMETRICS-130] move task type configuration to its own class for now
Browse files Browse the repository at this point in the history
  • Loading branch information
John Sanda committed Jun 19, 2015
1 parent 657cf13 commit 0a461d7
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.function.Predicate;

import rx.Observable;
import rx.Observer;

/**
* Interface that defines the functionality of the Metrics Service.
Expand Down Expand Up @@ -50,6 +51,31 @@ public interface MetricsService {

Observable<Tenant> getTenants();

/**
* <p>
* Clients are not required to required to explicitly create a metric via this method before storing data for it.
* This method does a few things. First, it updates indexes with the metric meta data (i.e., name, tags, etc.) so
* that it can be found in metric queries performed with {@link #findMetrics(String, MetricType)}. Querying by
* tags will be supported in the future. Secondly, this method ensures that there are no metric naming conflicts.
* If another metric with the same name already exists, then returned Observable will fail with a
* {@link MetricAlreadyExistsException}. Lastly, meta data settings are configured and persisted. Currently this
* includes a couple things - data retention and counter rates. If data retention is specified for the metric,
* those settings are stored so that they will be applied to any data points that get persisted. If the metric is
* a counter, then a background job is created to compute and store rate data points. It is not yet possible to
* configure the settings for the rate calculation job; however, that will change in the near future.
* </p>
* <p>
* Note that in the current implementation if metric creation fails, things can be in an inconsistent state. For
* example, an index that should have been updated might not have been. There is no work around for this currently.
* </p>
*
* @param metric The metric to create
*
* @return This method only has side effects and does not return any data. As such,
* {@link rx.Observer#onNext(Object) onNext} is not called. {@link Observer#onCompleted()} onCompleted} is called
* when the operation completes successfully, and {@link rx.Observer#onError(Throwable)} onError} is called when it
* fails.
*/
Observable<Void> createMetric(Metric<?> metric);

Observable<Metric> findMetric(String tenantId, MetricType type, MetricId id);
Expand Down Expand Up @@ -102,6 +128,18 @@ Observable<Map<MetricId, Set<DataPoint<AvailabilityType>>>> findAvailabilityByTa

Observable<DataPoint<Long>> findCounterData(String tenantId, MetricId id, long start, long end);

/**
* Fetches counter rate data points which are automatically generated for counter metrics. Note that rate data is
* generated if the metric has been explicitly created via the {@link #createMetric(Metric)} method.
*
* @param tenantId The teant to which the metric belongs
* @param id This is the id of the counter metric
* @param start The start time which is inclusive
* @param end The end time which is exclusive
*
* @return An Observable of {@link DataPoint data points} which are emitted in descending order. In other words,
* the most recent data is emitted first.
*/
Observable<DataPoint<Double>> findRateData(String tenantId, MetricId id, long start, long end);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import org.hawkular.metrics.schema.SchemaManager;
import org.hawkular.metrics.tasks.api.Task;
import org.hawkular.metrics.tasks.api.TaskService;
import org.hawkular.metrics.tasks.api.TaskType;
import org.hawkular.rx.cassandra.driver.RxUtil;
import org.joda.time.Duration;
import org.joda.time.Hours;
Expand Down Expand Up @@ -152,8 +151,6 @@ public int hashCode() {

private DataAccess dataAccess;

private List<TaskType> taskTypes;

private TaskService taskService;

private MetricRegistry metricRegistry;
Expand Down Expand Up @@ -322,10 +319,6 @@ void setDataAccess(DataAccess dataAccess) {
this.dataAccess = dataAccess;
}

void setTaskTypes(List<TaskType> taskTypes) {
this.taskTypes = taskTypes;
}

public void setTaskService(TaskService taskService) {
this.taskService = taskService;
}
Expand Down Expand Up @@ -438,9 +431,8 @@ public Observable<Void> createMetric(Metric<?> metric) {
}

if (metric.getType() == COUNTER) {
TaskType generateRatesType = taskTypes.get(0);
Task task = generateRatesType.createTask(metric.getTenantId(), metric.getId().getName() + "$rate",
metric.getId().getName());
Task task = TaskTypes.COMPUTE_RATE.createTask(metric.getTenantId(), metric.getId().getName() +
"$rate", metric.getId().getName());
taskService.scheduleTask(now(), task);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.core.impl;

import org.hawkular.metrics.tasks.api.TaskType;

/**
* <p>
* Computing rates is the first and only scheduled task that we support at the moment. Task types serve a couple
* purposes. They provide some configuration for tasks, like the execution interval. They also determine the order of
* execution of tasks. This class provides some defaults.
* </p>
* <p>
* The way in which tasks are configured, created, etc. is very like to change as the task schedule service undergoes
* continued refactored as it gets used more.
* </p>
*/
public class TaskTypes {

private TaskTypes() {
}

public static TaskType COMPUTE_RATE = new TaskType()
.setName("counter-rate")
.setSegments(10)
.setSegmentOffsets(10)
.setInterval(5)
.setWindow(5);

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
import org.hawkular.metrics.core.api.MetricType;
import org.hawkular.metrics.core.api.Retention;
import org.hawkular.metrics.core.api.Tenant;
import org.hawkular.metrics.tasks.api.TaskType;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.testng.annotations.BeforeClass;
Expand All @@ -92,12 +91,6 @@ public void initClass() {

metricsService = new MetricsServiceImpl();
metricsService.setTaskService(new FakeTaskService());
metricsService.setTaskTypes(singletonList(new TaskType()
.setName("counter-rate")
.setSegments(10)
.setSegmentOffsets(10)
.setInterval(5)
.setWindow(5)));
metricsService.startUp(session, getKeyspace(), false, new MetricRegistry());
dataAccess = metricsService.getDataAccess();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.hawkular.metrics.core.api.MetricId;
import org.hawkular.metrics.tasks.api.TaskService;
import org.hawkular.metrics.tasks.api.TaskServiceBuilder;
import org.hawkular.metrics.tasks.api.TaskType;
import org.hawkular.metrics.tasks.impl.TaskServiceImpl;
import org.joda.time.DateTime;
import org.testng.annotations.AfterClass;
Expand All @@ -57,25 +56,17 @@ public void initClass() {

dateTimeService = new DateTimeService();

TaskType taskType =new TaskType()
.setName("counter-rate")
.setSegments(10)
.setSegmentOffsets(10)
.setInterval(5)
.setWindow(5);

taskService = new TaskServiceBuilder()
.withSession(session)
.withTimeUnit(TimeUnit.SECONDS)
.withTaskTypes(singletonList(taskType))
.withTaskTypes(singletonList(TaskTypes.COMPUTE_RATE))
.build();
((TaskServiceImpl) taskService).setTimeUnit(TimeUnit.SECONDS);

metricsService = new MetricsServiceImpl();
metricsService.setTaskTypes(singletonList(taskType));
metricsService.setTaskService(taskService);

((TaskServiceImpl) taskService).subscribe(taskType, new GenerateRate(metricsService));
((TaskServiceImpl) taskService).subscribe(TaskTypes.COMPUTE_RATE, new GenerateRate(metricsService));

String keyspace = "hawkulartest";
System.setProperty("keyspace", keyspace);
Expand Down

0 comments on commit 0a461d7

Please sign in to comment.