Skip to content
Closed
51 changes: 51 additions & 0 deletions .monkeycode/specs/db-batch-optimization/tasklist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 数据库批量操作优化实施计划

- [x] 1. 实现 collectCurrentRuntimeStateSnapshot() 分页加载优化
- [x] 1.1 创建分页查询工具函数
- 在 backupService.ts 中实现 batchQueryAll(db, table, batchSize) 工具函数
- 每次查询最多返回 batchSize 条记录,支持游标分页
- [x] 1.2 重构 proxyLogs 分页加载
- 将 `db.select().from(schema.proxyLogs).all()` 改为分页加载
- 按 id 升序分页,每次最多 10 条,循环直到加载完全部数据
- 使用累积变量收集所有 proxyLogs 数据
- [x] 1.3 重构其他大表分页加载
- 对 checkinLogs 表实现相同分页策略

- [x] 2. 实现 importAccountsSection() 分批插入优化
- [x] 2.1 创建分批插入工具函数
- 实现 batchInsertHelper(tx, table, records, batchSize) 工具函数
- 每批最多插入 10 条记录,循环直到完成
- [x] 2.2 重构 sites 表分批插入
- 将 for 循环插入改为 batchInsertHelper 分批执行
- [x] 2.3 重构 siteApiEndpoints 表分批插入
- 将 for 循环插入改为 batchInsertHelper 分批执行
- [x] 2.4 重构 accounts 表分批插入
- 将 for 循环插入改为 batchInsertHelper 分批执行
- [x] 2.5 重构 accountTokens 表分批插入
- 将 for 循环插入改为 batchInsertHelper 分批执行
- [x] 2.6 重构 tokenRoutes 表分批插入
- 将 for 循环插入改为 batchInsertHelper 分批执行
- [x] 2.7 重构 routeChannels 表分批插入
- 将 for 循环插入改为 batchInsertHelper 分批执行
- [x] 2.8 重构 routeGroupSources 表分批插入
- 将 for 循环插入改为 batchInsertHelper 分批执行
- [x] 2.9 重构 modelAvailability 表分批插入
- 将 for 循环插入改为 batchInsertHelper 分批执行
- [x] 2.10 重构 tokenModelAvailability 表分批插入
- 将 for 循环插入改为 batchInsertHelper 分批执行
- [x] 2.11 重构 siteDisabledModels 表分批插入
- 将 for 循环插入改为 batchInsertHelper 分批执行
- [x] 2.12 重构 siteAnnouncements 表分批插入
- 将 for 循环改为批量收集记录后使用 batchInsertHelper 分批执行
- [x] 2.13 重构 proxyLogs 表分批插入
- 将 for 循环改为批量收集记录后使用 batchInsertHelper 分批执行
- [x] 2.14 重构 checkinLogs 表分批插入
- 将 for 循环改为批量收集记录后使用 batchInsertHelper 分批执行

- [x] 3. 检查点 - 确保代码编译通过
- 运行 TypeScript 编译检查
- 确保没有语法错误

- [x] 4. 编写单元测试
- [x] 4.1 为 batchQueryAll 编写单元测试
- [x] 4.2 为 batchInsertHelper 编写单元测试
12 changes: 12 additions & 0 deletions .trae/specs/database-timeout-optimization/checklist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Database and Gateway Timeout Optimization - Verification Checklist

- [x] Database connection timeout configuration is set to at least 300 seconds
- [x] Large data import timeout settings are appropriate for long-running operations
- [x] Gateway service timeout settings match or exceed database operation timeouts
- [x] Complex database operations complete successfully without timeout errors
- [x] No timeout errors occur during large data import operations
- [x] All timeout settings are consistent across database, application, and gateway layers
- [x] Documentation is updated with database and gateway timeout configuration best practices
- [x] Test scenarios are created for large database operations
- [x] Database connection pool settings are reviewed and adjusted if necessary
- [x] Gateway service routing settings are reviewed and adjusted if necessary
84 changes: 84 additions & 0 deletions .trae/specs/database-timeout-optimization/spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Database and Gateway Timeout Optimization - Product Requirement Document

## Overview
- **Summary**: This project aims to optimize timeout settings for database connections, large data imports, and gateway services to address timeout issues during database operations and large data processing.
- **Purpose**: To resolve timeout problems that occur during database operations, large data imports, and gateway service interactions by ensuring appropriate timeout settings across all relevant components.
- **Target Users**: System administrators, developers, and end users who interact with the application, especially those performing database operations and large data imports.

## Goals
- Optimize database connection timeout settings to handle long-running database operations
- Configure timeout settings for large data import operations
- Review and adjust gateway service timeout settings
- Ensure consistency in timeout settings across database, application, and gateway layers
- Provide documentation for timeout configuration best practices

## Non-Goals (Out of Scope)
- Modifying database schema or structure
- Adding new database features or capabilities
- Changing the core architecture of the application
- Optimizing database query performance (this is about timeout settings, not query optimization)

