Update time database to v3

A job's runtime is no longer dependent solely on its name, but also
the tenant, project, and branch.

And enable the test.

Change-Id: I0e1fc3b1fcb90e444aa796aab4c1bce2449b76c8
This commit is contained in:
James E. Blair 2017-09-13 10:55:15 -06:00
parent daa93e7a93
commit ae0f23ce57
4 changed files with 37 additions and 22 deletions

View File

@ -800,14 +800,22 @@ class TestTimeDataBase(BaseTestCase):
self.db = model.TimeDataBase(self.tmp_root)
def test_timedatabase(self):
self.assertEqual(self.db.getEstimatedTime('job-name'), 0)
self.db.update('job-name', 50, 'SUCCESS')
self.assertEqual(self.db.getEstimatedTime('job-name'), 50)
self.db.update('job-name', 100, 'SUCCESS')
self.assertEqual(self.db.getEstimatedTime('job-name'), 75)
pipeline = Dummy(layout=Dummy(tenant=Dummy(name='test-tenant')))
change = Dummy(project=Dummy(canonical_name='git.example.com/foo/bar'))
job = Dummy(name='job-name')
item = Dummy(pipeline=pipeline,
change=change)
build = Dummy(build_set=Dummy(item=item),
job=job)
self.assertEqual(self.db.getEstimatedTime(build), 0)
self.db.update(build, 50, 'SUCCESS')
self.assertEqual(self.db.getEstimatedTime(build), 50)
self.db.update(build, 100, 'SUCCESS')
self.assertEqual(self.db.getEstimatedTime(build), 75)
for x in range(10):
self.db.update('job-name', 100, 'SUCCESS')
self.assertEqual(self.db.getEstimatedTime('job-name'), 100)
self.db.update(build, 100, 'SUCCESS')
self.assertEqual(self.db.getEstimatedTime(build), 100)
class TestGraph(BaseTestCase):

View File

@ -617,7 +617,6 @@ class TestScheduler(ZuulTestCase):
self.assertEqual(B.reported, 2)
self.assertEqual(C.reported, 2)
@skip("Disabled for early v3 development")
def _test_time_database(self, iteration):
self.executor_server.hold_jobs_in_build = True
A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
@ -626,7 +625,7 @@ class TestScheduler(ZuulTestCase):
self.waitUntilSettled()
time.sleep(2)
data = json.loads(self.sched.formatStatusJSON())
data = json.loads(self.sched.formatStatusJSON('tenant-one'))
found_job = None
for pipeline in data['pipelines']:
if pipeline['name'] != 'gate':
@ -652,7 +651,6 @@ class TestScheduler(ZuulTestCase):
self.executor_server.release()
self.waitUntilSettled()
@skip("Disabled for early v3 development")
def test_time_database(self):
"Test the time database"

View File

@ -2668,20 +2668,30 @@ class JobTimeData(object):
class TimeDataBase(object):
def __init__(self, root):
self.root = root
self.jobs = {}
def _getTD(self, name):
td = self.jobs.get(name)
if not td:
td = JobTimeData(os.path.join(self.root, name))
self.jobs[name] = td
td.load()
def _getTD(self, build):
if hasattr(build.build_set.item.change, 'branch'):
branch = build.build_set.item.change.branch
else:
branch = ''
dir_path = os.path.join(
self.root,
build.build_set.item.pipeline.layout.tenant.name,
build.build_set.item.change.project.canonical_name,
branch)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
path = os.path.join(dir_path, build.job.name)
td = JobTimeData(path)
td.load()
return td
def getEstimatedTime(self, name):
return self._getTD(name).getEstimatedTime()
def update(self, name, elapsed, result):
td = self._getTD(name)
def update(self, build, elapsed, result):
td = self._getTD(build)
td.add(elapsed, result)
td.save()

View File

@ -826,7 +826,7 @@ class Scheduler(threading.Thread):
return
try:
build.estimated_time = float(self.time_database.getEstimatedTime(
build.job.name))
build))
except Exception:
self.log.exception("Exception estimating build time:")
pipeline.manager.onBuildStarted(event.build)
@ -865,8 +865,7 @@ class Scheduler(threading.Thread):
if build.end_time and build.start_time and build.result:
duration = build.end_time - build.start_time
try:
self.time_database.update(
build.job.name, duration, build.result)
self.time_database.update(build, duration, build.result)
except Exception:
self.log.exception("Exception recording build time:")
pipeline.manager.onBuildCompleted(event.build)