63fbbe1225
Major Changes: - Pipeline Queue integration completed (internal.file + pipeline.commands) - Fixed old job_worker database compatibility with new command_queue table - Compiled Swift OCR binary (was missing, causing empty OCR results) - Added deep scan functionality through re-scan API Fixes: - Changed CommandJob.id from i32 to i64 for database compatibility - Added UUID generation in create_command (message_id field) - Fixed claim_pending_commands to query queue_name = 'internal.file' - Fixed field mappings: command_id → message_id, job_type → command_type - Added support for 'processing' status in CommandJobStatus Queue System: - internal.file queue: for register/unregister/re-scan commands (job_worker) - pipeline.commands queue: for vectorize/store_asrx/etc commands (pipeline_worker) Documentation: - Added comprehensive queue system documentation - Created M4 workspace status reports - Updated AGENTS.md with queue system details
248 lines
8.3 KiB
PL/PgSQL
248 lines
8.3 KiB
PL/PgSQL
-- ============================================
|
|
-- VLM Request Queue Schema (Kafka-inspired)
|
|
-- ============================================
|
|
-- Date: 2026-10-11
|
|
-- Purpose: Implement VLM request queue with priority scheduling,
|
|
-- dead letter queue, and consumer offset tracking
|
|
-- Design: Based on Kafka concepts (append-only log, offset tracking,
|
|
-- partition by request_type, priority queue)
|
|
-- ============================================
|
|
|
|
-- 1. 消息表
|
|
CREATE TABLE vlm_requests (
|
|
-- Offset (单调递增)
|
|
id BIGSERIAL PRIMARY KEY,
|
|
|
|
-- 分区键
|
|
request_type VARCHAR(20) NOT NULL CHECK (
|
|
request_type IN ('agent_vlm', 'scene_vlm', 'face_vlm')
|
|
),
|
|
|
|
-- 优先级 (0 = 最高)
|
|
priority INTEGER DEFAULT 1 CHECK (priority >= 0 AND priority <= 10),
|
|
|
|
-- 状态
|
|
status VARCHAR(20) NOT NULL DEFAULT 'pending' CHECK (
|
|
status IN ('pending', 'processing', 'completed', 'failed', 'timeout')
|
|
),
|
|
|
|
-- 消息内容
|
|
payload JSONB NOT NULL,
|
|
/*
|
|
Example payload for Scene VLM:
|
|
{
|
|
"image_path": "/output/uuid/scenes/frame_001.jpg",
|
|
"prompt": "Describe this scene in detail...",
|
|
"max_tokens": 800,
|
|
"file_uuid": "384b0ff44aaaa1f1",
|
|
"scene_index": 5,
|
|
"language": "en"
|
|
}
|
|
|
|
Example payload for Agent VLM:
|
|
{
|
|
"image_path": "/output/uuid/frame.jpg",
|
|
"prompt": "Analyze this frame...",
|
|
"max_tokens": 300,
|
|
"context": "User asked: What is in this scene?"
|
|
}
|
|
*/
|
|
|
|
-- 结果
|
|
result TEXT,
|
|
error TEXT,
|
|
|
|
-- 处理信息
|
|
processing_time_ms BIGINT,
|
|
retry_count INTEGER DEFAULT 0 CHECK (retry_count >= 0),
|
|
|
|
-- 时间戳
|
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
|
started_at TIMESTAMP WITH TIME ZONE,
|
|
completed_at TIMESTAMP WITH TIME ZONE,
|
|
|
|
-- 约束
|
|
CONSTRAINT valid_processing_time CHECK (
|
|
processing_time_ms IS NULL OR processing_time_ms >= 0
|
|
)
|
|
);
|
|
|
|
-- 索引 (支持高效查询)
|
|
|
|
-- 1. Poll 索引: 用于 Worker 拉取待处理消息
|
|
-- 按优先级和创建时间排序,只索引 pending 状态
|
|
CREATE INDEX idx_vlm_requests_poll ON vlm_requests (
|
|
request_type, priority, created_at
|
|
) WHERE status = 'pending';
|
|
|
|
-- 2. 状态索引: 用于监控和查询
|
|
CREATE INDEX idx_vlm_requests_status ON vlm_requests (status, created_at);
|
|
|
|
-- 3. 文件索引: 用于按文件查询请求
|
|
CREATE INDEX idx_vlm_requests_file ON vlm_requests (
|
|
(payload->>'file_uuid')
|
|
);
|
|
|
|
-- 4. 场景索引: 用于 Scene VLM 按场景查询
|
|
CREATE INDEX idx_vlm_requests_scene ON vlm_requests (
|
|
(payload->>'file_uuid'),
|
|
(payload->>'scene_index')
|
|
) WHERE request_type = 'scene_vlm';
|
|
|
|
-- 注释
|
|
COMMENT ON TABLE vlm_requests IS 'VLM request message log (Kafka-inspired design)';
|
|
COMMENT ON COLUMN vlm_requests.id IS 'Offset (auto-increment, used for tracking)';
|
|
COMMENT ON COLUMN vlm_requests.request_type IS 'Partition key (agent_vlm, scene_vlm, face_vlm)';
|
|
COMMENT ON COLUMN vlm_requests.priority IS 'Priority (0 = highest, used for priority scheduling)';
|
|
COMMENT ON COLUMN vlm_requests.status IS 'Message status (pending, processing, completed, failed, timeout)';
|
|
COMMENT ON COLUMN vlm_requests.payload IS 'Request payload (JSON format)';
|
|
COMMENT ON COLUMN vlm_requests.retry_count IS 'Retry count for failed requests';
|
|
|
|
-- 2. 死信队列
|
|
CREATE TABLE vlm_dlq (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
|
|
-- 原始消息信息
|
|
original_id BIGINT NOT NULL,
|
|
original_request_type VARCHAR(20) NOT NULL,
|
|
original_priority INTEGER NOT NULL,
|
|
original_payload JSONB NOT NULL,
|
|
|
|
-- 错误信息
|
|
error TEXT NOT NULL,
|
|
retry_count INTEGER NOT NULL,
|
|
|
|
-- 时间戳
|
|
failed_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
|
|
|
-- 外键 (如果原消息被删除,DLQ 记录也删除)
|
|
FOREIGN KEY (original_id) REFERENCES vlm_requests(id) ON DELETE CASCADE
|
|
);
|
|
|
|
-- 索引
|
|
CREATE INDEX idx_vlm_dlq_failed_at ON vlm_dlq (failed_at);
|
|
CREATE INDEX idx_vlm_dlq_original_id ON vlm_dlq (original_id);
|
|
|
|
COMMENT ON TABLE vlm_dlq IS 'Dead letter queue for failed VLM requests';
|
|
COMMENT ON COLUMN vlm_dlq.original_id IS 'Original request ID (offset)';
|
|
COMMENT ON COLUMN vlm_dlq.error IS 'Error message';
|
|
COMMENT ON COLUMN vlm_dlq.retry_count IS 'Number of retries before failure';
|
|
|
|
-- 3. Consumer Offset 追踪 (类似 Kafka __consumer_offsets)
|
|
CREATE TABLE vlm_consumer_offsets (
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
-- Consumer ID (worker_0, worker_1, worker_2)
|
|
consumer_id VARCHAR(50) NOT NULL UNIQUE,
|
|
|
|
-- 当前 Offset
|
|
current_offset BIGINT NOT NULL DEFAULT 0,
|
|
|
|
-- 统计信息
|
|
total_processed BIGINT DEFAULT 0,
|
|
total_succeeded BIGINT DEFAULT 0,
|
|
total_failed BIGINT DEFAULT 0,
|
|
|
|
-- 时间戳
|
|
last_poll_at TIMESTAMP WITH TIME ZONE,
|
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
|
);
|
|
|
|
-- 初始化 Workers
|
|
INSERT INTO vlm_consumer_offsets (consumer_id) VALUES
|
|
('worker_0'), -- Agent VLM (P0, highest priority)
|
|
('worker_1'), -- Scene/Face VLM (P1)
|
|
('worker_2'); -- Scene/Face VLM (P1)
|
|
|
|
COMMENT ON TABLE vlm_consumer_offsets IS 'Consumer offset tracking (similar to Kafka __consumer_offsets)';
|
|
COMMENT ON COLUMN vlm_consumer_offsets.consumer_id IS 'Worker ID (worker_0, worker_1, worker_2)';
|
|
COMMENT ON COLUMN vlm_consumer_offsets.current_offset IS 'Current processing offset';
|
|
COMMENT ON COLUMN vlm_consumer_offsets.total_processed IS 'Total processed requests';
|
|
COMMENT ON COLUMN vlm_consumer_offsets.total_succeeded IS 'Successfully processed requests';
|
|
COMMENT ON COLUMN vlm_consumer_offsets.total_failed IS 'Failed requests';
|
|
|
|
-- 4. 监控视图
|
|
CREATE VIEW vlm_queue_stats AS
|
|
SELECT
|
|
request_type,
|
|
status,
|
|
COUNT(*) as count,
|
|
AVG(processing_time_ms) as avg_processing_time_ms,
|
|
MIN(processing_time_ms) as min_processing_time_ms,
|
|
MAX(processing_time_ms) as max_processing_time_ms
|
|
FROM vlm_requests
|
|
WHERE status IN ('pending', 'processing', 'completed')
|
|
GROUP BY request_type, status;
|
|
|
|
COMMENT ON VIEW vlm_queue_stats IS 'Queue statistics for monitoring';
|
|
|
|
-- 5. 辅助函数: 获取下一个 offset
|
|
CREATE OR REPLACE FUNCTION get_next_vlm_offset()
|
|
RETURNS BIGINT AS $$
|
|
BEGIN
|
|
RETURN nextval('vlm_requests_id_seq');
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
COMMENT ON FUNCTION get_next_vlm_offset() IS 'Get next VLM request offset (for pre-allocation)';
|
|
|
|
-- 6. 触发器: 自动更新 updated_at
|
|
CREATE OR REPLACE FUNCTION update_vlm_consumer_offsets_updated_at()
|
|
RETURNS TRIGGER AS $$
|
|
BEGIN
|
|
NEW.updated_at = NOW();
|
|
RETURN NEW;
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
CREATE TRIGGER trigger_update_vlm_consumer_offsets_updated_at
|
|
BEFORE UPDATE ON vlm_consumer_offsets
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION update_vlm_consumer_offsets_updated_at();
|
|
|
|
-- 7. 分区表 (可选,未来性能优化)
|
|
-- 如果请求量很大,可以按 request_type 分区
|
|
-- 目前先不分区,等数据量上来后再优化
|
|
|
|
-- ============================================
|
|
-- 验证 Schema
|
|
-- ============================================
|
|
|
|
-- 验证表创建
|
|
DO $$
|
|
BEGIN
|
|
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'vlm_requests') THEN
|
|
RAISE EXCEPTION 'Table vlm_requests not created';
|
|
END IF;
|
|
|
|
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'vlm_dlq') THEN
|
|
RAISE EXCEPTION 'Table vlm_dlq not created';
|
|
END IF;
|
|
|
|
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'vlm_consumer_offsets') THEN
|
|
RAISE EXCEPTION 'Table vlm_consumer_offsets not created';
|
|
END IF;
|
|
|
|
RAISE NOTICE 'VLM queue schema created successfully';
|
|
END
|
|
$$;
|
|
|
|
-- ============================================
|
|
-- 示例数据 (测试用)
|
|
-- ============================================
|
|
|
|
-- 插入测试消息
|
|
INSERT INTO vlm_requests (request_type, priority, payload) VALUES
|
|
('agent_vlm', 0, '{"image_path": "/test/frame.jpg", "prompt": "Test", "max_tokens": 300}'),
|
|
('scene_vlm', 1, '{"image_path": "/test/scene.jpg", "prompt": "Test scene", "max_tokens": 800, "file_uuid": "test-uuid", "scene_index": 0}');
|
|
|
|
-- 清理测试数据
|
|
DELETE FROM vlm_requests WHERE id <= 2;
|
|
|
|
-- ============================================
|
|
-- Migration 完成
|
|
-- ============================================
|
|
-- VLM Request Queue Schema 创建完成
|
|
-- 支持优先级调度、死信队列、Offset 追踪
|
|
-- 基于 Kafka 设计理念,适应单机 PostgreSQL 部署
|
|
-- 未来可扩展到分布式环境 |