Search in sources :

Example 11 with StoreException

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

the class AbstractJDBCMessageStore method getStorableMessageMetaData.

private StorableMessageMetaData getStorableMessageMetaData(final long messageId, final InputStream stream) throws SQLException {
    try {
        int typeOrdinal = stream.read() & 0xff;
        MessageMetaDataType type = MessageMetaDataTypeRegistry.fromOrdinal(typeOrdinal);
        try (QpidByteBuffer buf = QpidByteBuffer.asQpidByteBuffer(stream)) {
            return type.createMetaData(buf);
        }
    } catch (IOException | RuntimeException e) {
        throw new StoreException("Failed to stream metadata for message with id " + messageId, e);
    }
}
Also used : QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) IOException(java.io.IOException) MessageMetaDataType(org.apache.qpid.server.plugin.MessageMetaDataType) StoreException(org.apache.qpid.server.store.StoreException)

Example 12 with StoreException

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

the class AbstractJDBCPreferenceStore method openAndLoad.

@Override
public Collection<PreferenceRecord> openAndLoad(final PreferenceStoreUpdater updater) throws StoreException {
    if (!_storeState.compareAndSet(StoreState.CLOSED, StoreState.OPENING)) {
        throw new IllegalStateException(String.format("PreferenceStore cannot be opened when in state '%s'", _storeState.get()));
    }
    try {
        _storeState.set(StoreState.OPENED);
        Collection<PreferenceRecord> records;
        try (Connection connection = getConnection()) {
            createVersionTable(connection);
            createPreferencesTable(connection);
            ModelVersion preferencesVersion = getPreferencesVersion(connection);
            ModelVersion brokerModelVersion = ModelVersion.fromString(BrokerModel.MODEL_VERSION);
            if (brokerModelVersion.lessThan(preferencesVersion)) {
                throw new StoreException(String.format("Cannot downgrade preference store from '%s' to '%s'", preferencesVersion, brokerModelVersion));
            }
            records = getPreferenceRecords(connection);
            if (preferencesVersion.lessThan(brokerModelVersion)) {
                final Collection<UUID> ids = new HashSet<>();
                for (PreferenceRecord record : records) {
                    ids.add(record.getId());
                }
                records = updater.updatePreferences(preferencesVersion.toString(), records);
                removeAndAdd(ids, records, transactedConnection -> updateVersion(transactedConnection, brokerModelVersion.toString()));
            }
        }
        return records;
    } catch (SQLException e) {
        _storeState.set(StoreState.ERRORED);
        close();
        throw new StoreException(e);
    }
}
Also used : SQLException(java.sql.SQLException) PreferenceRecord(org.apache.qpid.server.store.preferences.PreferenceRecord) Connection(java.sql.Connection) ModelVersion(org.apache.qpid.server.model.ModelVersion) UUID(java.util.UUID) StoreException(org.apache.qpid.server.store.StoreException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 13 with StoreException

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

the class AbstractJDBCPreferenceStore method getPreferenceRecords.

private Collection<PreferenceRecord> getPreferenceRecords(final Connection connection) throws SQLException {
    Collection<PreferenceRecord> records = new LinkedHashSet<>();
    final ObjectMapper objectMapper = new ObjectMapper();
    try (PreparedStatement stmt = connection.prepareStatement(String.format(SELECT_FROM_PREFERENCES, getPreferencesTableName()))) {
        try (ResultSet rs = stmt.executeQuery()) {
            while (rs.next()) {
                String id = rs.getString(1);
                String attributes = getBlobAsString(rs, 2);
                final PreferenceRecord preferenceRecord = new PreferenceRecordImpl(UUID.fromString(id), objectMapper.readValue(attributes, Map.class));
                records.add(preferenceRecord);
            }
        } catch (IOException e) {
            throw new StoreException("Error recovering persistent state: " + e.getMessage(), e);
        }
    }
    return records;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PreferenceRecord(org.apache.qpid.server.store.preferences.PreferenceRecord) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PreferenceRecordImpl(org.apache.qpid.server.store.preferences.PreferenceRecordImpl) IOException(java.io.IOException) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StoreException(org.apache.qpid.server.store.StoreException)

Example 14 with StoreException

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

the class JsonFilePreferenceStore method openAndLoad.

@Override
public synchronized Collection<PreferenceRecord> openAndLoad(final PreferenceStoreUpdater updater) throws StoreException {
    if (_storeState != StoreState.CLOSED) {
        throw new IllegalStateException(String.format("PreferenceStore cannot be opened when in state '%s'", _storeState));
    }
    try {
        setup(DEFAULT_FILE_NAME, _storePath, _posixFilePermissions, Collections.singletonMap("version", BrokerModel.MODEL_VERSION));
        StoreContent storeContent;
        try {
            storeContent = _objectMapper.readValue(getConfigFile(), StoreContent.class);
        } catch (IOException e) {
            throw new StoreException("Failed to read preferences from store", e);
        }
        ModelVersion storedVersion = ModelVersion.fromString(storeContent.getVersion());
        ModelVersion currentVersion = new ModelVersion(BrokerModel.MODEL_MAJOR_VERSION, BrokerModel.MODEL_MINOR_VERSION);
        if (currentVersion.lessThan(storedVersion)) {
            throw new IllegalStateException(String.format("Cannot downgrade preference store storedVersion from '%s' to '%s'", currentVersion.toString(), BrokerModel.MODEL_VERSION));
        }
        Collection<PreferenceRecord> records = Arrays.<PreferenceRecord>asList(storeContent.getPreferences());
        if (storedVersion.lessThan(currentVersion)) {
            records = updater.updatePreferences(storedVersion.toString(), records);
            storeContent.setVersion(BrokerModel.MODEL_VERSION);
            storeContent.setPreferences(records.toArray(new StoredPreferenceRecord[records.size()]));
            save(storeContent);
        }
        for (StoredPreferenceRecord preferenceRecord : storeContent.getPreferences()) {
            _recordMap.put(preferenceRecord.getId(), preferenceRecord);
        }
        _storeState = StoreState.OPENED;
        return records;
    } catch (Exception e) {
        _storeState = StoreState.ERRORED;
        close();
        throw e;
    }
}
Also used : IOException(java.io.IOException) ModelVersion(org.apache.qpid.server.model.ModelVersion) StoreException(org.apache.qpid.server.store.StoreException) IOException(java.io.IOException) StoreException(org.apache.qpid.server.store.StoreException)

Example 15 with StoreException

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

the class AbstractBDBMessageStore method removeXid.

private void removeXid(Transaction txn, long format, byte[] globalId, byte[] branchId) throws StoreException {
    DatabaseEntry key = new DatabaseEntry();
    Xid xid = new Xid(format, globalId, branchId);
    XidBinding keyBinding = XidBinding.getInstance();
    keyBinding.objectToEntry(xid, key);
    try {
        OperationStatus status = getXidDb().delete(txn, key);
        if (status == OperationStatus.NOTFOUND) {
            throw new StoreException("Unable to find xid");
        } else if (status != OperationStatus.SUCCESS) {
            throw new StoreException("Unable to remove xid");
        }
    } catch (RuntimeException e) {
        getLogger().error("Failed to remove xid in transaction " + txn, e);
        throw getEnvironmentFacade().handleDatabaseException("Error accessing database while removing xid: " + e.getMessage(), e);
    }
}
Also used : Xid(org.apache.qpid.server.txn.Xid) OperationStatus(com.sleepycat.je.OperationStatus) XidBinding(org.apache.qpid.server.store.berkeleydb.tuple.XidBinding) DatabaseEntry(com.sleepycat.je.DatabaseEntry) 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