-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathparams.py
1392 lines (1191 loc) · 57.4 KB
/
params.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations
import collections
import inspect
import logging
import math
import numbers
import operator
import random
import time
from abc import ABC
from enum import Enum
from typing import Callable, Deque
from esrally import exceptions
from esrally.track import track
from esrally.utils import io
__PARAM_SOURCES_BY_OP: dict[track.OperationType, ParamSource] = {}
__PARAM_SOURCES_BY_NAME: dict[str, ParamSource] = {}
def param_source_for_operation(op_type, track, params, task_name):
try:
# we know that this can only be a Rally core parameter source
return __PARAM_SOURCES_BY_OP[op_type](track, params, operation_name=task_name)
except KeyError:
return ParamSource(track, params, operation_name=task_name)
def param_source_for_name(name, track, params):
param_source = __PARAM_SOURCES_BY_NAME[name]
if inspect.isfunction(param_source):
return DelegatingParamSource(track, params, param_source)
else:
return param_source(track, params)
def ensure_valid_param_source(param_source):
if not inspect.isfunction(param_source) and not inspect.isclass(param_source):
raise exceptions.RallyAssertionError(f"Parameter source [{param_source}] must be either a function or a class.")
def register_param_source_for_operation(op_type, param_source_class):
ensure_valid_param_source(param_source_class)
__PARAM_SOURCES_BY_OP[op_type.to_hyphenated_string()] = param_source_class
def register_param_source_for_name(name, param_source_class):
ensure_valid_param_source(param_source_class)
__PARAM_SOURCES_BY_NAME[name] = param_source_class
# only intended for tests
def _unregister_param_source_for_name(name):
# We intentionally do not specify a default value if the key does not exist. If we try to remove a key that we didn't insert then
# something is fishy with the test and we'd rather know early.
__PARAM_SOURCES_BY_NAME.pop(name)
# Default
class ParamSource:
"""
A `ParamSource` captures the parameters for a given operation. Rally will create one global ParamSource for each operation and will then
invoke `#partition()` to get a `ParamSource` instance for each client. During the benchmark, `#params()` will be called repeatedly
before Rally invokes the corresponding runner (that will actually execute the operation against Elasticsearch).
"""
def __init__(self, track, params, **kwargs):
"""
Creates a new ParamSource instance.
:param track: The current track definition
:param params: A hash of all parameters that have been extracted for this operation.
"""
self.track = track
self._params = params
self.kwargs = kwargs
def partition(self, partition_index, total_partitions):
"""
This method will be invoked by Rally at the beginning of the lifecycle. It splits a parameter source per client. If the
corresponding operation is idempotent, return `self` (e.g. for queries). If the corresponding operation has side-effects and it
matters which client executes which part (e.g. an index operation from a source file), return the relevant part.
Do NOT assume that you can share state between ParamSource objects in different partitions (technical explanation: each client
will be a dedicated process, so each object of a `ParamSource` lives in its own process and hence cannot share state with other
instances).
:param partition_index: The current partition for which a parameter source is needed. It is in the range [0, `total_partitions`).
:param total_partitions: The total number of partitions (i.e. clients).
:return: A parameter source for the current partition.
"""
return self
@property
def infinite(self):
# for bwc
return self.size() is None
# Deprecated
def size(self):
"""
Rally has two modes in which it can run:
* It will either run an operation for a pre-determined number of times or
* It can run until the parameter source is exhausted.
In the former case, you should determine the number of times that `#params()` will be invoked. With that number, Rally can show
the progress made so far to the user. In the latter case, return ``None``.
:return: The "size" of this parameter source or ``None`` if should run eternally.
"""
return None
def params(self):
"""
:return: A hash containing the parameters that will be provided to the corresponding operation runner (key: parameter name,
value: parameter value).
"""
return self._params
def _client_params(self):
"""
For use when a ParamSource does not propagate self._params but does use elasticsearch client under the hood
:return: all applicable parameters that are global to Rally and apply to the elasticsearch-py client
"""
return {
"request-timeout": self._params.get("request-timeout"),
"headers": self._params.get("headers"),
"opaque-id": self._params.get("opaque-id"),
}
class DelegatingParamSource(ParamSource):
def __init__(self, track, params, delegate, **kwargs):
super().__init__(track, params, **kwargs)
self.delegate = delegate
def params(self):
return self.delegate(self.track, self._params, **self.kwargs)
class SleepParamSource(ParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, **kwargs)
try:
duration = params["duration"]
except KeyError:
raise exceptions.InvalidSyntax("parameter 'duration' is mandatory for sleep operation")
if not isinstance(duration, numbers.Number):
raise exceptions.InvalidSyntax("parameter 'duration' for sleep operation must be a number")
if duration < 0:
raise exceptions.InvalidSyntax(f"parameter 'duration' must be non-negative but was {duration}")
def params(self):
return dict(self._params)
class CreateIndexParamSource(ParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, **kwargs)
self.request_params = params.get("request-params", {})
self.index_definitions = []
if track.indices:
filter_idx = params.get("index")
if isinstance(filter_idx, str):
filter_idx = [filter_idx]
settings = params.get("settings")
for idx in track.indices:
if not filter_idx or idx.name in filter_idx:
body = idx.body
if body and settings:
if "settings" in body:
# merge (and potentially override)
body["settings"].update(settings)
else:
body["settings"] = settings
elif not body and settings:
body = {"settings": settings}
self.index_definitions.append((idx.name, body))
else:
try:
# only 'index' is mandatory, the body is optional (may be ok to create an index without a body)
idx = params["index"]
body = params.get("body")
if isinstance(idx, str):
idx = [idx]
for i in idx:
self.index_definitions.append((i, body))
except KeyError:
raise exceptions.InvalidSyntax("Please set the property 'index' for the create-index operation")
def params(self):
p = {}
# ensure we pass all parameters...
p.update(self._params)
p.update(
{
"indices": self.index_definitions,
"request-params": self.request_params,
}
)
return p
class CreateDataStreamParamSource(ParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, **kwargs)
self.request_params = params.get("request-params", {})
self.data_stream_definitions = []
if track.data_streams:
filter_ds = params.get("data-stream")
if isinstance(filter_ds, str):
filter_ds = [filter_ds]
for ds in track.data_streams:
if not filter_ds or ds.name in filter_ds:
self.data_stream_definitions.append(ds.name)
else:
try:
data_stream = params["data-stream"]
data_streams = [data_stream] if isinstance(data_stream, str) else data_stream
for ds in data_streams:
self.data_stream_definitions.append(ds)
except KeyError:
raise exceptions.InvalidSyntax("Please set the property 'data-stream' for the create-data-stream operation")
def params(self):
p = {}
# ensure we pass all parameters...
p.update(self._params)
p.update(
{
"data-streams": self.data_stream_definitions,
"request-params": self.request_params,
}
)
return p
class DeleteDataStreamParamSource(ParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, **kwargs)
self.request_params = params.get("request-params", {})
self.only_if_exists = params.get("only-if-exists", True)
self.data_stream_definitions = []
target_data_stream = params.get("data-stream")
if target_data_stream:
target_data_stream = [target_data_stream] if isinstance(target_data_stream, str) else target_data_stream
for ds in target_data_stream:
self.data_stream_definitions.append(ds)
elif track.data_streams:
for ds in track.data_streams:
self.data_stream_definitions.append(ds.name)
else:
raise exceptions.InvalidSyntax("delete-data-stream operation targets no data stream")
def params(self):
p = {}
# ensure we pass all parameters...
p.update(self._params)
p.update(
{"data-streams": self.data_stream_definitions, "request-params": self.request_params, "only-if-exists": self.only_if_exists}
)
return p
class DeleteIndexParamSource(ParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, **kwargs)
self.request_params = params.get("request-params", {})
self.only_if_exists = params.get("only-if-exists", True)
self.index_definitions = []
target_index = params.get("index")
if target_index:
if isinstance(target_index, str):
target_index = [target_index]
for idx in target_index:
self.index_definitions.append(idx)
elif track.indices:
for idx in track.indices:
self.index_definitions.append(idx.name)
else:
raise exceptions.InvalidSyntax("delete-index operation targets no index")
def params(self):
p = {}
# ensure we pass all parameters...
p.update(self._params)
p.update(
{
"indices": self.index_definitions,
"request-params": self.request_params,
"only-if-exists": self.only_if_exists,
}
)
return p
class CreateIndexTemplateParamSource(ParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, **kwargs)
self.request_params = params.get("request-params", {})
self.template_definitions = []
if track.templates:
filter_template = params.get("template")
settings = params.get("settings")
for template in track.templates:
if not filter_template or template.name == filter_template:
body = template.content
if body and settings:
if "settings" in body:
# merge (and potentially override)
body["settings"].update(settings)
else:
body["settings"] = settings
self.template_definitions.append((template.name, body))
else:
try:
self.template_definitions.append((params["template"], params["body"]))
except KeyError:
raise exceptions.InvalidSyntax("Please set the properties 'template' and 'body' for the create-index-template operation")
def params(self):
p = {}
# ensure we pass all parameters...
p.update(self._params)
p.update(
{
"templates": self.template_definitions,
"request-params": self.request_params,
}
)
return p
class DeleteTemplateParamSource(ABC, ParamSource):
def __init__(self, track, params, templates, **kwargs):
super().__init__(track, params, **kwargs)
self.only_if_exists = params.get("only-if-exists", True)
self.request_params = params.get("request-params", {})
self.template_definitions = []
if templates:
filter_template = params.get("template")
for template in templates:
if not filter_template or template.name == filter_template:
self.template_definitions.append((template.name, template.delete_matching_indices, template.pattern))
else:
try:
template = params["template"]
except KeyError:
raise exceptions.InvalidSyntax(f"Please set the property 'template' for the {params.get('operation-type')} operation")
delete_matching = params.get("delete-matching-indices", False)
try:
index_pattern = params["index-pattern"] if delete_matching else None
except KeyError:
raise exceptions.InvalidSyntax(
"The property 'index-pattern' is required for delete-index-template if 'delete-matching-indices' is true."
)
self.template_definitions.append((template, delete_matching, index_pattern))
def params(self):
p = {}
# ensure we pass all parameters...
p.update(self._params)
p.update(
{
"templates": self.template_definitions,
"only-if-exists": self.only_if_exists,
"request-params": self.request_params,
}
)
return p
class DeleteIndexTemplateParamSource(DeleteTemplateParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, track.templates, **kwargs)
class DeleteComposableTemplateParamSource(DeleteTemplateParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, track.composable_templates, **kwargs)
class DeleteComponentTemplateParamSource(ParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, **kwargs)
self.only_if_exists = params.get("only-if-exists", True)
self.request_params = params.get("request-params", {})
self.template_definitions = []
if track.component_templates:
filter_template = params.get("template")
for template in track.component_templates:
if not filter_template or template.name == filter_template:
self.template_definitions.append(template.name)
else:
try:
template = params["template"]
self.template_definitions.append(template)
except KeyError:
raise exceptions.InvalidSyntax(f"Please set the property 'template' for the {params.get('operation-type')} operation.")
def params(self):
return {
"templates": self.template_definitions,
"only-if-exists": self.only_if_exists,
"request-params": self.request_params,
}
class CreateTemplateParamSource(ABC, ParamSource):
def __init__(self, track, params, templates, **kwargs):
super().__init__(track, params, **kwargs)
self.request_params = params.get("request-params", {})
self.template_definitions = []
if "template" in params and "body" in params:
self.template_definitions.append((params["template"], params["body"]))
elif templates:
filter_template = params.get("template")
settings = params.get("settings")
template_definitions = []
for template in templates:
if not filter_template or template.name == filter_template:
body = self._create_or_merge(template.content, ["template", "settings"], settings)
template_definitions.append((template.name, body))
if filter_template and not template_definitions:
template_names = ", ".join([template.name for template in templates])
raise exceptions.InvalidSyntax(f"Unknown template: {filter_template}. Available templates: {template_names}.")
self.template_definitions.extend(template_definitions)
else:
raise exceptions.InvalidSyntax(
"Please set the properties 'template' and 'body' for the "
f"{params.get('operation-type')} operation or declare composable and/or component "
"templates in the track"
)
@staticmethod
def _create_or_merge(content, path, new_content):
original_content = content
if new_content:
for sub_path in path:
if sub_path not in content:
content[sub_path] = {}
content = content[sub_path]
CreateTemplateParamSource.__merge(content, new_content)
return original_content
@staticmethod
def __merge(dct, merge_dct):
for k in merge_dct.keys():
if k in dct and isinstance(dct[k], dict) and isinstance(merge_dct[k], collections.abc.Mapping):
CreateTemplateParamSource.__merge(dct[k], merge_dct[k])
else:
dct[k] = merge_dct[k]
def params(self):
return {
"templates": self.template_definitions,
"request-params": self.request_params,
}
class CreateComposableTemplateParamSource(CreateTemplateParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, track.composable_templates, **kwargs)
class CreateComponentTemplateParamSource(CreateTemplateParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, track.component_templates, **kwargs)
class SearchParamSource(ParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, **kwargs)
target_name = get_target(track, params)
type_name = params.get("type")
if params.get("data-stream") and type_name:
raise exceptions.InvalidSyntax(f"'type' not supported with 'data-stream' for operation '{kwargs.get('operation_name')}'")
request_cache = params.get("cache", None)
detailed_results = params.get("detailed-results", False)
query_body = params.get("body", None)
pages = params.get("pages", None)
results_per_page = params.get("results-per-page", None)
request_params = params.get("request-params", {})
response_compression_enabled = params.get("response-compression-enabled", True)
with_point_in_time_from = params.get("with-point-in-time-from", None)
self.query_params = {
"index": target_name,
"type": type_name,
"cache": request_cache,
"detailed-results": detailed_results,
"request-params": request_params,
"response-compression-enabled": response_compression_enabled,
"body": query_body,
}
if not target_name:
raise exceptions.InvalidSyntax(
f"'index' or 'data-stream' is mandatory and is missing for operation '{kwargs.get('operation_name')}'"
)
if pages:
self.query_params["pages"] = pages
if results_per_page:
self.query_params["results-per-page"] = results_per_page
if with_point_in_time_from:
self.query_params["with-point-in-time-from"] = with_point_in_time_from
if "assertions" in params:
if not detailed_results:
# for paginated queries the value does not matter because detailed results are always retrieved.
is_paginated = bool(pages)
if not is_paginated:
raise exceptions.InvalidSyntax("The property [detailed-results] must be [true] if assertions are defined")
self.query_params["assertions"] = params["assertions"]
# Ensure we pass global parameters
self.query_params.update(self._client_params())
def params(self):
return self.query_params
class IndexIdConflict(Enum):
"""
Determines which id conflicts to simulate during indexing.
* NoConflicts: Produce no id conflicts
* SequentialConflicts: A document id is replaced with a document id with a sequentially increasing id
* RandomConflicts: A document id is replaced with a document id with a random other id
Note that this assumes that each document in the benchmark corpus has an id between [1, size_of(corpus)]
"""
NoConflicts = 0
SequentialConflicts = 1
RandomConflicts = 2
class BulkIndexParamSource(ParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, **kwargs)
id_conflicts = params.get("conflicts", None)
if not id_conflicts:
self.id_conflicts = IndexIdConflict.NoConflicts
elif id_conflicts == "sequential":
self.id_conflicts = IndexIdConflict.SequentialConflicts
elif id_conflicts == "random":
self.id_conflicts = IndexIdConflict.RandomConflicts
else:
raise exceptions.InvalidSyntax("Unknown 'conflicts' setting [%s]" % id_conflicts)
if "data-streams" in params and self.id_conflicts != IndexIdConflict.NoConflicts:
raise exceptions.InvalidSyntax("'conflicts' cannot be used with 'data-streams'")
if self.id_conflicts != IndexIdConflict.NoConflicts:
self.conflict_probability = self.float_param(
params, name="conflict-probability", default_value=25, min_value=0, max_value=100, min_operator=operator.lt
)
self.on_conflict = params.get("on-conflict", "index")
if self.on_conflict not in ["index", "update"]:
raise exceptions.InvalidSyntax(f"Unknown 'on-conflict' setting [{self.on_conflict}]")
self.recency = self.float_param(params, name="recency", default_value=0, min_value=0, max_value=1, min_operator=operator.lt)
else:
self.conflict_probability = None
self.on_conflict = None
self.recency = None
self.corpora = self.used_corpora(track, params)
if len(self.corpora) == 0:
raise exceptions.InvalidSyntax(
f"There is no document corpus definition for track {track}. You must add at "
f"least one before making bulk requests to Elasticsearch."
)
for corpus in self.corpora:
for document_set in corpus.documents:
if document_set.includes_action_and_meta_data and self.id_conflicts != IndexIdConflict.NoConflicts:
file_name = document_set.document_archive if document_set.has_compressed_corpus() else document_set.document_file
raise exceptions.InvalidSyntax(
"Cannot generate id conflicts [%s] as [%s] in document corpus [%s] already contains an "
"action and meta-data line." % (id_conflicts, file_name, corpus)
)
self.pipeline = params.get("pipeline", None)
try:
self.bulk_size = int(params["bulk-size"])
if self.bulk_size <= 0:
raise exceptions.InvalidSyntax("'bulk-size' must be positive but was %d" % self.bulk_size)
except KeyError:
raise exceptions.InvalidSyntax("Mandatory parameter 'bulk-size' is missing")
except ValueError:
raise exceptions.InvalidSyntax("'bulk-size' must be numeric")
try:
self.batch_size = int(params.get("batch-size", self.bulk_size))
if self.batch_size <= 0:
raise exceptions.InvalidSyntax("'batch-size' must be positive but was %d" % self.batch_size)
if self.batch_size < self.bulk_size:
raise exceptions.InvalidSyntax("'batch-size' must be greater than or equal to 'bulk-size'")
if self.batch_size % self.bulk_size != 0:
raise exceptions.InvalidSyntax("'batch-size' must be a multiple of 'bulk-size'")
except ValueError:
raise exceptions.InvalidSyntax("'batch-size' must be numeric")
self.ingest_percentage = self.float_param(params, name="ingest-percentage", default_value=100, min_value=0, max_value=100)
self.refresh = params.get("refresh")
self.looped = params.get("looped", False)
self.param_source = PartitionBulkIndexParamSource(
self.corpora,
self.batch_size,
self.bulk_size,
self.ingest_percentage,
self.id_conflicts,
self.conflict_probability,
self.on_conflict,
self.recency,
self.pipeline,
self.refresh,
self.looped,
self._params,
)
def float_param(self, params, name, default_value, min_value, max_value, min_operator=operator.le):
try:
value = float(params.get(name, default_value))
if min_operator(value, min_value) or value > max_value:
interval_min = "(" if min_operator is operator.le else "["
raise exceptions.InvalidSyntax(
f"'{name}' must be in the range {interval_min}{min_value:.1f}, {max_value:.1f}] but was {value:.1f}"
)
return value
except ValueError:
raise exceptions.InvalidSyntax(f"'{name}' must be numeric")
def used_corpora(self, t, params):
corpora = []
track_corpora_names = [corpus.name for corpus in t.corpora]
corpora_names = params.get("corpora", track_corpora_names)
if isinstance(corpora_names, str):
corpora_names = [corpora_names]
for corpus in t.corpora:
if corpus.name in corpora_names:
filtered_corpus = corpus.filter(
source_format=track.Documents.SOURCE_FORMAT_BULK,
target_indices=params.get("indices"),
target_data_streams=params.get("data-streams"),
)
if filtered_corpus.number_of_documents(source_format=track.Documents.SOURCE_FORMAT_BULK) > 0:
corpora.append(filtered_corpus)
# the track has corpora but none of them match
if t.corpora and not corpora:
raise exceptions.RallyAssertionError(
"The provided corpus %s does not match any of the corpora %s." % (corpora_names, track_corpora_names)
)
return corpora
def partition(self, partition_index, total_partitions):
# register the new partition internally
self.param_source.partition(partition_index, total_partitions)
return self.param_source
def params(self):
raise exceptions.RallyError("Do not use a BulkIndexParamSource without partitioning")
class PartitionBulkIndexParamSource:
def __init__(
self,
corpora,
batch_size,
bulk_size,
ingest_percentage,
id_conflicts,
conflict_probability,
on_conflict,
recency,
pipeline=None,
refresh=None,
looped: bool = False,
original_params=None,
):
"""
:param corpora: Specification of affected document corpora.
:param batch_size: The number of documents to read in one go.
:param bulk_size: The size of bulk index operations (number of documents per bulk).
:param ingest_percentage: A number between (0.0, 100.0] that defines how much of the whole corpus should be ingested.
:param id_conflicts: The type of id conflicts.
:param conflict_probability: A number between (0.0, 100.0] that defines the probability that a document is replaced by another one.
:param on_conflict: A string indicating which action should be taken on id conflicts (either "index" or "update").
:param recency: A number between [0.0, 1.0] indicating whether to bias generation of conflicting ids towards more recent ones.
May be None.
:param pipeline: The name of the ingest pipeline to run.
:param refresh: Optional string values are "true", "wait_for", "false".
If "true", Elasticsearch refreshes the affected shards in the background.
If "wait_for", the client is blocked until Elasticsearch finishes the refresh operation.
If "false", Elasticsearch will use the default refresh behavior.
:param looped: Set to True for looped mode where bulk requests are repeated from the beginning when entire corpus was ingested.
:param original_params: The original dict passed to the parent parameter source.
"""
self.corpora = corpora
self.partitions = []
self.total_partitions = None
self.batch_size = batch_size
self.bulk_size = bulk_size
self.ingest_percentage = ingest_percentage
self.id_conflicts = id_conflicts
self.conflict_probability = conflict_probability
self.on_conflict = on_conflict
self.recency = recency
self.pipeline = pipeline
self.refresh = refresh
self.looped = looped
self.original_params = original_params
# this is only intended for unit-testing
self.create_reader = original_params.pop("__create_reader", create_default_reader)
self.current_bulk = 0
# use a value > 0 so percent_completed returns a sensible value
self.total_bulks = 1
self.infinite = False
def partition(self, partition_index, total_partitions):
if self.total_partitions is None:
self.total_partitions = total_partitions
elif self.total_partitions != total_partitions:
raise exceptions.RallyAssertionError(
f"Total partitions is expected to be [{self.total_partitions}] but was [{total_partitions}]"
)
self.partitions.append(partition_index)
def params(self):
if self.current_bulk == 0:
self._init_internal_params()
# self.internal_params always reads all files. This is necessary to ensure we terminate early in case
# the user has specified ingest percentage.
if self.current_bulk == self.total_bulks:
# start from the beginning in looped mode, otherwise stop the run
if self.looped:
self.current_bulk = 0
self._init_internal_params()
else:
raise StopIteration()
self.current_bulk += 1
return next(self.internal_params)
def _init_internal_params(self):
# contains a continuous range of client ids
self.partitions = sorted(self.partitions)
start_index = self.partitions[0]
end_index = self.partitions[-1]
self.internal_params = bulk_data_based(
self.total_partitions,
start_index,
end_index,
self.corpora,
self.batch_size,
self.bulk_size,
self.id_conflicts,
self.conflict_probability,
self.on_conflict,
self.recency,
self.pipeline,
self.original_params,
self.create_reader,
)
all_bulks = number_of_bulks(self.corpora, start_index, end_index, self.total_partitions, self.bulk_size)
self.total_bulks = math.ceil((all_bulks * self.ingest_percentage) / 100)
@property
def percent_completed(self):
return self.current_bulk / self.total_bulks
class OpenPointInTimeParamSource(ParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, **kwargs)
target_name = get_target(track, params)
self._index_name = target_name
self._keep_alive = params.get("keep-alive")
def params(self):
parsed_params = {"index": self._index_name, "keep-alive": self._keep_alive}
parsed_params.update(self._client_params())
return parsed_params
class ClosePointInTimeParamSource(ParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, **kwargs)
self._pit_task_name = params.get("with-point-in-time-from")
def params(self):
parsed_params = {"with-point-in-time-from": self._pit_task_name}
parsed_params.update(self._client_params())
return parsed_params
class ForceMergeParamSource(ParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, **kwargs)
if len(track.indices) > 0 or len(track.data_streams) > 0:
# force merge data streams and indices - API call is the same so treat as indices
default_target = ",".join(map(str, track.indices + track.data_streams))
else:
default_target = "_all"
self._target_name = params.get("index")
if not self._target_name:
self._target_name = params.get("data-stream", default_target)
self._max_num_segments = params.get("max-num-segments")
self._poll_period = params.get("poll-period", 10)
self._mode = params.get("mode", "blocking")
def params(self):
parsed_params = {
"index": self._target_name,
"max-num-segments": self._max_num_segments,
"mode": self._mode,
"poll-period": self._poll_period,
}
parsed_params.update(self._client_params())
return parsed_params
class DownsampleParamSource(ParamSource):
def __init__(self, track, params, **kwargs):
super().__init__(track, params, **kwargs)
self._fixed_interval = params.get("fixed-interval", "1h")
params["index"] = params.get("source-index")
self._source_index = get_target(track, params)
self._target_index = params.get("target-index", f"{self._source_index}-{self._fixed_interval}")
def params(self):
parsed_params = {"fixed-interval": self._fixed_interval, "source-index": self._source_index, "target-index": self._target_index}
parsed_params.update(self._client_params())
return parsed_params
def get_target(track, params):
if len(track.indices) == 1:
default_target = track.indices[0].name
elif len(track.data_streams) == 1:
default_target = track.data_streams[0].name
else:
default_target = None
# indices are preferred but data streams can also be queried the same way
target_name = params.get("index")
if not target_name:
target_name = params.get("data-stream", default_target)
return target_name
def number_of_bulks(corpora, start_partition_index, end_partition_index, total_partitions, bulk_size):
"""
:return: The number of bulk operations that the given client will issue.
"""
bulks = 0
for corpus in corpora:
for docs in corpus.documents:
_, num_docs, _ = bounds(
docs.number_of_documents, start_partition_index, end_partition_index, total_partitions, docs.includes_action_and_meta_data
)
complete_bulks, rest = (num_docs // bulk_size, num_docs % bulk_size)
bulks += complete_bulks
if rest > 0:
bulks += 1
return bulks
def build_conflicting_ids(conflicts, docs_to_index, offset, shuffle=random.shuffle):
if conflicts is None or conflicts == IndexIdConflict.NoConflicts:
return None
all_ids = [0] * docs_to_index
for i in range(docs_to_index):
# always consider the offset as each client will index its own range and we don't want uncontrolled conflicts across clients
all_ids[i] = "%010d" % (offset + i)
if conflicts == IndexIdConflict.RandomConflicts:
shuffle(all_ids)
return all_ids
def chain(*iterables):
"""
Chains the given iterables similar to `itertools.chain` except that it also respects the context manager contract.
:param iterables: A number of iterable that should be chained.
:return: An iterable that will delegate to all provided iterables in turn.
"""
for it in filter(lambda x: x is not None, iterables):
# execute within a context
with it:
yield from it
def create_default_reader(
docs, offset, num_lines, num_docs, batch_size, bulk_size, id_conflicts, conflict_probability, on_conflict, recency
):
source = Slice(io.MmapSource, offset, num_lines)
target = None
use_create = False
if docs.target_index:
target = docs.target_index
elif docs.target_data_stream:
target = docs.target_data_stream
use_create = True
if id_conflicts != IndexIdConflict.NoConflicts:
# can only create docs in data streams
raise exceptions.RallyError("Conflicts cannot be generated with append only data streams")
if docs.includes_action_and_meta_data:
return SourceOnlyIndexDataReader(docs.document_file, batch_size, bulk_size, source, target, docs.target_type)
else:
am_handler = GenerateActionMetaData(
target,
docs.target_type,
build_conflicting_ids(id_conflicts, num_docs, offset),
conflict_probability,
on_conflict,
recency,
use_create=use_create,
)
return MetadataIndexDataReader(docs.document_file, batch_size, bulk_size, source, am_handler, target, docs.target_type)
def create_readers(
num_clients: int,
start_client_index: int,
end_client_index: int,
corpora: list[track.DocumentCorpus],
batch_size: int,
bulk_size: int,
id_conflicts: IndexIdConflict,
conflict_probability: float,
on_conflict: str,
recency: str,
create_reader: Callable[..., IndexDataReader],
) -> list[IndexDataReader]:
"""
Return a list of IndexDataReader instances to allow a range of clients to read their share of corpora.
We're looking for better parallelism between corpora in indexing tasks in two ways:
1. By giving each client its own starting point in the list of corpora (using a
modulus of the number of corpora listed and the number of the client). In a track
with 2 corpora and 5 clients, clients 1, 3, and 5 would start with the first corpus
and clients 2 and 4 would start with the second corpus.
2. By generating the IndexDataReader list round-robin among all files, instead of in
order. If I'm the first client, I start with the first partition of the first file
of the first corpus. Then I move on to the first partition of the first file of the
second corpus, and so on.
"""
corpora_readers: list[Deque[IndexDataReader]] = []
total_readers = 0
# stagger which corpus each client starts with for better parallelism (see 1. above)
start_corpora_id = start_client_index % len(corpora)
reordered_corpora = corpora[start_corpora_id:] + corpora[:start_corpora_id]
for corpus in reordered_corpora:
reader_queue: Deque[IndexDataReader] = collections.deque()
for docs in corpus.documents:
offset, num_docs, num_lines = bounds(
docs.number_of_documents, start_client_index, end_client_index, num_clients, docs.includes_action_and_meta_data
)