Search in sources :

Example 6 with Context

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

the class SamplesResource method getSamples.

@GET
@Timed
@Path("/{resource}")
public Collection<Collection<SampleDTO>> getSamples(@PathParam("resource") Resource resource, @QueryParam("start") Optional<TimestampParam> start, @QueryParam("end") Optional<TimestampParam> end, @QueryParam("context") Optional<String> contextId) {
    Optional<Timestamp> lower = Transform.toTimestamp(start);
    Optional<Timestamp> upper = Transform.toTimestamp(end);
    Context context = contextId.isPresent() ? new Context(contextId.get()) : Context.DEFAULT_CONTEXT;
    return Transform.sampleDTOs(m_sampleRepository.select(context, resource, lower, upper));
}
Also used : Context(org.opennms.newts.api.Context) Timestamp(org.opennms.newts.api.Timestamp) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 7 with Context

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

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

the class CassandraIndexer method update.

@Override
public void update(Collection<Sample> samples) {
    Timer.Context ctx = m_updateTimer.time();
    Set<StatementGenerator> generators = Sets.newHashSet();
    Map<Context, Map<Resource, ResourceMetadata>> cacheQueue = Maps.newHashMap();
    for (Sample sample : samples) {
        maybeIndexResource(cacheQueue, generators, sample.getContext(), sample.getResource());
        maybeIndexResourceAttributes(cacheQueue, generators, sample.getContext(), sample.getResource());
        maybeAddMetricName(cacheQueue, generators, sample.getContext(), sample.getResource(), sample.getName());
    }
    try {
        if (!generators.isEmpty()) {
            synchronized (statementsInFlight) {
                generators.removeAll(statementsInFlight);
                statementsInFlight.addAll(generators);
            }
            m_inserts.mark(generators.size());
            // Asynchronously execute the statements
            List<ResultSetFuture> futures = Lists.newArrayList();
            for (Statement statementToExecute : toStatements(generators)) {
                futures.add(m_session.executeAsync(statementToExecute));
            }
            for (ResultSetFuture future : futures) {
                future.getUninterruptibly();
            }
        }
        // Order matters here; We want the cache updated only after a successful Cassandra write.
        for (Context context : cacheQueue.keySet()) {
            for (Map.Entry<Resource, ResourceMetadata> entry : cacheQueue.get(context).entrySet()) {
                m_cache.merge(context, entry.getKey(), entry.getValue());
            }
        }
    } finally {
        synchronized (statementsInFlight) {
            statementsInFlight.removeAll(generators);
        }
        ctx.stop();
    }
}
Also used : Context(org.opennms.newts.api.Context) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) Sample(org.opennms.newts.api.Sample) RegularStatement(com.datastax.driver.core.RegularStatement) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Statement(com.datastax.driver.core.Statement) Resource(org.opennms.newts.api.Resource) StatementGenerator(org.opennms.newts.cassandra.search.support.StatementGenerator) Timer(com.codahale.metrics.Timer) Map(java.util.Map)

Example 9 with Context

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

the class GuavaSearchableResourceMetadataCacheTest method getResourceIdsWithPrefixPerftTest.

@Test
@Ignore
public void getResourceIdsWithPrefixPerftTest() {
    long numResourceIdsToCache = 200000;
    long numSearches = 1000;
    Context ctx = Context.DEFAULT_CONTEXT;
    GuavaSearchableResourceMetadataCache cache = new GuavaSearchableResourceMetadataCache(numResourceIdsToCache, m_registry);
    ResourceMetadata resourceMetadata = new ResourceMetadata();
    for (long k = 0; k < numResourceIdsToCache; k++) {
        Resource resource = new Resource(String.format("snmp:%d:eth0-x:ifHcInOctets", k));
        cache.merge(ctx, resource, resourceMetadata);
    }
    long start = System.currentTimeMillis();
    String prefix = "snmp:" + (numResourceIdsToCache - 1);
    for (long k = 0; k < numSearches; k++) {
        assertEquals(1, cache.getResourceIdsWithPrefix(ctx, prefix).size());
    }
    long elapsed = System.currentTimeMillis() - start;
    System.err.println("elapsed: " + elapsed);
}
Also used : Context(org.opennms.newts.api.Context) Resource(org.opennms.newts.api.Resource) ResourceMetadata(org.opennms.newts.cassandra.search.ResourceMetadata) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 10 with Context

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

the class GuavaSearchableResourceMetadataCacheTest method canGetEntriesWithPrefix.

@Test
public void canGetEntriesWithPrefix() {
    Context ctx = Context.DEFAULT_CONTEXT;
    GuavaSearchableResourceMetadataCache cache = new GuavaSearchableResourceMetadataCache(2048, m_registry);
    assertTrue(cache.getResourceIdsWithPrefix(ctx, "a").isEmpty());
    Resource resource = new Resource("a:b:c");
    ResourceMetadata resourceMetadata = new ResourceMetadata();
    cache.merge(ctx, resource, resourceMetadata);
    assertTrue(cache.getResourceIdsWithPrefix(ctx, "a").contains("a:b:c"));
    assertTrue(cache.getResourceIdsWithPrefix(ctx, "a:b").contains("a:b:c"));
    assertTrue(cache.getResourceIdsWithPrefix(ctx, "a:b:c").contains("a:b:c"));
    assertTrue(cache.getResourceIdsWithPrefix(ctx, "a:b:c:d").isEmpty());
}
Also used : Context(org.opennms.newts.api.Context) Resource(org.opennms.newts.api.Resource) ResourceMetadata(org.opennms.newts.cassandra.search.ResourceMetadata) Test(org.junit.Test)

Aggregations

Context (org.opennms.newts.api.Context)12 Resource (org.opennms.newts.api.Resource)7 Test (org.junit.Test)5 ResourceMetadata (org.opennms.newts.cassandra.search.ResourceMetadata)5 Timed (com.codahale.metrics.annotation.Timed)4 Path (javax.ws.rs.Path)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 GET (javax.ws.rs.GET)2 Timestamp (org.opennms.newts.api.Timestamp)2 EscapableResourceIdSplitter (org.opennms.newts.cassandra.search.EscapableResourceIdSplitter)2 Gauge (com.codahale.metrics.Gauge)1 Timer (com.codahale.metrics.Timer)1 BoundStatement (com.datastax.driver.core.BoundStatement)1 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 RegularStatement (com.datastax.driver.core.RegularStatement)1 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)1 Statement (com.datastax.driver.core.Statement)1 Joiner (com.google.common.base.Joiner)1 Optional (com.google.common.base.Optional)1 Preconditions (com.google.common.base.Preconditions)1