Skip to content

Commit

Permalink
feat: refactoring steve cache (#6278)
Browse files Browse the repository at this point in the history
* feat: caching with gcache instead of sync.Map

* fix: slow request

* feat: add cache configuration information to bootstrap

* perf: go fmt

* feat: setting environment variables

* feat: init when create steve cache and using the built-in method to get Internal Client Context

* feat: note yaml config

* feat: log more error details

* feat: log more error details
  • Loading branch information
CeerDecy authored Jul 2, 2024
1 parent 941eb31 commit 1d5d1d6
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 73 deletions.
8 changes: 6 additions & 2 deletions cmd/erda-server/bootstrap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,13 @@ erda.dop.rule.action.dingtalkworknotice: { }
#erda.core.org: {}

########### cmp
cmp: { }
cmp:
# steve cache time to live, default 10 min
cache_ttl: ${CMP_STEVE_CACHE_TTL:10m}
# steve cache total size (MB), default 64MB
cache_size: ${CMP_STEVE_CACHE_SIZE:64}
[email protected]:
addr: "${MONITOR_GRPC_ADDR:monitor:7080}"
addr: "${MONITOR_GRPC_ADDR:monitor:7080}"xwxxx
block: false
erda.core.monitor.metric-client: { }
[email protected]:
Expand Down
26 changes: 21 additions & 5 deletions internal/apps/cmp/endpoints/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package endpoints
import (
"context"
"net/http"
"time"

clusterpb "github.com/erda-project/erda-proto-go/core/clustermanager/cluster/pb"
cronpb "github.com/erda-project/erda-proto-go/core/pipeline/cron/pb"
Expand Down Expand Up @@ -62,10 +63,16 @@ type Endpoints struct {
ClusterSvc clusterpb.ClusterServiceServer
PipelineSvc pipelinepb.PipelineServiceServer

reportTable *resource.ReportTable
CronService cronpb.CronServiceServer
org org.Interface
registry registry.Interface
reportTable *resource.ReportTable
CronService cronpb.CronServiceServer
org org.Interface
registry registry.Interface
SteveCacheConfig *steveCacheConfig
}

type steveCacheConfig struct {
TTL time.Duration
Size int
}

type Option func(*Endpoints)
Expand All @@ -88,7 +95,7 @@ func New(ctx context.Context, db *dbclient.DBClient, js jsonstore.JsonStore, cac
e.metrics = ctx.Value("metrics").(*metrics.Metric)
e.Resource = ctx.Value("resource").(*resource.Resource)
e.CachedJS = cachedJS
e.SteveAggregator = steve.NewAggregator(ctx, e.bdl, e.ClusterSvc)
e.SteveAggregator = steve.NewAggregator(ctx, e.bdl, e.ClusterSvc, e.SteveCacheConfig.TTL, e.SteveCacheConfig.Size)
e.registry = registry.New(e.ClusterSvc)
return e
}
Expand All @@ -97,6 +104,15 @@ func (e *Endpoints) GetCluster() *clusters.Clusters {
return e.clusters
}

func WithSteveCacheConfig(ttl time.Duration, size int) Option {
return func(e *Endpoints) {
e.SteveCacheConfig = &steveCacheConfig{
TTL: ttl,
Size: size,
}
}
}

// WithBundle With bundle
func WithBundle(bdl *bundle.Bundle) Option {
return func(e *Endpoints) {
Expand Down
1 change: 1 addition & 0 deletions internal/apps/cmp/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func (p *provider) initEndpoints(ctx context.Context, db *dbclient.DBClient, js,
endpoints.WithClusterServiceServer(p.ClusterSvc),
endpoints.WithOrg(p.Org),
endpoints.WithPipelineSvc(p.PipelineSvc),
endpoints.WithSteveCacheConfig(p.Cfg.SteveCacheTTL, p.Cfg.SteveCacheSize),
)

// Sync org resource task status
Expand Down
6 changes: 6 additions & 0 deletions internal/apps/cmp/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ type provider struct {
Tran i18n.Translator `translator:"common"`
SteveAggregator *steve.Aggregator
Org org.Interface
Cfg *config
}

type config struct {
SteveCacheTTL time.Duration `file:"cache_ttl" default:"10m"`
SteveCacheSize int `file:"cache_size" default:"5000"`
}

// Run Run the provider
Expand Down
Loading

0 comments on commit 1d5d1d6

Please sign in to comment.