Search in sources :

Example 1 with TXManagerImpl

use of org.apache.geode.internal.cache.TXManagerImpl in project geode by apache.

the class IndexManager method processAction.

/**
   * @param opCode one of IndexProtocol.OTHER_OP, BEFORE_UPDATE_OP, AFTER_UPDATE_OP.
   */
private void processAction(RegionEntry entry, int action, int opCode) throws QueryException {
    final long startPA = getCachePerfStats().startIndexUpdate();
    DefaultQuery.setPdxReadSerialized(this.region.getCache(), true);
    TXStateProxy tx = null;
    if (!((InternalCache) this.region.getCache()).isClient()) {
        tx = ((TXManagerImpl) this.region.getCache().getCacheTransactionManager()).internalSuspend();
    }
    try {
        // creation thread
        if (IndexManager.testHook != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("IndexManager TestHook is set.");
            }
            // ConcurrentIndexInitOnOverflowRegionDUnitTest
            testHook.hook(6);
        }
        long start = 0;
        boolean indexLockAcquired = false;
        switch(action) {
            case ADD_ENTRY:
                {
                    if (IndexManager.testHook != null) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("IndexManager TestHook in ADD_ENTRY.");
                        }
                        testHook.hook(5);
                    }
                    // this action is only called after update
                    assert opCode == IndexProtocol.OTHER_OP;
                    // Asif The behaviour can arise if an index creation has already
                    // acted upon a newly added entry , but by the time callback
                    // occurs , the index is added to the map & thus
                    // the add operation will now have an effect of update.
                    // so we need to remove the mapping even if it is an Add action
                    // as otherwise the new results will get added into the
                    // old results instead of replacement
                    Iterator iter = this.indexes.values().iterator();
                    while (iter.hasNext()) {
                        Object ind = iter.next();
                        // the index is in create phase.
                        if (ind instanceof FutureTask) {
                            continue;
                        }
                        IndexProtocol index = (IndexProtocol) ind;
                        if (((AbstractIndex) index).isPopulated() && index.getType() != IndexType.PRIMARY_KEY) {
                            // apply IMQ on it
                            if (!index.containsEntry(entry)) {
                                if (logger.isDebugEnabled()) {
                                    logger.debug("Adding to index: {}{} value: {}", index.getName(), this.region.getFullPath(), entry.getKey());
                                }
                                start = ((AbstractIndex) index).updateIndexUpdateStats();
                                index.addIndexMapping(entry);
                                ((AbstractIndex) index).updateIndexUpdateStats(start);
                            }
                        }
                    }
                    break;
                }
            case UPDATE_ENTRY:
                {
                    if (IndexManager.testHook != null) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("IndexManager TestHook in UPDATE_ENTRY.");
                        }
                        testHook.hook(5);
                        // QueryDataInconsistencyDUnitTest
                        testHook.hook(9);
                    }
                    // this action is only called with opCode AFTER_UPDATE_OP
                    assert opCode == IndexProtocol.AFTER_UPDATE_OP;
                    Iterator iter = this.indexes.values().iterator();
                    while (iter.hasNext()) {
                        Object ind = iter.next();
                        // the index is in create phase.
                        if (ind instanceof FutureTask) {
                            continue;
                        }
                        IndexProtocol index = (IndexProtocol) ind;
                        if (((AbstractIndex) index).isPopulated() && index.getType() != IndexType.PRIMARY_KEY) {
                            if (logger.isDebugEnabled()) {
                                logger.debug("Updating index: {}{} value: {}", index.getName(), this.region.getFullPath(), entry.getKey());
                            }
                            start = ((AbstractIndex) index).updateIndexUpdateStats();
                            index.addIndexMapping(entry);
                            ((AbstractIndex) index).updateIndexUpdateStats(start);
                        }
                    }
                    break;
                }
            case REMOVE_ENTRY:
                {
                    if (IndexManager.testHook != null) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("IndexManager TestHook in REMOVE_ENTRY.");
                        }
                        testHook.hook(5);
                        testHook.hook(10);
                    }
                    Iterator iter = this.indexes.values().iterator();
                    while (iter.hasNext()) {
                        Object ind = iter.next();
                        // the index is in create phase.
                        if (ind instanceof FutureTask) {
                            continue;
                        }
                        IndexProtocol index = (IndexProtocol) ind;
                        if (((AbstractIndex) index).isPopulated() && index.getType() != IndexType.PRIMARY_KEY) {
                            AbstractIndex abstractIndex = (AbstractIndex) index;
                            if (logger.isDebugEnabled()) {
                                logger.debug("Removing from index: {}{} value: {}", index.getName(), this.region.getFullPath(), entry.getKey());
                            }
                            start = ((AbstractIndex) index).updateIndexUpdateStats();
                            index.removeIndexMapping(entry, opCode);
                            ((AbstractIndex) index).updateIndexUpdateStats(start);
                        }
                    }
                    break;
                }
            default:
                {
                    throw new IndexMaintenanceException(LocalizedStrings.IndexManager_INVALID_ACTION.toLocalizedString());
                }
        }
    } finally {
        DefaultQuery.setPdxReadSerialized(this.region.getCache(), false);
        if (tx != null) {
            ((TXManagerImpl) this.region.getCache().getCacheTransactionManager()).internalResume(tx);
        }
        getCachePerfStats().endIndexUpdate(startPA);
    }
}
Also used : TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) FutureTask(java.util.concurrent.FutureTask) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) Iterator(java.util.Iterator) IndexMaintenanceException(org.apache.geode.cache.query.IndexMaintenanceException)

