Search in sources :

Example 66 with StandardException

use of org.apache.derby.shared.common.error.StandardException in project derby by apache.

the class IndexStatisticsDaemonImpl method updateIndexStatsMinion.

/**
 * Updates the index statistics for the given table and the specified
 * indexes.
 * <p>
 * <strong>API note</strong>: Using {@code null} to update the statistics
 * for all conglomerates is preferred over explicitly passing an array with
 * all the conglomerates for the table. Doing so allows for some
 * optimizations, and will cause a disposable statistics check to be
 * performed.
 *
 * @param lcc language connection context used to perform the work
 * @param td the table to update index stats for
 * @param cds the conglomerates to update statistics for (non-index
 *      conglomerates will be ignored), {@code null} means all indexes
 * @param asBackgroundTask whether the updates are done automatically as
 *      part of a background task or if explicitly invoked by the user
 * @throws StandardException if something goes wrong
 */
private void updateIndexStatsMinion(LanguageConnectionContext lcc, TableDescriptor td, ConglomerateDescriptor[] cds, boolean asBackgroundTask) throws StandardException {
    // can only properly identify disposable stats if cds == null,
    // which means we are processing all indexes on the conglomerate.
    final boolean identifyDisposableStats = (cds == null);
    // Fetch descriptors if we're updating statistics for all indexes.
    if (cds == null) {
        cds = td.getConglomerateDescriptors();
    }
    // Extract/derive information from the table descriptor
    long[] conglomerateNumber = new long[cds.length];
    ExecIndexRow[] indexRow = new ExecIndexRow[cds.length];
    TransactionController tc = lcc.getTransactionExecute();
    ConglomerateController heapCC = tc.openConglomerate(td.getHeapConglomerateId(), false, 0, TransactionController.MODE_RECORD, asBackgroundTask ? TransactionController.ISOLATION_READ_UNCOMMITTED : TransactionController.ISOLATION_REPEATABLE_READ);
    // create a list of indexes that should have statistics, by looking
    // at all indexes on the conglomerate, and conditionally skipping
    // unique single column indexes.  This set is the "non disposable
    // stat list".
    UUID[] non_disposable_objectUUID = new UUID[cds.length];
    try {
        for (int i = 0; i < cds.length; i++) {
            // Skip non-index conglomerates
            if (!cds[i].isIndex()) {
                conglomerateNumber[i] = -1;
                continue;
            }
            IndexRowGenerator irg = cds[i].getIndexDescriptor();
            // or we are running in soft-upgrade-mode on a pre 10.9 db.
            if (skipDisposableStats) {
                if (irg.isUnique() && irg.numberOfOrderedColumns() == 1) {
                    conglomerateNumber[i] = -1;
                    continue;
                }
            }
            // at this point have found a stat for an existing
            // index which is not a single column unique index, add it
            // to the list of "non disposable stats"
            conglomerateNumber[i] = cds[i].getConglomerateNumber();
            non_disposable_objectUUID[i] = cds[i].getUUID();
            indexRow[i] = irg.getNullIndexRow(td.getColumnDescriptorList(), heapCC.newRowLocationTemplate());
        }
    } finally {
        heapCC.close();
    }
    if (identifyDisposableStats) {
        // Note this loop is not controlled by the skipDisposableStats
        // flag.  The above loop controls if we drop single column unique
        // index stats or not.  In all cases we are going to drop
        // stats with no associated index (orphaned stats).
        List<StatisticsDescriptor> existingStats = td.getStatistics();
        StatisticsDescriptor[] stats = (StatisticsDescriptor[]) existingStats.toArray(new StatisticsDescriptor[existingStats.size()]);
        // those entries that don't have a matching conglomerate in the
        for (int si = 0; si < stats.length; si++) {
            UUID referencedIndex = stats[si].getReferenceID();
            boolean isValid = false;
            for (int ci = 0; ci < conglomerateNumber.length; ci++) {
                if (referencedIndex.equals(non_disposable_objectUUID[ci])) {
                    isValid = true;
                    break;
                }
            }
            // mechanism in case of another bug like DERBY-5681 in Derby.
            if (!isValid) {
                String msg = "dropping disposable statistics entry " + stats[si].getUUID() + " for index " + stats[si].getReferenceID() + " (cols=" + stats[si].getColumnCount() + ")";
                logAlways(td, null, msg);
                trace(1, msg + " on table " + stats[si].getTableUUID());
                DataDictionary dd = lcc.getDataDictionary();
                if (!lcc.dataDictionaryInWriteMode()) {
                    dd.startWriting(lcc);
                }
                dd.dropStatisticsDescriptors(td.getUUID(), stats[si].getReferenceID(), tc);
                if (asBackgroundTask) {
                    lcc.internalCommit(true);
                }
            }
        }
    }
    // [x][0] = conglomerate number, [x][1] = start time, [x][2] = stop time
    long[][] scanTimes = new long[conglomerateNumber.length][3];
    int sci = 0;
    for (int indexNumber = 0; indexNumber < conglomerateNumber.length; indexNumber++) {
        if (conglomerateNumber[indexNumber] == -1)
            continue;
        // Check if daemon has been disabled.
        if (asBackgroundTask) {
            if (isShuttingDown()) {
                break;
            }
        }
        scanTimes[sci][0] = conglomerateNumber[indexNumber];
        scanTimes[sci][1] = System.currentTimeMillis();
        // Subtract one for the RowLocation added for indexes.
        int numCols = indexRow[indexNumber].nColumns() - 1;
        long[] cardinality = new long[numCols];
        KeyComparator cmp = new KeyComparator(indexRow[indexNumber]);
        /* Read uncommitted, with record locking. Actually CS store may
               not hold record locks */
        GroupFetchScanController gsc = tc.openGroupFetchScan(conglomerateNumber[indexNumber], // hold
        false, 0, // locking
        TransactionController.MODE_RECORD, TransactionController.ISOLATION_READ_UNCOMMITTED, // scancolumnlist-- want everything.
        null, // startkeyvalue-- start from the beginning.
        null, 0, // qualifiers, none!
        null, // stopkeyvalue,
        null, 0);
        try {
            int rowsFetched = 0;
            boolean giving_up_on_shutdown = false;
            while ((rowsFetched = cmp.fetchRows(gsc)) > 0) {
                // I/O that is processed as a convenient point.
                if (asBackgroundTask) {
                    if (isShuttingDown()) {
                        giving_up_on_shutdown = true;
                        break;
                    }
                }
                for (int i = 0; i < rowsFetched; i++) {
                    int whichPositionChanged = cmp.compareWithPrevKey(i);
                    if (whichPositionChanged >= 0) {
                        for (int j = whichPositionChanged; j < numCols; j++) cardinality[j]++;
                    }
                }
            }
            if (giving_up_on_shutdown)
                break;
            gsc.setEstimatedRowCount(cmp.getRowCount());
        } finally // try
        {
            gsc.close();
            gsc = null;
        }
        scanTimes[sci++][2] = System.currentTimeMillis();
        // We have scanned the indexes, so let's give this a few attempts
        // before giving up.
        int retries = 0;
        while (true) {
            try {
                writeUpdatedStats(lcc, td, non_disposable_objectUUID[indexNumber], cmp.getRowCount(), cardinality, asBackgroundTask);
                break;
            } catch (StandardException se) {
                retries++;
                if (se.isLockTimeout() && retries < 3) {
                    trace(2, "lock timeout when writing stats, retrying");
                    sleep(100 * retries);
                } else {
                    // o too many lock timeouts
                    throw se;
                }
            }
        }
    }
    log(asBackgroundTask, td, fmtScanTimes(scanTimes));
}
Also used : StatisticsDescriptor(org.apache.derby.iapi.sql.dictionary.StatisticsDescriptor) ConglomerateController(org.apache.derby.iapi.store.access.ConglomerateController) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) GroupFetchScanController(org.apache.derby.iapi.store.access.GroupFetchScanController) ExecIndexRow(org.apache.derby.iapi.sql.execute.ExecIndexRow) StandardException(org.apache.derby.shared.common.error.StandardException) IndexRowGenerator(org.apache.derby.iapi.sql.dictionary.IndexRowGenerator) TransactionController(org.apache.derby.iapi.store.access.TransactionController) UUID(org.apache.derby.catalog.UUID)

