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);
}
}
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;
}
}
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);
}
}
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;
}
}
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());
}
Aggregations