围棋中的数据竞赛模式

Data Race Patterns in Go

Uber has adopted Golang (Go for short) as a primary programming language for developing microservices. Our Go monorepo consists of about 50 million lines of code (and growing) and contains approximately 2,100 unique Go services (and growing).

Uber已经采用Golang(简称Go)作为开发微服务的主要编程语言。我们的Go monorepo由大约5000万行代码组成(并且还在增长),包含大约2100个独特的Go服务(并且还在增长)。

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. 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.

Go使并发性成为一流的公民;在函数调用前加上go关键字,就可以异步运行调用。Go中的这些异步函数调用被称为goroutines。开发者通过创建goroutines来隐藏延迟(例如,对其他服务的IO或RPC调用)。两个或多个goroutines可以通过消息传递(通道)或共享内存进行数据通信。共享内存正好是Go中最常用的数据通信方式。

Goroutines are considered “lightweight” and since they are easy to create, Go programmers use goroutines liberally. As a result, we notice that programs written in Go, typically, expose significantly more concurrency than programs written in other languages. For example, by scanning hundreds of thousands of microservice instances running our data centers, we found out that Go microservices expose ~8x more concurrency compared to Java microservices. Higher concurrency also means potential for more concurrency bugs. A data race is a concurrency bug that occurs when two or more goroutines access the same datum, at least one of them is a write, and there is no ordering between them. Data races are insidious bugs and must be avoided at all costs.

Goroutines被认为是 "轻量级的",由于它们很容易创建,Go程序员们大量使用Goroutines。因此,我们注意到,用Go编写的程序,通常比用其他语言编写的程序暴露出更多的并发性。例如,通过扫描运行在我们数据中心的数十万个微服务实例,我们发现Go微服务暴露的并发性比Java微服务高8倍。更高的并发性也意味着可能出现更多的并发性错误。数据竞赛是一种并发错误,它发生在两个或更多的goroutine访问同一个数据,其中至少有一个是写的,而且它们之间没有排序。数据竞赛是阴险的bug,必须不惜一切代价加以避免。

We developed a system to detect data races at Uber using a dynamic data ...

开通本站会员,查看完整译文。

首页 - Wiki
Copyright © 2011-2024 iteam. Current version is 2.125.1. UTC+08:00, 2024-05-17 16:44
浙ICP备14020137号-1 $访客地图$