@@ -119,7 +119,11 @@ def _window_to_group(value):
119
119
return value .pipe (
120
120
ops .to_iterable (),
121
121
ops .map (lambda x : rx .from_iterable (x ).pipe (
122
- ops .group_by (_group_by ), ops .map (_group_to_batch ), ops .merge_all ())), ops .merge_all ())
122
+ # Group window by 'organization', 'bucket' and 'precision'
123
+ ops .group_by (_group_by ),
124
+ # Create batch (concatenation line protocols by \n)
125
+ ops .map (_group_to_batch ),
126
+ ops .merge_all ())), ops .merge_all ())
123
127
124
128
125
129
class WriteApi (AbstractClient ):
@@ -129,15 +133,20 @@ def __init__(self, influxdb_client, write_options: WriteOptions = WriteOptions()
129
133
self ._write_service = WriteService (influxdb_client .api_client )
130
134
self ._write_options = write_options
131
135
if self ._write_options .write_type is WriteType .batching :
136
+ # Define Subject that listen incoming data and produces writes into InfluxDB
132
137
self ._subject = Subject ()
133
138
139
+ # Define a scheduler that is used for processing incoming data - default singleton
134
140
observable = self ._subject .pipe (ops .observe_on (self ._write_options .write_scheduler ))
135
141
self ._disposable = observable \
136
- .pipe (ops .window_with_time_or_count (count = write_options .batch_size ,
137
- timespan = timedelta (milliseconds = write_options .flush_interval )),
138
- ops .flat_map (lambda v : _window_to_group (v )),
139
- ops .map (mapper = lambda x : self ._retryable (data = x , delay = self ._jitter_delay ())),
140
- ops .merge_all ()) \
142
+ .pipe ( # Split incoming data to windows by batch_size or flush_interval
143
+ ops .window_with_time_or_count (count = write_options .batch_size ,
144
+ timespan = timedelta (milliseconds = write_options .flush_interval )),
145
+ # Map incoming batch window in groups defined by 'organization', 'bucket' and 'precision'
146
+ ops .flat_map (lambda v : _window_to_group (v )),
147
+ # Write data into InfluxDB (possibility to retry if its fail)
148
+ ops .map (mapper = lambda batch : self ._retryable (data = batch , delay = self ._jitter_delay ())), #
149
+ ops .merge_all ()) \
141
150
.subscribe (self ._on_next , self ._on_error , self ._on_complete )
142
151
else :
143
152
self ._subject = None
@@ -244,8 +253,11 @@ def _post_write(self, _async_req, bucket, org, body, precision):
244
253
def _retryable (self , data : str , delay : timedelta ):
245
254
246
255
return rx .of (data ).pipe (
256
+ # use delay if its specified
247
257
ops .delay (duetime = delay , scheduler = self ._write_options .write_scheduler ),
258
+ # invoke http call
248
259
ops .map (lambda x : self ._http (x )),
260
+ # if there is an error than retry
249
261
ops .catch (handler = lambda exception , source : self ._retry_handler (exception , source , data )),
250
262
)
251
263
0 commit comments