Skip to content

Commit

Permalink
Merge pull request #3 from oridool/liquibase-4.9
Browse files Browse the repository at this point in the history
Liquibase 4.9
  • Loading branch information
oridool authored Mar 20, 2022
2 parents 284cce2 + 1736c7e commit ffcaadb
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ Liquibase uses a locking table (default is `databasechangeloglock`) to indicate
Specifically, this table contains the `LOCKED` and `LOCKEDBY` columns.

When Liquibase starts upgrade, it is populating the locking table and sets `LOCKED = true`.
Thus, other application instances are started at the same time they wait each other. Eventually, only a single service instance is performing upgrade steps at a given time.
Thus, other application instances that are started at the same time keep waiting each other. Eventually, only a single application instance own the lock and executes the upgrade steps at any given time.

This mechanism is problematic if the application instance is stopped (crashed or brutally forced to stop) during upgrade time. The database and locking table remains in a 'locked' state, and any new application instance that tries to start would wait infinitely until database is fixed and lock is manually released.
This mechanism is problematic if the application instance owning the lock is suddenly stopped (crashed or brutally forced to stop) during upgrade time, while owning the lock. The database and locking table remains in a 'locked' state, and any new application instance that tries to start would wait infinitely until database is fixed and lock is **manually** released.
There is no way to automatically recover from this state.

The problem becomes more severe in the world of microservices and Kubernetes, where multiple instances per service (PODs) are executed, and those instances are controlled by K8S. K8S can shut down services unexpectedly, even during startup. We also expect full automation and auto-recovery at any state.

### Solution in this project
This project implements some Java classes that override the default Liquibase locking mechanism.
When upgrade starts, Liquibase will now utilizes the `LOCKEDBY` property with the value of the current DB session properties. This is a globally unique identifier, that only valid for the current session.
This project contains some Java classes that override the default Liquibase locking mechanism.
When upgrade starts, Liquibase now utilizes the `LOCKEDBY` property with the value of the current DB session properties. This is a globally unique identifier, that is only valid for the current session.
When any other application instance is starting, it first checks if the DB session that is currently stored in `LOCKEDBY` is alive.
If session is not alive, it means that the original client that locked the table is dead and the lock can be released. The new application instance will take ownership of the lock (using its own session id).
If session is still alive, application waits for the lock to be released by the lock owner (this is default Liquibase behavior).

The solution is currently written for **PostgreSQL** database, but can be easily enhanced to any other database type.
In PostgreSQL, the sessions are maintained in the table `pg_stat_activity`.
The `databasechangeloglock.LOCKEDBY` column is populated with both the session *PID* and its *Created Timestamp* to guarentee uniqueness.
The `databasechangeloglock.LOCKEDBY` column is populated with both the session *PID* and its *Created Timestamp* to guarantee uniqueness.

Example for `LOCKEDBY` value (`@@` is a pre-configured separator):
Example for the new `LOCKEDBY` value (`@@` is a pre-configured separator):
```
29800@@2020-03-09 10:38:12.95154
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

dependencies {
implementation 'org.liquibase:liquibase-core:4.3.1'
compileOnly 'org.liquibase:liquibase-core:4.9.0'

}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
22 changes: 19 additions & 3 deletions gradlew
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#!/usr/bin/env sh

#
# Copyright 2015 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

##############################################################################
##
## Gradle start up script for UN*X
Expand Down Expand Up @@ -28,7 +44,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m"'
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
Expand Down Expand Up @@ -109,8 +125,8 @@ if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
# For Cygwin or MSYS, switch paths to Windows format before running java
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
Expand Down
18 changes: 17 additions & 1 deletion gradlew.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
Expand All @@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m"
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
Expand Down

0 comments on commit ffcaadb

Please sign in to comment.