org.apache.hadoop.hbase.client.Mutation Java Examples

The following examples show how to use org.apache.hadoop.hbase.client.Mutation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StatisticsCollector.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private void writeStatsToStatsTable(final HRegion region,
        boolean delete, List<Mutation> mutations, long currentTime) throws IOException {
    try {
        // update the statistics table
        for (ImmutableBytesPtr fam : guidePostsMap.keySet()) {
            if (delete) {
                if(logger.isDebugEnabled()) {
                    logger.debug("Deleting the stats for the region "+region.getRegionInfo());
                }
                statsTable.deleteStats(region.getRegionInfo().getRegionName(), this, fam,
                        mutations);
            }
            if(logger.isDebugEnabled()) {
                logger.debug("Adding new stats for the region "+region.getRegionInfo());
            }
            statsTable.addStats((region.getRegionInfo().getRegionName()), this, fam,
                    mutations);
        }
    } catch (IOException e) {
        logger.error("Failed to update statistics table!", e);
        throw e;
    }
}
 
Example #2
Source File: Indexer.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public void preWALRestore(ObserverContext<RegionCoprocessorEnvironment> env, HRegionInfo info,
    HLogKey logKey, WALEdit logEdit) throws IOException {
    if (this.disabled) {
        super.preWALRestore(env, info, logKey, logEdit);
        return;
      }
  // TODO check the regions in transition. If the server on which the region lives is this one,
  // then we should rety that write later in postOpen.
  // we might be able to get even smarter here and pre-split the edits that are server-local
  // into their own recovered.edits file. This then lets us do a straightforward recovery of each
  // region (and more efficiently as we aren't writing quite as hectically from this one place).

  /*
   * Basically, we let the index regions recover for a little while long before retrying in the
   * hopes they come up before the primary table finishes.
   */
  Collection<Pair<Mutation, byte[]>> indexUpdates = extractIndexUpdate(logEdit);
  recoveryWriter.write(indexUpdates);
}
 
Example #3
Source File: HBaseSyncPostCommitter.java    From phoenix-omid with Apache License 2.0 6 votes vote down vote up
private void addShadowCell(HBaseCellId cell, HBaseTransaction tx, SettableFuture<Void> updateSCFuture,
                           Map<TableName,List<Mutation>> mutations) throws IOException, InterruptedException {
    Put put = new Put(cell.getRow());
    put.addColumn(cell.getFamily(),
            CellUtils.addShadowCellSuffixPrefix(cell.getQualifier(), 0, cell.getQualifier().length),
            cell.getTimestamp(),
            Bytes.toBytes(tx.getCommitTimestamp()));

    TableName table = cell.getTable().getHTable().getName();
    List<Mutation> tableMutations = mutations.get(table);
    if (tableMutations == null) {
        ArrayList<Mutation> newList = new ArrayList<>();
        newList.add(put);
        mutations.put(table, newList);
    } else {
        tableMutations.add(put);
        if (tableMutations.size() > MAX_BATCH_SIZE) {
            flushMutations(table, tableMutations);
            mutations.remove(table);
        }
    }
}
 
Example #4
Source File: ConnectionQueryServicesImpl.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public MetaDataMutationResult createTable(final List<Mutation> tableMetaData, byte[] tableName, PTableType tableType,
        Map<String,Object> tableProps, final List<Pair<byte[],Map<String,Object>>> families, byte[][] splits) throws SQLException {
    byte[][] rowKeyMetadata = new byte[3][];
    Mutation m = tableMetaData.get(0);
    byte[] key = m.getRow();
    SchemaUtil.getVarChars(key, rowKeyMetadata);
    byte[] tenantIdBytes = rowKeyMetadata[PhoenixDatabaseMetaData.TENANT_ID_INDEX];
    byte[] schemaBytes = rowKeyMetadata[PhoenixDatabaseMetaData.SCHEMA_NAME_INDEX];
    byte[] tableBytes = rowKeyMetadata[PhoenixDatabaseMetaData.TABLE_NAME_INDEX];
    if (tableType != PTableType.VIEW || tableName != null) {
        tableName = tableName == null ? SchemaUtil.getTableNameAsBytes(schemaBytes, tableBytes) : tableName;
        ensureTableCreated(tableName, tableType, tableProps, families, splits);
    }
    
    byte[] tableKey = SchemaUtil.getTableKey(tenantIdBytes, schemaBytes, tableBytes);
    MetaDataMutationResult result = metaDataCoprocessorExec(tableKey,
        new Batch.Call<MetaDataProtocol, MetaDataMutationResult>() {
            @Override
            public MetaDataMutationResult call(MetaDataProtocol instance) throws IOException {
              return instance.createTable(tableMetaData);
            }
        });
    return result;
}
 