## Background & Context
- The application experiences timeout issues during database operations and large data imports
- Database connection timeouts may occur during complex queries or large transactions
- Large data imports can exceed default timeout settings
- Gateway services may have timeout settings that don't align with database operation requirements
- The backup import operation has been optimized with batch processing, but still requires sufficient timeout settings

## Functional Requirements
- **FR-1**: Update database connection timeout settings to handle long-running database operations
- **FR-2**: Configure timeout settings for large data import operations
- **FR-3**: Review and adjust gateway service timeout settings
- **FR-4**: Test timeout settings with large database operations

## Non-Functional Requirements
- **NFR-1**: All timeout settings should be consistent across database, application, and gateway layers
- **NFR-2**: Timeout settings should be configurable through environment variables where possible
- **NFR-3**: Documentation should be updated to include timeout configuration best practices
- **NFR-4**: Performance should not be negatively impacted by increased timeout settings

## Constraints
- **Technical**: Must maintain compatibility with existing database systems and gateway services
- **Business**: Changes should not introduce security vulnerabilities
- **Dependencies**: Must work with existing database and gateway configurations

## Assumptions
- The application uses a relational database (SQLite, MySQL, or PostgreSQL)
- Large data imports involve importing significant amounts of data that may take longer than 30 seconds
- Gateway services are used to handle external requests to the application

## Acceptance Criteria

### AC-1: Database connection timeout configuration
- **Given**: The database connection is configured with appropriate timeout settings
- **When**: A long-running database operation is initiated
- **Then**: The operation completes successfully without database connection timeout errors
- **Verification**: `programmatic`
- **Notes**: Database connection timeout should be set to at least 300 seconds

### AC-2: Large data import timeout configuration
- **Given**: Large data import operations are configured with appropriate timeout settings
- **When**: A large data import is initiated
- **Then**: The import completes successfully without timeout errors
- **Verification**: `programmatic`
- **Notes**: Import timeout should be set to handle datasets that take several minutes to process

### AC-3: Gateway service timeout configuration
- **Given**: Gateway service timeout settings are reviewed and adjusted
- **When**: Gateway services handle requests that require database operations
- **Then**: The requests complete successfully without gateway service timeouts
- **Verification**: `programmatic`
- **Notes**: Gateway timeout should be set to match or exceed database operation timeouts

### AC-4: Consistency across all layers
- **Given**: All timeout settings are configured consistently
- **When**: A complex operation involving database, application, and gateway layers is performed
- **Then**: The operation completes successfully without timeout errors at any layer
- **Verification**: `programmatic`
- **Notes**: Test with operations that take 60+ seconds to complete

## Open Questions
- [ ] What is the current database connection timeout configuration?
- [ ] What is the current gateway service timeout configuration?
- [ ] Are there any reverse proxy or load balancer timeout settings that need to be considered?
- [ ] What is the maximum expected duration for large data import operations?
67 changes: 67 additions & 0 deletions .trae/specs/database-timeout-optimization/tasks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Database and Gateway Timeout Optimization - Implementation Plan

## [x] Task 1: Review current database connection timeout configuration
- **Priority**: P0
- **Depends On**: None
- **Description**:
- Review the current database connection timeout settings
- Identify database connection parameters related to timeouts
- Check if there are any database-specific timeout settings that need adjustment
- **Acceptance Criteria Addressed**: AC-1
- **Test Requirements**:
- `programmatic` TR-1.1: Verify database connection timeout is set to at least 300 seconds
- `programmatic` TR-1.2: Test that long-running database operations complete without connection timeouts
- **Notes**: Check database configuration files and connection pool settings

## [x] Task 2: Configure timeout settings for large data import operations
- **Priority**: P0
- **Depends On**: Task 1
- **Description**:
- Review current timeout settings for data import operations
- Adjust timeout settings for large data imports
- Ensure import operations have sufficient time to complete
- **Acceptance Criteria Addressed**: AC-2
- **Test Requirements**:
- `programmatic` TR-2.1: Verify large data import timeout settings are appropriate
- `programmatic` TR-2.2: Test large data import with dataset that takes 60+ seconds
- **Notes**: Focus on backup import and other data import operations

## [x] Task 3: Review and adjust gateway service timeout settings
- **Priority**: P0
- **Depends On**: Task 1
- **Description**:
- Review current gateway service timeout settings
- Adjust gateway service timeouts to match or exceed database operation timeouts
- Ensure gateway services can handle requests that require long-running database operations
- **Acceptance Criteria Addressed**: AC-3
- **Test Requirements**:
- `programmatic` TR-3.1: Verify gateway service timeout settings are appropriate
- `programmatic` TR-3.2: Test gateway service with requests that require long-running database operations
- **Notes**: Check gateway configuration files and routing settings

## [x] Task 4: Test timeout settings with large database operations
- **Priority**: P1
- **Depends On**: Tasks 1, 2, 3
- **Description**:
- Create test scenarios for large database operations
- Test database operations that take 60+ seconds to complete
- Verify that operations complete successfully without timeout errors
- **Acceptance Criteria Addressed**: AC-4
- **Test Requirements**:
- `programmatic` TR-4.1: Test complex database operations with large datasets
- `programmatic` TR-4.2: Verify no timeout errors occur at any layer
- **Notes**: Use realistic test data to simulate actual usage

