优化 local 模式兼容性与部署可靠性
- SQL 方言层:agent updated_at、dashboard 小时分桶、清理任务跨 SQLite/MySQL - Cache Capabilities:redka 跳过 ZSet/PubSub,Agent 聚合改 DB 直读 - local 模式 redka 初始化失败 Fatal;/version 返回 features - SQLite WAL + 连接池限制;K8s data 改 PVC 持久化 - run.sh:旧参数迁移、升级后 /version 校验
This commit is contained in:
1 parent
1f4f63cf6e
commit
b7166acda4
13 files changed
+374
-152
No files matched your search
@@ -2,6 +2,7 @@ package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ezops/config"
|
||||
@@ -28,7 +29,18 @@ func New(cfg config.DatabaseConfig) (*gorm.DB, error) {
|
||||
cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName)
|
||||
dialector = postgres.Open(dsn)
|
||||
case "sqlite":
|
||||
dialector = sqlite.Open(cfg.DBName)
|
||||
dsn := cfg.DBName
|
||||
if !strings.Contains(dsn, "?") {
|
||||
// WAL 提升并发读,busy_timeout 降低锁冲突 // Logan.Li
|
||||
dsn += "?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)"
|
||||
}
|
||||
dialector = sqlite.Open(dsn)
|
||||
if cfg.MaxConn > 4 {
|
||||
cfg.MaxConn = 4
|
||||
}
|
||||
if cfg.MaxIdle > 2 {
|
||||
cfg.MaxIdle = 2
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported database driver: %s", cfg.Driver)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// DriverName 从 GORM 连接获取驱动名(sqlite / mysql / postgres)// Logan.Li
|
||||
func DriverName(db *gorm.DB) string {
|
||||
if db == nil || db.Dialector == nil {
|
||||
return "mysql"
|
||||
}
|
||||
name := strings.ToLower(db.Dialector.Name())
|
||||
switch {
|
||||
case strings.Contains(name, "sqlite"):
|
||||
return "sqlite"
|
||||
case strings.Contains(name, "postgres"):
|
||||
return "postgres"
|
||||
default:
|
||||
return "mysql"
|
||||
}
|
||||
}
|
||||
|
||||
// HourBucketExpr 按小时分桶表达式(跨 SQLite / MySQL)// Logan.Li
|
||||
func HourBucketExpr(driver string) string {
|
||||
switch driver {
|
||||
case "sqlite":
|
||||
return "cast(strftime('%s', report_time) as integer) / 3600"
|
||||
default:
|
||||
return "UNIX_TIMESTAMP(report_time) DIV 3600"
|
||||
}
|
||||
}
|
||||
|
||||
// CleanupAgentReportsSQL 清理 7 天前 Agent 上报 // Logan.Li
|
||||
func CleanupAgentReportsSQL(driver string) string {
|
||||
switch driver {
|
||||
case "sqlite":
|
||||
return "DELETE FROM monitor_agent_reports WHERE report_time < datetime('now', '-7 days')"
|
||||
default:
|
||||
return "DELETE FROM monitor_agent_reports WHERE report_time < DATE_SUB(NOW(), INTERVAL 7 DAY)"
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,10 @@ func newGoRedis(cfg Config) (Cache, error) {
|
||||
return &goRedisCache{client: client}, nil
|
||||
}
|
||||
|
||||
func (r *goRedisCache) Capabilities() Capabilities {
|
||||
return Capabilities{SortedSet: true, PubSub: true}
|
||||
}
|
||||
|
||||
func (r *goRedisCache) Get(ctx context.Context, key string) (string, error) {
|
||||
val, err := r.client.Get(ctx, key).Result()
|
||||
if err == goredis.Nil {
|
||||
|
||||
@@ -40,8 +40,15 @@ type SortedSetMember struct {
|
||||
Member string
|
||||
}
|
||||
|
||||
// Capabilities 缓存后端能力(local/redka 与外部 Redis 差异)// Logan.Li
|
||||
type Capabilities struct {
|
||||
SortedSet bool `json:"sorted_set"`
|
||||
PubSub bool `json:"pubsub"`
|
||||
}
|
||||
|
||||
// Cache Redis 缓存接口 // Logan.Li
|
||||
type Cache interface {
|
||||
Capabilities() Capabilities
|
||||
Get(ctx context.Context, key string) (string, error)
|
||||
Set(ctx context.Context, key string, value string, expiration time.Duration) error
|
||||
Del(ctx context.Context, keys ...string) error
|
||||
|
||||
@@ -31,6 +31,10 @@ func newRedka(cfg Config) (Cache, error) {
|
||||
return &redkaCache{db: db}, nil
|
||||
}
|
||||
|
||||
func (r *redkaCache) Capabilities() Capabilities {
|
||||
return Capabilities{SortedSet: false, PubSub: false}
|
||||
}
|
||||
|
||||
func (r *redkaCache) Get(ctx context.Context, key string) (string, error) {
|
||||
// Redka v0.2.x: Str().Get() 返回 core.Value // Logan.Li
|
||||
val, err := r.db.Str().Get(key)
|
||||
@@ -115,5 +119,6 @@ func (r *redkaCache) ZRemRangeByScore(ctx context.Context, key string, min, max
|
||||
var errPubSubNotSupported = fmt.Errorf("Pub/Sub is not supported by the redka backend; use go-redis (driver=redis)")
|
||||
|
||||
func (r *redkaCache) Publish(ctx context.Context, channel string, message string) error {
|
||||
return errPubSubNotSupported
|
||||
// local/redka 无 Pub/Sub,流水线日志走轮询 Get,此处 no-op // Logan.Li
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user