indexserver: assert and check for ownership on index directory (#347)
We had a customer misconfigure a zoekt setup such that there were
multiple replicas owning the same directory. This lead to hard to debug
bugs. Instead we will now write a file (owner.txt) which contains the
identity of the owner and proactively look for changes in ownership.
Test Plan: go test and the following manual test plan
$ go run ./cmd/zoekt-sourcegraph-indexserver -sourcegraph_url ~/src -debug
# hit ctrl-c
# now I expect it to have my computers hostname "habitat"
$ cat ~/.zoekt/owner.txt
DO NOT EDIT! generated by zoekt-sourcegraph-indexserver.
This file records the identity of the owner of this zoekt index directory.
If it changes zoekt-sourcegraph-indexserver will exit with a non-zero exit code.
This is to prevent multiple owners/writers.
hostname=habitat
# Now I will run with an overrided hostname, I expect a warning and a new hostname
$ go run ./cmd/zoekt-sourcegraph-indexserver -hostname=foobarbaz -sourcegraph_url ~/src -debug
2022/05/19 16:43:10 WARN: detected a change in ownership at startup. You can ignore this if you only have one zoekt replica: detected a change of ownership of /home/keegan/.zoekt/owner.txt. In multiple replica setups this can lead to un-needed rebalancing or bugs if there are multiple writers: owner="habitat" current="foobarbaz"
# Now I mutate ~/.zoekt/owner.txt to have a different hostname and I expect
the program to crash
2022/05/19 16:47:47 detected a change of ownership of /home/keegan/.zoekt/owner.txt. In multiple replica setups this can lead to un-needed rebalancing or bugs if there are multiple writers: owner="boom" current="foobarbaz"
exit status 1