| 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 buildbot | 5 package buildbot |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "compress/gzip" | 9 "compress/gzip" |
| 10 "compress/zlib" | 10 "compress/zlib" |
| 11 "encoding/base64" | 11 "encoding/base64" |
| 12 "encoding/json" | 12 "encoding/json" |
| 13 "fmt" |
| 13 "net/http" | 14 "net/http" |
| 14 "strings" | 15 "strings" |
| 15 "time" | 16 "time" |
| 16 | 17 |
| 17 ds "github.com/luci/gae/service/datastore" | 18 ds "github.com/luci/gae/service/datastore" |
| 18 "github.com/luci/luci-go/common/clock" | 19 "github.com/luci/luci-go/common/clock" |
| 19 "github.com/luci/luci-go/common/iotools" | 20 "github.com/luci/luci-go/common/iotools" |
| 20 "github.com/luci/luci-go/common/logging" | 21 "github.com/luci/luci-go/common/logging" |
| 21 "github.com/luci/luci-go/server/router" | 22 "github.com/luci/luci-go/server/router" |
| 22 | 23 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 36 buildCounter = metric.NewCounter( | 37 buildCounter = metric.NewCounter( |
| 37 "luci/milo/buildbot_pubsub/builds", | 38 "luci/milo/buildbot_pubsub/builds", |
| 38 "The number of buildbot builds received by Milo from PubSub", | 39 "The number of buildbot builds received by Milo from PubSub", |
| 39 nil, | 40 nil, |
| 40 field.Bool("internal"), | 41 field.Bool("internal"), |
| 41 field.String("master"), | 42 field.String("master"), |
| 42 field.String("builder"), | 43 field.String("builder"), |
| 43 field.Bool("finished"), | 44 field.Bool("finished"), |
| 44 // Status can be one of 3 options. "New", "Replaced", "Rejected
". | 45 // Status can be one of 3 options. "New", "Replaced", "Rejected
". |
| 45 field.String("status")) | 46 field.String("status")) |
| 47 |
| 48 masterCounter = metric.NewCounter( |
| 49 "luci/milo/buildbot_pubsub/masters", |
| 50 "The number of buildbot master jsons received by Milo from PubSu
b", |
| 51 nil, |
| 52 field.Bool("internal"), |
| 53 field.String("master"), |
| 54 // Status can be one of 2 options. "success", "failure". |
| 55 field.String("status")) |
| 46 ) | 56 ) |
| 47 | 57 |
| 48 type pubSubMessage struct { | 58 type pubSubMessage struct { |
| 49 Attributes map[string]string `json:"attributes"` | 59 Attributes map[string]string `json:"attributes"` |
| 50 Data string `json:"data"` | 60 Data string `json:"data"` |
| 51 MessageID string `json:"message_id"` | 61 MessageID string `json:"message_id"` |
| 52 } | 62 } |
| 53 | 63 |
| 54 type pubSubSubscription struct { | 64 type pubSubSubscription struct { |
| 55 Message pubSubMessage `json:"message"` | 65 Message pubSubMessage `json:"message"` |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 bm.Builds = append(bm.Builds, build) | 150 bm.Builds = append(bm.Builds, build) |
| 141 slave.RunningbuildsMap[build.Buildername] = appe
nd( | 151 slave.RunningbuildsMap[build.Buildername] = appe
nd( |
| 142 slave.RunningbuildsMap[build.Buildername
], build.Number) | 152 slave.RunningbuildsMap[build.Buildername
], build.Number) |
| 143 } | 153 } |
| 144 slave.Runningbuilds = nil | 154 slave.Runningbuilds = nil |
| 145 } | 155 } |
| 146 } | 156 } |
| 147 return bm.Builds, bm.Master, nil | 157 return bm.Builds, bm.Master, nil |
| 148 } | 158 } |
| 149 | 159 |
| 150 // getOSInfo fetches the os family and version of hte build from the | 160 // getOSInfo fetches the os family and version of the slave the build was |
| 151 // master json on a best-effort basis. | 161 // running on from the master json on a best-effort basis. |
| 152 func getOSInfo(c context.Context, b *buildbotBuild, m *buildbotMaster) ( | 162 func getOSInfo(c context.Context, b *buildbotBuild, m *buildbotMaster) ( |
| 153 family, version string) { | 163 family, version string) { |
| 154 // Fetch the master info from datastore if not provided. | 164 // Fetch the master info from datastore if not provided. |
| 155 if m.Name == "" { | 165 if m.Name == "" { |
| 156 logging.Infof(c, "Fetching info for master %s", b.Master) | 166 logging.Infof(c, "Fetching info for master %s", b.Master) |
| 157 entry := buildbotMasterEntry{Name: b.Master} | 167 entry := buildbotMasterEntry{Name: b.Master} |
| 158 err := ds.Get(c, &entry) | 168 err := ds.Get(c, &entry) |
| 159 if err != nil { | 169 if err != nil { |
| 160 logging.WithError(err).Errorf( | 170 logging.WithError(err).Errorf( |
| 161 c, "Encountered error while fetching entry for %
s", b.Master) | 171 c, "Encountered error while fetching entry for %
s", b.Master) |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 b.Finished = true | 215 b.Finished = true |
| 206 b.Results = &results | 216 b.Results = &results |
| 207 b.Currentstep = nil | 217 b.Currentstep = nil |
| 208 b.Text = append(b.Text, "Build expired on Milo") | 218 b.Text = append(b.Text, "Build expired on Milo") |
| 209 return ds.Put(c, b) | 219 return ds.Put(c, b) |
| 210 } | 220 } |
| 211 | 221 |
| 212 func doMaster(c context.Context, master *buildbotMaster, internal bool) int { | 222 func doMaster(c context.Context, master *buildbotMaster, internal bool) int { |
| 213 // Store the master json into the datastore. | 223 // Store the master json into the datastore. |
| 214 err := putDSMasterJSON(c, master, internal) | 224 err := putDSMasterJSON(c, master, internal) |
| 225 fullname := fmt.Sprintf("master.%s", master.Name) |
| 215 if err != nil { | 226 if err != nil { |
| 216 logging.WithError(err).Errorf( | 227 logging.WithError(err).Errorf( |
| 217 c, "Could not save master in datastore %s", err) | 228 c, "Could not save master in datastore %s", err) |
| 229 masterCounter.Add(c, 1, internal, fullname, "failure") |
| 218 // This is transient, we do want PubSub to retry. | 230 // This is transient, we do want PubSub to retry. |
| 219 return 500 | 231 return 500 |
| 220 } | 232 } |
| 233 masterCounter.Add(c, 1, internal, fullname, "success") |
| 221 | 234 |
| 222 // Extract current builds data out of the master json, and use it to | 235 // Extract current builds data out of the master json, and use it to |
| 223 // clean up expired builds. | 236 // clean up expired builds. |
| 224 q := ds.NewQuery("buildbotBuild"). | 237 q := ds.NewQuery("buildbotBuild"). |
| 225 Eq("finished", false). | 238 Eq("finished", false). |
| 226 Eq("master", master.Name) | 239 Eq("master", master.Name) |
| 227 builds := []*buildbotBuild{} | 240 builds := []*buildbotBuild{} |
| 228 err = ds.GetAll(c, q, &builds) | 241 err = ds.GetAll(c, q, &builds) |
| 229 if err != nil { | 242 if err != nil { |
| 230 logging.WithError(err).Errorf(c, "Could not load current builds
from master %s", | 243 logging.WithError(err).Errorf(c, "Could not load current builds
from master %s", |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 | 397 |
| 385 } | 398 } |
| 386 if master != nil { | 399 if master != nil { |
| 387 code := doMaster(c, master, internal) | 400 code := doMaster(c, master, internal) |
| 388 if code != 0 { | 401 if code != 0 { |
| 389 return code | 402 return code |
| 390 } | 403 } |
| 391 } | 404 } |
| 392 return 200 | 405 return 200 |
| 393 } | 406 } |
| OLD | NEW |