Search in sources :

Example 26 with Sample

use of org.opennms.newts.api.Sample in project newts by OpenNMS.

the class CassandraIndexerITCase method canWalkTheResourceTree.

@Test
public void canWalkTheResourceTree() {
    Map<String, String> base = map("meat", "people", "bread", "beer");
    List<Sample> samples = Lists.newArrayList();
    samples.add(sampleFor(new Resource("a:b:c", Optional.of(base)), "m0"));
    samples.add(sampleFor(new Resource("a:b", Optional.of(base)), "m1"));
    samples.add(sampleFor(new Resource("x:b:z", Optional.of(base)), "m2"));
    CassandraSession session = newtsInstance.getCassandraSession();
    ResourceMetadataCache mockCache = mock(ResourceMetadataCache.class);
    when(mockCache.get(any(Context.class), any(Resource.class))).thenReturn(Optional.<ResourceMetadata>absent());
    MetricRegistry registry = new MetricRegistry();
    ContextConfigurations contextConfigurations = new ContextConfigurations();
    CassandraIndexingOptions options = new CassandraIndexingOptions.Builder().withHierarchicalIndexing(true).build();
    Indexer indexer = new CassandraIndexer(session, 86400, mockCache, registry, options, new SimpleResourceIdSplitter(), contextConfigurations);
    indexer.update(samples);
    CassandraSearcher searcher = new CassandraSearcher(session, registry, contextConfigurations);
    // Verify specific search results
    SearchResults results = searcher.search(Context.DEFAULT_CONTEXT, matchKeyAndValue("_parent", "_root"));
    Iterator<Result> it = results.iterator();
    Result result = it.next();
    assertThat(result.getResource().getId(), equalTo("a"));
    // a is a resource with no metrics
    assertThat(result.getMetrics().size(), equalTo(0));
    result = it.next();
    assertThat(result.getResource().getId(), equalTo("x"));
    // x is a resource with no metrics
    assertThat(result.getMetrics().size(), equalTo(0));
    results = searcher.search(Context.DEFAULT_CONTEXT, matchKeyAndValue("_parent", "a"));
    result = results.iterator().next();
    assertThat(result.getResource().getId(), equalTo("a:b"));
    assertThat(result.getMetrics().size(), equalTo(1));
    results = searcher.search(Context.DEFAULT_CONTEXT, matchKeyAndValue("_parent", "a:b"));
    result = results.iterator().next();
    assertThat(result.getResource().getId(), equalTo("a:b:c"));
    assertThat(result.getMetrics().size(), equalTo(1));
    results = searcher.search(Context.DEFAULT_CONTEXT, matchKeyAndValue("_parent", "a:b:c"));
    assertThat(results.iterator().hasNext(), equalTo(false));
    // Walk the tree via BFS
    LoggingResourceVisitor visitor = new LoggingResourceVisitor();
    CassandraResourceTreeWalker resourceTreeWalker = new CassandraResourceTreeWalker(searcher);
    resourceTreeWalker.breadthFirstSearch(Context.DEFAULT_CONTEXT, visitor);
    assertThat(visitor.getResourceIds(), equalTo(Lists.newArrayList("a", "x", "a:b", "x:b", "a:b:c", "x:b:z")));
    // Walk the tree via DFS
    visitor = new LoggingResourceVisitor();
    resourceTreeWalker.depthFirstSearch(Context.DEFAULT_CONTEXT, visitor);
    assertThat(visitor.getResourceIds(), equalTo(Lists.newArrayList("a", "a:b", "a:b:c", "x", "x:b", "x:b:z")));
}
Also used : Context(org.opennms.newts.api.Context) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) QueryBuilder(org.opennms.newts.api.search.QueryBuilder) Resource(org.opennms.newts.api.Resource) CassandraSession(org.opennms.newts.cassandra.CassandraSession) SearchResults(org.opennms.newts.api.search.SearchResults) Result(org.opennms.newts.api.search.SearchResults.Result) Indexer(org.opennms.newts.api.search.Indexer) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) Test(org.junit.Test)

Example 27 with Sample

use of org.opennms.newts.api.Sample in project newts by OpenNMS.

