Search in sources :

Example 1 with XaResource

use of org.neo4j.kernel.impl.transaction.xaframework.XaResource in project graphdb by neo4j-attic.

the class TxManager method recover.

private void recover(Iterator<List<TxLog.Record>> danglingRecordList) {
    msgLog.logMessage("TM non resolved transactions found in " + txLog.getName(), true);
    try {
        // contains NonCompletedTransaction that needs to be committed
        List<NonCompletedTransaction> commitList = new ArrayList<NonCompletedTransaction>();
        // contains Xids that should be rolledback
        List<Xid> rollbackList = new LinkedList<Xid>();
        // key = Resource(branchId) value = XAResource
        Map<Resource, XAResource> resourceMap = new HashMap<Resource, XAResource>();
        buildRecoveryInfo(commitList, rollbackList, resourceMap, danglingRecordList);
        // invoke recover on all xa resources found
        Iterator<Resource> resourceItr = resourceMap.keySet().iterator();
        List<Xid> recoveredXidsList = new LinkedList<Xid>();
        while (resourceItr.hasNext()) {
            XAResource xaRes = resourceMap.get(resourceItr.next());
            Xid[] xids = xaRes.recover(XAResource.TMNOFLAGS);
            for (int i = 0; i < xids.length; i++) {
                if (XidImpl.isThisTm(xids[i].getGlobalTransactionId())) {
                    // linear search
                    if (rollbackList.contains(xids[i])) {
                        log.fine("Found pre commit " + xids[i] + " rolling back ... ");
                        msgLog.logMessage("TM: Found pre commit " + xids[i] + " rolling back ... ", true);
                        rollbackList.remove(xids[i]);
                        xaRes.rollback(xids[i]);
                    } else {
                        recoveredXidsList.add(xids[i]);
                    }
                } else {
                    log.warning("Unknown xid: " + xids[i]);
                }
            }
        }
        // sort the commit list after sequence number
        Collections.sort(commitList, new Comparator<NonCompletedTransaction>() {

            public int compare(NonCompletedTransaction r1, NonCompletedTransaction r2) {
                return r1.getSequenceNumber() - r2.getSequenceNumber();
            }
        });
        // go through and commit
        Iterator<NonCompletedTransaction> commitItr = commitList.iterator();
        while (commitItr.hasNext()) {
            NonCompletedTransaction nct = commitItr.next();
            int seq = nct.getSequenceNumber();
            Xid[] xids = nct.getXids();
            log.fine("Marked as commit tx-seq[" + seq + "] branch length: " + xids.length);
            for (int i = 0; i < xids.length; i++) {
                if (!recoveredXidsList.contains(xids[i])) {
                    log.fine("Tx-seq[" + seq + "][" + xids[i] + "] not found in recovered xid list, " + "assuming already committed");
                    continue;
                }
                recoveredXidsList.remove(xids[i]);
                Resource resource = new Resource(xids[i].getBranchQualifier());
                if (!resourceMap.containsKey(resource)) {
                    throw new TransactionFailureException("Couldn't find XAResource for " + xids[i]);
                }
                log.fine("Commiting tx seq[" + seq + "][" + xids[i] + "] ... ");
                msgLog.logMessage("TM: Committing tx " + xids[i], true);
                resourceMap.get(resource).commit(xids[i], false);
            }
        }
        // rollback the rest
        Iterator<Xid> rollbackItr = recoveredXidsList.iterator();
        while (rollbackItr.hasNext()) {
            Xid xid = rollbackItr.next();
            Resource resource = new Resource(xid.getBranchQualifier());
            if (!resourceMap.containsKey(resource)) {
                throw new TransactionFailureException("Couldn't find XAResource for " + xid);
            }
            log.fine("Rollback " + xid + " ... ");
            msgLog.logMessage("TM: no match found for " + xid + " removing", true);
            resourceMap.get(resource).rollback(xid);
        }
        if (rollbackList.size() > 0) {
            log.fine("TxLog contained unresolved " + "xids that needed rollback. They couldn't be matched to " + "any of the XAResources recover list. " + "Assuming " + rollbackList.size() + " transactions already rolled back.");
            msgLog.logMessage("TM: no match found for in total " + rollbackList.size() + " transaction that should have been rolled back", true);
        }
    } catch (XAException e) {
        throw new TransactionFailureException("Recovery failed." + e);
    }
}
Also used : XAException(javax.transaction.xa.XAException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) XAResource(javax.transaction.xa.XAResource) XaResource(org.neo4j.kernel.impl.transaction.xaframework.XaResource) LinkedList(java.util.LinkedList) Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException)

