buchgr / bazel-remote

A remote cache for Bazel
https://bazel.build
Apache License 2.0
607 stars 156 forks source link

When set disable_http_ac_validation, enable_ac_key_instance_mangling will not work #791

Open hyphennn opened 3 days ago

hyphennn commented 3 days ago

Hi, there. I'm using bazel-remote as our cache, my command to run progress is:

./bazel-bin/bazel-remote_/bazel-remote  \
  --dir /tmp/bazel-cache/ --max_size 200 --disable_http_ac_validation=true \
  --enable_endpoint_metrics=true --enable_ac_key_instance_mangling=true

And I find that the enable_ac_key_instance_mangling doesn't work: no matter how I change instance in .bazelrc, it always hit all caches.

I find in server/http.go Line 215:

    if h.mangleACKeys && kind == cache.AC {
        hash = cache.TransformActionCacheKey(hash, instance, h.accessLogger)
    }

So, when disable_http_ac_validation==true, the function TransformActionCacheKey will never work.

I forked this repo, and modified these code, it seems work well.

So, here is my question: Is this by design? If not, I am happy to offer my pr to fix it.

mostynb commented 2 days ago

Hi, I'm not sure I understand the problem yet, but these two configuration settings should be independent:

Could you provide a test case that shows the problem?

hyphennn commented 1 day ago

Sure. I'll give an example, and I think it will be easily for you to reproduct.

Here is my command to start a bazel-remote server locally:

./bazel-bin/bazel-remote_/bazel-remote  \
  --dir /tmp/bazel-cache/ --max_size 200 --disable_http_ac_validation=true \
  --enable_ac_key_instance_mangling=true

Than I run bazel build with this remote cache and instance named 'test'

bazel build //:bazel-remote --remote_cache=http://localhost:8080/test
bazel clean
bazel build //:bazel-remote --remote_cache=http://localhost:8080/test

The second 'bazel build' is aim to validate cache hit. Of course all caches hit.In my machine, it is 264/270 as below:

INFO: 270 processes: 264 remote cache hit, 6 internal.
INFO: Build completed successfully, 270 total actions

Than I changed the instance to 'test2' with below command:

bazel clean
bazel build //:bazel-remote --remote_cache=http://localhost:8080/test2

As expected, it should not hit any cache. However, it still hits all cache:

INFO: 270 processes: 264 remote cache hit, 6 internal.
INFO: Build completed successfully, 270 total actions

I think it is easy to find the root cause. In below code, only when kind==AC, the hash will be transformed. https://github.com/buchgr/bazel-remote/blob/a563ac2ed3630e4c2539934d590d022bdbfe7321/server/http.go#L215-L217 The kind and hash are decided in below code: https://github.com/buchgr/bazel-remote/blob/a563ac2ed3630e4c2539934d590d022bdbfe7321/server/http.go#L208 Then let's see the func parseRequestURL: https://github.com/buchgr/bazel-remote/blob/a563ac2ed3630e4c2539934d590d022bdbfe7321/server/http.go#L114-L118 Only when validateAC==true, it will return AC, otherwise it will retuen RAW. And the variable validateAC is decided by settings --disable_http_ac_validation.

So, when disable_http_ac_validation==true, the kind will permanently be RAW, and the hash will never be transformed.

hyphennn commented 1 day ago

I think it's not hard to fix this problem. And I provide my pr, which has run in our bazel-remote cache with my forked repo, in #792 .