Example 2 with TXManagerImpl

use of org.apache.geode.internal.cache.TXManagerImpl in project geode by apache.

the class DefaultQuery method executeUsingContext.

public Object executeUsingContext(ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    QueryObserver observer = QueryObserverHolder.getInstance();
    long startTime = CachePerfStats.getStatTime();
    TXStateProxy tx = ((TXManagerImpl) this.cache.getCacheTransactionManager()).internalSuspend();
    try {
        observer.startQuery(this);
        observer.beforeQueryEvaluation(this.compiledQuery, context);
        if (DefaultQuery.testHook != null) {
            DefaultQuery.testHook.doTestHook(6);
        }
        Object results = null;
        try {
            // two-pass evaluation.
            // first pre-compute dependencies, cached in the context.
            this.compiledQuery.computeDependencies(context);
            if (testHook != null) {
                testHook.doTestHook(1);
            }
            results = this.compiledQuery.evaluate(context);
        } catch (QueryExecutionCanceledException ignore) {
            // when query is canceled.
            if (this.canceledException != null) {
                throw this.canceledException;
            } else {
                throw new QueryExecutionCanceledException("Query was canceled. It may be due to low memory or the query was running longer than the MAX_QUERY_EXECUTION_TIME.");
            }
        } finally {
            observer.afterQueryEvaluation(results);
        }
        return results;
    } finally {
        observer.endQuery();
        long endTime = CachePerfStats.getStatTime();
        updateStatistics(endTime - startTime);
        pdxClassToFieldsMap.remove();
        pdxClassToMethodsMap.remove();
        ((TXManagerImpl) this.cache.getCacheTransactionManager()).internalResume(tx);
    }
}
Also used : TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy)

Example 3 with TXManagerImpl

use of org.apache.geode.internal.cache.TXManagerImpl in project geode by apache.

the class PartitionMessage method process.

/**
   * Upon receipt of the message, both process the message and send an acknowledgement, not
   * necessarily in that order. Note: Any hang in this message may cause a distributed deadlock for
   * those threads waiting for an acknowledgement.
   * 
   * @throws PartitionedRegionException if the region does not exist (typically, if it has been
   *         destroyed)
   */
