Search in sources :

Example 6 with Pair

use of org.neo4j.helpers.Pair 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 7 with Pair

use of org.neo4j.helpers.Pair in project graphdb by neo4j-attic.

the class MasterImpl method makeSureThereIsAtLeastOneKernelTx.

private SlaveContext makeSureThereIsAtLeastOneKernelTx(SlaveContext context) {
    Collection<Pair<String, Long>> txs = new ArrayList<Pair<String, Long>>();
    for (Pair<String, Long> txEntry : context.lastAppliedTransactions()) {
        String resourceName = txEntry.first();
        XaDataSource dataSource = graphDbConfig.getTxModule().getXaDataSourceManager().getXaDataSource(resourceName);
        if (dataSource instanceof NeoStoreXaDataSource) {
            if (txEntry.other() == 1 || txEntry.other() < dataSource.getLastCommittedTxId()) {
                // copying
                return context;
            }
            // Put back slave one tx so that it gets one transaction
            txs.add(Pair.of(resourceName, dataSource.getLastCommittedTxId() - 1));
        //                System.out.println( "Pushed in one extra tx " + dataSource.getLastCommittedTxId() );
        } else {
            txs.add(Pair.of(resourceName, dataSource.getLastCommittedTxId()));
        }
    }
    return new SlaveContext(context.machineId(), context.getEventIdentifier(), txs.toArray(new Pair[0]));
}
Also used : SlaveContext(org.neo4j.com.SlaveContext) ArrayList(java.util.ArrayList) NeoStoreXaDataSource(org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource) NeoStoreXaDataSource(org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource) Pair(org.neo4j.helpers.Pair)

Example 8 with Pair

use of org.neo4j.helpers.Pair in project graphdb by neo4j-attic.

the class ZooClient method process.

public void process(WatchedEvent event) {
    try {
        String path = event.getPath();
        msgLog.logMessage(this + ", " + new Date() + " Got event: " + event + "(path=" + path + ")", true);
        if (path == null && event.getState() == Watcher.Event.KeeperState.Expired) {
            keeperState = KeeperState.Expired;
            if (zooKeeper != null) {
                try {
                    zooKeeper.close();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Thread.interrupted();
                }
            }
            zooKeeper = instantiateZooKeeper();
        } else if (path == null && event.getState() == Watcher.Event.KeeperState.SyncConnected) {
            long newSessionId = zooKeeper.getSessionId();
            Pair<Master, Machine> masterBeforeIWrite = getMasterFromZooKeeper(false);
            msgLog.logMessage("Get master before write:" + masterBeforeIWrite);
            boolean masterBeforeIWriteDiffers = masterBeforeIWrite.other().getMachineId() != getCachedMaster().other().getMachineId();
            if (newSessionId != sessionId || masterBeforeIWriteDiffers) {
                sequenceNr = setup();
                msgLog.logMessage("Did setup, seq=" + sequenceNr + " new sessionId=" + newSessionId);
                keeperState = KeeperState.SyncConnected;
                Pair<Master, Machine> masterAfterIWrote = getMasterFromZooKeeper(false);
                msgLog.logMessage("Get master after write:" + masterAfterIWrote);
                int masterId = masterAfterIWrote.other().getMachineId();
                msgLog.logMessage("Setting '" + MASTER_NOTIFY_CHILD + "' to " + masterId);
                setDataChangeWatcher(MASTER_NOTIFY_CHILD, masterId);
                msgLog.logMessage("Did set '" + MASTER_NOTIFY_CHILD + "' to " + masterId);
                if (sessionId != -1) {
                    receiver.newMaster(masterAfterIWrote, new Exception());
                }
                sessionId = newSessionId;
            } else {
                msgLog.logMessage("SyncConnected with same session id: " + sessionId);
                keeperState = KeeperState.SyncConnected;
            }
        } else if (path == null && event.getState() == Watcher.Event.KeeperState.Disconnected) {
            keeperState = KeeperState.Disconnected;
        } else if (event.getType() == Watcher.Event.EventType.NodeDataChanged) {
            Pair<Master, Machine> currentMaster = getMasterFromZooKeeper(true);
            if (path.contains(MASTER_NOTIFY_CHILD)) {
                setDataChangeWatcher(MASTER_NOTIFY_CHILD, -1);
                if (currentMaster.other().getMachineId() == machineId) {
                    receiver.newMaster(currentMaster, new Exception());
                }
            } else if (path.contains(MASTER_REBOUND_CHILD)) {
                setDataChangeWatcher(MASTER_REBOUND_CHILD, -1);
                if (currentMaster.other().getMachineId() != machineId) {
                    receiver.newMaster(currentMaster, new Exception());
                }
            } else {
                msgLog.logMessage("Unrecognized data change " + path);
            }
        }
    } catch (RuntimeException e) {
        msgLog.logMessage("Error in ZooClient.process", e, true);
        e.printStackTrace();
        throw e;
    } finally {
        msgLog.flush();
    }
}
Also used : Date(java.util.Date) KeeperException(org.apache.zookeeper.KeeperException) BufferUnderflowException(java.nio.BufferUnderflowException) Pair(org.neo4j.helpers.Pair)

