From f9e32512f65f1f463d711032748155b37776e11b Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Wed, 9 Mar 2022 10:01:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=87=AA=E5=8A=A8=E5=88=86=E8=A1=A8=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/models/http_access_log_dao.go | 13 ++++++-- internal/db/models/http_access_log_manager.go | 4 +-- internal/setup/sql_upgrade.go | 32 +++++++++++++++++++ internal/setup/sql_upgrade_test.go | 17 ++++++++++ 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/internal/db/models/http_access_log_dao.go b/internal/db/models/http_access_log_dao.go index b03767c3..471f94f1 100644 --- a/internal/db/models/http_access_log_dao.go +++ b/internal/db/models/http_access_log_dao.go @@ -44,7 +44,7 @@ var ( accessLogQueueChanged = make(chan zero.Zero, 1) accessLogEnableAutoPartial = true // 是否启用自动分表 - accessLogPartialRows int64 = 500_000 // 自动分表的单表最大值 + accessLogRowsPerTable int64 = 500_000 // 自动分表的单表最大值 ) type accessLogTableQuery struct { @@ -224,7 +224,7 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLog(tx *dbs.Tx, dao *HTTPAccessLog return err } - if lastId%accessLogPartialRows == 0 { + if accessLogEnableAutoPartial && accessLogRowsPerTable > 0 && lastId%accessLogRowsPerTable == 0 { SharedHTTPAccessLogManager.ResetTable(dao.Instance, day) } @@ -652,11 +652,18 @@ func (this *HTTPAccessLogDAO) SetupQueue() { config.MaxLength = 100_000 } + accessLogEnableAutoPartial = config.EnableAutoPartial + if config.RowsPerTable > 0 { + accessLogRowsPerTable = config.RowsPerTable + } + if accessLogQueueMaxLength != config.MaxLength { accessLogQueueMaxLength = config.MaxLength oldAccessLogQueue = accessLogQueue accessLogQueue = make(chan *pb.HTTPAccessLog, config.MaxLength) } - remotelogs.Println("HTTP_ACCESS_LOG_QUEUE", "change queue max length: "+types.String(config.MaxLength)+", percent: "+types.String(config.Percent)+", countPerSecond: "+types.String(config.CountPerSecond)) + if Tea.IsTesting() { + remotelogs.Println("HTTP_ACCESS_LOG_QUEUE", "change queue "+string(configJSON)) + } } diff --git a/internal/db/models/http_access_log_manager.go b/internal/db/models/http_access_log_manager.go index 2fdcbb31..27b7690f 100644 --- a/internal/db/models/http_access_log_manager.go +++ b/internal/db/models/http_access_log_manager.go @@ -214,7 +214,7 @@ func (this *HTTPAccessLogManager) findTableWithoutCache(db *dbs.DB, day string, } var lastTableName = tableNames[len(tableNames)-1] - if !force || !accessLogEnableAutoPartial { + if !force || !accessLogEnableAutoPartial || accessLogRowsPerTable <= 0 { hasRemoteAddrField, hasDomainField, err := this.checkTableFields(db, lastTableName) if err != nil { return nil, err @@ -235,7 +235,7 @@ func (this *HTTPAccessLogManager) findTableWithoutCache(db *dbs.DB, day string, if lastId != nil { var lastInt64Id = types.Int64(lastId) - if lastInt64Id >= accessLogPartialRows { + if accessLogRowsPerTable > 0 && lastInt64Id >= accessLogRowsPerTable { // create next partial table var nextTableName = "" if accessLogTableMainReg.MatchString(lastTableName) { diff --git a/internal/setup/sql_upgrade.go b/internal/setup/sql_upgrade.go index 04ae8520..71eb7d1f 100644 --- a/internal/setup/sql_upgrade.go +++ b/internal/setup/sql_upgrade.go @@ -69,6 +69,9 @@ var upgradeFuncs = []*upgradeVersion{ { "0.4.1", upgradeV0_4_1, }, + { + "0.4.5", upgradeV0_4_5, + }, } // UpgradeSQLData 升级SQL数据 @@ -590,3 +593,32 @@ func upgradeV0_4_1(db *dbs.DB) error { return nil } + +// v0.4.5 +func upgradeV0_4_5(db *dbs.DB) error { + // 升级访问日志自动分表 + { + var dao = models.NewSysSettingDAO() + valueJSON, err := dao.ReadSetting(nil, systemconfigs.SettingCodeAccessLogQueue) + if err != nil { + return err + } + if len(valueJSON) > 0 { + var config = &serverconfigs.AccessLogQueueConfig{} + err = json.Unmarshal(valueJSON, config) + if err == nil { + config.EnableAutoPartial = true + config.RowsPerTable = 500_000 + configJSON, err := json.Marshal(config) + if err == nil { + err = dao.UpdateSetting(nil, systemconfigs.SettingCodeAccessLogQueue, configJSON) + if err != nil { + return err + } + } + } + } + } + + return nil +} diff --git a/internal/setup/sql_upgrade_test.go b/internal/setup/sql_upgrade_test.go index a64e59a2..6df97999 100644 --- a/internal/setup/sql_upgrade_test.go +++ b/internal/setup/sql_upgrade_test.go @@ -116,3 +116,20 @@ func TestUpgradeSQLData_v0_4_1(t *testing.T) { } t.Log("ok") } + + +func TestUpgradeSQLData_v0_4_5(t *testing.T) { + db, err := dbs.NewInstanceFromConfig(&dbs.DBConfig{ + Driver: "mysql", + Dsn: "root:123456@tcp(127.0.0.1:3306)/db_edge?charset=utf8mb4&timeout=30s", + Prefix: "edge", + }) + if err != nil { + t.Fatal(err) + } + err = upgradeV0_4_5(db) + if err != nil { + t.Fatal(err) + } + t.Log("ok") +} \ No newline at end of file