Package org.apache.derby.io

Examples of org.apache.derby.io.StorageFile


                    // Make sure we find the log, do not assume
                    // it is OK that the log is not there because
                    // it could be a user typo(like when users edit
                    // service.properties to change the log device
                    // while restoring from backups using OS copy.
                    StorageFile checklogDir =
                        logStorageFactory.newStorageFile(
                                 LogFactory.LOG_DIRECTORY_NAME);
                    if (!privExists(checklogDir))
                    {
                        throw
                            StandardException.newException(
                            SQLState.LOG_FILE_NOT_FOUND, checklogDir.getPath());

                    }
                }
            }
        }
           
    //if user does not set the right value for the log buffer size,
    //default value is used instead.
    logBufferSize =  PropertyUtil.getSystemInt(org.apache.derby.iapi.reference.Property.LOG_BUFFER_SIZE,
                           LOG_BUFFER_SIZE_MIN,
                           LOG_BUFFER_SIZE_MAX,
                           DEFAULT_LOG_BUFFER_SIZE);
    jbmsVersion = Monitor.getMonitor().getEngineVersion();

   
    String logArchiveMode =
            startParams.getProperty(Property.LOG_ARCHIVE_MODE);
    logArchived = Boolean.valueOf(logArchiveMode).booleanValue();
   
    //get log factorty properties if any set in derby.properties
    getLogFactoryProperties(null);

    /* check if the storage factory supports write sync (rws and rwd). If
     * so, use it unless derby.storage.fileSyncTransactionLog property is
     * set true by user.
     */

    if (logStorageFactory.supportsWriteSync())
        {
      //write sync can be used in the jvm that database is running on.
      //disable write sync if derby.storage.fileSyncTransactionLog is true
      isWriteSynced =
        !(PropertyUtil.getSystemBoolean(Property.FILESYNC_TRANSACTION_LOG));
        }
    else
    {
      isWriteSynced = false;
    }


        // If derby.system.durability=test is set,then set flag to
        // disable sync of log records at commit and log file before
        // data page makes it to disk
        if (Property.DURABILITY_TESTMODE_NO_SYNC.equalsIgnoreCase(
               PropertyUtil.getSystemProperty(Property.DURABILITY_PROPERTY)))
        {
        // disable syncing of log.
        logNotSynced = true;
          //if log not being synced;files shouldn't be open in write sync mode
        isWriteSynced = false
    }
        else if (Performance.MEASURE)
        {
            // development build only feature, must by hand set the
            // Performance.MEASURE variable and rebuild.  Useful during
            // development to compare/contrast effect of syncing, release
            // users can use the above relaxed durability option to disable
            // all syncing. 

            logNotSynced =
                PropertyUtil.getSystemBoolean(
                    Property.STORAGE_LOG_NOT_SYNCED);

            if (logNotSynced)
            {
                isWriteSynced = false;
                Monitor.logMessage("Performance.logNotSynced = true");
            }
        }

    // try to access the log
    // if it doesn't exist, create it.
    // if it does exist, run recovery

    boolean createNewLog = create;

    if (SanityManager.DEBUG)
      SanityManager.ASSERT(fid != -1, "invalid log format Id");

    checkpointInstant = LogCounter.INVALID_LOG_INSTANT;
    try
    {
      StorageFile logControlFileName = getControlFileName();

      StorageFile logFile;

      if (!createNewLog)
      {
                if (privExists(logControlFileName))
        {
          checkpointInstant =
                        readControlFile(logControlFileName, startParams);

          // in case system was running previously with
                    // derby.system.durability=test then print a message
                    // to the derby log
                    if (wasDBInDurabilityTestModeNoSync)
                    {
                        // print message stating that the database was
                        // previously atleast at one time running with
                        // derby.system.durability=test mode
                        Monitor.logMessage(MessageService.getTextMessage(
                     MessageId.LOG_WAS_IN_DURABILITY_TESTMODE_NO_SYNC,
                     Property.DURABILITY_PROPERTY,
                            Property.DURABILITY_TESTMODE_NO_SYNC));
                    }
           
          if (checkpointInstant == LogCounter.INVALID_LOG_INSTANT &&
                    privExists(getMirrorControlFileName()))
                    {
            checkpointInstant =
                            readControlFile(
                                getMirrorControlFileName(), startParams);
                    }

        }
        else if (logDevice != null)
        {
          // Do not throw this error if logDevice is null because
          // in a read only configuration, it is acceptable
          // to not have a log directory.  But clearly, if the
          // logDevice property is set, then it should be there.
          throw StandardException.newException(
                            SQLState.LOG_FILE_NOT_FOUND,
                            logControlFileName.getPath());
        }

        if (checkpointInstant != LogCounter.INVALID_LOG_INSTANT)
          logFileNumber = LogCounter.getLogFileNumber(checkpointInstant);
        else
          logFileNumber = 1;

        logFile = getLogFileName(logFileNumber);

        // if log file is not there or if it is of the wrong format, create a
        // brand new log file and do not attempt to recover the database

                if (!privExists(logFile))
        {
          if (logDevice != null)
                    {
                        throw StandardException.newException(
                                SQLState.LOG_FILE_NOT_FOUND,
                                logControlFileName.getPath());
                    }

          logErrMsg(MessageService.getTextMessage(
                        MessageId.LOG_MAYBE_INCONSISTENT,
                        logFile.getPath()));

          createNewLog = true;
        }
        else if (!verifyLogFormat(logFile, logFileNumber))
        {
          Monitor.logTextMessage(MessageId.LOG_DELETE_INCOMPATIBLE_FILE, logFile);

          // blow away the log file if possible
                    if (!privDelete(logFile) && logFileNumber == 1)
                    {
                        logErrMsgForDurabilityTestModeNoSync();
            throw StandardException.newException(
                            SQLState.LOG_INCOMPATIBLE_FORMAT, dataDirectory);
                    }

          // If logFileNumber > 1, we are not going to write that
                    // file just yet.  Just leave it be and carry on.  Maybe
                    // when we get there it can be deleted.

          createNewLog = true;
        }
      }

      if (createNewLog)
      {
        // brand new log.  Start from log file number 1.

        // create or overwrite the log control file with an invalid
        // checkpoint instant since there is no checkpoint yet
        if (writeControlFile(logControlFileName,
                   LogCounter.INVALID_LOG_INSTANT))
        {
          firstLogFileNumber = 1;
          logFileNumber = 1;
          if (SanityManager.DEBUG)
          {
            if (SanityManager.DEBUG_ON(TEST_MAX_LOGFILE_NUMBER))
            {
              // set the value to be two less than max possible
              // log number, test case will perform some ops to
              // hit the max number case.
              firstLogFileNumber =
                                LogCounter.MAX_LOGFILE_NUMBER -2;

              logFileNumber = LogCounter.MAX_LOGFILE_NUMBER -2;
            }
          }
          logFile = getLogFileName(logFileNumber);

                    if (privExists(logFile))
          {
            // this log file maybe there because the system may have
            // crashed right after a log switch but did not write
                        // out any log record
            Monitor.logTextMessage(
                            MessageId.LOG_DELETE_OLD_FILE, logFile);

                        if (!privDelete(logFile))
                        {
                            logErrMsgForDurabilityTestModeNoSync();
              throw StandardException.newException(
                                    SQLState.LOG_INCOMPATIBLE_FORMAT,
                                    dataDirectory);
                        }
          }

          // don't need to try to delete it, we know it isn't there
                    firstLog = privRandomAccessFile(logFile, "rw");

          if (!initLogFile(firstLog, logFileNumber, LogCounter.INVALID_LOG_INSTANT))
                    {
            throw StandardException.newException(
                            SQLState.LOG_SEGMENT_NOT_EXIST, logFile.getPath());
                    }

          endPosition = firstLog.getFilePointer();
          lastFlush = firstLog.getFilePointer();

View Full Code Here


   * immediately after the checkpoint before truncations of logs completed.
   * see bug no: 3519 , for more details.
   */

  private void deleteObsoleteLogfiles(){
    StorageFile logDir;
    //find the first  log file number that is  useful
    long firstLogNeeded = getFirstLogNeeded(currentCheckpoint);
        if (firstLogNeeded == -1)
      return;

        // when  backup is in progress, log files that are yet to
        // be copied to the backup should not be deleted,  even
        // if they are not required  for crash recovery.
        if(backupInProgress) {
            long logFileNeededForBackup = logFileToBackup;
            // check if the log file number is yet to be copied
            // to the backup is less than the log file required
            // for crash recovery, if it is then make the first
            // log file that should not be deleted is the log file
            // that is yet to  be copied to the backup. 
            if (logFileNeededForBackup < firstLogNeeded)
                firstLogNeeded = logFileNeededForBackup;
        }

    try{
      logDir = getLogDirectory();
    }catch (StandardException se)
    {
      if (SanityManager.DEBUG)
        if (SanityManager.DEBUG_ON(LogToFile.DBG_FLAG))
          SanityManager.DEBUG(DBG_FLAG, "error opening log segment dir");
      return;
    }
     
    String[] logfiles = privList(logDir);
    if (logfiles != null)
    {
      StorageFile uselessLogFile = null;
      long fileNumber;
      for(int i=0 ; i < logfiles.length; i++)
      {
        // delete the log files that are not needed any more
        if(logfiles[i].startsWith("log") && logfiles[i].endsWith(".dat"))
        {
          fileNumber = Long.parseLong(logfiles[i].substring(3, (logfiles[i].length() -4)));
          if(fileNumber < firstLogNeeded )
          {
            uselessLogFile = logStorageFactory.newStorageFile(logDir, logfiles[i]);
            if (privDelete(uselessLogFile))
            {
              if (SanityManager.DEBUG)
              {
                if (SanityManager.DEBUG_ON(LogToFile.DBG_FLAG))
                  SanityManager.DEBUG(DBG_FLAG, "truncating obsolete log file " + uselessLogFile.getPath());
              }
            }
            else
            {
              if (SanityManager.DEBUG)
              {
                if (SanityManager.DEBUG_ON(LogToFile.DBG_FLAG))
                  SanityManager.DEBUG(DBG_FLAG, "Fail to truncate obsolete log file " + uselessLogFile.getPath());
              }
            }
          }
        }
      }
View Full Code Here

            LogCounter.getLogFileNumber(checkpointInstant) + 1;

        // check if there is a log file after
        // the log file that has the last
        // checkpoint record.
        StorageFile logFileAfterCheckpoint =
            getLogFileName(logFileNumberAfterCheckpoint);
        // System.out.println("checking " + logFileAfterCheckpoint);
        if (privExists(logFileAfterCheckpoint))
            return false;
        else
View Full Code Here

        throws StandardException
    {
        long logFileNumberAfterCheckpoint =
            LogCounter.getLogFileNumber(checkpointInstant) + 1;

        StorageFile logFileAfterCheckpoint =
            getLogFileName(logFileNumberAfterCheckpoint);

        // System.out.println("deleting " + logFileAfterCheckpoint);

        if (privExists(logFileAfterCheckpoint))
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

            if (fileName.equals(filter[j]))
              continue nextFile;
          }
        }

        StorageFile entry = storageFactory.newStorageFile(from, fileName);

        if (entry.isDirectory())
        {
                    if(copySubDirs) {
                        if (!copyDirectory( storageFactory, entry,
                                            new File(to,fileName), buffer,
                                            filter, copySubDirs))
View Full Code Here

      }
   
      backupInProgress = true;
   
      // copy the control files.
      StorageFile fromFile;
      File toFile;
      // copy the log control file
      fromFile = getControlFileName();
      toFile = new File(toDir,fromFile.getName());
      if(!privCopyFile(fromFile, toFile))
      {
        throw StandardException.newException(
                    SQLState.RAWSTORE_ERROR_COPYING_FILE, fromFile, toFile);
      }

      // copy the log mirror control file
      fromFile = getMirrorControlFileName();
      toFile = new File(toDir,fromFile.getName());
      if(!privCopyFile(fromFile, toFile))
      {
        throw StandardException.newException(
                    SQLState.RAWSTORE_ERROR_COPYING_FILE, fromFile, toFile);
      }
View Full Code Here

        throws StandardException
  {

    while(logFileToBackup <= lastLogFileToBackup)
    {
      StorageFile fromFile = getLogFileName(logFileToBackup);
      File toFile = new File(toDir, fromFile.getName());
      if(!privCopyFile(fromFile, toFile))
      {
        throw StandardException.newException(
                    SQLState.RAWSTORE_ERROR_COPYING_FILE, fromFile, toFile);
      }
View Full Code Here

                        properties.getProperty(Property.LOG_DEVICE_AT_BACKUP);
        }
      }  
       
            getLogStorageFactory();
      StorageFile logDir;
      logDir = logStorageFactory.newStorageFile(
                             LogFactory.LOG_DIRECTORY_NAME);
       
      //remove the log directory in case of restoreFrom
      //if it exist, this happens if the log device is on seperate
      //location than the db home.
      if (isRestoreFrom && logDevice != null)
      {
        if(!privRemoveDirectory(logDir))
        {
          //it may be just a file, try deleting it
          if(!privDelete(logDir))
                    {
            throw StandardException.newException(
                            SQLState.UNABLE_TO_REMOVE_DATA_DIRECTORY,
                            getLogDirPath( logDir));
                    }
        }
      }

            // if it is a create/restore from backup,
            // create the log directory.
            if (isCreateFrom || isRestoreFrom) {
                createLogDirectory();
            }

      File backupLogDir = new File(backupPath, LogFactory.LOG_DIRECTORY_NAME);
      String[] logfilelist = privList(backupLogDir);
      if(logfilelist !=null)
      {
        for (int i = 0; i < logfilelist.length; i++)
        {
          File blogFile = new File(backupLogDir, logfilelist[i]);
          StorageFile clogFile = logStorageFactory.newStorageFile(logDir, logfilelist[i]);
          if(!privCopyFile(blogFile , clogFile))
          {
            throw
              StandardException.newException(SQLState.UNABLE_TO_COPY_LOG_FILE, blogFile, clogFile);
          }
View Full Code Here

    private void privGetJBMSLockOnDB() throws StandardException
    {
        boolean fileLockExisted = false;
        String blownUUID = null;

        StorageFile fileLock = storageFactory.newStorageFile( DB_LOCKFILE_NAME);

        try
        {
            // assume we are not read only
            // SECURITY PERMISSION MP1
            if (fileLock.exists())
            {
                fileLockExisted = true;

                // see what it says in case we cannot count on delete failing
                // when someone else have an opened file descriptor.
                // I may be blowing this JBMS's lock away
                // SECURITY PERMISSION MP1
                // SECURITY PERMISSION OP4
                fileLockOnDB = fileLock.getRandomAccessFile( "rw");
                try
                {
                    blownUUID = fileLockOnDB.readUTF();
                }
                catch (IOException ioe)
                {
                    // The previous owner of the lock may have died before
                    // finish writing its UUID down.
                    fileLockExisted = false;
                }

                fileLockOnDB.close();
                fileLockOnDB = null;

                // SECURITY PERMISSION OP5
                if (!fileLock.delete())
                {
                    throw StandardException.newException(
                        SQLState.DATA_MULTIPLE_JBMS_ON_DB,
                        databaseDirectory);
                }
            }

            // if file does not exists, we grab it immediately - there is a
            // possibility that some other JBMS got to it sooner than we do,
            // check the UUID after we write it to make sure
            // SECURITY PERMISSION MP1
            // SECURITY PERMISSION OP5
            fileLockOnDB = fileLock.getRandomAccessFile( "rw");

            // write it out for future reference
            fileLockOnDB.writeUTF(myUUID.toString());

            fileLockOnDB.sync( false);
            fileLockOnDB.seek(0);
            // check the UUID
            UUID checkUUID = uuidFactory.recreateUUID(fileLockOnDB.readUTF());
            if (!checkUUID.equals(myUUID))
            {
                throw StandardException.newException(
                    SQLState.DATA_MULTIPLE_JBMS_ON_DB, databaseDirectory);
            }
        }
        catch (IOException ioe)
        {
            // probably a read only db, don't do anything more
            readOnly = true;
            try
            {
                if (fileLockOnDB != null)
                    fileLockOnDB.close();
            }
            catch (IOException ioe2)
            { /* did the best I could */ }
            fileLockOnDB = null;

            return;
        }

        if (fileLock.delete())
        {
            // if I can delete it while I am holding a opened file descriptor,
            // then the file lock is unreliable - send out a warning if I
            // have blown off another JBMS's lock on the DB

            Object[] args = new Object[3];
            args[0] = myUUID;
            args[1] = databaseDirectory;
            args[2] = blownUUID;

            //Try the exlcusive file lock method approach available in jdk1.4 or
            //above jvms where delete machanism  does not reliably prevent
            //double booting of derby databases. If we don't get a reliable
            //exclusive lock still we send out a warning.

            int exLockStatus = StorageFile.NO_FILE_LOCK_SUPPORT ;
            //If user has chosen to force lock option don't bother
            //about applying exclusive file lock mechanism
            if(!throwDBlckException)
            {
                exFileLock   =
                    storageFactory.newStorageFile( DB_EX_LOCKFILE_NAME);
                exLockStatus = exFileLock.getExclusiveFileLock();
            }

            if (exLockStatus == StorageFile.NO_FILE_LOCK_SUPPORT)
            {
                if (fileLockExisted && !throwDBlckException)
                {

                    StandardException multipleJBMSWarning =
                      StandardException.newException(
                          SQLState.DATA_MULTIPLE_JBMS_WARNING, args);

                    String warningMsg =
                      MessageService.getCompleteMessage(
                          SQLState.DATA_MULTIPLE_JBMS_WARNING, args);

                    logMsg(warningMsg);

                    // RESOLVE - need warning support.  Output to
                    // system.err.println rather than just send warning
                    // message to derby.log.
                    System.err.println(warningMsg);

                }
            }

            // filelock is unreliable, but we should at least leave a file
            // there to warn the next person
            try
            {
                // the existing fileLockOnDB file descriptor may already be
                // deleted by the delete call, close it and create the file
                // again
                if(fileLockOnDB != null)
                    fileLockOnDB.close();
                fileLockOnDB = fileLock.getRandomAccessFile( "rw");

                // write it out for future reference
                fileLockOnDB.writeUTF(myUUID.toString());

                fileLockOnDB.sync( false);
View Full Code Here

TOP

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

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.