Example #5
Source File: TestRegionObserverForAddingMutationsFromCoprocessors.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Override
public void preBatchMutate(ObserverContext<RegionCoprocessorEnvironment> c,
    MiniBatchOperationInProgress<Mutation> miniBatchOp) throws IOException {
  Mutation mut = miniBatchOp.getOperation(0);

  if (mut instanceof Delete) {
    List<Cell> cells = mut.getFamilyCellMap().get(test);
    Delete[] deletes = new Delete[] {
        // delete only 2 rows
        new Delete(row1, cells.get(0).getTimestamp()),
        new Delete(row2, cells.get(0).getTimestamp()),
    };
    LOG.info("Deleting:" + Arrays.toString(deletes));
    miniBatchOp.addOperationsFromCP(0, deletes);
  }
}
 
Example #6
Source File: MutationState.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void logMutationSize(HTableInterface htable, List<Mutation> mutations) {
    long byteSize = 0;
    int keyValueCount = 0;
    for (Mutation mutation : mutations) {
        if (mutation.getFamilyMap() != null) { // Not a Delete of the row
            for (Entry<byte[], List<KeyValue>> entry : mutation.getFamilyMap().entrySet()) {
                if (entry.getValue() != null) {
                    for (KeyValue kv : entry.getValue()) {
                        byteSize += kv.getBuffer().length;
                        keyValueCount++;
                    }
                }
            }
        }
    }
    logger.debug("Sending " + mutations.size() + " mutations for " + Bytes.toString(htable.getTableName()) + " with " + keyValueCount + " key values of total size " + byteSize + " bytes");
}
 
Example #7
Source File: ThriftUtilities.java    From hbase with Apache License 2.0 6 votes vote down vote up
public static TRowMutations rowMutationsFromHBase(RowMutations in) {
  TRowMutations tRowMutations = new TRowMutations();
  tRowMutations.setRow(in.getRow());
  for (Mutation mutation : in.getMutations()) {
    TMutation tMutation = new TMutation();
    if (mutation instanceof Put) {
      tMutation.setPut(ThriftUtilities.putFromHBase((Put)mutation));
    } else if (mutation instanceof Delete) {
      tMutation.setDeleteSingle(ThriftUtilities.deleteFromHBase((Delete)mutation));
    } else {
      throw new IllegalArgumentException(
          "Only Put and Delete is supported in mutateRow, but muation=" + mutation);
    }
    tRowMutations.addToMutations(tMutation);
  }
  return tRowMutations;
}
 
Example #8
Source File: RequestConverter.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Create a protocol buffer MultiRequest for row mutations.
 * Does not propagate Action absolute position.  Does not set atomic action on the created
 * RegionAtomic.  Caller should do that if wanted.
 * @param regionName
 * @param rowMutations
 * @return a data-laden RegionMutation.Builder
 * @throws IOException
 */
public static RegionAction.Builder buildRegionAction(final byte [] regionName,
    final RowMutations rowMutations)
throws IOException {
  RegionAction.Builder builder =
    getRegionActionBuilderWithRegion(RegionAction.newBuilder(), regionName);
  ClientProtos.Action.Builder actionBuilder = ClientProtos.Action.newBuilder();
  MutationProto.Builder mutationBuilder = MutationProto.newBuilder();
  for (Mutation mutation: rowMutations.getMutations()) {
    MutationType mutateType = null;
    if (mutation instanceof Put) {
      mutateType = MutationType.PUT;
    } else if (mutation instanceof Delete) {
      mutateType = MutationType.DELETE;
    } else {
      throw new DoNotRetryIOException("RowMutations supports only put and delete, not " +
        mutation.getClass().getName());
    }
    mutationBuilder.clear();
    MutationProto mp = ProtobufUtil.toMutation(mutateType, mutation, mutationBuilder);
    actionBuilder.clear();
    actionBuilder.setMutation(mp);
    builder.addAction(actionBuilder.build());
  }
  return builder;
}
 
