Skip to content

Commit 53ed6ce

Browse files
test: add multiprocess helper test
1 parent 0236b08 commit 53ed6ce

4 files changed

Lines changed: 55 additions & 49 deletions

File tree

influxdb_client_3/write_client/_sync/rest_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ def request(self, method, url, query_params=None, headers=None,
158158
r.data = r.data.decode('utf8')
159159

160160
if not 200 <= r.status <= 299:
161+
print('hahaha')
162+
print(r.data)
161163
raise ApiException(http_resp=r)
162164

163165
return r

influxdb_client_3/write_client/client/util/multiprocessing_helper.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import multiprocessing
99

10+
from influxdb_client_3 import InfluxDBClient3, write_client_options
1011
from influxdb_client_3.write_client import WriteOptions
1112
from influxdb_client_3.exceptions import InfluxDBError
1213

@@ -148,11 +149,13 @@ def write(self, **kwargs) -> None:
148149
def run(self):
149150
"""Initialize ``InfluxDBClient`` and waits for data to writes into InfluxDB."""
150151
# Initialize Client and Write API
151-
self.client = InfluxDBClient(**self.kwargs)
152-
self.write_api = self.client.write_api(write_options=self.kwargs.get('write_options', WriteOptions()),
153-
success_callback=self.kwargs.get('success_callback', _success_callback),
154-
error_callback=self.kwargs.get('error_callback', _error_callback),
155-
retry_callback=self.kwargs.get('retry_callback', _retry_callback))
152+
wco = write_client_options(write_options=self.kwargs.get('write_options', WriteOptions()),
153+
success_callback=self.kwargs.get('success_callback', _success_callback),
154+
error_callback=self.kwargs.get('error_callback', _error_callback),
155+
retry_callback=self.kwargs.get('retry_callback', _retry_callback)
156+
)
157+
self.client = InfluxDBClient3(write_client_options=wco, **self.kwargs)
158+
self.write_api = self.client._write_api
156159
# Infinite loop - until poison pill
157160
while True:
158161
next_record = self.queue_.get()
@@ -181,7 +184,7 @@ def terminate(self) -> None:
181184
self.write_api.__del__()
182185
self.write_api = None
183186
if self.client:
184-
self.client.__del__()
187+
self.client.close()
185188
self.client = None
186189
logger.info("closed")
187190

influxdb_client_3/write_client/client/write_api.py

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -961,49 +961,6 @@ def _should_gzip(self, payload: str, enable_gzip: bool = False, gzip_threshold:
961961

962962
return False
963963

964-
@staticmethod
965-
def _on_error(ex):
966-
logger.error("unexpected error during batching: %s", ex)
967-
968-
def _to_response(self, data: _BatchItem, delay: datetime.timedelta):
969-
return rx.of(data).pipe(
970-
ops.subscribe_on(self._write_options.write_scheduler),
971-
# use delay if its specified
972-
ops.delay(duetime=delay, scheduler=self._write_options.write_scheduler),
973-
# invoke http call
974-
ops.map(lambda x: self._http(x, **x.key.kwargs)),
975-
# catch exception to fail batch response
976-
ops.catch(handler=lambda exception, source: rx.just(_BatchResponse(exception=exception, data=data))),
977-
)
978-
979-
def _on_next(self, response: _BatchResponse):
980-
if response.exception:
981-
logger.error("The batch item wasn't processed successfully because: %s", response.exception)
982-
if self._error_callback:
983-
try:
984-
self._error_callback(response.data.to_key_tuple(), response.data.data, response.exception)
985-
except Exception as e:
986-
"""
987-
Unfortunately, because callbacks are user-provided generic code, exceptions can be entirely
988-
arbitrary
989-
990-
We trap it, log that it occurred and then proceed - there's not much more that we can
991-
really do.
992-
"""
993-
logger.error("The configured error callback threw an exception: %s", e)
994-
995-
else:
996-
logger.debug("The batch item: %s was processed successfully.", response)
997-
if self._success_callback:
998-
try:
999-
self._success_callback(response.data.to_key_tuple(), response.data.data)
1000-
except Exception as e:
1001-
logger.error("The configured success callback threw an exception: %s", e)
1002-
1003-
def _on_complete(self):
1004-
self._disposable.dispose()
1005-
logger.debug("the batching processor was disposed")
1006-
1007964
def _append_default_tag(self, key, val, record):
1008965
from influxdb_client_3.write_client import Point
1009966
if isinstance(record, bytes) or isinstance(record, str):
@@ -1088,6 +1045,49 @@ def __del__(self):
10881045
"""Close WriteApi."""
10891046
self.close()
10901047

1048+
@staticmethod
1049+
def _on_error(ex):
1050+
logger.error("unexpected error during batching: %s", ex)
1051+
1052+
def _on_complete(self):
1053+
self._disposable.dispose()
1054+
logger.debug("the batching processor was disposed")
1055+
1056+
def _to_response(self, data: _BatchItem, delay: datetime.timedelta):
1057+
return rx.of(data).pipe(
1058+
ops.subscribe_on(self._write_options.write_scheduler),
1059+
# use delay if its specified
1060+
ops.delay(duetime=delay, scheduler=self._write_options.write_scheduler),
1061+
# invoke http call
1062+
ops.map(lambda x: self._http(x, **x.key.kwargs)),
1063+
# catch exception to fail batch response
1064+
ops.catch(handler=lambda exception, source: rx.just(_BatchResponse(exception=exception, data=data))),
1065+
)
1066+
1067+
def _on_next(self, response: _BatchResponse):
1068+
if response.exception:
1069+
logger.error("The batch item wasn't processed successfully because: %s", response.exception)
1070+
if self._error_callback:
1071+
try:
1072+
self._error_callback(response.data.to_key_tuple(), response.data.data, response.exception)
1073+
except Exception as e:
1074+
"""
1075+
Unfortunately, because callbacks are user-provided generic code, exceptions can be entirely
1076+
arbitrary
1077+
1078+
We trap it, log that it occurred and then proceed - there's not much more that we can
1079+
really do.
1080+
"""
1081+
logger.error("The configured error callback threw an exception: %s", e)
1082+
1083+
else:
1084+
logger.debug("The batch item: %s was processed successfully.", response)
1085+
if self._success_callback:
1086+
try:
1087+
self._success_callback(response.data.to_key_tuple(), response.data.data)
1088+
except Exception as e:
1089+
logger.error("The configured success callback threw an exception: %s", e)
1090+
10911091
def __getstate__(self):
10921092
"""Return a dict of attributes that you want to pickle."""
10931093
state = self.__dict__.copy()

tests/test_influxdb_client_3_integration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from influxdb_client_3 import InfluxDBClient3, write_client_options, WriteOptions, \
1515
WriteType, InfluxDB3ClientQueryError
1616
from influxdb_client_3.exceptions import InfluxDBError, InfluxDBPartialWriteError
17+
from influxdb_client_3.write_client.client.util.multiprocessing_helper import MultiprocessingWriter
1718
from influxdb_client_3.write_client.rest import ApiException
1819
from tests.util import asyncio_run, lp_to_py_object
1920

0 commit comments

Comments
 (0)