public void delete(
      final TransactionState transactionState, final Delete delete, final boolean bool_addLocation)
      throws IOException {
    SingleVersionDeleteNotSupported.validateDelete(delete);
    if (bool_addLocation) addLocation(transactionState, super.getRegionLocation(delete.getRow()));
    final String regionName =
        super.getRegionLocation(delete.getRow()).getRegionInfo().getRegionNameAsString();

    Batch.Call<TrxRegionService, DeleteTransactionalResponse> callable =
        new Batch.Call<TrxRegionService, DeleteTransactionalResponse>() {
          ServerRpcController controller = new ServerRpcController();
          BlockingRpcCallback<DeleteTransactionalResponse> rpcCallback =
              new BlockingRpcCallback<DeleteTransactionalResponse>();

          @Override
          public DeleteTransactionalResponse call(TrxRegionService instance) throws IOException {
            org.apache.hadoop.hbase.coprocessor.transactional.generated.TrxRegionProtos
                    .DeleteTransactionalRequest.Builder
                builder = DeleteTransactionalRequest.newBuilder();
            builder.setTransactionId(transactionState.getTransactionId());
            builder.setRegionName(ByteString.copyFromUtf8(regionName));

            MutationProto m1 = ProtobufUtil.toMutation(MutationType.DELETE, delete);
            builder.setDelete(m1);

            instance.delete(controller, builder.build(), rpcCallback);
            return rpcCallback.get();
          }
        };

    byte[] row = delete.getRow();
    DeleteTransactionalResponse result = null;
    try {
      int retryCount = 0;
      boolean retry = false;
      do {
        Iterator<Map.Entry<byte[], DeleteTransactionalResponse>> it =
            super.coprocessorService(TrxRegionService.class, row, row, callable)
                .entrySet()
                .iterator();
        if (it.hasNext()) {
          result = it.next().getValue();
          retry = false;
        }

        if (result == null || result.getException().contains("closing region")) {
          Thread.sleep(TransactionalTable.delay);
          retry = true;
          transactionState.setRetried(true);
          retryCount++;
        }
      } while (retryCount < TransactionalTable.retries && retry == true);
    } catch (Throwable t) {
      if (LOG.isErrorEnabled()) LOG.error("ERROR while calling delete ", t);
      throw new IOException("ERROR while calling coprocessor ", t);
    }
    if (result == null) throw new IOException(retryErrMsg);
    else if (result.hasException()) throw new IOException(result.getException());
  }
Exemple #2
0
  /**
   * Transactional version of {@link HTable#delete(Delete)}
   *
   * @param transactionState Identifier of the transaction
   * @see HTable#delete(Delete)
   * @throws IOException
   */
  public void delete(TransactionState transactionState, Delete delete) throws IOException {
    final long startTimestamp = transactionState.getStartTimestamp();
    boolean issueGet = false;

    final Put deleteP = new Put(delete.getRow(), startTimestamp);
    final Get deleteG = new Get(delete.getRow());
    Map<byte[], List<KeyValue>> fmap = delete.getFamilyMap();
    if (fmap.isEmpty()) {
      issueGet = true;
    }
    for (List<KeyValue> kvl : fmap.values()) {
      for (KeyValue kv : kvl) {
        switch (KeyValue.Type.codeToType(kv.getType())) {
          case DeleteColumn:
            deleteP.add(kv.getFamily(), kv.getQualifier(), startTimestamp, null);
            break;
          case DeleteFamily:
            deleteG.addFamily(kv.getFamily());
            issueGet = true;
            break;
          case Delete:
            if (kv.getTimestamp() == HConstants.LATEST_TIMESTAMP) {
              deleteP.add(kv.getFamily(), kv.getQualifier(), startTimestamp, null);
              break;
            } else {
              throw new UnsupportedOperationException(
                  "Cannot delete specific versions on Snapshot Isolation.");
            }
        }
      }
    }
    if (issueGet) {
      Result result = this.get(deleteG);
      for (Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> entryF :
          result.getMap().entrySet()) {
        byte[] family = entryF.getKey();
        for (Entry<byte[], NavigableMap<Long, byte[]>> entryQ : entryF.getValue().entrySet()) {
          byte[] qualifier = entryQ.getKey();
          deleteP.add(family, qualifier, null);
        }
      }
    }

    transactionState.addRow(
        new RowKeyFamily(delete.getRow(), getTableName(), deleteP.getFamilyMap()));

    put(deleteP);
  }
    @Override
    public Object terminatePartial(AggregationBuffer agg) throws HiveException {
      DeleteBuffer myagg = (DeleteBuffer) agg;

      ArrayList<List<String>> ret = new ArrayList<List<String>>();
      ArrayList tname = new ArrayList<String>();
      tname.add(configMap.get(HTableFactory.TABLE_NAME_TAG));
      tname.add(configMap.get(HTableFactory.ZOOKEEPER_QUORUM_TAG));

      for (Map.Entry<String, String> entry : configMap.entrySet()) {
        if (!entry.getKey().equals(HTableFactory.TABLE_NAME_TAG)
            && !entry.getKey().equals(HTableFactory.ZOOKEEPER_QUORUM_TAG)) {

          tname.add(entry.getKey());
        }
      }
      ret.add(tname);

      for (Delete theDelete : myagg.deleteList) {
        ArrayList<String> kvList = new ArrayList<String>();
        kvList.add(new String(theDelete.getRow()));
        ret.add(kvList);
      }

      return ret;
    }
