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