mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-30 20:06:22 -05:00
Zim estimates and searches run in a libzim worker process so a cold Zim cannot stall the API worker (#928)
* Zim search estimates run in a separate process so a cold estimate cannot stall the API worker The global search estimates every auto-search Zim on each keystroke. On a large library that estimate took tens of seconds when the Zims' pages were not cached, and every other request routed to the same Sanic worker, such as the channel suggestions, waited behind it. Two things held the worker: opening a large Zim cold takes seconds (ten for a 75 GB Zim) and `Zims.get_all` did that on the event loop; and python-libzim never releases the GIL, so the estimate offloaded with asyncio.to_thread still froze the worker. Everything libzim does for an estimate now runs in a standalone script started as a subprocess (Sanic workers are daemonic, so a multiprocessing pool cannot be forked from them). It reads the Zim paths and search string on stdin and writes the counts on stdout. A Zim it cannot read is reported and left out, as `Zims.get_all` would leave it out. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * Zim searches run in the libzim worker process too The /api/zim/search endpoints had the same stall as the estimate: they opened every Zim through Zims.get_all and searched them with libzim, which holds the GIL, so a thread offload still froze the Sanic worker. The estimate script is now a general libzim worker with estimate, search and entries jobs. A search of all Zims sends every Zim's job to one worker process; the API worker only headlines the returned HTML and looks up tags. The worker also returns each Zim's metadata so Zim.__json__ does not open the Zim again on the event loop. Zim.entries_with_tags is split so the tagged paths come from the database without opening the Zim. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * Tagged Zim estimates count from the database, and a cancelled request kills the libzim worker Review of the earlier commits found two gaps. Estimates with tags selected still went through Zims.entries_with_tags, which opened every Zim in the API process, and after the thread offload was removed it did so on the event loop; they now count the tagged paths in SQL and ask the worker only for metadata. And Sanic cancels a handler at its response timeout or on disconnect, which left the worker process running until its stdout pipe filled; the runner now kills and reaps it on cancellation. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
9c258b41ee
commit
bfe7b8afbd
6 changed files with 376 additions and 154 deletions
|
|
@ -1,4 +1,3 @@
|
|||
import asyncio
|
||||
import urllib.parse
|
||||
from http import HTTPStatus
|
||||
|
||||
|
|
@ -12,7 +11,6 @@ from wrolpi.common import logger
|
|||
from wrolpi.downloader import download_manager
|
||||
from wrolpi.events import Events
|
||||
from . import lib, schema
|
||||
from .models import Zims
|
||||
|
||||
zim_bp = Blueprint('Zim', '/api/zim')
|
||||
|
||||
|
|
@ -64,13 +62,8 @@ async def post_set_zim_auto_search(request: Request, zim_id: int, body: schema.Z
|
|||
)
|
||||
@validate(schema.ZimSearchRequest)
|
||||
async def search_all_zims(request: Request, body: schema.ZimSearchRequest):
|
||||
# libzim searches are synchronous and slow; run off the event loop. The request's Session is used
|
||||
# in the worker thread; the handler awaits the thread, so the Session has one user at a time.
|
||||
def search():
|
||||
return lib.search_all_zims(request.ctx.session, body.search_str, tag_names=body.tag_names,
|
||||
offset=body.offset, limit=body.limit)
|
||||
|
||||
results = await asyncio.to_thread(search)
|
||||
results = await lib.search_all_zims(request.ctx.session, body.search_str, tag_names=body.tag_names,
|
||||
offset=body.offset, limit=body.limit)
|
||||
return json_response({'zims': results})
|
||||
|
||||
|
||||
|
|
@ -82,11 +75,8 @@ async def search_all_zims(request: Request, body: schema.ZimSearchRequest):
|
|||
)
|
||||
@validate(schema.ZimSearchRequest)
|
||||
async def search_zim(request: Request, zim_id: int, body: schema.ZimSearchRequest):
|
||||
def search():
|
||||
return lib.headline_zim(request.ctx.session, body.search_str, zim_id, tag_names=body.tag_names,
|
||||
offset=body.offset, limit=body.limit)
|
||||
|
||||
headlines = await asyncio.to_thread(search)
|
||||
headlines = await lib.headline_zim(request.ctx.session, body.search_str, zim_id, tag_names=body.tag_names,
|
||||
offset=body.offset, limit=body.limit)
|
||||
return json_response({'zim': headlines})
|
||||
|
||||
|
||||
|
|
@ -210,17 +200,14 @@ async def post_search_estimates(request: Request, body: schema.SearchEstimateReq
|
|||
if not body.search_str and not body.tag_names:
|
||||
return response.empty(HTTPStatus.BAD_REQUEST)
|
||||
|
||||
def estimate():
|
||||
session = request.ctx.session
|
||||
if body.tag_names:
|
||||
# Get actual count of entries tagged with the tag names.
|
||||
counts = Zims.entries_with_tags(session, body.tag_names)
|
||||
else:
|
||||
# Get estimates using libzim.
|
||||
counts = Zims.estimate(session, body.search_str)
|
||||
return [dict(estimate=count, **zim.__json__()) for zim, count in counts.items()]
|
||||
|
||||
zims_estimates = await asyncio.to_thread(estimate)
|
||||
session = request.ctx.session
|
||||
if body.tag_names:
|
||||
# Count the entries tagged with the tag names (from the database; see `lib.count_tagged_zim_entries`).
|
||||
counts = await lib.count_tagged_zim_entries(session, body.tag_names)
|
||||
else:
|
||||
# Get estimates using libzim, in the estimate process (see `lib.estimate_zims`).
|
||||
counts = await lib.estimate_zims(session, body.search_str)
|
||||
zims_estimates = [dict(estimate=count, **zim.__json__()) for zim, count in counts.items()]
|
||||
|
||||
ret = dict(
|
||||
zims_estimates=zims_estimates
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue