Software Engineering with Go in Infrasturecture〜mercari.go〜

Software Engineering with Go in Infrasturecture〜mercari.go〜

5d74d743eabd2bf7d4d2f68b9d3c727d?s=128

Tatsuhiko Kubo

October 11, 2015
Tweet

Transcript

  1. Software Engineering with Go in Infrasturecture Tatsuhiko Kubo@cubicdaiya Go All

    Stars@2015/10/11 ʙmercari.goʙ
  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. None
  4. https://www.mercari.com/ Mercari - Your Friendly Mobile Marketplace

  5. @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, …
  6. @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, …
  7. Infrastructure Engineeringʁ • Responsible for • DevOps / Reliability /

    Scalability / Performance / Security / Troubleshooting / … • Works are various and varies by • position / organization structure / authoritiy / speciality
  8. 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.
  9. 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)
  10. 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
  11. 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
  12. Reason I select Go • I like C • In

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

    • cubicdaiya / slackboard • mercari / gaurun • etc…
  14. 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
  15. 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
  16. 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 }
  17. 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
  18. cachectl • Page cache controller • https://github.com/cubicdaiya/cachectl • CLI-tool for

    controlling page cache for OS • Daemonization as cachectld is available
  19. 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) }
  20. 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
  21. 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ʙ
  22. Use case for Mercari

  23. slackboard-cli $ echo mercari | \ slackboard-cli \ -c tech-test

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

    slackboard-server:29800 -- ls hoge TMBDLCPBSE POST /notify-directly Post to Slack
  25. 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
  26. 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
  27. Gaurun • A general push notification server for smartphone application

    • https://github.com/mercari/gaurun • Proxing push-requests to APNS and GCM • Provides HTTP APIs
  28. Push system@mercari

  29. Requirements for Gaurun • As HTTP server • Good performance

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

    Job Queue / Job Workers • Job Queue is channel • Job Worker is goroutine
  31. 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
  32. 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ʂ
  33. 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
  34. We are hiring!