the class CassandraIndexerStressITCase method canIndexManyResources.

@Test
public void canIndexManyResources() {
    final int numResources = 20000;
    final int numSamplesPerResource = 3;
    // Setup the indexer
    ResultSetFuture future = mock(ResultSetFuture.class);
    CassandraSession session = mock(CassandraSession.class);
    when(session.executeAsync(any(Statement.class))).thenReturn(future);
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    BoundStatement boundStatement = mock(BoundStatement.class);
    when(session.prepare(any(RegularStatement.class))).thenReturn(preparedStatement);
    when(preparedStatement.bind()).thenReturn(boundStatement);
    when(boundStatement.setString(any(String.class), any(String.class))).thenReturn(boundStatement);
    ContextConfigurations contexts = new ContextConfigurations();
    MetricRegistry metrics = new MetricRegistry();
    CassandraIndexingOptions options = new CassandraIndexingOptions.Builder().withHierarchicalIndexing(true).build();
    ResourceIdSplitter resourceIdSplitter = new EscapableResourceIdSplitter();
    GuavaResourceMetadataCache cache = new GuavaResourceMetadataCache(numResources * 2, metrics);
    CassandraIndexer indexer = new CassandraIndexer(session, 0, cache, metrics, options, resourceIdSplitter, contexts);
    // Generate the resources and sample sets
    Resource[] resources = new Resource[numResources];
    List<List<Sample>> sampleSets = Lists.newArrayListWithCapacity(numResources);
    System.out.println("Building sample sets...");
    for (int i = 0; i < numResources; i++) {
        resources[i] = new Resource(String.format("snmp:%d:eth0-x:ifHcInOctets", i));
        List<Sample> samples = Lists.newArrayListWithCapacity(numSamplesPerResource);
        for (int j = 0; j < numSamplesPerResource; j++) {
            samples.add(new Sample(Timestamp.now(), resources[i], "y" + j, MetricType.COUNTER, new Counter(i * j)));
        }
        sampleSets.add(samples);
    }
    ;
    System.out.println("Done building sample sets.");
    // Index the resources and associated samples several times over
    for (int k = 0; k < 3; k++) {
        System.out.println("Indexing samples sets...");
        long start = System.currentTimeMillis();
        for (List<Sample> sampleSet : sampleSets) {
            indexer.update(sampleSet);
        }
        long elapsed = System.currentTimeMillis() - start;
        System.out.println("Done indexing samples in : " + elapsed + " ms");
    }
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) RegularStatement(com.datastax.driver.core.RegularStatement) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Statement(com.datastax.driver.core.Statement) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) Resource(org.opennms.newts.api.Resource) CassandraSession(org.opennms.newts.cassandra.CassandraSession) PreparedStatement(com.datastax.driver.core.PreparedStatement) RegularStatement(com.datastax.driver.core.RegularStatement) Counter(org.opennms.newts.api.Counter) List(java.util.List) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) BoundStatement(com.datastax.driver.core.BoundStatement) Test(org.junit.Test)

Example 28 with Sample

use of org.opennms.newts.api.Sample in project newts by OpenNMS.

the class CassandraSampleRepository method select.

@Override
public Results<Sample> select(Context context, Resource resource, Optional<Timestamp> start, Optional<Timestamp> end) {
    Timer.Context timer = m_sampleSelectTimer.time();
    validateSelect(start, end);
    Timestamp upper = end.isPresent() ? end.get() : Timestamp.now();
    Timestamp lower = start.isPresent() ? start.get() : upper.minus(Duration.seconds(86400));
    LOG.debug("Querying database for resource {}, from {} to {}", resource, lower, upper);
    Results<Sample> samples = new Results<>();
    DriverAdapter driverAdapter = new DriverAdapter(cassandraSelect(context, resource, lower, upper));
    for (Row<Sample> row : driverAdapter) {
        samples.addRow(row);
    }
    LOG.debug("{} results returned from database", driverAdapter.getResultCount());
    m_samplesSelected.mark(driverAdapter.getResultCount());
    try {
        return samples;
    } finally {
        timer.stop();
    }
}
Also used : Timer(com.codahale.metrics.Timer) Results(org.opennms.newts.api.Results) Sample(org.opennms.newts.api.Sample) Timestamp(org.opennms.newts.api.Timestamp)

