Search in sources :

Example 6 with Duration

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
        }
    }
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Future(java.util.concurrent.Future) Duration(org.opennms.newts.api.Duration) IntervalGenerator(org.opennms.newts.aggregate.IntervalGenerator) ExecutionException(java.util.concurrent.ExecutionException) Timestamp(org.opennms.newts.api.Timestamp) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 7 with Duration

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);
}
Also used : Future(java.util.concurrent.Future) Duration(org.opennms.newts.api.Duration) IntervalGenerator(org.opennms.newts.aggregate.IntervalGenerator) Timestamp(org.opennms.newts.api.Timestamp) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 8 with Duration

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();
    }
}
Also used : Measurement(org.opennms.newts.api.Measurement) Timer(com.codahale.metrics.Timer) Duration(org.opennms.newts.api.Duration) ResultProcessor(org.opennms.newts.aggregate.ResultProcessor) Timestamp(org.opennms.newts.api.Timestamp)

Aggregations

Duration (org.opennms.newts.api.Duration)8 Timestamp (org.opennms.newts.api.Timestamp)7 Sample (org.opennms.newts.api.Sample)3 Timer (com.codahale.metrics.Timer)2 BoundStatement (com.datastax.driver.core.BoundStatement)2 Future (java.util.concurrent.Future)2 IntervalGenerator (org.opennms.newts.aggregate.IntervalGenerator)2 Measurement (org.opennms.newts.api.Measurement)2 Row (org.opennms.newts.api.Results.Row)2 Timed (com.codahale.metrics.annotation.Timed)1 ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)1 ResultSet (com.datastax.driver.core.ResultSet)1 Batch (com.datastax.driver.core.querybuilder.Batch)1 Insert (com.datastax.driver.core.querybuilder.Insert)1 QueryBuilder.unloggedBatch (com.datastax.driver.core.querybuilder.QueryBuilder.unloggedBatch)1 NoSuchElementException (java.util.NoSuchElementException)1 ExecutionException (java.util.concurrent.ExecutionException)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Test (org.junit.Test)1