Created
August 1, 2018 12:05
-
-
Save therve/05557e09e9ec3935c101d64ec6d15fb5 to your computer and use it in GitHub Desktop.
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
"""Helper script to fix some Heat convergence issues. | |
Up until queens, Heat had a few bugs which could result in unresolved | |
references to resources that are not present in the database. That ends up | |
making some operations using resources stuck IN_PROGRESS. | |
This script fixed a few of those issues, removing the dead references. | |
USE AT YOUR OWN RISK. Please make a backup of your database before using it. | |
It's also encouraged to stop any Heat engines to prevent bad interaction. | |
""" | |
import argparse | |
import json | |
import pymysql | |
def main(do_commit): | |
def commit(): | |
if do_commit: | |
connection.commit() | |
connection = pymysql.connect(user="root", database="heat") | |
try: | |
with connection.cursor() as cursor: | |
cursor.execute( | |
"select resource.id from resource " | |
"left join resource as reqres on " | |
"resource.replaced_by = reqres.id where reqres.id is null " | |
"and resource.replaced_by is not null") | |
update = "update resource set replaced_by = null where id = %s" | |
for (resource,) in cursor.fetchall(): | |
print("Fixed replaced_by for %s" % resource) | |
cursor.execute(update, (resource,)) | |
commit() | |
with connection.cursor() as cursor: | |
cursor.execute( | |
"select resource.id from resource " | |
"left join resource as reqres on " | |
"resource.replaces = reqres.id where reqres.id is null " | |
"and resource.replaces is not null") | |
update = "update resource set replaces = null where id = %s" | |
for (resource,) in cursor.fetchall(): | |
print("Fixed replaces for %s" % resource) | |
cursor.execute(update, (resource,)) | |
commit() | |
with connection.cursor() as cursor: | |
query = ("select resource.id, resource.requires from " | |
"resource left join resource as reqres on " | |
"json_extract(resource.requires, '$[%s]') = reqres.id " | |
"where reqres.id is NULL and " | |
"json_extract(resource.requires, '$[%s]') is not null") | |
for i in range(10): | |
cursor.execute(query, (i, i)) | |
update = "update resource set requires = %s where id = %s" | |
for (resource, requires) in cursor.fetchall(): | |
print("Fixed requires for %s" % resource) | |
requires = json.loads(requires) | |
del requires[i] | |
cursor.execute(update, (json.dumps(requires), resource)) | |
commit() | |
with connection.cursor() as cursor: | |
query = ("select resource.id, resource.needed_by from " | |
"resource left join resource as reqres on " | |
"json_extract(resource.needed_by, '$[%s]') = reqres.id " | |
"where reqres.id is NULL and " | |
"json_extract(resource.needed_by, '$[%s]') is not null") | |
for i in range(10): | |
cursor.execute(query, (i, i)) | |
update = "update resource set needed_by = %s where id = %s" | |
for (resource, needed_by) in cursor.fetchall(): | |
print("Fixed needed_by for %s" % resource) | |
needed_by = json.loads(needed_by) | |
del needed_by[i] | |
cursor.execute(update, (json.dumps(needed_by), resource)) | |
commit() | |
finally: | |
connection.close() | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser( | |
description="Fix Heat convergence issues.") | |
parser.add_argument("--commit", action="store_true", | |
help="If set, will commit fixes") | |
args = parser.parse_args() | |
main(args.commit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment