Skip to content

Commit

Permalink
Store race files always as race.json
Browse files Browse the repository at this point in the history
With this commit we store the race file always as `race.json`.
Previously Rally had a logic built-in to create a unique file name if
there was a potential for duplicate race files (with the same race
timestamp) but as we're using the race id now for the base directory
this is not possible anymore.

Relates elastic#720
Relates elastic#773
  • Loading branch information
danielmitterdorfer authored Sep 16, 2019
1 parent 59e0b60 commit 4bd131b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
17 changes: 6 additions & 11 deletions esrally/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import collections
import logging
import math
import os
import pickle
import random
import statistics
Expand Down Expand Up @@ -1387,32 +1388,26 @@ def list(self):
class FileRaceStore(RaceStore):
def __init__(self, cfg):
super().__init__(cfg)
self.user_provided_start_timestamp = self.cfg.opts("system", "time.start.user_provided", mandatory=False, default_value=False)
self.races_path = paths.races_root(self.cfg)
self.race_path = paths.race_root(self.cfg)

def _store(self, doc):
import json
io.ensure_dir(self.race_path)
# if the user has overridden the effective start date we guarantee a unique file name but do not let them use them for tournaments.
with open(self._output_file_name(doc), mode="wt", encoding="utf-8") as f:
with open(self._race_file(), mode="wt", encoding="utf-8") as f:
f.write(json.dumps(doc, indent=True, ensure_ascii=False))

def _output_file_name(self, doc):
if self.user_provided_start_timestamp:
suffix = "_{}".format(doc.get("user-tags", {}).get("name", doc["trial-id"]))
else:
suffix = ""
return "%s/race%s.json" % (self.race_path, suffix)
def _race_file(self, trial_id=None):
return os.path.join(paths.race_root(cfg=self.cfg, trial_id=trial_id), "race.json")

def list(self):
import glob
results = glob.glob("%s/*/race*.json" % self.races_path)
results = glob.glob(self._race_file(trial_id="*"))
all_races = self._to_races(results)
return all_races[:self._max_results()]

def find_by_trial_id(self, trial_id):
race_file = "%s/race.json" % paths.race_root(cfg=self.cfg, trial_id=trial_id)
race_file = self._race_file(trial_id=trial_id)
if io.exists(race_file):
races = self._to_races([race_file])
if races:
Expand Down
4 changes: 2 additions & 2 deletions esrally/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ def rally_root():


def races_root(cfg):
return "%s/races" % cfg.opts("node", "root.dir")
return os.path.join(cfg.opts("node", "root.dir"), "races")


def race_root(cfg=None, trial_id=None):
if not trial_id:
trial_id = cfg.opts("system", "trial.id")
return "%s/%s" % (races_root(cfg), trial_id)
return os.path.join(races_root(cfg), trial_id)

1 change: 1 addition & 0 deletions tests/metrics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1378,5 +1378,6 @@ def test_store_race(self):
self.race_store.store_race(race)

retrieved_race = self.race_store.find_by_trial_id(trial_id=FileRaceStoreTests.TRIAL_ID)
self.assertEqual(race.trial_id, retrieved_race.trial_id)
self.assertEqual(race.trial_timestamp, retrieved_race.trial_timestamp)
self.assertEqual(1, len(self.race_store.list()))

0 comments on commit 4bd131b

Please sign in to comment.