@Override
public void process(final DistributionManager dm) {
    Throwable thr = null;
    boolean sendReply = true;
    PartitionedRegion pr = null;
    long startTime = 0;
    EntryLogger.setSource(getSender(), "PR");
    try {
        if (checkCacheClosing(dm) || checkDSClosing(dm)) {
            thr = new CacheClosedException(LocalizedStrings.PartitionMessage_REMOTE_CACHE_IS_CLOSED_0.toLocalizedString(dm.getId()));
            return;
        }
        pr = getPartitionedRegion();
        if (pr == null && failIfRegionMissing()) {
            // if the distributed system is disconnecting, don't send a reply saying
            // the partitioned region can't be found (bug 36585)
            thr = new ForceReattemptException(LocalizedStrings.PartitionMessage_0_COULD_NOT_FIND_PARTITIONED_REGION_WITH_ID_1.toLocalizedString(dm.getDistributionManagerId(), regionId));
            // reply sent in finally block below
            return;
        }
        if (pr != null) {
            startTime = getStartPartitionMessageProcessingTime(pr);
        }
        thr = UNHANDLED_EXCEPTION;
        InternalCache cache = getInternalCache();
        if (cache == null) {
            throw new ForceReattemptException(LocalizedStrings.PartitionMessage_REMOTE_CACHE_IS_CLOSED_0.toLocalizedString());
        }
        TXManagerImpl txMgr = getTXManagerImpl(cache);
        TXStateProxy tx = txMgr.masqueradeAs(this);
        if (tx == null) {
            sendReply = operateOnPartitionedRegion(dm, pr, startTime);
        } else {
            try {
                if (txMgr.isClosed()) {
                    // NO DISTRIBUTED MESSAGING CAN BE DONE HERE!
                    sendReply = false;
                } else if (tx.isInProgress()) {
                    sendReply = operateOnPartitionedRegion(dm, pr, startTime);
                    tx.updateProxyServer(this.getSender());
                }
            } finally {
                txMgr.unmasquerade(tx);
            }
        }
        thr = null;
    } catch (ForceReattemptException fre) {
        thr = fre;
    } catch (DataLocationException fre) {
        thr = new ForceReattemptException(fre.getMessage(), fre);
    } catch (DistributedSystemDisconnectedException se) {
        // bug 37026: this is too noisy...
        // throw new CacheClosedException("remote system shutting down");
        // thr = se; cache is closed, no point trying to send a reply
        thr = null;
        sendReply = false;
        if (logger.isDebugEnabled()) {
            logger.debug("shutdown caught, abandoning message: {}", se.getMessage(), se);
        }
    } catch (RegionDestroyedException | RegionNotFoundException rde) {
        // [bruce] RDE does not always mean that the sender's region is also
        // destroyed, so we must send back an exception. If the sender's
        // region is also destroyed, who cares if we send it an exception
        // if (pr != null && pr.isClosed) {
        thr = new ForceReattemptException(LocalizedStrings.PartitionMessage_REGION_IS_DESTROYED_IN_0.toLocalizedString(dm.getDistributionManagerId()), rde);
    // }
    } catch (VirtualMachineError err) {
        SystemFailure.initiateFailure(err);
        // now, so don't let this thread continue.
        throw err;
    } catch (Throwable t) {
        // Whenever you catch Error or Throwable, you must also
        // catch VirtualMachineError (see above). However, there is
        // _still_ a possibility that you are dealing with a cascading
        // error condition, so you also need to check to see if the JVM
        // is still usable:
        SystemFailure.checkFailure();
        // log the exception at fine level if there is no reply to the message
        thr = null;
        if (sendReply) {
            if (!checkDSClosing(dm)) {
                thr = t;
            } else {
                // don't pass arbitrary runtime exceptions and errors back if this
                // cache/vm is closing
                thr = new ForceReattemptException(LocalizedStrings.PartitionMessage_DISTRIBUTED_SYSTEM_IS_DISCONNECTING.toLocalizedString());
            }
        }
        if (logger.isTraceEnabled(LogMarker.DM) && t instanceof RuntimeException) {
            logger.trace(LogMarker.DM, "Exception caught while processing message: {}", t.getMessage(), t);
        }
    } finally {
        if (sendReply) {
            ReplyException rex = null;
            if (thr != null) {
                // don't transmit the exception if this message was to a listener
                // and this listener is shutting down
                boolean excludeException = this.notificationOnly && ((thr instanceof CancelException) || (thr instanceof ForceReattemptException));
                if (!excludeException) {
                    rex = new ReplyException(thr);
                }
            }
            // Send the reply if the operateOnPartitionedRegion returned true
            sendReply(getSender(), this.processorId, dm, rex, pr, startTime);
            EntryLogger.clearSource();
        }
    }
}
Also used : DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) DataLocationException(org.apache.geode.internal.cache.DataLocationException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) InternalCache(org.apache.geode.internal.cache.InternalCache) CacheClosedException(org.apache.geode.cache.CacheClosedException) ReplyException(org.apache.geode.distributed.internal.ReplyException) ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) CancelException(org.apache.geode.CancelException)

Example 4 with TXManagerImpl

use of org.apache.geode.internal.cache.TXManagerImpl in project geode by apache.

