Search in sources :

Example 61 with Resource

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

the class NewtsResourceStorageDao method setStringAttribute.

@Override
public void setStringAttribute(ResourcePath path, String key, String value) {
    // Create a mock sample referencing the resource
    Map<String, String> attributes = new ImmutableMap.Builder<String, String>().put(key, value).build();
    Resource resource = new Resource(toResourceId(path), Optional.of(attributes));
    Sample sample = NewtsUtils.createSampleForIndexingStrings(m_context, resource);
    // Index, but do not insert the sample(s)
    // The key/value pair specified in the attributes map will be merged with the others.
    m_newtsWriter.index(Lists.newArrayList(sample));
}
Also used : Sample(org.opennms.newts.api.Sample) Resource(org.opennms.newts.api.Resource)

Example 62 with Resource

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

the class NewtsFetchStrategyTest method createMockResource.

public Source createMockResource(final String label, final String attr, final String node, boolean expect) {
    OnmsResourceType type = EasyMock.createNiceMock(OnmsResourceType.class);
    final int nodeId = node.hashCode();
    final String newtsResourceId = "response:" + node + ":" + attr;
    final ResourceId resourceId = ResourceId.get("nodeSource", "NODES:" + nodeId).resolve("responseTime", node);
    OnmsResource resource = m_resources.get(resourceId);
    if (resource == null) {
        resource = new OnmsResource(attr, label, type, Sets.newHashSet(), ResourcePath.get("foo"));
        m_resources.put(resourceId, resource);
    }
    Set<OnmsAttribute> attributes = resource.getAttributes();
    attributes.add(new RrdGraphAttribute(attr, "", newtsResourceId));
    Results<Measurement> results = new Results<Measurement>();
    Resource res = new Resource(newtsResourceId);
    Row<Measurement> row = new Row<Measurement>(Timestamp.fromEpochSeconds(0), res);
    Measurement measurement = new Measurement(Timestamp.fromEpochSeconds(0), res, label, 0.0d);
    row.addElement(measurement);
    results.addRow(row);
    if (expect) {
        EasyMock.expect(m_sampleRepository.select(EasyMock.eq(m_context), EasyMock.eq(res), EasyMock.anyObject(), EasyMock.anyObject(), EasyMock.anyObject(), EasyMock.anyObject(), EasyMock.anyObject())).andReturn(results);
    }
    final Source source = new Source();
    source.setAggregation("AVERAGE");
    source.setAttribute(attr);
    source.setLabel(label);
    source.setResourceId(resourceId.toString());
    source.setTransient(false);
    return source;
}
Also used : Measurement(org.opennms.newts.api.Measurement) OnmsResource(org.opennms.netmgt.model.OnmsResource) Resource(org.opennms.newts.api.Resource) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) Source(org.opennms.netmgt.measurements.model.Source) OnmsResource(org.opennms.netmgt.model.OnmsResource) OnmsResourceType(org.opennms.netmgt.model.OnmsResourceType) ResourceId(org.opennms.netmgt.model.ResourceId) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) Results(org.opennms.newts.api.Results) Row(org.opennms.newts.api.Results.Row)

Example 63 with Resource

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

the class NewtsWriterTest method canWriteToSampleRepositoryUsingMultipleThreads.

/**
     * Uses a latch to verify that multiple that multiple threads
     * are used to concurrently insert samples into the SampleRepository.
     */
@Test
public void canWriteToSampleRepositoryUsingMultipleThreads() {
    int ringBufferSize = 1024;
    int numWriterThreads = 8;
    LatchedSampleRepository sampleRepo = new LatchedSampleRepository(numWriterThreads);
    MetricRegistry registry = new MetricRegistry();
    NewtsWriter writer = new NewtsWriter(1, ringBufferSize, numWriterThreads, registry);
    writer.setSampleRepository(sampleRepo);
    for (int i = 0; i < ringBufferSize * 2; i++) {
        Resource x = new Resource("x");
        Sample s = new Sample(Timestamp.now(), x, "y", MetricType.COUNTER, new Counter(i));
        writer.insert(Lists.newArrayList(s));
    }
}
Also used : Counter(org.opennms.newts.api.Counter) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) Resource(org.opennms.newts.api.Resource) Test(org.junit.Test)

Example 64 with Resource

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

the class NewtsWriterTest method samplesAreDroppedWhenRingBufferIsFull.

/**
     * Fills the ring buffer and locks all of the writer threads to verify
     * that samples additional samples are dropped.
     */
@Test
public void samplesAreDroppedWhenRingBufferIsFull() throws Exception {
    Resource x = new Resource("x");
    int ringBufferSize = 1024;
    int numWriterThreads = 8;
    Lock lock = new ReentrantLock();
    LockedSampleRepository sampleRepo = new LockedSampleRepository(lock);
    MetricRegistry registry = new MetricRegistry();
    NewtsWriter writer = new NewtsWriter(1, ringBufferSize, numWriterThreads, registry);
    writer.setSampleRepository(sampleRepo);
    lock.lock();
    for (int i = 0; i < ringBufferSize; i++) {
        Sample s = new Sample(Timestamp.now(), x, "y", MetricType.COUNTER, new Counter(i));
        writer.insert(Lists.newArrayList(s));
    }
    // The ring buffer should be full, and all of the threads should be locked
    Thread.sleep(250);
    assertEquals(numWriterThreads, sampleRepo.getNumThreadsLocked());
    // Attempt to insert another batch of samples
    for (int i = 0; i < 8; i++) {
        Sample s = new Sample(Timestamp.now(), x, "y", MetricType.COUNTER, new Counter(i));
        writer.insert(Lists.newArrayList(s));
    }
    ;
    // Unlock the writer threads and wait for the ring buffer to drain
    lock.unlock();
    writer.destroy();
    // Verify the number of inserted samples
    assertEquals(0, sampleRepo.getNumThreadsLocked());
    assertEquals(ringBufferSize, sampleRepo.getNumSamplesInserted());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Counter(org.opennms.newts.api.Counter) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) Resource(org.opennms.newts.api.Resource) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Example 65 with Resource

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

the class NewtsPersistOperationBuilder method getSamplesToIndex.

public List<Sample> getSamplesToIndex() {
    final List<Sample> samples = Lists.newLinkedList();
    // Convert string attributes to samples
    for (Entry<ResourcePath, Map<String, String>> entry : m_stringAttributesByPath.entrySet()) {
        Resource resource = new Resource(toResourceId(entry.getKey()), Optional.of(entry.getValue()));
        samples.add(NewtsUtils.createSampleForIndexingStrings(m_context, resource));
    }
    return samples;
}
Also used : ResourcePath(org.opennms.netmgt.model.ResourcePath) Sample(org.opennms.newts.api.Sample) Resource(org.opennms.newts.api.Resource) Map(java.util.Map)

Aggregations

Resource (org.opennms.newts.api.Resource)65 Test (org.junit.Test)48 Row (org.opennms.newts.api.Results.Row)33 ResultDescriptor (org.opennms.newts.api.query.ResultDescriptor)31 MeasurementRowsBuilder (org.opennms.newts.aggregate.Utils.MeasurementRowsBuilder)20 Sample (org.opennms.newts.api.Sample)19 SampleRowsBuilder (org.opennms.newts.aggregate.Utils.SampleRowsBuilder)18 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