Example #9
Source File: CoveredColumnsIndexBuilder.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
  * Split the mutation into batches based on the timestamps of each keyvalue. We need to check each
  * key-value in the update to see if it matches the others. Generally, this will be the case, but
  * you can add kvs to a mutation that don't all have the timestamp, so we need to manage
  * everything in batches based on timestamp.
  * <p>
  * Adds all the updates in the {@link Mutation} to the state, as a side-effect.
  * @param updateMap index updates into which to add new updates. Modified as a side-effect.
  * @param state current state of the row for the mutation.
  * @param m mutation to batch
* @throws IOException 
  */
 private void batchMutationAndAddUpdates(IndexUpdateManager manager, Mutation m) throws IOException {
   // split the mutation into timestamp-based batches
   Collection<Batch> batches = createTimestampBatchesFromMutation(m);

   // create a state manager, so we can manage each batch
   LocalTableState state = new LocalTableState(env, localTable, m);

   // go through each batch of keyvalues and build separate index entries for each
   boolean cleanupCurrentState = true;
   for (Batch batch : batches) {
     /*
      * We have to split the work between the cleanup and the update for each group because when we
      * update the current state of the row for the current batch (appending the mutations for the
      * current batch) the next group will see that as the current state, which will can cause the
      * a delete and a put to be created for the next group.
      */
     if (addMutationsForBatch(manager, batch, state, cleanupCurrentState)) {
       cleanupCurrentState = false;
     }
   }
 }
 
Example #10
Source File: RSGroupInfoManagerImpl.java    From hbase with Apache License 2.0 6 votes vote down vote up
private void multiMutate(List<Mutation> mutations) throws IOException {
  MutateRowsRequest.Builder builder = MutateRowsRequest.newBuilder();
  for (Mutation mutation : mutations) {
    if (mutation instanceof Put) {
      builder
          .addMutationRequest(ProtobufUtil.toMutation(MutationProto.MutationType.PUT, mutation));
    } else if (mutation instanceof Delete) {
      builder.addMutationRequest(
        ProtobufUtil.toMutation(MutationProto.MutationType.DELETE, mutation));
    } else {
      throw new DoNotRetryIOException(
          "multiMutate doesn't support " + mutation.getClass().getName());
    }
  }
  MutateRowsRequest request = builder.build();
  AsyncTable<?> table = conn.getTable(RSGROUP_TABLE_NAME);
  FutureUtils.get(table.<MultiRowMutationService, MutateRowsResponse> coprocessorService(
    MultiRowMutationService::newStub,
    (stub, controller, done) -> stub.mutateRows(controller, request, done), ROW_KEY));
}
 
Example #11
Source File: HBaseBolt.java    From storm-hbase with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Tuple tuple) {
    byte[] rowKey = this.mapper.rowKey(tuple);
    ColumnList cols = this.mapper.columns(tuple);
    List<Mutation> mutations = hBaseClient.constructMutationReq(rowKey, cols, writeToWAL? Durability.SYNC_WAL : Durability.SKIP_WAL);

    try {
        this.hBaseClient.batchMutate(mutations);
    } catch(Exception e){
        LOG.warn("Failing tuple. Error writing rowKey " + rowKey, e);
        this.collector.fail(tuple);
        return;
    }

    this.collector.ack(tuple);
}
 