Example 67 with StandardException

use of org.apache.derby.shared.common.error.StandardException in project derby by apache.

the class IndexStatisticsDaemonImpl method processingLoop.

/**
 * Main processing loop which will compute statistics until the queue
 * of scheduled work units has been drained.
 */
private void processingLoop() {
    // If we don't have a connection to the database, create one.
    if (daemonLCC == null) {
        try {
            daemonLCC = db.setupConnection(ctxMgr, dbOwner, null, databaseName);
            // Initialize the lcc/transaction.
            // TODO: Would be nice to name the transaction.
            daemonLCC.setIsolationLevel(Connection.TRANSACTION_READ_UNCOMMITTED);
            // Don't wait for any locks.
            daemonLCC.getTransactionExecute().setNoLockWait(true);
        } catch (StandardException se) {
            log(AS_BACKGROUND_TASK, null, se, "failed to initialize index statistics updater");
            return;
        }
    }
    TransactionController tc = null;
    try {
        tc = daemonLCC.getTransactionExecute();
        trace(0, "worker thread started (xid=" + tc.getTransactionIdString() + ")");
        TableDescriptor td = null;
        long start = 0;
        while (true) {
            synchronized (queue) {
                if (daemonDisabled) {
                    // Clean the lcc and exit.
                    try {
                        tc.destroy();
                    } catch (ShutdownException se) {
                    // Ignore
                    }
                    tc = null;
                    daemonLCC = null;
                    queue.clear();
                    trace(1, "daemon disabled");
                    break;
                }
                if (queue.isEmpty()) {
                    trace(1, "queue empty");
                    break;
                }
                td = queue.get(0);
            }
            try {
                start = System.currentTimeMillis();
                generateStatistics(daemonLCC, td);
                wuProcessed++;
                // Reset consecutive error counter.
                errorsConsecutive = 0;
                log(AS_BACKGROUND_TASK, td, "generation complete (" + ((System.currentTimeMillis() - start)) + " ms)");
            } catch (StandardException se) {
                errorsConsecutive++;
                // For less severe errors, rollback tx to clean up.
                if (!handleFatalErrors(ctxMgr, se)) {
                    boolean handled = handleExpectedErrors(td, se);
                    if (!handled) {
                        handled = handleUnexpectedErrors(td, se);
                    }
                    daemonLCC.internalRollback();
                    if (SanityManager.DEBUG) {
                        SanityManager.ASSERT(handled);
                    }
                }
            } finally {
                // Whatever happened, discard the unit of work.
                synchronized (queue) {
                    // Queue may have been cleared due to shutdown.
                    if (!queue.isEmpty()) {
                        queue.remove(0);
                    }
                }
                // Create an exception to force logging of the message.
                if (errorsConsecutive >= 50) {
                    log(AS_BACKGROUND_TASK, null, new IllegalStateException("degraded state"), "shutting down daemon, " + errorsConsecutive + " consecutive errors seen");
                    stop();
                }
            }
        }
    } catch (StandardException se) {
        log(AS_BACKGROUND_TASK, null, se, "thread died");
    // Do nothing, just let the thread die.
    } finally {
        synchronized (queue) {
            runningThread = null;
        }
        if (daemonLCC != null && !daemonLCC.isTransactionPristine()) {
            if (SanityManager.DEBUG) {
                SanityManager.THROWASSERT("transaction not pristine");
            }
            log(AS_BACKGROUND_TASK, null, "transaction not pristine - forcing rollback");
            try {
                daemonLCC.internalRollback();
            } catch (StandardException se) {
                // Log, then continue.
                log(AS_BACKGROUND_TASK, null, se, "forced rollback failed");
            }
        }
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) ShutdownException(org.apache.derby.shared.common.error.ShutdownException) TransactionController(org.apache.derby.iapi.store.access.TransactionController) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor)

