use of org.apache.hbase.thirdparty.io.netty.util.Timeout in project hbase by apache.
the class AsyncBufferedMutatorImpl method mutate.
@Override
public List<CompletableFuture<Void>> mutate(List<? extends Mutation> mutations) {
List<CompletableFuture<Void>> futures = Stream.<CompletableFuture<Void>>generate(CompletableFuture::new).limit(mutations.size()).collect(Collectors.toList());
long heapSize = 0;
for (Mutation mutation : mutations) {
heapSize += mutation.heapSize();
if (mutation instanceof Put) {
validatePut((Put) mutation, maxKeyValueSize);
}
}
synchronized (this) {
if (closed) {
IOException ioe = new IOException("Already closed");
futures.forEach(f -> f.completeExceptionally(ioe));
return futures;
}
if (this.mutations.isEmpty() && periodicFlushTimeoutNs > 0) {
periodicFlushTask = periodicalFlushTimer.newTimeout(timeout -> {
synchronized (AsyncBufferedMutatorImpl.this) {
// are equal.
if (timeout == periodicFlushTask) {
periodicFlushTask = null;
internalFlush();
}
}
}, periodicFlushTimeoutNs, TimeUnit.NANOSECONDS);
}
this.mutations.addAll(mutations);
this.futures.addAll(futures);
bufferedSize += heapSize;
if (bufferedSize >= writeBufferSize) {
internalFlush();
}
}
return futures;
}
use of org.apache.hbase.thirdparty.io.netty.util.Timeout in project hbase by apache.
the class RawAsyncHBaseAdmin method execProcedure.
@Override
public CompletableFuture<Void> execProcedure(String signature, String instance, Map<String, String> props) {
CompletableFuture<Void> future = new CompletableFuture<>();
ProcedureDescription procDesc = ProtobufUtil.buildProcedureDescription(signature, instance, props);
addListener(this.<Long>newMasterCaller().action((controller, stub) -> this.<ExecProcedureRequest, ExecProcedureResponse, Long>call(controller, stub, ExecProcedureRequest.newBuilder().setProcedure(procDesc).build(), (s, c, req, done) -> s.execProcedure(c, req, done), resp -> resp.getExpectedTimeout())).call(), (expectedTimeout, err) -> {
if (err != null) {
future.completeExceptionally(err);
return;
}
TimerTask pollingTask = new TimerTask() {
int tries = 0;
long startTime = EnvironmentEdgeManager.currentTime();
long endTime = startTime + expectedTimeout;
long maxPauseTime = expectedTimeout / maxAttempts;
@Override
public void run(Timeout timeout) throws Exception {
if (EnvironmentEdgeManager.currentTime() < endTime) {
addListener(isProcedureFinished(signature, instance, props), (done, err2) -> {
if (err2 != null) {
future.completeExceptionally(err2);
return;
}
if (done) {
future.complete(null);
} else {
// retry again after pauseTime.
long pauseTime = ConnectionUtils.getPauseTime(TimeUnit.NANOSECONDS.toMillis(pauseNs), ++tries);
pauseTime = Math.min(pauseTime, maxPauseTime);
AsyncConnectionImpl.RETRY_TIMER.newTimeout(this, pauseTime, TimeUnit.MICROSECONDS);
}
});
} else {
future.completeExceptionally(new IOException("Procedure '" + signature + " : " + instance + "' wasn't completed in expectedTime:" + expectedTimeout + " ms"));
}
}
};
// Queue the polling task into RETRY_TIMER to poll procedure state asynchronously.
AsyncConnectionImpl.RETRY_TIMER.newTimeout(pollingTask, 1, TimeUnit.MILLISECONDS);
});
return future;
}
use of org.apache.hbase.thirdparty.io.netty.util.Timeout in project hbase by apache.
the class RawAsyncHBaseAdmin method snapshot.
@Override
public CompletableFuture<Void> snapshot(SnapshotDescription snapshotDesc) {
SnapshotProtos.SnapshotDescription snapshot = ProtobufUtil.createHBaseProtosSnapshotDesc(snapshotDesc);
try {
ClientSnapshotDescriptionUtils.assertSnapshotRequestIsValid(snapshot);
} catch (IllegalArgumentException e) {
return failedFuture(e);
}
CompletableFuture<Void> future = new CompletableFuture<>();
final SnapshotRequest request = SnapshotRequest.newBuilder().setSnapshot(snapshot).build();
addListener(this.<Long>newMasterCaller().action((controller, stub) -> this.<SnapshotRequest, SnapshotResponse, Long>call(controller, stub, request, (s, c, req, done) -> s.snapshot(c, req, done), resp -> resp.getExpectedTimeout())).call(), (expectedTimeout, err) -> {
if (err != null) {
future.completeExceptionally(err);
return;
}
TimerTask pollingTask = new TimerTask() {
int tries = 0;
long startTime = EnvironmentEdgeManager.currentTime();
long endTime = startTime + expectedTimeout;
long maxPauseTime = expectedTimeout / maxAttempts;
@Override
public void run(Timeout timeout) throws Exception {
if (EnvironmentEdgeManager.currentTime() < endTime) {
addListener(isSnapshotFinished(snapshotDesc), (done, err2) -> {
if (err2 != null) {
future.completeExceptionally(err2);
} else if (done) {
future.complete(null);
} else {
// retry again after pauseTime.
long pauseTime = ConnectionUtils.getPauseTime(TimeUnit.NANOSECONDS.toMillis(pauseNs), ++tries);
pauseTime = Math.min(pauseTime, maxPauseTime);
AsyncConnectionImpl.RETRY_TIMER.newTimeout(this, pauseTime, TimeUnit.MILLISECONDS);
}
});
} else {
future.completeExceptionally(new SnapshotCreationException("Snapshot '" + snapshot.getName() + "' wasn't completed in expectedTime:" + expectedTimeout + " ms", snapshotDesc));
}
}
};
AsyncConnectionImpl.RETRY_TIMER.newTimeout(pollingTask, 1, TimeUnit.MILLISECONDS);
});
return future;
}
use of org.apache.hbase.thirdparty.io.netty.util.Timeout in project hbase by apache.
the class TestAsyncBufferMutator method testCancelPeriodicFlushByManuallyFlush.
@Test
public void testCancelPeriodicFlushByManuallyFlush() throws InterruptedException, ExecutionException {
try (AsyncBufferedMutatorImpl mutator = (AsyncBufferedMutatorImpl) CONN.getBufferedMutatorBuilder(TABLE_NAME).setWriteBufferPeriodicFlush(1, TimeUnit.SECONDS).build()) {
CompletableFuture<?> future = mutator.mutate(new Put(Bytes.toBytes(0)).addColumn(CF, CQ, VALUE));
Timeout task = mutator.periodicFlushTask;
// we should have scheduled a periodic flush task
assertNotNull(task);
mutator.flush();
assertTrue(task.isCancelled());
future.get();
AsyncTable<?> table = CONN.getTable(TABLE_NAME);
assertArrayEquals(VALUE, table.get(new Get(Bytes.toBytes(0))).get().getValue(CF, CQ));
}
}
use of org.apache.hbase.thirdparty.io.netty.util.Timeout in project hbase by apache.
the class TestAsyncBufferMutator method testRaceBetweenNormalFlushAndPeriodicFlush.
@Test
public void testRaceBetweenNormalFlushAndPeriodicFlush() throws InterruptedException, ExecutionException {
Put put = new Put(Bytes.toBytes(0)).addColumn(CF, CQ, VALUE);
try (AsyncBufferMutatorForTest mutator = new AsyncBufferMutatorForTest(AsyncConnectionImpl.RETRY_TIMER, CONN.getTable(TABLE_NAME), 10 * put.heapSize(), TimeUnit.MILLISECONDS.toNanos(200), 1024 * 1024)) {
CompletableFuture<?> future = mutator.mutate(put);
Timeout task = mutator.periodicFlushTask;
// we should have scheduled a periodic flush task
assertNotNull(task);
synchronized (mutator) {
// synchronized on mutator to prevent periodic flush to be executed
Thread.sleep(500);
// the timeout should be issued
assertTrue(task.isExpired());
// but no flush is issued as we hold the lock
assertEquals(0, mutator.flushCount);
assertFalse(future.isDone());
// manually flush, then release the lock
mutator.flush();
}
// this is a bit deep into the implementation in netty but anyway let's add a check here to
// confirm that an issued timeout can not be canceled by netty framework.
assertFalse(task.isCancelled());
// and the mutation is done
future.get();
AsyncTable<?> table = CONN.getTable(TABLE_NAME);
assertArrayEquals(VALUE, table.get(new Get(Bytes.toBytes(0))).get().getValue(CF, CQ));
// only the manual flush, the periodic flush should have been canceled by us
assertEquals(1, mutator.flushCount);
}
}
Aggregations