Search in sources :

Example 1 with WritableStorageFactory

use of org.apache.derby.io.WritableStorageFactory in project derby by apache.

the class StorageFactoryService method removeServiceRoot.

// end of getDirectoryPath
public boolean removeServiceRoot(final String serviceName) {
    if (!(rootStorageFactory instanceof WritableStorageFactory))
        return false;
    try {
        return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

            public Object run() throws StandardException, IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
                StorageFactory storageFactory = privGetStorageFactoryInstance(true, serviceName, null, null);
                try {
                    if (SanityManager.DEBUG) {
                        // Run this through getCanonicalServiceName as
                        // an extra sanity check. Prepending the
                        // protocol lead in to the canonical name from
                        // the storage factory should be enough.
                        String tmpCanonical = getCanonicalServiceName(getProtocolLeadIn() + storageFactory.getCanonicalName());
                        // These should give the same result.
                        SanityManager.ASSERT(tmpCanonical.equals(getProtocolLeadIn() + storageFactory.getCanonicalName()));
                        SanityManager.ASSERT(serviceName.equals(tmpCanonical), "serviceName = " + serviceName + " ; protocolLeadIn + " + "storageFactory.getCanoicalName = " + tmpCanonical);
                    }
                    StorageFile serviceDirectory = storageFactory.newStorageFile(null);
                    return serviceDirectory.deleteAll() ? this : null;
                } finally {
                    storageFactory.shutdown();
                }
            }
        }) != null;
    } catch (PrivilegedActionException pae) {
        return false;
    }
}
Also used : WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) StorageFactory(org.apache.derby.io.StorageFactory) PrivilegedActionException(java.security.PrivilegedActionException) WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) StorageFile(org.apache.derby.io.StorageFile) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction)

Example 2 with WritableStorageFactory

use of org.apache.derby.io.WritableStorageFactory in project derby by apache.

the class StorageFactoryService method recreateServiceRoot.

/*
	**Recreates service root if required depending on which of the following
	**attribute is specified on the conection URL:
	** Attribute.CREATE_FROM (Create database from backup if it does not exist):
	** When a database not exist, the service(database) root is created
	** and the PersistentService.PROPERTIES_NAME (service.properties) file
	** is restored from the backup.
	** Attribute.RESTORE_FROM (Delete the whole database if it exists and then restore
	** it from backup)
	** Existing database root  is deleted and the new the service(database) root is created.
	** PersistentService.PROPERTIES_NAME (service.properties) file is restored from the backup.
	** Attribute.ROLL_FORWARD_RECOVERY_FROM:(Perform Rollforward Recovery;
	** except for the log directory everthing else is replced  by the copy  from
	** backup. log files in the backup are copied to the existing online log
	** directory.):
	** When a database not exist, the service(database) root is created.
	** PersistentService.PROPERTIES_NAME (service.properties) file is deleted
	** from the service dir and  recreated with the properties from backup.
	*/
