use of org.opennms.newts.cassandra.ContextConfigurations in project newts by OpenNMS.
the class CassandraIndexerITCase method testDelete.
@Test
public void testDelete() {
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();
CassandraSession session = newtsInstance.getCassandraSession();
CassandraIndexingOptions options = new CassandraIndexingOptions.Builder().withHierarchicalIndexing(false).build();
Indexer indexer = new CassandraIndexer(session, 86400, cache, registry, options, new SimpleResourceIdSplitter(), contextConfigurations);
CassandraSearcher searcher = new CassandraSearcher(session, registry, contextConfigurations);
Map<String, String> base = map("meat", "people", "bread", "beer");
List<Sample> samples = Lists.newArrayList();
samples.add(sampleFor(new Resource("aaa", Optional.of(base)), "m0"));
samples.add(sampleFor(new Resource("aab", Optional.of(map(base, "music", "metal", "beverage", "beer"))), "m0"));
samples.add(sampleFor(new Resource("aac:aaa", Optional.of(map(base, "music", "country"))), "m0"));
indexer.update(samples);
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(2));
indexer.delete(Context.DEFAULT_CONTEXT, new Resource("aaa", Optional.of(base)));
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(1));
indexer.delete(Context.DEFAULT_CONTEXT, new Resource("aaa", Optional.of(base)));
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(1));
indexer.delete(Context.DEFAULT_CONTEXT, new Resource("aac:aaa", Optional.of(base)));
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(0));
}
use of org.opennms.newts.cassandra.ContextConfigurations in project newts by OpenNMS.
the class CassandraIndexerTest method insertStatementsAreDeduplicatedWhenIndexingManySamples.
@Test
public void insertStatementsAreDeduplicatedWhenIndexingManySamples() {
CassandraSession session = mock(CassandraSession.class);
ArgumentCaptor<Statement> statementCaptor = ArgumentCaptor.forClass(Statement.class);
when(session.executeAsync(statementCaptor.capture())).thenReturn(mock(ResultSetFuture.class));
PreparedStatement statement = mock(PreparedStatement.class);
BoundStatement boundStatement = mock(BoundStatement.class);
when(session.prepare(any(RegularStatement.class))).thenReturn(statement);
when(statement.bind()).thenReturn(boundStatement);
when(boundStatement.setString(any(String.class), any(String.class))).thenReturn(boundStatement);
CassandraIndexingOptions options = new CassandraIndexingOptions.Builder().withHierarchicalIndexing(true).withMaxBatchSize(1).build();
MetricRegistry registry = new MetricRegistry();
GuavaResourceMetadataCache cache = new GuavaResourceMetadataCache(2048, registry);
CassandraIndexer indexer = new CassandraIndexer(session, 0, cache, registry, options, new EscapableResourceIdSplitter(), new ContextConfigurations());
Resource r = new Resource("snmp:1589:vmware5Cpu:2:vmware5Cpu");
List<Sample> samples = Lists.newArrayList();
samples.add(new Sample(Timestamp.now(), r, "CpuCostopSum", MetricType.GAUGE, new Gauge(0)));
samples.add(new Sample(Timestamp.now(), r, "CpuIdleSum", MetricType.GAUGE, new Gauge(19299.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuMaxLdSum", MetricType.GAUGE, new Gauge(0)));
samples.add(new Sample(Timestamp.now(), r, "CpuOverlapSum", MetricType.GAUGE, new Gauge(5.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuRdySum", MetricType.GAUGE, new Gauge(41.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuRunSum", MetricType.GAUGE, new Gauge(619.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuSpwaitSum", MetricType.GAUGE, new Gauge(0)));
samples.add(new Sample(Timestamp.now(), r, "CpuSystemSum", MetricType.GAUGE, new Gauge(0)));
samples.add(new Sample(Timestamp.now(), r, "CpuUsagemhzAvg", MetricType.GAUGE, new Gauge(32.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuUsedSum", MetricType.GAUGE, new Gauge(299.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuWaitSum", MetricType.GAUGE, new Gauge(19343)));
// Index the collection of samples
indexer.update(samples);
// Verify the number of exectuteAsync calls
verify(session, times(20)).executeAsync(any(Statement.class));
}
use of org.opennms.newts.cassandra.ContextConfigurations in project newts by OpenNMS.
the class CassandraGuiceModule method configure.
@Override
protected void configure() {
bind(String.class).annotatedWith(named("cassandra.keyspace")).toInstance(m_newtsConf.getCassandraKeyspace());
bind(String.class).annotatedWith(named("cassandra.hostname")).toInstance(m_newtsConf.getCassandraHost());
bind(Integer.class).annotatedWith(named("cassandra.port")).toInstance(m_newtsConf.getCassandraPort());
bind(String.class).annotatedWith(named("cassandra.compression")).toInstance(m_newtsConf.getCassandraCompression());
bind(String.class).annotatedWith(named("cassandra.username")).toInstance(m_newtsConf.getCassandraUsername());
bind(String.class).annotatedWith(named("cassandra.password")).toInstance(m_newtsConf.getCassandraPassword());
bind(Boolean.class).annotatedWith(named("cassandra.ssl")).toInstance(m_newtsConf.getCassandraSsl());
bind(Integer.class).annotatedWith(named("samples.cassandra.time-to-live")).toInstance(m_newtsConf.getCassandraColumnTTL());
bind(Integer.class).annotatedWith(named("search.cassandra.time-to-live")).toInstance(m_newtsConf.getCassandraColumnTTL());
bind(Integer.class).annotatedWith(named("sampleProcessor.maxThreads")).toInstance(m_newtsConf.getMaxSampleProcessorThreads());
bind(Long.class).annotatedWith(named("search.resourceMetadata.maxCacheEntries")).toInstance(m_newtsConf.getSearchConfig().getMaxCacheEntries());
bind(Boolean.class).annotatedWith(named("search.hierarical-indexing")).toInstance(m_newtsConf.getSearchConfig().isHierarchicalIndexingEnabled());
bind(CassandraSession.class).to(CassandraSessionImpl.class);
bind(ResourceMetadataCache.class).to(GuavaResourceMetadataCache.class);
bind(Searcher.class).to(CassandraSearcher.class);
bind(SampleRepository.class).to(CassandraSampleRepository.class);
bind(Indexer.class).to(CassandraIndexer.class);
Multibinder<SampleProcessor> processors = Multibinder.newSetBinder(binder(), SampleProcessor.class);
if (m_newtsConf.getSearchConfig().isSeparatorEscapingEnabled()) {
bind(ResourceIdSplitter.class).to(EscapableResourceIdSplitter.class);
} else {
bind(ResourceIdSplitter.class).to(SimpleResourceIdSplitter.class);
}
// Only add the search indexer if search is enabled
if (m_newtsConf.getSearchConfig().isEnabled()) {
processors.addBinding().to(CassandraIndexerSampleProcessor.class);
}
// Pull in context specific attributes
ContextConfigurations contextConfigurations = new ContextConfigurations();
for (ContextConfig contextConfig : m_newtsConf.getContextConfigs().values()) {
contextConfigurations.addContextConfig(contextConfig.getContext(), contextConfig.getResourceShard(), contextConfig.getReadConsistency(), contextConfig.getWriteConsistency());
}
bind(ContextConfigurations.class).toInstance(contextConfigurations);
}
use of org.opennms.newts.cassandra.ContextConfigurations in project opennms by OpenNMS.
the class ContextConfigurationFactory method getContextConfigurations.
public static ContextConfigurations getContextConfigurations() {
String resourceShardStr = System.getProperty("org.opennms.newts.config.resource_shard", "604800");
String readConsistencyStr = System.getProperty("org.opennms.newts.config.read_consistency", "ONE");
String writeConsistencyStr = System.getProperty("org.opennms.newts.config.write_consistency", "ANY");
Duration resourceShard = Duration.seconds(Long.parseLong(resourceShardStr));
ConsistencyLevel readConsistency = ConsistencyLevel.valueOf(readConsistencyStr);
ConsistencyLevel writeConsistency = ConsistencyLevel.valueOf(writeConsistencyStr);
ContextConfigurations contexts = new ContextConfigurations();
contexts.addContextConfig(Context.DEFAULT_CONTEXT, resourceShard, readConsistency, writeConsistency);
return contexts;
}
use of org.opennms.newts.cassandra.ContextConfigurations in project newts by OpenNMS.
the class CassandraIndexerITCase method test.
@Test
public void test() {
Map<String, String> base = map("meat", "people", "bread", "beer");
List<Sample> samples = Lists.newArrayList();
samples.add(sampleFor(new Resource("aaa", Optional.of(base)), "m0"));
samples.add(sampleFor(new Resource("aab", Optional.of(map(base, "music", "metal", "beverage", "beer"))), "m0"));
samples.add(sampleFor(new Resource("aac:aaa", Optional.of(map(base, "music", "country"))), "m0"));
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(false).build();
Indexer indexer = new CassandraIndexer(session, 86400, mockCache, registry, options, new SimpleResourceIdSplitter(), contextConfigurations);
indexer.update(samples);
CassandraSearcher searcher = new CassandraSearcher(session, registry, contextConfigurations);
// Match path components
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(2));
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aac")).size(), equalTo(1));
// Match attribute values
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("people")).size(), equalTo(3));
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("metal")).size(), equalTo(1));
// Match attribute key + value pairs
BooleanQuery query = new BooleanQuery();
query.add(new TermQuery(new Term("beverage", "beer")), Operator.OR);
assertThat(searcher.search(Context.DEFAULT_CONTEXT, query).size(), equalTo(1));
// Or'd terms
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("metal", "country")).size(), equalTo(2));
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("beer", "wine")).size(), equalTo(3));
// And'd terms
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAllValues("metal", "country")).size(), equalTo(0));
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAllValues("aaa", "aac")).size(), equalTo(1));
// Groups queries
// (beer AND metal) OR (aaa AND country)
BooleanQuery subquery1 = new BooleanQuery();
subquery1.add(new TermQuery(new Term("beer")), Operator.OR);
subquery1.add(new TermQuery(new Term("metal")), Operator.AND);
BooleanQuery subquery2 = new BooleanQuery();
subquery2.add(new TermQuery(new Term("aaa")), Operator.OR);
subquery2.add(new TermQuery(new Term("country")), Operator.AND);
query = new BooleanQuery();
query.add(subquery1, Operator.OR);
query.add(subquery2, Operator.OR);
assertThat(searcher.search(Context.DEFAULT_CONTEXT, query).size(), equalTo(2));
// Attributes are retrieved
Result r = searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("metal")).iterator().next();
assertThat(r.getResource().getId(), is(equalTo("aab")));
assertThat(r.getResource().getAttributes().isPresent(), is(true));
assertThat(r.getResource().getAttributes().get(), equalTo(map(base, "music", "metal", "beverage", "beer")));
// Metrics too
assertThat(r.getMetrics().size(), equalTo(1));
assertThat(r.getMetrics().iterator().next(), equalTo("m0"));
}
Aggregations