Search in sources :

Example 6 with TimerContext

use of com.yammer.metrics.core.TimerContext in project bagheera by mozilla-metrics.

the class FlushResult method flush.

public void flush() throws IOException {
    IOException lastException = null;
    this.currentTimeMillis = System.currentTimeMillis();
    int i;
    for (i = 0; i < getRetryCount(); i++) {
        HTableInterface table = hbasePool.getTable(tableName);
        try {
            table.setAutoFlush(false);
            final TimerContext flushTimerContext = flushTimer.time();
            try {
                List<Row> rows = new ArrayList<Row>(batchSize);
                while (!rowQueue.isEmpty() && rows.size() < batchSize) {
                    Row row = rowQueue.poll();
                    if (row != null) {
                        rows.add(row);
                        rowQueueSize.decrementAndGet();
                    }
                }
                try {
                    FlushResult result = flushTable(table, rows);
                    stored.mark(result.successfulPutCount);
                    storeFailed.mark(result.failedPutCount);
                    deleted.mark(result.successfulDeleteCount);
                    deleteFailed.mark(result.failedDeleteCount);
                } catch (InterruptedException e) {
                    LOG.error("Error flushing batch of " + batchSize + " messages", e);
                }
            } finally {
                flushTimerContext.stop();
                if (table != null) {
                    table.close();
                }
            }
            break;
        } catch (IOException e) {
            LOG.warn(String.format("Error in flush attempt %d of %d, clearing Region cache", (i + 1), getRetryCount()), e);
            lastException = e;
            // connection.clearRegionCache();
            try {
                Thread.sleep(getRetrySleepSeconds() * 1000);
            } catch (InterruptedException e1) {
                // wake up
                LOG.info("woke up by interruption", e1);
            }
        }
    }
    if (i >= getRetryCount() && lastException != null) {
        LOG.error("Error in final flush attempt, giving up.");
        throw lastException;
    }
    LOG.debug("Flush finished");
}
Also used : TimerContext(com.yammer.metrics.core.TimerContext) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Row(org.apache.hadoop.hbase.client.Row) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface)

Example 7 with TimerContext

use of com.yammer.metrics.core.TimerContext in project bagheera by mozilla-metrics.

the class FlushResult method flushTable.

private FlushResult flushTable(HTableInterface table, List<Row> puts) throws IOException, InterruptedException {
    List<Row> currentAttempt = puts;
    Object[] batch = null;
    FlushResult result = null;
    int successfulPuts = 0;
    int successfulDeletes = 0;
    TimerContext htableTimerContext = htableTimer.time();
    try {
        for (int attempt = 0; attempt < retryCount; attempt++) {
            // TODO: wrap each attempt in a try/catch?
            batch = table.batch(currentAttempt);
            table.flushCommits();
            List<Row> fails = new ArrayList<Row>(currentAttempt.size());
            if (batch != null) {
                for (int i = 0; i < batch.length; i++) {
                    if (batch[i] == null) {
                        fails.add(currentAttempt.get(i));
                    } else {
                        // figure out what type it was
                        Row row = currentAttempt.get(i);
                        if (row instanceof Delete) {
                            successfulDeletes++;
                        } else if (row instanceof Put) {
                            successfulPuts++;
                        } else {
                            LOG.warn("We succeeded in flushing something that's neither a Delete nor a Put");
                        }
                    }
                }
                currentAttempt = fails;
                if (currentAttempt.isEmpty()) {
                    break;
                }
            } else {
                // something badly broke, retry the whole list.
                LOG.error("Result of table.batch() was null");
            }
        }
        int failedPuts = 0;
        int failedDeletes = 0;
        if (!currentAttempt.isEmpty()) {
            for (Row row : currentAttempt) {
                if (row instanceof Delete) {
                    failedDeletes++;
                } else if (row instanceof Put) {
                    failedPuts++;
                } else {
                    LOG.error("We failed to flush something that's neither a Delete nor a Put");
                }
            }
        }
        result = new FlushResult(failedPuts, failedDeletes, successfulPuts, successfulDeletes);
    } finally {
        htableTimerContext.stop();
    }
    return result;
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) TimerContext(com.yammer.metrics.core.TimerContext) ArrayList(java.util.ArrayList) Row(org.apache.hadoop.hbase.client.Row) Put(org.apache.hadoop.hbase.client.Put)

Aggregations

TimerContext (com.yammer.metrics.core.TimerContext)7 BufferedImage (java.awt.image.BufferedImage)4 ArrayList (java.util.ArrayList)2 Row (org.apache.hadoop.hbase.client.Row)2 Result (com.google.caliper.Result)1 Run (com.google.caliper.Run)1 ScenarioResult (com.google.caliper.ScenarioResult)1 CacheControl (com.yammer.dropwizard.jersey.caching.CacheControl)1 Timed (com.yammer.metrics.annotation.Timed)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ImageWriter (javax.imageio.ImageWriter)1 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)1 Delete (org.apache.hadoop.hbase.client.Delete)1 HTableInterface (org.apache.hadoop.hbase.client.HTableInterface)1 Put (org.apache.hadoop.hbase.client.Put)1