Go代码中的动态数据竞赛检测
Uber has extensively adopted Go as a primary programming language for developing microservices. Our Go monorepo consists of about 50 million lines of code and contains approximately 2,100 unique Go services. Go makes concurrency a first-class citizen; prefixing function calls with the go keyword runs the call asynchronously. These asynchronous function calls in Go are called goroutines. Developers hide latency (e.g., IO or RPC calls to other services) by creating goroutines within a single running Go program. Goroutines are considered “lightweight”, and the Go runtime context switches them on the operating-system (OS) threads. Go programmers often use goroutines liberally. Two or more goroutines can communicate data either via message passing (channels) or shared memory. Shared memory happens to be the most commonly used means of data communication in Go.
Uber已经广泛地采用Go作为开发微服务的主要编程语言。我们的Go monorepo由大约5000万行代码组成,包含大约2100个独特的Go服务。Go使并发性成为一流的公民;在函数调用前加上go关键字,就会异步运行调用。Go中的这些异步函数调用被称为goroutines。开发人员通过在单个运行的Go程序中创建goroutines来隐藏延迟(例如,对其他服务的IO或RPC调用)。goroutines被认为是 "轻量级的",Go的运行时上下文在操作系统(OS)线程上切换它们。Go程序员经常随意使用goroutines。两个或多个goroutines可以通过消息传递(通道)或共享内存进行数据通信。共享内存恰好是Go中最常用的数据通信手段。
A data race occurs in Go when two or more goroutines access the same memory location, at least one of them is a write, and there is no ordering between them, as defined by the Go memory model. Outages caused by data races in Go programs are a recurring and painful problem in our microservices. These issues have brought down our critical, customer-facing services for hours in total, causing inconvenience to our customers and impacting our revenue. In this blog, we discuss deploying Go’s default dynamic race detector to continuously detect data races in our Go development environment. This deployment has enabled detection of more than 2,000 races resulting in ~1,000 data races fixed by more than two hundred engineers.
在Go中,当两个或更多的goroutine访问同一个内存位置,其中至少有一个是写的,而且它们之间没有排序,这就是Go内存模型所定义的数据竞赛。在我们的微服务中,由Go程序中的数据竞赛引起的中断是一个反复出现的痛苦问题。这些问题使我们关键的、面向客户的服务总共...