Search in sources :

Example 1 with IntervalGenerator

use of org.opennms.newts.aggregate.IntervalGenerator 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 2 with IntervalGenerator

use of org.opennms.newts.aggregate.IntervalGenerator in project newts by OpenNMS.

the class SelectDispatcher method go.

@Override
void go() throws InterruptedException {
    createThreads();
    for (Timestamp t : new IntervalGenerator(m_config.getStart(), m_config.getEnd(), m_config.getSelectLength(), true)) {
        for (String resource : m_config.getResources()) {
            m_queryQueue.put(new Query(resource, t.minus(m_config.getSelectLength()), t, m_config.getResolution()));
        }
    }
    shutdown();
}
Also used : IntervalGenerator(org.opennms.newts.aggregate.IntervalGenerator) Timestamp(org.opennms.newts.api.Timestamp)

Example 3 with IntervalGenerator

use of org.opennms.newts.aggregate.IntervalGenerator 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)

Aggregations

IntervalGenerator (org.opennms.newts.aggregate.IntervalGenerator)3 Timestamp (org.opennms.newts.api.Timestamp)3 BoundStatement (com.datastax.driver.core.BoundStatement)2 Future (java.util.concurrent.Future)2 Duration (org.opennms.newts.api.Duration)2 ResultSet (com.datastax.driver.core.ResultSet)1 ExecutionException (java.util.concurrent.ExecutionException)1