@Override public long deleteOldSnapshots(int numberToKeep) throws IOException { // always only keep the last snapshot return lastSnapshot.getTimestamp(); }
private static void printSnapshot(TransactionSnapshot snapshot) { Date snapshotDate = new Date(snapshot.getTimestamp()); System.out.println("TransactionSnapshot at " + snapshotDate.toString()); System.out.println("\t" + snapshot.toString()); }
private static void printSnapshot(TransactionSnapshot snapshot) { Date snapshotDate = new Date(snapshot.getTimestamp()); System.out.println("TransactionSnapshot at " + snapshotDate.toString()); System.out.println("\t" + snapshot.toString()); }
@Override public void writeSnapshot(TransactionSnapshot snapshot) throws IOException { // create a temporary file, and save the snapshot Path snapshotTmpFile = new Path(snapshotDir, TMP_SNAPSHOT_FILE_PREFIX + snapshot.getTimestamp()); LOG.debug("Writing snapshot to temporary file {}", snapshotTmpFile); FSDataOutputStream out = fs.create(snapshotTmpFile, false, BUFFER_SIZE); // encode the snapshot and stream the serialized version to the file try { codecProvider.encode(out, snapshot); } finally { out.close(); } // move the temporary file into place with the correct filename Path finalFile = new Path(snapshotDir, SNAPSHOT_FILE_PREFIX + snapshot.getTimestamp()); fs.rename(snapshotTmpFile, finalFile); LOG.debug("Completed snapshot to file {}", finalFile); }
@Override public void writeSnapshot(TransactionSnapshot snapshot) throws IOException { // create a temporary file, and save the snapshot Path snapshotTmpFile = new Path(snapshotDir, TMP_SNAPSHOT_FILE_PREFIX + snapshot.getTimestamp()); LOG.debug("Writing snapshot to temporary file {}", snapshotTmpFile); FSDataOutputStream out = fs.create(snapshotTmpFile, false, BUFFER_SIZE); // encode the snapshot and stream the serialized version to the file try { codecProvider.encode(out, snapshot); } finally { out.close(); } // move the temporary file into place with the correct filename Path finalFile = new Path(snapshotDir, SNAPSHOT_FILE_PREFIX + snapshot.getTimestamp()); fs.rename(snapshotTmpFile, finalFile); LOG.debug("Completed snapshot to file {}", finalFile); }
@Override public void writeSnapshot(TransactionSnapshot snapshot) throws IOException { // save the snapshot to a temporary file File snapshotTmpFile = new File(snapshotDir, TMP_SNAPSHOT_FILE_PREFIX + snapshot.getTimestamp()); LOG.debug("Writing snapshot to temporary file {}", snapshotTmpFile); OutputStream out = Files.newOutputStreamSupplier(snapshotTmpFile).getOutput(); boolean threw = true; try { codecProvider.encode(out, snapshot); threw = false; } finally { Closeables.close(out, threw); } // move the temporary file into place with the correct filename File finalFile = new File(snapshotDir, SNAPSHOT_FILE_PREFIX + snapshot.getTimestamp()); if (!snapshotTmpFile.renameTo(finalFile)) { throw new IOException("Failed renaming temporary snapshot file " + snapshotTmpFile.getName() + " to " + finalFile.getName()); } LOG.debug("Completed snapshot to file {}", finalFile); }
@Override public void writeSnapshot(TransactionSnapshot snapshot) throws IOException { // save the snapshot to a temporary file File snapshotTmpFile = new File(snapshotDir, TMP_SNAPSHOT_FILE_PREFIX + snapshot.getTimestamp()); LOG.debug("Writing snapshot to temporary file {}", snapshotTmpFile); OutputStream out = Files.newOutputStreamSupplier(snapshotTmpFile).getOutput(); boolean threw = true; try { codecProvider.encode(out, snapshot); threw = false; } finally { Closeables.close(out, threw); } // move the temporary file into place with the correct filename File finalFile = new File(snapshotDir, SNAPSHOT_FILE_PREFIX + snapshot.getTimestamp()); if (!snapshotTmpFile.renameTo(finalFile)) { throw new IOException("Failed renaming temporary snapshot file " + snapshotTmpFile.getName() + " to " + finalFile.getName()); } LOG.debug("Completed snapshot to file {}", finalFile); }
@Override public List<String> listSnapshots() throws IOException { List<String> snapshots = Lists.newArrayListWithCapacity(1); if (lastSnapshot != null) { snapshots.add(Long.toString(lastSnapshot.getTimestamp())); } return snapshots; }
public synchronized TransactionSnapshot getSnapshot() throws IOException { TransactionSnapshot snapshot = null; if (!isRunning() && !isStopping()) { return null; } long now = System.currentTimeMillis(); // avoid duplicate snapshots at same timestamp if (now == lastSnapshotTime || (currentLog != null && now == currentLog.getTimestamp())) { try { TimeUnit.MILLISECONDS.sleep(1); } catch (InterruptedException ie) { } } // copy in memory state snapshot = getCurrentState(); LOG.debug("Starting snapshot of transaction state with timestamp {}", snapshot.getTimestamp()); LOG.debug("Returning snapshot of state: " + snapshot); return snapshot; }
public synchronized TransactionSnapshot getSnapshot() throws IOException { TransactionSnapshot snapshot = null; if (!isRunning() && !isStopping()) { return null; } long now = System.currentTimeMillis(); // avoid duplicate snapshots at same timestamp if (now == lastSnapshotTime || (currentLog != null && now == currentLog.getTimestamp())) { try { TimeUnit.MILLISECONDS.sleep(1); } catch (InterruptedException ie) { } } // copy in memory state snapshot = getCurrentState(); LOG.debug("Starting snapshot of transaction state with timestamp {}", snapshot.getTimestamp()); LOG.debug("Returning snapshot of state: " + snapshot); return snapshot; }
/** * Restore the initial in-memory transaction state from a snapshot. */ private void restoreSnapshot(TransactionSnapshot snapshot) { LOG.info("Restoring transaction state from snapshot at " + snapshot.getTimestamp()); Preconditions.checkState(lastSnapshotTime == 0, "lastSnapshotTime has been set!"); Preconditions.checkState(readPointer == 0, "readPointer has been set!"); Preconditions.checkState(lastWritePointer == 0, "lastWritePointer has been set!"); Preconditions.checkState(invalid.isEmpty(), "invalid list should be empty!"); Preconditions.checkState(inProgress.isEmpty(), "inProgress map should be empty!"); Preconditions.checkState(committingChangeSets.isEmpty(), "committingChangeSets should be empty!"); Preconditions.checkState(committedChangeSets.isEmpty(), "committedChangeSets should be empty!"); LOG.info("Restoring snapshot of state: " + snapshot); lastSnapshotTime = snapshot.getTimestamp(); readPointer = snapshot.getReadPointer(); lastWritePointer = snapshot.getWritePointer(); invalid.addAll(snapshot.getInvalid()); inProgress.putAll(txnBackwardsCompatCheck(defaultLongTimeout, longTimeoutTolerance, snapshot.getInProgress())); committingChangeSets.putAll(snapshot.getCommittingChangeSets()); committedChangeSets.putAll(snapshot.getCommittedChangeSets()); }
/** * Restore the initial in-memory transaction state from a snapshot. */ private void restoreSnapshot(TransactionSnapshot snapshot) { LOG.info("Restoring transaction state from snapshot at " + snapshot.getTimestamp()); Preconditions.checkState(lastSnapshotTime == 0, "lastSnapshotTime has been set!"); Preconditions.checkState(readPointer == 0, "readPointer has been set!"); Preconditions.checkState(lastWritePointer == 0, "lastWritePointer has been set!"); Preconditions.checkState(invalid.isEmpty(), "invalid list should be empty!"); Preconditions.checkState(inProgress.isEmpty(), "inProgress map should be empty!"); Preconditions.checkState(committingChangeSets.isEmpty(), "committingChangeSets should be empty!"); Preconditions.checkState(committedChangeSets.isEmpty(), "committedChangeSets should be empty!"); LOG.info("Restoring snapshot of state: " + snapshot); lastSnapshotTime = snapshot.getTimestamp(); readPointer = snapshot.getReadPointer(); lastWritePointer = snapshot.getWritePointer(); invalid.addAll(snapshot.getInvalid()); inProgress.putAll(txnBackwardsCompatCheck(defaultLongTimeout, longTimeoutTolerance, snapshot.getInProgress())); committingChangeSets.putAll(snapshot.getCommittingChangeSets()); committedChangeSets.putAll(snapshot.getCommittedChangeSets()); }
snapshotTime = snapshot.getTimestamp(); currentLog = persistor.createLog(snapshot.getTimestamp());
snapshotTime = snapshot.getTimestamp(); currentLog = persistor.createLog(snapshot.getTimestamp());
@Override public void encode(OutputStream out, TransactionSnapshot snapshot) { try { BinaryEncoder encoder = new BinaryEncoder(out); encoder.writeLong(snapshot.getTimestamp()); encoder.writeLong(snapshot.getReadPointer()); encoder.writeLong(snapshot.getWritePointer()); encodeInvalid(encoder, snapshot.getInvalid()); encodeInProgress(encoder, snapshot.getInProgress()); encodeChangeSets(encoder, snapshot.getCommittingChangeSets()); encodeChangeSets(encoder, snapshot.getCommittedChangeSets()); } catch (IOException e) { LOG.error("Unable to serialize transaction state: ", e); throw Throwables.propagate(e); } }
@Override public void encode(OutputStream out, TransactionSnapshot snapshot) { try { BinaryEncoder encoder = new BinaryEncoder(out); encoder.writeLong(snapshot.getTimestamp()); encoder.writeLong(snapshot.getReadPointer()); encoder.writeLong(snapshot.getWritePointer()); encodeInvalid(encoder, snapshot.getInvalid()); encodeInProgress(encoder, snapshot.getInProgress()); encodeChangeSets(encoder, snapshot.getCommittingChangeSets()); encodeChangeSets(encoder, snapshot.getCommittedChangeSets()); } catch (IOException e) { LOG.error("Unable to serialize transaction state: ", e); throw Throwables.propagate(e); } }
@Test public void testResetState() throws Exception { // have tx in progress, committing and committed then reset, // get the last snapshot and see that it is empty TransactionSystemClient client = getClient(); TransactionStateStorage stateStorage = getStateStorage(); Transaction tx1 = client.startShort(); Transaction tx2 = client.startShort(); client.canCommit(tx1, asList(C1, C2)); client.commit(tx1); client.canCommit(tx2, asList(C3, C4)); Transaction txPreReset = client.startShort(); long currentTs = System.currentTimeMillis(); client.resetState(); TransactionSnapshot snapshot = stateStorage.getLatestSnapshot(); Assert.assertTrue(snapshot.getTimestamp() >= currentTs); Assert.assertEquals(0, snapshot.getInvalid().size()); Assert.assertEquals(0, snapshot.getInProgress().size()); Assert.assertEquals(0, snapshot.getCommittingChangeSets().size()); Assert.assertEquals(0, snapshot.getCommittedChangeSets().size()); // confirm that transaction IDs are not reset Transaction txPostReset = client.startShort(); Assert.assertTrue("New tx ID should be greater than last ID before reset", txPostReset.getTransactionId() > txPreReset.getTransactionId()); }