| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package jobsim | 5 package jobsim |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "encoding/json" | 8 "encoding/json" |
| 9 "fmt" | 9 "fmt" |
| 10 "math/rand" | 10 "math/rand" |
| 11 "strings" | |
| 12 "time" | 11 "time" |
| 13 | 12 |
| 14 "github.com/golang/protobuf/jsonpb" | 13 "github.com/golang/protobuf/jsonpb" |
| 15 "google.golang.org/grpc/codes" | 14 "google.golang.org/grpc/codes" |
| 16 | 15 |
| 17 "github.com/luci/luci-go/common/clock" | 16 "github.com/luci/luci-go/common/clock" |
| 18 "github.com/luci/luci-go/common/data/rand/cryptorand" | 17 "github.com/luci/luci-go/common/data/rand/cryptorand" |
| 18 "github.com/luci/luci-go/common/lhttp" |
| 19 "github.com/luci/luci-go/common/logging" | 19 "github.com/luci/luci-go/common/logging" |
| 20 "github.com/luci/luci-go/common/transport" | 20 "github.com/luci/luci-go/common/transport" |
| 21 "github.com/luci/luci-go/dm/api/distributor/jobsim" | 21 "github.com/luci/luci-go/dm/api/distributor/jobsim" |
| 22 dm "github.com/luci/luci-go/dm/api/service/v1" | 22 dm "github.com/luci/luci-go/dm/api/service/v1" |
| 23 "github.com/luci/luci-go/grpc/grpcutil" | 23 "github.com/luci/luci-go/grpc/grpcutil" |
| 24 "github.com/luci/luci-go/grpc/prpc" | 24 "github.com/luci/luci-go/grpc/prpc" |
| 25 "golang.org/x/net/context" | 25 "golang.org/x/net/context" |
| 26 ) | 26 ) |
| 27 | 27 |
| 28 // state is the opaque state data that DM will pass between re-executions of the | 28 // state is the opaque state data that DM will pass between re-executions of the |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 _, err = r.dmc.FinishAttempt(r.c, &dm.FinishAttemptReq{ | 311 _, err = r.dmc.FinishAttempt(r.c, &dm.FinishAttemptReq{ |
| 312 Auth: r.auth, | 312 Auth: r.auth, |
| 313 Data: executionResult(false, 0, time.Time{}), | 313 Data: executionResult(false, 0, time.Time{}), |
| 314 }) | 314 }) |
| 315 if err != nil { | 315 if err != nil { |
| 316 logging.WithError(err).Warningf(r.c, "got error on FinishAttempt
") | 316 logging.WithError(err).Warningf(r.c, "got error on FinishAttempt
") |
| 317 } | 317 } |
| 318 return | 318 return |
| 319 } | 319 } |
| 320 | 320 |
| 321 func isLocalHost(host string) bool { | |
| 322 switch { | |
| 323 case host == "localhost", strings.HasPrefix(host, "localhost:"): | |
| 324 case host == "127.0.0.1", strings.HasPrefix(host, "127.0.0.1:"): | |
| 325 case host == "[::1]", strings.HasPrefix(host, "[::1]:"): | |
| 326 case strings.HasPrefix(host, ":"): | |
| 327 | |
| 328 default: | |
| 329 return false | |
| 330 } | |
| 331 return true | |
| 332 } | |
| 333 | |
| 334 // runJob is analogous to a single Execution of a recipe. It will: | 321 // runJob is analogous to a single Execution of a recipe. It will: |
| 335 // * Activate itself with DM. | 322 // * Activate itself with DM. |
| 336 // * Inspect its previous State to determine where it left off on the previous | 323 // * Inspect its previous State to determine where it left off on the previous |
| 337 // execution. | 324 // execution. |
| 338 // * Execute stages (incrementing the Stage counter in the state, and/or | 325 // * Execute stages (incrementing the Stage counter in the state, and/or |
| 339 // accumulating into Sum) until it hits a stop condition: | 326 // accumulating into Sum) until it hits a stop condition: |
| 340 // * depending on incomplete Attempts | 327 // * depending on incomplete Attempts |
| 341 // * arriving at a final result | 328 // * arriving at a final result |
| 342 // | 329 // |
| 343 // If it hits some underlying error it will return that error, and expect to be | 330 // If it hits some underlying error it will return that error, and expect to be |
| 344 // retried by DM. | 331 // retried by DM. |
| 345 func runJob(c context.Context, host string, state *state, job *jobsim.Phrase, au
th *dm.Execution_Auth, cfgName string) error { | 332 func runJob(c context.Context, host string, state *state, job *jobsim.Phrase, au
th *dm.Execution_Auth, cfgName string) error { |
| 346 pcli := &prpc.Client{ | 333 pcli := &prpc.Client{ |
| 347 C: transport.GetClient(c), | 334 C: transport.GetClient(c), |
| 348 Host: host, | 335 Host: host, |
| 349 Options: prpc.DefaultOptions(), | 336 Options: prpc.DefaultOptions(), |
| 350 } | 337 } |
| 351 » pcli.Options.Insecure = isLocalHost(host) | 338 » pcli.Options.Insecure = lhttp.IsLocalHost(host) |
| 352 dmc := dm.NewDepsPRPCClient(pcli) | 339 dmc := dm.NewDepsPRPCClient(pcli) |
| 353 r := runner{c, auth, dmc, state} | 340 r := runner{c, auth, dmc, state} |
| 354 | 341 |
| 355 ok, err := r.activateExecution() | 342 ok, err := r.activateExecution() |
| 356 if !ok || err != nil { | 343 if !ok || err != nil { |
| 357 return err | 344 return err |
| 358 } | 345 } |
| 359 | 346 |
| 360 stop := false | 347 stop := false |
| 361 for ; r.state.Stage < len(job.Stages); r.state.Stage++ { | 348 for ; r.state.Stage < len(job.Stages); r.state.Stage++ { |
| 362 switch stg := job.Stages[r.state.Stage].StageType.(type) { | 349 switch stg := job.Stages[r.state.Stage].StageType.(type) { |
| 363 case *jobsim.Stage_Deps: | 350 case *jobsim.Stage_Deps: |
| 364 stop, err = r.doDeps(job.Seed, stg.Deps, cfgName) | 351 stop, err = r.doDeps(job.Seed, stg.Deps, cfgName) |
| 365 case *jobsim.Stage_Stall: | 352 case *jobsim.Stage_Stall: |
| 366 r.doStall(stg.Stall) | 353 r.doStall(stg.Stall) |
| 367 case *jobsim.Stage_Failure: | 354 case *jobsim.Stage_Failure: |
| 368 stop, err = r.doFailure(job.Seed, stg.Failure.Chance) | 355 stop, err = r.doFailure(job.Seed, stg.Failure.Chance) |
| 369 default: | 356 default: |
| 370 err = fmt.Errorf("don't know how to handle StageType: %T
", stg) | 357 err = fmt.Errorf("don't know how to handle StageType: %T
", stg) |
| 371 } | 358 } |
| 372 if stop || err != nil { | 359 if stop || err != nil { |
| 373 return err | 360 return err |
| 374 } | 361 } |
| 375 } | 362 } |
| 376 return r.doReturnStage(job.ReturnStage) | 363 return r.doReturnStage(job.ReturnStage) |
| 377 } | 364 } |
| OLD | NEW |