Search in sources :

Example 1 with ContextConfigurations

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));
}
Also used : Context(org.opennms.newts.api.Context) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) QueryBuilder(org.opennms.newts.api.search.QueryBuilder) Resource(org.opennms.newts.api.Resource) CassandraSession(org.opennms.newts.cassandra.CassandraSession) Indexer(org.opennms.newts.api.search.Indexer) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) Test(org.junit.Test)

Example 2 with ContextConfigurations

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));
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) RegularStatement(com.datastax.driver.core.RegularStatement) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Statement(com.datastax.driver.core.Statement) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) Resource(org.opennms.newts.api.Resource) CassandraSession(org.opennms.newts.cassandra.CassandraSession) PreparedStatement(com.datastax.driver.core.PreparedStatement) RegularStatement(com.datastax.driver.core.RegularStatement) Gauge(org.opennms.newts.api.Gauge) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) BoundStatement(com.datastax.driver.core.BoundStatement) Test(org.junit.Test)

Example 3 with ContextConfigurations

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);
}
Also used : CassandraSampleRepository(org.opennms.newts.persistence.cassandra.CassandraSampleRepository) SampleRepository(org.opennms.newts.api.SampleRepository) CassandraIndexer(org.opennms.newts.cassandra.search.CassandraIndexer) Indexer(org.opennms.newts.api.search.Indexer) CassandraSearcher(org.opennms.newts.cassandra.search.CassandraSearcher) Searcher(org.opennms.newts.api.search.Searcher) ResourceMetadataCache(org.opennms.newts.cassandra.search.ResourceMetadataCache) GuavaResourceMetadataCache(org.opennms.newts.cassandra.search.GuavaResourceMetadataCache) CassandraSession(org.opennms.newts.cassandra.CassandraSession) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) CassandraIndexerSampleProcessor(org.opennms.newts.cassandra.search.CassandraIndexerSampleProcessor) SampleProcessor(org.opennms.newts.api.SampleProcessor) EscapableResourceIdSplitter(org.opennms.newts.cassandra.search.EscapableResourceIdSplitter) ResourceIdSplitter(org.opennms.newts.cassandra.search.ResourceIdSplitter) SimpleResourceIdSplitter(org.opennms.newts.cassandra.search.SimpleResourceIdSplitter)

Example 4 with 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;
}
Also used : ConsistencyLevel(com.datastax.driver.core.ConsistencyLevel) Duration(org.opennms.newts.api.Duration) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations)

Example 5 with ContextConfigurations

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"));
}
Also used : Context(org.opennms.newts.api.Context) BooleanQuery(org.opennms.newts.api.search.BooleanQuery) TermQuery(org.opennms.newts.api.search.TermQuery) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) QueryBuilder(org.opennms.newts.api.search.QueryBuilder) Resource(org.opennms.newts.api.Resource) CassandraSession(org.opennms.newts.cassandra.CassandraSession) Term(org.opennms.newts.api.search.Term) Result(org.opennms.newts.api.search.SearchResults.Result) Indexer(org.opennms.newts.api.search.Indexer) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) Test(org.junit.Test)

Aggregations

ContextConfigurations (org.opennms.newts.cassandra.ContextConfigurations)8 MetricRegistry (com.codahale.metrics.MetricRegistry)6 Test (org.junit.Test)6 Resource (org.opennms.newts.api.Resource)6 Sample (org.opennms.newts.api.Sample)6 CassandraSession (org.opennms.newts.cassandra.CassandraSession)6 Indexer (org.opennms.newts.api.search.Indexer)5 Context (org.opennms.newts.api.Context)4 QueryBuilder (org.opennms.newts.api.search.QueryBuilder)4 BoundStatement (com.datastax.driver.core.BoundStatement)2 PreparedStatement (com.datastax.driver.core.PreparedStatement)2 RegularStatement (com.datastax.driver.core.RegularStatement)2 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)2 Statement (com.datastax.driver.core.Statement)2 Result (org.opennms.newts.api.search.SearchResults.Result)2 ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)1 List (java.util.List)1 Counter (org.opennms.newts.api.Counter)1 Duration (org.opennms.newts.api.Duration)1 Gauge (org.opennms.newts.api.Gauge)1