Commit graph

14 commits

Author SHA1 Message Date
undaunt
2dc3e1ab1f Fix concurrent stat insertion duplicate key errors
This commit resolves duplicate key errors that occur when multiple monitors
attempt to insert statistics simultaneously into stat_* tables.

The fix addresses three interconnected issues:

1. **Circular Dependency Resolution**: database.js imports UptimeCalculator at
   module level, but UptimeCalculator needs Database.dbConfig. Fixed by using
   local imports in UptimeCalculator methods to ensure Database.dbConfig is
   properly initialized when accessed.

2. **Database Configuration Initialization**: Database.dbConfig was not set
   in the catch block when db-config.json is missing, causing undefined access
   errors. Fixed by ensuring Database.dbConfig is always set.

3. **Schema Column Naming Mismatch**: RedBean ORM uses camelCase (pingMin/pingMax)
   but Knex migrations create snake_case columns (ping_min/ping_max). Fixed by
   using correct snake_case column names in SQL queries.

4. **Atomic Upsert Operations**: Implemented database-specific upsert logic:
   - SQLite: INSERT ... ON CONFLICT DO UPDATE
   - MariaDB: INSERT ... ON DUPLICATE KEY UPDATE

The solution maintains backward compatibility by falling back to R.store()
when upsert fails, ensuring no data loss while eliminating race conditions
for users with many monitors (200+).

Fixes #5357
2025-06-17 22:41:41 -07:00
undaunt
19889a57b2 Fix concurrent stat insertions causing duplicate key errors
Resolves issue where multiple monitors updating statistics simultaneously
can cause "Duplicate entry" database errors for the same monitor_id and
timestamp combination in stat_hourly and stat_daily tables.

Changes:
- Add database-specific upsert logic for SQLite and MariaDB
- Replace R.store() calls with atomic upsert operations
- Add fallback to original R.store() if upsert fails
- Initialize default values for new stat beans to prevent null conflicts
- Use ON CONFLICT/ON DUPLICATE KEY UPDATE for atomic stat updates

This fix is particularly important for high-volume monitoring scenarios
with 400+ monitors where concurrent heartbeats can trigger race conditions
in the stat insertion process.

Fixes #5357
2025-06-17 21:27:17 -07:00
Louis Lam
4d779cfc69
Data migration and history retention for 2.0.0 (#5075) 2024-10-26 20:50:29 +08:00
Qi Zhao
4941b17a46 perf: ️ revert some overengineer code 2024-06-20 10:50:35 +08:00
Qi Zhao
53c4bba387 fix: 🐛 badge requestedDuration 2024-06-20 10:50:35 +08:00
Qi Zhao
e5ff86e6ac fix: 🐛 status & ping badge duration 2024-06-20 10:50:35 +08:00
Nelson Chan
a581a85633
Feat: Use UptimeCalculator for PingChart (#4264)
Co-authored-by: Frank Elsinga <frank@elsinga.de>
2024-05-19 22:03:32 +02:00
CoolCu
b25ac55a2f
chore: fix some typos in comments (#4679)
Signed-off-by: CoolCu <coolcui@qq.com>
2024-04-16 16:59:07 +02:00
Nelson Chan
b8858f4605
Feat: Handle maintenance in UptimeCalculator (#4406)
Co-authored-by: Frank Elsinga <frank@elsinga.de>
2024-03-27 10:19:25 +08:00
Nelson Chan
bf1e3a3d5e
Feat: Add stat_hourly & min. max. ping (#4267)
Co-authored-by: Frank Elsinga <frank@elsinga.de>
2024-01-05 20:42:24 +08:00
Frank Elsinga
d6302198f3
chore(jsdoc):Linting fixes (#3703)
* fixed the lockfile having a different version

* jsdoc
2023-09-07 15:42:44 +08:00
Louis Lam
ed04008569 Remove incorrect warning 2023-09-02 17:20:36 +08:00
Louis Lam
f3517bc08d Fix avg ping 2023-09-02 17:11:22 +08:00
Louis Lam
076331bf00
Uptime calculation improvement and 1-year uptime (#2750) 2023-09-01 05:19:21 +08:00