写点什么

ARTS week 1

用户头像
锈蠢刀
关注
发布于: 2020 年 05 月 27 日

Algo

https://leetcode.com/problems/number-of-islands/

Simple DFS with a visited matrix, written in golang

func numIslands(grid [][]byte) int {
var rows = len(grid)
var cols = len(grid[0])
visited := make([][]bool, rows)
for row := range visited {
visited[row] = make([]bool, cols)
}
var count = 0
for i := 0 ; i < rows ; i++ {
for j := 0 ; j < cols ; j++ {
if grid[i][j] == '0' || visited[i][j]{
continue
}
do(grid, visited, i, j)
count++
}
}
return count
}
func do(A [][]byte, V [][]bool, i int, j int) {
if i < 0 || i >= len(A) {
return
}
if j < 0 || j >= len(A[0]) {
return
}
if A[i][j] == '0' || V[i][j]{
return
}
A[i][j] = '0'
V[i][j] = true
do(A, V, i - 1, j)
do(A, V, i + 1, j)
do(A, V, i, j + 1)
do(A, V, i, j - 1)
}



Review

https://medium.com/naukri-engineering/garbage-collection-in-elasticsearch-and-the-g1gc-16b79a447181



Chose this article because it does a benchmark of G1GC (a new java gc impl) with ElasticSearch which demonstrated some promising stuff of G1GC.



A bit more context of this article, if you have worked with ElasticSearch, or if you have written enterprise Java code, hopefully you should already share the headache caused by GC. In production, even a short 2~3 seconds STW triggered by full GC can cause you some non-trivial problems because it essentially pauses your service while collecting garbage.



In case of ElasticSearch, I've seen STW causes a query to timeout, which gets propagated all the way up to api gateway, in this situation, if your services do not have proper timeout/circuit/retry protection, it could end up with something commonly referred to as an avalanche, where increasing large amount of threads / goroutines hang there waiting for slow queries to complete, causing resource spikes and even service outage if you are unlucky :)



This is why I'm personally very interested to see if better GC can be done for ElasticSearch. From this article, it looks like G1GC is now compatible with ES (previously there was some incompatibility with lucene), and GCG1 has an observable gain on GC pauses, at the cost of slightly increased CPU. Good stuff.



Tip

分享一个golang UT coverage的command alias, 它会跑当前目录下所有的testcase并生成一个带有coverage的html并打开,写UT的时候非常handy.

go test -c -coverpkg=. -o /tmp/example -tags test && /tmp/example -test.coverprofile=/tmp/profile && go tool cover -html /tmp/profile -o /tmp/profile.html && open /tmp/profile.html




Share

https://www.infoq.cn/article/qzrGhlzq_HikN10xC5jC 聊到了学习Go时需要扭转的几个思维. 对于长期活在java c#世界中的人来说可能非常有意思.

用户头像

锈蠢刀

关注

还未添加个人签名 2018.12.25 加入

还未添加个人简介

评论

发布
暂无评论
ARTS week 1