the class TXFailoverCommand method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException, ClassNotFoundException, InterruptedException {
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    // Build the TXId for the transaction
    InternalDistributedMember client = (InternalDistributedMember) serverConnection.getProxyID().getDistributedMember();
    int uniqId = clientMessage.getTransactionId();
    if (logger.isDebugEnabled()) {
        logger.debug("TX: Transaction {} from {} is failing over to this server", uniqId, client);
    }
    TXId txId = new TXId(client, uniqId);
    TXManagerImpl mgr = (TXManagerImpl) serverConnection.getCache().getCacheTransactionManager();
    // in case it's already completing here in another
    mgr.waitForCompletingTransaction(txId);
    // thread
    if (mgr.isHostedTxRecentlyCompleted(txId)) {
        writeReply(clientMessage, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        mgr.removeHostedTXState(txId);
        return;
    }
    // fixes bug 43350
    boolean wasInProgress = mgr.setInProgress(true);
    TXStateProxy tx = mgr.getTXState();
    Assert.assertTrue(tx != null);
    if (!tx.isRealDealLocal()) {
        // send message to all peers to find out who hosts the transaction
        FindRemoteTXMessageReplyProcessor processor = FindRemoteTXMessage.send(serverConnection.getCache(), txId);
        try {
            processor.waitForRepliesUninterruptibly();
        } catch (ReplyException e) {
            e.handleAsUnexpected();
        }
        // if hosting member is not null, bootstrap PeerTXStateStub to that member
        // if hosting member is null, rebuild TXCommitMessage from partial TXCommitMessages
        InternalDistributedMember hostingMember = processor.getHostingMember();
        if (hostingMember != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("TX: txState is not local, bootstrapping PeerTXState stub for targetNode: {}", hostingMember);
            }
            // inject the real deal
            tx.setLocalTXState(new PeerTXStateStub(tx, hostingMember, client));
        } else {
            // bug #42228 and bug #43504 - this cannot return until the current view
            // has been installed by all members, so that dlocks are released and
            // the same keys can be used in a new transaction by the same client thread
            InternalCache cache = serverConnection.getCache();
            try {
                WaitForViewInstallation.send((DistributionManager) cache.getDistributionManager());
            } catch (InterruptedException e) {
                cache.getDistributionManager().getCancelCriterion().checkCancelInProgress(e);
                Thread.currentThread().interrupt();
            }
            // tx host has departed, rebuild the tx
            if (processor.getTxCommitMessage() != null) {
                if (logger.isDebugEnabled()) {
                    logger.debug("TX: for txId: {} rebuilt a recently completed tx", txId);
                }
                mgr.saveTXCommitMessageForClientFailover(txId, processor.getTxCommitMessage());
            } else {
                writeException(clientMessage, new TransactionDataNodeHasDepartedException("Could not find transaction host for " + txId), false, serverConnection);
                serverConnection.setAsTrue(RESPONDED);
                mgr.removeHostedTXState(txId);
                return;
            }
        }
    }
    if (!wasInProgress) {
        mgr.setInProgress(false);
    }
    writeReply(clientMessage, serverConnection);
    serverConnection.setAsTrue(RESPONDED);
}
Also used : TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) TXId(org.apache.geode.internal.cache.TXId) InternalCache(org.apache.geode.internal.cache.InternalCache) FindRemoteTXMessageReplyProcessor(org.apache.geode.internal.cache.FindRemoteTXMessage.FindRemoteTXMessageReplyProcessor) ReplyException(org.apache.geode.distributed.internal.ReplyException) PeerTXStateStub(org.apache.geode.internal.cache.PeerTXStateStub) TransactionDataNodeHasDepartedException(org.apache.geode.cache.TransactionDataNodeHasDepartedException)

Example 5 with TXManagerImpl

use of org.apache.geode.internal.cache.TXManagerImpl in project geode by apache.

the class PeerTypeRegistration method updateRegion.

private void updateRegion(Object k, Object v) {
    Region<Object, Object> r = getIdToType();
    InternalCache cache = (InternalCache) r.getRegionService();
    checkDistributedTypeRegistryState();
    TXManagerImpl txManager = (TXManagerImpl) cache.getCacheTransactionManager();
    TXStateProxy currentState = suspendTX();
    boolean state = useUDPMessagingIfNecessary();
    try {
        // The loop might not be necessary because we're
        // updating a replicated region inside a dlock,
        // but just in case we'll make sure to retry the transaction.
        int failureCount = 0;
        while (true) {
            txManager.begin();
            try {
                r.put(k, v);
                txManager.commit();
                return;
            } catch (TransactionException e) {
                // even put can now throw a TransactionException, rollback if required
                if (txManager.exists()) {
                    txManager.rollback();
                }
                // Let's just make sure things don't get out of hand.
                if (++failureCount > MAX_TRANSACTION_FAILURES) {
                    throw e;
                }
            }
        }
    } finally {
        releaseUDPMessaging(state);
        resumeTX(currentState);
    }
}
Also used : TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) TransactionException(org.apache.geode.cache.TransactionException) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) InternalCache(org.apache.geode.internal.cache.InternalCache)

Aggregations

TXManagerImpl (org.apache.geode.internal.cache.TXManagerImpl)31 TXStateProxy (org.apache.geode.internal.cache.TXStateProxy)20 Test (org.junit.Test)13 LocalRegion (org.apache.geode.internal.cache.LocalRegion)10 CommitConflictException (org.apache.geode.cache.CommitConflictException)8 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)8 Region (org.apache.geode.cache.Region)7 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)6 InternalCache (org.apache.geode.internal.cache.InternalCache)6 IOException (java.io.IOException)5 CacheException (org.apache.geode.cache.CacheException)5 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)5 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)5 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)5 EntryExistsException (org.apache.geode.cache.EntryExistsException)4 TransactionException (org.apache.geode.cache.TransactionException)4 TXId (org.apache.geode.internal.cache.TXId)4 AttributesFactory (org.apache.geode.cache.AttributesFactory)3 CacheWriterException (org.apache.geode.cache.CacheWriterException)3 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)3