When grabbing the schema migration lock, ensure that the current

version is set to what we expect it to be.

Otherwise if one node reads an old value for the schema version, it will
be able to grab the migration lock as long as another node has already
completed the migration.
This commit is contained in:
Ray Greenwell
2012-01-05 20:35:55 +00:00
parent 73c111c20b
commit fa66796863
2 changed files with 8 additions and 3 deletions
@@ -530,7 +530,7 @@ public class DepotMarshaller<T extends PersistentRecord> implements QueryMarshal
// try to update migratingVersion to the new version to indicate to other processes
// that we are handling the migration and that they should wait
if (_meta.updateMigratingVersion(getTableName(), _schemaVersion, 0)) {
if (_meta.updateMigratingVersion(getTableName(), currentVersion, _schemaVersion, 0)) {
break; // we got the lock, let's go
}
@@ -549,6 +549,7 @@ public class DepotMarshaller<T extends PersistentRecord> implements QueryMarshal
// fetch all relevant information regarding our table from the database
TableMetaData metaData = TableMetaData.load(ctx, getTableName());
int expectedDbVersion = currentVersion;
try {
if (!metaData.tableExists) {
// if the table does not exist, create it
@@ -564,11 +565,13 @@ public class DepotMarshaller<T extends PersistentRecord> implements QueryMarshal
// and update our version in the schema version table
_meta.updateVersion(getTableName(), _schemaVersion);
expectedDbVersion = _schemaVersion;
} finally {
// set our migrating version back to zero
try {
if (!_meta.updateMigratingVersion(getTableName(), 0, _schemaVersion)) {
if (!_meta.updateMigratingVersion(
getTableName(), expectedDbVersion, 0, _schemaVersion)) {
log.warning("Failed to restore migrating version to zero!", "record", _pClass);
}
} catch (Exception e) {
@@ -126,7 +126,8 @@ public class DepotMetaData
* if some other process acquired the lock.
*/
public boolean updateMigratingVersion (
final String pClass, final int newMigratingVersion, final int guardVersion)
final String pClass, final int currentVersion,
final int newMigratingVersion, final int guardVersion)
{
return _ctx.invoke(new SimpleModifier() {
@Override
@@ -135,6 +136,7 @@ public class DepotMetaData
"update " + liaison.tableSQL(SCHEMA_VERSION_TABLE) +
" set " + liaison.columnSQL(MV_COLUMN) + " = " + newMigratingVersion +
" where " + liaison.columnSQL(P_COLUMN) + " = '" + pClass + "'" +
" and " + liaison.columnSQL(V_COLUMN) + " = " + currentVersion +
" and " + liaison.columnSQL(MV_COLUMN) + " = " + guardVersion);
}
}) > 0;