Example 2 with XaResource

use of org.neo4j.kernel.impl.transaction.xaframework.XaResource in project graphdb by neo4j-attic.

the class TxManager method buildRecoveryInfo.

private void buildRecoveryInfo(List<NonCompletedTransaction> commitList, List<Xid> rollbackList, Map<Resource, XAResource> resourceMap, Iterator<List<TxLog.Record>> danglingRecordList) {
    while (danglingRecordList.hasNext()) {
        Iterator<TxLog.Record> dListItr = danglingRecordList.next().iterator();
        TxLog.Record startRecord = dListItr.next();
        if (startRecord.getType() != TxLog.TX_START) {
            throw new TransactionFailureException("First record not a start record, type=" + startRecord.getType());
        }
        // get branches & commit status
        HashSet<Resource> branchSet = new HashSet<Resource>();
        int markedCommit = -1;
        while (dListItr.hasNext()) {
            TxLog.Record record = dListItr.next();
            if (record.getType() == TxLog.BRANCH_ADD) {
                if (markedCommit != -1) {
                    throw new TransactionFailureException("Already marked commit " + startRecord);
                }
                branchSet.add(new Resource(record.getBranchId()));
            } else if (record.getType() == TxLog.MARK_COMMIT) {
                if (markedCommit != -1) {
                    throw new TransactionFailureException("Already marked commit " + startRecord);
                }
                markedCommit = record.getSequenceNumber();
            } else {
                throw new TransactionFailureException("Illegal record type[" + record.getType() + "]");
            }
        }
        Iterator<Resource> resourceItr = branchSet.iterator();
        List<Xid> xids = new LinkedList<Xid>();
        while (resourceItr.hasNext()) {
            Resource resource = resourceItr.next();
            if (!resourceMap.containsKey(resource)) {
                resourceMap.put(resource, getXaResource(resource.getResourceId()));
            }
            xids.add(new XidImpl(startRecord.getGlobalId(), resource.getResourceId()));
        }
        if (// this xid needs to be committed
        markedCommit != -1) {
            commitList.add(new NonCompletedTransaction(markedCommit, xids));
        } else {
            rollbackList.addAll(xids);
        }
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) XaResource(org.neo4j.kernel.impl.transaction.xaframework.XaResource) LinkedList(java.util.LinkedList) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Xid(javax.transaction.xa.Xid) HashSet(java.util.HashSet)

Example 3 with XaResource

use of org.neo4j.kernel.impl.transaction.xaframework.XaResource in project neo4j-mobile-android by neo4j-contrib.

the class TxManager method buildRecoveryInfo.

private void buildRecoveryInfo(List<NonCompletedTransaction> commitList, List<Xid> rollbackList, Map<Resource, XAResource> resourceMap, Iterator<List<TxLog.Record>> danglingRecordList) {
    while (danglingRecordList.hasNext()) {
        Iterator<TxLog.Record> dListItr = danglingRecordList.next().iterator();
        TxLog.Record startRecord = dListItr.next();
        if (startRecord.getType() != TxLog.TX_START) {
            throw logAndReturn("TM error building recovery info", new TransactionFailureException("First record not a start record, type=" + startRecord.getType()));
        }
        // get branches & commit status
        HashSet<Resource> branchSet = new HashSet<Resource>();
        int markedCommit = -1;
        while (dListItr.hasNext()) {
            TxLog.Record record = dListItr.next();
            if (record.getType() == TxLog.BRANCH_ADD) {
                if (markedCommit != -1) {
                    throw logAndReturn("TM error building recovery info", new TransactionFailureException("Already marked commit " + startRecord));
                }
                branchSet.add(new Resource(record.getBranchId()));
            } else if (record.getType() == TxLog.MARK_COMMIT) {
                if (markedCommit != -1) {
                    throw logAndReturn("TM error building recovery info", new TransactionFailureException("Already marked commit " + startRecord));
                }
                markedCommit = record.getSequenceNumber();
            } else {
                throw logAndReturn("TM error building recovery info", new TransactionFailureException("Illegal record type[" + record.getType() + "]"));
            }
        }
        Iterator<Resource> resourceItr = branchSet.iterator();
        List<Xid> xids = new LinkedList<Xid>();
        while (resourceItr.hasNext()) {
            Resource resource = resourceItr.next();
            if (!resourceMap.containsKey(resource)) {
                resourceMap.put(resource, getXaResource(resource.getResourceId()));
            }
            xids.add(new XidImpl(startRecord.getGlobalId(), resource.getResourceId()));
        }
        if (// this xid needs to be committed
        markedCommit != -1) {
            commitList.add(new NonCompletedTransaction(markedCommit, xids));
        } else {
            rollbackList.addAll(xids);
        }
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) XaResource(org.neo4j.kernel.impl.transaction.xaframework.XaResource) LinkedList(java.util.LinkedList) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Xid(javax.transaction.xa.Xid) HashSet(java.util.HashSet)

Example 4 with XaResource

use of org.neo4j.kernel.impl.transaction.xaframework.XaResource in project neo4j-mobile-android by neo4j-contrib.

the class XaDataSourceManager method getBranchId.

synchronized byte[] getBranchId(XAResource xaResource) {
    if (xaResource instanceof XaResource) {
        byte[] branchId = ((XaResource) xaResource).getBranchId();
        if (branchId != null) {
            return branchId;
        }
    }
    Iterator<Map.Entry<String, XaDataSource>> itr = dataSources.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry<String, XaDataSource> entry = itr.next();
        XaDataSource dataSource = entry.getValue();
        XAResource resource = dataSource.getXaConnection().getXaResource();
        try {
            if (resource.isSameRM(xaResource)) {
                String name = entry.getKey();
                return sourceIdMapping.get(name);
            }
        } catch (XAException e) {
            throw new TransactionFailureException("Unable to check is same resource", e);
        }
    }
    throw new TransactionFailureException("Unable to find mapping for XAResource[" + xaResource + "]");
}
Also used : XAResource(javax.transaction.xa.XAResource) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) XAException(javax.transaction.xa.XAException) XaResource(org.neo4j.kernel.impl.transaction.xaframework.XaResource) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with XaResource

