Reproducing flaky Go tests using Linux cgroups and systemd

Sometimes, the -race option might not be enough to trigger/debug races in Go tests. You might have time.Sleep() in a test thinking that some event will surely trigger in some time. You might run it on GitHub actions on a shared runner. Alas, you don’t see that event happen. What could have caused this? Most of the time it is because of the minimal CPU time allocated for your tests. The runners are shared between many projects and thus sometimes CPU might be very split between many processes.

Reproducing a limited CPU environment locally might not be the most straightforward task. Go tests might spawn child processes or even Docker containers in case of end-to-end tests. Facilities for limiting available processor time are different between operating systems. In this post, I will tell you how to do this easily using systemd-run and https://github.com/efficientgo/e2e. The advice regarding end-to-end tests is applicable to any e2e testing framework that uses Docker underneath. I have tried using https://manpages.ubuntu.com/manpages/xenial/man1/cpulimit.1.html cpulimit(1) before however it always continuously sends SIGSTOP/SIGCONT signals to the processes and that is very annoying. Also, from my experience, it hogs the CPU for some reason so it’s a non-goer. Let’s use cgroups which was exactly made for this – controlling resources available to processes on Linux systems! Also, this is the same exact mechanism used by Kubernetes.

systemd-run is a nice wrapper around running processes in individual cgroups. It is available in Linux distributions that use the systemd system daemon. You can find its manual page here. I came up with this concoction that runs a Go test in a CPU limited systemd slice:

systemd-run -E GO111MODULE=on -E GOPATH="${GOPATH}" --working-directory="$(pwd)" -p CPUQuota=10% -P -G --user /bin/sh -c '/bin/go test -count=1 -v -timeout 10m -run ^TestStoreGatewayBytesLimit$ github.com/thanos-io/thanos/test/e2e'

This command will run go test in a temporary systemd unit with a tty attached to it i.e. it is interactive. Also, that temporary unit will be recycled after the test. Meaning that you can simply run this command over and over in a shell of your choice. The 10% is the amount of CPU time you want to allocate to this test.

If you are using some kind of e2e testing framework for Go that uses Docker then you can achieve this for spawned processes through the --cpus parameter to docker run. efficientgo/e2e makes this easy for you by providing the ability to set an environment variable that is used as the value for the --cpus parameter. Simply set E2E_DOCKER_CPUS in the Go test through t.Setenv("E2E_DOCKER_CPUS", ...) or do export E2E_DOCKER_CPUS=0.1 before-hand.

All in all, the Linux operating system provides a great way of allocating CPU time available through processes through cgroupv2. It is just a matter of how we could put our processes that run tests into CPU time-limited control groups. This post provides two ways that I found useful. So far I fixed one flaky test but there are much more of them. I hope this post will be useful for you too!

Cheat Sheet Of How To Get Accepted Into LFX

Hello everyone! I’ve been a mentor in the LFX program for quite a few semesters and I have been participating in it since the complete beginning, when it was still called community bridge. I also participated in GSoC once as a mentor. Since I get questions about how to get accepted into LFX, I thought about writing an article about this topic. Please note that even if you will follow everything to the letter, you still might not get accepted. There is some luck involved but I believe that my suggestions greatly increase your chance of getting accepted as a mentee.  

I would divide all suggestions into two parts – technical and non-technical. Both are equally important, perhaps the non-technical part is even more important.

Let’s start with the ability to work independently. This is a broad topic that encompasses many things. First of all, most mentors already have full-time jobs meaning that they won’t be able to give you lots of time. It is completely fine and encouraged to ask questions but it is expected that you will be doing a lot of research, reading yourself. Also, depending on the project, some experience might be expected from you. It’s not a necessity but would be a clear signal that you can tackle more complex problems relatively easily. At least from what I have seen is that mentors are looking for a combination of the following which serves as an indicator of success:

– Do you have any prior contributions to the project in question? It doesn’t have to be something huge but anything helps, especially those contributions that show that you understand what that project is about

– Perhaps you have some other contributions to similar projects? A GitHub or similar profile is always very nice.

– Last but not least, for example, if the project that you are applying to uses Go, it would be nice to see examples of Go projects on your resume.

Most if not all mentors are also looking for someone that will stay around even after the LFX. For us, it is one of the primary opportunities to attract more contributors to the community and ensure the longevity of our project. It is hard to pin down into words how to show this to potential mentors but this probably needs to reflect in your cover letter. In my personal opinion, enthusiasm is contagious and it just seeps through written words. For instance, if your cover letter is completely generic and has a bunch of sentences copied from the project’s website then it doesn’t show that you are interested in it. It’s better to talk about what you think of the original problem, and how do you think you could solve it. There are no right answers here but I think it’s better to spend some time on a few projects that interest you than to send a bunch of generic applications to many more in comparison.

There have been also a few instances where someone has clearly used ChatGPT to generate cover letters. Please don’t do that as it is dehumanizing to the mentors and because it just shows what you think of the whole process. It might seem tedious and pointless from the mentee’s point of view but mentors aren’t robots, they are volunteers, and they actually want to help you become a better version of yourself, and to improve themselves too.

When it comes to choosing a resume template, don’t worry too much about the aesthetics. The focus should be on the substance within. Since mentors may only have a few minutes per applicant, it’s advisable to include concise yet impactful information that sets you apart. Have experience in a relevant programming language? Highlight it on your resume! Have you written an impressive blog? Showcase it! While I don’t have direct experience as a recruiter or in human resources, this approach likely holds value in professional environments. Your resume should emphasize the key selling points that differentiate you, rather than delving into pages of your entire life’s story. Not just that but it also saves be it mentor’s or recruiter’s time, and there’s less to update in the future when the circumstances change.

Mentors also typically look at how busy a potential mentee is. It’s not uncommon to receive applications from individuals who are already engaged in full-time employment. While we appreciate your dedication, it’s important to note that our projects typically require a commitment of 20-40 hours per week. Considering this workload, we would advise against participating in a mentorship program alongside your current obligations. Burnout is a significant concern, and we aim to discourage such practices. Hence, the typical “ideal” LFX mentee is probably a student or someone who is without a job currently, or looking to change careers.

Finally – diversity. It’s quite a sensitive topic but in my opinion, projects need not only a continuous stream of newcomers and contributors but also a diverse collection of viewpoints and opinions to keep thriving. There are many ways to approach this problem. A good book that is tangentially related is “The Wisdom of Crowds” by James Surowiecki. I would encourage you to read it if you are interested in topics such as this. One simple method of evaluating the situation is to look at gender diversity. Software engineering has a serious problem with that in my anecdotal experience. For example, during the most recent LFX iteration, the Thanos project only received 1 application by a woman out of 45 applicants. That’s a huge disbalance. It’s not always like this, sometimes it was a bit better, but this example stuck out perhaps due to recency bias but maybe also because it illustrates the point well. Note that it’s a broader problem in the space. Linux Foundation puts a lot of effort into trying to solve this. Let’s hope that the situation will improve in the future. If you are reading this then I would encourage you to take a leap and apply to a LFX mentorship program. Mentors care about this stuff and all of their applicants. We want to have an inclusive, diverse, and welcoming ecosystem.

All in all, it could be a tough and frustrating time trying to apply to a mentorship program but I would suggest you try applying nonetheless. Don’t undervalue yourself, you are doing better than you think. Also, remember that if you get accepted then it’s a very rewarding activity. You will learn so much and it’s going to be a ton of fun! Mentors love this process too because they get to meet new people, check and improve their knowledge, and strengthen their soft skills among a plethora of other things.