Skip to content

Commit 1ba5bcf

Browse files
github-actions[bot]pquentinseagrine
authored
[Backport 8.16] Make BulkIndexError and ScanError serializable (#2700)
Co-authored-by: Quentin Pradet <[email protected]> Co-authored-by: Sebastian Goodman <[email protected]>
1 parent b113880 commit 1ba5bcf

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

Diff for: elasticsearch/helpers/errors.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,26 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from typing import Any, Dict, List
18+
from typing import Any, Dict, List, Tuple, Type
1919

2020

2121
class BulkIndexError(Exception):
22-
def __init__(self, message: Any, errors: List[Dict[str, Any]]):
22+
def __init__(self, message: str, errors: List[Dict[str, Any]]):
2323
super().__init__(message)
2424
self.errors: List[Dict[str, Any]] = errors
2525

26+
def __reduce__(
27+
self,
28+
) -> Tuple[Type["BulkIndexError"], Tuple[str, List[Dict[str, Any]]]]:
29+
return (self.__class__, (self.args[0], self.errors))
30+
2631

2732
class ScanError(Exception):
2833
scroll_id: str
2934

30-
def __init__(self, scroll_id: str, *args: Any, **kwargs: Any) -> None:
31-
super().__init__(*args, **kwargs)
35+
def __init__(self, scroll_id: str, *args: Any) -> None:
36+
super().__init__(*args)
3237
self.scroll_id = scroll_id
38+
39+
def __reduce__(self) -> Tuple[Type["ScanError"], Tuple[str, str]]:
40+
return (self.__class__, (self.scroll_id,) + self.args)

Diff for: test_elasticsearch/test_helpers.py

+17
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
import pickle
1819
import threading
1920
import time
2021
from unittest import mock
@@ -182,3 +183,19 @@ class TestExpandActions:
182183
@pytest.mark.parametrize("action", ["whatever", b"whatever"])
183184
def test_string_actions_are_marked_as_simple_inserts(self, action):
184185
assert ({"index": {}}, b"whatever") == helpers.expand_action(action)
186+
187+
188+
def test_serialize_bulk_index_error():
189+
error = helpers.BulkIndexError("message", [{"error": 1}])
190+
pickled = pickle.loads(pickle.dumps(error))
191+
assert pickled.__class__ == helpers.BulkIndexError
192+
assert pickled.errors == error.errors
193+
assert pickled.args == error.args
194+
195+
196+
def test_serialize_scan_error():
197+
error = helpers.ScanError("scroll_id", "shard_message")
198+
pickled = pickle.loads(pickle.dumps(error))
199+
assert pickled.__class__ == helpers.ScanError
200+
assert pickled.scroll_id == error.scroll_id
201+
assert pickled.args == error.args

0 commit comments

Comments
 (0)