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

Server Side Swiftを使った サービス開発とその運用を考える

9ebab3d4f66a62a859ad238e7e97597f?s=47 Yuki Takei
January 28, 2017

Server Side Swiftを使った サービス開発とその運用を考える

Tokyo Server Side Swift meetup#6 27, Jan, 2017

9ebab3d4f66a62a859ad238e7e97597f?s=128

Yuki Takei

January 28, 2017
Tweet

Transcript

  1. Server Side SwiftΛ࢖ͬͨ αʔϏε։ൃͱͦͷӡ༻Λߟ͑Δ Yuki Takei@TSSS meetup #6 27 Jan,

    2017
  2. ࣗݾ঺հ

  3. Serve-Side-SwiftͰෳ਺ͷOSSΛެ։͍ͯ͠·͢ʂ • Slimane: Web Framework + Web Server • Prorsum:

    Go Style Concurrent System and Networking Library • SwiftKnex: QueryBuilder for Mysql
  4. ͜ͷ౓ɺෆ࿑ॴಘΛಘ͍ͨʂͱ͍͏͜ͱͰɺ αʔϏεΛҰຊग़͢͜ͱʹ͠·ͨ͠ɻʢসʣ

  5. ϓϥΠϕʔτΧϯύχʔͰ࡞ΔͨΊɺ ͋Δఔ౓ԿΛͯ͠΋OK…ͱ͍͏͜ͱͰ

  6. Server Side SwiftͰ։ൃ͢Δ͜ͱʹ͠·ͨ͠ʂ

  7. ɾ࣮ӡ༻Λલఏͱͯ͠ͲͷΑ͏ʹػೳ։ൃΛ͍͔ͯ͘͠ʁ ɾ࣮ࡍͷӡ༻ΛͲ͏͢Δ͔ʁ ࠓճ͸ɺServer Side SwiftͰ ͱ͍ͬͨ࿩͕Ͱ͖Ε͹ͱࢥ͍·͢ɻ

  8. ۩ମతͳαʔϏε಺༰ʹ͸৮Εͣɺ ٕज़తͳ؍఺Ͱൃද͍͖ͯ͠·͢ɻ

  9. • εΫϨΠϐϯάͰwebίϯςϯπΛूΊΔ • ίϯςϯπΛղੳ͠ɺΫϥελϦϯάɺΧςΰϥ Πζ • ղੳޙͷίϯςϯπΛ࢖ͬͨwebαʔϏεΛ։ ൃ͢Δ • ϢʔβʔߦಈΛղੳͤ͞ɺϢʔβʔʹఏڙ͢Δί

    ϯςϯπΛֶशɺมԽͤ͞Δ αʔϏεཁ݅
  10. • εΫϨΠϐϯάͰwebίϯςϯπΛूΊΔ • ίϯςϯπΛղੳ͠ɺΫϥελϦϯάɺΧςΰϥ Πζ • ղੳޙͷίϯςϯπΛ࢖ͬͨwebαʔϏεΛ։ ൃ͢Δ • ϢʔβʔߦಈΛղੳͤ͞ɺϢʔβʔʹఏڙ͢Δί

    ϯςϯπΛֶशɺมԽͤ͞Δ αʔϏεཁ݅ ͜͜͸PythonͰ΍Δͱͯ͠
  11. • εΫϨΠϐϯάͰwebίϯςϯπΛूΊΔ • ίϯςϯπΛղੳ͠ɺΫϥελϦϯάɺΧςΰϥ Πζ • ղੳޙͷίϯςϯπΛ࢖ͬͨwebαʔϏεΛ։ ൃ͢Δ • ϢʔβʔߦಈΛղੳ͠ɺϢʔβʔʹఏڙ͢Δίϯ

    ςϯπΛֶशɺมԽͤ͞Δ αʔϏεཁ݅ ͜͜ΛSwiftͰ΍Δ͜ͱʹ͠·ͨ͠
  12. • Ϋϥ΢υαʔϏε͸ͲΕΛ࢖͏ʁ • ϑϨʔϜϫʔΫ͸Ͳ͏͢Δʁ • σʔλϕʔε͸Ͳ͏͢Δʁ • ΞϓϦ΍WebͳͲͷΫϥΠΞϯτͱ͸Ͳ͏௨৴͢ Δʁ •

    Swiftαʔόʔͷӡ༻ͱσϓϩΠ͸Ͳ͏͢Δʁ SwiftͰWebαʔϏεΛ࡞Δͱ͍ͬͯ΋ʁ
  13. Ϋϥ΢υαʔϏε͸ԿΛ࢖͏ʁ • Swift͸όΠφϦͳͷͰVM͕ͳ͍ɻͦͷͨΊޓ׵ ੑͷ͋ΔOSͰ͋Ε͹ͳΜͰ΋OK • Ubuntu্ͰόΠφϦͷ࣮ߦɺσʔϞϯԽ͕ग़དྷ Δ΋ͷͰ͋Ε͹OK • ͋ͱ͸ɺ͓ۚ࣍ୈ

  14. • Swift੡ͷWebϑϨʔϜϫʔΫ͸਺ଟ͘ଘࡏ͢Δ • ༗ྗͳͷ͸ɺKitura or vapor͔ʢຊ൪ӡ༻ͷ࣮੷͕ ෆ໌ͳ্ɺރΕ͍ͯΔͱ͸ݴ͑ͳ͍ʣ • ίϛϡχςΟͷن໛ͱ։ൃεϐʔυΛॏࢹ •

    ݴޠͷ੒௕΋ؚΊɺ1೥ޙʹ͸શ͘ҧ͏΋ͷʹͳͬͯ ͍ΔՄೳੑ͕ߴ͍ɻࠜؾڧ͘ϝϯς͢Δ֮ޛ͕ඞཁ • ։ൃ͕͍͖ͳΓStop͢Δةݥੑ΋(Slimane͕ͦ͏…) ϑϨʔϜϫʔΫ͸Ͳ͏͢Δʁ
  15. • ͋·ΓϓϩάϥϜݴޠ͸ؔ܎ͳ͍ • αʔϏεཁ݅࣍ୈͰԿΛ࢖͏ͷ΋ࣗ༝ • ͨͩ͠ɺSwiftͰ͸DBʹΑͬͯ͸υϥΠό͕ଘࡏ ͠ͳ͍ or ރΕ͍ͯͳ͍ͨΊࣗ࡞͢Δඞཁ͕͋Δ ৔߹΋

    σʔλϕʔε͸Ͳ͏͢Δʁ
  16. • HTTP/1.1Λ࢖ͬͨRest API+JSON͕Ұൠత͔ • apple͕ެࣜͰSwift൛protocol buffersΛαϙʔτ ͍ͯͯ͠ɺਓؾ͕ٸ্ঢத • γϯάϧϖʔδ΍ωΠςΟϒͰͳ͍৔߹͸ɺαʔ όʔαΠυϨϯμϦϯάͰ΋OKʢSwift੡ͷςϯ

    ϓϨʔτΤϯδϯ΋ෳ਺ݸଘࡏ͢Δʣ ΞϓϦ΍WebͳͲͷΫϥΠΞϯτͱ͸Ͳ͏௨৴͢Δʁ
  17. • SwiftαʔόʔΛී௨ʹཱͪ͋͛ͯӡ༻͢Δ৔߹͸ɺ super visorͰϓϩηεΛ؂ࢹ͢Δͷ͕Ұൠత • Docker(Ubuntu)΋໰୊ͳ͘࢖͑Δ • VMݴޠͰ͸ͳ͍ͷͰɺӡ༻͢ΔOSͰϏϧυ͢Δඞཁ ͕͋Δ •

    CI Tool(Ubuntu)ͰϏϧυͯͦ͠ͷόΠφϦΛσϓϩΠ ͢Δ͔ɺDocker ContainerΛ࡞ͬͯσϓϩΠ͕Ұൠత Swiftαʔόʔͷӡ༻ͱσϓϩΠ͸Ͳ͏͢Δʁ
  18. Ҏ্ͷ͜ͱΛߟྀ͢Ε͹ɺSwiftͰ΋αʔόʔ։ൃ͕ՄೳͰ͢

  19. ·ͨɺ͜Ε·Ͱͷ·ͱΊͱͯ͠

  20. ϝδϟʔͳαʔόʔݴޠͱൺֱͯ͠ɺ ಉ༷ͳ఺ɾҟͳΔ఺΋গ͠ݟ͍͖ͯ·͠ΐ͏

  21. ଞͷϝδϟʔͳWeb։ൃݴޠͱಉ༷ͳ఺ • Ϋϥ΢υαʔϏε • ϛυϧ΢ΣΞ΍DB • جຊతͳWebαʔϏεͷߏ੒ • جຊతͳαʔόʔΞʔΩςΫνϟ(preforkɺevent driven

    etc..) • ΫϥΠΞϯτͱͷ௨৴पΓ • CIɺςετ؀ڥ
  22. • ͦ΋ͦ΋ຊ൪ӡ༻࣮੷͕ஶ͘͠গͳ͍ • ެࣜαϙʔτͷOS͕·ͩ·ͩଟ͘ͳ͍(LinuxͰ΋Destribution ʹΑͬͯ͸ಈ͔ͳ͍͜ͱ΋) • ݴޠͱϑϨʔϜϫʔΫͷ੒௕͕ૣ͍ͨΊɺࠜؾڧ͍ϝϯς͕ ඞཁ • SPMͰ࢖͑ΔϥΠϒϥϦ͸·ͩ·ͩগͳ͍ͨΊɺࣗ࡞͢Δ΋

    ͷ͕ଟ͍ (AWS-SDKͱ͔ͱ͔…) • ΫϥΠΞϯτ(iOSɺMac)ͱαʔόʔؒͰιʔείʔυͷڞ༗ ͕Ͱ͖Δ ଞͷϝδϟʔͳWeb։ൃݴޠͱҟͳΔ఺
  23. Ͱ͸ɺ͜ΕΒΛ౿·͑

  24. ࠓճͷαʔϏεͰɺ ͲͷΑ͏ʹ։ൃɾӡ༻͍͔ͯ͘͠Λൃද͍͖ͯ͠·͢

  25. • Ϋϥ΢υαʔϏε͸AWS • GRPCΛ࠾༻͠ɺWebϑϨʔϜϫʔΫ͸࢖Θͳ͍ • ௨৴ͷγϦΞϥΠζϑΥʔϚοτʹ͸protocol buffersΛ ར༻ʢweb͸JSONʹϑΥʔϧόοΫʣ • σʔλϕʔεʹ͸Mysql

    5.7Λ࠾༻ • ΞϓϦ͸ReactNativeͰ࡞Δ͔΋ • Circle CI͔ΒϏϧυͱσϓϩΠΛߦ͏ ߏ੒ͱେ࿮
  26. αʔόʔߏ੒ਤ εΫϨΠϐϯά Lambda ղੳ JSON HTTP/1.1 ALB ECS GRPC Service

    ECS JSON <-> PB Proxy Service JSON HTTP/2.0
  27. • Swiftαʔόʔӡ༻΋໰୊ͳ͘ग़དྷΔ • αʔόʔϨεӡ༻͕͠΍͍͢ (εΫϨΠϐϯά͸ lambdaΛఆظ࣮ߦ • ECS͕ศར͗͢Δ • http/2.0͕ALBͰ࢖͑Δ

    QɿΫϥ΢υαʔϏε͸ʁ AɿΫϥ΢υαʔϏε͸AWS
  28. • ࠓճͷαʔϏεͰ͸ɺෳࡶͳRouting΍ɺαʔ όʔαΠυϨϯμϦϯά͕ෆཁ • ਖ਼௚ɺGraphQL΋͔ͳΓؾʹͳΔ • GRPCͷଞɺ͓ख੡ͷWebαʔόʔΛΞϓϦέʔ γϣϯαʔόʔͱͯ͠࢖͏ʢޙड़ʣ • Model෦෼ͷΈɺ͓ख੡ͷQueryBuilderΛ༻͍Δ

    ʢޙड़ʣ QɿϑϨʔϜϫʔΫ͸ʁ AɿGRPCΛ࠾༻͠ɺϑϨʔϜϫʔΫ͸࢖Θͳ͍
  29. GRPCͱ͸ʁ www.grpc.io • google੡ͷRPCϑϨʔϜϫʔΫ • సૹϑΥʔϚοτ͕protocol buffers • ௨৴΋ؚ·ΕΔ •

    HTTP/2.0ඞਢ • ༷ʑͳݴޠ༻ʹplugin͕͋Δ • ϚΠΫϩαʔϏεؒͷ௨৴ʹgoogleͰ࢖͍ͬͯΔΒ͍͠
  30. Swift༻ͷϓϥάΠϯ΋͋Γ·͢

  31. • Amazon Aurora΁ͷҠ২Λߟྀ • ϊϯεΩʔϚͳεΫϨΠϐϯάσʔλ͸JSONܕͰ อଘ͢Δ(Mongo΍ESͷӡ༻͸͠ΜͲ͗͢) • ͋Δ͍͸ɺεΫϨΠϐϯάσʔλΛS3ʹอଘ͠ Amazon AthenaͰݕࡧͰ͖ͨΒເͷΑ͏

    • ͓ख੡ͷMYSQLυϥΠόͰ௨৴͢Δ Qɿσʔλϕʔε͸ʁ A : Mysql5.7Λ࠾༻
  32. • ωΠςΟϒΫϥΠΞϯτͱɺϚΠΫϩαʔϏεؒͷ ௨৴͸protocol-buffers • webΫϥΠΞϯτͷ௨৴͸JSONʹϑΥʔϧόοΫ • Androidઐଐͷ։ൃऀ͕͍ͳ͍ͷͰɺωΠςΟϒΫ ϥΠΞϯτ͸ReactNativeͰ΍ΔՄೳੑ͕ߴ͍ Q :

    ΫϥΠΞϯτͱͷ௨৴͸ʁ AɿGRPC(protocol-buffers)ͱHTTP/1.1+JSON
  33. • swift buildޙʹdocker containerΛCircleCI্Ͱ࡞ ੒ • CircleCI͔ΒECSͷΦʔέετϨʔγϣϯΛ࣮ߦ • Blue/GreenσϓϩΠϝϯτ •

    ΑͬͯɺSwiftઐ༻ͷσϓϩΠπʔϧ͸ෆཁ Q : σϓϩΠ͸Ͳ͏͢Δʁ A : Circle CIͰϏϧυͱσϓϩΠΛߦ͏
  34. σϓϩΠϑϩʔਤ git push hook ɾϚΠάϨʔγϣϯ ɾσϓϩΠ ɾdockerίϯςφͷ੾Γସ͑ ɾswift build ɾςετ

    ɾ֤छdockerίϯςφͷ࡞੒
  35. ·ͣ͸ɺ͜ͷߏ੒ͰαʔϏε։ൃͱӡ༻ Λߦ͍ͬͯ͜͏ͱࢥ͍·͢ɻ

  36. ͨͩ͠ɺϑϨʔϜϫʔΫΛ࢖Θͳ͍ͨΊɺ ͜ͷ··͙͢ʹ։ൃʹೖΔͷ͸ͪΐͬͱେมͰ͢ɻ

  37. • Webܥͷػೳ͕શવͳ͍(HTTPαʔόʔɺ WebSocketͳͲ) • LinuxͰಈ͔ͳ͍API͕·ͩ·ͩ͋Δ • ඇಉظॲཧ͸DispatchQueueΛ࢖͏͜ͱʹͳΔ ͕ɺcallbackϕʔεʹͳΔ • GCD͸threadϕʔεͳͷͰɺϨʔείϯσΟγϣ

    ϯ͕ා͍ Swiftඪ४Ͱ͸…
  38. ͦ͜Ͱ

  39. None
  40. • Swift੡ͷωοτϫʔΫϓϩάϥϛϯά؀ڥ (LinuxͰͪΌΜͱಈ͘) • ୤ίʔϧόοΫϔϧΛओ؟ʹɺGOελΠϧͳฒྻॲཧ/ಉظػೳΛ ࣋ͭʢCoroutine͸ͳ͍ʣ • TCPɺDNSͳͲجຊతͳωοτϫʔΫػೳΛCF**Λ࢖Θͣʹఏڙ • HTTP(S)/1.1ɺWebSocketͷαʔόʔɺΫϥΠΞϯτΛఏڙ

    • TLSʹ͸libresslΛ࠾༻ • NonBlocking I/OͱBlocking I/O+GCDΛ૊Έ߹ΘͤͨϋΠϒϦου ϞσϧͳαʔόʔΞʔΩςΫνϟ(KituraΑΓߴ଎) Prorsum
  41. SwiftͰඇಉظॲཧͷయܕྫ

  42. ͜ɺ͜Ε͸…

  43. None
  44. CallBack Hellʂ

  45. ·ͨɺThread ϓϩάϥϛϯάʹΑ͋͘Δͷ͕… IPA ηΩϡϦςΟηϯλʔ ϨʔείϯσΟγϣϯͷҰൠతରࡦΑΓ

  46. ·ͨɺThread ϓϩάϥϛϯάʹΑ͋͘Δͷ͕… IPA ηΩϡϦςΟηϯλʔ ϨʔείϯσΟγϣϯͷҰൠతରࡦΑΓ ϨʔείϯσΟγϣϯ΍σουϩοΫ

  47. Swift͸͜ΕΒͷ໰୊ΛજࡏతʹሃΉ…

  48. ͨͩ͠

  49. ͜ΕΒΛ͏·͘ղܾͨ͠ݴޠ͕͋Γ·ͨ͠

  50. None
  51. ͦ͏ɺΈΜͳେ޷͖GoݴޠͰ͢

  52. Goݴޠ͸ɺGoroutineͰฒྻ/ฒߦॲཧΛߦ͍ɺ ChannelͰಉظΛͱ͍ͬͯ·ͨ͠ɻ

  53. Goroutine Goroutine͸ෳ਺Thread্ʹଟॏԽ͞ΕͨCoroutineͷΑ͏ͳಈ࡞Λ͢Δɻ Ұͭͷgoroutine͕௕͍࣌ؒϒϩοΫ͢Α͏ͳؔ਺Ͱ΋ɺผͷgoroutineʹhand off͠ ͯॲཧΛଓߦ͢ΔͨΊɺCPUΛ༨Βͤͣʹޮ཰తʹϓϩάϥϜΛ࣮ߦͰ͖Δ G1 G2 G3 G4 G5

    sleep(1) G6 G4 G5 sleep(1) G6 G8 G4 G7 G9 thread1 thread2 thread3
  54. Channel Gorotuine͸ThreadؒΛ·͍ͨͰ࣮ߦ͞ΕΔͨΊɺGoroutineಉ࢜ͰҰͭͷม਺΁ΞΫ ηε͢ΔͱRace Condition͕ൃੜͯ͠͠·͏ɻ ͦΕΛ๷͙ͨΊʹɺGoͰ͸Channelͱ͍͏ػೳ͕ఏڙ͞Ε͓ͯΓɺͦΕΛ࢖ͬͯ஋ Λૹड৴͢Δ G1 G2 G3 send

    send send main thread receive Channel
  55. ProrsumͰ͸ɺgoɺchannelɺselectͳͲΛSwiftʹҠ২ (ͨͩ͠ɺProrosumͷgo͸ϢʔβʔϥϯυͷίϯςΩετεΠον͸ͳ͍)

  56. SwiftͰ΋goɺchannelɺselectΛ

  57. HTTPαʔόʔ΋Node.jsͷΑ͏ʹΧδϡΞϧʹ࢖͑·͢

  58. None
  59. ProrsumͷαʔόʔΞʔΩςΫνϟ DispatchSource + DispatchQueueΛ࢖ͬͨ Πϕϯτۦಈ + WorkerThreadͷϋΠϒϦοτϞσϧ main thread DispatchSourceͰɺ

    listeningSocketΛ؂ࢹ Queue Queue Queue Queue Queue Queue Queue Queue Queue client DispatchQueue(concurrent) nonblocking I/O blocking I/O
  60. Benchmarking Request/sec 0 17500 35000 52500 70000 Prorsum Kitura Go

    1.7 HTTP Server Express 14,769 64,768 17,144 29,436 ɾwrk -d 30s -t 4 -c 20 ɾResponded with the 10 length of random JSON array ɾMachine: MacOS Sierra, 8 logical cores, 8GB RAM
  61. ͜ͷHTTPαʔόʔΛࠓճͷΞϓϦέʔγϣϯαʔόʔͱͯ͠ Քಇͤ͞ɺಈతͳHTMLͷ഑৴΍ɺgrpc <-> http1.1ͷϓϩΩγ αʔόͱͯ͠ར༻͠·͢ɻ

  62. https://speakerdeck.com/noppoman/swiftnishi-sitasabaakitekutiyawozai- kao-siteshi-zhuang-madesitemiru TSSS meetup #5ࢿྉ https://medium.com/@yukitakei/rethink-appropriate-server-architecture-for- swift-7c8513944db8#.ds9jhnmk8 Medium Article ͞Βʹৄ͘͠஌Γ͍ͨํ͸….

  63. ProrsumΛ࢖͏͜ͱͰɺओཁͳωοτϫʔΫػೳͷ΄͔ ΑΓ؆୯ʹඇಉظ/ฒྻϓϩάϥϛϯά͕ՄೳʹͳΓ·͢ɻ

  64. ࣍ʹɺσʔλϕʔεૢ࡞΍ɺ ϚΠάϨʔγϣϯ͸Ͳ͏͠·͠ΐ͏

  65. • Swift੡ͷMysqlυϥΠό͸ԿΛ࢖ͬͨΒ͍͍ͷ͔ • Ͱ͖Ε͹Migration·Ͱαϙʔτͯ͠΄͍͠ • ϕλͰSQLΛॻ͘΋ͷ͸ϝϯς͕ޙʑେม • DispatchQueue͸ॏ͍ΫΤϦͩͱthread͕ϒϩοΫ ͞ΕΔͨΊɺඇಉظI/O͕࢖͑Δͱخ͍͠έʔε΋

  66. ͢΂ͯͷཁ݅ʹ͋ͯ͸·ΔυϥΠό͸͋Γ·ͤΜͰͨ͠ (´ʀωʀʆ)

  67. ͳͷͰɺ࡞Γ·ͨ͠ʢসʣ

  68. None
  69. SwiftKenxͱ͸ • Pure Swift Mysql Client + Query Builder •

    libmysqlclientʹґଘ͠ͳ͍ • TCPϨΠϠʔ͸Prorsum • εΩʔϚϚΠάϨʔγϣϯػೳΛαϙʔτ • SwiftݴޠͰදݱྗͷߴ͍SQL͕ॻ͚Δ • ܕηʔϑͳfetchɺinsert • ඇಉظI/OɺଟॏԽͷαϙʔτʢ͜Ε͔Βʣ
  70. None
  71. None
  72. None
  73. None
  74. None
  75. Migration

  76. MigrationΫϥε

  77. migration࣮ߦϑΝΠϧ(Migration/main.swift)

  78. None
  79. None
  80. αʔϏε։ൃΛ͠ͳ͕ΒඞཁͳػೳΛॱ࣍଍͍͖ͯ͠·͢

  81. ͞Βʹৄ͘͠͸ɺnoppoMan/SwiftKnex

  82. ࠷ޙʹ

  83. None
  84. appleެࣜͷαʔόʔAPIϓϩδΣΫτ͕࢝ಈ͍ͯ͠·͢

  85. • RFC 7230-7235(HTTP/1.1) • RFC 6455 (WebSocket) • RFC 7540(HTTP/2.0)

    • TCP/IP(v6,v4), UDP including I/O • TLS/Encryption ର৅͸ҎԼ
  86. ެࣜAPI͕ग़ͨλΠϛϯάͰɺ ๻ΒͷαʔϏε΋ஞ࣍Ҡ২Λߦ͏ͭ΋ΓͰ͢ɻ

  87. ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