protected String recreateServiceRoot(final String serviceName, Properties properties) throws StandardException {
    // if there are no propertues then nothing to do in this routine
    if (properties == null) {
        return null;
    }
    // location where backup copy of service properties available
    String restoreFrom;
    boolean createRoot = false;
    boolean deleteExistingRoot = false;
    // check if user wants to create a database from a backup copy
    restoreFrom = properties.getProperty(Attribute.CREATE_FROM);
    if (restoreFrom != null) {
        // create root dicretory if it  does not exist.
        createRoot = true;
        deleteExistingRoot = false;
    } else {
        // check if user requested a complete restore(version recovery) from backup
        restoreFrom = properties.getProperty(Attribute.RESTORE_FROM);
        // create root dir if it does not exists and  if there exists one already delete and recreate
        if (restoreFrom != null) {
            createRoot = true;
            deleteExistingRoot = true;
        } else {
            // check if user has requested roll forward recovery using a backup
            restoreFrom = properties.getProperty(Attribute.ROLL_FORWARD_RECOVERY_FROM);
            if (restoreFrom != null) {
                // failed and user is trying to restore it some other device.
                try {
                    if (AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

                        public Object run() throws IOException, StandardException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
                            StorageFactory storageFactory = privGetStorageFactoryInstance(true, serviceName, null, null);
                            try {
                                StorageFile serviceDirectory = storageFactory.newStorageFile(null);
                                return serviceDirectory.exists() ? this : null;
                            } finally {
                                storageFactory.shutdown();
                            }
                        }
                    }) == null) {
                        createRoot = true;
                        deleteExistingRoot = false;
                    }
                } catch (PrivilegedActionException pae) {
                    throw Monitor.exceptionStartingModule((IOException) pae.getException());
                }
            }
        }
    }
    // restore the service properties from backup
    if (restoreFrom != null) {
        // First make sure backup service directory exists in the specified path
        File backupRoot = new File(restoreFrom);
        if (fileExists(backupRoot)) {
            // First make sure backup have service.properties
            File bserviceProp = new File(restoreFrom, PersistentService.PROPERTIES_NAME);
            if (fileExists(bserviceProp)) {
                // create service root if required
                if (createRoot)
                    createServiceRoot(serviceName, deleteExistingRoot);
                try {
                    AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

                        public Object run() throws IOException, StandardException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
                            WritableStorageFactory storageFactory = (WritableStorageFactory) privGetStorageFactoryInstance(true, serviceName, null, null);
                            try {
                                StorageFile cserviceProp = storageFactory.newStorageFile(PersistentService.PROPERTIES_NAME);
                                if (cserviceProp.exists())
                                    if (!cserviceProp.delete())
                                        throw StandardException.newException(SQLState.UNABLE_TO_DELETE_FILE, cserviceProp);
                                return null;
                            } finally {
                                storageFactory.shutdown();
                            }
                        }
                    });
                } catch (PrivilegedActionException pae) {
                    throw Monitor.exceptionStartingModule((IOException) pae.getException());
                }
            } else
                throw StandardException.newException(SQLState.PROPERTY_FILE_NOT_FOUND_IN_BACKUP, bserviceProp);
        } else
            throw StandardException.newException(SQLState.SERVICE_DIRECTORY_NOT_IN_BACKUP, backupRoot);
        properties.put(Property.IN_RESTORE_FROM_BACKUP, "True");
        if (createRoot)
            properties.put(Property.DELETE_ROOT_ON_ERROR, "True");
    }
    return restoreFrom;
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) StandardException(org.apache.derby.shared.common.error.StandardException) WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) StorageFactory(org.apache.derby.io.StorageFactory) StorageFile(org.apache.derby.io.StorageFile) StorageFile(org.apache.derby.io.StorageFile) StorageRandomAccessFile(org.apache.derby.io.StorageRandomAccessFile) File(java.io.File)

Example 3 with WritableStorageFactory

use of org.apache.derby.io.WritableStorageFactory in project derby by apache.

the class BaseDataFileFactory method boot.

