use of org.apache.derby.io.StorageRandomAccessFile in project derby by apache.
the class T_RecoverBadLog method simulateLogFileCorruption.
/*
* simulate log corruption to test the checksuming of log records.
*/
private void simulateLogFileCorruption() throws T_Fail, StandardException {
long filenum;
long filepos;
long amountOfLogWritten;
LogCounter logInstant = (LogCounter) logFactory.getFirstUnflushedInstant();
filenum = logInstant.getLogFileNumber();
filepos = logInstant.getLogFilePosition();
logFactory.flushAll();
logInstant = (LogCounter) logFactory.getFirstUnflushedInstant();
filenum = logInstant.getLogFileNumber();
amountOfLogWritten = logInstant.getLogFilePosition() - filepos;
try {
StorageRandomAccessFile log = logFactory.getLogFileToSimulateCorruption(filenum);
int noWrites = (int) amountOfLogWritten / 512;
// mess up few bytes in every block of a 512 bytes.
filepos += 512;
java.util.Random r = new java.util.Random();
for (int i = 0; i < noWrites; i++) {
REPORT("corruptig log file : filenum " + filenum + " fileposition " + filepos);
log.seek(filepos);
log.writeInt(r.nextInt());
filepos += 512;
}
log.sync();
log.close();
} catch (IOException ie) {
throw T_Fail.exceptionFail(ie);
}
}
use of org.apache.derby.io.StorageRandomAccessFile in project derby by apache.
the class VirtualFileTest method testCloseIdempotent.
/**
* Verify that the close() method of VirtualRandomAccessFile can be
* called more than once.
*/
public void testCloseIdempotent() throws IOException {
DataStore store = getStore();
VirtualFile f = new VirtualFile("afile", store);
StorageRandomAccessFile raf = f.getRandomAccessFile("rw");
raf.close();
// The second close() used to throw NullPointerException (DERBY-5960)
raf.close();
}
use of org.apache.derby.io.StorageRandomAccessFile in project derby by apache.
the class VirtualFileTest method testGetRAFNonExisting.
/**
* Getting a random access file in write mode for a non-existing file
* should cause the file to be created.
*/
public void testGetRAFNonExisting() throws FileNotFoundException {
DataStore store = getStore();
VirtualFile vFile = new VirtualFile("aNewFile.txt", store);
assertFalse(vFile.exists());
StorageRandomAccessFile vRAF = vFile.getRandomAccessFile("rw");
assertNotNull(vRAF);
assertTrue(vFile.exists());
}
use of org.apache.derby.io.StorageRandomAccessFile in project derby by apache.
the class LogToFile method checkJvmSyncError.
// end of getLogDirPath
/**
* In Java 1.4.2 and newer rws and rwd modes for RandomAccessFile
* are supported. Still, on some JVMs (e.g. early versions of 1.4.2
* and 1.5 on Mac OS and FreeBSD) the support for rws and rwd is
* not working. This method attempts to detect this by opening an
* existing file in "rws" mode. If this fails, Derby should fall
* back to use "rw" mode for the log files followed by explicit
* syncing of the log.
*
* Note: it is important to use "rws" for the test. If "rwd" is used, no
* exception is thrown when opening the file, but the syncing does not
* take place.
*
* For more details see DERBY-1 (and DERBY-2020).
*
* @param logFile information about the log file to be opened
*
* @return true if a JVM error is detected, false otherwise
*
* @exception StandardException Standard Derby exception
*/
private boolean checkJvmSyncError(StorageFile logFile) throws IOException {
boolean hasJvmSyncError = false;
StorageRandomAccessFile rwsTest;
// Normally this log file already exists but in case it does
// not we open the file using "rw" mode. This is needed in
// order to ensure that the file already exists when it is
// opened in "rws" mode. This should succeed on all JVMs
rwsTest = privRandomAccessFile(logFile, "rw");
rwsTest.close();
// Try to re-open the file in "rws" mode
try {
rwsTest = privRandomAccessFile(logFile, "rws");
rwsTest.close();
} catch (FileNotFoundException ex) {
// Normally this exception should never occur. For some
// reason currently on some Mac and FreeBSD JVM 1.4.2 and
// 1.5 FileNotFoundException exception is thrown if a file
// is opened in "rws" mode and if it already
// exists. Please refer to DERBY-1 for more details on
// this issue. Temporary workaround to avoid this problem
// is to make the logging system use file sync mechanism.
logErrMsg("LogToFile.checkJvmSyncError: Your JVM seems to have a " + "problem with implicit syncing of log files. Will use " + "explicit syncing instead.");
hasJvmSyncError = true;
}
// Set this variable to true to avoid that this method is called
// multiple times
jvmSyncErrorChecked = true;
return hasJvmSyncError;
}
use of org.apache.derby.io.StorageRandomAccessFile in project derby by apache.
the class LogToFile method getLogFileToSimulateCorruption.
/**
* Get the log file to Simulate a log corruption
* FOR UNIT TESTING USAGE ONLY
*/
public StorageRandomAccessFile getLogFileToSimulateCorruption(long filenum) throws IOException, StandardException {
if (SanityManager.DEBUG) {
// long filenum = LogCounter.getLogFileNumber(logInstant);
// long filepos = LogCounter.getLogFilePosition(logInstant);
StorageFile fileName = getLogFileName(filenum);
StorageRandomAccessFile log = null;
return privRandomAccessFile(fileName, "rw");
}
return null;
}
Aggregations