Search in sources :

Example 1 with ModelVersion

use of org.apache.qpid.server.model.ModelVersion 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 2 with ModelVersion

use of org.apache.qpid.server.model.ModelVersion 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 3 with ModelVersion

use of org.apache.qpid.server.model.ModelVersion in project qpid-broker-j by apache.

the class AbstractBDBPreferenceStore method getStoredVersion.

ModelVersion getStoredVersion() {
    try (Cursor cursor = getPreferencesVersionDb().openCursor(null, null)) {
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        ModelVersion storedVersion = null;
        while (cursor.getNext(key, value, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS) {
            String versionString = StringBinding.entryToString(key);
            ModelVersion version = ModelVersion.fromString(versionString);
            if (storedVersion == null || storedVersion.lessThan(version)) {
                storedVersion = version;
            }
        }
        if (storedVersion == null) {
            throw new StoreException("No preference version information.");
        }
        return storedVersion;
    } catch (RuntimeException e) {
        throw getEnvironmentFacade().handleDatabaseException("Cannot visit preference version", e);
    }
}
Also used : DatabaseEntry(com.sleepycat.je.DatabaseEntry) ModelVersion(org.apache.qpid.server.model.ModelVersion) Cursor(com.sleepycat.je.Cursor) StoreException(org.apache.qpid.server.store.StoreException)

Example 4 with ModelVersion

use of org.apache.qpid.server.model.ModelVersion in project qpid-broker-j by apache.

the class AbstractBDBPreferenceStore 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'", getStoreState()));
    }
    EnvironmentFacade environmentFacade = getEnvironmentFacade();
    try {
        _storeState.set(StoreState.OPENED);
        ModelVersion currentVersion = new ModelVersion(BrokerModel.MODEL_MAJOR_VERSION, BrokerModel.MODEL_MINOR_VERSION);
        ModelVersion storedVersion = getStoredVersion();
        if (currentVersion.lessThan(storedVersion)) {
            throw new StoreException(String.format("Cannot downgrade preference store from '%s' to '%s'", storedVersion, currentVersion));
        }
        Collection<PreferenceRecord> records = getPreferenceRecords(environmentFacade);
        if (storedVersion.lessThan(currentVersion)) {
            final Collection<UUID> ids = new HashSet<>();
            for (PreferenceRecord record : records) {
                ids.add(record.getId());
            }
            records = updater.updatePreferences(storedVersion.toString(), records);
            removeAndAdd(ids, records, transaction -> updateVersion(transaction, currentVersion.toString()));
        }
        return records;
    } catch (Exception e) {
        _storeState.set(StoreState.ERRORED);
        close();
        throw e;
    }
}
Also used : PreferenceRecord(org.apache.qpid.server.store.preferences.PreferenceRecord) ModelVersion(org.apache.qpid.server.model.ModelVersion) UUID(java.util.UUID) StoreException(org.apache.qpid.server.store.StoreException) DatabaseNotFoundException(com.sleepycat.je.DatabaseNotFoundException) StoreException(org.apache.qpid.server.store.StoreException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 5 with ModelVersion

use of org.apache.qpid.server.model.ModelVersion in project qpid-broker-j by apache.

the class BDBPreferenceStoreTest method testVersionAfterUpgrade.

public void testVersionAfterUpgrade() throws Exception {
    FileUtils.delete(_storeFile, true);
    _storeFile.mkdirs();
    ModelVersion storeVersion = new ModelVersion(BrokerModel.MODEL_MAJOR_VERSION - 1, BrokerModel.MODEL_MINOR_VERSION);
    populateTestData(_testInitialRecords, storeVersion.toString());
    _preferenceStore.openAndLoad(_updater);
    ModelVersion storedVersion = _preferenceStore.getStoredVersion();
    assertEquals("Unexpected version", BrokerModel.MODEL_VERSION, storedVersion.toString());
}
Also used : ModelVersion(org.apache.qpid.server.model.ModelVersion)

Aggregations

ModelVersion (org.apache.qpid.server.model.ModelVersion)9 StoreException (org.apache.qpid.server.store.StoreException)7 Cursor (com.sleepycat.je.Cursor)3 DatabaseEntry (com.sleepycat.je.DatabaseEntry)3 HashSet (java.util.HashSet)3 DatabaseNotFoundException (com.sleepycat.je.DatabaseNotFoundException)2 SQLException (java.sql.SQLException)2 LinkedHashSet (java.util.LinkedHashSet)2 UUID (java.util.UUID)2 LinkDefinition (org.apache.qpid.server.protocol.v1_0.LinkDefinition)2 PreferenceRecord (org.apache.qpid.server.store.preferences.PreferenceRecord)2 Database (com.sleepycat.je.Database)1 Transaction (com.sleepycat.je.Transaction)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 Statement (java.sql.Statement)1 LinkDefinitionImpl (org.apache.qpid.server.protocol.v1_0.LinkDefinitionImpl)1 LinkKey (org.apache.qpid.server.protocol.v1_0.LinkKey)1 Source (org.apache.qpid.server.protocol.v1_0.type.messaging.Source)1