Skip to content

Commit d8425c6

Browse files
committed
cmd/coordinator, internal/coordinator/log: create coordinator log pkg
This change moves the coordinator event logging into a package. Updates golang/go#38337 Updates golang/go#48742 Change-Id: If3714ca741f48ba703e4585e3cbe3755e66b8613 Reviewed-on: https://go-review.googlesource.com/c/build/+/368675 Trust: Carlos Amedee <[email protected]> Run-TryBot: Carlos Amedee <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Reviewed-by: Alex Rakoczy <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent c4e3711 commit d8425c6

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

cmd/coordinator/buildstatus.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"golang.org/x/build/dashboard"
3333
"golang.org/x/build/internal/buildgo"
3434
"golang.org/x/build/internal/buildstats"
35+
clog "golang.org/x/build/internal/coordinator/log"
3536
"golang.org/x/build/internal/coordinator/pool"
3637
"golang.org/x/build/internal/singleflight"
3738
"golang.org/x/build/internal/sourcecache"
@@ -206,7 +207,7 @@ func (st *buildStatus) start() {
206207
log.Println(st.BuilderRev, "failed:", err)
207208
}
208209
st.setDone(err == nil)
209-
putBuildRecord(st.buildRecord())
210+
clog.CoordinatorProcess().PutBuildRecord(st.buildRecord())
210211
}
211212
markDone(st.BuilderRev)
212213
}()
@@ -424,7 +425,7 @@ func (st *buildStatus) build() error {
424425
cancel()
425426
}
426427

427-
putBuildRecord(st.buildRecord())
428+
clog.CoordinatorProcess().PutBuildRecord(st.buildRecord())
428429

429430
sp := st.CreateSpan("checking_for_snapshot")
430431
if pool.NewGCEConfiguration().InStaging() {

cmd/coordinator/coordinator.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import (
5757
"golang.org/x/build/internal/buildgo"
5858
"golang.org/x/build/internal/buildstats"
5959
"golang.org/x/build/internal/cloud"
60+
clog "golang.org/x/build/internal/coordinator/log"
6061
"golang.org/x/build/internal/coordinator/pool"
6162
"golang.org/x/build/internal/coordinator/remote"
6263
"golang.org/x/build/internal/https"
@@ -244,6 +245,8 @@ func main() {
244245
https.RegisterFlags(flag.CommandLine)
245246
flag.Parse()
246247

248+
clog.SetProcessMetadata(processID, processStartTime)
249+
247250
if Version == "" && *mode == "dev" {
248251
Version = "dev"
249252
}
@@ -299,7 +302,7 @@ func main() {
299302
}
300303
}
301304

302-
go updateInstanceRecord()
305+
go clog.CoordinatorProcess().UpdateInstanceRecord()
303306

304307
switch *mode {
305308
case "dev", "prod":
@@ -2021,7 +2024,7 @@ func (s *span) Done(err error) error {
20212024
fmt.Fprintf(&text, "; %v", s.optText)
20222025
}
20232026
if st, ok := s.el.(*buildStatus); ok {
2024-
putSpanRecord(st.spanRecord(s, err))
2027+
clog.CoordinatorProcess().PutSpanRecord(st.spanRecord(s, err))
20252028
}
20262029
s.el.LogEventTime("finish_"+s.event, text.String())
20272030
return err

cmd/coordinator/log.go renamed to internal/coordinator/log/log.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build go1.16 && (linux || darwin)
6-
// +build go1.16
5+
//go:build linux || darwin
76
// +build linux darwin
87

9-
package main
8+
package log
109

1110
import (
1211
"context"
1312
"fmt"
1413
"log"
14+
"sync"
1515
"time"
1616

1717
"cloud.google.com/go/datastore"
@@ -20,6 +20,33 @@ import (
2020
"golang.org/x/build/types"
2121
)
2222

23+
type Process struct {
24+
processID string
25+
processStartTime time.Time
26+
}
27+
28+
var (
29+
process *Process
30+
once sync.Once
31+
)
32+
33+
// SetProcessMetadata sets metadata about the process. This should be called before any
34+
// event logging operations.
35+
func SetProcessMetadata(id string, startTime time.Time) *Process {
36+
once.Do(func() {
37+
process = &Process{
38+
processID: id,
39+
processStartTime: startTime,
40+
}
41+
})
42+
return process
43+
}
44+
45+
// CoordinatorProcess returns the package level process logger.
46+
func CoordinatorProcess() *Process {
47+
return process
48+
}
49+
2350
// Process is a datastore record about the lifetime of a coordinator process.
2451
//
2552
// Example GQL query:
@@ -33,17 +60,17 @@ type ProcessRecord struct {
3360
// GCE instance type?
3461
}
3562

36-
func updateInstanceRecord() {
63+
func (p *Process) UpdateInstanceRecord() {
3764
dsClient := pool.NewGCEConfiguration().DSClient()
3865
if dsClient == nil {
3966
return
4067
}
4168
ctx := context.Background()
4269
for {
43-
key := datastore.NameKey("Process", processID, nil)
70+
key := datastore.NameKey("Process", p.processID, nil)
4471
_, err := dsClient.Put(ctx, key, &ProcessRecord{
45-
ID: processID,
46-
Start: processStartTime,
72+
ID: p.processID,
73+
Start: p.processStartTime,
4774
LastHeartbeat: time.Now(),
4875
})
4976
if err != nil {
@@ -53,7 +80,7 @@ func updateInstanceRecord() {
5380
}
5481
}
5582

56-
func putBuildRecord(br *types.BuildRecord) {
83+
func (p *Process) PutBuildRecord(br *types.BuildRecord) {
5784
dsClient := pool.NewGCEConfiguration().DSClient()
5885
if dsClient == nil {
5986
return
@@ -65,7 +92,7 @@ func putBuildRecord(br *types.BuildRecord) {
6592
}
6693
}
6794

68-
func putSpanRecord(sr *types.SpanRecord) {
95+
func (p *Process) PutSpanRecord(sr *types.SpanRecord) {
6996
dsClient := pool.NewGCEConfiguration().DSClient()
7097
if dsClient == nil {
7198
return

0 commit comments

Comments
 (0)