Example 68 with StandardException

use of org.apache.derby.shared.common.error.StandardException in project derby by apache.

the class StorageFactoryService method getServiceProperties.

/**
 *		Open the service properties in the directory identified by the service name.
 *
 *		@return A Properties object or null if serviceName does not represent a valid service.
 *
 *		@exception StandardException Service appears valid but the properties cannot be created.
 */
public Properties getServiceProperties(final String serviceName, Properties defaultProperties) throws StandardException {
    if (SanityManager.DEBUG) {
        if (!serviceName.equals(getCanonicalServiceName(serviceName))) {
            SanityManager.THROWASSERT("serviceName (" + serviceName + ") expected to equal getCanonicalServiceName(serviceName) (" + getCanonicalServiceName(serviceName) + ")");
        }
    }
    // recreate the service root  if requested by the user.
    final String recreateFrom = recreateServiceRoot(serviceName, defaultProperties);
    final Properties serviceProperties = new Properties(defaultProperties);
    try {
        AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

            public Object run() throws IOException, StandardException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
                if (// restore from a file
                recreateFrom != null) {
                    File propFile = new File(recreateFrom, PersistentService.PROPERTIES_NAME);
                    InputStream is = new FileInputStream(propFile);
                    try {
                        serviceProperties.load(new BufferedInputStream(is));
                    } finally {
                        is.close();
                    }
                } else {
                    StorageFactory storageFactory = privGetStorageFactoryInstance(true, serviceName, null, null);
                    StorageFile file = storageFactory.newStorageFile(PersistentService.PROPERTIES_NAME);
                    resolveServicePropertiesFiles(storageFactory, file);
                    try {
                        InputStream is = file.getInputStream();
                        try {
                            // Need to load the properties before closing the
                            // StorageFactory.
                            serviceProperties.load(new BufferedInputStream(is));
                        } finally {
                            is.close();
                        }
                    } finally {
                        storageFactory.shutdown();
                    }
                }
                return null;
            }
        });
        return serviceProperties;
    } catch (PrivilegedActionException pae) {
        if (pae.getException() instanceof FileNotFoundException)
            return null;
        throw Monitor.exceptionStartingModule(pae.getException());
    } catch (SecurityException se) {
        throw Monitor.exceptionStartingModule(/*serviceName, */
        se);
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) InvocationTargetException(java.lang.reflect.InvocationTargetException) FileInputStream(java.io.FileInputStream) StandardException(org.apache.derby.shared.common.error.StandardException) WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) StorageFactory(org.apache.derby.io.StorageFactory) BufferedInputStream(java.io.BufferedInputStream) StorageFile(org.apache.derby.io.StorageFile) StorageFile(org.apache.derby.io.StorageFile) StorageRandomAccessFile(org.apache.derby.io.StorageRandomAccessFile) File(java.io.File)

