Skip to content

Commit

Permalink
Update rally-* index template settings to default to Elasticsearch de…
Browse files Browse the repository at this point in the history
…faults (#1312)

This commit changes the defaults for the number of shards and replicas used in the Elasticsearch datastore first introduced in #1310:
instead of hardcoding them -- when unset -- to `1p/0r`, we rely on ES defaults[1] and only attempt to override these index settings if they've
been explicitly specified in the `ini` file.

Additionally, we also remove the hardcoded `refresh_interval: 5s` setting, to allow us to take advantage of Elasticsearch's default refresh behavior ([2], [3]).

\[1\]: https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#_static_index_settings
\[2\]: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html#refresh-api-desc
\[3\]: https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#dynamic-index-settings
  • Loading branch information
b-deam authored Aug 16, 2021
1 parent db25ac0 commit 9474c0d
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 14 deletions.
4 changes: 2 additions & 2 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ The following settings are applicable only if ``datastore.type`` is set to "elas
* ``datastore.user``: Sets the name of the Elasticsearch user for the metrics store.
* ``datastore.password``: Sets the password of the Elasticsearch user for the metrics store.
* ``datastore.probe.cluster_version`` (default: true): Enables automatic detection of the metric store's version.
* ``datastore.number_of_shards`` (default: 1): The number of primary shards that the ``rally-*`` indices should have. Any updates to this setting after initial index creation will only be applied to new ``rally-*`` indices.
* ``datastore.number_of_replicas`` (default: 0): The number of replicas each primary shard has. Defaults to 0. Any updates to this setting after initial index creation will only be applied to new ``rally-*`` indices.
* ``datastore.number_of_shards`` (default: `Elasticsearch default value <https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#_static_index_settings>`_): The number of primary shards that the ``rally-*`` indices should have. Any updates to this setting after initial index creation will only be applied to new ``rally-*`` indices.
* ``datastore.number_of_replicas`` (default: `Elasticsearch default value <https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#_static_index_settings>`_): The number of replicas each primary shard has. Defaults to . Any updates to this setting after initial index creation will only be applied to new ``rally-*`` indices.

**Examples**

Expand Down
15 changes: 11 additions & 4 deletions esrally/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ class IndexTemplateProvider:

def __init__(self, cfg):
self._config = cfg
self._number_of_shards = self._config.opts("reporting", "datastore.number_of_shards", default_value=1, mandatory=False)
self._number_of_replicas = self._config.opts("reporting", "datastore.number_of_replicas", default_value=0, mandatory=False)
self._number_of_shards = self._config.opts("reporting", "datastore.number_of_shards", default_value=None, mandatory=False)
self._number_of_replicas = self._config.opts("reporting", "datastore.number_of_replicas", default_value=None, mandatory=False)
self.script_dir = self._config.opts("node", "rally.root")

def metrics_template(self):
Expand All @@ -254,8 +254,15 @@ def results_template(self):
def _read(self, template_name):
with open("%s/resources/%s.json" % (self.script_dir, template_name), encoding="utf-8") as f:
template = json.load(f)
template["settings"]["index"]["number_of_shards"] = self._number_of_shards
template["settings"]["index"]["number_of_replicas"] = self._number_of_replicas
if self._number_of_shards is not None:
if int(self._number_of_shards) < 1:
raise exceptions.SystemSetupError(
f"The setting: datastore.number_of_shards must be >= 1. Please "
f"check the configuration in {self._config.config_file.location}"
)
template["settings"]["index"]["number_of_shards"] = int(self._number_of_shards)
if self._number_of_replicas is not None:
template["settings"]["index"]["number_of_replicas"] = int(self._number_of_replicas)
return json.dumps(template)


Expand Down
2 changes: 0 additions & 2 deletions esrally/resources/metrics-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"index_patterns": ["rally-metrics-*"],
"settings": {
"index": {
"refresh_interval": "5s",
"number_of_shards": 1
}
},
"mappings": {
Expand Down
2 changes: 0 additions & 2 deletions esrally/resources/races-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"index_patterns": ["rally-races-*"],
"settings": {
"index": {
"refresh_interval": "5s",
"number_of_shards": 1
}
},
"mappings": {
Expand Down
2 changes: 0 additions & 2 deletions esrally/resources/results-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"index_patterns": ["rally-results-*"],
"settings": {
"index": {
"refresh_interval": "5s",
"number_of_shards": 1
}
},
"mappings": {
Expand Down
91 changes: 89 additions & 2 deletions tests/metrics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2340,10 +2340,10 @@ def setUp(self):
self.cfg.add(config.Scope.application, "system", "time.start", FileRaceStoreTests.RACE_TIMESTAMP)
self.cfg.add(config.Scope.application, "system", "race.id", FileRaceStoreTests.RACE_ID)

def test_datastore_type_elasticsearch_index_template_update(self):
def test_primary_and_replica_shard_count_specified_index_template_update(self):
_datastore_type = "elasticsearch"
_datastore_number_of_shards = random.randint(1, 100)
_datastore_number_of_replicas = random.randint(1, 100)
_datastore_number_of_replicas = random.randint(0, 100)

self.cfg.add(config.Scope.applicationOverride, "reporting", "datastore.type", _datastore_type)
self.cfg.add(config.Scope.applicationOverride, "reporting", "datastore.number_of_shards", _datastore_number_of_shards)
Expand All @@ -2361,3 +2361,90 @@ def test_datastore_type_elasticsearch_index_template_update(self):
t = json.loads(template)
assert t["settings"]["index"]["number_of_shards"] == _datastore_number_of_shards
assert t["settings"]["index"]["number_of_replicas"] == _datastore_number_of_replicas

def test_primary_shard_count_specified_index_template_update(self):
_datastore_type = "elasticsearch"
_datastore_number_of_shards = random.randint(1, 100)

self.cfg.add(config.Scope.applicationOverride, "reporting", "datastore.type", _datastore_type)
self.cfg.add(config.Scope.applicationOverride, "reporting", "datastore.number_of_shards", _datastore_number_of_shards)

_index_template_provider = metrics.IndexTemplateProvider(self.cfg)

templates = [
_index_template_provider.metrics_template(),
_index_template_provider.races_template(),
_index_template_provider.results_template(),
]

for template in templates:
t = json.loads(template)
assert t["settings"]["index"]["number_of_shards"] == _datastore_number_of_shards
with self.assertRaises(KeyError):
# pylint: disable=unused-variable
number_of_replicas = t["settings"]["index"]["number_of_replicas"]

def test_replica_shard_count_specified_index_template_update(self):
_datastore_type = "elasticsearch"
_datastore_number_of_replicas = random.randint(1, 100)

self.cfg.add(config.Scope.applicationOverride, "reporting", "datastore.type", _datastore_type)
self.cfg.add(config.Scope.applicationOverride, "reporting", "datastore.number_of_replicas", _datastore_number_of_replicas)

_index_template_provider = metrics.IndexTemplateProvider(self.cfg)

templates = [
_index_template_provider.metrics_template(),
_index_template_provider.races_template(),
_index_template_provider.results_template(),
]

for template in templates:
t = json.loads(template)
assert t["settings"]["index"]["number_of_replicas"] == _datastore_number_of_replicas
with self.assertRaises(KeyError):
# pylint: disable=unused-variable
number_of_shards = t["settings"]["index"]["number_of_shards"]

def test_primary_shard_count_less_than_one(self):
_datastore_type = "elasticsearch"
_datastore_number_of_shards = 0

self.cfg.add(config.Scope.applicationOverride, "reporting", "datastore.type", _datastore_type)
self.cfg.add(config.Scope.applicationOverride, "reporting", "datastore.number_of_shards", _datastore_number_of_shards)
_index_template_provider = metrics.IndexTemplateProvider(self.cfg)

with self.assertRaises(exceptions.SystemSetupError) as ctx:
# pylint: disable=unused-variable
templates = [
_index_template_provider.metrics_template(),
_index_template_provider.races_template(),
_index_template_provider.results_template(),
]
self.assertEqual(
"The setting: datastore.number_of_shards must be >= 1. Please check the configuration in "
f"{_index_template_provider._config.config_file.location}",
ctx.exception.args[0],
)

def test_primary_and_replica_shard_counts_passed_as_strings(self):
_datastore_type = "elasticsearch"
_datastore_number_of_shards = "200"
_datastore_number_of_replicas = "1"

self.cfg.add(config.Scope.applicationOverride, "reporting", "datastore.type", _datastore_type)
self.cfg.add(config.Scope.applicationOverride, "reporting", "datastore.number_of_shards", _datastore_number_of_shards)
self.cfg.add(config.Scope.applicationOverride, "reporting", "datastore.number_of_replicas", _datastore_number_of_replicas)

_index_template_provider = metrics.IndexTemplateProvider(self.cfg)

templates = [
_index_template_provider.metrics_template(),
_index_template_provider.races_template(),
_index_template_provider.results_template(),
]

for template in templates:
t = json.loads(template)
assert t["settings"]["index"]["number_of_shards"] == 200
assert t["settings"]["index"]["number_of_replicas"] == 1

0 comments on commit 9474c0d

Please sign in to comment.