Example #12
Source File: NonTxIndexBuilderTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void assertContains(Collection<Pair<Mutation, byte[]>> indexUpdates,
        final long mutationTs, final byte[] row, final Type cellType, final byte[] fam,
        final byte[] qual, final long cellTs) {
    Predicate<Pair<Mutation, byte[]>> hasCellPredicate =
            new Predicate<Pair<Mutation, byte[]>>() {
                @Override
                public boolean apply(Pair<Mutation, byte[]> input) {
                    assertEquals(TEST_TABLE_INDEX_STRING, Bytes.toString(input.getSecond()));
                    Mutation mutation = input.getFirst();
                    if (mutationTs == mutation.getTimeStamp()) {
                        NavigableMap<byte[], List<Cell>> familyCellMap =
                                mutation.getFamilyCellMap();
                        Cell updateCell = familyCellMap.get(fam).get(0);
                        if (cellType == KeyValue.Type.codeToType(updateCell.getTypeByte())
                                && Bytes.compareTo(fam, CellUtil.cloneFamily(updateCell)) == 0
                                && Bytes.compareTo(qual,
                                    CellUtil.cloneQualifier(updateCell)) == 0
                                && cellTs == updateCell.getTimestamp()) {
                            return true;
                        }
                    }
                    return false;
                }
            };
    Optional<Pair<Mutation, byte[]>> tryFind =
            Iterables.tryFind(indexUpdates, hasCellPredicate);
    assertTrue(tryFind.isPresent());
}
 
Example #13
Source File: WALRecoveryRegionPostOpenIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public void handleFailure(Multimap<HTableInterfaceReference, Mutation> attempted, Exception cause) throws IOException
{
    LOGGER.info("Found index update failure!");
    handleFailureCalledCount++;
    tableReferenceToMutation=attempted;
    LOGGER.info("failed index update on WAL recovery - allowing index table can be write.");
    failIndexTableWrite=false;
    super.handleFailure(attempted, cause);

    if(handleFailureCountDownLatch!=null) {
        handleFailureCountDownLatch.countDown();
    }
 }
 
Example #14
Source File: ProtobufUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static MutationProto toProto(Mutation mutation) throws IOException {
    MutationType type;
    if (mutation instanceof Put) {
        type = MutationType.PUT;
    } else if (mutation instanceof Delete) {
        type = MutationType.DELETE;
    } else {
        throw new IllegalArgumentException("Only Put and Delete are supported");
    }
    return org.apache.hadoop.hbase.protobuf.ProtobufUtil.toMutation(type, mutation);
}
 
Example #15
Source File: RegionCoprocessorHost.java    From hbase with Apache License 2.0 5 votes vote down vote up
public List<Pair<Cell, Cell>> postAppendBeforeWAL(final Mutation mutation,
    final List<Pair<Cell, Cell>> cellPairs) throws IOException {
  if (this.coprocEnvironments.isEmpty()) {
    return cellPairs;
  }
  return execOperationWithResult(
      new ObserverOperationWithResult<RegionObserver, List<Pair<Cell, Cell>>>(
          regionObserverGetter, cellPairs) {
        @Override
        public List<Pair<Cell, Cell>> call(RegionObserver observer) throws IOException {
          return observer.postAppendBeforeWAL(this, mutation, getResult());
        }
      });
}
 
Example #16
Source File: Indexer.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
  public void preWALRestore(
          org.apache.hadoop.hbase.coprocessor.ObserverContext<? extends RegionCoprocessorEnvironment> ctx,
          org.apache.hadoop.hbase.client.RegionInfo info, org.apache.hadoop.hbase.wal.WALKey logKey, WALEdit logEdit)
          throws IOException {

    if (this.disabled) {
        return;
    }

  // TODO check the regions in transition. If the server on which the region lives is this one,
  // then we should rety that write later in postOpen.
  // we might be able to get even smarter here and pre-split the edits that are server-local
  // into their own recovered.edits file. This then lets us do a straightforward recovery of each
  // region (and more efficiently as we aren't writing quite as hectically from this one place).

    long start = EnvironmentEdgeManager.currentTimeMillis();
    try {
        /*
         * Basically, we let the index regions recover for a little while long before retrying in the
         * hopes they come up before the primary table finishes.
         */
        Collection<Pair<Mutation, byte[]>> indexUpdates = extractIndexUpdate(logEdit);
        recoveryWriter.writeAndHandleFailure(indexUpdates, true, ScanUtil.UNKNOWN_CLIENT_VERSION);
    } finally {
        long duration = EnvironmentEdgeManager.currentTimeMillis() - start;
        if (duration >= slowPreWALRestoreThreshold) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(getCallTooSlowMessage("preWALRestore",
                        duration, slowPreWALRestoreThreshold));
            }
            metricSource.incrementNumSlowPreWALRestoreCalls();
        }
        metricSource.updatePreWALRestoreTime(duration);
    }
}
 
