Search in sources :

Example 6 with XaDataSource

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

the class TestXaFramework method testCreateXaResource.

@Test
public void testCreateXaResource() throws Exception {
    Map<Object, Object> config = new HashMap<Object, Object>();
    config.put("store_dir", "target/var");
    config.put(LogBufferFactory.class, CommonFactories.defaultLogBufferFactory(config));
    xaDsMgr.registerDataSource("dummy_datasource", new DummyXaDataSource(config), UTF8.encode("DDDDDD"));
    XaDataSource xaDs = xaDsMgr.getXaDataSource("dummy_datasource");
    DummyXaConnection xaC = null;
    try {
        xaC = (DummyXaConnection) xaDs.getXaConnection();
        try {
            xaC.doStuff1();
            fail("Non enlisted resource should throw exception");
        } catch (XAException e) {
        // good
        }
        Xid xid = new XidImpl(new byte[0], new byte[0]);
        xaC.getXaResource().start(xid, XAResource.TMNOFLAGS);
        try {
            xaC.doStuff1();
            xaC.doStuff2();
        } catch (XAException e) {
            fail("Enlisted resource should not throw exception");
        }
        xaC.getXaResource().end(xid, XAResource.TMSUCCESS);
        xaC.getXaResource().prepare(xid);
        xaC.getXaResource().commit(xid, false);
    } finally {
        xaDsMgr.unregisterDataSource("dummy_datasource");
        if (xaC != null) {
            xaC.destroy();
        }
    }
    // cleanup dummy resource log
    File dir = new File(".");
    File[] files = dir.listFiles(new FilenameFilter() {

        public boolean accept(File dir, String fileName) {
            return fileName.startsWith(resourceFile());
        }
    });
    for (int i = 0; i < files.length; i++) {
        files[i].delete();
    }
}
Also used : XAException(javax.transaction.xa.XAException) HashMap(java.util.HashMap) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource) FilenameFilter(java.io.FilenameFilter) Xid(javax.transaction.xa.Xid) File(java.io.File) Test(org.junit.Test)

Example 7 with XaDataSource

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

the class HighlyAvailableGraphDatabase method tryToEnsureIAmNotABrokenMachine.

private void tryToEnsureIAmNotABrokenMachine(Pair<Master, Machine> master) {
    if (master.other().getMachineId() == machineId) {
        return;
    } else if (master.first() == null) {
        // Temporarily disconnected from ZK
        RuntimeException cause = new RuntimeException("Unable to get master from ZK");
        shutdown(cause, false);
        throw cause;
    }
    XaDataSource nioneoDataSource = getConfig().getTxModule().getXaDataSourceManager().getXaDataSource(Config.DEFAULT_DATA_SOURCE_NAME);
    long myLastCommittedTx = nioneoDataSource.getLastCommittedTxId();
    long highestCommonTxId = Math.min(myLastCommittedTx, master.other().getLastCommittedTxId());
    int masterForMyHighestCommonTxId = -1;
    try {
        masterForMyHighestCommonTxId = nioneoDataSource.getMasterForCommittedTx(highestCommonTxId);
        ;
    } catch (IOException e) {
        // This is quite dangerous to just catch here... but the special case is
        // where this db was just now copied from the master where there's data,
        // but no logical logs were transfered and hence no masterId info is here
        msgLog.logMessage("Couldn't get master ID for txId " + highestCommonTxId + ". It may be that a log file is missing due to the db being copied from master?", e);
        return;
    }
    int masterForMastersHighestCommonTxId = master.first().getMasterIdForCommittedTx(highestCommonTxId).response();
    // Compare those two, if equal -> good
    if (masterForMyHighestCommonTxId == masterForMastersHighestCommonTxId) {
        msgLog.logMessage("Master id for last committed tx ok with highestCommonTxId=" + highestCommonTxId + " with masterId=" + masterForMyHighestCommonTxId, true);
        return;
    } else {
        String msg = "Broken store, I (machineId:" + machineId + ") think machineId for txId (" + highestCommonTxId + ") is " + masterForMyHighestCommonTxId + ", but master (machineId:" + master.other().getMachineId() + ") says that it's " + masterForMastersHighestCommonTxId;
        msgLog.logMessage(msg, true);
        RuntimeException exception = new BranchedDataException(msg);
        shutdown(exception, false);
        throw exception;
    }
}
Also used : IOException(java.io.IOException) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource) BranchedDataException(org.neo4j.kernel.ha.BranchedDataException)

