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));
}
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));
}
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();
}
}
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);
}
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());
}
Aggregations