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;
}
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]));
}
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();
}
}
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();
}
}
}
};
}
Aggregations