feat(docreader): speed up scanned-PDF parsing, stream image results, isolate heavy async queues

Large scanned PDFs (hundreds of pages) were slow and fragile end-to-end.
This change addresses the parse, transport, and task-scheduling layers:

docreader (parse + transport):
- Parallelize per-page scanned rendering across processes (forkserver/fork),
  with serial fallback. ~4-7x faster on large scanned PDFs; pdfium is not
  thread-safe so we fan out across processes. Configurable via
  DOCREADER_PDF_RENDER_PARALLELISM.
- Add server-streaming ReadStream RPC: emit one meta frame then one frame per
  image, so documents with many page images are no longer capped by the unary
  gRPC message-size limit (a 874-page PDF produced ~193MiB of images, far over
  the 50MB cap) and memory is bounded on both ends. Unary Read is kept for
  backward compatibility; the Go production reader switches to ReadStream.

VLM:
- Make the VLM HTTP timeout configurable (VLM_HTTP_TIMEOUT_SECONDS) and raise
  the default 90s -> 180s so dense scanned-page OCR does not time out with
  "context deadline exceeded".

Async task queues:
- Isolate high-volume, model-heavy fan-out tasks into dedicated asynq queues so
  a single large document cannot saturate the shared worker pool and block
  user-facing document parsing:
    image:multimodal  -> "multimodal"
    chunk:extract     -> "graph"
    question:generation -> "question"
- Register the new queues in the server weight map and the cancel inspector's
  scanned-queue set (so cancelling a knowledge still purges its pending tasks).
This commit is contained in:
wizardchen
2026-06-02 09:50:09 +08:00
committed by lyingbug
parent 2ba1237598
commit 7b1bb1054f
26 changed files with 1994 additions and 188 deletions
+56 -2
View File
@@ -1,7 +1,7 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.5.1
// - protoc v6.33.4
// - protoc v5.29.3
// source: docreader.proto
package proto
@@ -20,6 +20,7 @@ const _ = grpc.SupportPackageIsVersion9
const (
DocReader_Read_FullMethodName = "/docreader.DocReader/Read"
DocReader_ReadStream_FullMethodName = "/docreader.DocReader/ReadStream"
DocReader_ListEngines_FullMethodName = "/docreader.DocReader/ListEngines"
)
@@ -28,6 +29,13 @@ const (
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type DocReaderClient interface {
Read(ctx context.Context, in *ReadRequest, opts ...grpc.CallOption) (*ReadResponse, error)
// ReadStream is the streaming counterpart of Read. It first emits one
// ReadStreamResponse carrying the parse metadata (markdown / metadata /
// error), then emits one message per image. This keeps every gRPC message
// small so large scanned PDFs (hundreds of page images, far exceeding the
// unary message-size cap) can be returned without RESOURCE_EXHAUSTED and
// with bounded memory on both ends.
ReadStream(ctx context.Context, in *ReadRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[ReadStreamResponse], error)
ListEngines(ctx context.Context, in *ListEnginesRequest, opts ...grpc.CallOption) (*ListEnginesResponse, error)
}
@@ -49,6 +57,25 @@ func (c *docReaderClient) Read(ctx context.Context, in *ReadRequest, opts ...grp
return out, nil
}
func (c *docReaderClient) ReadStream(ctx context.Context, in *ReadRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[ReadStreamResponse], error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
stream, err := c.cc.NewStream(ctx, &DocReader_ServiceDesc.Streams[0], DocReader_ReadStream_FullMethodName, cOpts...)
if err != nil {
return nil, err
}
x := &grpc.GenericClientStream[ReadRequest, ReadStreamResponse]{ClientStream: stream}
if err := x.ClientStream.SendMsg(in); err != nil {
return nil, err
}
if err := x.ClientStream.CloseSend(); err != nil {
return nil, err
}
return x, nil
}
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
type DocReader_ReadStreamClient = grpc.ServerStreamingClient[ReadStreamResponse]
func (c *docReaderClient) ListEngines(ctx context.Context, in *ListEnginesRequest, opts ...grpc.CallOption) (*ListEnginesResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(ListEnginesResponse)
@@ -64,6 +91,13 @@ func (c *docReaderClient) ListEngines(ctx context.Context, in *ListEnginesReques
// for forward compatibility.
type DocReaderServer interface {
Read(context.Context, *ReadRequest) (*ReadResponse, error)
// ReadStream is the streaming counterpart of Read. It first emits one
// ReadStreamResponse carrying the parse metadata (markdown / metadata /
// error), then emits one message per image. This keeps every gRPC message
// small so large scanned PDFs (hundreds of page images, far exceeding the
// unary message-size cap) can be returned without RESOURCE_EXHAUSTED and
// with bounded memory on both ends.
ReadStream(*ReadRequest, grpc.ServerStreamingServer[ReadStreamResponse]) error
ListEngines(context.Context, *ListEnginesRequest) (*ListEnginesResponse, error)
mustEmbedUnimplementedDocReaderServer()
}
@@ -78,6 +112,9 @@ type UnimplementedDocReaderServer struct{}
func (UnimplementedDocReaderServer) Read(context.Context, *ReadRequest) (*ReadResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method Read not implemented")
}
func (UnimplementedDocReaderServer) ReadStream(*ReadRequest, grpc.ServerStreamingServer[ReadStreamResponse]) error {
return status.Errorf(codes.Unimplemented, "method ReadStream not implemented")
}
func (UnimplementedDocReaderServer) ListEngines(context.Context, *ListEnginesRequest) (*ListEnginesResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method ListEngines not implemented")
}
@@ -120,6 +157,17 @@ func _DocReader_Read_Handler(srv interface{}, ctx context.Context, dec func(inte
return interceptor(ctx, in, info, handler)
}
func _DocReader_ReadStream_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(ReadRequest)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(DocReaderServer).ReadStream(m, &grpc.GenericServerStream[ReadRequest, ReadStreamResponse]{ServerStream: stream})
}
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
type DocReader_ReadStreamServer = grpc.ServerStreamingServer[ReadStreamResponse]
func _DocReader_ListEngines_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(ListEnginesRequest)
if err := dec(in); err != nil {
@@ -154,6 +202,12 @@ var DocReader_ServiceDesc = grpc.ServiceDesc{
Handler: _DocReader_ListEngines_Handler,
},
},
Streams: []grpc.StreamDesc{},
Streams: []grpc.StreamDesc{
{
StreamName: "ReadStream",
Handler: _DocReader_ReadStream_Handler,
ServerStreams: true,
},
},
Metadata: "docreader.proto",
}