Skip to content

Commit

Permalink
feat: add logger.CoreLogger to searcher plugin (#2232)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <[email protected]>
  • Loading branch information
gaius-qi authored Mar 30, 2023
1 parent 0d4ff92 commit 3fbeac2
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion manager/rpcserver/manager_server_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func (s *managerServerV1) ListSchedulers(ctx context.Context, req *managerv1.Lis
candidateSchedulerClusters []models.SchedulerCluster
err error
)
candidateSchedulerClusters, err = s.searcher.FindSchedulerClusters(ctx, schedulerClusters, req.Hostname, req.Ip, req.HostInfo, log)
candidateSchedulerClusters, err = s.searcher.FindSchedulerClusters(ctx, schedulerClusters, req.Hostname, req.Ip, req.HostInfo, logger.CoreLogger)
if err != nil {
log.Error(err)
metrics.SearchSchedulerClusterFailureCount.WithLabelValues(req.Version, req.Commit).Inc()
Expand Down
2 changes: 1 addition & 1 deletion manager/rpcserver/manager_server_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ func (s *managerServerV2) ListSchedulers(ctx context.Context, req *managerv2.Lis
candidateSchedulerClusters []models.SchedulerCluster
err error
)
candidateSchedulerClusters, err = s.searcher.FindSchedulerClusters(ctx, schedulerClusters, req.Hostname, req.Ip, req.HostInfo, log)
candidateSchedulerClusters, err = s.searcher.FindSchedulerClusters(ctx, schedulerClusters, req.Hostname, req.Ip, req.HostInfo, logger.CoreLogger)
if err != nil {
log.Error(err)
metrics.SearchSchedulerClusterFailureCount.WithLabelValues(req.Version, req.Commit).Inc()
Expand Down
4 changes: 2 additions & 2 deletions manager/searcher/mocks/searcher_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions manager/searcher/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/mitchellh/mapstructure"
"github.com/yl2chen/cidranger"
"go.uber.org/zap"

logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/manager/models"
Expand Down Expand Up @@ -86,12 +87,10 @@ type Scopes struct {
type Searcher interface {
// FindSchedulerClusters finds scheduler clusters that best matches the evaluation.
FindSchedulerClusters(ctx context.Context, schedulerClusters []models.SchedulerCluster, ip, hostname string,
conditions map[string]string, log *logger.SugaredLoggerOnWith) ([]models.SchedulerCluster, error)
conditions map[string]string, log *zap.SugaredLogger) ([]models.SchedulerCluster, error)
}

type searcher struct {
cidrs []string
}
type searcher struct{}

func New(pluginDir string) Searcher {
s, err := LoadPlugin(pluginDir)
Expand All @@ -106,7 +105,9 @@ func New(pluginDir string) Searcher {

// FindSchedulerClusters finds scheduler clusters that best matches the evaluation.
func (s *searcher) FindSchedulerClusters(ctx context.Context, schedulerClusters []models.SchedulerCluster, ip, hostname string,
conditions map[string]string, log *logger.SugaredLoggerOnWith) ([]models.SchedulerCluster, error) {
conditions map[string]string, log *zap.SugaredLogger) ([]models.SchedulerCluster, error) {
log = log.With("ip", ip, "hostname", hostname, "conditions", conditions)

if len(schedulerClusters) <= 0 {
return nil, errors.New("empty scheduler clusters")
}
Expand Down Expand Up @@ -179,7 +180,7 @@ func FilterSchedulerClusters(conditions map[string]string, schedulerClusters []m
}

// Evaluate the degree of matching between scheduler cluster and dfdaemon.
func Evaluate(ip, hostname string, conditions map[string]string, scopes Scopes, cluster models.SchedulerCluster, log *logger.SugaredLoggerOnWith) float64 {
func Evaluate(ip, hostname string, conditions map[string]string, scopes Scopes, cluster models.SchedulerCluster, log *zap.SugaredLogger) float64 {
return securityDomainAffinityWeight*calculateSecurityDomainAffinityScore(conditions[ConditionSecurityDomain], cluster.SecurityGroup.SecurityRules) +
cidrAffinityWeight*calculateCIDRAffinityScore(ip, scopes.CIDRs, log) +
idcAffinityWeight*calculateIDCAffinityScore(conditions[ConditionIDC], scopes.IDC) +
Expand All @@ -201,7 +202,7 @@ func calculateSecurityDomainAffinityScore(securityDomain string, securityRules [
}

// calculateCIDRAffinityScore 0.0~1.0 larger and better.
func calculateCIDRAffinityScore(ip string, cidrs []string, log *logger.SugaredLoggerOnWith) float64 {
func calculateCIDRAffinityScore(ip string, cidrs []string, log *zap.SugaredLogger) float64 {
// Construct CIDR ranger.
ranger := cidranger.NewPCTrieRanger()
for _, cidr := range cidrs {
Expand Down
2 changes: 1 addition & 1 deletion manager/searcher/searcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ func TestSearcher_FindSchedulerClusters(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
searcher := New(pluginDir)
clusters, found := searcher.FindSchedulerClusters(context.Background(), tc.schedulerClusters, "128.168.1.0", "foo", tc.conditions, &logger.SugaredLoggerOnWith{})
clusters, found := searcher.FindSchedulerClusters(context.Background(), tc.schedulerClusters, "128.168.1.0", "foo", tc.conditions, logger.CoreLogger)
tc.expect(t, clusters, found)
})
}
Expand Down
2 changes: 1 addition & 1 deletion manager/searcher/testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func main() {
os.Exit(1)
}

clusters, err := s.FindSchedulerClusters(context.Background(), []models.SchedulerCluster{}, "127.0.0.1", "foo", map[string]string{}, &logger.SugaredLoggerOnWith{})
clusters, err := s.FindSchedulerClusters(context.Background(), []models.SchedulerCluster{}, "127.0.0.1", "foo", map[string]string{}, logger.CoreLogger)
if err != nil {
fmt.Println("scheduler cluster not found")
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions manager/searcher/testdata/plugin/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ package main
import (
"context"

logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/manager/models"
"go.uber.org/zap"
)

type searcher struct{}

func (s *searcher) FindSchedulerClusters(ctx context.Context, schedulerClusters []models.SchedulerCluster, hostname, ip string,
conditions map[string]string, log *logger.SugaredLoggerOnWith) ([]models.SchedulerCluster, error) {
conditions map[string]string, log *zap.SugaredLogger) ([]models.SchedulerCluster, error) {
return []models.SchedulerCluster{{Name: "foo"}}, nil
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package log
import (
"go.uber.org/zap/zapcore"

"d7y.io/dragonfly/v2/internal/dflog"
logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/pkg/dfpath"
)

Expand All @@ -35,7 +35,6 @@ func SetGrpcLevel(level zapcore.Level) {

// SetupDaemon sets daemon log config: path, console
func SetupDaemon(logDir string, verbose bool, console bool) error {

var options []dfpath.Option
if logDir != "" {
options = append(options, dfpath.WithLogDir(logDir))
Expand Down

0 comments on commit 3fbeac2

Please sign in to comment.