Search in sources :

Example 16 with StatementContext

use of org.skife.jdbi.v2.StatementContext in project druid by druid-io.

the class SqlSegmentsMetadataManager method retrieveUnusedSegments.

private List<DataSegment> retrieveUnusedSegments(final String dataSource, final Set<String> segmentIds, final Handle handle) throws UnknownSegmentIdsException {
    List<String> unknownSegmentIds = new ArrayList<>();
    List<DataSegment> segments = segmentIds.stream().map(segmentId -> {
        Iterator<DataSegment> segmentResultIterator = handle.createQuery(StringUtils.format("SELECT used, payload FROM %1$s WHERE dataSource = :dataSource AND id = :id", getSegmentsTable())).bind("dataSource", dataSource).bind("id", segmentId).map((int index, ResultSet resultSet, StatementContext context) -> {
            try {
                if (!resultSet.getBoolean("used")) {
                    return jsonMapper.readValue(resultSet.getBytes("payload"), DataSegment.class);
                } else {
                    // We emit nulls for used segments. They are filtered out below in this method.
                    return null;
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).iterator();
        if (!segmentResultIterator.hasNext()) {
            unknownSegmentIds.add(segmentId);
            return null;
        } else {
            @Nullable DataSegment segment = segmentResultIterator.next();
            if (segmentResultIterator.hasNext()) {
                log.error("There is more than one row corresponding to segment id [%s] in data source [%s] in the database", segmentId, dataSource);
            }
            return segment;
        }
    }).filter(// Filter nulls corresponding to used segments.
    Objects::nonNull).collect(Collectors.toList());
    if (!unknownSegmentIds.isEmpty()) {
        throw new UnknownSegmentIdsException(unknownSegmentIds);
    }
    return segments;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) DataSegment(org.apache.druid.timeline.DataSegment) StatementContext(org.skife.jdbi.v2.StatementContext) ResultSet(java.sql.ResultSet) Nullable(javax.annotation.Nullable)

Example 17 with StatementContext

use of org.skife.jdbi.v2.StatementContext in project metrics by dropwizard.

the class InstrumentedTimingCollectorTest method updatesTimerForSqlObjectsWithoutMethod.

@Test
public void updatesTimerForSqlObjectsWithoutMethod() throws Exception {
    final StatementNameStrategy strategy = new SmartNameStrategy();
    final InstrumentedTimingCollector collector = new InstrumentedTimingCollector(registry, strategy);
    final StatementContext ctx = mock(StatementContext.class);
    doReturn("SELECT 1").when(ctx).getRawSql();
    doReturn(getClass()).when(ctx).getSqlObjectType();
    collector.collect(TimeUnit.SECONDS.toNanos(1), ctx);
    final String name = strategy.getStatementName(ctx);
    final Timer timer = registry.timer(name);
    assertThat(name).isEqualTo(name(getClass(), "SELECT 1"));
    assertThat(timer.getSnapshot().getMax()).isEqualTo(1000000000);
}
Also used : StatementNameStrategy(com.codahale.metrics.jdbi.strategies.StatementNameStrategy) Timer(com.codahale.metrics.Timer) SmartNameStrategy(com.codahale.metrics.jdbi.strategies.SmartNameStrategy) StatementContext(org.skife.jdbi.v2.StatementContext) Test(org.junit.Test)

Example 18 with StatementContext

use of org.skife.jdbi.v2.StatementContext in project metrics by dropwizard.

the class InstrumentedTimingCollectorTest method updatesTimerForContextGroupTypeAndName.

@Test
public void updatesTimerForContextGroupTypeAndName() throws Exception {
    final StatementNameStrategy strategy = new SmartNameStrategy();
    final InstrumentedTimingCollector collector = new InstrumentedTimingCollector(registry, strategy);
    final StatementContext ctx = mock(StatementContext.class);
    doReturn("SELECT 1").when(ctx).getRawSql();
    doReturn("my-group").when(ctx).getAttribute(NameStrategies.STATEMENT_GROUP);
    doReturn("my-type").when(ctx).getAttribute(NameStrategies.STATEMENT_TYPE);
    doReturn("updatesTimerForContextGroupTypeAndName").when(ctx).getAttribute(NameStrategies.STATEMENT_NAME);
    collector.collect(TimeUnit.SECONDS.toNanos(5), ctx);
    final String name = strategy.getStatementName(ctx);
    final Timer timer = registry.timer(name);
    assertThat(name).isEqualTo(name("my-group", "my-type", "updatesTimerForContextGroupTypeAndName"));
    assertThat(timer.getSnapshot().getMax()).isEqualTo(5000000000L);
}
Also used : StatementNameStrategy(com.codahale.metrics.jdbi.strategies.StatementNameStrategy) Timer(com.codahale.metrics.Timer) SmartNameStrategy(com.codahale.metrics.jdbi.strategies.SmartNameStrategy) StatementContext(org.skife.jdbi.v2.StatementContext) Test(org.junit.Test)

Example 19 with StatementContext

use of org.skife.jdbi.v2.StatementContext in project metrics by dropwizard.

the class InstrumentedTimingCollectorTest method updatesTimerForRawSql.

@Test
public void updatesTimerForRawSql() throws Exception {
    final StatementNameStrategy strategy = new SmartNameStrategy();
    final InstrumentedTimingCollector collector = new InstrumentedTimingCollector(registry, strategy);
    final StatementContext ctx = mock(StatementContext.class);
    doReturn("SELECT 1").when(ctx).getRawSql();
    collector.collect(TimeUnit.SECONDS.toNanos(2), ctx);
    final String name = strategy.getStatementName(ctx);
    final Timer timer = registry.timer(name);
    assertThat(name).isEqualTo(name("sql", "raw", "SELECT 1"));
    assertThat(timer.getSnapshot().getMax()).isEqualTo(2000000000);
}
Also used : StatementNameStrategy(com.codahale.metrics.jdbi.strategies.StatementNameStrategy) Timer(com.codahale.metrics.Timer) SmartNameStrategy(com.codahale.metrics.jdbi.strategies.SmartNameStrategy) StatementContext(org.skife.jdbi.v2.StatementContext) Test(org.junit.Test)

Example 20 with StatementContext

use of org.skife.jdbi.v2.StatementContext in project metrics by dropwizard.

the class InstrumentedTimingCollectorTest method updatesTimerForNonSqlishRawSql.

@Test
public void updatesTimerForNonSqlishRawSql() throws Exception {
    final StatementNameStrategy strategy = new SmartNameStrategy();
    final InstrumentedTimingCollector collector = new InstrumentedTimingCollector(registry, strategy);
    final StatementContext ctx = mock(StatementContext.class);
    doReturn("don't know what it is but it's not SQL").when(ctx).getRawSql();
    collector.collect(TimeUnit.SECONDS.toNanos(3), ctx);
    final String name = strategy.getStatementName(ctx);
    final Timer timer = registry.timer(name);
    assertThat(name).isEqualTo(name("sql", "raw", "don't know what it is but it's not SQL"));
    assertThat(timer.getSnapshot().getMax()).isEqualTo(3000000000L);
}
Also used : StatementNameStrategy(com.codahale.metrics.jdbi.strategies.StatementNameStrategy) Timer(com.codahale.metrics.Timer) SmartNameStrategy(com.codahale.metrics.jdbi.strategies.SmartNameStrategy) StatementContext(org.skife.jdbi.v2.StatementContext) Test(org.junit.Test)

Aggregations

StatementContext (org.skife.jdbi.v2.StatementContext)20 Test (org.junit.Test)13 Timer (com.codahale.metrics.Timer)11 StatementNameStrategy (com.codahale.metrics.jdbi.strategies.StatementNameStrategy)11 SmartNameStrategy (com.codahale.metrics.jdbi.strategies.SmartNameStrategy)9 ResultSet (java.sql.ResultSet)8 SQLException (java.sql.SQLException)5 Handle (org.skife.jdbi.v2.Handle)5 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 DataSegment (org.apache.druid.timeline.DataSegment)3 Interval (org.joda.time.Interval)3 ShortNameStrategy (com.codahale.metrics.jdbi.strategies.ShortNameStrategy)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DataSegment (io.druid.timeline.DataSegment)2 HashSet (java.util.HashSet)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Nullable (javax.annotation.Nullable)2 TransactionStatus (org.skife.jdbi.v2.TransactionStatus)2