Search in sources :

Example 1 with StorageRandomAccessFile

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);
    }
}
Also used : StorageRandomAccessFile(org.apache.derby.io.StorageRandomAccessFile) IOException(java.io.IOException)

Example 2 with StorageRandomAccessFile

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();
}
Also used : VirtualFile(org.apache.derby.impl.io.vfmem.VirtualFile) StorageRandomAccessFile(org.apache.derby.io.StorageRandomAccessFile) DataStore(org.apache.derby.impl.io.vfmem.DataStore)

Example 3 with StorageRandomAccessFile

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());
}
Also used : VirtualFile(org.apache.derby.impl.io.vfmem.VirtualFile) StorageRandomAccessFile(org.apache.derby.io.StorageRandomAccessFile) DataStore(org.apache.derby.impl.io.vfmem.DataStore)

Example 4 with StorageRandomAccessFile

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;
}
Also used : StorageRandomAccessFile(org.apache.derby.io.StorageRandomAccessFile) FileNotFoundException(java.io.FileNotFoundException)

Example 5 with StorageRandomAccessFile

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;
}
Also used : StorageRandomAccessFile(org.apache.derby.io.StorageRandomAccessFile) StorageFile(org.apache.derby.io.StorageFile)

Aggregations

StorageRandomAccessFile (org.apache.derby.io.StorageRandomAccessFile)15 IOException (java.io.IOException)10 StorageFile (org.apache.derby.io.StorageFile)6 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 DataStore (org.apache.derby.impl.io.vfmem.DataStore)2 VirtualFile (org.apache.derby.impl.io.vfmem.VirtualFile)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 InputStream (java.io.InputStream)1 CipherProvider (org.apache.derby.iapi.services.crypto.CipherProvider)1 ArrayInputStream (org.apache.derby.iapi.services.io.ArrayInputStream)1 Formatable (org.apache.derby.iapi.services.io.Formatable)1 LogInstant (org.apache.derby.iapi.store.raw.log.LogInstant)1 RawTransaction (org.apache.derby.iapi.store.raw.xact.RawTransaction)1 StandardException (org.apache.derby.shared.common.error.StandardException)1