2022-09-23 09:50:52 +08:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-14 19:59:24 +08:00
|
|
|
"fmt"
|
2022-09-23 09:50:52 +08:00
|
|
|
"plugin"
|
2023-06-25 17:20:43 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/hook"
|
2023-08-16 16:03:33 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/config"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
|
|
|
"github.com/milvus-io/milvus/pkg/metrics"
|
2023-08-16 16:03:33 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
2022-09-23 09:50:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type defaultHook struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d defaultHook) Init(params map[string]string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-24 09:56:51 +08:00
|
|
|
func (d defaultHook) Mock(ctx context.Context, req interface{}, fullMethod string) (bool, interface{}, error) {
|
2022-09-23 09:50:52 +08:00
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
2022-09-28 13:26:54 +08:00
|
|
|
func (d defaultHook) Before(ctx context.Context, req interface{}, fullMethod string) (context.Context, error) {
|
|
|
|
return ctx, nil
|
2022-09-23 09:50:52 +08:00
|
|
|
}
|
|
|
|
|
2022-09-24 09:56:51 +08:00
|
|
|
func (d defaultHook) After(ctx context.Context, result interface{}, err error, fullMethod string) error {
|
2022-09-23 09:50:52 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d defaultHook) Release() {}
|
|
|
|
|
|
|
|
var hoo hook.Hook
|
|
|
|
|
2022-10-14 19:59:24 +08:00
|
|
|
func initHook() error {
|
2022-12-07 18:01:19 +08:00
|
|
|
path := Params.ProxyCfg.SoPath.GetValue()
|
2022-09-23 09:50:52 +08:00
|
|
|
if path == "" {
|
|
|
|
hoo = defaultHook{}
|
2022-10-14 19:59:24 +08:00
|
|
|
return nil
|
2022-09-23 09:50:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug("start to load plugin", zap.String("path", path))
|
|
|
|
p, err := plugin.Open(path)
|
|
|
|
if err != nil {
|
2022-10-14 19:59:24 +08:00
|
|
|
return fmt.Errorf("fail to open the plugin, error: %s", err.Error())
|
2022-09-23 09:50:52 +08:00
|
|
|
}
|
|
|
|
logger.Debug("plugin open")
|
|
|
|
|
|
|
|
h, err := p.Lookup("MilvusHook")
|
|
|
|
if err != nil {
|
2022-10-14 19:59:24 +08:00
|
|
|
return fmt.Errorf("fail to the 'MilvusHook' object in the plugin, error: %s", err.Error())
|
2022-09-23 09:50:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
hoo, ok = h.(hook.Hook)
|
|
|
|
if !ok {
|
2022-10-14 19:59:24 +08:00
|
|
|
return fmt.Errorf("fail to convert the `Hook` interface")
|
2022-09-23 09:50:52 +08:00
|
|
|
}
|
2023-09-05 10:31:48 +08:00
|
|
|
if err = hoo.Init(paramtable.GetHookParams().SoConfig.GetValue()); err != nil {
|
2022-10-14 19:59:24 +08:00
|
|
|
return fmt.Errorf("fail to init configs for the hook, error: %s", err.Error())
|
2022-09-23 09:50:52 +08:00
|
|
|
}
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.GetHookParams().WatchHookWithPrefix("watch_hook", "", func(event *config.Event) {
|
2023-08-16 16:03:33 +08:00
|
|
|
log.Info("receive the hook refresh event", zap.Any("event", event))
|
|
|
|
go func() {
|
2023-09-05 10:31:48 +08:00
|
|
|
soConfig := paramtable.GetHookParams().SoConfig.GetValue()
|
2023-08-16 16:03:33 +08:00
|
|
|
log.Info("refresh hook configs", zap.Any("config", soConfig))
|
|
|
|
if err = hoo.Init(soConfig); err != nil {
|
|
|
|
log.Panic("fail to init configs for the hook when refreshing", zap.Error(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
})
|
2022-10-14 19:59:24 +08:00
|
|
|
return nil
|
2022-09-23 09:50:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func UnaryServerHookInterceptor() grpc.UnaryServerInterceptor {
|
2022-10-14 19:59:24 +08:00
|
|
|
if hookError := initHook(); hookError != nil {
|
2022-12-07 18:01:19 +08:00
|
|
|
logger.Error("hook error", zap.String("path", Params.ProxyCfg.SoPath.GetValue()), zap.Error(hookError))
|
2022-10-14 19:59:24 +08:00
|
|
|
hoo = defaultHook{}
|
|
|
|
}
|
2022-09-23 09:50:52 +08:00
|
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
|
|
|
var (
|
|
|
|
fullMethod = info.FullMethod
|
2022-09-28 13:26:54 +08:00
|
|
|
newCtx context.Context
|
2022-09-23 09:50:52 +08:00
|
|
|
isMock bool
|
|
|
|
mockResp interface{}
|
|
|
|
realResp interface{}
|
|
|
|
realErr error
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2022-09-24 09:56:51 +08:00
|
|
|
if isMock, mockResp, err = hoo.Mock(ctx, req, fullMethod); isMock {
|
2023-02-13 16:50:33 +08:00
|
|
|
log.Info("hook mock", zap.String("user", getCurrentUser(ctx)),
|
|
|
|
zap.String("full method", fullMethod), zap.Error(err))
|
|
|
|
metrics.ProxyHookFunc.WithLabelValues(metrics.HookMock, fullMethod).Inc()
|
2023-06-25 17:20:43 +08:00
|
|
|
updateProxyFunctionCallMetric(fullMethod)
|
2022-09-23 09:50:52 +08:00
|
|
|
return mockResp, err
|
|
|
|
}
|
|
|
|
|
2022-09-28 13:26:54 +08:00
|
|
|
if newCtx, err = hoo.Before(ctx, req, fullMethod); err != nil {
|
2023-02-13 16:50:33 +08:00
|
|
|
log.Warn("hook before error", zap.String("user", getCurrentUser(ctx)), zap.String("full method", fullMethod),
|
|
|
|
zap.Any("request", req), zap.Error(err))
|
|
|
|
metrics.ProxyHookFunc.WithLabelValues(metrics.HookBefore, fullMethod).Inc()
|
2023-06-25 17:20:43 +08:00
|
|
|
updateProxyFunctionCallMetric(fullMethod)
|
2022-09-23 09:50:52 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-28 13:26:54 +08:00
|
|
|
realResp, realErr = handler(newCtx, req)
|
|
|
|
if err = hoo.After(newCtx, realResp, realErr, fullMethod); err != nil {
|
2023-02-13 16:50:33 +08:00
|
|
|
log.Warn("hook after error", zap.String("user", getCurrentUser(ctx)), zap.String("full method", fullMethod),
|
|
|
|
zap.Any("request", req), zap.Error(err))
|
|
|
|
metrics.ProxyHookFunc.WithLabelValues(metrics.HookAfter, fullMethod).Inc()
|
2023-06-25 17:20:43 +08:00
|
|
|
updateProxyFunctionCallMetric(fullMethod)
|
2022-09-23 09:50:52 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return realResp, realErr
|
|
|
|
}
|
|
|
|
}
|
2023-02-13 16:50:33 +08:00
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
func updateProxyFunctionCallMetric(fullMethod string) {
|
|
|
|
if fullMethod == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
method := strings.Split(fullMethod, "/")[0]
|
|
|
|
metrics.ProxyFunctionCall.WithLabelValues(strconv.FormatInt(paramtable.GetNodeID(), 10), method, metrics.TotalLabel).Inc()
|
|
|
|
metrics.ProxyFunctionCall.WithLabelValues(strconv.FormatInt(paramtable.GetNodeID(), 10), method, metrics.FailLabel).Inc()
|
|
|
|
}
|
|
|
|
|
2023-02-13 16:50:33 +08:00
|
|
|
func getCurrentUser(ctx context.Context) string {
|
|
|
|
username, err := GetCurUserFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("fail to get current user", zap.Error(err))
|
|
|
|
}
|
|
|
|
return username
|
|
|
|
}
|