Example 8 with XaDataSource

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

the class HighlyAvailableGraphDatabase method getSlaveContext.

@Override
public SlaveContext getSlaveContext(int eventIdentifier) {
    XaDataSourceManager localDataSourceManager = getConfig().getTxModule().getXaDataSourceManager();
    Collection<XaDataSource> dataSources = localDataSourceManager.getAllRegisteredDataSources();
    @SuppressWarnings("unchecked") Pair<String, Long>[] txs = new Pair[dataSources.size()];
    int i = 0;
    for (XaDataSource dataSource : dataSources) {
        txs[i++] = Pair.of(dataSource.getName(), dataSource.getLastCommittedTxId());
    }
    return new SlaveContext(machineId, eventIdentifier, txs);
}
Also used : SlaveContext(org.neo4j.com.SlaveContext) XaDataSourceManager(org.neo4j.kernel.impl.transaction.XaDataSourceManager) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource) Pair(org.neo4j.helpers.Pair)

Example 9 with XaDataSource

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

the class Client method getMyStoreId.

protected StoreId getMyStoreId() {
    if (myStoreId == null) {
        XaDataSource ds = ((AbstractGraphDatabase) graphDb).getConfig().getTxModule().getXaDataSourceManager().getXaDataSource(Config.DEFAULT_DATA_SOURCE_NAME);
        myStoreId = ((NeoStoreXaDataSource) ds).getStoreId();
    }
    return myStoreId;
}
Also used : NeoStoreXaDataSource(org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource)

Example 10 with XaDataSource

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

the class MasterUtil method packResponseWithoutTransactionStream.

public static <T> Response<T> packResponseWithoutTransactionStream(GraphDatabaseService graphDb, SlaveContext context, T response) {
    XaDataSource ds = ((AbstractGraphDatabase) graphDb).getConfig().getTxModule().getXaDataSourceManager().getXaDataSource(Config.DEFAULT_DATA_SOURCE_NAME);
    StoreId storeId = ((NeoStoreXaDataSource) ds).getStoreId();
    return new Response<T>(response, storeId, TransactionStream.EMPTY);
}
Also used : StoreId(org.neo4j.kernel.impl.nioneo.store.StoreId) NeoStoreXaDataSource(org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource) NeoStoreXaDataSource(org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource)

Aggregations

XaDataSource (org.neo4j.kernel.impl.transaction.xaframework.XaDataSource)22 NeoStoreXaDataSource (org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource)7 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)6 XaDataSourceManager (org.neo4j.kernel.impl.transaction.XaDataSourceManager)6 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Pair (org.neo4j.helpers.Pair)4 AbstractGraphDatabase (org.neo4j.kernel.AbstractGraphDatabase)4 ArrayList (java.util.ArrayList)3 XAException (javax.transaction.xa.XAException)3 SlaveContext (org.neo4j.com.SlaveContext)3 File (java.io.File)2 Map (java.util.Map)2 XAResource (javax.transaction.xa.XAResource)2 Test (org.junit.Test)2 StoreId (org.neo4j.kernel.impl.nioneo.store.StoreId)2 XaResource (org.neo4j.kernel.impl.transaction.xaframework.XaResource)2 FileInputStream (java.io.FileInputStream)1 FilenameFilter (java.io.FilenameFilter)1 ByteBuffer (java.nio.ByteBuffer)1