This is an automated email from the ASF dual-hosted git repository.
janhoy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr-orbit.git
The following commit(s) were added to refs/heads/main by this push:
new dcdeb21e Page through collections with SolrClient.search in
create-workload (#125)
dcdeb21e is described below
commit dcdeb21eb9d41d6ea52e1ef91a60708825c2063e
Author: Syed Ahnaf Raza <[email protected]>
AuthorDate: Sat Oct 17 04:29:08 2026 +0500
Page through collections with SolrClient.search in create-workload (#125)
* Test the create-workload cursor scan against SolrClient
_cursor_scan() calls _get_session(), base_url and timeout, which only
SolrAdminClient has, so it fails on the SolrClient that create-workload passes
in (#122).
* Page through collections with SolrClient.search in create-workload
_cursor_scan() reached for SolrAdminClient internals on the SolrClient it
is given. Use the data client's search() instead, which goes through pysolr
with the client's host, auth, TLS and timeout, and read the cursor from the
Results.
Fixes #122
---
solrorbit/workload_generator/extractors.py | 18 +++-------
tests/workload_generator/extractors_test.py | 54 +++++++++++++++++++++++++++++
2 files changed, 58 insertions(+), 14 deletions(-)
diff --git a/solrorbit/workload_generator/extractors.py
b/solrorbit/workload_generator/extractors.py
index 46db822b..f2280f8a 100644
--- a/solrorbit/workload_generator/extractors.py
+++ b/solrorbit/workload_generator/extractors.py
@@ -31,23 +31,13 @@ def _cursor_scan(client, collection, batch_size=1000):
Each yielded document is a plain dict with Solr internal fields
(``_version_``) removed.
"""
cursor = "*"
- session = client._get_session() # pylint: disable=protected-access
- url = f"{client.base_url}/solr/{collection}/select"
while True:
- resp = session.get(
- url,
- params={"q": "*:*", "rows": batch_size, "sort": "id asc",
- "cursorMark": cursor, "wt": "json"},
- timeout=client.timeout,
- )
- resp.raise_for_status()
- data = resp.json()
- docs = data["response"]["docs"]
- if not docs:
+ results = client.search(collection, "*:*", rows=batch_size, sort="id
asc", cursorMark=cursor)
+ if not results.docs:
break
- for doc in docs:
+ for doc in results.docs:
yield {k: v for k, v in doc.items() if k not in
_SOLR_INTERNAL_FIELDS}
- next_cursor = data.get("nextCursorMark", cursor)
+ next_cursor = results.nextCursorMark or cursor
if next_cursor == cursor:
break
cursor = next_cursor
diff --git a/tests/workload_generator/extractors_test.py
b/tests/workload_generator/extractors_test.py
new file mode 100644
index 00000000..50a02d58
--- /dev/null
+++ b/tests/workload_generator/extractors_test.py
@@ -0,0 +1,54 @@
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF 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.
+
+"""Reading every document of a collection for create-workload."""
+
+import unittest.mock as mock
+from unittest import TestCase
+
+from solrorbit.client import SolrClient
+from solrorbit.workload_generator import extractors
+
+
+def _page(docs, next_cursor_mark):
+ return mock.Mock(docs=docs, nextCursorMark=next_cursor_mark)
+
+
+class CursorScanTests(TestCase):
+ def test_pages_through_a_collection_with_the_data_client(self):
+ client = SolrClient(host="localhost", port=8983)
+ pages = [
+ _page([{"id": "1", "_version_": 11}, {"id": "2", "_version_":
12}], "AoE2"),
+ _page([{"id": "3", "_version_": 13}], "AoE3"),
+ _page([], "AoE3"),
+ ]
+ with mock.patch.object(client, "search", side_effect=pages) as search:
+ docs = list(extractors._cursor_scan(client, "geonames",
batch_size=2)) # pylint: disable=protected-access
+
+ self.assertEqual([{"id": "1"}, {"id": "2"}, {"id": "3"}], docs)
+ self.assertEqual(["*", "AoE2", "AoE3"], [call.kwargs["cursorMark"] for
call in search.call_args_list])
+ for call in search.call_args_list:
+ self.assertEqual(("geonames", "*:*"), call.args)
+ self.assertEqual({"rows": 2, "sort": "id asc"}, {k: call.kwargs[k]
for k in ("rows", "sort")})
+
+ def test_stops_when_the_cursor_does_not_move(self):
+ client = SolrClient(host="localhost", port=8983)
+ with mock.patch.object(client, "search", return_value=_page([{"id":
"1"}], "*")) as search:
+ docs = list(extractors._cursor_scan(client, "geonames")) #
pylint: disable=protected-access
+
+ self.assertEqual([{"id": "1"}], docs)
+ search.assert_called_once()