public static Journal create(Location location) { BufferChannel chan ; String channelName = journalFilename(location) ; if ( location.isMem() ) chan = BufferChannelMem.create(channelName) ; else chan = BufferChannelFile.create(channelName) ; return new Journal(chan) ; }
public static void print(Journal journal) { System.out.println("Size: "+journal.size()) ; Iterator<JournalEntry> iter = journal.entries() ; for ( ; ; ) { long posn0 = journal.position(); if ( ! iter.hasNext() ) break; JournalEntry e = iter.next() ; long posn1 = journal.position(); long size = posn1 - posn0; System.out.printf("Posn: (%d, %d) Len=%d reverse %d\n", posn0, posn1, size, (journal.size()-journal.position())) ; System.out.print(" "); System.out.println(JournalEntry.format(e)) ; } }
@Test public void journal_04() { JournalEntry entry1 = new JournalEntry(JournalEntryType.Object, testRef, bb1) ; JournalEntry entry2 = new JournalEntry(JournalEntryType.Object, testRef, bb3) ; long x1 = journal.writeJournal(entry1) ; long x2 = journal.writeJournal(entry2) ; Iterator<JournalEntry> iter = journal.entries() ; JournalEntry entry1a = iter.next(); JournalEntry entry2a = iter.next(); assertFalse(iter.hasNext()) ; }
/** Scan to a commit entry, starting at a given position in the journal. * Return address of entry after commit if found, else -1. */ private static long scanForCommit(Journal jrnl, long startPosn) { Iterator<JournalEntry> iter = jrnl.entries(startPosn) ; try { for ( ; iter.hasNext() ; ) { JournalEntry e = iter.next() ; if ( e.getType() == JournalEntryType.Commit ) return e.getEndPosition() ; } return -1 ; } finally { Iter.close(iter) ; } }
/** Recover a base storage DatasetGraph */ public static void recovery(DatasetGraphTDB dsg) { if ( dsg.getLocation().isMem() ) return ; // Do we need to recover? Journal journal = findJournal(dsg) ; if ( journal == null || journal.isEmpty() ) return ; recoverFromJournal(dsg.getConfig(), journal) ; journal.close(); // Recovery complete. Tidy up. Node journal files have already been handled. if ( journal.getFilename() != null ) { if ( FileOps.exists(journal.getFilename()) ) FileOps.delete(journal.getFilename()) ; } }
/** Dump a journal - debug support function - opens the journal specially - inconsistent views possible */ public static void print(String filename) { BufferChannelFile chan = BufferChannelFile.createUnmanaged(filename, "r") ; Journal journal = new Journal(chan) ; JournalControl.print(journal) ; chan.close() ; }
public TransactionManager(DatasetGraphTDB dsg){ this.baseDataset = dsg ; this.journal = Journal.create(dsg.getLocation()) ; // LATER // Committer c = new Committer() ; // this.committerThread = new Thread(c) ; // committerThread.setDaemon(true) ; // committerThread.start() ; }
synchronized public JournalEntry readJournal(long id) { return _readJournal(id) ; }
synchronized public void closedown() { processDelayedReplayQueue(null) ; journal.close() ; }
public static boolean exists(Location location) { if ( location.isMem() ) return false ; return FileOps.exists(journalFilename(location)) ; }
private JournalEntry _readJournal(long id) { long x = channel.position() ; if ( x != id ) channel.position(id) ; JournalEntry entry = _read() ; long x2 = channel.position() ; entry.setPosition(id) ; entry.setEndPosition(x2) ; if ( x != id ) channel.position(x) ; return entry ; }
/** Stop managing a location. Use with great care (testing only). */ public static synchronized void expel(Location location, boolean force) { StoreConnection sConn = cache.get(location) ; if (sConn == null) return ; if (!force && sConn.transactionManager.activeTransactions()) throw new TDBTransactionException("Can't expel: Active transactions for location: " + location) ; // No transactions at this point (or we don't care and are clearing up forcefully.) sConn.transactionManager.closedown() ; sConn.baseDSG.close() ; sConn.isValid = false ; cache.remove(location) ; ChannelManager.release(sConn.transactionManager.getJournal().getFilename()) ; // Release the lock if (SystemTDB.DiskLocationMultiJvmUsagePrevention) { if (location.getLock().isOwned()) { location.getLock().release(); } else if (location.getLock().canLock()) { SystemTDB.errlog.warn("Location " + location.getDirectoryPath() + " was not locked, if another JVM accessed this location simultaneously data corruption may have occurred"); } } }
@Test public void journal_04() { JournalEntry entry1 = new JournalEntry(JournalEntryType.Object, testRef, bb1) ; JournalEntry entry2 = new JournalEntry(JournalEntryType.Object, testRef, bb3) ; long x1 = journal.writeJournal(entry1) ; long x2 = journal.writeJournal(entry2) ; Iterator<JournalEntry> iter = journal.entries() ; JournalEntry entry1a = iter.next(); JournalEntry entry2a = iter.next(); assertFalse(iter.hasNext()) ; }
/** Recover one transaction from the start position given. * Scan to see if there is a commit; if found, play the * journal from the start point to the commit. * Return true is a commit was found. * Leave journal positioned just after commit or at end if none found. */ private static void recoverSegment(Journal jrnl, long startPosn, long endPosn, StorageConfig sConf) { //System.out.printf("Segment: %d %d\n", startPosn, endPosn); Iterator<JournalEntry> iter = jrnl.entries(startPosn) ; iter = jrnl.entries(startPosn) ; try { for ( ; iter.hasNext() ; ) { JournalEntry e = iter.next() ; if ( e.getType() == JournalEntryType.Commit ) { if ( e.getEndPosition() != endPosn ) log.warn(format("Inconsistent: end at %d; expected %d", e.getEndPosition(), endPosn)) ; return ; } replay(e, sConf) ; } } finally { Iter.close(iter) ; } }
@Before public void before() { BufferChannel mem = BufferChannelMem.create("journal") ; journal = new Journal(mem) ; bb1.clear() ; bb2.clear() ; bb3.clear() ; }
private static Journal findJournal(DatasetGraphTDB dsg) { Location loc = dsg.getLocation() ; String journalFilename = loc.absolute(Names.journalFile) ; File f = new File(journalFilename) ; //if ( FileOps.exists(journalFilename) if ( f.exists() && f.isFile() && f.length() > 0 ) return Journal.create(loc) ; else return null ; }