add command resetwebsite

This commit is contained in:
yangdsh 2019-10-15 18:03:24 +00:00 committed by Dana Van Aken
parent fb7ee5c6a3
commit e4a97d060d
5 changed files with 70 additions and 15 deletions

View File

@ -1,5 +1,5 @@
# #
# OtterTune - dumpknobtunability.py # OtterTune - dumpknob.py
# #
# Copyright (c) 2017-18, Carnegie Mellon University Database Group # Copyright (c) 2017-18, Carnegie Mellon University Database Group
# #
@ -12,7 +12,7 @@ from website.models import Session, SessionKnob, SessionKnobManager
class Command(BaseCommand): class Command(BaseCommand):
help = 'Dump knob tunability for the session with the given upload code.' help = 'Dump knobs for the session with the given upload code.'
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument( parser.add_argument(
@ -40,7 +40,7 @@ class Command(BaseCommand):
raise CommandError( raise CommandError(
"ERROR: Session with upload code '{}' not exist.".format(options['uploadcode'])) "ERROR: Session with upload code '{}' not exist.".format(options['uploadcode']))
session_knobs = SessionKnobManager.get_knob_tunability(session) session_knobs = SessionKnobManager.get_knob_min_max_tunability(session)
filename = options['filename'] or 'knobs.json' filename = options['filename'] or 'knobs.json'
path = os.path.join(directory, filename) path = os.path.join(directory, filename)
@ -49,4 +49,4 @@ class Command(BaseCommand):
json.dump(session_knobs, f, indent=4) json.dump(session_knobs, f, indent=4)
self.stdout.write(self.style.SUCCESS( self.stdout.write(self.style.SUCCESS(
"Successfully dumped knob tunability to '{}'.".format(path))) "Successfully dumped knob information to '{}'.".format(path)))

View File

@ -1,5 +1,5 @@
# #
# OtterTune - loadknobtunability.py # OtterTune - loadknob.py
# #
# Copyright (c) 2017-18, Carnegie Mellon University Database Group # Copyright (c) 2017-18, Carnegie Mellon University Database Group
# #
@ -12,7 +12,7 @@ from website.models import Session, SessionKnob, SessionKnobManager
class Command(BaseCommand): class Command(BaseCommand):
help = 'load session tunability for the session with the given upload code.' help = 'load knobs for the session with the given upload code.'
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument( parser.add_argument(
@ -46,7 +46,7 @@ class Command(BaseCommand):
with open(path, 'r') as f: with open(path, 'r') as f:
knobs = json.load(f) knobs = json.load(f)
SessionKnobManager.set_knob_tunability(session, knobs) SessionKnobManager.set_knob_min_max_tunability(session, knobs)
self.stdout.write(self.style.SUCCESS( self.stdout.write(self.style.SUCCESS(
"Successfully load knob tunability from '{}'.".format(path))) "Successfully load knob information from '{}'.".format(path)))

View File

@ -0,0 +1,50 @@
#
# OtterTune - resetwebsite.py
#
# Copyright (c) 2017-18, Carnegie Mellon University Database Group
#
from django.core.management.base import BaseCommand
from fabric.api import local
from website.settings import DATABASES
class Command(BaseCommand):
help = 'dump the website; reset the website; load data from file if specified.'
def add_arguments(self, parser):
parser.add_argument(
'-d', '--dumpfile',
metavar='FILE',
help='Name of the file to dump data to. '
'Default: dump_website.json')
parser.add_argument(
'-l', '--loadfile',
metavar='FILE',
help='Name of the file to load data from. ')
def reset_website(self):
# WARNING: destroys the existing website and creates with all
# of the required inital data loaded (e.g., the KnobCatalog)
# Recreate the ottertune database
user = DATABASES['default']['USER']
passwd = DATABASES['default']['PASSWORD']
name = DATABASES['default']['NAME']
local("mysql -u {} -p{} -N -B -e \"DROP DATABASE IF EXISTS {}\"".format(
user, passwd, name))
local("mysql -u {} -p{} -N -B -e \"CREATE DATABASE {}\"".format(
user, passwd, name))
# Reinitialize the website
local('python manage.py migrate')
def handle(self, *args, **options):
dumpfile = options['dumpfile'] if options['dumpfile'] else 'dump_website.json'
local("python manage.py dumpdata admin auth django_db_logger djcelery sessions\
sites website > {}".format(dumpfile))
self.reset_website()
if options['loadfile']:
local("python manage.py loaddata '{}'".format(options['loadfile']))
self.stdout.write(self.style.SUCCESS(
"Successfully reset website."))

View File

@ -11,7 +11,7 @@ from fabric.api import local
class Command(BaseCommand): class Command(BaseCommand):
help = 'Start celery and celerybeat in the background.' help = 'Stop celery and celerybeat and remove pid files.'
celery_cmd = 'python3 manage.py {cmd} {opts} &'.format celery_cmd = 'python3 manage.py {cmd} {opts} &'.format
max_wait_sec = 15 max_wait_sec = 15

View File

@ -194,24 +194,29 @@ class SessionKnobManager(models.Manager):
return knob_dicts return knob_dicts
@staticmethod @staticmethod
def get_knob_tunability(session): def get_knob_min_max_tunability(session):
# Returns a dict of the knob # Returns a dict of the knob
knobs = KnobCatalog.objects.filter(dbms=session.dbms) knobs = KnobCatalog.objects.filter(dbms=session.dbms)
knob_dicts = list(knobs.values()) knob_dicts = list(knobs.values())
session_knob_dict = {} session_knob_dicts = {}
for i, _ in enumerate(knob_dicts): for i, _ in enumerate(knob_dicts):
if SessionKnob.objects.filter(session=session, knob=knobs[i]).exists(): if SessionKnob.objects.filter(session=session, knob=knobs[i]).exists():
new_knob = SessionKnob.objects.filter(session=session, knob=knobs[i])[0] new_knob = SessionKnob.objects.filter(session=session, knob=knobs[i])[0]
session_knob_dict[new_knob.name] = new_knob.tunable min_max_tunability = {"minval": new_knob.minval,
return session_knob_dict "maxval": new_knob.maxval,
"tunable": new_knob.tunable}
session_knob_dicts[new_knob.name] = min_max_tunability
return session_knob_dicts
@staticmethod @staticmethod
def set_knob_tunability(session, knob_dicts): def set_knob_min_max_tunability(session, knob_dicts):
# Returns a dict of the knob # Returns a dict of the knob
session_knobs = SessionKnob.objects.filter(session=session) session_knobs = SessionKnob.objects.filter(session=session)
for session_knob in session_knobs: for session_knob in session_knobs:
if knob_dicts.__contains__(session_knob.name): if knob_dicts.__contains__(session_knob.name):
session_knob.tunable = knob_dicts[session_knob.name] session_knob.minval = knob_dicts[session_knob.name]["minval"]
session_knob.maxval = knob_dicts[session_knob.name]["maxval"]
session_knob.tunable = knob_dicts[session_knob.name]["tunable"]
session_knob.save() session_knob.save()