-
Notifications
You must be signed in to change notification settings - Fork 0
/
localexec.go
100 lines (88 loc) · 2.9 KB
/
localexec.go
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
// Copyright 2023 Google LLC
//
// 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 reproxy
import (
"context"
"os/exec"
"time"
"github.com/bazelbuild/reclient/internal/pkg/labels"
"github.com/bazelbuild/reclient/internal/pkg/localresources"
"github.com/bazelbuild/reclient/internal/pkg/logger"
"github.com/bazelbuild/reclient/internal/pkg/logger/event"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/command"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/outerr"
lpb "github.com/bazelbuild/reclient/api/log"
)
type requirements struct {
cpus int64
ramMBs int64
}
var (
defaultReqs = requirements{1, 512}
lblReqs = map[labels.Labels]requirements{
labels.MetalavaLabels(): {8, 8192},
labels.JavacLabels(): {8, 4096},
labels.R8Labels(): {8, 4096},
labels.D8Labels(): {8, 4096},
labels.ClangLinkLabels(): {1, 8192},
}
)
// Executor can run commands and retrieve their outputs.
type Executor interface {
// ExecuteWithOutErr runs the given command inside the working directory.
ExecuteWithOutErr(ctx context.Context, cmd *command.Command, oe outerr.OutErr) error
}
// LocalPool is responsible for executing commands locally.
type LocalPool struct {
executor Executor
resMgr *localresources.Manager
}
// NewLocalPool creates a pool with the given args.
func NewLocalPool(exec Executor, resMgr *localresources.Manager) *LocalPool {
return &LocalPool{
executor: exec,
resMgr: resMgr,
}
}
// Run runs a command locally. Returns the stdout, stderr, exit code, and error in case more
// information about the failure is needed.
func (l *LocalPool) Run(ctx, cCtx context.Context, cmd *command.Command, lbls map[string]string, oe outerr.OutErr, rec *logger.LogRecord) (int, error) {
var req requirements
var ok bool
if req, ok = lblReqs[labels.FromMap(lbls)]; !ok {
req = defaultReqs
}
if rec.GetLocalMetadata() == nil {
rec.LocalMetadata = &lpb.LocalMetadata{}
}
qt := time.Now()
release, err := l.resMgr.Lock(cCtx, req.cpus, req.ramMBs)
et := rec.RecordEventTime(event.LocalCommandQueued, qt)
if err != nil {
return 0, err
}
defer release()
defer func() {
rec.RecordEventTime(event.LocalCommandExecution, et)
}()
if v := ctx.Value(testOnlyBlockLocalExecKey); v != nil {
v.(func())()
}
err = l.executor.ExecuteWithOutErr(ctx, cmd, oe)
exitCode := 0
if exitErr, _ := err.(*exec.ExitError); exitErr != nil {
exitCode = exitErr.ExitCode()
}
return exitCode, err
}