mirror of
https://github.com/portainer/portainer.git
synced 2025-08-02 20:35:25 +02:00
fix(performance): optimize performance for edge EE-3311 (#8040)
This commit is contained in:
parent
3d28a6f877
commit
dd0d1737b0
23 changed files with 577 additions and 164 deletions
|
@ -2,8 +2,10 @@ package edgestack
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/internal/edge/cache"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
@ -16,6 +18,8 @@ const (
|
|||
// Service represents a service for managing Edge stack data.
|
||||
type Service struct {
|
||||
connection portainer.Connection
|
||||
idxVersion map[portainer.EdgeStackID]int
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func (service *Service) BucketName() string {
|
||||
|
@ -29,9 +33,21 @@ func NewService(connection portainer.Connection) (*Service, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return &Service{
|
||||
s := &Service{
|
||||
connection: connection,
|
||||
}, nil
|
||||
idxVersion: make(map[portainer.EdgeStackID]int),
|
||||
}
|
||||
|
||||
es, err := s.EdgeStacks()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, e := range es {
|
||||
s.idxVersion[e.ID] = e.Version
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// EdgeStacks returns an array containing all edge stacks
|
||||
|
@ -42,7 +58,6 @@ func (service *Service) EdgeStacks() ([]portainer.EdgeStack, error) {
|
|||
BucketName,
|
||||
&portainer.EdgeStack{},
|
||||
func(obj interface{}) (interface{}, error) {
|
||||
//var tag portainer.Tag
|
||||
stack, ok := obj.(*portainer.EdgeStack)
|
||||
if !ok {
|
||||
log.Debug().Str("obj", fmt.Sprintf("%#v", obj)).Msg("failed to convert to EdgeStack object")
|
||||
|
@ -70,22 +85,77 @@ func (service *Service) EdgeStack(ID portainer.EdgeStackID) (*portainer.EdgeStac
|
|||
return &stack, nil
|
||||
}
|
||||
|
||||
// EdgeStackVersion returns the version of the given edge stack ID directly from an in-memory index
|
||||
func (service *Service) EdgeStackVersion(ID portainer.EdgeStackID) (int, bool) {
|
||||
service.mu.RLock()
|
||||
v, ok := service.idxVersion[ID]
|
||||
service.mu.RUnlock()
|
||||
|
||||
return v, ok
|
||||
}
|
||||
|
||||
// CreateEdgeStack saves an Edge stack object to db.
|
||||
func (service *Service) Create(id portainer.EdgeStackID, edgeStack *portainer.EdgeStack) error {
|
||||
|
||||
edgeStack.ID = id
|
||||
|
||||
return service.connection.CreateObjectWithId(
|
||||
err := service.connection.CreateObjectWithId(
|
||||
BucketName,
|
||||
int(edgeStack.ID),
|
||||
edgeStack,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
service.mu.Lock()
|
||||
service.idxVersion[id] = edgeStack.Version
|
||||
service.mu.Unlock()
|
||||
|
||||
for endpointID := range edgeStack.Status {
|
||||
cache.Del(endpointID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Deprecated: Use UpdateEdgeStackFunc instead.
|
||||
func (service *Service) UpdateEdgeStack(ID portainer.EdgeStackID, edgeStack *portainer.EdgeStack) error {
|
||||
service.mu.Lock()
|
||||
defer service.mu.Unlock()
|
||||
|
||||
prevEdgeStack, err := service.EdgeStack(ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
identifier := service.connection.ConvertToKey(int(ID))
|
||||
return service.connection.UpdateObject(BucketName, identifier, edgeStack)
|
||||
|
||||
err = service.connection.UpdateObject(BucketName, identifier, edgeStack)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
service.idxVersion[ID] = edgeStack.Version
|
||||
|
||||
// Invalidate cache for removed environments
|
||||
for endpointID := range prevEdgeStack.Status {
|
||||
if _, ok := edgeStack.Status[endpointID]; !ok {
|
||||
cache.Del(endpointID)
|
||||
}
|
||||
}
|
||||
|
||||
// Invalidate cache when version changes and for added environments
|
||||
for endpointID := range edgeStack.Status {
|
||||
if prevEdgeStack.Version == edgeStack.Version {
|
||||
if _, ok := prevEdgeStack.Status[endpointID]; ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
cache.Del(endpointID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateEdgeStackFunc updates an Edge stack inside a transaction avoiding data races.
|
||||
|
@ -93,15 +163,66 @@ func (service *Service) UpdateEdgeStackFunc(ID portainer.EdgeStackID, updateFunc
|
|||
id := service.connection.ConvertToKey(int(ID))
|
||||
edgeStack := &portainer.EdgeStack{}
|
||||
|
||||
service.mu.Lock()
|
||||
defer service.mu.Unlock()
|
||||
|
||||
return service.connection.UpdateObjectFunc(BucketName, id, edgeStack, func() {
|
||||
prevEndpoints := make(map[portainer.EndpointID]struct{}, len(edgeStack.Status))
|
||||
for endpointID := range edgeStack.Status {
|
||||
if _, ok := edgeStack.Status[endpointID]; !ok {
|
||||
prevEndpoints[endpointID] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
updateFunc(edgeStack)
|
||||
|
||||
prevVersion := service.idxVersion[ID]
|
||||
service.idxVersion[ID] = edgeStack.Version
|
||||
|
||||
// Invalidate cache for removed environments
|
||||
for endpointID := range prevEndpoints {
|
||||
if _, ok := edgeStack.Status[endpointID]; !ok {
|
||||
cache.Del(endpointID)
|
||||
}
|
||||
}
|
||||
|
||||
// Invalidate cache when version changes and for added environments
|
||||
for endpointID := range edgeStack.Status {
|
||||
if prevVersion == edgeStack.Version {
|
||||
if _, ok := prevEndpoints[endpointID]; ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
cache.Del(endpointID)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteEdgeStack deletes an Edge stack.
|
||||
func (service *Service) DeleteEdgeStack(ID portainer.EdgeStackID) error {
|
||||
service.mu.Lock()
|
||||
defer service.mu.Unlock()
|
||||
|
||||
edgeStack, err := service.EdgeStack(ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
identifier := service.connection.ConvertToKey(int(ID))
|
||||
return service.connection.DeleteObject(BucketName, identifier)
|
||||
|
||||
err = service.connection.DeleteObject(BucketName, identifier)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
delete(service.idxVersion, ID)
|
||||
|
||||
for endpointID := range edgeStack.Status {
|
||||
cache.Del(endpointID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetNextIdentifier returns the next identifier for an environment(endpoint).
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue