Search in sources :

Example 16 with XaDataSource

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

the class MasterUtil method applyReceivedTransactions.

public static <T> void applyReceivedTransactions(Response<T> response, GraphDatabaseService graphDb, TxHandler txHandler) throws IOException {
    XaDataSourceManager dataSourceManager = ((AbstractGraphDatabase) graphDb).getConfig().getTxModule().getXaDataSourceManager();
    for (Triplet<String, Long, TxExtractor> tx : IteratorUtil.asIterable(response.transactions())) {
        String resourceName = tx.first();
        XaDataSource dataSource = dataSourceManager.getXaDataSource(resourceName);
        txHandler.accept(tx, dataSource);
        ReadableByteChannel txStream = tx.third().extract();
        try {
            dataSource.applyCommittedTransaction(tx.second(), txStream);
        } finally {
            txStream.close();
        }
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) XaDataSourceManager(org.neo4j.kernel.impl.transaction.XaDataSourceManager) AbstractGraphDatabase(org.neo4j.kernel.AbstractGraphDatabase) NeoStoreXaDataSource(org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource)

Example 17 with XaDataSource

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

the class MasterUtil method rotateLogsAndStreamStoreFiles.

public static SlaveContext rotateLogsAndStreamStoreFiles(GraphDatabaseService graphDb, StoreWriter writer) {
    File baseDir = getBaseDir(graphDb);
    XaDataSourceManager dsManager = ((AbstractGraphDatabase) graphDb).getConfig().getTxModule().getXaDataSourceManager();
    Collection<XaDataSource> sources = dsManager.getAllRegisteredDataSources();
    @SuppressWarnings("unchecked") Pair<String, Long>[] appliedTransactions = new Pair[sources.size()];
    int i = 0;
    for (XaDataSource ds : sources) {
        appliedTransactions[i++] = Pair.of(ds.getName(), ds.getLastCommittedTxId());
        try {
            ds.getXaContainer().getResourceManager().rotateLogicalLog();
        } catch (IOException e) {
            // TODO: what about error message?
            throw new MasterFailureException(e);
        }
    }
    SlaveContext context = new SlaveContext(-1, -1, appliedTransactions);
    ByteBuffer temporaryBuffer = ByteBuffer.allocateDirect(1024 * 1024);
    for (XaDataSource ds : sources) {
        try {
            ClosableIterable<File> files = ds.listStoreFiles();
            try {
                for (File storefile : files) {
                    FileInputStream stream = new FileInputStream(storefile);
                    try {
                        writer.write(relativePath(baseDir, storefile), stream.getChannel(), temporaryBuffer, storefile.length() > 0);
                    } finally {
                        stream.close();
                    }
                }
            } finally {
                files.close();
            }
        } catch (IOException e) {
            // TODO: what about error message?
            throw new MasterFailureException(e);
        }
    }
    return context;
}
Also used : XaDataSourceManager(org.neo4j.kernel.impl.transaction.XaDataSourceManager) IOException(java.io.IOException) NeoStoreXaDataSource(org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream) AbstractGraphDatabase(org.neo4j.kernel.AbstractGraphDatabase) File(java.io.File) Pair(org.neo4j.helpers.Pair)

Example 18 with XaDataSource

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

the class MasterUtil method packResponse.

public static <T> Response<T> packResponse(GraphDatabaseService graphDb, SlaveContext context, T response, Predicate<Long> filter) {
    List<Triplet<String, Long, TxExtractor>> stream = new ArrayList<Triplet<String, Long, TxExtractor>>();
    Set<String> resourceNames = new HashSet<String>();
    XaDataSourceManager dsManager = ((AbstractGraphDatabase) graphDb).getConfig().getTxModule().getXaDataSourceManager();
    for (Pair<String, Long> txEntry : context.lastAppliedTransactions()) {
        String resourceName = txEntry.first();
        final XaDataSource dataSource = dsManager.getXaDataSource(resourceName);
        if (dataSource == null) {
            throw new RuntimeException("No data source '" + resourceName + "' found");
        }
        resourceNames.add(resourceName);
        long masterLastTx = dataSource.getLastCommittedTxId();
        for (long txId = txEntry.other() + 1; txId <= masterLastTx; txId++) {
            if (filter.accept(txId)) {
                final long tx = txId;
                TxExtractor extractor = new TxExtractor() {

                    @Override
                    public ReadableByteChannel extract() {
                        try {
                            return dataSource.getCommittedTransaction(tx);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    @Override
                    public void extract(LogBuffer buffer) {
                        try {
                            dataSource.getCommittedTransaction(tx, buffer);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                };
                stream.add(Triplet.of(resourceName, txId, extractor));
            }
        }
    }
    StoreId storeId = ((NeoStoreXaDataSource) dsManager.getXaDataSource(Config.DEFAULT_DATA_SOURCE_NAME)).getStoreId();
    return new Response<T>(response, storeId, TransactionStream.create(resourceNames, stream));
}
Also used : XaDataSourceManager(org.neo4j.kernel.impl.transaction.XaDataSourceManager) Triplet(org.neo4j.helpers.Triplet) ArrayList(java.util.ArrayList) IOException(java.io.IOException) NeoStoreXaDataSource(org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource) LogBuffer(org.neo4j.kernel.impl.transaction.xaframework.LogBuffer) StoreId(org.neo4j.kernel.impl.nioneo.store.StoreId) NeoStoreXaDataSource(org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource) AbstractGraphDatabase(org.neo4j.kernel.AbstractGraphDatabase) HashSet(java.util.HashSet)

Example 19 with XaDataSource

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

the class XaDataSourceManager method unregisterDataSource.

/**
     * Public for testing purpose. Do not use.
     */
public synchronized void unregisterDataSource(String name) {
    XaDataSource dataSource = dataSources.get(name);
    byte[] branchId = getBranchId(dataSource.getXaConnection().getXaResource());
    dataSources.remove(name);
    branchIdMapping.remove(UTF8.decode(branchId));
    sourceIdMapping.remove(name);
    dataSource.close();
}
Also used : XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource)

Example 20 with XaDataSource

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

the class TxModule method registerDataSource.

public XaDataSource registerDataSource(String dsName, String className, byte[] resourceId, Map<?, ?> params, boolean useExisting) {
    XaDataSourceManager xaDsMgr = xaDsManager;
    String name = dsName.toLowerCase();
    if (xaDsMgr.hasDataSource(name)) {
        if (useExisting) {
            return xaDsMgr.getXaDataSource(name);
        }
        throw new TransactionFailureException("Data source[" + name + "] has already been registered");
    }
    try {
        XaDataSource dataSource = xaDsMgr.create(className, params);
        xaDsMgr.registerDataSource(name, dataSource, resourceId);
        return dataSource;
    } catch (Exception e) {
        throw new TransactionFailureException("Could not create data source " + name + "[" + name + "]", e);
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException)

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