整体方案
远程缓存,用于开发组内部或持续集成(CI )系统内,进行构建输出的共享。如果你的构建是需要反复执行的,一台机器的输出能够被另外一台机器安全的重用,那么这将会使构建速度显著的提升。
参照Bazel Remote Caching,现在有很多种后端解决方案,都可以被用来作为远程缓存。有如下选项:
本篇为Bazel Remote Caching的各个方案实践。
nginx+本地文件系统
安装nginx
获取nginx的docker镜像。
$ docker pull nginx创建nginx相关目录,用于存放后面的一些文件。
$mkdir-p ~/nginx/www~/nginx/logs~/nginx/conf
配置nginx
启动nginx,使其能够使用共享目录存储。
$exportPWD=`pwd`/nginx$ docker run -p 80:80 --name mynginx -v$PWD/www:/www-v$PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v$PWD/logs:/wwwlogs-d nginx配置nginx,使其能够处理Bazel的HTTP缓存协议。
1234567891011121314151617181920server {listen 80;listen 443 ssl;server_name cache.bazel.test.co;ssl_certificate /etc/nginx/ssl/cache.bazel.test.co.crt;ssl_certificate_key /etc/nginx/ssl/cache.bazel.test.co.key;access_log /wwwlogs/cache.bazel.test.co.access.log logstash;error_log /wwwlogs/cache.bazel.test.co.error.log;location ~ /(ac|cas) {# The path to the directory where nginx should store the cache contents.root /www/cache;# Allow PUTdav_methods PUT;# Allow nginx to create the /ac and /cas subdirectories.create_full_put_path on;# The maximum size of a single file.client_max_body_size 1G;allow all;}}
配置Bazel工程
在Bazel工程的工程目录下,创建.bazelrc文件,并进行如下配置:
配置Bazel为使用Remote Cache
build --spawn_strategy=remote --genrule_strategy=remotebuild --strategy=Javac=remote --strategy=Closure=remote指定Bazel Remote Cache的IP地址和端口(由于使用了hosts绑定,直接使用了域名)
build --remote_http_cache=http://cache.bazel.test.co
使用Remote Cache
一切都准备就绪,使用Bazel构建一波,查看下Remote Cache的魅力。
这里使用了Bazel提供的官方示例代码,进行编译。
|
1
2
3
4
5
6
7
|
$ bazel build
//main
:hello-world --verbose_failures
INFO: Analysed target
//main
:hello-world (5 packages loaded).
INFO: Found 1 target...
Target
//main
:hello-world up-to-
date
:
bazel-bin
/main/hello-world
INFO: Elapsed
time
: 2.383s, Critical Path: 1.64s
INFO: Build completed successfully, 9 total actions
|
查看Bazel编译生成的缓存。
Bazel Remote Cache
安装Bazel Remote Cache
Bazel Remote Cache是一个用golang编写的,可以支持HTTP Remote Cache协议的远程缓存服务应用。可通过--max_size=N设置缓存的大小(单位:GB),缓存不够时,将使用LRU(Latest Recently Used)策略,将缓存强制清除。
Bazel Remote Cache为开源项目,且有docker镜像。可以直接通过docker pull的方式,将镜像下载。
$ docker pull buchgr
/bazel-remote-cache
|
设置最大缓存并运行
- 使用--max_size=N(单位:GB)设置缓存大小
- 创建本地目录bazel_cache,并将其共享为Bazel Remote Cache的缓存目录
运行docker容器
docker run -v`pwd`/bazel_cache/:/data-p 9090:8080 buchgr/bazel-remote-cache--max_size=5
使用Bazel Remote Cache
在Bazel工程的工程目录下,创建.bazelrc文件,并进行如下配置:
配置Bazel为使用Remote Cache
build --spawn_strategy=remote --genrule_strategy=remotebuild --strategy=Javac=remote --strategy=Closure=remote指定Bazel Remote Cache的IP地址和端口
build --remote_http_cache=http://127.0.0.1:9090构建Bazel工程
123456789$ bazel build//main:hello-world --verbose_failuresExtracting Bazel installation.................................INFO: Analysed target//main:hello-world (7 packages loaded).INFO: Found 1 target...Target//main:hello-world up-to-date:bazel-bin/main/hello-worldINFO: Elapsedtime: 9.899s, Critical Path: 1.45sINFO: Build completed successfully, 9 total actions
查看结果和统计
查看结果
1234567891011$lsbazel_cache/*bazel_cache/ac:15f49e602ad2c82cbe759f7b7310d91ddbd9a061b70368e4c189f2113bcb5d02 88088f27ef879ba2f82dea78ec746b7ddcab16749662515bc152a7a876f9297f44ac7b2e16c60198a149677e8f35bbe118a8532fd455ef0146f43d4f419b74dd 978f605c645444576cf48229c0bf6184101d030eebd21abd82d8160665f850325205dc6763dfa091b8f04d4a16b63778396d413a2c554bd36c8e43679b323df2 bfd62672e515977f81144f95e8ca79fba3a40ba39fa26c680eafe32a9c1a0cb0bazel_cache/cas:114d4c09f4a58d94f9bdb6076dcf1e2b1b30a182db244127c1732abe2d93abc0 c7fc55c90eb4f64b62ab7b69de4d0c6699a7eb5332363211d0e3dc13b07f370e3a0d0bdaf602f40fd3b1fe97f78926ee82fe85bb4d29f94d90ab463ed9195c96 e279fe99f46d7fda095270c8f695afdb4c51a4e2baaea32a1dd1db3daac24ab7694c29de86f3c2e7f5e2a63323cb2d1d0bdec95d468df679e351e3ec89e22e70 e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85576c48cd28ebd2c5c38c1b698422a7327e3c598e47db6334f5320dce7080de9e6 f26707dedd82605863117bcd3d4716eaedd4e5b4fa609e632b3d13a8a04535a09b4540215195018e0a32a69e7d0e7e20dc27c6894842b09da7af383c9adc5f23 fe5eb96a5055c6592dc8766d5e2ab32db6f3c2b334f2941c411f794ca3571702查看统计
在浏览器中,输入:http://127.0.0.1:9090/status,显示如下:123456{"CurrSize": 71629,"MaxSize": 5368709120,"NumFiles": 16,"ServerTime": 1525242652}
缓存使用对比
初始化缓存
在本地环境下,使用Bazel构建,并上传缓存。
|
1
2
3
4
5
6
7
8
9
10
11
|
$ bazel build
//main:hello-world
Extracting Bazel installation...
Starting local Bazel server and connecting to it...
...............
INFO: Analysed target
//main:hello-world (7 packages loaded).
INFO: Found 1 target...
Target
//main:hello-world up-to-date:
bazel-bin/main/hello-world
INFO: Elapsed time: 5.567s, Critical Path: 0.78s
INFO: 5 processes, local.
INFO: Build completed successfully, 8 total actions
|
使用缓存
在docker环境中,使用Bazel从Remote Cache获取缓存,到本地,并复用。
|
1
2
3
4
5
6
7
8
9
10
11
|
bazel build //main:hello
-world
Extracting Bazel installation...
Starting local Bazel server and connecting to it...
................
INFO: Analysed target //main:hello
-world
(7 packages loaded).
INFO: Found 1 target...
Target //main:hello
-world
up
-to
-date
:
bazel
-bin
/main/hello
-world
INFO: Elapsed time: 5.245s, Critical Path: 0.49s
INFO: 5 processes: 4 remote cache hit, 1 local.
INFO: Build completed successfully, 8 total actions
|
结论
通过上述两种方案:nginx+本地缓存,Bazel Remote Cache的比较
Bazel Remote Cache在以下特性上表现更加明显:
- 针对Bazel Remote Cache设计的系统,支持PUT和GET请求。
- 使用Map表,将文件key值进行缓存,方便快速查找和管理文件。
- 提供统计接口,Json结果输出,方便二次开发和扩展。
- 使用LRU机制,对缓存进行管理,不需要使用定时任务扫描过期文件,并删除。
- 现有的docker镜像,部署方便,不需要进行多余的HTTP方面的配置。
缺点
- 尝试阶段,不提供支持
从易用性和可维护性来看,建议使用Bazel Remote Cache。
本文介绍了Bazel Remote Caching的实践,包括使用nginx+本地文件系统作为缓存后端的配置步骤,以及Bazel Remote Cache的安装和运行。通过比较两者,Bazel Remote Cache在特性上表现出更优,如支持PUT/GET请求、快速文件管理、统计接口和LRU管理。尽管仍处于尝试阶段,但其易用性和可维护性推荐在实际项目中使用。
2394

被折叠的 条评论
为什么被折叠?



