Skip to content

Commit c4975b6

Browse files
fix: cap diff depth and batch collab reads
Signed-off-by: huanghongbo-hhb <huanghongbo@koderover.com>
1 parent 9f6036e commit c4975b6

3 files changed

Lines changed: 61 additions & 22 deletions

File tree

pkg/microservice/aslan/core/release_plan/service/collaboration.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -444,23 +444,22 @@ func listActiveReleasePlanEditingSessions(planID string) ([]*ReleasePlanEditingS
444444
if err != nil {
445445
return nil, err
446446
}
447+
if len(sessionIDs) == 0 {
448+
return []*ReleasePlanEditingSession{}, nil
449+
}
447450

448-
resp := make([]*ReleasePlanEditingSession, 0, len(sessionIDs))
451+
keys := make([]string, 0, len(sessionIDs))
449452
for _, sessionID := range sessionIDs {
450-
value, err := redisCache.GetString(releasePlanCollabSessionKey(sessionID))
451-
if err != nil {
452-
continue
453-
}
454-
session := new(ReleasePlanEditingSession)
455-
if err := json.Unmarshal([]byte(value), session); err != nil {
456-
continue
457-
}
458-
if session.PlanID != planID {
459-
continue
460-
}
461-
resp = append(resp, session)
453+
keys = append(keys, releasePlanCollabSessionKey(sessionID))
454+
}
455+
456+
values, err := redisCache.MGet(keys)
457+
if err != nil {
458+
return nil, err
462459
}
463460

461+
resp := decodeReleasePlanEditingSessions(planID, values)
462+
464463
sort.Slice(resp, func(i, j int) bool {
465464
if resp[i].SectionKey == resp[j].SectionKey {
466465
return resp[i].EditingStartedAt < resp[j].EditingStartedAt
@@ -471,6 +470,25 @@ func listActiveReleasePlanEditingSessions(planID string) ([]*ReleasePlanEditingS
471470
return resp, nil
472471
}
473472

473+
func decodeReleasePlanEditingSessions(planID string, values []interface{}) []*ReleasePlanEditingSession {
474+
resp := make([]*ReleasePlanEditingSession, 0, len(values))
475+
for _, value := range values {
476+
raw, ok := value.(string)
477+
if !ok || raw == "" {
478+
continue
479+
}
480+
session := new(ReleasePlanEditingSession)
481+
if err := json.Unmarshal([]byte(raw), session); err != nil {
482+
continue
483+
}
484+
if session.PlanID != planID {
485+
continue
486+
}
487+
resp = append(resp, session)
488+
}
489+
return resp
490+
}
491+
474492
func persistReleasePlanEditingSession(session *ReleasePlanEditingSession) error {
475493
if session == nil {
476494
return errors.New("nil editing session")

pkg/microservice/aslan/core/release_plan/service/diff.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
const (
3737
releasePlanHashPruneMinMapKeys = 4
3838
releasePlanHashPruneMinArrayItems = 4
39+
releasePlanDiffMaxDepth = 50
3940
releasePlanDiffChangeTypeOrder = "order_changed"
4041
releasePlanDiffDisplayApprovalSpec = "approval_spec"
4142
releasePlanDiffDisplayWorkflowSpec = "workflow_spec"
@@ -368,6 +369,10 @@ func extractReleasePlanSectionSnapshot(snapshot interface{}, sectionKey string)
368369
}
369370

370371
func diffReleasePlanValues(ctx releasePlanDiffContext, path string, left, right interface{}, entries *[]*releasePlanRawDiffEntry) {
372+
diffReleasePlanValuesWithDepth(ctx, path, 0, left, right, entries)
373+
}
374+
375+
func diffReleasePlanValuesWithDepth(ctx releasePlanDiffContext, path string, depth int, left, right interface{}, entries *[]*releasePlanRawDiffEntry) {
371376
if shouldIgnoreReleasePlanDiffPath(path) {
372377
return
373378
}
@@ -380,6 +385,15 @@ func diffReleasePlanValues(ctx releasePlanDiffContext, path string, left, right
380385
return
381386
}
382387

388+
if depth >= releasePlanDiffMaxDepth {
389+
*entries = append(*entries, &releasePlanRawDiffEntry{
390+
Path: path,
391+
Before: left,
392+
After: right,
393+
})
394+
return
395+
}
396+
383397
leftMap, leftIsMap := left.(map[string]interface{})
384398
rightMap, rightIsMap := right.(map[string]interface{})
385399
if leftIsMap || rightIsMap {
@@ -397,15 +411,15 @@ func diffReleasePlanValues(ctx releasePlanDiffContext, path string, left, right
397411
sort.Strings(keys)
398412
for _, key := range keys {
399413
nextPath := joinReleasePlanDiffPath(path, key)
400-
diffReleasePlanValues(ctx, nextPath, leftMap[key], rightMap[key], entries)
414+
diffReleasePlanValuesWithDepth(ctx, nextPath, depth+1, leftMap[key], rightMap[key], entries)
401415
}
402416
return
403417
}
404418

405419
leftList, leftIsList := left.([]interface{})
406420
rightList, rightIsList := right.([]interface{})
407421
if leftIsList || rightIsList {
408-
diffReleasePlanArray(ctx, path, leftList, rightList, entries)
422+
diffReleasePlanArray(ctx, path, depth, leftList, rightList, entries)
409423
return
410424
}
411425

@@ -460,17 +474,17 @@ func hashReleasePlanSubtree(value interface{}) (string, error) {
460474
return hex.EncodeToString(sum[:]), nil
461475
}
462476

463-
func diffReleasePlanArray(ctx releasePlanDiffContext, path string, left, right []interface{}, entries *[]*releasePlanRawDiffEntry) {
477+
func diffReleasePlanArray(ctx releasePlanDiffContext, path string, depth int, left, right []interface{}, entries *[]*releasePlanRawDiffEntry) {
464478
rule := matchReleasePlanArrayDiffRule(ctx, path)
465479
if rule == nil || rule.Strategy == releasePlanArrayDiffStrategyIndex {
466-
diffReleasePlanArrayByIndex(ctx, path, left, right, entries)
480+
diffReleasePlanArrayByIndex(ctx, path, depth, left, right, entries)
467481
return
468482
}
469483

470484
leftMap, leftOrdered, leftMapped := buildReleasePlanArrayMap(left, rule.BuildKey)
471485
rightMap, rightOrdered, rightMapped := buildReleasePlanArrayMap(right, rule.BuildKey)
472486
if !leftMapped || !rightMapped {
473-
diffReleasePlanArrayByIndex(ctx, path, left, right, entries)
487+
diffReleasePlanArrayByIndex(ctx, path, depth, left, right, entries)
474488
return
475489
}
476490

@@ -500,12 +514,12 @@ func diffReleasePlanArray(ctx releasePlanDiffContext, path string, left, right [
500514
continue
501515
}
502516
nextPath := fmt.Sprintf("%s[%s]", path, key)
503-
diffReleasePlanValues(ctx, nextPath, leftMap[key], rightMap[key], entries)
517+
diffReleasePlanValuesWithDepth(ctx, nextPath, depth+1, leftMap[key], rightMap[key], entries)
504518
}
505519
return
506520
}
507521

508-
diffReleasePlanArrayByIndex(ctx, path, left, right, entries)
522+
diffReleasePlanArrayByIndex(ctx, path, depth, left, right, entries)
509523
}
510524

511525
func shouldSkipReleasePlanWorkflowTaskPresenceChange(path string, left, right interface{}) bool {
@@ -516,7 +530,7 @@ func shouldSkipReleasePlanWorkflowTaskPresenceChange(path string, left, right in
516530
return normalizedPath == "spec.workflow.jobs" || strings.HasSuffix(normalizedPath, ".spec.workflow.jobs")
517531
}
518532

519-
func diffReleasePlanArrayByIndex(ctx releasePlanDiffContext, path string, left, right []interface{}, entries *[]*releasePlanRawDiffEntry) {
533+
func diffReleasePlanArrayByIndex(ctx releasePlanDiffContext, path string, depth int, left, right []interface{}, entries *[]*releasePlanRawDiffEntry) {
520534
maxLen := len(left)
521535
if len(right) > maxLen {
522536
maxLen = len(right)
@@ -530,7 +544,7 @@ func diffReleasePlanArrayByIndex(ctx releasePlanDiffContext, path string, left,
530544
if i < len(right) {
531545
rightVal = right[i]
532546
}
533-
diffReleasePlanValues(ctx, nextPath, leftVal, rightVal, entries)
547+
diffReleasePlanValuesWithDepth(ctx, nextPath, depth+1, leftVal, rightVal, entries)
534548
}
535549
}
536550

pkg/tool/cache/redis_cache.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ func (c *RedisCache) GetString(key string) (string, error) {
9191
return c.redisClient.Get(context.TODO(), key).Result()
9292
}
9393

94+
func (c *RedisCache) MGet(keys []string) ([]interface{}, error) {
95+
if len(keys) == 0 {
96+
return []interface{}{}, nil
97+
}
98+
return c.redisClient.MGet(context.TODO(), keys...).Result()
99+
}
100+
94101
func (c *RedisCache) HGetString(key, field string) (string, error) {
95102
return c.redisClient.HGet(context.TODO(), key, field).Result()
96103
}

0 commit comments

Comments
 (0)