Search in sources :

Example 21 with StoreException

use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.

the class BDBBackup method takeBackupNoLock.

/**
 * Takes a hot backup when another process has locked the BDB database.
 *
 * @param fromdir The source directory path.
 * @param todir   The destination directory path.
 *
 * @return A list of all of the names of the files succesfully backed up.
 */
public String[] takeBackupNoLock(String fromdir, String todir) {
    if (log.isDebugEnabled()) {
        log.debug("public void takeBackupNoLock(String fromdir = " + fromdir + ", String todir = " + todir + "): called");
    }
    File fromDirFile = new File(fromdir);
    if (!fromDirFile.isDirectory()) {
        throw new IllegalArgumentException("The specified fromdir(" + fromdir + ") must be the directory containing your bdbstore.");
    }
    File toDirFile = new File(todir);
    if (!toDirFile.exists()) {
        // Create directory if it doesn't exist
        toDirFile.mkdirs();
        if (log.isDebugEnabled()) {
            log.debug("Created backup directory:" + toDirFile);
        }
    }
    if (!toDirFile.isDirectory()) {
        throw new IllegalArgumentException("The specified todir(" + todir + ") must be a directory.");
    }
    // Repeat until manage to open consistent set of files for reading.
    boolean consistentSet = false;
    FileInputStream[] fileInputStreams = new FileInputStream[0];
    File[] fileSet = new File[0];
    long start = System.currentTimeMillis();
    while (!consistentSet) {
        // List all .jdb files in the directory.
        fileSet = fromDirFile.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(LOG_FILE_SUFFIX);
            }
        });
        if (fileSet == null || fileSet.length == 0) {
            throw new StoreException("There are no BDB log files to backup in the '" + fromdir + "' directory.");
        }
        // The files must be copied in alphabetical order (numerical in effect)
        Arrays.sort(fileSet);
        // Open them all for reading.
        fileInputStreams = new FileInputStream[fileSet.length];
        for (int i = 0; i < fileSet.length; i++) {
            try {
                fileInputStreams[i] = new FileInputStream(fileSet[i]);
            } catch (FileNotFoundException e) {
                // Close any files opened for reading so far.
                for (int j = 0; j < i; j++) {
                    if (fileInputStreams[j] != null) {
                        try {
                            fileInputStreams[j].close();
                        } catch (IOException ioEx) {
                            // Rethrow this as a runtime exception, as something strange has happened.
                            throw new StoreException(ioEx);
                        }
                    }
                }
                // Could not open a consistent file set so try again.
                break;
            }
            // A consistent set has been opened if all files were successfully opened for reading.
            if (i == (fileSet.length - 1)) {
                consistentSet = true;
            }
        }
        // Check that the script has not timed out, and raise an error if it has.
        long now = System.currentTimeMillis();
        if ((now - start) > TIMEOUT) {
            throw new StoreException("Hot backup script failed to complete in " + (TIMEOUT / 1000) + " seconds.");
        }
    }
    // Copy the consistent set of open files.
    List<String> backedUpFileNames = new LinkedList<String>();
    for (int j = 0; j < fileSet.length; j++) {
        File destFile = new File(todir + File.separator + fileSet[j].getName());
        try {
            Files.copy(fileInputStreams[j], destFile.toPath());
        } catch (IOException ioe) {
            throw new StoreException(ioe.getMessage() + " fromDir:" + fromdir + " toDir:" + toDirFile, ioe);
        }
        backedUpFileNames.add(destFile.getName());
        // Close all of the files.
        try {
            fileInputStreams[j].close();
        } catch (IOException e) {
            // Rethrow this as a runtime exception, as something strange has happened.
            throw new StoreException(e);
        }
    }
    return backedUpFileNames.toArray(new String[backedUpFileNames.size()]);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) LinkedList(java.util.LinkedList) StoreException(org.apache.qpid.server.store.StoreException) FilenameFilter(java.io.FilenameFilter) File(java.io.File)

Example 22 with StoreException

use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.

the class BDBConfigurationStore method writeHierarchyRecords.