Example #17
Source File: MasterCoprocessorHost.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Invoked before merge regions operation writes the new region to hbase:meta
 * @param regionsToMerge the regions to merge
 * @param metaEntries the meta entry
 * @param user the user
 * @throws IOException
 */
public void preMergeRegionsCommit(
    final RegionInfo[] regionsToMerge,
    final @MetaMutationAnnotation List<Mutation> metaEntries,
    final User user) throws IOException {
  execOperation(coprocEnvironments.isEmpty() ? null : new MasterObserverOperation(user) {
    @Override
    public void call(MasterObserver observer) throws IOException {
      observer.preMergeRegionsCommitAction(this, regionsToMerge, metaEntries);
    }
  });
}
 
Example #18
Source File: MetaDataUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static PTableType getTableType(List<Mutation> tableMetaData, KeyValueBuilder builder,
  ImmutableBytesPtr value) {
    if (getMutationValue(getPutOnlyTableHeaderRow(tableMetaData),
        PhoenixDatabaseMetaData.TABLE_TYPE_BYTES, builder, value)) {
        return PTableType.fromSerializedValue(value.get()[value.getOffset()]);
    }
    return null;
}
 
Example #19
Source File: ScanUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static ScanRanges newScanRanges(List<? extends Mutation> mutations) throws SQLException {
    List<KeyRange> keys = Lists.newArrayListWithExpectedSize(mutations.size());
    for (Mutation m : mutations) {
        keys.add(PVarbinary.INSTANCE.getKeyRange(m.getRow()));
    }
    ScanRanges keyRanges = ScanRanges.createPointLookup(keys);
    return keyRanges;
}
 
Example #20
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Get the list of uncommitted KeyValues for the connection. Currently used to write an
 * Phoenix-compliant HFile from a map/reduce job.
 * @param conn an open JDBC connection
 * @return the list of HBase mutations for uncommitted data
 * @throws SQLException
 */