use of org.neo4j.kernel.impl.transaction.xaframework.XaResource in project graphdb by neo4j-attic.

the class XaDataSourceManager method getBranchId.

synchronized byte[] getBranchId(XAResource xaResource) {
    if (xaResource instanceof XaResource) {
        byte[] branchId = ((XaResource) xaResource).getBranchId();
        if (branchId != null) {
            return branchId;
        }
    }
    Iterator<Map.Entry<String, XaDataSource>> itr = dataSources.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry<String, XaDataSource> entry = itr.next();
        XaDataSource dataSource = entry.getValue();
        XAResource resource = dataSource.getXaConnection().getXaResource();
        try {
            if (resource.isSameRM(xaResource)) {
                String name = entry.getKey();
                return sourceIdMapping.get(name);
            }
        } catch (XAException e) {
            throw new TransactionFailureException("Unable to check is same resource", e);
        }
    }
    throw new TransactionFailureException("Unable to find mapping for XAResource[" + xaResource + "]");
}
Also used : XAResource(javax.transaction.xa.XAResource) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) XAException(javax.transaction.xa.XAException) XaResource(org.neo4j.kernel.impl.transaction.xaframework.XaResource) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

XAResource (javax.transaction.xa.XAResource)6 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)6 XaResource (org.neo4j.kernel.impl.transaction.xaframework.XaResource)6 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)4 XAException (javax.transaction.xa.XAException)4 Xid (javax.transaction.xa.Xid)4 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 XaDataSource (org.neo4j.kernel.impl.transaction.xaframework.XaDataSource)2 IOException (java.io.IOException)1