Skip to content

Commit

Permalink
Merge remote-tracking branch 'github-bk-bcs/master'
Browse files Browse the repository at this point in the history
* github-bk-bcs/master:
  upd: 脚本管理版本相关的交互优化 (#2723)
  fix: flush cache when updating credential (#2722)
  fix:修复体验交互问题 (#2724)
  fix: duplicated register bedis client metrics (#2721)
  • Loading branch information
wenxinlee2015 committed Nov 1, 2023
2 parents a08645d + 7250998 commit a67195f
Show file tree
Hide file tree
Showing 20 changed files with 318 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package event

import (
"encoding/json"
"fmt"
"sync"

"bscp.io/cmd/cache-service/service/cache/keys"
Expand Down Expand Up @@ -151,10 +152,13 @@ func (c *consumer) refreshAllCache(kt *kit.Kit, events []*table.Event) error {
// consumeUpdateEvent consume update event.
func (c *consumer) consumeUpdateEvent(kt *kit.Kit, events []*table.Event) error {
updateAppEvents := make([]*table.Event, 0)
updateCredentialEvents := make([]*table.Event, 0)
for _, event := range events {
switch event.Spec.Resource {
case table.Application:
updateAppEvents = append(updateAppEvents, event)
case table.CredentialEvent:
updateCredentialEvents = append(updateCredentialEvents, event)
default:
logs.Errorf("unsupported update event resource: %s, id: %s, rid: %s", event.Spec.Resource, event.ID, kt.Rid)
continue
Expand All @@ -168,19 +172,30 @@ func (c *consumer) consumeUpdateEvent(kt *kit.Kit, events []*table.Event) error
}
}

if len(updateCredentialEvents) != 0 {
if err := c.refreshCredentialCache(kt, updateCredentialEvents); err != nil {
logs.Errorf("refresh credential cache failed, err: %v, rid: %s", err, kt.Rid)
return err
}
}

return nil
}

// consumeDeleteEvent delete strategy publish and instance publish cache.
func (c *consumer) consumeDeleteEvent(kt *kit.Kit, events []*table.Event) error {
delPublishEvents := make([]*table.Event, 0)
delAppEvents := make([]*table.Event, 0)
delCredentialEvents := make([]*table.Event, 0)
for _, event := range events {
switch event.Spec.Resource {
case table.Publish:
delPublishEvents = append(delPublishEvents, event)
case table.Application:
delAppEvents = append(delAppEvents, event)
case table.CredentialEvent:
delCredentialEvents = append(delCredentialEvents, event)

default:
logs.Errorf("unsupported delete event resource: %s, id: %s, rid: %s", event.Spec.Resource, event.ID, kt.Rid)
continue
Expand All @@ -199,6 +214,12 @@ func (c *consumer) consumeDeleteEvent(kt *kit.Kit, events []*table.Event) error
}
}

if len(delCredentialEvents) != 0 {
if err := c.deleteCredentialCache(kt, delCredentialEvents); err != nil {
return err
}
}

return nil
}

Expand All @@ -218,6 +239,22 @@ func (c *consumer) deleteAppMetaCache(kt *kit.Kit, events []*table.Event) error
return nil
}

// deleteCredentialCache delete credential cache from event.
func (c *consumer) deleteCredentialCache(kt *kit.Kit, events []*table.Event) error {

appKeys := make([]string, 0)
for _, one := range events {
appKeys = append(appKeys, keys.Key.Credential(one.Attachment.BizID, one.Spec.ResourceUid))
}

if err := c.bds.Delete(kt.Ctx, appKeys...); err != nil {
logs.Errorf("delete credential cache failed, keys: %v, err: %v, rid: %s", appKeys, err, kt.Rid)
return err
}

return nil
}

// cacheReleasedCI cache the all publish related release's configure items.
func (c *consumer) cacheReleasedCI(kt *kit.Kit, releaseBizID map[uint32]uint32) error {
reminder := make(map[uint32][]uint32, 0)
Expand Down Expand Up @@ -400,6 +437,44 @@ func (c *consumer) refreshOneBizAppMetaCache(kt *kit.Kit, bizID uint32, appIDs [
return nil
}

func (c *consumer) refreshCredentialCache(kt *kit.Kit, events []*table.Event) error {
if len(events) == 0 {
return nil
}

for _, event := range events {
cred, err := c.op.Credential().GetByCredentialString(kt, event.Attachment.BizID, event.Spec.ResourceUid)
if err != nil {
return err
}
details, _, err := c.op.CredentialScope().Get(kt, cred.ID, cred.Attachment.BizID)
if err != nil {
return err
}
scope := make([]string, 0, len(details))
for _, detail := range details {
scope = append(scope, string(detail.Spec.CredentialScope))
}
credentialCache := &types.CredentialCache{
Enabled: cred.Spec.Enable,
Scope: scope,
}
b, err := jsoni.Marshal(credentialCache)
if err != nil {
logs.Errorf("marshal credential: %d-%s,failed, err: %v", cred.Attachment.BizID, event.Spec.ResourceUid, err)
return err
}
// refresh credential cache.
if err := c.bds.Set(kt.Ctx, keys.Key.Credential(event.Attachment.BizID, event.Spec.ResourceUid),
b, keys.Key.CredentialTtlSec(false)); err != nil {
return fmt.Errorf("set biz: %d, credential: %s, cache failed, err: %v",
event.Attachment.BizID, event.Spec.ResourceUid, err)
}
}

return nil
}

type releaseBizID struct {
lock *sync.RWMutex
data map[ /*releaseID*/ uint32] /*bizID*/ uint32
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var Key = &keyGenerator{
nullKeyTTLRange: [2]int{60, 120},
releasedGroupTTLRange: [2]int{30 * 60, 60 * 60},
credentialMatchedCITTLRange: [2]int{30 * 60, 60 * 60},
credentialTTLRange: [2]int{5, 60},
credentialTTLRange: [2]int{30 * 60, 60 * 60},
releasedCITTLRange: [2]int{6 * oneDaySeconds, 7 * oneDaySeconds},
releasedHookTTLRange: [2]int{6 * oneDaySeconds, 7 * oneDaySeconds},
appMetaTTLRange: [2]int{6 * oneDaySeconds, 7 * oneDaySeconds},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func (s *Credential) CanMatchCI(kt *kit.Kit, bizID uint32, app string, credentia

if hit {
s.mc.hitCounter.With(prm.Labels{"resource": "credential", "biz": tools.Itoa(bizID)}).Inc()
if !c.Enabled {
return false, nil
}
for _, s := range c.Scope {
if tools.MatchAppConfigItem(s, app, ci.Path, ci.Name) {
return true, nil
Expand All @@ -103,12 +106,15 @@ func (s *Credential) CanMatchCI(kt *kit.Kit, bizID uint32, app string, credentia
return false, err
}

if err := s.client.SetWithExpire(fmt.Sprintf("%d-%s", bizID, credential), c, time.Second); err != nil {
if err := s.client.SetWithExpire(fmt.Sprintf("%d-%s", bizID, credential), c, 10*time.Second); err != nil {
logs.Errorf("refresh credential %d-%s cache failed, %s", bizID, credential, err.Error())
// do not return, ignore th error directly.
}

for _, s := range c.Scope {
if !c.Enabled {
return false, nil
}
if tools.MatchAppConfigItem(s, app, ci.Path, ci.Name) {
return true, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func main() {
err := plugin.Serve(&plugin.ServeOpts{
BackendFactoryFunc: service.Factory,
TLSProviderFunc: tlsProviderFunc,
Logger: logger,
})
if err != nil {
logger.Error("plugin shutting down", "error", err)
Expand Down
1 change: 1 addition & 0 deletions bcs-services/bcs-bscp/pkg/cc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ func (s *Repository) trySetDefault() {
if len(s.StorageType) == 0 {
s.StorageType = BkRepo
}
s.RedisCluster.trySetDefault()
}

// validate repo runtime.
Expand Down
47 changes: 29 additions & 18 deletions bcs-services/bcs-bscp/pkg/dal/bedis/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,46 @@
package bedis

import (
"sync"

"github.com/prometheus/client_golang/prometheus"

"bscp.io/pkg/metrics"
)

var (
metricInstance *metric
once sync.Once
)

func initMetric() *metric {
m := new(metric)
labels := prometheus.Labels{}
m.cmdLagMS = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: metrics.Namespace,
Subsystem: metrics.BedisCmdSubSys,
Name: "lag_milliseconds",
Help: "the lags(milliseconds) to exec a bedis command",
ConstLabels: labels,
Buckets: []float64{1, 2, 3, 4, 5, 7, 9, 12, 14, 16, 18, 20, 40, 60, 80, 100, 150, 200, 500},
}, []string{"cmd"})
metrics.Register().MustRegister(m.cmdLagMS)

m.errCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
once.Do(func() {
m := new(metric)
labels := prometheus.Labels{}
m.cmdLagMS = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: metrics.Namespace,
Subsystem: metrics.BedisCmdSubSys,
Name: "total_err_count",
Help: "the total error count when exec a bedis command",
Name: "lag_milliseconds",
Help: "the lags(milliseconds) to exec a bedis command",
ConstLabels: labels,
Buckets: []float64{1, 2, 3, 4, 5, 7, 9, 12, 14, 16, 18, 20, 40, 60, 80, 100, 150, 200, 500},
}, []string{"cmd"})
metrics.Register().MustRegister(m.errCounter)
metrics.Register().MustRegister(m.cmdLagMS)

m.errCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: metrics.Namespace,
Subsystem: metrics.BedisCmdSubSys,
Name: "total_err_count",
Help: "the total error count when exec a bedis command",
ConstLabels: labels,
}, []string{"cmd"})
metrics.Register().MustRegister(m.errCounter)

metricInstance = m

return m
})
return metricInstance
}

type metric struct {
Expand Down
9 changes: 2 additions & 7 deletions bcs-services/bcs-bscp/pkg/dal/dao/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,9 @@ func (dao *appDao) DeleteWithTx(kit *kit.Kit, tx *gen.QueryTx, g *table.App) err
Revision: &table.CreatedRevision{Creator: kit.User},
}
eDecorator := dao.event.Eventf(kit)
if err = eDecorator.Fire(one); err != nil {
if err = eDecorator.FireWithTx(tx, one); err != nil {
logs.Errorf("fire delete app: %s event failed, err: %v, rid: %s", g.ID, err, kit.Rid)
}
eDecorator.Finalizer(err)

if err != nil {
logs.Errorf("delete app: %d failed, err: %v, rid: %v", g.ID, err, kit.Rid)
return err
return errors.New("fire event failed, " + err.Error())
}

return nil
Expand Down
Loading

0 comments on commit a67195f

Please sign in to comment.