Exemple #4
0
 /** @param d Delete to clone. */
 public Delete(final Delete d) {
   this.row = d.getRow();
   this.ts = d.getTimeStamp();
   this.familyMap.putAll(d.getFamilyCellMap());
   this.durability = d.durability;
   for (Map.Entry<String, byte[]> entry : d.getAttributesMap().entrySet()) {
     this.setAttribute(entry.getKey(), entry.getValue());
   }
 }
Exemple #5
0
 /** @param d Delete to clone. */
 public Delete(final Delete d) {
   this.row = d.getRow();
   this.ts = d.getTimeStamp();
   this.lockId = d.getLockId();
   this.familyMap.putAll(d.getFamilyMap());
 }
 /**
  * See safeQueueUpdate(RowMutations mutate)
  *
  * @param delete Row mutation to perform on the queue
  */
 private void safeQueueUpdate(Delete delete) throws ReplicationException, IOException {
   RowMutations mutations = new RowMutations(delete.getRow());
   mutations.add(delete);
   safeQueueUpdate(mutations);
 }
  /**
   * Looking forward to TransactionalRegion-side implementation
   *
   * @param transactionState
   * @param deletes
   * @throws IOException
   */
  public void delete(final TransactionState transactionState, List<Delete> deletes)
      throws IOException {
    long transactionId = transactionState.getTransactionId();
    if (LOG.isTraceEnabled())
      LOG.trace(
          "Enter TransactionalTable.delete[] <List> size: "
              + deletes.size()
              + ", transid: "
              + transactionId);
    // collect all rows from same region
    final Map<TransactionRegionLocation, List<Delete>> rows =
        new HashMap<TransactionRegionLocation, List<Delete>>();
    HRegionLocation hlocation = null;
    TransactionRegionLocation location = null;
    List<Delete> list = null;
    int size = 0;
    for (Delete del : deletes) {
      hlocation = this.getRegionLocation(del.getRow(), false);
      location =
          new TransactionRegionLocation(hlocation.getRegionInfo(), hlocation.getServerName());
      if (LOG.isTraceEnabled())
        LOG.trace(
            "delete <List> with trRegion ["
                + location.getRegionInfo().getEncodedName()
                + "], endKey: "
                + Hex.encodeHexString(location.getRegionInfo().getEndKey())
                + " and transaction ["
                + transactionId
                + "], delete number: "
                + size);
      if (!rows.containsKey(location)) {
        if (LOG.isTraceEnabled())
          LOG.trace(
              "delete adding new <List> for region ["
                  + location.getRegionInfo().getRegionNameAsString()
                  + "], endKey: "
                  + Hex.encodeHexString(location.getRegionInfo().getEndKey())
                  + " and transaction ["
                  + transactionId
                  + "], delete number: "
                  + size);
        list = new ArrayList<Delete>();
        rows.put(location, list);
      } else {
        list = rows.get(location);
      }
      list.add(del);
      size++;
    }

    final List<Delete> rowsInSameRegion = new ArrayList<Delete>();
    for (Map.Entry<TransactionRegionLocation, List<Delete>> entry : rows.entrySet()) {
      rowsInSameRegion.clear();
      rowsInSameRegion.addAll(entry.getValue());
      final String regionName = entry.getKey().getRegionInfo().getRegionNameAsString();

      Batch.Call<TrxRegionService, DeleteMultipleTransactionalResponse> callable =
          new Batch.Call<TrxRegionService, DeleteMultipleTransactionalResponse>() {
            ServerRpcController controller = new ServerRpcController();
            BlockingRpcCallback<DeleteMultipleTransactionalResponse> rpcCallback =
                new BlockingRpcCallback<DeleteMultipleTransactionalResponse>();

            @Override
            public DeleteMultipleTransactionalResponse call(TrxRegionService instance)
                throws IOException {
              org.apache.hadoop.hbase.coprocessor.transactional.generated.TrxRegionProtos
                      .DeleteMultipleTransactionalRequest.Builder
                  builder = DeleteMultipleTransactionalRequest.newBuilder();
              builder.setTransactionId(transactionState.getTransactionId());
              builder.setRegionName(ByteString.copyFromUtf8(regionName));

              for (Delete delete : rowsInSameRegion) {
                MutationProto m1 = ProtobufUtil.toMutation(MutationType.DELETE, delete);
                builder.addDelete(m1);
              }

              instance.deleteMultiple(controller, builder.build(), rpcCallback);
              return rpcCallback.get();
            }
          };

      DeleteMultipleTransactionalResponse result = null;
      try {
        int retryCount = 0;
        boolean retry = false;
        do {
          Iterator<Map.Entry<byte[], DeleteMultipleTransactionalResponse>> it =
              super.coprocessorService(
                      TrxRegionService.class,
                      entry.getValue().get(0).getRow(),
                      entry.getValue().get(0).getRow(),
                      callable)
                  .entrySet()
                  .iterator();
          if (it.hasNext()) {
            result = it.next().getValue();
            retry = false;
          }

          if (result == null || result.getException().contains("closing region")) {
            Thread.sleep(TransactionalTable.delay);
            retry = true;
            transactionState.setRetried(true);
            retryCount++;
          }
        } while (retryCount < TransactionalTable.retries && retry == true);

      } catch (Throwable e) {
        if (LOG.isErrorEnabled()) LOG.error("ERROR while calling deleteMultipleTransactional ", e);
        throw new IOException("ERROR while calling deleteMultipleTransactional", e);
      }

      if (result == null) throw new IOException(retryErrMsg);
      else if (result.hasException()) throw new IOException(result.getException());
    }
  }
  public boolean checkAndDelete(
      final TransactionState transactionState,
      final byte[] row,
      final byte[] family,
      final byte[] qualifier,
      final byte[] value,
      final Delete delete)
      throws IOException {
    if (LOG.isTraceEnabled())
      LOG.trace(
          "Enter TransactionalTable.checkAndDelete row: "
              + row
              + " family: "
              + family
              + " qualifier: "
              + qualifier
              + " value: "
              + value);
    if (!Bytes.equals(row, delete.getRow())) {
      throw new IOException("Action's getRow must match the passed row");
    }
    final String regionName =
        super.getRegionLocation(delete.getRow()).getRegionInfo().getRegionNameAsString();
    if (regionName == null) throw new IOException("Null regionName");
    Batch.Call<TrxRegionService, CheckAndDeleteResponse> callable =
        new Batch.Call<TrxRegionService, CheckAndDeleteResponse>() {
          ServerRpcController controller = new ServerRpcController();
          BlockingRpcCallback<CheckAndDeleteResponse> rpcCallback =
              new BlockingRpcCallback<CheckAndDeleteResponse>();

          @Override
          public CheckAndDeleteResponse call(TrxRegionService instance) throws IOException {
            org.apache.hadoop.hbase.coprocessor.transactional.generated.TrxRegionProtos
                    .CheckAndDeleteRequest.Builder
                builder = CheckAndDeleteRequest.newBuilder();
            builder.setTransactionId(transactionState.getTransactionId());
            builder.setRegionName(ByteString.copyFromUtf8(regionName));
            builder.setRow(HBaseZeroCopyByteString.wrap(row));
            if (family != null) builder.setFamily(HBaseZeroCopyByteString.wrap(family));
            else builder.setFamily(HBaseZeroCopyByteString.wrap(new byte[] {}));

            if (qualifier != null) builder.setQualifier(HBaseZeroCopyByteString.wrap(qualifier));
            else builder.setQualifier(HBaseZeroCopyByteString.wrap(new byte[] {}));
            if (value != null) builder.setValue(HBaseZeroCopyByteString.wrap(value));
            else builder.setValue(HBaseZeroCopyByteString.wrap(new byte[] {}));

            MutationProto m1 = ProtobufUtil.toMutation(MutationType.DELETE, delete);
            builder.setDelete(m1);

            instance.checkAndDelete(controller, builder.build(), rpcCallback);
            return rpcCallback.get();
          }
        };

    CheckAndDeleteResponse result = null;
    try {
      int retryCount = 0;
      boolean retry = false;
      do {
        Iterator<Map.Entry<byte[], CheckAndDeleteResponse>> it =
            super.coprocessorService(
                    TrxRegionService.class, delete.getRow(), delete.getRow(), callable)
                .entrySet()
                .iterator();
        if (it.hasNext()) {
          result = it.next().getValue();
          retry = false;
        }

        if (result == null || result.getException().contains("closing region")) {
          Thread.sleep(TransactionalTable.delay);
          retry = true;
          transactionState.setRetried(true);
          retryCount++;
        }
      } while (retryCount < TransactionalTable.retries && retry == true);
    } catch (Throwable e) {
      if (LOG.isErrorEnabled()) LOG.error("ERROR while calling checkAndDelete ", e);
      throw new IOException("ERROR while calling checkAndDelete ", e);
    }
    if (result == null) throw new IOException(retryErrMsg);
    else if (result.hasException()) throw new IOException(result.getException());
    return result.getResult();
  }