Example 69 with StandardException

use of org.apache.derby.shared.common.error.StandardException 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 70 with StandardException

use of org.apache.derby.shared.common.error.StandardException in project derby by apache.

the class JarLoader method initialize.

/**
 *  Initialize the class loader so it knows if it
 *  is loading from a ZipFile or an InputStream
 */
void initialize() {
    String schemaName = name[IdUtil.DBCP_SCHEMA_NAME];
    String sqlName = name[IdUtil.DBCP_SQL_JAR_NAME];
    Exception e;
    try {
        installedJar = updateLoader.getJarReader().getJarFile(schemaName, sqlName);
        if (installedJar instanceof File) {
            try {
                jar = AccessController.doPrivileged(new java.security.PrivilegedExceptionAction<JarFile>() {

                    public JarFile run() throws IOException {
                        return new JarFile((File) installedJar);
                    }
                });
            } catch (PrivilegedActionException pae) {
                throw (IOException) pae.getException();
            }
            return;
        }
        // Jar is only accessible as an InputStream,
        // which means we need to re-open the stream for
        // each access.
        isStream = true;
        return;
    } catch (IOException ioe) {
        e = ioe;
    } catch (StandardException se) {
        e = se;
    }
    if (vs != null)
        vs.println(MessageService.getTextMessage(MessageId.CM_LOAD_JAR_EXCEPTION, getJarName(), e));
    // No such zip.
    setInvalid();
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) StorageFile(org.apache.derby.io.StorageFile) JarFile(java.util.jar.JarFile) File(java.io.File) StandardException(org.apache.derby.shared.common.error.StandardException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) GeneralSecurityException(java.security.GeneralSecurityException)

Aggregations

StandardException (org.apache.derby.shared.common.error.StandardException)276 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)43 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)37 IOException (java.io.IOException)32 Properties (java.util.Properties)29 RawTransaction (org.apache.derby.iapi.store.raw.xact.RawTransaction)27 TransactionController (org.apache.derby.iapi.store.access.TransactionController)26 ContextManager (org.apache.derby.iapi.services.context.ContextManager)22 RawContainerHandle (org.apache.derby.iapi.store.raw.data.RawContainerHandle)20 SQLException (java.sql.SQLException)17 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)17 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)16 ConglomerateController (org.apache.derby.iapi.store.access.ConglomerateController)12 RowLocation (org.apache.derby.iapi.types.RowLocation)11 SQLLongint (org.apache.derby.iapi.types.SQLLongint)11 StorageFile (org.apache.derby.io.StorageFile)10 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)9 ScanController (org.apache.derby.iapi.store.access.ScanController)9 File (java.io.File)8 LogInstant (org.apache.derby.iapi.store.raw.log.LogInstant)8