Skip to content

run count and search concurrently, but don't wait for count to be done #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Database logic."""
import asyncio
import logging
from base64 import urlsafe_b64decode, urlsafe_b64encode
from typing import Dict, List, Optional, Tuple, Type, Union
Expand Down Expand Up @@ -196,24 +197,28 @@ async def execute_search(
base_url: str,
) -> Tuple[List[Item], Optional[int], Optional[str]]:
"""Database logic to execute search with limit."""
body = search.to_dict()

maybe_count = (
await self.client.count(index=ITEMS_INDEX, body=search.to_dict(count=True))
).get("count")

search_after = None
if token:
search_after = urlsafe_b64decode(token.encode()).decode().split(",")

es_response = await self.client.search(
index=ITEMS_INDEX,
query=body.get("query"),
sort=sort or DEFAULT_SORT,
search_after=search_after,
size=limit,
query = search.query.to_dict() if search.query else None

search_task = asyncio.create_task(
self.client.search(
index=ITEMS_INDEX,
query=query,
sort=sort or DEFAULT_SORT,
search_after=search_after,
size=limit,
)
)

count_task = asyncio.create_task(
self.client.count(index=ITEMS_INDEX, body=search.to_dict(count=True))
)

es_response = await search_task

hits = es_response["hits"]["hits"]
items = [
self.item_serializer.db_to_stac(hit["_source"], base_url=base_url)
Expand All @@ -226,6 +231,15 @@ async def execute_search(
",".join([str(x) for x in sort_array]).encode()
).decode()

# (1) count should not block returning results, so don't wait for it to be done
# (2) don't cancel the task so that it will populate the ES cache for subsequent counts
maybe_count = None
if count_task.done():
try:
maybe_count = count_task.result().get("count")
except Exception as e: # type: ignore
logger.error(f"Count task failed: {e}")

return items, maybe_count, next_token

""" TRANSACTION LOGIC """
Expand Down
4 changes: 3 additions & 1 deletion stac_fastapi/elasticsearch/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ async def test_app_context_extension(app_client, ctx, txn_client):
resp_json = resp.json()
assert len(resp_json["features"]) == 1
assert "context" in resp_json
assert resp_json["context"]["returned"] == resp_json["context"]["matched"] == 1
assert resp_json["context"]["returned"] == 1
if matched := resp_json["context"].get("matched"):
assert matched == 1


@pytest.mark.skip(reason="fields not implemented yet")
Expand Down
3 changes: 2 additions & 1 deletion stac_fastapi/elasticsearch/tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ async def test_get_item_collection(app_client, ctx, txn_client):
assert resp.status_code == 200

item_collection = resp.json()
assert item_collection["context"]["matched"] == item_count + 1
if matched := item_collection["context"].get("matched"):
assert matched == item_count + 1


@pytest.mark.skip(reason="Pagination extension not implemented")
Expand Down