Skip to content

Commit

Permalink
Allow index-stats to compare non-existing path
Browse files Browse the repository at this point in the history
With this commit we make the index-stats operation more lenient so it
can check conditions for paths that don't (yet) exist. One use case for
this is to wait until a certain index shows up in the stats.

Relates elastic#925
  • Loading branch information
danielmitterdorfer committed Mar 4, 2020
1 parent a6f514b commit 191e2c9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
15 changes: 10 additions & 5 deletions esrally/driver/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,10 +608,15 @@ class IndicesStats(Runner):
"""

def _get(self, v, path):
if len(path) == 1:
return v[path[0]]
if v is None:
return None
elif len(path) == 1:
return v.get(path[0])
else:
return self._get(v[path[0]], path[1:])
return self._get(v.get(path[0]), path[1:])

def _safe_string(self, v):
return str(v) if v is not None else None

def __call__(self, es, params):
index = params.get("index", "_all")
Expand All @@ -628,8 +633,8 @@ def __call__(self, es, params):
"condition": {
"path": path,
# avoid mapping issues in the ES metrics store by always rendering values as strings
"actual-value": str(actual_value),
"expected-value": str(expected_value)
"actual-value": self._safe_string(actual_value),
"expected-value": self._safe_string(expected_value)
},
# currently we only support "==" as a predicate but that might change in the future
"success": actual_value == expected_value
Expand Down
33 changes: 33 additions & 0 deletions tests/driver/runner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,39 @@ def test_indices_stats_with_successful_condition(self, es):

es.indices.stats.assert_called_once_with(index="logs-*", metric="_all")

@mock.patch("elasticsearch.Elasticsearch")
def test_indices_stats_with_non_existing_path(self, es):
es.indices.stats.return_value = {
"indices": {
"total": {
"docs": {
"current": 0
}
}
}
}

indices_stats = runner.IndicesStats()

result = indices_stats(es, params={
"index": "logs-*",
"condition": {
# non-existing path
"path": "indices.my_index.total.docs.count",
"expected-value": 0
}
})
self.assertEqual(1, result["weight"])
self.assertEqual("ops", result["unit"])
self.assertFalse(result["success"])
self.assertDictEqual({
"path": "indices.my_index.total.docs.count",
"actual-value": None,
"expected-value": "0"
}, result["condition"])

es.indices.stats.assert_called_once_with(index="logs-*", metric="_all")


class QueryRunnerTests(TestCase):
@mock.patch("elasticsearch.Elasticsearch")
Expand Down

0 comments on commit 191e2c9

Please sign in to comment.