优化节点配置生成速度

This commit is contained in:
刘祥超
2021-11-11 14:16:42 +08:00
parent 8afd21f2f4
commit 5b32343a2c
49 changed files with 527 additions and 198 deletions

View File

@@ -8,8 +8,8 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers"
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
)
// NSDomainService 域名相关服务
@@ -149,7 +149,7 @@ func (this *NSDomainService) ListEnabledNSDomains(ctx context.Context, req *pb.L
return nil, err
}
pbDomains := []*pb.NSDomain{}
var cacheMap = maps.Map{}
var cacheMap = utils.NewCacheMap()
for _, domain := range domains {
// 集群
cluster, err := models.SharedNSClusterDAO.FindEnabledNSCluster(tx, int64(domain.ClusterId))

View File

@@ -755,7 +755,7 @@ func (this *AdminService) UpdateAdminTheme(ctx context.Context, req *pb.UpdateAd
// 查找集群、节点和服务的指标数据
func (this *AdminService) findMetricDataCharts(tx *dbs.Tx) (result []*pb.MetricDataChart, err error) {
// 集群指标
items, err := models.SharedMetricItemDAO.FindAllPublicItems(tx, serverconfigs.MetricItemCategoryHTTP)
items, err := models.SharedMetricItemDAO.FindAllPublicItems(tx, serverconfigs.MetricItemCategoryHTTP, nil)
if err != nil {
return nil, err
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
@@ -147,7 +148,7 @@ func (this *MessageMediaInstanceService) FindEnabledMessageMediaInstance(ctx con
}
var tx = this.NullTx()
var cacheMap = maps.Map{}
var cacheMap = utils.NewCacheMap()
instance, err := models.SharedMessageMediaInstanceDAO.FindEnabledMessageMediaInstance(tx, req.MessageMediaInstanceId, cacheMap)
if err != nil {
return nil, err

View File

@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/dbs"
@@ -69,7 +70,7 @@ func (this *MessageReceiverService) FindAllEnabledMessageReceivers(ctx context.C
}
var tx = this.NullTx()
var cacheMap = maps.Map{}
var cacheMap = utils.NewCacheMap()
receivers, err := models.SharedMessageReceiverDAO.FindAllEnabledReceivers(tx, req.Role, req.NodeClusterId, req.NodeId, req.ServerId, "")
if err != nil {
return nil, err

View File

@@ -3,8 +3,8 @@ package services
import (
"context"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
)
// MessageRecipientService 消息接收人服务
@@ -83,7 +83,7 @@ func (this *MessageRecipientService) ListEnabledMessageRecipients(ctx context.Co
}
var tx = this.NullTx()
var cacheMap = maps.Map{}
var cacheMap = utils.NewCacheMap()
recipients, err := models.SharedMessageRecipientDAO.ListAllEnabledRecipients(tx, req.AdminId, req.MessageRecipientGroupId, req.MediaType, req.Keyword, req.Offset, req.Size)
if err != nil {
return nil, err
@@ -163,7 +163,7 @@ func (this *MessageRecipientService) FindEnabledMessageRecipient(ctx context.Con
}
var tx = this.NullTx()
var cacheMap = maps.Map{}
var cacheMap = utils.NewCacheMap()
recipient, err := models.SharedMessageRecipientDAO.FindEnabledMessageRecipient(tx, req.MessageRecipientId, cacheMap)
if err != nil {
return nil, err

View File

@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
@@ -37,7 +38,7 @@ func (this *MessageTaskService) FindSendingMessageTasks(ctx context.Context, req
}
var tx = this.NullTx()
var cacheMap = maps.Map{}
var cacheMap = utils.NewCacheMap()
tasks, err := models.SharedMessageTaskDAO.FindSendingMessageTasks(tx, req.Size)
if err != nil {
return nil, err
@@ -186,7 +187,7 @@ func (this *MessageTaskService) FindEnabledMessageTask(ctx context.Context, req
}
var tx = this.NullTx()
var cacheMap = maps.Map{}
var cacheMap = utils.NewCacheMap()
task, err := models.SharedMessageTaskDAO.FindEnabledMessageTask(tx, req.MessageTaskId)
if err != nil {
return nil, err
@@ -305,7 +306,7 @@ func (this *MessageTaskService) ListMessageTasksWithStatus(ctx context.Context,
}
var tx = this.NullTx()
var cacheMap = maps.Map{}
var cacheMap = utils.NewCacheMap()
tasks, err := models.SharedMessageTaskDAO.ListMessageTasksWithStatus(tx, types.Int(req.Status), req.Offset, req.Size)
if err != nil {
return nil, err

View File

@@ -3,8 +3,8 @@ package services
import (
"context"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
)
// MessageTaskLogService 消息发送日志相关服务
@@ -33,7 +33,7 @@ func (this *MessageTaskLogService) ListMessageTaskLogs(ctx context.Context, req
return nil, err
}
var tx = this.NullTx()
var cacheMap = maps.Map{}
var cacheMap = utils.NewCacheMap()
logs, err := models.SharedMessageTaskLogDAO.ListLogs(tx, req.Offset, req.Size)
if err != nil {
return nil, err

View File

@@ -10,6 +10,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/installers"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
@@ -23,8 +24,18 @@ import (
"net"
"path/filepath"
"strings"
"sync"
"time"
)
// NodeVersionCache 节点版本缓存
type NodeVersionCache struct {
CacheMap map[int64]*utils.CacheMap // version => map
}
var nodeVersionCacheMap = map[int64]*NodeVersionCache{} // [cluster_id]_[version] => cache
var nodeVersionCacheLocker = &sync.Mutex{}
// NodeService 边缘节点相关服务
type NodeService struct {
BaseService
@@ -604,8 +615,6 @@ func (this *NodeService) FindEnabledBasicNode(ctx context.Context, req *pb.FindE
// FindCurrentNodeConfig 组合节点配置
func (this *NodeService) FindCurrentNodeConfig(ctx context.Context, req *pb.FindCurrentNodeConfigRequest) (*pb.FindCurrentNodeConfigResponse, error) {
_ = req
// 校验节点
_, _, nodeId, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeNode)
if err != nil {
@@ -623,7 +632,12 @@ func (this *NodeService) FindCurrentNodeConfig(ctx context.Context, req *pb.Find
return &pb.FindCurrentNodeConfigResponse{IsChanged: false}, nil
}
nodeConfig, err := models.SharedNodeDAO.ComposeNodeConfig(tx, nodeId, nil)
clusterId, err := models.SharedNodeDAO.FindNodeClusterId(tx, nodeId)
if err != nil {
return nil, err
}
var cacheMap = this.findClusterCacheMap(clusterId, req.NodeTaskVersion)
nodeConfig, err := models.SharedNodeDAO.ComposeNodeConfig(tx, nodeId, cacheMap)
if err != nil {
return nil, err
}
@@ -633,6 +647,7 @@ func (this *NodeService) FindCurrentNodeConfig(ctx context.Context, req *pb.Find
return nil, err
}
// 压缩
var isCompressed = false
if req.Compress {
var buf = &bytes.Buffer{}
@@ -1561,3 +1576,37 @@ func (this *NodeService) UpdateNodeCache(ctx context.Context, req *pb.UpdateNode
return this.Success()
}
// 获取缓存CacheMap
func (this *NodeService) findClusterCacheMap(clusterId int64, version int64) *utils.CacheMap {
nodeVersionCacheLocker.Lock()
defer nodeVersionCacheLocker.Unlock()
cache, ok := nodeVersionCacheMap[clusterId]
if ok {
cacheMap, ok := cache.CacheMap[version]
if ok {
return cacheMap
}
// 清除以前版本
for v := range cache.CacheMap {
if version-v > 60*time.Second.Nanoseconds() {
delete(cache.CacheMap, v)
}
}
// 添加
cacheMap = utils.NewCacheMap()
cache.CacheMap[version] = cacheMap
return cacheMap
} else {
var cacheMap = utils.NewCacheMap()
cache = &NodeVersionCache{
CacheMap: map[int64]*utils.CacheMap{
version: cacheMap,
}}
nodeVersionCacheMap[clusterId] = cache
return cacheMap
}
}

View File

@@ -665,7 +665,7 @@ func (this *NodeClusterService) FindEnabledNodeClusterTOA(ctx context.Context, r
tx := this.NullTx()
config, err := models.SharedNodeClusterDAO.FindClusterTOAConfig(tx, req.NodeClusterId)
config, err := models.SharedNodeClusterDAO.FindClusterTOAConfig(tx, req.NodeClusterId, nil)
if err != nil {
return nil, err
}

View File

@@ -8,12 +8,12 @@ import (
"github.com/iwind/TeaGo/maps"
)
// 防火墙动作服务
// NodeClusterFirewallActionService 防火墙动作服务
type NodeClusterFirewallActionService struct {
BaseService
}
// 创建动作
// CreateNodeClusterFirewallAction 创建动作
func (this *NodeClusterFirewallActionService) CreateNodeClusterFirewallAction(ctx context.Context, req *pb.CreateNodeClusterFirewallActionRequest) (*pb.NodeClusterFirewallActionResponse, error) {
adminId, err := this.ValidateAdmin(ctx, 0)
if err != nil {
@@ -36,7 +36,7 @@ func (this *NodeClusterFirewallActionService) CreateNodeClusterFirewallAction(ct
return &pb.NodeClusterFirewallActionResponse{NodeClusterFirewallActionId: actionId}, nil
}
// 修改动作
// UpdateNodeClusterFirewallAction 修改动作
func (this *NodeClusterFirewallActionService) UpdateNodeClusterFirewallAction(ctx context.Context, req *pb.UpdateNodeClusterFirewallActionRequest) (*pb.RPCSuccess, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
@@ -59,7 +59,7 @@ func (this *NodeClusterFirewallActionService) UpdateNodeClusterFirewallAction(ct
return this.Success()
}
// 删除动作
// DeleteNodeClusterFirewallAction 删除动作
func (this *NodeClusterFirewallActionService) DeleteNodeClusterFirewallAction(ctx context.Context, req *pb.DeleteNodeClusterFirewallActionRequest) (*pb.RPCSuccess, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
@@ -74,7 +74,7 @@ func (this *NodeClusterFirewallActionService) DeleteNodeClusterFirewallAction(ct
return this.Success()
}
// 查询集群的所有动作
// FindAllEnabledNodeClusterFirewallActions 查询集群的所有动作
func (this *NodeClusterFirewallActionService) FindAllEnabledNodeClusterFirewallActions(ctx context.Context, req *pb.FindAllEnabledNodeClusterFirewallActionsRequest) (*pb.FindAllEnabledNodeClusterFirewallActionsResponse, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
@@ -82,7 +82,7 @@ func (this *NodeClusterFirewallActionService) FindAllEnabledNodeClusterFirewallA
}
var tx = this.NullTx()
actions, err := models.SharedNodeClusterFirewallActionDAO.FindAllEnabledFirewallActions(tx, req.NodeClusterId)
actions, err := models.SharedNodeClusterFirewallActionDAO.FindAllEnabledFirewallActions(tx, req.NodeClusterId, nil)
if err != nil {
return nil, err
}
@@ -100,7 +100,7 @@ func (this *NodeClusterFirewallActionService) FindAllEnabledNodeClusterFirewallA
return &pb.FindAllEnabledNodeClusterFirewallActionsResponse{NodeClusterFirewallActions: pbActions}, nil
}
// 查询单个动作
// FindEnabledNodeClusterFirewallAction 查询单个动作
func (this *NodeClusterFirewallActionService) FindEnabledNodeClusterFirewallAction(ctx context.Context, req *pb.FindEnabledNodeClusterFirewallActionRequest) (*pb.FindEnabledNodeClusterFirewallActionResponse, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
@@ -125,7 +125,7 @@ func (this *NodeClusterFirewallActionService) FindEnabledNodeClusterFirewallActi
}}, nil
}
// 计算动作数量
// CountAllEnabledNodeClusterFirewallActions 计算动作数量
func (this *NodeClusterFirewallActionService) CountAllEnabledNodeClusterFirewallActions(ctx context.Context, req *pb.CountAllEnabledNodeClusterFirewallActionsRequest) (*pb.RPCCountResponse, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {

View File

@@ -18,7 +18,9 @@ import (
"time"
)
// 命令请求相关
var primaryNodeId int64 = 0
// CommandRequest 命令请求相关
type CommandRequest struct {
Id int64
Code string
@@ -74,6 +76,26 @@ func (this *NodeService) NodeStream(server pb.NodeService_NodeStreamServer) erro
return err
}
// 选择一个作为主节点
if primaryNodeId == 0 {
primaryNodeId = nodeId
}
defer func() {
if primaryNodeId == nodeId {
primaryNodeId = 0
nodeLocker.Lock()
if len(nodeRequestChanMap) > 0 {
for anotherNodeId := range nodeRequestChanMap {
primaryNodeId = anotherNodeId
break
}
}
nodeLocker.Unlock()
}
}()
// 返回连接成功
{
apiConfig, err := configs.SharedAPIConfig()

View File

@@ -35,8 +35,10 @@ func (this *NodeTaskService) FindNodeTasks(ctx context.Context, req *pb.FindNode
pbTasks := []*pb.NodeTask{}
for _, task := range tasks {
pbTasks = append(pbTasks, &pb.NodeTask{
Id: int64(task.Id),
Type: task.Type,
Id: int64(task.Id),
Type: task.Type,
Version: int64(task.Version),
IsPrimary: primaryNodeId == nodeId,
})
}

View File

@@ -8,6 +8,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/messageconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
@@ -1639,7 +1640,7 @@ func (this *ServerService) PurgeServerCache(ctx context.Context, req *pb.PurgeSe
}
var tx = this.NullTx()
var cacheMap = maps.Map{}
var cacheMap = utils.NewCacheMap()
var purgeResponse = &pb.PurgeServerCacheResponse{}
for _, domain := range req.Domains {

View File

@@ -566,7 +566,7 @@ func (this *ServerStatBoardService) findNodeClusterMetricDataCharts(tx *dbs.Tx,
metricItemIds = append(metricItemIds, itemId)
}
publicMetricItems, err := models.SharedMetricItemDAO.FindAllPublicItems(tx, category)
publicMetricItems, err := models.SharedMetricItemDAO.FindAllPublicItems(tx, category, nil)
if err != nil {
return nil, err
}