use of org.apache.derby.iapi.store.raw.log.LogInstant in project derby by apache.
the class LogToFile method findCheckpoint.
/*
Find a checkpoint log record at the checkpointInstant
<P> MT- read only
*/
private CheckpointOperation findCheckpoint(long checkpointInstant, FileLogger logger) throws IOException, StandardException, ClassNotFoundException {
StreamLogScan scan = (StreamLogScan) openForwardsScan(checkpointInstant, (LogInstant) null);
// estimated size of a checkpoint log record, which contains 3 longs
// and assorted other log record overhead
Loggable lop = logger.readLogRecord(scan, 100);
scan.close();
if (lop instanceof CheckpointOperation)
return (CheckpointOperation) lop;
else
return null;
}
use of org.apache.derby.iapi.store.raw.log.LogInstant in project derby by apache.
the class LogToFile method checkpointWithTran.
/**
* checkpoint with pre-start transaction
*
* @param rsf The RawStoreFactory to use to do the checkpoint.
* @param df The DataFactory to use to do the checkpoint.
* @param tf The TransactionFactory to use to do the checkpoint.
* @param wait If an existing checkpoint is in progress, then if
* wait=true then this routine will wait for the
* checkpoint to complete and the do another checkpoint
* and wait for it to finish before returning.
*
* @exception StandardException Derby Standard Error Policy
*/
private boolean checkpointWithTran(RawTransaction cptran, RawStoreFactory rsf, DataFactory df, TransactionFactory tf, boolean wait) throws StandardException {
LogInstant redoLWM;
// logout is set
if (logOut == null) {
return false;
}
long approxLogLength;
boolean proceed = true;
do {
synchronized (this) {
if (corrupt != null) {
throw StandardException.newException(SQLState.LOG_STORE_CORRUPT, corrupt);
}
// current end position
approxLogLength = endPosition;
if (!inCheckpoint) {
// no checkpoint in progress, change status to indicate
// this code is doing the checkpoint.
inCheckpoint = true;
// in this routine.
break;
} else {
if (wait) {
while (inCheckpoint) {
try {
wait();
} catch (InterruptedException ie) {
InterruptStatus.setInterrupted();
}
}
} else {
// caller did not want to wait for already executing
// checkpoint to finish. Routine will return false
// upon exiting the loop.
proceed = false;
}
}
// don't return from inside of a sync block
}
} while (proceed);
if (!proceed) {
return false;
}
// needCPtran == true if not supplied with a pre-started transaction
boolean needCPTran = (cptran == null);
if (SanityManager.DEBUG) {
if (logSwitchInterval == 0) {
SanityManager.THROWASSERT("switching log file: Approx log length = " + approxLogLength + " logSwitchInterval = 0");
}
}
try {
if (approxLogLength > logSwitchInterval) {
switchLogFile();
// log switch is occuring in conjuction with the
// checkpoint, set the amount of log written from last
// checkpoint to zero.
logWrittenFromLastCheckPoint = 0;
} else {
// checkpoint is happening without the log switch,
// in the middle of a log file. Amount of log written already for
// the current log file should not be included in caluculation
// of when next check point is due. By assigning the negative
// value of amount of log written for this file. Later it will
// be subtracted when we switch the log file or while
// calculating whether we are due a for checkpoint at flush time.
logWrittenFromLastCheckPoint = -endPosition;
}
if (SanityManager.DEBUG) {
if (SanityManager.DEBUG_ON(TEST_LOG_SWITCH_LOG))
return false;
}
// start a checkpoint transaction
if (needCPTran)
cptran = tf.startInternalTransaction(rsf, getContextService().getCurrentContextManager());
// ///////////////////////////////////////////////////
// gather a snapshot of the various interesting points of the log
// ///////////////////////////////////////////////////
long undoLWM_long;
long redoLWM_long;
synchronized (// we could synchronized on something else, it
this) // doesn't matter as long as logAndDo sync on
// the same thing
{
// The redo LWM is the current log instant. We are going to
// clean the cache shortly, any log record before this point
// will not ever need to be redone.
redoLWM_long = currentInstant();
redoLWM = new LogCounter(redoLWM_long);
// The undo LWM is what we need to rollback all transactions.
// Synchronize this with the starting of a new transaction so
// that the transaction factory can have a consistent view
// See FileLogger.logAndDo
LogCounter undoLWM = (LogCounter) (tf.firstUpdateInstant());
if (undoLWM == null)
// no active transaction
undoLWM_long = redoLWM_long;
else
undoLWM_long = undoLWM.getValueAsLong();
}
// ///////////////////////////////////////////////////
// clean the buffer cache
// ///////////////////////////////////////////////////
df.checkpoint();
// ///////////////////////////////////////////////////
// write out the checkpoint log record
// ///////////////////////////////////////////////////
// send the checkpoint record to the log
Formatable transactionTable = tf.getTransactionTable();
CheckpointOperation nextCheckpoint = new CheckpointOperation(redoLWM_long, undoLWM_long, transactionTable);
cptran.logAndDo(nextCheckpoint);
LogCounter checkpointInstant = (LogCounter) (cptran.getLastLogInstant());
if (checkpointInstant != null) {
// since checkpoint is an internal transaction, I need to
// flush it to make sure it actually goes to the log
flush(checkpointInstant);
} else {
throw StandardException.newException(SQLState.LOG_CANNOT_LOG_CHECKPOINT);
}
cptran.commit();
if (needCPTran) {
// if we started it, we will close it
cptran.close();
cptran = null;
}
if (!writeControlFile(getControlFileName(), checkpointInstant.getValueAsLong())) {
throw StandardException.newException(SQLState.LOG_CONTROL_FILE, getControlFileName());
}
// next checkpoint becomes the current checkpoint
currentCheckpoint = nextCheckpoint;
if (!logArchived()) {
truncateLog(currentCheckpoint);
}
// are needed to recover from the backup checkpoint on restore.
if (!backupInProgress)
df.removeDroppedContainerFileStubs(redoLWM);
} catch (IOException ioe) {
throw markCorrupt(StandardException.newException(SQLState.LOG_IO_ERROR, ioe));
} finally {
synchronized (this) {
inCheckpoint = false;
notifyAll();
}
if (cptran != null && needCPTran) {
try {
cptran.commit();
cptran.close();
} catch (StandardException se) {
throw markCorrupt(StandardException.newException(SQLState.LOG_CORRUPTED, se));
}
}
}
return true;
}
use of org.apache.derby.iapi.store.raw.log.LogInstant in project derby by apache.
the class LockCount method popSavePoints.
/**
* Pop all savepoints upto the one with the given name and rollback
* all changes made since this savepoint was pushed.
* If release is true then this savepoint is popped as well,
* otherwise it is left in the stack (at the top).
*
* @return true if any work is rolled back, false if no work is rolled back
* @exception StandardException Thrown if a error of severity less than TransactionException#SEVERITY
* is encountered during the rollback of this savepoint.
*/
protected boolean popSavePoints(int position, boolean release) throws StandardException {
if (release) {
savePoints.setSize(position);
return false;
}
LogInstant rollbackTo = null;
int size = savePoints.size();
for (int i = position; i < size; i++) {
SavePoint rollbackSavePoint = savePoints.elementAt(i);
LogInstant li = rollbackSavePoint.getSavePoint();
if (li != null) {
rollbackTo = li;
break;
}
}
savePoints.setSize(position + 1);
if (rollbackTo == null)
return false;
// now perform the rollback
try {
logger.undo(this, getId(), rollbackTo, getLastLogInstant());
} catch (StandardException se) {
if (se.getSeverity() < ExceptionSeverity.TRANSACTION_SEVERITY) {
throw StandardException.newException(SQLState.XACT_ROLLBACK_EXCEPTION, se);
}
throw se;
}
return true;
}
use of org.apache.derby.iapi.store.raw.log.LogInstant in project derby by apache.
the class LockCount method logAndDo.
/**
* Log the operation and do it.
*
* If this transaction has not generated any log records prior to this,
* then log a beginXact log record.
*
* If the passed in operation is null, then do nothing (after logging the
* beginXact if needed).
*
* @exception StandardException Standard Derby exception policy
* @see Transaction#logAndDo
*/
public void logAndDo(Loggable operation) throws StandardException {
LogInstant instant = null;
if (logger == null)
getLogger();
if (logger == null) {
throw StandardException.newException(SQLState.XACT_CANNOT_LOG_CHANGE);
}
setActiveState();
if (state == ACTIVE) {
instant = logger.logAndDo(this, new BeginXact(getGlobalId(), statusForBeginXactLog()));
setUpdateState();
}
seenUpdates = true;
if (operation != null) {
instant = logger.logAndDo(this, operation);
if (instant != null) {
setLastLogInstant(instant);
if ((savePoints != null) && !savePoints.empty()) {
for (int i = savePoints.size() - 1; i >= 0; i--) {
// set the top savepoint to rollback to this record if
// it doesn't yet have a point saved
SavePoint sp = savePoints.elementAt(i);
if (sp.getSavePoint() == null) {
sp.setSavePoint(instant);
} else
break;
}
}
}
} else {
if (instant != null)
setLastLogInstant(instant);
}
}
Aggregations