@Override public Status delete(String table, String key) { if (debug) { System.out.println("Doing delete for key: " + key); } setTable(table); final MutateRowRequest.Builder rowMutation = MutateRowRequest.newBuilder() .setRowKey(ByteString.copyFromUtf8(key)) .setTableNameBytes(ByteStringer.wrap(lastTableBytes)); rowMutation.addMutationsBuilder().setDeleteFromRow( DeleteFromRow.getDefaultInstance()); try { if (clientSideBuffering) { bulkMutation.add(rowMutation.build()); } else { client.mutateRow(rowMutation.build()); } return Status.OK; } catch (RuntimeException e) { System.err.println("Failed to delete key: " + key + " " + e.getMessage()); return Status.ERROR; } }
/** * Little helper to set the table byte array. If it's different than the last * table we reset the byte array. Otherwise we just use the existing array. * @param table The table we're operating against */ private void setTable(final String table) { if (!lastTable.equals(table)) { lastTable = table; BigtableTableName tableName = options .getInstanceName() .toTableName(table); lastTableBytes = tableName .toString() .getBytes(); synchronized(this) { if (bulkMutation != null) { try { bulkMutation.flush(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } } bulkMutation = session.createBulkMutation(tableName); } } }
/** * This test ensures that protobuf creation and interactions with {@link BulkMutation} work as * expected. * * @throws IOException * @throws InterruptedException */ @Test public void testWrite() throws IOException, InterruptedException { BigtableService.Writer underTest = new BigtableServiceImpl.BigtableWriterImpl(mockSession, TABLE_NAME); Mutation mutation = Mutation.newBuilder() .setSetCell(SetCell.newBuilder().setFamilyName("Family").build()) .build(); ByteString key = ByteString.copyFromUtf8("key"); SettableFuture<MutateRowResponse> fakeResponse = SettableFuture.create(); when(mockBulkMutation.add(any(MutateRowsRequest.Entry.class))).thenReturn(fakeResponse); underTest.writeRecord(KV.of(key, ImmutableList.of(mutation))); Entry expected = MutateRowsRequest.Entry.newBuilder().setRowKey(key).addMutations(mutation).build(); verify(mockBulkMutation, times(1)).add(expected); underTest.close(); verify(mockBulkMutation, times(1)).flush(); } }
@Override public void run() { synchronized (BulkMutation.this) { scheduledFlush = null; sendUnsent(); } } }, autoflushMs, TimeUnit.MILLISECONDS);
/** * <p>hasInflightRequests.</p> * * @return a boolean. */ public boolean hasInflightRequests() { return this.asyncExecutor.hasInflightRequests() || (bulkMutation != null && !bulkMutation.isFlushed()); } }
/** * <p>createBulkMutation.</p> * * @param tableName a {@link com.google.cloud.bigtable.grpc.BigtableTableName} object. * @return a {@link com.google.cloud.bigtable.grpc.async.BulkMutation} object. */ public BulkMutation createBulkMutation(BigtableTableName tableName) { return new BulkMutation( tableName, throttlingDataClient, BigtableSessionSharedThreadPools.getInstance().getRetryExecutor(), options.getBulkOptions()); }
public void sendUnsent() { if (bulkMutation != null) { bulkMutation.sendUnsent(); } }
/** * <p>hasInflightRequests.</p> * * @return a boolean. */ public boolean hasInflightRequests() { return this.asyncExecutor.hasInflightRequests() || (bulkMutation != null && !bulkMutation.isFlushed()); } }
bulkMutation.add(rowMutation.build()); } else { client.mutateRow(rowMutation.build());
@Override public void cleanup() throws DBException { if (bulkMutation != null) { try { bulkMutation.flush(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new DBException(e); } catch(RuntimeException e){ throw new DBException(e); } } synchronized (CONFIG) { --threadCount; if (threadCount <= 0) { try { session.close(); } catch (IOException e) { throw new DBException(e); } } } }
public void sendUnsent() { if (bulkMutation != null) { bulkMutation.sendUnsent(); } }
private ListenableFuture<?> issueAsyncRequest(BulkOperation bulkOperation, Row row) { try { if (row instanceof Get) { return bulkOperation.bulkRead.add(requestAdapter.adapt((Get) row)); } else if (row instanceof Put) { return bulkOperation.bulkMutation.add(requestAdapter.adaptEntry((Put) row)); } else if (row instanceof Delete) { return bulkOperation.bulkMutation.add(requestAdapter.adaptEntry((Delete) row)); } else if (row instanceof Append) { return asyncExecutor.readModifyWriteRowAsync(requestAdapter.adapt((Append) row)); } else if (row instanceof Increment) { return asyncExecutor.readModifyWriteRowAsync(requestAdapter.adapt((Increment) row)); } else if (row instanceof RowMutations) { return bulkOperation.bulkMutation.add(requestAdapter.adaptEntry((RowMutations) row)); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); return Futures.immediateFailedFuture(new IOException("Could not process the batch due to interrupt", e)); } catch (Throwable e) { return Futures.immediateFailedFuture(new IOException("Could not process the batch", e)); } LOG.error("Encountered unknown action type %s", row.getClass()); return Futures.immediateFailedFuture( new IllegalArgumentException("Encountered unknown action type: " + row.getClass())); }
@Override public void flush() throws IOException { if (bulkMutation != null) { try { bulkMutation.flush(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // We fail since flush() operation was interrupted. throw new IOException(e); } } }
protected void flush() { // If there is a bulk mutation in progress, then send it. bulkMutation.sendUnsent(); bulkRead.flush(); } }
private ListenableFuture<?> issueAsyncRequest(BulkOperation bulkOperation, Row row) { try { if (row instanceof Get) { return bulkOperation.bulkRead.add(requestAdapter.adapt((Get) row)); } else if (row instanceof Put) { return bulkOperation.bulkMutation.add(requestAdapter.adaptEntry((Put) row)); } else if (row instanceof Delete) { return bulkOperation.bulkMutation.add(requestAdapter.adaptEntry((Delete) row)); } else if (row instanceof Append) { return asyncExecutor.readModifyWriteRowAsync(requestAdapter.adapt((Append) row)); } else if (row instanceof Increment) { return asyncExecutor.readModifyWriteRowAsync(requestAdapter.adapt((Increment) row)); } else if (row instanceof RowMutations) { return bulkOperation.bulkMutation.add(requestAdapter.adaptEntry((RowMutations) row)); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); return Futures.immediateFailedFuture(new IOException("Could not process the batch due to interrupt", e)); } catch (Throwable e) { return Futures.immediateFailedFuture(new IOException("Could not process the batch", e)); } LOG.error("Encountered unknown action type %s", row.getClass()); return Futures.immediateFailedFuture( new IllegalArgumentException("Encountered unknown action type: " + row.getClass())); }
public void flush() throws IOException { // If there is a bulk mutation in progress, then send it. if (bulkMutation != null) { try { bulkMutation.flush(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException("flush() was interrupted", e); } } asyncExecutor.flush(); }
/** * Send any outstanding {@link MutateRowRequest}s and wait until all requests are complete. */ public void flush() throws InterruptedException { sendUnsent(); operationAccountant.awaitCompletion(); }
new IllegalArgumentException("Cannot perform a mutation on a null object.")); } else if (mutation instanceof Put) { future = bulkMutation.add(adapter.adaptEntry((Put) mutation)); } else if (mutation instanceof Delete) { future = bulkMutation.add(adapter.adaptEntry((Delete) mutation)); } else if (mutation instanceof Increment) { future = asyncExecutor.readModifyWriteRowAsync(adapter.adapt((Increment) mutation));
public void flush() throws IOException { // If there is a bulk mutation in progress, then send it. if (bulkMutation != null) { try { bulkMutation.flush(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException("flush() was interrupted", e); } } asyncExecutor.flush(); }