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. Software Engineering with Go
    in Infrasturecture
    Tatsuhiko [email protected]
    Go All [email protected]/10/11
    ʙmercari.goʙ

    View Slide

  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, …

    View Slide

  3. View Slide

  4. https://www.mercari.com/
    Mercari - Your Friendly Mobile Marketplace

    View Slide

  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, …

    View Slide

  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, …

    View Slide

  7. Infrastructure Engineeringʁ
    • Responsible for
    • DevOps / Reliability / Scalability / Performance /
    Security / Troubleshooting / …
    • Works are various and varies by
    • position / organization structure /
    authoritiy / speciality

    View Slide

  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.

    View Slide

  9. Software Engineering in [email protected]
    • Developing & Operating
    • CLI-tools for usual oprations
    • e.g. nginx-build, slackboard, cachectl
    • High performance & Scalable systems (e.g. ↓)
    • Gaurunʙpush [email protected]ʙ
    • Pascalʙlog analysis [email protected]ʙ
    • Task force (e.g. Migration from PHP5.3 to 5.6)

    View Slide

  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 [email protected]ʙ
    These are built with Go

    View Slide

  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

    View Slide

  12. Reason I select Go
    • I like C
    • In my opinion,
    • Go is C in 21st century
    • Rust is C++ in 21st century

    View Slide

  13. mercari.go families
    • cubicdaiya / nginx-build
    • cubicdaiya / cachectl
    • cubicdaiya / slackboard
    • mercari / gaurun
    • etc…

    View Slide

  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

    View Slide

  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

    View Slide

  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++ {
    }

    View Slide

  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

    View Slide

  18. cachectl
    • Page cache controller
    • https://github.com/cubicdaiya/cachectl
    • CLI-tool for controlling page cache for OS
    • Daemonization as cachectld is available

    View Slide

  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)
    }

    View Slide

  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

    View Slide

  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ʙ

    View Slide

  22. Use case for Mercari

    View Slide

  23. slackboard-cli
    $ echo mercari | \
    slackboard-cli \
    -c tech-test \
    -s slackboard-server:29800
    TMBDLCPBSE
    POST /notify-directly
    Post to Slack

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  28. View Slide

  29. Requirements for Gaurun
    • As HTTP server
    • Good performance
    • High Concurrency
    • net/http is good choice

    View Slide

  30. Gaurun Internals
    • Gaurun consists of
    • HTTP server / Job Queue / Job Workers
    • Job Queue is channel
    • Job Worker is goroutine

    View Slide

  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

    View Slide

  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ʂ

    View Slide

  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

    View Slide

  34. We are hiring!

    View Slide