2013-07-31 21:23:51 -06:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// +build appengine
|
|
|
|
|
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2014-01-23 20:52:23 -07:00
|
|
|
"errors"
|
2013-07-31 21:23:51 -06:00
|
|
|
"fmt"
|
2014-01-23 20:52:23 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
2014-05-13 01:00:32 -06:00
|
|
|
"sort"
|
2013-07-31 21:23:51 -06:00
|
|
|
"text/template"
|
2014-01-23 20:52:23 -07:00
|
|
|
|
|
|
|
"appengine"
|
|
|
|
"appengine/datastore"
|
|
|
|
"appengine/delay"
|
|
|
|
"appengine/mail"
|
|
|
|
"appengine/urlfetch"
|
2013-07-31 21:23:51 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
mailFrom = "builder@golang.org" // use this for sending any mail
|
|
|
|
failMailTo = "golang-dev@googlegroups.com"
|
|
|
|
domain = "build.golang.org"
|
2014-01-23 20:52:23 -07:00
|
|
|
gobotBase = "http://research.swtch.com/gobot_codereview"
|
2013-07-31 21:23:51 -06:00
|
|
|
)
|
|
|
|
|
2014-01-27 23:59:29 -07:00
|
|
|
// ignoreFailure is a set of builders that we don't email about because
|
|
|
|
// they are not yet production-ready.
|
|
|
|
var ignoreFailure = map[string]bool{
|
2014-03-03 17:53:58 -07:00
|
|
|
"dragonfly-386": true,
|
|
|
|
"dragonfly-amd64": true,
|
|
|
|
"netbsd-arm-rpi": true,
|
|
|
|
"solaris-amd64-smartos": true,
|
|
|
|
"solaris-amd64-solaris11": true,
|
2013-07-31 21:23:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// notifyOnFailure checks whether the supplied Commit or the subsequent
|
|
|
|
// Commit (if present) breaks the build for this builder.
|
|
|
|
// If either of those commits break the build an email notification is sent
|
|
|
|
// from a delayed task. (We use a task because this way the mail won't be
|
|
|
|
// sent if the enclosing datastore transaction fails.)
|
|
|
|
//
|
|
|
|
// This must be run in a datastore transaction, and the provided *Commit must
|
|
|
|
// have been retrieved from the datastore within that transaction.
|
|
|
|
func notifyOnFailure(c appengine.Context, com *Commit, builder string) error {
|
2014-01-27 23:59:29 -07:00
|
|
|
if ignoreFailure[builder] {
|
2013-07-31 21:23:51 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(adg): implement notifications for packages
|
|
|
|
if com.PackagePath != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &Package{Path: com.PackagePath}
|
|
|
|
var broken *Commit
|
|
|
|
cr := com.Result(builder, "")
|
|
|
|
if cr == nil {
|
|
|
|
return fmt.Errorf("no result for %s/%s", com.Hash, builder)
|
|
|
|
}
|
|
|
|
q := datastore.NewQuery("Commit").Ancestor(p.Key(c))
|
|
|
|
if cr.OK {
|
|
|
|
// This commit is OK. Notify if next Commit is broken.
|
|
|
|
next := new(Commit)
|
|
|
|
q = q.Filter("ParentHash=", com.Hash)
|
|
|
|
if err := firstMatch(c, q, next); err != nil {
|
|
|
|
if err == datastore.ErrNoSuchEntity {
|
|
|
|
// OK at tip, no notification necessary.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if nr := next.Result(builder, ""); nr != nil && !nr.OK {
|
|
|
|
c.Debugf("commit ok: %#v\nresult: %#v", com, cr)
|
|
|
|
c.Debugf("next commit broken: %#v\nnext result:%#v", next, nr)
|
|
|
|
broken = next
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This commit is broken. Notify if the previous Commit is OK.
|
|
|
|
prev := new(Commit)
|
|
|
|
q = q.Filter("Hash=", com.ParentHash)
|
|
|
|
if err := firstMatch(c, q, prev); err != nil {
|
|
|
|
if err == datastore.ErrNoSuchEntity {
|
|
|
|
// No previous result, let the backfill of
|
|
|
|
// this result trigger the notification.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if pr := prev.Result(builder, ""); pr != nil && pr.OK {
|
|
|
|
c.Debugf("commit broken: %#v\nresult: %#v", com, cr)
|
|
|
|
c.Debugf("previous commit ok: %#v\nprevious result:%#v", prev, pr)
|
|
|
|
broken = com
|
|
|
|
}
|
|
|
|
}
|
2014-05-13 01:00:32 -06:00
|
|
|
if broken == nil {
|
|
|
|
return nil
|
2013-07-31 21:23:51 -06:00
|
|
|
}
|
2014-05-13 01:00:32 -06:00
|
|
|
r := broken.Result(builder, "")
|
|
|
|
if r == nil {
|
|
|
|
return fmt.Errorf("finding result for %q: %+v", builder, com)
|
|
|
|
}
|
|
|
|
return commonNotify(c, broken, builder, r.LogHash)
|
2013-07-31 21:23:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// firstMatch executes the query q and loads the first entity into v.
|
|
|
|
func firstMatch(c appengine.Context, q *datastore.Query, v interface{}) error {
|
|
|
|
t := q.Limit(1).Run(c)
|
|
|
|
_, err := t.Next(v)
|
|
|
|
if err == datastore.Done {
|
|
|
|
err = datastore.ErrNoSuchEntity
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-23 20:52:23 -07:00
|
|
|
var notifyLater = delay.Func("notify", notify)
|
|
|
|
|
|
|
|
// notify tries to update the CL for the given Commit with a failure message.
|
|
|
|
// If it doesn't succeed, it sends a failure email to golang-dev.
|
2014-05-13 01:00:32 -06:00
|
|
|
func notify(c appengine.Context, com *Commit, builder, logHash string) {
|
|
|
|
if !updateCL(c, com, builder, logHash) {
|
2014-01-23 20:52:23 -07:00
|
|
|
// Send a mail notification if the CL can't be found.
|
2014-05-13 01:00:32 -06:00
|
|
|
sendFailMail(c, com, builder, logHash)
|
2014-01-23 20:52:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateCL updates the CL for the given Commit with a failure message
|
|
|
|
// for the given builder.
|
2014-05-13 01:00:32 -06:00
|
|
|
func updateCL(c appengine.Context, com *Commit, builder, logHash string) bool {
|
2014-01-23 20:52:23 -07:00
|
|
|
cl, err := lookupCL(c, com)
|
|
|
|
if err != nil {
|
|
|
|
c.Errorf("could not find CL for %v: %v", com.Hash, err)
|
|
|
|
return false
|
|
|
|
}
|
2014-05-13 01:00:32 -06:00
|
|
|
url := fmt.Sprintf("%v?cl=%v&brokebuild=%v&log=%v", gobotBase, cl, builder, logHash)
|
2014-01-23 20:52:23 -07:00
|
|
|
r, err := urlfetch.Client(c).Post(url, "text/plain", nil)
|
|
|
|
if err != nil {
|
|
|
|
c.Errorf("could not update CL %v: %v", cl, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
r.Body.Close()
|
|
|
|
if r.StatusCode != http.StatusOK {
|
|
|
|
c.Errorf("could not update CL %v: %v", cl, r.Status)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var clURL = regexp.MustCompile(`https://codereview.appspot.com/([0-9]+)`)
|
|
|
|
|
|
|
|
// lookupCL consults code.google.com for the full change description for the
|
|
|
|
// provided Commit, and returns the relevant CL number.
|
|
|
|
func lookupCL(c appengine.Context, com *Commit) (string, error) {
|
|
|
|
url := "https://code.google.com/p/go/source/detail?r=" + com.Hash
|
|
|
|
r, err := urlfetch.Client(c).Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
if r.StatusCode != http.StatusOK {
|
|
|
|
return "", fmt.Errorf("retrieving %v: %v", url, r.Status)
|
|
|
|
}
|
|
|
|
b, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
m := clURL.FindAllSubmatch(b, -1)
|
|
|
|
if m == nil {
|
|
|
|
return "", errors.New("no CL URL found on changeset page")
|
|
|
|
}
|
|
|
|
// Return the last visible codereview URL on the page,
|
|
|
|
// in case the change description refers to another CL.
|
|
|
|
return string(m[len(m)-1][1]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var sendFailMailTmpl = template.Must(template.New("notify.txt").
|
|
|
|
Funcs(template.FuncMap(tmplFuncs)).
|
|
|
|
ParseFiles("build/notify.txt"))
|
2013-07-31 21:23:51 -06:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
gob.Register(&Commit{}) // for delay
|
|
|
|
}
|
|
|
|
|
2014-05-13 01:00:32 -06:00
|
|
|
var (
|
|
|
|
sendPerfMailLater = delay.Func("sendPerfMail", sendPerfMailFunc)
|
|
|
|
sendPerfMailTmpl = template.Must(
|
|
|
|
template.New("perf_notify.txt").
|
|
|
|
Funcs(template.FuncMap(tmplFuncs)).
|
|
|
|
ParseFiles("build/perf_notify.txt"),
|
|
|
|
)
|
|
|
|
)
|
2013-07-31 21:23:51 -06:00
|
|
|
|
2014-05-13 01:00:32 -06:00
|
|
|
func sendPerfFailMail(c appengine.Context, builder string, res *PerfResult) error {
|
|
|
|
com := &Commit{Hash: res.CommitHash}
|
|
|
|
if err := datastore.Get(c, com.Key(c), com); err != nil {
|
|
|
|
return fmt.Errorf("getting commit %v: %v", com.Hash, err)
|
|
|
|
}
|
|
|
|
logHash := ""
|
|
|
|
parsed := res.ParseData()
|
|
|
|
for _, data := range parsed[builder] {
|
|
|
|
if !data.OK {
|
|
|
|
logHash = data.Artifacts["log"]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if logHash == "" {
|
|
|
|
return fmt.Errorf("can not find failed result for commit %v on builder %v", com.Hash, builder)
|
2013-07-31 21:23:51 -06:00
|
|
|
}
|
2014-05-13 01:00:32 -06:00
|
|
|
return commonNotify(c, com, builder, logHash)
|
|
|
|
}
|
2013-07-31 21:23:51 -06:00
|
|
|
|
2014-05-13 01:00:32 -06:00
|
|
|
func commonNotify(c appengine.Context, com *Commit, builder, logHash string) error {
|
|
|
|
if com.FailNotificationSent {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.Infof("%s is broken commit; notifying", com.Hash)
|
|
|
|
notifyLater.Call(c, com, builder, logHash) // add task to queue
|
|
|
|
com.FailNotificationSent = true
|
|
|
|
_, err := datastore.Put(c, com.Key(c), com)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendFailMail sends a mail notification that the build failed on the
|
|
|
|
// provided commit and builder.
|
|
|
|
func sendFailMail(c appengine.Context, com *Commit, builder, logHash string) {
|
2013-07-31 21:23:51 -06:00
|
|
|
// get Log
|
2014-05-13 01:00:32 -06:00
|
|
|
k := datastore.NewKey(c, "Log", logHash, 0, nil)
|
2013-07-31 21:23:51 -06:00
|
|
|
l := new(Log)
|
|
|
|
if err := datastore.Get(c, k, l); err != nil {
|
2014-05-13 01:00:32 -06:00
|
|
|
c.Errorf("finding Log record %v: %v", logHash, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logText, err := l.Text()
|
|
|
|
if err != nil {
|
|
|
|
c.Errorf("unpacking Log record %v: %v", logHash, err)
|
2013-07-31 21:23:51 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare mail message
|
|
|
|
var body bytes.Buffer
|
2014-05-13 01:00:32 -06:00
|
|
|
err = sendFailMailTmpl.Execute(&body, map[string]interface{}{
|
|
|
|
"Builder": builder, "Commit": com, "LogHash": logHash, "LogText": logText,
|
2013-07-31 21:23:51 -06:00
|
|
|
"Hostname": domain,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.Errorf("rendering mail template: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
subject := fmt.Sprintf("%s broken by %s", builder, shortDesc(com.Desc))
|
|
|
|
msg := &mail.Message{
|
|
|
|
Sender: mailFrom,
|
|
|
|
To: []string{failMailTo},
|
|
|
|
ReplyTo: failMailTo,
|
|
|
|
Subject: subject,
|
|
|
|
Body: body.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// send mail
|
|
|
|
if err := mail.Send(c, msg); err != nil {
|
|
|
|
c.Errorf("sending mail: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2014-05-13 01:00:32 -06:00
|
|
|
|
|
|
|
type PerfChangeBenchmark struct {
|
|
|
|
Name string
|
|
|
|
Metrics []*PerfChangeMetric
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerfChangeMetric struct {
|
|
|
|
Name string
|
|
|
|
Old uint64
|
|
|
|
New uint64
|
|
|
|
Delta float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerfChangeBenchmarkSlice []*PerfChangeBenchmark
|
|
|
|
|
|
|
|
func (l PerfChangeBenchmarkSlice) Len() int { return len(l) }
|
|
|
|
func (l PerfChangeBenchmarkSlice) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
|
|
|
func (l PerfChangeBenchmarkSlice) Less(i, j int) bool {
|
|
|
|
b1, p1 := splitBench(l[i].Name)
|
|
|
|
b2, p2 := splitBench(l[j].Name)
|
|
|
|
if b1 != b2 {
|
|
|
|
return b1 < b2
|
|
|
|
}
|
|
|
|
return p1 < p2
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerfChangeMetricSlice []*PerfChangeMetric
|
|
|
|
|
|
|
|
func (l PerfChangeMetricSlice) Len() int { return len(l) }
|
|
|
|
func (l PerfChangeMetricSlice) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
|
|
|
func (l PerfChangeMetricSlice) Less(i, j int) bool { return l[i].Name < l[j].Name }
|
|
|
|
|
|
|
|
func sendPerfMailFunc(c appengine.Context, com *Commit, prevCommitHash, builder string, changes []*PerfChange) {
|
|
|
|
// Sort the changes into the right order.
|
|
|
|
var benchmarks []*PerfChangeBenchmark
|
|
|
|
for _, ch := range changes {
|
|
|
|
// Find the benchmark.
|
|
|
|
var b *PerfChangeBenchmark
|
|
|
|
for _, b1 := range benchmarks {
|
2014-08-20 03:20:12 -06:00
|
|
|
if b1.Name == ch.Bench {
|
2014-05-13 01:00:32 -06:00
|
|
|
b = b1
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if b == nil {
|
2014-08-20 03:20:12 -06:00
|
|
|
b = &PerfChangeBenchmark{Name: ch.Bench}
|
2014-05-13 01:00:32 -06:00
|
|
|
benchmarks = append(benchmarks, b)
|
|
|
|
}
|
2014-08-20 03:20:12 -06:00
|
|
|
b.Metrics = append(b.Metrics, &PerfChangeMetric{Name: ch.Metric, Old: ch.Old, New: ch.New, Delta: ch.Diff})
|
2014-05-13 01:00:32 -06:00
|
|
|
}
|
|
|
|
for _, b := range benchmarks {
|
|
|
|
sort.Sort(PerfChangeMetricSlice(b.Metrics))
|
|
|
|
}
|
|
|
|
sort.Sort(PerfChangeBenchmarkSlice(benchmarks))
|
|
|
|
|
|
|
|
url := fmt.Sprintf("http://%v/perfdetail?commit=%v&commit0=%v&kind=builder&builder=%v", domain, com.Hash, prevCommitHash, builder)
|
|
|
|
|
|
|
|
// prepare mail message
|
|
|
|
var body bytes.Buffer
|
|
|
|
err := sendPerfMailTmpl.Execute(&body, map[string]interface{}{
|
|
|
|
"Builder": builder, "Commit": com, "Hostname": domain, "Url": url, "Benchmarks": benchmarks,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.Errorf("rendering perf mail template: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
subject := fmt.Sprintf("Perf changes on %s by %s", builder, shortDesc(com.Desc))
|
|
|
|
msg := &mail.Message{
|
|
|
|
Sender: mailFrom,
|
|
|
|
To: []string{failMailTo},
|
|
|
|
ReplyTo: failMailTo,
|
|
|
|
Subject: subject,
|
|
|
|
Body: body.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// send mail
|
|
|
|
if err := mail.Send(c, msg); err != nil {
|
|
|
|
c.Errorf("sending mail: %v", err)
|
|
|
|
}
|
|
|
|
}
|