Package org.apache.derby.io

Examples of org.apache.derby.io.StorageRandomAccessFile


        /////////////////////////////////////////////////////////////
        //
        // determine where the log ends
        //
        /////////////////////////////////////////////////////////////
        StorageRandomAccessFile theLog = null;


        // if logend == LogCounter.INVALID_LOG_SCAN, that means there
                // is no log record in the log - most likely it is corrupted in
                // some way ...
        if (logEnd == LogCounter.INVALID_LOG_INSTANT)
        {
          Monitor.logTextMessage(MessageId.LOG_LOG_NOT_FOUND);

          StorageFile logFile = getLogFileName(logFileNumber);

                    if (privExists(logFile))
          {
            // if we can delete this strange corrupted file, do so,
            // otherwise, skip it
                        if (!privDelete(logFile))
            {
              logFile = getLogFileName(++logFileNumber);
            }
          }

          try
          {
                        theLog =   privRandomAccessFile(logFile, "rw");
          }
          catch (IOException ioe)
          {
            theLog = null;
          }

                    if (theLog == null || !privCanWrite(logFile))
          {
            if (theLog != null)
              theLog.close();

            theLog = null;

            ReadOnlyDB = true;
          }
          else
          {
            try
            {
              // no previous log file or previous log position
              if (!initLogFile(
                                    theLog, logFileNumber,
                                    LogCounter.INVALID_LOG_INSTANT))
                            {
                throw markCorrupt(
                                    StandardException.newException(
                                        SQLState.LOG_SEGMENT_NOT_EXIST,
                                        logFile.getPath()));
                            }
            }
            catch (IOException ioe)
            {
              throw markCorrupt(
                                StandardException.newException(
                                    SQLState.LOG_IO_ERROR, ioe));
            }

                        // successfully init'd the log file - set up markers,
                        // and position at the end of the log.
            endPosition = theLog.getFilePointer();
            lastFlush   = endPosition;
           
            //if write sync is true , prellocate the log file
            //and reopen the file in rwd mode.
            if(isWriteSynced)
            {
              //extend the file by wring zeros to it
              preAllocateNewLogFile(theLog);
              theLog.close();
              theLog = openLogFileInWriteMode(logFile);
              //postion the log at the current end postion
              theLog.seek(endPosition);
            }
           
            if (SanityManager.DEBUG)
            {
              SanityManager.ASSERT(
                                endPosition == LOG_FILE_HEADER_SIZE,
                                "empty log file has wrong size");
            }
           
            //because we already incrementing the log number
            //here, no special log switch required for
            //backup recoveries.
            logSwitchRequired = false;
          }
        }
        else
        {
          // logEnd is the instant of the next log record in the log
          // it is used to determine the last known good position of
          // the log
          logFileNumber = LogCounter.getLogFileNumber(logEnd);

          ReadOnlyDB = df.isReadOnly();

          StorageFile logFile = getLogFileName(logFileNumber);

          if (!ReadOnlyDB)
          {
            // if datafactory doesn't think it is readonly, we can
            // do some futher test of our own
            try
            {
              if(isWriteSynced)
                theLog = openLogFileInWriteMode(logFile);
              else
                theLog = privRandomAccessFile(logFile, "rw");
            }
            catch (IOException ioe)
            {
              theLog = null;
            }
                        if (theLog == null || !privCanWrite(logFile))
            {
              if (theLog != null)
                theLog.close();
              theLog = null;

              ReadOnlyDB = true;
            }
          }

          if (!ReadOnlyDB)
          {
            endPosition = LogCounter.getLogFilePosition(logEnd);

            //
            // The end of the log is at endPosition.  Which is where
            // the next log should be appending.
            //
            // if the last log record ends before the end of the
                        // log file, then this log file has a fuzzy end.
                        // Zap all the bytes to between endPosition to EOF to 0.
            //
            // the end log marker is 4 bytes (of zeros)
            //
            // if endPosition + 4 == logOut.length, we have a
                        // properly terminated log file
            //
            // if endPosition + 4 is > logOut.length, there are 0,
                        // 1, 2, or 3 bytes of 'fuzz' at the end of the log. We
                        // can ignore that because it is guaranteed to be
                        // overwritten by the next log record.
            //
            // if endPosition + 4 is < logOut.length, we have a
                        // partial log record at the end of the log.
            //
            // We need to overwrite all of the incomplete log
                        // record, because if we start logging but cannot
                        // 'consume' all the bad log, then the log will truly
                        // be corrupted if the next 4 bytes (the length of the
                        // log record) after that is small enough that the next
                        // time the database is recovered, it will be
                        // interpreted that the whole log record is in the log
                        // and will try to objectify, only to get classNotFound
                        // error or worse.
            //

            //find out if log had incomplete log records at the end.
            if (redoScan.isLogEndFuzzy())
            {
              theLog.seek(endPosition);
              long eof = theLog.length();

              Monitor.logTextMessage(MessageId.LOG_INCOMPLETE_LOG_RECORD,
                logFile, new Long(endPosition), new Long(eof));

              /* Write zeros from incomplete log record to end of file */
              long nWrites = (eof - endPosition)/logBufferSize;
              int rBytes = (int)((eof - endPosition) % logBufferSize);
              byte zeroBuf[]= new byte[logBufferSize];
             
              //write the zeros to file
              while(nWrites-- > 0)
                theLog.write(zeroBuf);
              if(rBytes !=0)
                theLog.write(zeroBuf, 0, rBytes);
             
              if(!isWriteSynced)
                syncFile(theLog);
            }

            if (SanityManager.DEBUG)
            {
              if (theLog.length() != endPosition)
              {
                SanityManager.ASSERT(
                                    theLog.length() > endPosition,
                                    "log end > log file length, bad scan");
              }
            }

            // set the log to the true end position,
                        // and not the end of the file

            lastFlush = endPosition;
            theLog.seek(endPosition);
          }
        }

        if (theLog != null)
          logOut = new LogAccessFile(this, theLog, logBufferSize);
