1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.arvados.org/arvados.git/lib/cloud"
18 "git.arvados.org/arvados.git/lib/controller/dblock"
19 "git.arvados.org/arvados.git/lib/ctrlctx"
20 "git.arvados.org/arvados.git/lib/dispatchcloud/container"
21 "git.arvados.org/arvados.git/lib/dispatchcloud/scheduler"
22 "git.arvados.org/arvados.git/lib/dispatchcloud/sshexecutor"
23 "git.arvados.org/arvados.git/lib/dispatchcloud/worker"
24 "git.arvados.org/arvados.git/sdk/go/arvados"
25 "git.arvados.org/arvados.git/sdk/go/auth"
26 "git.arvados.org/arvados.git/sdk/go/ctxlog"
27 "git.arvados.org/arvados.git/sdk/go/health"
28 "git.arvados.org/arvados.git/sdk/go/httpserver"
29 "github.com/julienschmidt/httprouter"
30 "github.com/prometheus/client_golang/prometheus"
31 "github.com/prometheus/client_golang/prometheus/promhttp"
32 "github.com/sirupsen/logrus"
33 "golang.org/x/crypto/ssh"
37 defaultPollInterval = time.Second
38 defaultStaleLockTimeout = time.Minute
44 Instances() []worker.InstanceView
45 SetIdleBehavior(cloud.InstanceID, worker.IdleBehavior) error
46 KillInstance(id cloud.InstanceID, reason string) error
50 type dispatcher struct {
51 Cluster *arvados.Cluster
52 Context context.Context
53 ArvClient *arvados.Client
55 Registry *prometheus.Registry
56 InstanceSetID cloud.InstanceSetID
58 dbConnector ctrlctx.DBConnector
59 logger logrus.FieldLogger
60 instanceSet cloud.InstanceSet
62 queue scheduler.ContainerQueue
63 httpHandler http.Handler
71 // Start starts the dispatcher. Start can be called multiple times
72 // with no ill effect.
73 func (disp *dispatcher) Start() {
74 disp.setupOnce.Do(disp.setup)
77 // ServeHTTP implements service.Handler.
78 func (disp *dispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
80 disp.httpHandler.ServeHTTP(w, r)
83 // CheckHealth implements service.Handler.
84 func (disp *dispatcher) CheckHealth() error {
86 return disp.pool.CheckHealth()
89 // Done implements service.Handler.
90 func (disp *dispatcher) Done() <-chan struct{} {
94 // Stop dispatching containers and release resources. Typically used
96 func (disp *dispatcher) Close() {
99 case disp.stop <- struct{}{}:
105 // Make a worker.Executor for the given instance.
106 func (disp *dispatcher) newExecutor(inst cloud.Instance) worker.Executor {
107 exr := sshexecutor.New(inst)
108 exr.SetTargetPort(disp.Cluster.Containers.CloudVMs.SSHPort)
109 exr.SetSigners(disp.sshKey)
113 func (disp *dispatcher) typeChooser(ctr *arvados.Container) (arvados.InstanceType, error) {
114 return ChooseInstanceType(disp.Cluster, ctr)
117 func (disp *dispatcher) setup() {
122 func (disp *dispatcher) initialize() {
123 disp.logger = ctxlog.FromContext(disp.Context)
124 disp.dbConnector = ctrlctx.DBConnector{PostgreSQL: disp.Cluster.PostgreSQL}
126 disp.ArvClient.AuthToken = disp.AuthToken
128 if disp.InstanceSetID == "" {
129 if strings.HasPrefix(disp.AuthToken, "v2/") {
130 disp.InstanceSetID = cloud.InstanceSetID(strings.Split(disp.AuthToken, "/")[1])
132 // Use some other string unique to this token
133 // that doesn't reveal the token itself.
134 disp.InstanceSetID = cloud.InstanceSetID(fmt.Sprintf("%x", md5.Sum([]byte(disp.AuthToken))))
137 disp.stop = make(chan struct{}, 1)
138 disp.stopped = make(chan struct{})
140 if key, err := ssh.ParsePrivateKey([]byte(disp.Cluster.Containers.DispatchPrivateKey)); err != nil {
141 disp.logger.Fatalf("error parsing configured Containers.DispatchPrivateKey: %s", err)
146 instanceSet, err := newInstanceSet(disp.Cluster, disp.InstanceSetID, disp.logger, disp.Registry)
148 disp.logger.Fatalf("error initializing driver: %s", err)
150 dblock.Dispatch.Lock(disp.Context, disp.dbConnector.GetDB)
151 disp.instanceSet = instanceSet
152 disp.pool = worker.NewPool(disp.logger, disp.ArvClient, disp.Registry, disp.InstanceSetID, disp.instanceSet, disp.newExecutor, disp.sshKey.PublicKey(), disp.Cluster)
153 disp.queue = container.NewQueue(disp.logger, disp.Registry, disp.typeChooser, disp.ArvClient)
155 if disp.Cluster.ManagementToken == "" {
156 disp.httpHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
157 http.Error(w, "Management API authentication is not configured", http.StatusForbidden)
160 mux := httprouter.New()
161 mux.HandlerFunc("GET", "/arvados/v1/dispatch/containers", disp.apiContainers)
162 mux.HandlerFunc("POST", "/arvados/v1/dispatch/containers/kill", disp.apiContainerKill)
163 mux.HandlerFunc("GET", "/arvados/v1/dispatch/instances", disp.apiInstances)
164 mux.HandlerFunc("POST", "/arvados/v1/dispatch/instances/hold", disp.apiInstanceHold)
165 mux.HandlerFunc("POST", "/arvados/v1/dispatch/instances/drain", disp.apiInstanceDrain)
166 mux.HandlerFunc("POST", "/arvados/v1/dispatch/instances/run", disp.apiInstanceRun)
167 mux.HandlerFunc("POST", "/arvados/v1/dispatch/instances/kill", disp.apiInstanceKill)
168 metricsH := promhttp.HandlerFor(disp.Registry, promhttp.HandlerOpts{
169 ErrorLog: disp.logger,
171 mux.Handler("GET", "/metrics", metricsH)
172 mux.Handler("GET", "/metrics.json", metricsH)
173 mux.Handler("GET", "/_health/:check", &health.Handler{
174 Token: disp.Cluster.ManagementToken,
176 Routes: health.Routes{"ping": disp.CheckHealth},
178 disp.httpHandler = auth.RequireLiteralToken(disp.Cluster.ManagementToken, mux)
182 func (disp *dispatcher) run() {
183 defer dblock.Dispatch.Unlock()
184 defer close(disp.stopped)
185 defer disp.instanceSet.Stop()
186 defer disp.pool.Stop()
188 staleLockTimeout := time.Duration(disp.Cluster.Containers.StaleLockTimeout)
189 if staleLockTimeout == 0 {
190 staleLockTimeout = defaultStaleLockTimeout
192 pollInterval := time.Duration(disp.Cluster.Containers.CloudVMs.PollInterval)
193 if pollInterval <= 0 {
194 pollInterval = defaultPollInterval
196 sched := scheduler.New(disp.Context, disp.queue, disp.pool, disp.Registry, staleLockTimeout, pollInterval)
203 // Management API: all active and queued containers.
204 func (disp *dispatcher) apiContainers(w http.ResponseWriter, r *http.Request) {
206 Items []container.QueueEnt `json:"items"`
208 qEntries, _ := disp.queue.Entries()
209 for _, ent := range qEntries {
210 resp.Items = append(resp.Items, ent)
212 json.NewEncoder(w).Encode(resp)
215 // Management API: all active instances (cloud VMs).
216 func (disp *dispatcher) apiInstances(w http.ResponseWriter, r *http.Request) {
218 Items []worker.InstanceView `json:"items"`
220 resp.Items = disp.pool.Instances()
221 json.NewEncoder(w).Encode(resp)
224 // Management API: set idle behavior to "hold" for specified instance.
225 func (disp *dispatcher) apiInstanceHold(w http.ResponseWriter, r *http.Request) {
226 disp.apiInstanceIdleBehavior(w, r, worker.IdleBehaviorHold)
229 // Management API: set idle behavior to "drain" for specified instance.
230 func (disp *dispatcher) apiInstanceDrain(w http.ResponseWriter, r *http.Request) {
231 disp.apiInstanceIdleBehavior(w, r, worker.IdleBehaviorDrain)
234 // Management API: set idle behavior to "run" for specified instance.
235 func (disp *dispatcher) apiInstanceRun(w http.ResponseWriter, r *http.Request) {
236 disp.apiInstanceIdleBehavior(w, r, worker.IdleBehaviorRun)
239 // Management API: shutdown/destroy specified instance now.
240 func (disp *dispatcher) apiInstanceKill(w http.ResponseWriter, r *http.Request) {
241 id := cloud.InstanceID(r.FormValue("instance_id"))
243 httpserver.Error(w, "instance_id parameter not provided", http.StatusBadRequest)
246 err := disp.pool.KillInstance(id, "via management API: "+r.FormValue("reason"))
248 httpserver.Error(w, err.Error(), http.StatusNotFound)
253 // Management API: send SIGTERM to specified container's crunch-run
255 func (disp *dispatcher) apiContainerKill(w http.ResponseWriter, r *http.Request) {
256 uuid := r.FormValue("container_uuid")
258 httpserver.Error(w, "container_uuid parameter not provided", http.StatusBadRequest)
261 if !disp.pool.KillContainer(uuid, "via management API: "+r.FormValue("reason")) {
262 httpserver.Error(w, "container not found", http.StatusNotFound)
267 func (disp *dispatcher) apiInstanceIdleBehavior(w http.ResponseWriter, r *http.Request, want worker.IdleBehavior) {
268 id := cloud.InstanceID(r.FormValue("instance_id"))
270 httpserver.Error(w, "instance_id parameter not provided", http.StatusBadRequest)
273 err := disp.pool.SetIdleBehavior(id, want)
275 httpserver.Error(w, err.Error(), http.StatusNotFound)