DEV Community

Gerry Tan
Gerry Tan

Posted on Edited on

Building huge go repo

I came across interesting problem recently when building AzureRM: due to a massive refactoring, the repo now have a lot more files and packages

Ran out of tmp space

First, my WSL box throw tons of write /tmp/go-build123456789/...: no space left on device error. The tmp file system ran out of space.

This can be fixed be mapping another larger volume for compilation: export GOTMPDIR="$HOME/.cache/go-tmp" although I suspect this makes the build slower?

Slow compilation time

A cold build with no cache hit took 12 minutes. A common daily main pull require 5 - 7 mins depending on how much are changing.

I learned that go keep local build cache in $HOME/.cache/go-build and the morning ritual of pulling latest main to warm up the cache is really useful to speed up subsequent build throughout the day.

Stripping debugging info cuts down my build time to 10 minutes, it's not much, but why not if we don't need it? go build -ldflags="-s -w" ..

What about a more powerful machine? In my 16vCPU/64GB devbox, the cold build took 6 minutes, which is half the time, not bad!

Which got me thinking.. wouldn't it be great if devbox and my local can share the cache?

gobuildcache

I found this gobuildcache library which is pretty close to what I want: it allows me to upload the build cache to a blob store. Let's give it a try!

First I installed the binary: go install github.com/richardartoul/gobuildcache@v1

Then I created the blob store that will hold the remote cache:

RESOURCE_GROUP=my-azurerm-buildcache
LOCATION=australiaeast
STORAGE_ACCOUNT=myazurermbuildcachev1
CONTAINER=go-build-cache

az group create --name "$RESOURCE_GROUP" --location "$LOCATION"

az storage account create --name "$STORAGE_ACCOUNT" \
  --resource-group "$RESOURCE_GROUP" --location "$LOCATION" \
  --kind BlockBlobStorage --sku Premium_LRS \
  --allow-blob-public-access false --allow-shared-key-access false \
  --min-tls-version TLS1_2
STORAGE_ID=$(az storage account show --name "$STORAGE_ACCOUNT" \
  --resource-group "$RESOURCE_GROUP" --query id -o tsv)

# Grant the signed-in user data-plane access to create the container.
USER_ID=$(az ad signed-in-user show --query id -o tsv)
az role assignment create --assignee-object-id "$USER_ID" \
  --assignee-principal-type User --role 'Storage Blob Data Contributor' \
  --scope "$STORAGE_ID"

az storage container create --account-name "$STORAGE_ACCOUNT" \
  --name "$CONTAINER" --auth-mode login --public-access off
Enter fullscreen mode Exit fullscreen mode

Drop a few environment variables to tell go to use gobuildcache (hint use direnv to automate this 😉):

export GOCACHEPROG=/path/to/go/bin/gobuildcache

export GOBUILDCACHE_BACKEND_TYPE=azblob
export GOBUILDCACHE_AZURE_ACCOUNT=myazurermbuildcachev1
export GOBUILDCACHE_AZBLOB_CONTAINER=go-build-cache
export GOBUILDCACHE_READ_ONLY=true
export GOBUILDCACHE_PRINT_STATS=true
export GOBUILDCACHE_CACHE_DIR=/path/to/.cache/gobuildcache/cache
export GOBUILDCACHE_LOCK_DIR=/path/to/.cache/gobuildcache/locks
export GOBUILDCACHE_DEBUG=false
Enter fullscreen mode Exit fullscreen mode

Ran the build again in devbox to warm up my remote cache...

Tension builds.. I'll update this article when I have time to finish the work..

Top comments (0)