use of org.opennms.newts.api.Duration in project newts by OpenNMS.
the class CassandraSampleRepository method delete.
@Override
public void delete(Context context, Resource resource) {
/**
* Check for ttl value > 0
*/
if (m_ttl > 0) {
/**
* Delete exactly from (now - ttl) till now
*/
final Timestamp start = Timestamp.now().minus(m_ttl, TimeUnit.SECONDS);
final Timestamp end = Timestamp.now();
final Duration resourceShard = m_contextConfigurations.getResourceShard(context);
final List<Future<ResultSet>> futures = Lists.newArrayList();
for (Timestamp partition : new IntervalGenerator(start.stepFloor(resourceShard), end.stepFloor(resourceShard), resourceShard)) {
BoundStatement bindStatement = m_deleteStatement.bind();
bindStatement.setString(SchemaConstants.F_CONTEXT, context.getId());
bindStatement.setInt(SchemaConstants.F_PARTITION, (int) partition.asSeconds());
bindStatement.setString(SchemaConstants.F_RESOURCE, resource.getId());
futures.add(m_session.executeAsync(bindStatement));
}
for (final Future<ResultSet> future : futures) {
try {
future.get();
} catch (final InterruptedException | ExecutionException e) {
throw Throwables.propagate(e);
}
}
} else {
// Choose (now - one year) till now...
Timestamp end = Timestamp.now();
Timestamp start = end.minus(DELETION_INTERVAL, TimeUnit.DAYS);
// ... and check whether samples exist for this period of time.
while (cassandraSelect(context, resource, start, end).hasNext()) {
// Now delete the samples...
final Duration resourceShard = m_contextConfigurations.getResourceShard(context);
final List<Future<ResultSet>> futures = Lists.newArrayList();
for (Timestamp partition : new IntervalGenerator(start.stepFloor(resourceShard), end.stepFloor(resourceShard), resourceShard)) {
BoundStatement bindStatement = m_deleteStatement.bind();
bindStatement.setString(SchemaConstants.F_CONTEXT, context.getId());
bindStatement.setInt(SchemaConstants.F_PARTITION, (int) partition.asSeconds());
bindStatement.setString(SchemaConstants.F_RESOURCE, resource.getId());
futures.add(m_session.executeAsync(bindStatement));
}
for (final Future<ResultSet> future : futures) {
try {
future.get();
} catch (final InterruptedException | ExecutionException e) {
throw Throwables.propagate(e);
}
}
// ...set end to start and start to (end - one year)
end = start;
start = end.minus(DELETION_INTERVAL, TimeUnit.DAYS);
// and start over again until no more samples are found
}
}
}
use of org.opennms.newts.api.Duration in project newts by OpenNMS.
the class CassandraSampleRepository method cassandraSelect.
private Iterator<com.datastax.driver.core.Row> cassandraSelect(Context context, Resource resource, Timestamp start, Timestamp end) {
List<Future<ResultSet>> futures = Lists.newArrayList();
Duration resourceShard = m_contextConfigurations.getResourceShard(context);
Timestamp lower = start.stepFloor(resourceShard);
Timestamp upper = end.stepFloor(resourceShard);
for (Timestamp partition : new IntervalGenerator(lower, upper, resourceShard)) {
BoundStatement bindStatement = m_selectStatement.bind();
bindStatement.setString(SchemaConstants.F_CONTEXT, context.getId());
bindStatement.setInt(SchemaConstants.F_PARTITION, (int) partition.asSeconds());
bindStatement.setString(SchemaConstants.F_RESOURCE, resource.getId());
bindStatement.setTimestamp("start", start.asDate());
bindStatement.setTimestamp("end", end.asDate());
// Use the context specific consistency level
bindStatement.setConsistencyLevel(m_contextConfigurations.getReadConsistency(context));
futures.add(m_session.executeAsync(bindStatement));
}
return new ConcurrentResultWrapper(futures);
}
use of org.opennms.newts.api.Duration in project newts by OpenNMS.
the class CassandraSampleRepository method select.
@Override
public Results<Measurement> select(Context context, Resource resource, Optional<Timestamp> start, Optional<Timestamp> end, ResultDescriptor descriptor, Optional<Duration> resolution, SampleSelectCallback callback) {
Timer.Context timer = m_measurementSelectTimer.time();
validateSelect(start, end);
Timestamp upper = end.isPresent() ? end.get() : Timestamp.now();
Timestamp lower = start.isPresent() ? start.get() : upper.minus(Duration.seconds(86400));
Duration step;
if (resolution.isPresent()) {
step = resolution.get();
} else {
// Determine the ideal step size, splitting the interval evenly into N slices
long stepMillis = upper.minus(lower).asMillis() / TARGET_NUMBER_OF_STEPS;
// But every step must be a multiple of the interval
long intervalMillis = descriptor.getInterval().asMillis();
// If the interval is greater than the target step, use the 2 * interval as the step
if (intervalMillis >= stepMillis) {
step = descriptor.getInterval().times(2);
} else {
// Otherwise, round stepMillkeyis up to the closest multiple of intervalMillis
long remainderMillis = stepMillis % intervalMillis;
if (remainderMillis != 0) {
stepMillis = stepMillis + intervalMillis - remainderMillis;
}
step = Duration.millis(stepMillis);
}
}
LOG.debug("Querying database for resource {}, from {} to {}", resource, lower.minus(step), upper);
DriverAdapter driverAdapter = new DriverAdapter(cassandraSelect(context, resource, lower.minus(step), upper), descriptor.getSourceNames());
Results<Measurement> results;
callback.beforeProcess();
try {
results = new ResultProcessor(resource, lower, upper, descriptor, step).process(driverAdapter);
} finally {
callback.afterProcess();
}
LOG.debug("{} results returned from database", driverAdapter.getResultCount());
m_samplesSelected.mark(driverAdapter.getResultCount());
try {
return results;
} finally {
timer.stop();
}
}
Aggregations