Search in sources :

Example 36 with Resource

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

the class CassandraIndexerITCase method testCache.

@Test
public void testCache() {
    ResourceMetadataCache cache = mock(ResourceMetadataCache.class);
    when(cache.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(false).build();
    Indexer indexer = new CassandraIndexer(newtsInstance.getCassandraSession(), 86400, cache, registry, options, new SimpleResourceIdSplitter(), contextConfigurations);
    Sample s = sampleFor(new Resource("aaa", Optional.of(map("beverage", "beer"))), "m0");
    indexer.update(Collections.singletonList(s));
    ResourceMetadata expected = new ResourceMetadata().putMetric("m0").putAttribute("beverage", "beer");
    verify(cache, atLeast(1)).get(any(Context.class), any(Resource.class));
    verify(cache).merge(any(Context.class), any(Resource.class), eq(expected));
}
Also used : Context(org.opennms.newts.api.Context) Indexer(org.opennms.newts.api.search.Indexer) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) QueryBuilder(org.opennms.newts.api.search.QueryBuilder) Resource(org.opennms.newts.api.Resource) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) Test(org.junit.Test)

Example 37 with Resource

use of org.opennms.newts.api.Resource 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 38 with Resource

use of org.opennms.newts.api.Resource 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 39 with Resource

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

the class GuavaResourceMetadataTest method test.

@Test
public void test() {
    Context c = new Context("c");
    Resource r = new Resource("r");
    ResourceMetadataCache cache = new GuavaResourceMetadataCache(10000, new MetricRegistry());
    assertThat(cache.get(c, r).isPresent(), not(true));
    cache.merge(c, r, new ResourceMetadata());
    assertThat(cache.get(c, r).isPresent(), is(true));
    assertThat(cache.get(c, r).get().containsMetric("m0"), not(true));
    assertThat(cache.get(c, r).get().containsMetric("m1"), not(true));
    cache.merge(c, r, new ResourceMetadata().putMetric("m0").putMetric("m1"));
    assertThat(cache.get(c, r).get().containsMetric("m0"), is(true));
    assertThat(cache.get(c, r).get().containsMetric("m1"), is(true));
    assertThat(cache.get(c, r).get().containsAttribute("meat", "beef"), not(true));
    assertThat(cache.get(c, r).get().containsAttribute("pudding", "bread"), not(true));
    cache.merge(c, r, new ResourceMetadata().putAttribute("meat", "beef"));
    cache.merge(c, r, new ResourceMetadata().putAttribute("pudding", "bread"));
    assertThat(cache.get(c, r).get().containsAttribute("meat", "beef"), is(true));
    assertThat(cache.get(c, r).get().containsAttribute("pudding", "bread"), is(true));
}
Also used : Context(org.opennms.newts.api.Context) MetricRegistry(com.codahale.metrics.MetricRegistry) Resource(org.opennms.newts.api.Resource) Test(org.junit.Test)

Example 40 with Resource

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

the class PrimaryDataTest method testHeartbeatNaNs.

@Test
public void testHeartbeatNaNs() {
    // Test for NEWTS-70
    Iterator<Row<Sample>> testData = new SampleRowsBuilder(new Resource("localhost"), MetricType.GAUGE).row(0).element("m1", 1).row(300).element("m1", 2).row(1800).element("m1", 8).build();
    ResultDescriptor rDescriptor = new ResultDescriptor().step(Duration.seconds(300)).datasource("m1", "m1", Duration.seconds(600), null);
    // Expected results
    Iterator<Row<Measurement>> expected = new MeasurementRowsBuilder(new Resource("localhost")).row(300).element("m1", 2).row(600).element("m1", Double.NaN).row(900).element("m1", Double.NaN).row(1200).element("m1", Double.NaN).row(1500).element("m1", Double.NaN).row(1800).element("m1", Double.NaN).build();
    PrimaryData primaryData = new PrimaryData(new Resource("localhost"), Timestamp.fromEpochSeconds(300), Timestamp.fromEpochSeconds(1800), rDescriptor, testData);
    assertRowsEqual(expected, primaryData);
}
Also used : MeasurementRowsBuilder(org.opennms.newts.aggregate.Utils.MeasurementRowsBuilder) Resource(org.opennms.newts.api.Resource) ResultDescriptor(org.opennms.newts.api.query.ResultDescriptor) Row(org.opennms.newts.api.Results.Row) SampleRowsBuilder(org.opennms.newts.aggregate.Utils.SampleRowsBuilder) Test(org.junit.Test)

Aggregations

Resource (org.opennms.newts.api.Resource)67 Test (org.junit.Test)50 Row (org.opennms.newts.api.Results.Row)35 ResultDescriptor (org.opennms.newts.api.query.ResultDescriptor)31 MeasurementRowsBuilder (org.opennms.newts.aggregate.Utils.MeasurementRowsBuilder)20 SampleRowsBuilder (org.opennms.newts.aggregate.Utils.SampleRowsBuilder)20 Sample (org.opennms.newts.api.Sample)19 Measurement (org.opennms.newts.api.Measurement)14 Context (org.opennms.newts.api.Context)12 MetricRegistry (com.codahale.metrics.MetricRegistry)9 SearchResults (org.opennms.newts.api.search.SearchResults)8 Timestamp (org.opennms.newts.api.Timestamp)7 ResourcePath (org.opennms.netmgt.model.ResourcePath)6 ContextConfigurations (org.opennms.newts.cassandra.ContextConfigurations)6 Map (java.util.Map)5 Results (org.opennms.newts.api.Results)5 CassandraSession (org.opennms.newts.cassandra.CassandraSession)5 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)4 List (java.util.List)4 Result (org.opennms.newts.api.search.SearchResults.Result)4