Skip to content

Commit

Permalink
[HWMETRICS-52] separate interface and impl classes
Browse files Browse the repository at this point in the history
  • Loading branch information
John Sanda committed May 12, 2015
1 parent 3be1baa commit d68d873
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* @author jsanda
*/
class DateTimeService {
public class DateTimeService {

/**
* @return A DateTime object rounded down to the start of the current hour. For example, if the current time is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.tasks;
package org.hawkular.metrics.tasks.api;

import java.util.Set;

Expand All @@ -33,14 +33,14 @@ public interface Task {
TaskType getTaskType();

/**
* This is a key or identifier of the entity or thing associated with any data produced by the task. Consider
* aggregating metrics or events as an example. Let's say there is a task for computing a 5 minute rollup from raw
* This is a key or identifier of the time series that is associated with the task. Consider aggregating metrics or
* events as an example. Let's say there is a task for computing a 5 minute rollup from raw
* data. This property should be the key or identifier of the 5 minute rollup time series.
*/
String getTarget();

/**
* The keys or identifiers of the entities associated with the source data being operated on. There can be one or
* The keys or identifiers of the time series associated with the source data being operated on. There can be one or
* more sources. Consider aggregating metrics or events as an example. There is a task for computing a 5 minute
* rollup from raw data. This property identifies the time series of the raw data to be aggregated.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.tasks;
package org.hawkular.metrics.tasks.api;

/**
* @author jsanda
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.tasks.api;

import com.google.common.util.concurrent.ListenableFuture;
import org.joda.time.DateTime;

/**
* @author jsanda
*/
public interface TaskService {
/**
* Starts the scheduler. Task execution can be scheduled every second or every minute. Task execution for a
* particular time slice will run at the end of said time slice or later but never sooner.
*
* TODO log warning if scheduling falls behind
*/
void start();

void shutdown();

ListenableFuture<Task> scheduleTask(DateTime time, Task task);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.tasks;
package org.hawkular.metrics.tasks.api;

import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.hawkular.metrics.tasks.impl.TaskImpl;

/**
* Tasks are grouped by type, and execution order is also determined by type. If there are types A, B, C, then for
* a given time slice all tasks of type A are executed followed by tasks of type B and finally tasks of type C are
* executed. The task type specifies the granularity of partitioning of tasks in the database. It also provides a
* factory function that produces a function to execute tasks.
*
* @author jsanda
*/
public class TaskType {
Expand All @@ -33,6 +40,9 @@ public class TaskType {

private int segmentOffsets;

/**
* The task type name which must be unique among task types.
*/
public String getName() {
return name;
}
Expand All @@ -42,6 +52,9 @@ public TaskType setName(String name) {
return this;
}

/**
* A function that produces functions that carry out task execution.
*/
public Supplier<Consumer<Task>> getFactory() {
return factory;
}
Expand All @@ -51,6 +64,10 @@ public TaskType setFactory(Supplier<Consumer<Task>> factory) {
return this;
}

/**
* The number of partitions in the database to use for storing tasks. The segment is determined by,
* <code>task.target.hashCode() % segments</code>
*/
public int getSegments() {
return segments;
}
Expand All @@ -60,6 +77,14 @@ public TaskType setSegments(int segments) {
return this;
}

/**
* Tasks are associated with leases. A client needs to acquire a lease before it can execute the tasks associated
* with the lease. This property specifies how many tasks in terms of segments are associated with a lease. Let's
* say that we have 100 segments and 10 segment offsets. This means that there are 10 task segments per lease.
* When a client acquires a lease, the client will then execute the tasks in each of those segments.
* <br/><br/>
* TODO Come up with a more descriptive name
*/
public int getSegmentOffsets() {
return segmentOffsets;
}
Expand All @@ -69,6 +94,15 @@ public TaskType setSegmentOffsets(int segmentOffsets) {
return this;
}

/**
* A factory method for creating tasks of this type.
*
* @param target Identifier or key of the entity or time series associated with the task
* @param source Identifiers or keys of the time series that provide input data for the task
* @param interval Specifies the frequency of execution in minutes
* @param window Specifies the amount of data to include in minutes
* @return A {@link Task}
*/
public Task createTask(String target, String source, int interval, int window) {
return new TaskImpl(this, null, target, source, interval, window);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.tasks;
package org.hawkular.metrics.tasks.impl;

import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.tasks;
package org.hawkular.metrics.tasks.impl;

import static java.util.stream.Collectors.toList;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.tasks;
package org.hawkular.metrics.tasks.impl;

import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Session;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.tasks;
package org.hawkular.metrics.tasks.impl;

import java.util.Iterator;
import java.util.Objects;
Expand All @@ -25,6 +25,8 @@
import java.util.TreeSet;
import java.util.function.Consumer;

import org.hawkular.metrics.tasks.api.TaskType;
import org.hawkular.metrics.tasks.api.Task;
import org.joda.time.DateTime;
import org.joda.time.Duration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.tasks;
package org.hawkular.metrics.tasks.impl;

import static org.joda.time.Duration.standardMinutes;

import java.util.Objects;
import java.util.Set;

import com.google.common.collect.ImmutableSet;
import org.hawkular.metrics.tasks.api.TaskType;
import org.hawkular.metrics.tasks.api.Task;
import org.joda.time.DateTime;
import org.joda.time.Duration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.tasks;
package org.hawkular.metrics.tasks.impl;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
Expand Down Expand Up @@ -48,6 +48,10 @@
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.Uninterruptibles;
import org.hawkular.metrics.tasks.DateTimeService;
import org.hawkular.metrics.tasks.api.TaskExecutionException;
import org.hawkular.metrics.tasks.api.Task;
import org.hawkular.metrics.tasks.api.TaskType;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.slf4j.Logger;
Expand All @@ -56,9 +60,9 @@
/**
* @author jsanda
*/
public class TaskService {
public class TaskServiceImpl implements org.hawkular.metrics.tasks.api.TaskService {

This comment has been minimized.

Copy link
@stefannegrea

stefannegrea May 12, 2015

Contributor

Do all the classes in *.impl package have to be public?


private static final Logger logger = LoggerFactory.getLogger(TaskService.class);
private static final Logger logger = LoggerFactory.getLogger(TaskServiceImpl.class);

private Duration timeSliceDuration;

Expand Down Expand Up @@ -95,7 +99,7 @@ public class TaskService {

private TimeUnit tickerUnit = TimeUnit.MINUTES;

public TaskService(Session session, Queries queries, LeaseManager leaseManager, Duration timeSliceDuration,
public TaskServiceImpl(Session session, Queries queries, LeaseManager leaseManager, Duration timeSliceDuration,
List<TaskType> taskTypes) {
this.session = session;
this.queries = queries;
Expand All @@ -114,12 +118,7 @@ void setTickerUnit(TimeUnit tickerUnit) {
this.tickerUnit = tickerUnit;
}

/**
* Starts the scheduler. Task execution can be scheduled every second or every minute. Task execution for a
* particular time slice will run at the end of said time slice or later but never sooner.
*
* TODO log warning if scheduling falls behind
*/
@Override
public void start() {
Runnable runnable = () -> {
DateTime timeSlice = dateTimeService.getTimeSlice(now(), timeSliceDuration);
Expand All @@ -128,6 +127,7 @@ public void start() {
ticker.scheduleAtFixedRate(runnable, 0, 1, tickerUnit);
}

@Override
public void shutdown() {
// ticker.shutdownNow();
// scheduler.shutdownNow();
Expand All @@ -150,6 +150,7 @@ private TaskType findTaskType(String type) {
.orElseThrow(() -> new IllegalArgumentException(type + " is not a recognized task type"));
}

@Override
public ListenableFuture<Task> scheduleTask(DateTime time, Task task) {
TaskType taskType = findTaskType(task.getTaskType().getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.datastax.driver.core.Session;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.Uninterruptibles;
import org.hawkular.metrics.tasks.impl.Queries;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.tasks;
package org.hawkular.metrics.tasks.impl;

import static org.joda.time.DateTime.now;
import static org.testng.Assert.assertEquals;
Expand All @@ -26,6 +26,7 @@
import com.datastax.driver.core.PreparedStatement;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListenableFuture;
import org.hawkular.metrics.tasks.BaseTest;
import org.joda.time.DateTime;
import org.joda.time.Minutes;
import org.testng.annotations.BeforeClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.tasks;
package org.hawkular.metrics.tasks.impl;

import static java.util.Arrays.asList;
import static org.joda.time.DateTime.now;
Expand All @@ -25,6 +25,8 @@
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.hawkular.metrics.tasks.BaseTest;
import org.hawkular.metrics.tasks.api.TaskType;
import org.joda.time.DateTime;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -52,7 +54,8 @@ public void startScheduler() throws Exception {
dateTimeService.getTimeSlice(now().plusSeconds(2), standardSeconds(3))
);

TaskService taskService = new TaskService(session, queries, leaseManager, standardSeconds(1), taskTypes) {
TaskServiceImpl taskService = new TaskServiceImpl(session, queries, leaseManager, standardSeconds(1),
taskTypes) {
@Override
public void executeTasks(DateTime timeSlice) {
actualTimeSlices.add(timeSlice);
Expand Down

0 comments on commit d68d873

Please sign in to comment.