Skip to content

Commit

Permalink
feat: add log to searcher plugin (#2231)
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 35f4a4f commit 0d4ff92
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 20 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)
candidateSchedulerClusters, err = s.searcher.FindSchedulerClusters(ctx, schedulerClusters, req.Hostname, req.Ip, req.HostInfo, log)
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)
candidateSchedulerClusters, err = s.searcher.FindSchedulerClusters(ctx, schedulerClusters, req.Hostname, req.Ip, req.HostInfo, log)
if err != nil {
log.Error(err)
metrics.SearchSchedulerClusterFailureCount.WithLabelValues(req.Version, req.Commit).Inc()
Expand Down
9 changes: 5 additions & 4 deletions manager/searcher/mocks/searcher_mock.go

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

24 changes: 13 additions & 11 deletions manager/searcher/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ 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) ([]models.SchedulerCluster, error)
FindSchedulerClusters(ctx context.Context, schedulerClusters []models.SchedulerCluster, ip, hostname string,
conditions map[string]string, log *logger.SugaredLoggerOnWith) ([]models.SchedulerCluster, error)
}

type searcher struct {
Expand All @@ -104,7 +105,8 @@ 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) ([]models.SchedulerCluster, error) {
func (s *searcher) FindSchedulerClusters(ctx context.Context, schedulerClusters []models.SchedulerCluster, ip, hostname string,
conditions map[string]string, log *logger.SugaredLoggerOnWith) ([]models.SchedulerCluster, error) {
if len(schedulerClusters) <= 0 {
return nil, errors.New("empty scheduler clusters")
}
Expand All @@ -119,16 +121,16 @@ func (s *searcher) FindSchedulerClusters(ctx context.Context, schedulerClusters
func(i, j int) bool {
var si, sj Scopes
if err := mapstructure.Decode(clusters[i].Scopes, &si); err != nil {
logger.Errorf("cluster %s decode scopes failed: %v", clusters[i].Name, err)
log.Errorf("cluster %s decode scopes failed: %v", clusters[i].Name, err)
return false
}

if err := mapstructure.Decode(clusters[j].Scopes, &sj); err != nil {
logger.Errorf("cluster %s decode scopes failed: %v", clusters[i].Name, err)
log.Errorf("cluster %s decode scopes failed: %v", clusters[i].Name, err)
return false
}

return Evaluate(ip, hostname, conditions, si, clusters[i]) > Evaluate(ip, hostname, conditions, sj, clusters[j])
return Evaluate(ip, hostname, conditions, si, clusters[i], log) > Evaluate(ip, hostname, conditions, sj, clusters[j], log)
},
)

Expand Down Expand Up @@ -177,9 +179,9 @@ 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) float64 {
func Evaluate(ip, hostname string, conditions map[string]string, scopes Scopes, cluster models.SchedulerCluster, log *logger.SugaredLoggerOnWith) float64 {
return securityDomainAffinityWeight*calculateSecurityDomainAffinityScore(conditions[ConditionSecurityDomain], cluster.SecurityGroup.SecurityRules) +
cidrAffinityWeight*calculateCIDRAffinityScore(ip, scopes.CIDRs) +
cidrAffinityWeight*calculateCIDRAffinityScore(ip, scopes.CIDRs, log) +
idcAffinityWeight*calculateIDCAffinityScore(conditions[ConditionIDC], scopes.IDC) +
locationAffinityWeight*calculateMultiElementAffinityScore(conditions[ConditionLocation], scopes.Location) +
clusterTypeWeight*calculateClusterTypeScore(cluster)
Expand All @@ -199,26 +201,26 @@ func calculateSecurityDomainAffinityScore(securityDomain string, securityRules [
}

// calculateCIDRAffinityScore 0.0~1.0 larger and better.
func calculateCIDRAffinityScore(ip string, cidrs []string) float64 {
func calculateCIDRAffinityScore(ip string, cidrs []string, log *logger.SugaredLoggerOnWith) float64 {
// Construct CIDR ranger.
ranger := cidranger.NewPCTrieRanger()
for _, cidr := range cidrs {
_, network, err := net.ParseCIDR(cidr)
if err != nil {
logger.Error(err)
log.Error(err)
continue
}

if err := ranger.Insert(cidranger.NewBasicRangerEntry(*network)); err != nil {
logger.Error(err)
log.Error(err)
continue
}
}

// Determine whether an IP is contained in the constructed networks ranger.
contains, err := ranger.Contains(net.ParseIP(ip))
if err != nil {
logger.Error(err)
log.Error(err)
return minScore
}

Expand Down
3 changes: 2 additions & 1 deletion manager/searcher/searcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/stretchr/testify/assert"

logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/manager/models"
)

Expand Down Expand Up @@ -751,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)
clusters, found := searcher.FindSchedulerClusters(context.Background(), tc.schedulerClusters, "128.168.1.0", "foo", tc.conditions, &logger.SugaredLoggerOnWith{})
tc.expect(t, clusters, found)
})
}
Expand Down
3 changes: 2 additions & 1 deletion manager/searcher/testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"

logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/manager/models"
"d7y.io/dragonfly/v2/manager/searcher"
)
Expand All @@ -32,7 +33,7 @@ func main() {
os.Exit(1)
}

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

logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/manager/models"
)

type searcher struct{}

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

Expand Down

0 comments on commit 0d4ff92

Please sign in to comment.