View Full Code Here


     throws StandardException
  {
    boolean ret = false;
    try
    {
      StorageRandomAccessFile log = privRandomAccessFile(logFileName, "r");
      ret = verifyLogFormat(log, number);
      log.close();
    }
    catch (IOException ioe)
    {
     
    }
View Full Code Here

        throw StandardException.newException(
                        SQLState.LOG_EXCEED_MAX_LOG_FILE_NUMBER,
                        new Long(maxLogFileNumber));
            }

      StorageRandomAccessFile newLog = null// the new log file
      try
      {
        // if the log file exist and cannot be deleted, cannot
        // switch log right now
                if (privExists(newLogFile) && !privDelete(newLogFile))
        {
          logErrMsg(MessageService.getTextMessage(
                        MessageId.LOG_NEW_LOGFILE_EXIST,
              newLogFile.getPath()));
          return;
        }

        try
        {
                    newLog =   privRandomAccessFile(newLogFile, "rw");
        }
        catch (IOException ioe)
        {
          newLog = null;
        }

                if (newLog == null || !privCanWrite(newLogFile))
        {
          if (newLog != null)
            newLog.close();
          newLog = null;

          return;
        }

        if (initLogFile(newLog, logFileNumber+1,
                LogCounter.makeLogInstantAsLong(logFileNumber, endPosition)))
        {

          // New log file init ok, close the old one and
          // switch over, after this point, need to shutdown the
          // database if any error crops up
          switchedOver = true;

          // write out an extra 0 at the end to mark the end of the log
          // file.
         
          logOut.writeEndMarker(0);

          endPosition += 4;
          //set that we are in log switch to prevent flusher
          //not requesting  to switch log again
          inLogSwitch = true;
          // flush everything including the int we just wrote
          flush(logFileNumber, endPosition);
         
         
          // simulate out of log error after the switch over
          if (SanityManager.DEBUG)
          {
            if (SanityManager.DEBUG_ON(TEST_SWITCH_LOG_FAIL2))
              throw new IOException("TestLogSwitchFail2");
          }


          logOut.close();    // close the old log file
         
          logWrittenFromLastCheckPoint += endPosition;

          endPosition = newLog.getFilePointer();
          lastFlush = endPosition;
         
          if(isWriteSynced)
          {
            //extend the file by wring zeros to it
            preAllocateNewLogFile(newLog);
            newLog.close();
            newLog = openLogFileInWriteMode(newLogFile);
            newLog.seek(endPosition);
          }

          logOut = new LogAccessFile(this, newLog, logBufferSize);
          newLog = null;


          if (SanityManager.DEBUG)
          {
            if (endPosition != LOG_FILE_HEADER_SIZE)
              SanityManager.THROWASSERT(
                      "new log file has unexpected size" +
                       + endPosition);
          }
          logFileNumber++;

          if (SanityManager.DEBUG)
          {
            SanityManager.ASSERT(endPosition == LOG_FILE_HEADER_SIZE,
                       "empty log file has wrong size");
          }

        }
        else  // something went wrong, delete the half baked file
        {
          newLog.close();
          newLog = null;

          if (privExists(newLogFile))
              privDelete(newLogFile);
          newLogFile = null;

          logErrMsg(MessageService.getTextMessage(
                        MessageId.LOG_CANNOT_CREATE_NEW,
                        newLogFile.getPath()));
         }

      }
      catch (IOException ioe)
      {

        inLogSwitch = false;
        // switching log file is an optional operation and there is no direct user
        // control.  Just sends a warning message to whomever, if any,
        // system adminstrator there may be

                logErrMsg(MessageService.getTextMessage(
                    MessageId.LOG_CANNOT_CREATE_NEW_DUETO,
                    newLogFile.getPath(),
                    ioe.toString()));

        try
        {
          if (newLog != null)
          {
            newLog.close();
            newLog = null;
          }
        }
        catch (IOException ioe2) {}
View Full Code Here

    <P> MT- synchronized by caller
  */
  boolean writeControlFile(StorageFile logControlFileName, long value)
     throws IOException, StandardException
  {
    StorageRandomAccessFile logControlFile = null;

    ByteArrayOutputStream baos = new ByteArrayOutputStream(64);
    DataOutputStream daos = new DataOutputStream(baos);

    daos.writeInt(fid);

    // so that when this db is booted by 1.1x and 1.2x JBMS, a IOException
    // stack trace rather than some error message that tells
    // the user to delete the database will show up.
    daos.writeInt(OBSOLETE_LOG_VERSION_NUMBER);
    daos.writeLong(value);

    if (onDiskMajorVersion == 0) {
      onDiskMajorVersion = jbmsVersion.getMajorVersion();
      onDiskMinorVersion = jbmsVersion.getMinorVersion();
      onDiskBeta = jbmsVersion.isBeta();
    }

    // previous to 1.3, that's all we wrote. 
    // from 1.3 and onward, also write out the JBMSVersion
    daos.writeInt(onDiskMajorVersion);
    daos.writeInt(onDiskMinorVersion);

    // For 2.0 beta we added the build number and the isBeta indication.
    // (5 bytes from our first spare long)
    daos.writeInt(jbmsVersion.getBuildNumberAsInt());

    byte flags = 0;
    if (onDiskBeta)
            flags |= IS_BETA_FLAG;
       
        // When database is booted with derby.system.durability=test,
        // this mode does not guarantee that
        // - database will recover
        // - committed transactions will not be lost
        // - database will be in a consistent state
        // Hence necessary to keep track of this state so we don't
        // waste time resolving issues in such cases.
        // wasDBInDurabilityTestModeNoSync has information if database was
        // previously booted at any time in this mode
        if (logNotSynced || wasDBInDurabilityTestModeNoSync)
            flags |= IS_DURABILITY_TESTMODE_NO_SYNC_FLAG;
     daos.writeByte(flags);

    //
    // write some spare bytes after 2.0 we have 3 + 2(8) spare bytes.
     long spare = 0;
      
    daos.writeByte(0);
    daos.writeByte(0);
        daos.writeByte(0);
    daos.writeLong(spare);
    daos.flush();
    // write the checksum for the control data written
    checksum.reset();
    checksum.update(baos.toByteArray(), 0, baos.size());
    daos.writeLong(checksum.getValue());
    daos.flush();

    try
    {
            checkCorrupt();

      try
      {
                logControlFile = privRandomAccessFile(logControlFileName, "rw");
      }
      catch (IOException ioe)
      {
        logControlFile = null;
        return false;
      }

            if (!privCanWrite(logControlFileName))
        return false;

      if (SanityManager.DEBUG)
      {
        if (SanityManager.DEBUG_ON(TEST_LOG_FULL))
          testLogFull();
      }

      logControlFile.seek(0);
      logControlFile.write(baos.toByteArray());
            syncFile(logControlFile);
            logControlFile.close();

      // write the same data to mirror control file
      try
      {
        logControlFile =
                    privRandomAccessFile(getMirrorControlFileName(), "rw");
      }
      catch (IOException ioe)
      {
        logControlFile = null;
        return false;
      }

      logControlFile.seek(0);
      logControlFile.write(baos.toByteArray());
            syncFile(logControlFile);

    }
    finally
    {
      if (logControlFile != null)
        logControlFile.close();
    }

    return true;

  }
View Full Code Here

    <P> MT- read only
  */
  private long readControlFile(StorageFile logControlFileName, Properties startParams)
     throws IOException, StandardException
  {
    StorageRandomAccessFile logControlFile = null;
    ByteArrayInputStream bais = null;
        DataInputStream dais = null;
    logControlFile =  privRandomAccessFile(logControlFileName, "r");
    boolean upgradeNeeded = false;
    long value = LogCounter.INVALID_LOG_INSTANT;
    long onDiskChecksum = 0;
    long controlFilelength = logControlFile.length();
    byte barray[] = null;

    try
    {
      // The length of the file is less than the minimum in any version
            // It is possibly hosed , no point in reading data from this file
            // skip reading checksum  control file is before 1.5
            if (controlFilelength < 16)
        onDiskChecksum = -1;
      else if (controlFilelength == 16)
      {
        barray = new byte[16];
        logControlFile.readFully(barray);
      }else if (controlFilelength > 16)
            {
        barray = new byte[(int) logControlFile.length() - 8];
        logControlFile.readFully(barray);
        onDiskChecksum = logControlFile.readLong();
        if (onDiskChecksum !=0 )
        {
          checksum.reset();
          checksum.update(barray, 0, barray.length);
        }
      }

      if ( onDiskChecksum == checksum.getValue() || onDiskChecksum ==0)
      {

        bais = new ByteArrayInputStream(barray);
        dais = new DataInputStream(bais);

        if (dais.readInt() != fid)
              {
                  throw StandardException.newException(
                          SQLState.LOG_INCOMPATIBLE_FORMAT, dataDirectory);
              }
 
        int obsoleteVersion = dais.readInt();
        value = dais.readLong();
 
        if (SanityManager.DEBUG)
        {
          if (SanityManager.DEBUG_ON(LogToFile.DBG_FLAG))
                      SanityManager.DEBUG(LogToFile.DBG_FLAG,
                          "log control file ckp instance = " +
                          LogCounter.toDebugString(value));
        }
 
 
        // from version 1.5 onward, we added an int for storing JBMS
        // version and an int for storing checkpoint interval
        // and log switch interval
        onDiskMajorVersion = dais.readInt();
        onDiskMinorVersion = dais.readInt();
        int dbBuildNumber = dais.readInt();
        int flags = dais.readByte();
       
        // check if the database was booted previously at any time with
                // derby.system.durability=test mode
                // If yes, then on a boot error we report that this setting is
                // probably the cause for the error and also log a warning
                // in the derby.log that this mode was set previously
                wasDBInDurabilityTestModeNoSync =
                    (flags & IS_DURABILITY_TESTMODE_NO_SYNC_FLAG) != 0;

                if (SanityManager.DEBUG) {
                    if (SanityManager.DEBUG_ON(LogToFile.DBG_FLAG))
                        SanityManager.DEBUG(LogToFile.DBG_FLAG,
                        "log control file, was derby.system.durability set to test = " +
                        wasDBInDurabilityTestModeNoSync);
                }
                   
       
        onDiskBeta = (flags & IS_BETA_FLAG) != 0;
        if (onDiskBeta)
        {
          // if is beta, can only be booted by exactly the same
          // version
          if (!jbmsVersion.isBeta() ||
            onDiskMajorVersion != jbmsVersion.getMajorVersion() ||
            onDiskMinorVersion != jbmsVersion.getMinorVersion())
          {
            boolean forceBetaUpgrade = false;
            if (SanityManager.DEBUG)
            {
              // give ourselves an out for this beta check for debugging purposes
              if (SanityManager.DEBUG_ON("forceBetaUpgrade"))
              {
                Monitor.logMessage("WARNING !! : forcing beta upgrade.");
                forceBetaUpgrade =true;
              }
            }

            if (!forceBetaUpgrade)
                      {
              throw StandardException.newException(
                              SQLState.LOG_CANNOT_UPGRADE_BETA,
                              dataDirectory,
                ProductVersionHolder.simpleVersionString(onDiskMajorVersion, onDiskMinorVersion, onDiskBeta));
                      }
          }
        }
         
 
        // JBMS_VERSION must be numbered in a way so that it is ever
        // increasing.  We are backwards compatible but not forwards
        // compatible
        //
        if (onDiskMajorVersion > jbmsVersion.getMajorVersion() ||
          (onDiskMajorVersion == jbmsVersion.getMajorVersion() &&
           onDiskMinorVersion > jbmsVersion.getMinorVersion()))
        {
          // don't need to worry about point release, no format
          // upgrade is allowed.
          throw StandardException.newException(
                          SQLState.LOG_INCOMPATIBLE_VERSION,
                          dataDirectory,
              ProductVersionHolder.simpleVersionString(onDiskMajorVersion, onDiskMinorVersion, onDiskBeta));
        }

        // Ensure that upgrade has been requested for a major or minor upgrade
        // maintaince (point) versions should not require an upgrade.
        if ((onDiskMajorVersion != jbmsVersion.getMajorVersion()) ||
          (onDiskMinorVersion != jbmsVersion.getMinorVersion()))
        {
          upgradeNeeded = true;
        }
        // if checksum is zeros in  version > 3.5 file is hosed
        // except incase of upgrade from versions <= 3.5
        if (onDiskChecksum == 0 &&
          (!(onDiskMajorVersion <= 3 && onDiskMinorVersion <=5) ||
          onDiskMajorVersion == 0))
          value = LogCounter.INVALID_LOG_INSTANT;
      }
    }
    finally
    {
      if (logControlFile != null)
        logControlFile.close();
      if (bais != null)
        bais.close();
      if (dais != null)
        dais.close();
    }
View Full Code Here

      return null;
    }


    StorageRandomAccessFile log = null;

    try
    {
            log = privRandomAccessFile(fileName, "r");

      // verify that the log file is of the right format
      if (!verifyLogFormat(log, filenum))
      {
        if (SanityManager.DEBUG)
        {
          if (SanityManager.DEBUG_ON(LogToFile.DBG_FLAG))
            SanityManager.DEBUG(LogToFile.DBG_FLAG, fileName.getPath() + " format mismatch");
        }

        log.close();
        log = null;
      }
      else
      {
        log.seek(filepos);
      }
    }
    catch (IOException ioe)
    {
      try
      {
        if (log != null)
        {
          log.close();
          log = null;
        }

        if (SanityManager.DEBUG)
        {
View Full Code Here

    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;
View Full Code Here

                isWriteSynced = false;
                return privRandomAccessFile(logFile, "rw");
            }
        }

    StorageRandomAccessFile log = privRandomAccessFile(logFile, "rwd");
    return log ;
  }
View Full Code Here

     * @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
View Full Code Here

        throws StandardException
    {
        BasePage page = null;
        StorageFile newFile =
            dataFactory.getStorageFactory().newStorageFile(newFilePath);
        StorageRandomAccessFile newRaf = null;
        try {
            long lastPageNumber= getLastPageNumber(handle);
            newRaf = privGetRandomAccessFile(newFile);

            byte[] encryptionBuf = null;
            encryptionBuf = new byte[pageSize];

            // copy all the pages from the current container to the
            // new container file after encryting the pages.
            for (long pageNumber = FIRST_ALLOC_PAGE_NUMBER;
                 pageNumber <= lastPageNumber; pageNumber++)
            {

                page = getLatchedPage(handle, pageNumber);
                       
                // update the page array before writing to the disk
                // with container header and encrypt it.
                       
                byte[] dataToWrite = updatePageArray(pageNumber,
                                                     page.getPageArray(),
                                                     encryptionBuf,
                                                     true);
                newRaf.write(dataToWrite, 0, pageSize);

                // unlatch releases page from cache.
                page.unlatch();
                page = null;
            }

            // sync the new version of the container.
            newRaf.sync(true);
            newRaf.close();
            newRaf = null;
           
        }catch (IOException ioe) {
            throw StandardException.newException(
                                    SQLState.FILE_CONTAINER_EXCEPTION,
                                    ioe,
                                    newFile);
        } finally {

            if (page != null) {
                page.unlatch();
                page = null;
            }
           
            if (newRaf != null) {
                try {
                    newRaf.close();
                }catch (IOException ioe)
                {
                    newRaf = null;
                    throw StandardException.newException(
                                    SQLState.FILE_CONTAINER_EXCEPTION,
View Full Code Here

TOP

Related Classes of org.apache.derby.io.StorageRandomAccessFile

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.