public void boot(boolean create, Properties startParams) throws StandardException {
    jbmsVersion = getMonitor().getEngineVersion();
    jvmVersion = buildJvmVersion();
    osInfo = buildOSinfo();
    jarCPath = jarClassPath(getClass());
    dataDirectory = startParams.getProperty(PersistentService.ROOT);
    UUIDFactory uf = getMonitor().getUUIDFactory();
    identifier = uf.createUUID();
    PersistentService ps = getMonitor().getServiceType(this);
    try {
        storageFactory = ps.getStorageFactoryInstance(true, dataDirectory, startParams.getProperty(Property.STORAGE_TEMP_DIRECTORY, PropertyUtil.getSystemProperty(Property.STORAGE_TEMP_DIRECTORY)), identifier.toANSIidentifier());
    } catch (IOException ioe) {
        if (create) {
            throw StandardException.newException(SQLState.SERVICE_DIRECTORY_CREATE_ERROR, ioe, dataDirectory);
        } else {
            throw StandardException.newException(SQLState.DATABASE_NOT_FOUND, ioe, dataDirectory);
        }
    }
    // you can't encrypt a database if the Lucene plugin is loaded
    if (luceneLoaded()) {
        String encryptionProp = startParams.getProperty(Attribute.DATA_ENCRYPTION);
        if ((encryptionProp != null) && "TRUE".equals(encryptionProp.toUpperCase())) {
            throw StandardException.newException(SQLState.LUCENE_ENCRYPTED_DB);
        }
    }
    if (storageFactory instanceof WritableStorageFactory)
        writableStorageFactory = (WritableStorageFactory) storageFactory;
    actionCode = BOOT_ACTION;
    try {
        AccessController.doPrivileged(this);
    } catch (PrivilegedActionException pae) {
    // BOOT_ACTION does not throw any exceptions.
    }
    String value = startParams.getProperty(Property.FORCE_DATABASE_LOCK, PropertyUtil.getSystemProperty(Property.FORCE_DATABASE_LOCK));
    throwDBlckException = Boolean.valueOf((value != null ? value.trim() : value)).booleanValue();
    if (// read only db, not interested in filelock
    !isReadOnly())
        getJBMSLockOnDB(identifier, uf, dataDirectory);
    // If the database is being restored/created from backup
    // the restore the data directory(seg*) from backup
    String restoreFrom = null;
    restoreFrom = startParams.getProperty(Attribute.CREATE_FROM);
    if (restoreFrom == null)
        restoreFrom = startParams.getProperty(Attribute.RESTORE_FROM);
    if (restoreFrom == null)
        restoreFrom = startParams.getProperty(Attribute.ROLL_FORWARD_RECOVERY_FROM);
    if (restoreFrom != null) {
        try {
            // restoreFrom and createFrom operations also need to know if database
            // is encrypted
            String dataEncryption = startParams.getProperty(Attribute.DATA_ENCRYPTION);
            databaseEncrypted = Boolean.valueOf(dataEncryption).booleanValue();
            restoreDataDirectory(restoreFrom);
        } catch (StandardException se) {
            releaseJBMSLockOnDB();
            throw se;
        }
    }
    logMsg(LINE);
    String messageID = (isReadOnly()) ? MessageId.STORE_BOOT_MSG_READ_ONLY : MessageId.STORE_BOOT_MSG;
    boolean logBootTrace = Boolean.valueOf(startParams.getProperty(Property.LOG_BOOT_TRACE, PropertyUtil.getSystemProperty(Property.LOG_BOOT_TRACE))).booleanValue();
    logMsg(new Date() + MessageService.getTextMessage(messageID, jbmsVersion, identifier, dataDirectory, // cast to Object so we get object hash code
    (Object) this.getClass().getClassLoader(), jarCPath));
    // Log the JVM version info
    logMsg(jvmVersion);
    // Log the OS info
    logMsg(osInfo);
    // Log derby.system.home It will have null value if user didn't set it
    logMsg(Property.SYSTEM_HOME_PROPERTY + "=" + PropertyUtil.getSystemProperty(Property.SYSTEM_HOME_PROPERTY));
    // Log properties related to redirection of derby.log
    String target = PropertyUtil.getSystemProperty(Property.ERRORLOG_STYLE_PROPERTY);
    if (target != null)
        logMsg(Property.ERRORLOG_STYLE_PROPERTY + "=" + target);
    target = PropertyUtil.getSystemProperty(Property.ERRORLOG_FILE_PROPERTY);
    if (target != null)
        logMsg(Property.ERRORLOG_FILE_PROPERTY + "=" + target);
    target = PropertyUtil.getSystemProperty(Property.ERRORLOG_METHOD_PROPERTY);
    if (target != null)
        logMsg(Property.ERRORLOG_METHOD_PROPERTY + "=" + target);
    target = PropertyUtil.getSystemProperty(Property.ERRORLOG_FIELD_PROPERTY);
    if (target != null)
        logMsg(Property.ERRORLOG_FIELD_PROPERTY + "=" + target);
    if (logBootTrace)
        Monitor.logThrowable(new Throwable("boot trace"));
    uf = null;
    CacheFactory cf = (CacheFactory) startSystemModule(org.apache.derby.shared.common.reference.Module.CacheFactory);
    // Initialize the page cache
    int pageCacheSize = getIntParameter(RawStoreFactory.PAGE_CACHE_SIZE_PARAMETER, null, RawStoreFactory.PAGE_CACHE_SIZE_DEFAULT, RawStoreFactory.PAGE_CACHE_SIZE_MINIMUM, RawStoreFactory.PAGE_CACHE_SIZE_MAXIMUM);
    pageCache = cf.newCacheManager(this, "PageCache", pageCacheSize / 2, pageCacheSize);
    // Initialize the container cache
    int fileCacheSize = getIntParameter(RawStoreFactory.CONTAINER_CACHE_SIZE_PARAMETER, null, RawStoreFactory.CONTAINER_CACHE_SIZE_DEFAULT, RawStoreFactory.CONTAINER_CACHE_SIZE_MINIMUM, RawStoreFactory.CONTAINER_CACHE_SIZE_MAXIMUM);
    containerCache = cf.newCacheManager(this, "ContainerCache", fileCacheSize / 2, fileCacheSize);
    // Register MBeans that allow users to monitor the page cache
    // and the container cache.
    pageCache.registerMBean(dataDirectory);
    containerCache.registerMBean(dataDirectory);
    if (create) {
        String noLog = startParams.getProperty(Property.CREATE_WITH_NO_LOG);
        inCreateNoLog = (noLog != null && Boolean.valueOf(noLog).booleanValue());
    }
    droppedTableStubInfo = new Hashtable<LogInstant, Object[]>();
    // writes during checkpoint
    if (Property.DURABILITY_TESTMODE_NO_SYNC.equalsIgnoreCase(PropertyUtil.getSystemProperty(Property.DURABILITY_PROPERTY))) {
        // - disable syncing of data during checkpoint.
        dataNotSyncedAtCheckpoint = true;
        // log message stating that derby.system.durability
        // is set to a mode, where syncs wont be forced and the
        // possible consequences of setting this mode
        Monitor.logMessage(MessageService.getTextMessage(MessageId.STORE_DURABILITY_TESTMODE_NO_SYNC, Property.DURABILITY_PROPERTY, Property.DURABILITY_TESTMODE_NO_SYNC));
    } 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.
        // debug only flag - disable syncing of data during checkpoint.
        dataNotSyncedAtCheckpoint = PropertyUtil.getSystemBoolean(Property.STORAGE_DATA_NOT_SYNCED_AT_CHECKPOINT);
        if (dataNotSyncedAtCheckpoint)
            Monitor.logMessage("Warning: " + Property.STORAGE_DATA_NOT_SYNCED_AT_CHECKPOINT + "set to true.");
    }
    fileHandler = new RFResource(this);
}
Also used : LogInstant(org.apache.derby.iapi.store.raw.log.LogInstant) PrivilegedActionException(java.security.PrivilegedActionException) UUIDFactory(org.apache.derby.iapi.services.uuid.UUIDFactory) WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) IOException(java.io.IOException) Date(java.util.Date) StandardException(org.apache.derby.shared.common.error.StandardException) PersistentService(org.apache.derby.iapi.services.monitor.PersistentService) CacheFactory(org.apache.derby.iapi.services.cache.CacheFactory)

