Skip to content

Commit

Permalink
enhance: add the config to control the way when fail to init plugin (#…
Browse files Browse the repository at this point in the history
…32680)

issue: #32679

Signed-off-by: SimFG <[email protected]>
  • Loading branch information
SimFG committed May 7, 2024
1 parent 7da1ca9 commit 0ea08b0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/util/hookutil/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ func InitOnceHook() {
initOnce.Do(func() {
err := initHook()
if err != nil {
log.Warn("fail to init hook",
logFunc := log.Warn
if paramtable.Get().CommonCfg.PanicWhenPluginFail.GetAsBool() {
logFunc = log.Panic
}
logFunc("fail to init hook",
zap.String("so_path", paramtable.Get().ProxyCfg.SoPath.GetValue()),
zap.Error(err))
}
Expand Down
29 changes: 29 additions & 0 deletions internal/util/hookutil/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package hookutil

import (
"sync"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -39,6 +40,34 @@ func TestInitHook(t *testing.T) {
paramtable.Get().Save(Params.ProxyCfg.SoPath.Key, "")
}

func TestHookInitPanicError(t *testing.T) {
paramtable.Init()
p := paramtable.Get()
p.Save(p.ProxyCfg.SoPath.Key, "/a/b/hook.so")
defer p.Reset(p.ProxyCfg.SoPath.Key)
err := initHook()
assert.Error(t, err)
assert.Panics(t, func() {
initOnce = sync.Once{}
InitOnceHook()
})
}

func TestHookInitLogError(t *testing.T) {
paramtable.Init()
p := paramtable.Get()
p.Save(p.ProxyCfg.SoPath.Key, "/a/b/hook.so")
defer p.Reset(p.ProxyCfg.SoPath.Key)
p.Save(p.CommonCfg.PanicWhenPluginFail.Key, "false")
defer p.Reset(p.CommonCfg.PanicWhenPluginFail.Key)
err := initHook()
assert.Error(t, err)
assert.NotPanics(t, func() {
initOnce = sync.Once{}
InitOnceHook()
})
}

func TestDefaultHook(t *testing.T) {
d := &DefaultHook{}
assert.NoError(t, d.Init(nil))
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/paramtable/component_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ type commonConfig struct {
TraceLogMode ParamItem `refreshable:"true"`
BloomFilterSize ParamItem `refreshable:"true"`
MaxBloomFalsePositive ParamItem `refreshable:"true"`
PanicWhenPluginFail ParamItem `refreshable:"false"`
}

func (p *commonConfig) init(base *BaseTable) {
Expand Down Expand Up @@ -723,6 +724,14 @@ like the old password verification when updating the credential`,
Export: true,
}
p.MaxBloomFalsePositive.Init(base.mgr)

p.PanicWhenPluginFail = ParamItem{
Key: "common.panicWhenPluginFail",
Version: "2.4.2",
DefaultValue: "true",
Doc: "panic or not when plugin fail to init",
}
p.PanicWhenPluginFail.Init(base.mgr)
}

type gpuConfig struct {
Expand Down

0 comments on commit 0ea08b0

Please sign in to comment.