This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
random_cluster_selector.go
185 lines (168 loc) · 6.47 KB
/
random_cluster_selector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package impl
import (
"context"
"fmt"
"hash/fnv"
"math/rand"
"github.com/prometheus/client_golang/prometheus"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flyteadmin/pkg/errors"
"github.com/flyteorg/flyteadmin/pkg/manager/impl/resources"
managerInterfaces "github.com/flyteorg/flyteadmin/pkg/manager/interfaces"
"github.com/flyteorg/flyteadmin/pkg/repositories"
"google.golang.org/grpc/codes"
"github.com/flyteorg/flyteadmin/pkg/executioncluster"
"github.com/flyteorg/flyteadmin/pkg/executioncluster/interfaces"
"github.com/flyteorg/flytestdlib/random"
runtime "github.com/flyteorg/flyteadmin/pkg/runtime/interfaces"
)
// Implementation of Random cluster selector
// Selects cluster based on weights and domains.
type RandomClusterSelector struct {
equalWeightedAllClusters random.WeightedRandomList
labelWeightedRandomMap map[string]random.WeightedRandomList
executionTargetMap map[string]executioncluster.ExecutionTarget
resourceManager managerInterfaces.ResourceInterface
}
func getRandSource(seed string) (rand.Source, error) {
h := fnv.New64a()
_, err := h.Write([]byte(seed))
if err != nil {
return nil, err
}
hashedSeed := int64(h.Sum64())
return rand.NewSource(hashedSeed), nil
}
func getExecutionTargets(ctx context.Context, initializationErrorCounter prometheus.Counter, executionTargetProvider interfaces.ExecutionTargetProvider,
clusterConfig runtime.ClusterConfiguration) (random.WeightedRandomList, map[string]executioncluster.ExecutionTarget, error) {
executionTargetMap := make(map[string]executioncluster.ExecutionTarget)
entries := make([]random.Entry, 0)
for _, cluster := range clusterConfig.GetClusterConfigs() {
if _, ok := executionTargetMap[cluster.Name]; ok {
return nil, nil, fmt.Errorf("duplicate clusters for name %s", cluster.Name)
}
executionTarget, err := executionTargetProvider.GetExecutionTarget(initializationErrorCounter, cluster)
if err != nil {
return nil, nil, err
}
executionTargetMap[cluster.Name] = *executionTarget
if executionTarget.Enabled {
targetEntry := random.Entry{
Item: *executionTarget,
}
entries = append(entries, targetEntry)
}
}
weightedRandomList, err := random.NewWeightedRandom(ctx, entries)
if err != nil {
return nil, nil, err
}
return weightedRandomList, executionTargetMap, nil
}
func getLabeledWeightedRandomForCluster(ctx context.Context,
clusterConfig runtime.ClusterConfiguration, executionTargetMap map[string]executioncluster.ExecutionTarget) (map[string]random.WeightedRandomList, error) {
labeledWeightedRandomMap := make(map[string]random.WeightedRandomList)
for label, clusterEntities := range clusterConfig.GetLabelClusterMap() {
entries := make([]random.Entry, 0)
for _, clusterEntity := range clusterEntities {
cluster := executionTargetMap[clusterEntity.ID]
// If cluster is not enabled, it is not eligible for selection
if !cluster.Enabled {
continue
}
targetEntry := random.Entry{
Item: cluster,
Weight: clusterEntity.Weight,
}
entries = append(entries, targetEntry)
}
if len(entries) > 0 {
weightedRandomList, err := random.NewWeightedRandom(ctx, entries)
if err != nil {
return nil, err
}
labeledWeightedRandomMap[label] = weightedRandomList
}
}
return labeledWeightedRandomMap, nil
}
func (s RandomClusterSelector) GetAllValidTargets() []executioncluster.ExecutionTarget {
v := make([]executioncluster.ExecutionTarget, 0)
for _, value := range s.executionTargetMap {
if value.Enabled {
v = append(v, value)
}
}
return v
}
func (s RandomClusterSelector) GetTarget(ctx context.Context, spec *executioncluster.ExecutionTargetSpec) (*executioncluster.ExecutionTarget, error) {
if spec == nil {
return nil, fmt.Errorf("empty executionTargetSpec")
}
if spec.TargetID != "" {
if val, ok := s.executionTargetMap[spec.TargetID]; ok {
return &val, nil
}
return nil, fmt.Errorf("invalid cluster target %s", spec.TargetID)
}
resource, err := s.resourceManager.GetResource(ctx, managerInterfaces.ResourceRequest{
Project: spec.Project,
Domain: spec.Domain,
Workflow: spec.Workflow,
LaunchPlan: spec.LaunchPlan,
ResourceType: admin.MatchableResource_EXECUTION_CLUSTER_LABEL,
})
if err != nil {
if flyteAdminError, ok := err.(errors.FlyteAdminError); !ok || flyteAdminError.Code() != codes.NotFound {
return nil, err
}
}
var weightedRandomList random.WeightedRandomList
if resource != nil && resource.Attributes.GetExecutionClusterLabel() != nil {
label := resource.Attributes.GetExecutionClusterLabel().Value
if _, ok := s.labelWeightedRandomMap[label]; ok {
weightedRandomList = s.labelWeightedRandomMap[label]
} else {
logger.Debugf(ctx, "No cluster mapping found for the label %s", label)
}
} else {
logger.Debugf(ctx, "No override found for the spec %v", spec)
}
// If there is no label associated (or) if the label is invalid, choose from all enabled clusters.
// Note that if there is a valid label with zero "Enabled" clusters, we still choose from all enabled ones.
if weightedRandomList == nil {
weightedRandomList = s.equalWeightedAllClusters
}
executionName := spec.ExecutionID
if executionName != "" {
randSrc, err := getRandSource(executionName)
if err != nil {
return nil, err
}
result, err := weightedRandomList.GetWithSeed(randSrc)
if err != nil {
return nil, err
}
execTarget := result.(executioncluster.ExecutionTarget)
return &execTarget, nil
}
execTarget := weightedRandomList.Get().(executioncluster.ExecutionTarget)
return &execTarget, nil
}
func NewRandomClusterSelector(initializationErrorCounter prometheus.Counter, config runtime.Configuration, executionTargetProvider interfaces.ExecutionTargetProvider, db repositories.RepositoryInterface) (interfaces.ClusterInterface, error) {
equalWeightedAllClusters, executionTargetMap, err := getExecutionTargets(context.Background(), initializationErrorCounter, executionTargetProvider, config.ClusterConfiguration())
if err != nil {
return nil, err
}
labelWeightedRandomMap, err := getLabeledWeightedRandomForCluster(context.Background(), config.ClusterConfiguration(), executionTargetMap)
if err != nil {
return nil, err
}
return &RandomClusterSelector{
labelWeightedRandomMap: labelWeightedRandomMap,
executionTargetMap: executionTargetMap,
resourceManager: resources.NewResourceManager(db, config.ApplicationConfiguration()),
equalWeightedAllClusters: equalWeightedAllClusters,
}, nil
}