## [x] Task 5: Update documentation for timeout configuration
- **Priority**: P2
- **Depends On**: Tasks 1, 2, 3, 4
- **Description**:
- Update project documentation to include database and gateway timeout configuration best practices
- Document database connection timeout settings
- Document gateway service timeout settings
- Provide guidance for configuring timeouts in different environments
- **Acceptance Criteria Addressed**: NFR-3
- **Test Requirements**:
- `human-judgment` TR-5.1: Documentation is clear and comprehensive
- `human-judgment` TR-5.2: Documentation covers all timeout configuration levels
- **Notes**: Update README or deployment documentation
12 changes: 12 additions & 0 deletions .trae/specs/timeout-optimization/checklist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Timeout Optimization - Verification Checklist

- [x] Server timeout configuration is set to at least 300 seconds
- [x] Docker container configuration has appropriate timeout settings
- [x] Service-level timeout settings are adjusted for critical operations
- [x] Backup import operation completes successfully with large datasets
- [x] No timeout errors occur during long-running operations
- [x] All timeout settings are consistent across server, Docker, and service levels
- [x] Documentation is updated with timeout configuration best practices
- [x] Test scenarios are created for long-running operations
- [x] Docker HEALTHCHECK settings are reviewed and adjusted if necessary
- [x] Service-level timeouts match or exceed server-level settings
81 changes: 81 additions & 0 deletions .trae/specs/timeout-optimization/spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Timeout Optimization - Product Requirement Document

## Overview
- **Summary**: This project aims to optimize timeout settings across server configuration, Docker configuration, and service configuration to address 30-second timeout issues.
- **Purpose**: To resolve timeout problems that occur during long-running operations, such as backup import, by ensuring appropriate timeout settings at all levels of the application stack.
- **Target Users**: System administrators, developers, and end users who interact with the application, especially those performing operations that require longer processing times.

## Goals
- Optimize server-side timeout settings to handle long-running operations
- Configure Docker container timeout settings to match application requirements
- Review and adjust service-level timeout settings for critical operations
- Ensure consistency in timeout settings across all layers of the application
- Provide documentation for timeout configuration best practices

## Non-Goals (Out of Scope)
- Modifying application logic to reduce processing time (this is about configuring timeouts, not optimizing performance)
- Adding new features unrelated to timeout settings
- Changing the core architecture of the application

## Background & Context
- The application currently experiences 30-second timeout issues during operations like backup import
- Server-side timeout is already set to 300 seconds, but other layers may have shorter timeouts
- Docker containers and service-level configurations may have default 30-second timeouts
- The backup import operation has been optimized with batch processing, but still requires sufficient timeout settings

## Functional Requirements
- **FR-1**: Update server-side timeout configuration to ensure consistent timeout settings
- **FR-2**: Configure Docker container timeout settings to match application requirements
- **FR-3**: Review and adjust service-level timeout settings for critical operations
- **FR-4**: Test timeout settings with long-running operations

## Non-Functional Requirements
- **NFR-1**: All timeout settings should be consistent across server, Docker, and service levels
- **NFR-2**: Timeout settings should be configurable through environment variables where possible
- **NFR-3**: Documentation should be updated to include timeout configuration best practices
- **NFR-4**: Performance should not be negatively impacted by increased timeout settings

## Constraints
- **Technical**: Must maintain compatibility with existing application architecture
- **Business**: Changes should not introduce security vulnerabilities
- **Dependencies**: Must work with existing Docker and server configurations

## Assumptions
- The application is running in a Docker container
- The server is using Fastify as the web framework
- Long-running operations like backup import require more than 30 seconds to complete

## Acceptance Criteria

### AC-1: Server-side timeout configuration
- **Given**: The server is configured with appropriate timeout settings
- **When**: A long-running operation (like backup import) is initiated
- **Then**: The operation completes successfully without timeout errors
- **Verification**: `programmatic`
- **Notes**: Server timeout should be set to at least 300 seconds

### AC-2: Docker container timeout configuration
- **Given**: Docker container is configured with appropriate timeout settings
- **When**: The application runs inside the Docker container
- **Then**: Long-running operations complete successfully without container-level timeouts
- **Verification**: `programmatic`
- **Notes**: Docker container should not have timeout limits that override application settings

### AC-3: Service-level timeout configuration
- **Given**: Service-level timeout settings are reviewed and adjusted
- **When**: Services perform long-running operations
- **Then**: Operations complete successfully without service-level timeouts
- **Verification**: `programmatic`
- **Notes**: Focus on critical services like backup service

### AC-4: Consistency across all layers
- **Given**: All timeout settings are configured consistently
- **When**: A long-running operation is performed
- **Then**: The operation completes successfully without timeout errors at any layer
- **Verification**: `programmatic`
- **Notes**: Test with operations that take 60+ seconds to complete

## Open Questions
- [ ] What is the current Docker container configuration?
- [ ] Are there any reverse proxy or load balancer timeout settings that need to be considered?
- [ ] What are the specific service-level timeout settings that need to be adjusted?
Loading
Loading