Search in sources :

Example 1 with PhoenixTxIndexMutationGenerator

use of org.apache.phoenix.execute.PhoenixTxIndexMutationGenerator in project phoenix by apache.

the class PhoenixTransactionalIndexer method preBatchMutate.

@Override
public void preBatchMutate(ObserverContext<RegionCoprocessorEnvironment> c, MiniBatchOperationInProgress<Mutation> miniBatchOp) throws IOException {
    Mutation m = miniBatchOp.getOperation(0);
    if (!codec.isEnabled(m)) {
        super.preBatchMutate(c, miniBatchOp);
        return;
    }
    PhoenixIndexMetaData indexMetaData = new PhoenixIndexMetaDataBuilder(c.getEnvironment()).getIndexMetaData(miniBatchOp);
    if (indexMetaData.getClientVersion() >= PhoenixDatabaseMetaData.MIN_TX_CLIENT_SIDE_MAINTENANCE && !indexMetaData.hasLocalIndexes()) {
        // Still generate index updates server side for local indexes
        super.preBatchMutate(c, miniBatchOp);
        return;
    }
    BatchMutateContext context = new BatchMutateContext();
    setBatchMutateContext(c, context);
    Collection<Pair<Mutation, byte[]>> indexUpdates = null;
    // get the current span, or just use a null-span to avoid a bunch of if statements
    try (TraceScope scope = Trace.startSpan("Starting to build index updates")) {
        Span current = scope.getSpan();
        if (current == null) {
            current = NullSpan.INSTANCE;
        }
        RegionCoprocessorEnvironment env = c.getEnvironment();
        PhoenixTransactionContext txnContext = indexMetaData.getTransactionContext();
        if (txnContext == null) {
            throw new NullPointerException("Expected to find transaction in metadata for " + env.getRegionInfo().getTable().getNameAsString());
        }
        PhoenixTxIndexMutationGenerator generator = new PhoenixTxIndexMutationGenerator(env.getConfiguration(), indexMetaData, env.getRegionInfo().getTable().getName(), env.getRegionInfo().getStartKey(), env.getRegionInfo().getEndKey());
        try (HTableInterface htable = env.getTable(env.getRegionInfo().getTable())) {
            // get the index updates for all elements in this batch
            indexUpdates = generator.getIndexUpdates(htable, getMutationIterator(miniBatchOp));
        }
        byte[] tableName = c.getEnvironment().getRegion().getTableDesc().getTableName().getName();
        Iterator<Pair<Mutation, byte[]>> indexUpdatesItr = indexUpdates.iterator();
        List<Mutation> localUpdates = new ArrayList<Mutation>(indexUpdates.size());
        while (indexUpdatesItr.hasNext()) {
            Pair<Mutation, byte[]> next = indexUpdatesItr.next();
            if (Bytes.compareTo(next.getSecond(), tableName) == 0) {
                // These mutations will not go through the preDelete hooks, so we
                // must manually convert them here.
                Mutation mutation = TransactionUtil.convertIfDelete(next.getFirst());
                localUpdates.add(mutation);
                indexUpdatesItr.remove();
            }
        }
        if (!localUpdates.isEmpty()) {
            miniBatchOp.addOperationsFromCP(0, localUpdates.toArray(new Mutation[localUpdates.size()]));
        }
        if (!indexUpdates.isEmpty()) {
            context.indexUpdates = indexUpdates;
        }
        current.addTimelineAnnotation("Built index updates, doing preStep");
        TracingUtils.addAnnotation(current, "index update count", context.indexUpdates.size());
    } catch (Throwable t) {
        String msg = "Failed to update index with entries:" + indexUpdates;
        LOG.error(msg, t);
        ServerUtil.throwIOException(msg, t);
    }
}
Also used : TraceScope(org.apache.htrace.TraceScope) ArrayList(java.util.ArrayList) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) Span(org.apache.htrace.Span) NullSpan(org.apache.phoenix.trace.util.NullSpan) PhoenixTxIndexMutationGenerator(org.apache.phoenix.execute.PhoenixTxIndexMutationGenerator) RegionCoprocessorEnvironment(org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment) DelegateRegionCoprocessorEnvironment(org.apache.phoenix.coprocessor.DelegateRegionCoprocessorEnvironment) Mutation(org.apache.hadoop.hbase.client.Mutation) PhoenixTransactionContext(org.apache.phoenix.transaction.PhoenixTransactionContext) Pair(org.apache.hadoop.hbase.util.Pair)

Aggregations

ArrayList (java.util.ArrayList)1 HTableInterface (org.apache.hadoop.hbase.client.HTableInterface)1 Mutation (org.apache.hadoop.hbase.client.Mutation)1 RegionCoprocessorEnvironment (org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment)1 Pair (org.apache.hadoop.hbase.util.Pair)1 Span (org.apache.htrace.Span)1 TraceScope (org.apache.htrace.TraceScope)1 DelegateRegionCoprocessorEnvironment (org.apache.phoenix.coprocessor.DelegateRegionCoprocessorEnvironment)1 PhoenixTxIndexMutationGenerator (org.apache.phoenix.execute.PhoenixTxIndexMutationGenerator)1 NullSpan (org.apache.phoenix.trace.util.NullSpan)1 PhoenixTransactionContext (org.apache.phoenix.transaction.PhoenixTransactionContext)1