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");
}
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;
}
Aggregations