Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Software Engineering with Go in Infrasturecture〜mercari.go〜

Software Engineering with Go in Infrasturecture〜mercari.go〜

Tatsuhiko Kubo

October 11, 2015
Tweet

More Decks by Tatsuhiko Kubo

Other Decks in Technology

Transcript

  1. @cubicdaiya / Tatsuhiko Kubo • Software Engineer @ Mercari, Inc.

    • Infrastructure Engineering • Skills • C, Go, Lua, nginx, … • OSS Developer • ngx_small_light, ngx_dynamic_upstream, nginx-build, slackboard, cachectl, gaurun, …
  2. @cubicdaiya / Tatsuhiko Kubo • Software Engineer @ Mercari, Inc.

    • Infrastructure Engineering • Skills • C, Go, Lua, nginx, … • OSS Developer • ngx_small_light, ngx_dynamic_upstream, nginx-build, slackboard, cachectl, gaurun, …
  3. @cubicdaiya / Tatsuhiko Kubo • Software Engineer @ Mercari, Inc.

    • Infrastructure Engineering • Skills • C, Go, Lua, nginx, … • OSS Developer • ngx_small_light, ngx_dynamic_upstream, nginx-build, slackboard, cachectl, gaurun, …
  4. Infrastructure Engineeringʁ • Responsible for • DevOps / Reliability /

    Scalability / Performance / Security / Troubleshooting / … • Works are various and varies by • position / organization structure / authoritiy / speciality
  5. As for me • Basically, • Almost all root-user can

    doʂ • Especially, • Software Engineering in Infrastructure Usually, I designate myself as Software Engineer in Infrastructure Engineering.
  6. Software Engineering in Infrastructure@myself • Developing & Operating • CLI-tools

    for usual oprations • e.g. nginx-build, slackboard, cachectl • High performance & Scalable systems (e.g. ↓) • Gaurunʙpush system@Mercariʙ • Pascalʙlog analysis base@Mercariʙ • Task force (e.g. Migration from PHP5.3 to 5.6)
  7. Software Engineering in Infrastructure • Developing & Operating • CLI-tools

    for operations • e.g. nginx-build, slackboard, cachectl • High performance & Scalable systems (e.g. ↓) • Gaurunʙpush system@Mercariʙ These are built with Go
  8. Reason I select Go • Go has features Software Engineer

    in Infrastructure Engineering likes • Simple build and deployment • Powerful cuncurrency support • Enough scalability & productivity & robustness • Go fits in many situations as alternative for C and LL
  9. Reason I select Go • I like C • In

    my opinion, • Go is C in 21st century • Rust is C++ in 21st century
  10. mercari.go families • cubicdaiya / nginx-build • cubicdaiya / cachectl

    • cubicdaiya / slackboard • mercari / gaurun • etc…
  11. nginx-build • Seamless nginx builder • https://github.com/cubicdaiya/nginx-build • Single-binary does

    everything for building nginx • Download source • Embed external libraries (PCRE, ZLIB, OpenSSL) • Embed 3rd party modules
  12. Building nginx with nginx-build $ nginx-build -d work -pcre -zlib

    -openssl nginx-build: 0.6.0 Compiler: gc go1.5.1 2015/10/11 02:19:10 Download nginx-1.9.5..... 2015/10/11 02:19:10 Download openssl-1.0.2d..... 2015/10/11 02:19:10 Download zlib-1.2.8..... 2015/10/11 02:19:10 Download pcre-8.37..... 2015/10/11 02:19:12 Extract nginx-1.9.5.tar.gz..... 2015/10/11 02:19:12 Extract zlib-1.2.8.tar.gz..... 2015/10/11 02:19:22 Extract pcre-8.37.tar.gz..... 2015/10/11 02:19:25 Extract openssl-1.0.2d.tar.gz..... 2015/10/11 02:19:25 Generate configure script for nginx-1.9.5..... 2015/10/11 02:19:25 Configure nginx-1.9.5..... 2015/10/11 02:19:32 Build nginx-1.9.5..... 2015/10/11 02:22:19 Complete building nginx! ... 2015/10/11 02:22:19 Enter the following command for install nginx. $ cd work/nginx/1.9.5/nginx-1.9.5 $ sudo make install download concurrently
  13. Download concurrently parallels := 0 done := make(chan bool) (...)

    if *pcreStatic { parallels++ go downloadAndExtractParallel(&pcreBuilder, done) } if *openSSLStatic { parallels++ go downloadAndExtractParallel(&openSSLBuilder, done) } if *zlibStatic { parallels++ go downloadAndExtractParallel(&zlibBuilder, done) } parallels++ go downloadAndExtractParallel(&nginxBuilder, done) (...) for i := 0; i < parallels; i++ { <-done }
  14. Go as alternative for shellscript • Before I started to

    write nginx-build with shellscript • after 2 hours -> (ϊ ʄДʄ)ϊ~~~ᵲᵲ • I noticed Go fits in this case • Easy to maintain and distribute binary • Cross-platform support
  15. cachectl • Page cache controller • https://github.com/cubicdaiya/cachectl • CLI-tool for

    controlling page cache for OS • Daemonization as cachectld is available
  16. Seamless combination with C • cachectl requires • communication with

    system calls (posiv_fadivse, mincore, mmap, etc…) • Go provides FFI (cgo) // C.xxx() are functions came from C world func activePages(path string) int { cs := C.CString(path) result := C.activePages(cs) C.free(unsafe.Pointer(cs)) return int(result) }
  17. Cautions for cgo • Allocated objects must be released manually

    • Not garvage-collected • e.g. Object generated with C.CString(string) requires C.free() • cgo is possible to cause slow-compilation
  18. slackboard • Slack proxy server • https://github.com/cubicdaiya/slackboard • Building blocks

    • slackboardʙslack proxy serverʙ • slackboard-cliʙclient for slackboardʙ • slackboard-logʙclient for blackboard like cronlogʙ
  19. slackboard-cli $ echo mercari | \ slackboard-cli \ -c tech-test

    \ -s slackboard-server:29800 TMBDLCPBSE POST /notify-directly Post to Slack
  20. slackboard-log $ ls $ slackboard-log \ -c tech-test \ -s

    slackboard-server:29800 -- ls hoge TMBDLCPBSE POST /notify-directly Post to Slack
  21. Go simplifies build all: bin/slackboard bin/slackboard-cli bin/slackboard-log gom: go get

    -u github.com/mattn/gom bundle: gom install bin/slackboard: slackboard.go slackboard/*.go gom build $(GOFLAGS) -o bin/slackboard slackboard.go bin/slackboard-cli: slackboard-cli.go slackboard/*.go gom build $(GOFLAGS) -o bin/slackboard-cli slackboard-cli.go bin/slackboard-log: slackboard-log.go slackboard/*.go gom build $(GOFLAGS) -o bin/slackboard-log slackboard-log.go fmt: go fmt ./... clean: rm -rf bin/slackboard* Each mercari.go family has small Makefile
  22. Go simplifies deployment • Go programs are available as single-binary

    • Easy to package with RPM • At mercari, administrators can install and update them with yum • cachectl & slackboard-cli are deployed on all servers at setup
  23. Gaurun • A general push notification server for smartphone application

    • https://github.com/mercari/gaurun • Proxing push-requests to APNS and GCM • Provides HTTP APIs
  24. Requirements for Gaurun • As HTTP server • Good performance

    • High Concurrency • net/http is good choice
  25. Gaurun Internals • Gaurun consists of • HTTP server /

    Job Queue / Job Workers • Job Queue is channel • Job Worker is goroutine
  26. Gaurun Internals (BVSVO )551 1045QVTI #PEZJT+40/ $MJFOU 1VTI/PUJpDBUJPO 8PSLFS 1VTI/PUJpDBUJPO

    8PSLFS 2VFVF 1VTI/PUJpDBUJPO )BOEMFS &ORVFVF 1VTI/PUJpDBUJPO 8PSLFS 1VTI/PUJpDBUJPO 8PSLFS %FRVFVF
  27. Go is powerful • Go provides enough • scalability &

    productivity & robustness • Gaurun scales easily • 1st version of slackboard was written in 1 hour • mercari.go families work fineʂ
  28. Summary • Go has features Software Engineer in Infrastructure Engineering

    likes • Simple build and deployment • Powerful cuncurrency support • Enough scalability & productivity & robustness • Go fits in many situations as alternative for C and LL