private void writeHierarchyRecords(final Transaction txn, final ConfiguredObjectRecord configuredObject) {
    OperationStatus status;
    HierarchyKeyBinding hierarchyBinding = HierarchyKeyBinding.getInstance();
    DatabaseEntry hierarchyKey = new DatabaseEntry();
    DatabaseEntry hierarchyValue = new DatabaseEntry();
    for (Map.Entry<String, UUID> parent : configuredObject.getParents().entrySet()) {
        hierarchyBinding.objectToEntry(new HierarchyKey(configuredObject.getId(), parent.getKey()), hierarchyKey);
        UUIDTupleBinding.getInstance().objectToEntry(parent.getValue(), hierarchyValue);
        status = getConfiguredObjectHierarchyDb().put(txn, hierarchyKey, hierarchyValue);
        if (status != OperationStatus.SUCCESS) {
            throw new StoreException("Error writing configured object " + configuredObject + " parent record to database: " + status);
        }
    }
}
Also used : HierarchyKeyBinding(org.apache.qpid.server.store.berkeleydb.tuple.HierarchyKeyBinding) OperationStatus(com.sleepycat.je.OperationStatus) HierarchyKey(org.apache.qpid.server.store.berkeleydb.entry.HierarchyKey) DatabaseEntry(com.sleepycat.je.DatabaseEntry) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) StoreException(org.apache.qpid.server.store.StoreException)

Example 23 with StoreException

use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.

the class BDBConfigurationStore method storeConfiguredObjectEntry.

private void storeConfiguredObjectEntry(final Transaction txn, ConfiguredObjectRecord configuredObject) throws StoreException {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Storing configured object record: " + configuredObject);
    }
    DatabaseEntry key = new DatabaseEntry();
    UUIDTupleBinding uuidBinding = UUIDTupleBinding.getInstance();
    uuidBinding.objectToEntry(configuredObject.getId(), key);
    DatabaseEntry value = new DatabaseEntry();
    ConfiguredObjectBinding queueBinding = ConfiguredObjectBinding.getInstance();
    queueBinding.objectToEntry(configuredObject, value);
    try {
        OperationStatus status = getConfiguredObjectsDb().put(txn, key, value);
        if (status != OperationStatus.SUCCESS) {
            throw new StoreException("Error writing configured object " + configuredObject + " to database: " + status);
        }
        writeHierarchyRecords(txn, configuredObject);
    } catch (RuntimeException e) {
        throw _environmentFacade.handleDatabaseException("Error writing configured object " + configuredObject + " to database: " + e.getMessage(), e);
    }
}
Also used : ConfiguredObjectBinding(org.apache.qpid.server.store.berkeleydb.tuple.ConfiguredObjectBinding) OperationStatus(com.sleepycat.je.OperationStatus) UUIDTupleBinding(org.apache.qpid.server.store.berkeleydb.tuple.UUIDTupleBinding) DatabaseEntry(com.sleepycat.je.DatabaseEntry) StoreException(org.apache.qpid.server.store.StoreException)

Example 24 with StoreException

use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.

the class BDBPreferenceStore method doClose.

@Override
protected void doClose() {
    try {
        _environmentFacade.close();
        _environmentFacade = null;
    } catch (RuntimeException e) {
        throw new StoreException("Exception occurred on preference store close", e);
    }
}
Also used : StoreException(org.apache.qpid.server.store.StoreException)

Example 25 with StoreException

use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.

the class ConfiguredObjectBinding method objectToEntry.

@Override
public void objectToEntry(ConfiguredObjectRecord object, TupleOutput tupleOutput) {
    try {
        StringWriter writer = new StringWriter();
        final ObjectMapper objectMapper = ConfiguredObjectJacksonModule.newObjectMapper(true);
        objectMapper.writeValue(writer, object.getAttributes());
        tupleOutput.writeString(object.getType());
        tupleOutput.writeString(writer.toString());
    } catch (IOException e) {
        throw new StoreException(e);
    }
}
Also used : StringWriter(java.io.StringWriter) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StoreException(org.apache.qpid.server.store.StoreException)

Aggregations

StoreException (org.apache.qpid.server.store.StoreException)70 SQLException (java.sql.SQLException)28 Connection (java.sql.Connection)23 PreparedStatement (java.sql.PreparedStatement)21 DatabaseEntry (com.sleepycat.je.DatabaseEntry)20 IOException (java.io.IOException)18 OperationStatus (com.sleepycat.je.OperationStatus)13 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 ResultSet (java.sql.ResultSet)10 UUID (java.util.UUID)8 Database (com.sleepycat.je.Database)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 ModelVersion (org.apache.qpid.server.model.ModelVersion)7 LinkKey (org.apache.qpid.server.protocol.v1_0.LinkKey)7 HashSet (java.util.HashSet)5 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)5 Cursor (com.sleepycat.je.Cursor)4 DatabaseConfig (com.sleepycat.je.DatabaseConfig)4