Example 4 with WritableStorageFactory

use of org.apache.derby.io.WritableStorageFactory in project derby by apache.

the class RawStore method boot.

public void boot(boolean create, Properties properties) throws StandardException {
    boolean transformExistingData = false;
    boolean inReplicationSlaveMode = false;
    String slave = properties.getProperty(SlaveFactory.REPLICATION_MODE);
    if (slave != null && slave.equals(SlaveFactory.SLAVE_MODE)) {
        inReplicationSlaveMode = true;
    }
    DaemonFactory daemonFactory = (DaemonFactory) startSystemModule(org.apache.derby.shared.common.reference.Module.DaemonFactory);
    rawStoreDaemon = daemonFactory.createNewDaemon("rawStoreDaemon");
    xactFactory = (TransactionFactory) bootServiceModule(create, this, getTransactionFactoryModule(), properties);
    dataFactory = (DataFactory) bootServiceModule(create, this, getDataFactoryModule(), properties);
    storageFactory = dataFactory.getStorageFactory();
    String restoreFromBackup = null;
    if (properties != null) {
        // check if this is a restore from a backup copy.
        restoreFromBackup = properties.getProperty(Attribute.CREATE_FROM);
        if (restoreFromBackup == null)
            restoreFromBackup = properties.getProperty(Attribute.RESTORE_FROM);
        if (restoreFromBackup == null)
            restoreFromBackup = properties.getProperty(Attribute.ROLL_FORWARD_RECOVERY_FROM);
    }
    // setup database encryption engines.
    if (create) {
        transformExistingData = setupEncryptionEngines(create, properties);
        if (SanityManager.DEBUG) {
            SanityManager.ASSERT(!transformExistingData, "no crypto data transformation for a new db");
        }
    }
    // let everyone knows who their rawStoreFactory is and they can use it
    // to get to other modules
    // pass in create and properties to dataFactory so it can boot the log
    // factory
    dataFactory.setRawStoreFactory(this, create, properties);
    xactFactory.setRawStoreFactory(this);
    if (properties instanceof UpdateServiceProperties) {
        if (storageFactory instanceof WritableStorageFactory)
            ((UpdateServiceProperties) properties).setStorageFactory((WritableStorageFactory) storageFactory);
    }
    // log factory is booted by the data factory
    logFactory = (LogFactory) findServiceModule(this, getLogFactoryModule());
    // if this is a restore from backup, restore the jar files.
    if (restoreFromBackup != null) {
        restoreRemainingFromBackup(restoreFromBackup);
    }
    // If the log is at another location, make sure  service.properties
    // file has it.
    String logDevice = properties.getProperty(Attribute.LOG_DEVICE);
    if (logDevice != null) {
        if (// We do not care about log location if read only
        !isReadOnly() && (create || !logDevice.equals(logFactory.getCanonicalLogPath()) || restoreFromBackup != null)) {
            // get the real location from the log factory
            properties.put(Attribute.LOG_DEVICE, logFactory.getCanonicalLogPath());
            // make the log device param stored in backup is same as current log device.
            properties.put(Property.LOG_DEVICE_AT_BACKUP, logFactory.getCanonicalLogPath());
        }
    } else {
        // into service.propertues in such cases.
        if (restoreFromBackup != null && logFactory.getCanonicalLogPath() != null) {
            // logdevice might have got changed because of backup restore.
            properties.put(Attribute.LOG_DEVICE, logFactory.getCanonicalLogPath());
        } else {
            // might have been OS copy restore. We default log to db home
            properties.remove(Property.LOG_DEVICE_AT_BACKUP);
        }
    }
    // restore from. This marks the end of restore from backup.
    if (restoreFromBackup != null) {
        ((UpdateServiceProperties) properties).saveServiceProperties();
    }
    // setup database encryption engine
    if (!create) {
        // existing database.
        if (properties.getProperty(RawStoreFactory.DB_ENCRYPTION_STATUS) != null) {
            handleIncompleteDbCryptoOperation(properties);
        }
        transformExistingData = setupEncryptionEngines(create, properties);
    }
    if (isEncryptedDatabase) {
        // let log factory know if the database is encrypted .
        logFactory.setDatabaseEncrypted(true, false);
        // let data factory know if the database is encrypted.
        dataFactory.setDatabaseEncrypted(true);
    }
    // RawStoreFactory is used by LogFactory.recover() and by
    // SlaveFactory.startSlave (for the SlaveFactory case, it is
    // only used if the database is encrypted)
    logFactory.setRawStoreFactory(this);
    // when in replication slave mode.
    if (inReplicationSlaveMode) {
        // The LogFactory has already been booted in slave mode.
        // Can now start slave replication by booting the
        // SlaveFactory service
        slaveFactory = (SlaveFactory) bootServiceModule(create, this, getSlaveFactoryModule(), properties);
        slaveFactory.startSlave(this, logFactory);
    }
    // no need to tell log factory which raw store factory it belongs to
    // since this is passed into the log factory for recovery
    // after the factories are loaded, recover the database
    logFactory.recover(dataFactory, xactFactory);
    // a new alogorithm then do that now.
    if (transformExistingData) {
        applyBulkCryptoOperation(properties, newCipherFactory);
    }
}
Also used : WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) UpdateServiceProperties(org.apache.derby.impl.services.monitor.UpdateServiceProperties) DaemonFactory(org.apache.derby.iapi.services.daemon.DaemonFactory)

