Search in sources :

Example 1 with Context

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

the class RedisResourceMetadataCache method getResourceIdsWithPrefix.

@Override
public List<String> getResourceIdsWithPrefix(Context context, String resourceIdPrefix) {
    try (Jedis jedis = m_pool.getResource()) {
        final List<String> elements = Lists.newArrayList(SEARCH_PREFIX, context.getId());
        elements.addAll(m_resourceIdSplitter.splitIdIntoElements(resourceIdPrefix));
        return jedis.lrange(m_resourceIdSplitter.joinElementsToId(elements).getBytes(), 0, -1).stream().map(bytes -> resourceId(METADATA_PREFIX, context.getId(), bytes)).collect(Collectors.toList());
    }
}
Also used : Context(org.opennms.newts.api.Context) MetricRegistry(com.codahale.metrics.MetricRegistry) Resource(org.opennms.newts.api.Resource) Collectors(java.util.stream.Collectors) Inject(javax.inject.Inject) ResourceIdSplitter(org.opennms.newts.cassandra.search.ResourceIdSplitter) List(java.util.List) Lists(com.google.common.collect.Lists) Jedis(redis.clients.jedis.Jedis) Optional(com.google.common.base.Optional) FSTConfiguration(org.nustaq.serialization.FSTConfiguration) ResourceMetadata(org.opennms.newts.cassandra.search.ResourceMetadata) Transaction(redis.clients.jedis.Transaction) Preconditions(com.google.common.base.Preconditions) JedisPool(redis.clients.jedis.JedisPool) Named(javax.inject.Named) Gauge(com.codahale.metrics.Gauge) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig) Joiner(com.google.common.base.Joiner) Jedis(redis.clients.jedis.Jedis)

Example 2 with Context

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

the class RedisResourceMetadataCacheIT method canGetEntriesWithPrefix.

@Test
public void canGetEntriesWithPrefix() {
    Context ctx = Context.DEFAULT_CONTEXT;
    RedisResourceMetadataCache cache = new RedisResourceMetadataCache(REDIS_HOSTNAME, REDIS_PORT, 8, m_registry, new EscapableResourceIdSplitter());
    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());
}
Also used : Context(org.opennms.newts.api.Context) EscapableResourceIdSplitter(org.opennms.newts.cassandra.search.EscapableResourceIdSplitter) Resource(org.opennms.newts.api.Resource) ResourceMetadata(org.opennms.newts.cassandra.search.ResourceMetadata) Test(org.junit.Test)

Example 3 with Context

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

the class SearchResource method search.

@GET
@Timed
public Collection<SearchResultDTO> search(@QueryParam("q") Optional<String> query, @QueryParam("context") Optional<String> contextId) {
    checkArgument(query.isPresent(), "missing required query parameter (q=<argument>)");
    QueryParser qp = new QueryParser();
    Query parsedQuery;
    try {
        parsedQuery = qp.parse(query.get());
    } catch (ParseException e) {
        throw new WebApplicationException(e, Response.status(Status.BAD_REQUEST).entity("Invalid query " + query.get()).build());
    }
    Context context = contextId.isPresent() ? new Context(contextId.get()) : Context.DEFAULT_CONTEXT;
    return Transform.searchResultDTOs(m_searcher.search(context, parsedQuery));
}
Also used : Context(org.opennms.newts.api.Context) QueryParser(org.opennms.newts.api.search.query.QueryParser) Query(org.opennms.newts.api.search.Query) WebApplicationException(javax.ws.rs.WebApplicationException) ParseException(org.opennms.newts.api.search.query.ParseException) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 4 with Context

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

the class CassandraCachePrimer method prime.