public static Iterator<Pair<byte[],List<KeyValue>>> getUncommittedDataIterator(Connection conn, boolean includeMutableIndexes) throws SQLException {
    final PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    final Iterator<Pair<byte[],List<Mutation>>> iterator = pconn.getMutationState().toMutations(includeMutableIndexes);
    return new Iterator<Pair<byte[],List<KeyValue>>>() {

        @Override
        public boolean hasNext() {
            return iterator.hasNext();
        }

        @Override
        public Pair<byte[], List<KeyValue>> next() {
            Pair<byte[],List<Mutation>> pair = iterator.next();
            List<KeyValue> keyValues = Lists.newArrayListWithExpectedSize(pair.getSecond().size() * 5); // Guess-timate 5 key values per row
            for (Mutation mutation : pair.getSecond()) {
                for (List<Cell> keyValueList : mutation.getFamilyCellMap().values()) {
                    for (Cell keyValue : keyValueList) {
                        keyValues.add(org.apache.hadoop.hbase.KeyValueUtil.ensureKeyValue(keyValue));
                    }
                }
            }
            Collections.sort(keyValues, pconn.getKeyValueBuilder().getKeyValueComparator());
            return new Pair<byte[], List<KeyValue>>(pair.getFirst(),keyValues);
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

    };
}
 
Example #21
Source File: VisibilityController.java    From hbase with Apache License 2.0 5 votes vote down vote up
private Cell createNewCellWithTags(Mutation mutation, Cell newCell) throws IOException {
  List<Tag> tags = Lists.newArrayList();
  CellVisibility cellVisibility = null;
  try {
    cellVisibility = mutation.getCellVisibility();
  } catch (DeserializationException e) {
    throw new IOException(e);
  }
  if (cellVisibility == null) {
    return newCell;
  }
  // Prepend new visibility tags to a new list of tags for the cell
  // Don't check user auths for labels with Mutations when the user is super user
  boolean authCheck = authorizationEnabled && checkAuths && !(isSystemOrSuperUser());
  tags.addAll(this.visibilityLabelService.createVisibilityExpTags(cellVisibility.getExpression(),
      true, authCheck));
  // Carry forward all other tags
  Iterator<Tag> tagsItr = PrivateCellUtil.tagsIterator(newCell);
  while (tagsItr.hasNext()) {
    Tag tag = tagsItr.next();
    if (tag.getType() != TagType.VISIBILITY_TAG_TYPE
        && tag.getType() != TagType.VISIBILITY_EXP_SERIALIZATION_FORMAT_TAG_TYPE) {
      tags.add(tag);
    }
  }

  return PrivateCellUtil.createCell(newCell, tags);
}
 
Example #22
Source File: MutationTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void assertDurability(Connection conn, Durability durability) throws SQLException {
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    Iterator<Pair<byte[], List<Mutation>>> it = pconn.getMutationState().toMutations();
    assertTrue(it.hasNext());
    while (it.hasNext()) {
        Pair<byte[], List<Mutation>> pair = it.next();
        assertFalse(pair.getSecond().isEmpty());
        for (Mutation m : pair.getSecond()) {
            assertEquals(durability, m.getDurability());
        }
    }
}
 
Example #23
Source File: IndexUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static void writeLocalUpdates(Region region, final List<Mutation> mutations, boolean skipWAL) throws IOException {
    if(skipWAL) {
        for (Mutation m : mutations) {
            m.setDurability(Durability.SKIP_WAL);
        }
    }
    region.batchMutate(mutations.toArray(new Mutation[mutations.size()]));
}
 
Example #24
Source File: TestSplitTransactionOnCluster.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public void preSplitRegionBeforeMETAAction(
    final ObserverContext<MasterCoprocessorEnvironment> ctx,
    final byte[] splitKey,
    final List<Mutation> metaEntries) throws IOException {
  latch.countDown();
  throw new IOException("Causing rollback of region split");
}
 
Example #25
Source File: Mutators.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
public static void write(Table table, Mutator... writers) {
    List<Mutation> batch = new ArrayList<>();
    for (Mutator writer : writers) {
        writer.constructMutations().forEachRemaining(batch::add);
    }
    write(table, batch);
}
 
Example #26
Source File: PTableImpl.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void removeIfPresent(Mutation m, byte[] family, byte[] qualifier) {
    Map<byte[],List<Cell>> familyMap = m.getFamilyCellMap();
    List<Cell> kvs = familyMap.get(family);
    if (kvs != null) {
        Iterator<Cell> iterator = kvs.iterator();
        while (iterator.hasNext()) {
            Cell kv = iterator.next();
            if (Bytes.compareTo(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength(),
                  qualifier, 0, qualifier.length) == 0) {
                iterator.remove();
            }
        }
    }
}
 
Example #27
Source File: MutableIndexFailureIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public void preBatchMutate(ObserverContext<RegionCoprocessorEnvironment> c, MiniBatchOperationInProgress<Mutation> miniBatchOp) throws IOException {
    boolean throwException = false;
    if (FAIL_NEXT_WRITE) {
        throwException = true;
        FAIL_NEXT_WRITE = false;
    } else if (c.getEnvironment().getRegionInfo().getTable().getNameAsString().endsWith("A_" + FAIL_INDEX_NAME)
            && FAIL_WRITE) {
        throwException = true;
        if (TOGGLE_FAIL_WRITE_FOR_RETRY) {
            FAIL_WRITE = !FAIL_WRITE;
        }
    } else {
        // When local index updates are atomic with data updates, testing a write failure to a local
        // index won't make sense.
        Mutation operation = miniBatchOp.getOperation(0);
        if (FAIL_WRITE) {
            Map<byte[],List<Cell>>cellMap = operation.getFamilyCellMap();
            for (Map.Entry<byte[],List<Cell>> entry : cellMap.entrySet()) {
                byte[] family = entry.getKey();
                if (Bytes.toString(family).startsWith(QueryConstants.LOCAL_INDEX_COLUMN_FAMILY_PREFIX)) {
                    int regionStartKeyLen = c.getEnvironment().getRegionInfo().getStartKey().length;
                    Cell firstCell = entry.getValue().get(0);
                    long indexId = MetaDataUtil.getViewIndexIdDataType().getCodec().decodeLong(firstCell.getRowArray(), firstCell.getRowOffset() + regionStartKeyLen, SortOrder.getDefault());
                    // Only throw for first local index as the test may have multiple local indexes
                    if (indexId == Short.MIN_VALUE) {
                        throwException = true;
                        break;
                    }
                }
            }
        }
    }
    if (throwException) {
        if (!TOGGLE_FAIL_WRITE_FOR_RETRY) {
            dropIndex(c);
        }
        throw new DoNotRetryIOException();
    }
}
 
Example #28
Source File: PhoenixKeyValueUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static void setTimestamp(Mutation m, long timestamp) {
    byte[] tsBytes = Bytes.toBytes(timestamp);
    for (List<Cell> family : m.getFamilyCellMap().values()) {
        List<KeyValue> familyKVs = org.apache.hadoop.hbase.KeyValueUtil.ensureKeyValues(family);
        for (KeyValue kv : familyKVs) {
            int tsOffset = kv.getTimestampOffset();
            System.arraycopy(tsBytes, 0, kv.getBuffer(), tsOffset, Bytes.SIZEOF_LONG);
        }
    }
}
 
Example #29
Source File: MultiThreadedUpdater.java    From hbase with Apache License 2.0 5 votes vote down vote up
public void mutate(Table table, Mutation m,
    long keyBase, byte[] row, byte[] cf, byte[] q, byte[] v) {
  long start = System.currentTimeMillis();
  try {
    m = dataGenerator.beforeMutate(keyBase, m);
    if (m instanceof Increment) {
      table.increment((Increment)m);
    } else if (m instanceof Append) {
      table.append((Append)m);
    } else if (m instanceof Put) {
      table.checkAndMutate(row, cf).qualifier(q).ifEquals(v).thenPut((Put)m);
    } else if (m instanceof Delete) {
      table.checkAndMutate(row, cf).qualifier(q).ifEquals(v).thenDelete((Delete)m);
    } else {
      throw new IllegalArgumentException(
        "unsupported mutation " + m.getClass().getSimpleName());
    }
    totalOpTimeMs.addAndGet(System.currentTimeMillis() - start);
  } catch (IOException e) {
    if (ignoreNonceConflicts) {
      LOG.info("Detected nonce conflict, ignoring: " + e.getMessage());
      totalOpTimeMs.addAndGet(System.currentTimeMillis() - start);
      return;
    }
    failedKeySet.add(keyBase);
    String exceptionInfo;
    if (e instanceof RetriesExhaustedWithDetailsException) {
      RetriesExhaustedWithDetailsException aggEx = (RetriesExhaustedWithDetailsException) e;
      exceptionInfo = aggEx.getExhaustiveDescription();
    } else {
      exceptionInfo = StringUtils.stringifyException(e);
    }
    LOG.error("Failed to mutate: " + keyBase + " after " +
        (System.currentTimeMillis() - start) +
      "ms; region information: " + getRegionDebugInfoSafe(table, m.getRow()) + "; errors: "
        + exceptionInfo);
  }
}
 
Example #30
Source File: VerifySingleIndexRowTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private Mutation getDeleteMutation(Mutation orig, Long ts) {
    Mutation m = new Delete(orig.getRow());
    List<Cell> origList = orig.getFamilyCellMap().firstEntry().getValue();
    ts = ts == null ? EnvironmentEdgeManager.currentTimeMillis() : ts;
    Cell c = getNewPutCell(orig, origList, ts, KeyValue.Type.DeleteFamilyVersion);
    Cell empty = getEmptyCell(orig, origList, ts, KeyValue.Type.Put, true);
    byte[] fam = CellUtil.cloneFamily(origList.get(0));
    List<Cell> famCells = Lists.newArrayList();
    m.getFamilyCellMap().put(fam, famCells);
    famCells.add(c);
    famCells.add(empty);
    return m;
}