Example 5 with WritableStorageFactory

use of org.apache.derby.io.WritableStorageFactory in project derby by apache.

the class CorruptDiskStorageFactory method getRealStorageFactory.

/*
	 * returns the real storage factory to which all the call should be 
	 * delegated from the proxy methods.  
	 */
WritableStorageFactory getRealStorageFactory() {
    String dirStorageFactoryClass = "org.apache.derby.impl.io.DirStorageFactory";
    WritableStorageFactory storageFactory = null;
    try {
        Class<?> storageFactoryClass = Class.forName(dirStorageFactoryClass);
        storageFactory = (WritableStorageFactory) storageFactoryClass.getConstructor().newInstance();
    } catch (Exception e) {
        System.out.println("Failed to instantiate the disk storeage classes");
        e.printStackTrace();
    }
    return storageFactory;
}
Also used : WritableStorageFactory(org.apache.derby.io.WritableStorageFactory)

Aggregations

WritableStorageFactory (org.apache.derby.io.WritableStorageFactory)5 PrivilegedActionException (java.security.PrivilegedActionException)3 IOException (java.io.IOException)2 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)2 StorageFactory (org.apache.derby.io.StorageFactory)2 StorageFile (org.apache.derby.io.StorageFile)2 StandardException (org.apache.derby.shared.common.error.StandardException)2 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Date (java.util.Date)1 CacheFactory (org.apache.derby.iapi.services.cache.CacheFactory)1 DaemonFactory (org.apache.derby.iapi.services.daemon.DaemonFactory)1 PersistentService (org.apache.derby.iapi.services.monitor.PersistentService)1 UUIDFactory (org.apache.derby.iapi.services.uuid.UUIDFactory)1 LogInstant (org.apache.derby.iapi.store.raw.log.LogInstant)1 UpdateServiceProperties (org.apache.derby.impl.services.monitor.UpdateServiceProperties)1 StorageRandomAccessFile (org.apache.derby.io.StorageRandomAccessFile)1