add pull update control env (#16051)

These variables are temporary solution for issue: https://github.com/goharbor/harbor/issues/16039
When user disable the pull count/time/audit log, it will decrease the database access, especially in large concurrency pull scenarios.

1, PULL_TIME_UPDATE_DISABLE : The flag to indicate if pull time is disable for pull request.
2, PULL_COUNT_UPDATE_DISABLE : The flag to indicate if pull count is disable for pull request.
3, pull audit log will not create on disabling pull time.

Signed-off-by: Wang Yan <wangyan@vmware.com>
This commit is contained in:
Wang Yan 2021-11-26 18:13:23 +08:00 committed by GitHub
parent cad78f6af4
commit 7608df4b5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 10 deletions

View File

@ -186,4 +186,14 @@ const (
TraceOtelCompression = "trace_otel_compression"
TraceOtelInsecure = "trace_otel_insecure"
TraceOtelTimeout = "trace_otel_timeout"
// These variables are temporary solution for issue: https://github.com/goharbor/harbor/issues/16039
// When user disable the pull count/time/audit log, it will decrease the database access, especially in large concurrency pull scenarios.
// TODO: Once we have a complete solution, delete these variables.
// PullCountUpdateDisable indicate if pull count is disable for pull request.
PullCountUpdateDisable = "pull_count_update_disable"
// PullTimeUpdateDisable indicate if pull time is disable for pull request.
PullTimeUpdateDisable = "pull_time_update_disable"
// PullAuditLogDisable indicate if pull audit log is disable for pull request.
PullAuditLogDisable = "pull_audit_log_disable"
)

View File

@ -17,6 +17,7 @@ package auditlog
import (
"context"
"github.com/goharbor/harbor/src/controller/event"
"github.com/goharbor/harbor/src/lib/config"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/audit"
am "github.com/goharbor/harbor/src/pkg/audit/model"
@ -39,10 +40,19 @@ func (h *Handler) Name() string {
// Handle ...
func (h *Handler) Handle(ctx context.Context, value interface{}) error {
var auditLog *am.AuditLog
var addAuditLog bool
switch v := value.(type) {
case *event.PushArtifactEvent, *event.PullArtifactEvent, *event.DeleteArtifactEvent,
case *event.PushArtifactEvent, *event.DeleteArtifactEvent,
*event.DeleteRepositoryEvent, *event.CreateProjectEvent, *event.DeleteProjectEvent,
*event.DeleteTagEvent, *event.CreateTagEvent:
addAuditLog = true
case *event.PullArtifactEvent:
addAuditLog = !config.PullAuditLogDisable(ctx)
default:
log.Errorf("Can not handler this event type! %#v", v)
}
if addAuditLog {
resolver := value.(AuditResolver)
al, err := resolver.ResolveToAuditLog()
if err != nil {
@ -50,13 +60,11 @@ func (h *Handler) Handle(ctx context.Context, value interface{}) error {
return err
}
auditLog = al
default:
log.Errorf("Can not handler this event type! %#v", v)
}
if auditLog != nil {
_, err := audit.Mgr.Create(ctx, auditLog)
if err != nil {
log.Debugf("add audit log err: %v", err)
if auditLog != nil {
_, err := audit.Mgr.Create(ctx, auditLog)
if err != nil {
log.Debugf("add audit log err: %v", err)
}
}
}
return nil

View File

@ -16,6 +16,7 @@ package internal
import (
"context"
"github.com/goharbor/harbor/src/lib/config"
"time"
"github.com/goharbor/harbor/src/controller/artifact"
@ -54,8 +55,12 @@ func (a *Handler) IsStateful() bool {
}
func (a *Handler) onPull(ctx context.Context, event *event.ArtifactEvent) error {
go func() { a.updatePullTime(ctx, event) }()
go func() { a.addPullCount(ctx, event) }()
if !config.PullTimeUpdateDisable(ctx) {
go func() { a.updatePullTime(ctx, event) }()
}
if !config.PullCountUpdateDisable(ctx) {
go func() { a.addPullCount(ctx, event) }()
}
return nil
}

View File

@ -179,5 +179,9 @@ var (
{Name: common.TraceOtelCompression, Scope: SystemScope, Group: BasicGroup, EnvKey: "TRACE_OTEL_COMPRESSION", DefaultValue: "", ItemType: &BoolType{}, Editable: false, Description: `The compression of the Otel`},
{Name: common.TraceOtelInsecure, Scope: SystemScope, Group: BasicGroup, EnvKey: "TRACE_OTEL_INSECURE", DefaultValue: "", ItemType: &BoolType{}, Editable: false, Description: `The insecure of the Otel`},
{Name: common.TraceOtelTimeout, Scope: SystemScope, Group: BasicGroup, EnvKey: "TRACE_OTEL_TIMEOUT", DefaultValue: "", ItemType: &IntType{}, Editable: false, Description: `The timeout of the Otel`},
{Name: common.PullTimeUpdateDisable, Scope: UserScope, Group: BasicGroup, EnvKey: "PULL_TIME_UPDATE_DISABLE", DefaultValue: "false", ItemType: &BoolType{}, Editable: false, Description: `The flag to indicate if pull time is disable for pull request.`},
{Name: common.PullCountUpdateDisable, Scope: UserScope, Group: BasicGroup, EnvKey: "PULL_COUNT_UPDATE_DISABLE", DefaultValue: "false", ItemType: &BoolType{}, Editable: false, Description: `The flag to indicate if pull count is disable for pull request.`},
{Name: common.PullAuditLogDisable, Scope: UserScope, Group: BasicGroup, EnvKey: "PULL_AUDIT_LOG_DISABLE", DefaultValue: "false", ItemType: &BoolType{}, Editable: false, Description: `The flag to indicate if pull audit log is disable for pull request.`},
}
)

View File

@ -226,3 +226,18 @@ func SplitAndTrim(s, sep string) []string {
}
return res
}
// PullCountUpdateDisable returns a bool to indicate if pull count is disable for pull request.
func PullCountUpdateDisable(ctx context.Context) bool {
return defaultMgr().Get(ctx, common.PullCountUpdateDisable).GetBool()
}
// PullTimeUpdateDisable returns a bool to indicate if pull time is disable for pull request.
func PullTimeUpdateDisable(ctx context.Context) bool {
return defaultMgr().Get(ctx, common.PullTimeUpdateDisable).GetBool()
}
// PullAuditLogDisable returns a bool to indicate if pull audit log is disable for pull request.
func PullAuditLogDisable(ctx context.Context) bool {
return defaultMgr().Get(ctx, common.PullAuditLogDisable).GetBool()
}