-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathnative_orchestrator.go
More file actions
232 lines (218 loc) · 8.42 KB
/
Copy pathnative_orchestrator.go
File metadata and controls
232 lines (218 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) 2025 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package orchestrator
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"time"
"github.com/uber-go/tally"
"github.com/uber/tango/config"
"github.com/uber/tango/core/bazel"
"github.com/uber/tango/core/common"
"github.com/uber/tango/core/git"
"github.com/uber/tango/core/repomanager"
"github.com/uber/tango/core/storage"
"github.com/uber/tango/core/workspace"
"github.com/uber/tango/graphrunner"
"go.uber.org/zap"
)
// nativeOrchestrator implements native version of Orchestrator
type nativeOrchestrator struct {
storage storage.Storage
repoManager repomanager.RepoManager
logger *zap.SugaredLogger
scope tally.Scope
// gitFactory allows injecting a git.Interface constructor for testing
gitFactory func(directory string) git.Interface
graphRunner graphrunner.GraphRunner
config *config.Config
// appCtx represents the app's overall lifetime. It is passed in by the
// caller at construction and is expected to be cancelled when the whole
// application is shutting down (e.g. on SIGTERM/SIGINT). Any future
// fire-and-forget goroutines this orchestrator starts should use this
// context instead of context.Background() so they abort promptly on
// shutdown rather than running unbounded past server teardown.
//
// Per-request cancellation should still use the request's own context;
// appCtx is only for work that intentionally outlives the request.
appCtx context.Context
}
type Params struct {
Storage storage.Storage
RepoManager repomanager.RepoManager
Logger *zap.SugaredLogger
Scope tally.Scope
GitFactory func(directory string) git.Interface
GraphRunner graphrunner.GraphRunner
Config *config.Config // required
}
// NewNativeOrchestrator creates a new native orchestrator with the given parameters.
//
// appCtx is the application-lifetime context. Cancel it when the process is
// shutting down (e.g. wire it to SIGTERM/SIGINT in main) to abort any
// background goroutines the orchestrator spawns.
func NewNativeOrchestrator(appCtx context.Context, p Params) (Orchestrator, error) {
if p.Config == nil {
return nil, errors.New("config is required")
}
scope := p.Scope
if scope == nil {
scope = tally.NoopScope
}
return &nativeOrchestrator{
storage: p.Storage,
repoManager: p.RepoManager,
logger: p.Logger,
scope: scope.SubScope("orchestrator"),
gitFactory: p.GitFactory,
graphRunner: p.GraphRunner,
appCtx: appCtx,
config: p.Config,
}, nil
}
// GetTargetGraph is used to compute the target graph locally.
// It leases a workspace, checks out the base revision, applies the change requests, and computes the target graph.
func (b *nativeOrchestrator) GetTargetGraph(ctx context.Context, param GetTargetGraphParam) (_ storage.GraphReader, retErr error) {
scope := b.scope.SubScope("get_target_graph")
scope.Counter("calls").Inc(1)
defer func() {
if retErr != nil {
scope.Counter("failure").Inc(1)
var ce common.ClassifiedError
if !errors.As(retErr, &ce) {
ce = common.WithReason(common.FailureReasonUnknown, common.ErrorTypeInfra, retErr)
}
scope.Tagged(map[string]string{
"failure_type": ce.Type(),
"failure_reason": ce.Reason(),
}).Counter("failure_type").Inc(1)
} else {
scope.Counter("success").Inc(1)
}
}()
logger := b.logger.With(zap.Any("build_description", param.Req.BuildDescription))
logger.Infow("GetTargetGraph: Processing request")
remote := param.Req.BuildDescription.Remote
repoCfg, ok := b.config.GetRepositoryConfig(remote)
if !ok {
return nil, fmt.Errorf("no repository configuration found for remote %q", remote)
}
ws, err := b.repoManager.Lease(ctx, *param.Req.BuildDescription)
if err != nil {
return nil, fmt.Errorf("lease workspace: %w", err)
}
defer func() {
err := ws.Release()
if err != nil {
// clean up the workspace if release fails.
if removeErr := os.RemoveAll(ws.Path()); removeErr != nil {
logger.Errorw("GetTargetGraph: Failed to remove workspace", zap.Error(removeErr))
}
}
}()
err = ws.Checkout(ctx, param.Req.BuildDescription.Remote, param.Req.BuildDescription.BaseSha)
if err != nil {
return nil, fmt.Errorf("checkout %s@%s: %w", param.Req.BuildDescription.Remote, param.Req.BuildDescription.BaseSha, err)
}
logger.Infow("GetTargetGraph: Checked out base revision")
requests := make([]workspace.Request, 0, len(param.Req.BuildDescription.Requests))
gitFactory := b.gitFactory
if gitFactory == nil {
gitFactory = func(dir string) git.Interface { return git.New(dir, b.logger) }
}
gitModule := gitFactory(ws.Path())
for _, req := range param.Req.BuildDescription.Requests {
request, err := workspace.NewRequest(req.GetUrl(), gitModule, param.Req.BuildDescription.BaseSha, req.GetCommit(), logger)
if err != nil {
return nil, fmt.Errorf("create request for %q: %w", req.GetUrl(), err)
}
requests = append(requests, request)
}
err = ws.ApplyRequests(ctx, requests)
if err != nil {
return nil, fmt.Errorf("apply requests: %w", err)
}
logger.Infow("GetTargetGraph: Applied requests", zap.Int("request_count", len(requests)))
// Compute the treehash and download the target graph from storage if exists.
treehash, err := gitModule.RevParse(ctx, "HEAD^{tree}")
if err != nil {
return nil, fmt.Errorf("compute treehash: %w", err)
}
treehashPath := common.GetGraphByTreeHash(param.Req.BuildDescription.Remote, treehash, param.Req.BuildDescription.GetStrategy(), param.Req.GetRequestOptions())
if !param.BypassCache {
graphReader, err := storage.NewGraphReader(ctx, b.storage, treehashPath)
if err == nil {
logger.Infow("GetTargetGraph: Cache hit on treehash", zap.String("treehash", treehash))
return graphReader, nil
}
if !storage.IsNotFound(err) {
return nil, fmt.Errorf("read graph at treehash %s: %w", treehash, err)
}
logger.Infow("GetTargetGraph: Treehash not found, computing target graph", zap.String("treehash", treehash))
} else {
logger.Infow("GetTargetGraph: bypass_cache=true, computing target graph")
}
// Compute the target graph and store it in storage.
runner := b.graphRunner
if runner == nil {
client, err := bazel.NewBazelClient(ctx, bazel.Params{
WorkspacePath: ws.Path(),
Logger: b.logger,
BazelCommand: repoCfg.BazelCommand,
QueryTimeout: time.Duration(repoCfg.QueryTimeout) * time.Second,
StreamLogs: repoCfg.StreamBazelLogs,
})
if err != nil {
return nil, fmt.Errorf("create bazel client: %w", err)
}
// Use default native graph runner
runner = graphrunner.NewNativeGraphRunner(graphrunner.NativeGraphRunnerParams{
BazelClient: client,
GitClient: gitModule,
Config: repoCfg,
ExtraExcludedFiles: param.Req.GetRequestOptions().GetExtraExcludeFilesRegex(),
Scope: b.scope,
})
}
result, err := runner.Compute(ctx, ws)
if err != nil {
return nil, fmt.Errorf("compute target graph: %w", err)
}
responses, err := common.ResultToGetTargetGraphResponse(ctx, result,
b.config.Service.Chunking.TargetChunkSize,
b.config.Service.Chunking.MetadataMapChunkSize,
)
if err != nil {
return nil, fmt.Errorf("convert target graph to response: %w", err)
}
err = storage.WriteGraphStream(ctx, b.storage, treehashPath, responses)
if err != nil {
return nil, fmt.Errorf("write graph to storage at %s: %w", treehashPath, err)
}
treehashCachePath := common.GetTreehashCachePath(param.Req.BuildDescription)
treehashReader := bytes.NewReader([]byte(treehash))
err = b.storage.Put(ctx, storage.UploadRequest{Key: treehashCachePath, Reader: treehashReader})
if err != nil {
return nil, fmt.Errorf("store treehash mapping at %s: %w", treehashCachePath, err)
}
graphReader, err := storage.NewGraphReader(ctx, b.storage, treehashPath)
if err != nil {
return nil, fmt.Errorf("create graph reader at %s: %w", treehashPath, err)
}
logger.Infow("GetTargetGraph: Done computing and storing target graph", zap.String("treehash", treehash))
return graphReader, nil
}