Example 29 with Sample

use of org.opennms.newts.api.Sample in project newts by OpenNMS.

the class DriverAdapter method next.

@Override
public Results.Row<Sample> next() {
    if (!hasNext())
        throw new NoSuchElementException();
    Results.Row<Sample> nextNext = null;
    while (m_results.hasNext()) {
        Sample m = getNextSample();
        if (m.getTimestamp().gt(m_next.getTimestamp())) {
            nextNext = new Results.Row<>(m.getTimestamp(), m.getResource());
            addSample(nextNext, m);
            break;
        }
        addSample(m_next, m);
    }
    try {
        return m_next;
    } finally {
        m_next = nextNext;
    }
}
Also used : Results(org.opennms.newts.api.Results) Sample(org.opennms.newts.api.Sample) NoSuchElementException(java.util.NoSuchElementException)

Example 30 with Sample

use of org.opennms.newts.api.Sample in project newts by OpenNMS.

the class RateTest method test.

@Test
public void test() {
    Results<Sample> input = new Results<>();
    int rows = 10, cols = 2, rate = 100;
    for (int i = 1; i <= rows; i++) {
        Timestamp t = Timestamp.fromEpochMillis(i * 1000);
        for (int j = 0; j < cols; j++) {
            input.addElement(new Sample(t, m_resource, m_metrics[j], COUNTER, new Counter((i + j) * rate)));
        }
    }
    Iterator<Results.Row<Sample>> output = new Rate(input.iterator(), getMetrics(cols)).iterator();
    for (int i = 1; i <= rows; i++) {
        assertTrue("Insufficient number of results", output.hasNext());
        Results.Row<Sample> row = output.next();
        assertEquals("Unexpected row timestamp", Timestamp.fromEpochMillis(i * 1000), row.getTimestamp());
        assertEquals("Unexpected row resource", m_resource, row.getResource());
        assertEquals("Unexpected number of columns", cols, row.getElements().size());
        for (int j = 0; j < cols; j++) {
            String name = m_metrics[j];
            assertNotNull("Missing sample" + name, row.getElement(name));
            assertEquals("Unexpected sample name", name, row.getElement(name).getName());
            assertEquals("Unexpected sample type", GAUGE, row.getElement(name).getType());
            // Samples in the first row are null, this is normal.
            if (i != 1) {
                assertEquals("Incorrect rate value", 100.0d, row.getElement(name).getValue().doubleValue(), 0.0d);
            }
        }
    }
}
Also used : Counter(org.opennms.newts.api.Counter) Results(org.opennms.newts.api.Results) Sample(org.opennms.newts.api.Sample) Rate(org.opennms.newts.aggregate.Rate) Row(org.opennms.newts.api.Results.Row) Timestamp(org.opennms.newts.api.Timestamp) Test(org.junit.Test)

Aggregations

Sample (org.opennms.newts.api.Sample)37 Resource (org.opennms.newts.api.Resource)18 Test (org.junit.Test)14 Timestamp (org.opennms.newts.api.Timestamp)12 MetricRegistry (com.codahale.metrics.MetricRegistry)9 Gauge (org.opennms.newts.api.Gauge)6 ContextConfigurations (org.opennms.newts.cassandra.ContextConfigurations)6 Timer (com.codahale.metrics.Timer)5 List (java.util.List)5 Counter (org.opennms.newts.api.Counter)5 Row (org.opennms.newts.api.Results.Row)5 CassandraSession (org.opennms.newts.cassandra.CassandraSession)5 Map (java.util.Map)4 Context (org.opennms.newts.api.Context)4 Results (org.opennms.newts.api.Results)4 Meter (com.codahale.metrics.Meter)3 BoundStatement (com.datastax.driver.core.BoundStatement)3 PreparedStatement (com.datastax.driver.core.PreparedStatement)3 RegularStatement (com.datastax.driver.core.RegularStatement)3 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)3