diff --git a/README.md b/README.md index 9cb867f..86b34f0 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/build.gradle b/build.gradle index c031f65..db79cdf 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 87b738c..5c2d1cf 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 7c4388a..00e33ed 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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 diff --git a/gradlew b/gradlew index af6708f..83f2acf 100644 --- a/gradlew +++ b/gradlew @@ -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 @@ -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" @@ -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"` diff --git a/gradlew.bat b/gradlew.bat index 6d57edc..9618d8d 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -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 @@ -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