Bazel Remote Caching实践

本文介绍了Bazel Remote Caching的实践,包括使用nginx+本地文件系统作为缓存后端的配置步骤,以及Bazel Remote Cache的安装和运行。通过比较两者,Bazel Remote Cache在特性上表现出更优,如支持PUT/GET请求、快速文件管理、统计接口和LRU管理。尽管仍处于尝试阶段,但其易用性和可维护性推荐在实际项目中使用。

整体方案

远程缓存,用于开发组内部或持续集成(CI )系统内,进行构建输出的共享。如果你的构建是需要反复执行的,一台机器的输出能够被另外一台机器安全的重用,那么这将会使构建速度显著的提升。

参照Bazel Remote Caching,现在有很多种后端解决方案,都可以被用来作为远程缓存。有如下选项:

本篇为Bazel Remote Caching的各个方案实践。

nginx+本地文件系统

安装nginx

  1. 获取nginx的docker镜像。

    $ docker pull nginx
  2. 创建nginx相关目录,用于存放后面的一些文件。

    mkdir  -p ~ /nginx/www  ~ /nginx/logs  ~ /nginx/conf

配置nginx

  1. 启动nginx,使其能够使用共享目录存储。

    export  PWD=` 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
  2. 配置nginx,使其能够处理Bazel的HTTP缓存协议。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    server {
        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 PUT
            dav_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文件,并进行如下配置:

  1. 配置Bazel为使用Remote Cache

    build --spawn_strategy=remote --genrule_strategy=remote
    build --strategy=Javac=remote --strategy=Closure=remote
  2. 指定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编译生成的缓存。

 Expand source

 

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文件,并进行如下配置:

  1. 配置Bazel为使用Remote Cache

    build --spawn_strategy=remote --genrule_strategy=remote
    build --strategy=Javac=remote --strategy=Closure=remote
  2. 指定Bazel Remote Cache的IP地址和端口

    build --remote_http_cache=http: //127 .0.0.1:9090
  3. 构建Bazel工程

    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ bazel build  //main :hello-world --verbose_failures
    Extracting 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-world
    INFO: Elapsed  time : 9.899s, Critical Path: 1.45s
    INFO: Build completed successfully, 9 total actions

查看结果和统计

  • 查看结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    ls  bazel_cache/*
    bazel_cache /ac :
    15f49e602ad2c82cbe759f7b7310d91ddbd9a061b70368e4c189f2113bcb5d02 88088f27ef879ba2f82dea78ec746b7ddcab16749662515bc152a7a876f9297f
    44ac7b2e16c60198a149677e8f35bbe118a8532fd455ef0146f43d4f419b74dd 978f605c645444576cf48229c0bf6184101d030eebd21abd82d8160665f85032
    5205dc6763dfa091b8f04d4a16b63778396d413a2c554bd36c8e43679b323df2 bfd62672e515977f81144f95e8ca79fba3a40ba39fa26c680eafe32a9c1a0cb0
    bazel_cache /cas :
    114d4c09f4a58d94f9bdb6076dcf1e2b1b30a182db244127c1732abe2d93abc0 c7fc55c90eb4f64b62ab7b69de4d0c6699a7eb5332363211d0e3dc13b07f370e
    3a0d0bdaf602f40fd3b1fe97f78926ee82fe85bb4d29f94d90ab463ed9195c96 e279fe99f46d7fda095270c8f695afdb4c51a4e2baaea32a1dd1db3daac24ab7
    694c29de86f3c2e7f5e2a63323cb2d1d0bdec95d468df679e351e3ec89e22e70 e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
    76c48cd28ebd2c5c38c1b698422a7327e3c598e47db6334f5320dce7080de9e6 f26707dedd82605863117bcd3d4716eaedd4e5b4fa609e632b3d13a8a04535a0
    9b4540215195018e0a32a69e7d0e7e20dc27c6894842b09da7af383c9adc5f23 fe5eb96a5055c6592dc8766d5e2ab32db6f3c2b334f2941c411f794ca3571702
  • 查看统计
    在浏览器中,输入:http://127.0.0.1:9090/status,显示如下:

    1
    2
    3
    4
    5
    6
    {
      "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。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值