Example 9 with Pair

use of org.neo4j.helpers.Pair in project neo4j-mobile-android by neo4j-contrib.

the class LuceneDataSource method listStoreFiles.

@Override
public ClosableIterable<File> listStoreFiles(boolean includeLogicalLogs) throws IOException {
    // Never include logical logs since they are of little importance
    final Collection<File> files = new ArrayList<File>();
    final Collection<SnapshotDeletionPolicy> snapshots = new ArrayList<SnapshotDeletionPolicy>();
    makeSureAllIndexesAreInstantiated();
    for (Map.Entry<IndexIdentifier, Pair<IndexWriter, AtomicBoolean>> writer : indexWriters.entrySet()) {
        SnapshotDeletionPolicy deletionPolicy = (SnapshotDeletionPolicy) writer.getValue().first().getConfig().getIndexDeletionPolicy();
        File indexDirectory = getFileDirectory(baseStorePath, writer.getKey());
        try {
            // Throws IllegalStateException if no commits yet
            IndexCommit commit = deletionPolicy.snapshot(SNAPSHOT_ID);
            for (String fileName : commit.getFileNames()) {
                files.add(new File(indexDirectory, fileName));
            }
            snapshots.add(deletionPolicy);
        } catch (IllegalStateException e) {
        // TODO Review this
        /*
                 * This is insane but happens if we try to snapshot an existing index
                 * that has no commits. This is a bad API design - it should return null
                 * or something. This is not exceptional.
                 */
        }
    }
    files.add(providerStore.getFile());
    return new ClosableIterable<File>() {

        public Iterator<File> iterator() {
            return files.iterator();
        }

        public void close() {
            for (SnapshotDeletionPolicy deletionPolicy : snapshots) {
                try {
                    deletionPolicy.release(SNAPSHOT_ID);
                } catch (IOException e) {
                    // TODO What to do?
                    e.printStackTrace();
                }
            }
        }
    };
}
Also used : ArrayList(java.util.ArrayList) ClosableIterable(org.neo4j.helpers.collection.ClosableIterable) IOException(java.io.IOException) SnapshotDeletionPolicy(org.apache.lucene.index.SnapshotDeletionPolicy) IndexCommit(org.apache.lucene.index.IndexCommit) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) Pair(org.neo4j.helpers.Pair)

Aggregations

Pair (org.neo4j.helpers.Pair)9 SlaveContext (org.neo4j.com.SlaveContext)4 XaDataSource (org.neo4j.kernel.impl.transaction.xaframework.XaDataSource)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 XaDataSourceManager (org.neo4j.kernel.impl.transaction.XaDataSourceManager)3 File (java.io.File)2 AbstractGraphDatabase (org.neo4j.kernel.AbstractGraphDatabase)2 NeoStoreXaDataSource (org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource)2 FileInputStream (java.io.FileInputStream)1 BufferUnderflowException (java.nio.BufferUnderflowException)1 ByteBuffer (java.nio.ByteBuffer)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 IndexCommit (org.apache.lucene.index.IndexCommit)1 SnapshotDeletionPolicy (org.apache.lucene.index.SnapshotDeletionPolicy)1 KeeperException (org.apache.zookeeper.KeeperException)1 Test (org.junit.Test)1 ToFileStoreWriter (org.neo4j.com.ToFileStoreWriter)1