|
| 1 | +# Persistent Worker Demo |
| 2 | + |
| 3 | +At the time of writing (2024-09-25) Buck2 supports persistent workers for local |
| 4 | +builds through a dedicated Buck2 persistent worker gRPC protocol. However, Buck2 |
| 5 | +does not support persistent workers for builds that use remote execution. This |
| 6 | +demo is part of a patch-set that adds support for remote persistent workers to |
| 7 | +Buck2, see [#776]. |
| 8 | + |
| 9 | +[#776]: https://github.com/facebook/buck2/issues/776 |
| 10 | + |
| 11 | +## Requirements |
| 12 | + |
| 13 | +This demo uses BuildBuddy remote execution to demonstrate remote persistent |
| 14 | +workers. You will need an API token for at least a free open source account. You |
| 15 | +can use [direnv] to set up the environment: |
| 16 | + |
| 17 | +Credentials for [BuildBuddy] stored in `.envrc.private`: |
| 18 | + |
| 19 | +``` |
| 20 | +export BUILDBUDDY_API_KEY=... |
| 21 | +``` |
| 22 | + |
| 23 | +On CI the API key is not available for pipelines initiated from forks of the |
| 24 | +main Buck2 repository. The corresponding tests will be skipped in that case. A |
| 25 | +Meta engineer can manually initiate a pipeline run with the token set. |
| 26 | + |
| 27 | +[direnv]: https://direnv.net/ |
| 28 | +[BuildBuddy]: https://www.buildbuddy.io/ |
| 29 | + |
| 30 | +## Local Build |
| 31 | + |
| 32 | +Configure a local build without persistent workers: |
| 33 | + |
| 34 | +``` |
| 35 | +$ cd examples/persistent_worker |
| 36 | +$ echo '<file:.buckconfig.no-workers>' > .buckconfig.local |
| 37 | +``` |
| 38 | + |
| 39 | +Run a clean build: |
| 40 | + |
| 41 | +``` |
| 42 | +$ buck2 clean; buck2 build : -vstderr |
| 43 | +... |
| 44 | +stderr for root//:demo-7 (demo): |
| 45 | +... |
| 46 | +ONE-SHOT START |
| 47 | +... |
| 48 | +``` |
| 49 | + |
| 50 | +## Local Persistent Worker |
| 51 | + |
| 52 | +Configure a local build with persistent workers: |
| 53 | + |
| 54 | +``` |
| 55 | +$ cd examples/persistent_worker |
| 56 | +$ echo '<file:.buckconfig.local-persistent-workers>' > .buckconfig.local |
| 57 | +``` |
| 58 | + |
| 59 | +Run a clean build: |
| 60 | + |
| 61 | +``` |
| 62 | +$ buck2 clean; buck2 build : -vstderr |
| 63 | +... |
| 64 | +stderr for root//:demo-7 (demo): |
| 65 | +... |
| 66 | +Buck2 persistent worker ... |
| 67 | +... |
| 68 | +``` |
| 69 | + |
| 70 | +## Remote Execution |
| 71 | + |
| 72 | +Configure a remote build without persistent workers: |
| 73 | + |
| 74 | +``` |
| 75 | +$ cd examples/persistent_worker |
| 76 | +$ echo '<file:.buckconfig.buildbuddy>' > .buckconfig.local |
| 77 | +``` |
| 78 | + |
| 79 | +Run a clean build: |
| 80 | + |
| 81 | +``` |
| 82 | +$ buck2 clean; buck2 build : -vstderr |
| 83 | +... |
| 84 | +stderr for root//:demo-7 (demo): |
| 85 | +... |
| 86 | +ONE-SHOT START |
| 87 | +... |
| 88 | +``` |
| 89 | + |
| 90 | +## Remote Persistent Worker |
| 91 | + |
| 92 | +Configure a remote build with persistent workers: |
| 93 | + |
| 94 | +``` |
| 95 | +$ cd examples/persistent_worker |
| 96 | +$ echo '<file:.buckconfig.buildbuddy-persistent-workers>' > .buckconfig.local |
| 97 | +``` |
| 98 | + |
| 99 | +Run a clean build: |
| 100 | + |
| 101 | +``` |
| 102 | +$ buck2 clean; buck2 build : -vstderr |
| 103 | +... |
| 104 | +stderr for root//:demo-7 (demo): |
| 105 | +... |
| 106 | +Bazel persistent worker ... |
| 107 | +... |
| 108 | +``` |
| 109 | + |
| 110 | +## Protocol |
| 111 | + |
| 112 | +### Starlark |
| 113 | + |
| 114 | +A Buck2 persistent worker is created by a rule that emits the `WorkerInfo` |
| 115 | +provider. Setting `remote = True` on this provider indicates that this worker is |
| 116 | +remote execution capable. |
| 117 | + |
| 118 | +Buck2 actions indicate that they can utilize a persistent worker by setting the |
| 119 | +`exe` parameter to `ctx.actions.run` to `WorkerRunInfo(worker, exe)`, where |
| 120 | +`worker` is a `WorkerInfo` provider, and `exe` defines the fallback executable |
| 121 | +for non persistent-worker execution. |
| 122 | + |
| 123 | +Buck2 actions that want to utilize a remote persistent worker must pass |
| 124 | +command-line arguments in an argument file specified as `@argfile`, |
| 125 | +`-flagfile=argfile`, or `--flagfile=argfile` on the command-line. |
| 126 | + |
| 127 | +### Local Persistent Worker |
| 128 | + |
| 129 | +A locally executed Buck2 persistent worker falls under the |
| 130 | +[Buck2 persistent worker protocol](./proto/buck2/worker.proto): It is started |
| 131 | +and managed by Buck2 and passed a file path in the `WORKER_SOCKET` environment |
| 132 | +variable where it should create a gRPC Unix domain socket to serve worker |
| 133 | +requests over. Multiple requests may be sent in parallel and expected to be |
| 134 | +served at the same time depending on the `concurrency` attribute of the |
| 135 | +`WorkerInfo` provider. |
| 136 | + |
| 137 | +### Remote Persistent Worker |
| 138 | + |
| 139 | +A remotely executed Buck2 persistent worker falls under the |
| 140 | +[Bazel persistent worker protocol](./proto/bazel/worker_protocol.proto): It is |
| 141 | +started and managed by the remote execution system. Work requests are sent as |
| 142 | +length prefixed protobuf objects to the standard input of the worker process. |
| 143 | +Work responses are expected as length prefixed protobuf objects on the standard |
| 144 | +output of the worker process. The worker process may not use standard output for |
| 145 | +anything else. |
0 commit comments