-
Notifications
You must be signed in to change notification settings - Fork 1.2k
check for active MSses before starting DB upgrade #12140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
DaanHoogland
wants to merge
4
commits into
apache:4.20
Choose a base branch
from
shapeblue:ghi11973-DbUpgradeOnlyStandalone
base: 4.20
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+59
−24
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,7 +123,10 @@ | |
| import com.cloud.utils.crypt.DBEncryptionUtil; | ||
| import com.cloud.utils.db.GlobalLock; | ||
| import com.cloud.utils.db.ScriptRunner; | ||
| import com.cloud.utils.db.Transaction; | ||
| import com.cloud.utils.db.TransactionCallback; | ||
| import com.cloud.utils.db.TransactionLegacy; | ||
| import com.cloud.utils.db.TransactionStatus; | ||
| import com.cloud.utils.exception.CloudRuntimeException; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
|
|
||
|
|
@@ -448,39 +451,71 @@ public void check() { | |
| throw new CloudRuntimeException("Unable to acquire lock to check for database integrity."); | ||
| } | ||
|
|
||
| try { | ||
| initializeDatabaseEncryptors(); | ||
| // not sure about the right moment to do this yet | ||
| checkIfStandalone(); | ||
DaanHoogland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| doUpgrades(lock); | ||
| } finally { | ||
| lock.releaseRef(); | ||
| } | ||
| } | ||
| private void checkIfStandalone() throws CloudRuntimeException { | ||
DaanHoogland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| boolean standalone = Transaction.execute(new TransactionCallback<>() { | ||
| @Override | ||
| public Boolean doInTransaction(TransactionStatus status) { | ||
| String sql = "SELECT COUNT(*) FROM `cloud`.`mshost` WHERE `state` = 'UP'"; | ||
| try (Connection conn = TransactionLegacy.getStandaloneConnection(); | ||
| PreparedStatement pstmt = conn.prepareStatement(sql); | ||
| ResultSet rs = pstmt.executeQuery()) { | ||
| if (rs.next()) { | ||
| int count = rs.getInt(1); | ||
| return count == 0; | ||
| } | ||
| } catch (SQLException e) { | ||
| String errorMessage = "Unable to check if the management server is running in standalone mode."; | ||
| LOGGER.error(errorMessage, e); | ||
| throw new CloudRuntimeException(errorMessage, e); | ||
| } | ||
| return true; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe be save and return false as we are not sure. |
||
| } | ||
| }); | ||
| if (! standalone) { | ||
| String msg = "CloudStack is running multiple management servers while attempting to upgrade. Upgrades can only be run in standalone mode. Aborting."; | ||
| LOGGER.info(msg); | ||
DaanHoogland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new CloudRuntimeException(msg); | ||
| } | ||
| } | ||
|
|
||
| final CloudStackVersion dbVersion = CloudStackVersion.parse(_dao.getCurrentVersion()); | ||
| final String currentVersionValue = this.getClass().getPackage().getImplementationVersion(); | ||
| private void doUpgrades(GlobalLock lock) { | ||
| try { | ||
| initializeDatabaseEncryptors(); | ||
|
|
||
| if (StringUtils.isBlank(currentVersionValue)) { | ||
| return; | ||
| } | ||
| final CloudStackVersion dbVersion = CloudStackVersion.parse(_dao.getCurrentVersion()); | ||
| final String currentVersionValue = this.getClass().getPackage().getImplementationVersion(); | ||
|
|
||
| String csVersion = SystemVmTemplateRegistration.parseMetadataFile(); | ||
| final CloudStackVersion sysVmVersion = CloudStackVersion.parse(csVersion); | ||
| final CloudStackVersion currentVersion = CloudStackVersion.parse(currentVersionValue); | ||
| SystemVmTemplateRegistration.CS_MAJOR_VERSION = String.valueOf(sysVmVersion.getMajorRelease()) + "." + String.valueOf(sysVmVersion.getMinorRelease()); | ||
| SystemVmTemplateRegistration.CS_TINY_VERSION = String.valueOf(sysVmVersion.getPatchRelease()); | ||
| if (StringUtils.isBlank(currentVersionValue)) { | ||
| return; | ||
| } | ||
|
|
||
| LOGGER.info("DB version = " + dbVersion + " Code Version = " + currentVersion); | ||
| String csVersion = SystemVmTemplateRegistration.parseMetadataFile(); | ||
| final CloudStackVersion sysVmVersion = CloudStackVersion.parse(csVersion); | ||
| final CloudStackVersion currentVersion = CloudStackVersion.parse(currentVersionValue); | ||
| SystemVmTemplateRegistration.CS_MAJOR_VERSION = String.valueOf(sysVmVersion.getMajorRelease()) + "." + String.valueOf(sysVmVersion.getMinorRelease()); | ||
| SystemVmTemplateRegistration.CS_TINY_VERSION = String.valueOf(sysVmVersion.getPatchRelease()); | ||
|
|
||
| if (dbVersion.compareTo(currentVersion) > 0) { | ||
| throw new CloudRuntimeException("Database version " + dbVersion + " is higher than management software version " + currentVersionValue); | ||
| } | ||
| LOGGER.info("DB version = " + dbVersion + " Code Version = " + currentVersion); | ||
|
|
||
| if (dbVersion.compareTo(currentVersion) == 0) { | ||
| LOGGER.info("DB version and code version matches so no upgrade needed."); | ||
| return; | ||
| } | ||
| if (dbVersion.compareTo(currentVersion) > 0) { | ||
| throw new CloudRuntimeException("Database version " + dbVersion + " is higher than management software version " + currentVersionValue); | ||
| } | ||
|
|
||
| upgrade(dbVersion, currentVersion); | ||
| } finally { | ||
| lock.unlock(); | ||
| if (dbVersion.compareTo(currentVersion) == 0) { | ||
| LOGGER.info("DB version and code version matches so no upgrade needed."); | ||
| return; | ||
| } | ||
|
|
||
| upgrade(dbVersion, currentVersion); | ||
| } finally { | ||
| lock.releaseRef(); | ||
| lock.unlock(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.