public void prime(ResourceMetadataCache cache, Context context) {
    BoundStatement bindStatement = m_selectAllMetricsStatement.bind();
    bindStatement.setFetchSize(m_fetchSize);
    ResultSet rs = m_session.execute(bindStatement);
    for (Row row : rs) {
        // of waiting until all rows have been processed
        if (rs.getAvailableWithoutFetching() == m_fetchMoreThreshold && !rs.isFullyFetched()) {
            // this is asynchronous
            rs.fetchMoreResults();
        }
        final Context rowContext = new Context(row.getString(Constants.Schema.C_METRICS_CONTEXT));
        if (context != null && !context.equals(rowContext)) {
            // Skip this entry, it's not in the context we're interested in
            continue;
        }
        final Resource resource = new Resource(row.getString(Constants.Schema.C_METRICS_RESOURCE));
        final ResourceMetadata resourceMetadata = new ResourceMetadata();
        // As cassandra is unable to provide remaining TTL values for tables with partition/primary keys only, we
        // hope that the cached metadata is merged with one from the `resource_attributes` table on which the TTL
        // can be determined. As a fallback, we let the cache expire immediately avoiding objects in the cache with
        // are in fact already timed out.
        resourceMetadata.setExpires(null);
        resourceMetadata.putMetric(row.getString(Constants.Schema.C_METRICS_NAME));
        cache.merge(rowContext, resource, resourceMetadata);
    }
    bindStatement = m_selectAllAttributesStatement.bind();
    bindStatement.setFetchSize(m_fetchSize);
    for (Row row : m_session.execute(bindStatement)) {
        // of waiting until all rows have been processed
        if (rs.getAvailableWithoutFetching() == m_fetchMoreThreshold && !rs.isFullyFetched()) {
            // this is asynchronous
            rs.fetchMoreResults();
        }
        final Context rowContext = new Context(row.getString(Constants.Schema.C_ATTRS_CONTEXT));
        if (context != null && !context.equals(rowContext)) {
            // Skip this entry, it's not in the context we're interested in
            continue;
        }
        final Resource resource = new Resource(row.getString(Constants.Schema.C_ATTRS_RESOURCE));
        final ResourceMetadata resourceMetadata = new ResourceMetadata();
        // Let the caches expire before the real TTL to avoid corner-cases and add some margin
        // Cassandra supports fetching of TTL values only on rows where not all columns are primary keys. Therefore
        // we assume that the TTL of entries in this table is similar to entries of other metadata tables. Setting
        // the expiration time only once will will merge the value to all other cached entries for the same resource
        resourceMetadata.setExpires(System.currentTimeMillis() + row.getInt("ttl") * 1000L * 3L / 4L);
        resourceMetadata.putAttribute(row.getString(Constants.Schema.C_ATTRS_ATTR), row.getString(Constants.Schema.C_ATTRS_VALUE));
        cache.merge(rowContext, resource, resourceMetadata);
    }
}
Also used : Context(org.opennms.newts.api.Context) ResultSet(com.datastax.driver.core.ResultSet) Resource(org.opennms.newts.api.Resource) Row(com.datastax.driver.core.Row) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 5 with Context

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

the class MeasurementsResource method getMeasurements.

@POST
@Path("/{resource}")
@Timed
public Collection<Collection<MeasurementDTO>> getMeasurements(ResultDescriptorDTO descriptorDTO, @PathParam("resource") Resource resource, @QueryParam("start") Optional<TimestampParam> start, @QueryParam("end") Optional<TimestampParam> end, @QueryParam("resolution") Optional<DurationParam> resolution, @QueryParam("context") Optional<String> contextId) {
    Optional<Timestamp> lower = Transform.toTimestamp(start);
    Optional<Timestamp> upper = Transform.toTimestamp(end);
    Optional<Duration> step = Transform.toDuration(resolution);
    Context context = contextId.isPresent() ? new Context(contextId.get()) : Context.DEFAULT_CONTEXT;
    LOG.debug("Retrieving measurements for resource {}, from {} to {} w/ resolution {} and w/ report {}", resource, lower, upper, step, descriptorDTO);
    ResultDescriptor rDescriptor = Transform.resultDescriptor(descriptorDTO);
    return Transform.measurementDTOs(m_repository.select(context, resource, lower, upper, rDescriptor, step));
}
Also used : Context(org.opennms.newts.api.Context) Duration(org.opennms.newts.api.Duration) ResultDescriptor(org.opennms.newts.api.query.ResultDescriptor) Timestamp(org.opennms.newts.api.Timestamp) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

Context (org.opennms.newts.api.Context)13 Resource (org.opennms.newts.api.Resource)8 Test (org.junit.Test)5 ResourceMetadata (org.opennms.newts.cassandra.search.ResourceMetadata)5 Timed (com.codahale.metrics.annotation.Timed)4 Path (javax.ws.rs.Path)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 BoundStatement (com.datastax.driver.core.BoundStatement)2 GET (javax.ws.rs.GET)2 Timestamp (org.opennms.newts.api.Timestamp)2 EscapableResourceIdSplitter (org.opennms.newts.cassandra.search.EscapableResourceIdSplitter)2 Gauge (com.codahale.metrics.Gauge)1 Timer (com.codahale.metrics.Timer)1 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 RegularStatement (com.datastax.driver.core.RegularStatement)1 ResultSet (com.datastax.driver.core.ResultSet)1 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)1 Row (com.datastax.driver.core.Row)1 Statement (com.datastax.driver.core.Statement)1 Joiner (com.google.common.base.Joiner)1