自动检测本地数据库磁盘是否已满,如果已满,则不再写入访问日志

This commit is contained in:
刘祥超
2022-10-27 10:27:47 +08:00
parent 4b425e1698
commit 0a6111b2e5
6 changed files with 145 additions and 9 deletions

View File

@@ -2,6 +2,7 @@ package models
import (
"fmt"
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
"github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
@@ -29,8 +30,9 @@ var httpAccessLogDAOMapping = map[int64]*HTTPAccessLogDAOWrapper{} // dbNodeId =
// HTTPAccessLogDAOWrapper HTTP访问日志DAO
type HTTPAccessLogDAOWrapper struct {
DAO *HTTPAccessLogDAO
NodeId int64
DAO *HTTPAccessLogDAO
NodeId int64
IsLocal bool
}
func init() {
@@ -195,7 +197,7 @@ func (this *DBNodeInitializer) loop() error {
continue
}
daoObject := dbs.DAOObject{
var daoObject = dbs.DAOObject{
Instance: db,
DB: node.Name + "(id:" + strconv.Itoa(int(node.Id)) + ")",
Table: tableDef.Name,
@@ -210,12 +212,13 @@ func (this *DBNodeInitializer) loop() error {
accessLogLocker.Lock()
accessLogDBMapping[nodeId] = db
dao := &HTTPAccessLogDAO{
var dao = &HTTPAccessLogDAO{
DAOObject: daoObject,
}
httpAccessLogDAOMapping[nodeId] = &HTTPAccessLogDAOWrapper{
DAO: dao,
NodeId: nodeId,
DAO: dao,
NodeId: nodeId,
IsLocal: dbutils.IsLocalAddr(node.Host),
}
accessLogLocker.Unlock()
}

View File

@@ -161,16 +161,27 @@ func (this *HTTPAccessLogDAO) DumpAccessLogsFromQueue(size int) (hasMore bool, e
size = 100
}
if len(oldAccessLogQueue) == 0 && len(accessLogQueue) == 0 {
return false, nil
}
var dao = randomHTTPAccessLogDAO()
if dao == nil {
dao = &HTTPAccessLogDAOWrapper{
DAO: SharedHTTPAccessLogDAO,
NodeId: 0,
}
}
if len(oldAccessLogQueue) == 0 && len(accessLogQueue) == 0 {
return false, nil
// 检查本地数据库空间
if dbutils.IsLocalDatabase && !dbutils.HasFreeSpace {
return false, errors.New("dump accesslog failed: there is no enough space left for database (" + dbutils.LocalDatabaseDataDir + ")")
}
} else if dao.IsLocal {
// 检查本地数据库空间
// 我们假定本地只能安装一个数据库访问日志中的数据库和当前API连接的数据库一致
if !dbutils.HasFreeSpace {
return true, errors.New("dump accesslog failed: there is no enough space left for database (" + dbutils.LocalDatabaseDataDir + ")")
}
}
// 开始事务