Skip to content

Optimize database operations with batch processing and transaction refactoring#127

Open
cgalibern wants to merge 27 commits intoopensvc:mainfrom
cgalibern:dev
Open

Optimize database operations with batch processing and transaction refactoring#127
cgalibern wants to merge 27 commits intoopensvc:mainfrom
cgalibern:dev

Conversation

@cgalibern
Copy link
Contributor

Summary

This pull request introduces multiple optimizations and refactorings to improve database operation efficiency and reliability:

  • Standardized database execution by replacing oDb.DB.ExecContext with oDb.ExecContext throughout the codebase.
  • Simplified transaction handling in compliance and authentication handlers by removing manual transaction management.
  • Add Prometheus counters for improved db monitoring.
  • Added a transaction-aware ExecContext method with retry mechanisms for handling MySQL deadlocks efficiently.

cgalibern and others added 27 commits March 1, 2026 19:52
…dling

- Implemented `ExecContext` method in `cdb/db.go` to support transaction retries for deadlocks.
- Added Prometheus counters for monitoring transaction execution, retries, rollbacks, and errors.
- Introduced logic to identify and handle MySQL deadlocks using error codes.
…theus counters

- Migrated from `PrepareDB` to `ODBSetter` for cleaner API design.
- Updated `cdb.New` to accept `subsystem` for initializing Prometheus counters.
- Refactored usage of `ODB` across worker and runner modules to leverage the new changes.
- Improved monitoring capabilities through newly introduced counters in database operations.
- Removed manual transaction management (e.g., `Commit`, `Rollback`, `markSuccess` logic) in favor of cleaner and simplified control flow.
…codebase

- Simplified database operations by consistently using `oDb.ExecContext` in place of `oDb.DB.ExecContext`.
…th batch processing

- Refactored `ObjectPing` to `ObjectsPing` for efficient batch updates using multiple IDs.
- Updated worker methods (`dbUpdateServices`, `dbPingObjects`) to group and pass object IDs for bulk operations.
- Improved database query efficiency by reducing the number of individual calls.
- Introduced `sync.RWMutex` to `Session` to ensure thread-safe access to `tables`.
- Updated `SetChanges` and `listChanges` methods with appropriate locking mechanisms (`Lock`, `RLock`).
- Moved `ExecTxDeadlock.Inc` to the correct position within the deadlock-handling logic.
- Corrected `err` to `err1` in `Rollback` error checking, ensuring proper error propagation.
- Introduced `Backoff` function for calculating retry delays with capped exponential backoff and jitter.
- Increased `maxRetries` from 3 to 9 for improved fault tolerance during transaction execution.
- Updated retry logic to use `Backoff` instead of fixed delay, enhancing robustness against transient issues.
- Added elapsed time tracking in the retry process for better error reporting.
… `updated` column

The `services.updated` column is datetime NOT NULL

- Introduced `Updated` field in `DBObject` to capture the last update timestamp.
- Modified relevant SQL queries to fetch the `updated` column for `services` table.
… batching

- Enhanced `heartbeatToDB` logic to include batch processing for better efficiency.
- Added support for structured updates to `hbmon_log` and `hbmon_log_last` tables.
- Refactored SQL queries in `HBUpdate`, `HBLogUpdate`, and related methods to optimize database operations.
- Improved handling of `hbmon_log_last` state transitions and interval extensions.
- Added the `blocking: true` flag to several operations in `Operations()` to indicate they are blocking.
- Improved clarity of operation behavior in the `jobFeedDaemonStatus` workflow.
…pdates

- Introduced `ObjectStatusLogLastUpdate` for batched insert or update of the latest object status logs.
- Added `ObjectStatusLogLastExtend` to update `svc_end` timestamps for specified object IDs.
- Implemented `ObjectStatusLogUpdate` for bulk insertion of object status logs into `services_log`.
- Optimized database operations with prepared statements and dynamic placeholders.
…base queries

- Introduced `DashboardObjectWithTypeDelete` for bulk deletion using dynamic placeholders.
- Refactored `ObjectInAckUnavailabilityPeriod` to handle multiple object IDs and return matching ones.
- Updated `DashboardUpdateObject` for batched insert and update operations with improved efficiency.
…h processing

- Replaced `ObjectUpdateStatus` with `ObjectStatusUpdate` for bulk insert and update operations.
- Improved query efficiency with dynamic placeholders and batched arguments handling.
oDb.DB.ExecContext(ctx, updateSvcLogLastSvc
->
oDb.execCountContext
… name "updated": destination not a pointer

Fix: failure dbCreateServices objectCreate %s: sql: Scan error on column index 8, name "updated": destination not a pointer
…anagement

- Introduced `ODB` initialization in `server` and passed it to API handlers.
- Deprecated redundant `cdbSession` method in favor of using `ODB` directly.
…roper preservation

Enhanced Resmon log processing by incorporating the `changed` field, ensuring its preservation when matching `resmon_log_last` values, and only refreshing `updated` during analysis.
- Replaced repetitive single-row operations with batched inserts and updates.
- Improved query construction efficiency with dynamic placeholders and argument batching.

Signed-off-by: Cyril Galibern <cyril.galibern@opensvc.com>
…Session`

Signed-off-by: Cyril Galibern <cyril.galibern@opensvc.com>
Signed-off-by: Cyril Galibern <cyril.galibern@opensvc.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants