ISUCONにRustで挑戦した話 / Participating in ISUCON with Rust

ISUCONにRustで挑戦した話 / Participating in ISUCON with Rust

https://forcia.connpass.com/event/187287/ に向けた発表資料です。

49df09ab2eadd025342b9b3387936eb9?s=128

すてにゃん

October 20, 2020
Tweet

Transcript

  1. ISUCONʹRustͰ௅ઓͨ͠࿩ RustͷLTձ Shinjuku.rs #12 @ΦϯϥΠϯ https://forcia.connpass.com/event/187287/ 2020/10/20 ͯ͢ʹΌΜ (id:stefafafan)

  2. ໨࣍ • ࣗݾ঺հ • ISUCONͱ͸ʁ • ࣄલ४උ΍ௐ΂ͨRustͷCrateͷ঺հ • ISUCON༧બ •

    ֶͼɾײ૝ͳͲ 2
  3. ࣗݾ঺հ • ͯ͢ʹΌΜ (id:stefafafan) • גࣜձࣾ͸ͯͳͷࣾһ • WebΞϓϦέʔγϣϯΤϯδχΞ • Rustॳ৺ऀ

    • ISUCON10͸Trust RustνʔϜͱͯ͠ࢀՃ
  4. ISUCONͱ͸ʁ

  5. ISUCONͱ͸ • LINEגࣜձࣾओ࠵ʮIikanjini Speed Up ContestʯͷུশͰ͋Γɺ༩͑ ΒΕͨWebΞϓϦέʔγϣϯΛߴ଎Խ͠ڝ͍߹͏ίϯςετ • 1ਓʙ3ਓͷνʔϜͰࢀՃ͢Δ͜ͱ͕Ͱ͖ͯɺΞϓϦέʔγϣϯίʔυ ͷΈͳΒͣྫ͑͹nginx΍MySQLͷνϡʔχϯάͳͲ΋ߦ͏͜ͱʹͳ

    Δ • ༻ҙ͞ΕͨϕϯνϚʔΫΛ࣮ߦ͢Δ͜ͱʹΑͬͯʮ఺਺ʯ͕֬ೝͰ͖ Δ
  6. ISUCONͱ͸ • ࣾձਓ΍ֶੜ΋ࢀՃՄೳ • ܾউ্ҐʹೖΔͱ৆ۚ΋͋Δ ※ʮISUCONʯ͸ɺLINEגࣜձࣾͷ঎ඪ·ͨ͸ొ࿥঎ඪͰ͢ɻ http://isucon.net/

  7. ༧બग़৔·Ͱͷ४උ

  8. ༧બग़৔·Ͱͷ४උ • νʔϜ࡞Γͱ໾ׂ෼୲ܾΊ • nginx΍MySQLͷϩάղੳɾνϡʔχϯά • Rustͷษڧ • WebΞϓϦέʔγϣϯͱRust

  9. ༧બग़৔·Ͱͷ४උ • νʔϜ࡞Γͱ໾ׂ෼୲ܾΊˡࠓճ͸࿩͞ͳ͍ • nginx΍MySQLͷϩάղੳɾνϡʔχϯάˡࠓճ͸࿩͞ͳ͍ • RustͷษڧˡTour of Rust΍Γ·ͨ͠ (https://tourofrust.com/)

    • WebΞϓϦέʔγϣϯͱRust
  10. WebΞϓϦέʔγϣϯͱRust

  11. ISUCONͷࢀߟ࣮૷ʹ͍ͭͯ • ISUCONͰ͸Կछྨ͔ͷݴޠͰͷ࣮૷͕ఏڙ͞Ε্ͨͰڝٕ͕ߦΘΕ Δ • ౰ॳզʑ͸Rustͷࢀߟ࣮૷͕ͳ͍έʔε΋ߟྀ͠ɺ1͔ΒRustͰΞϓ ϦέʔγϣϯΛ࣮૷͢Δ͜ͱ΋ݕ౼ʹೖΕ͍ͯͨ • RustͰWebΞϓϦέʔγϣϯΛ࡞ΔͷʹඞཁͳϥΠϒϥϦͳͲΛ ஌͓ͬͯ͘ඞཁ͕͋Δ

  12. WebΞϓϦέʔγϣϯΛRustͰهड़͢ΔͷʹԿ ͕ඞཁͳͷ͔ • WebϑϨʔϜϫʔΫ͕΄͍͠ • ςϯϓϨʔτΤϯδϯ͕΄͍͠ • APIୟ͍ͨΓ͢ΔͳΒHTTPΫϥΠΞϯτ͕΄͍͠ • JSONฦ͢ͳΒSerialize/DeserializeͰ͖ΔΑ͏ʹ͍ͨ͠

    • MySQL΍PostgreSQLΫϥΠΞϯτ͕΄͍͠
  13. WebΞϓϦέʔγϣϯΛRustͰهड़͢ΔͷʹԿ ͕ඞཁͳͷ͔ • WebϑϨʔϜϫʔΫ͕΄͍͠ • ςϯϓϨʔτΤϯδϯ͕΄͍͠ • APIୟ͍ͨΓ͢ΔͳΒHTTPΫϥΠΞϯτ͕΄͍͠ • JSONฦ͢ͳΒSerialize/DeserializeͰ͖ΔΑ͏ʹ͍ͨ͠

    • MySQL΍PostgreSQLΫϥΠΞϯτ͕΄͍͠
  14. actix-web use actix_web::{web, App, HttpResponse, HttpServer}; // ϢʔβҰཡΛදࣔ͢Δ async fn

    show_users() -> HttpResponse { HttpResponse::Ok().body(“Show users…”) } // ΤϯυϙΠϯτ͝ͱʹͲͷؔ਺ΛݺͿ͔ͳͲ͜͜Ͱهड़͢Δ #[actix_rt::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new().service( web::scope(“/users”) .route(“/show”, web::get().to(show_users)), ) }) .bind(“127.0.0.1:8088”)? .run() .await } RustͷWeb framework https://github.com/actix/ actix-web
  15. actix-web READMEʹػೳҰཡ΍ Example͕͍͔ͭ͘ࡌͬͯ ͍Δ

  16. WebΞϓϦέʔγϣϯΛRustͰهड़͢ΔͷʹԿ ͕ඞཁͳͷ͔ • WebϑϨʔϜϫʔΫ͕΄͍͠ • ςϯϓϨʔτΤϯδϯ͕΄͍͠ • APIୟ͍ͨΓ͢ΔͳΒHTTPΫϥΠΞϯτ͕΄͍͠ • JSONฦ͢ͳΒSerialize/DeserializeͰ͖ΔΑ͏ʹ͍ͨ͠

    • MySQL΍PostgreSQLΫϥΠΞϯτ͕΄͍͠
  17. Tera <h2>My Programming Languages</h2> <ul> {% for language in programming_languages

    %} {% if language.name == “rust” %} <li>Rust Rules!!!!</li> {% else %} <li>{{ language.name }}</li> {% endif %} {% endfor %} </ul> … Template engine inspired by Jinja2 https://github.com/Keats/ tera
  18. WebΞϓϦέʔγϣϯΛRustͰهड़͢ΔͷʹԿ ͕ඞཁͳͷ͔ • WebϑϨʔϜϫʔΫ͕΄͍͠ • ςϯϓϨʔτΤϯδϯ͕΄͍͠ • APIୟ͍ͨΓ͢ΔͳΒHTTPΫϥΠΞϯτ͕΄͍͠ • JSONฦ͢ͳΒSerialize/DeserializeͰ͖ΔΑ͏ʹ͍ͨ͠

    • MySQL΍PostgreSQLΫϥΠΞϯτ͕΄͍͠
  19. reqwest let client = reqwest::Client::new(); // form()ͷଞʹbody()ͱ͔json()ͱ͔΋࢖͑Δ let res =

    client.post(“https://…”) .form(&params) .send() .await?; RustͷHTTPΫϥΠΞϯτ https://github.com/ seanmonstar/reqwest
  20. WebΞϓϦέʔγϣϯΛRustͰهड़͢ΔͷʹԿ ͕ඞཁͳͷ͔ • WebϑϨʔϜϫʔΫ͕΄͍͠ • ςϯϓϨʔτΤϯδϯ͕΄͍͠ • APIୟ͍ͨΓ͢ΔͳΒHTTPΫϥΠΞϯτ͕΄͍͠ • JSONฦ͢ͳΒSerialize/DeserializeͰ͖ΔΑ͏ʹ͍ͨ͠

    • MySQL΍PostgreSQLΫϥΠΞϯτ͕΄͍͠
  21. serde use actix_web::{HttpResponse, Responder}; use serve::{Serialize, Deserialize}; #[derive(Serialize, Deserialize)] struct

    User { id: i32, username: String, name: String } async fn user_detail_json() -> imp Responder { let user = User { id: 12345, username: “stefafafan”.to_string(), name: “ͯ͢ʹΌΜ”.to_string(), } // ͜Ε͸actix_webͷؔ਺͚ͩͲJSONͱͯ͠ฦͤͯΔ HttpResponse::Ok().json(user) } Rust޲͚ʹσʔλΛSerialize/ Deserialize͢ΔϑϨʔϜϫʔΫ https://github.com/serde-rs/ serde
  22. WebΞϓϦέʔγϣϯΛRustͰهड़͢ΔͷʹԿ ͕ඞཁͳͷ͔ • WebϑϨʔϜϫʔΫ͕΄͍͠ • ςϯϓϨʔτΤϯδϯ͕΄͍͠ • APIୟ͍ͨΓ͢ΔͳΒHTTPΫϥΠΞϯτ͕΄͍͠ • JSONฦ͢ͳΒSerialize/DeserializeͰ͖ΔΑ͏ʹ͍ͨ͠

    • MySQL΍PostgreSQLΫϥΠΞϯτ͕΄͍͠
  23. SQLx use sqlx::{MySqlPool}; async fn main() -> std::io::Result<()> { let

    pool = MySqlPool::connect(&DATABASE_URL).await; let row = sqlx::query!(“SELECT …”) .fetch_all(&pool) .await .unwrap(); … } Rust޲͚SQLϥΠϒϥϦ https://github.com/ launchbadge/sqlx
  24. ISUCON༧બ

  25. ISUCON10 ༧બ໰୊ͷղઆͱߨධ http://isucon.net/archives/55025156.html

  26. ISUCON10 ༧બ֓ཁ • Πεʹ߹͏෺݅Λݕࡧ͢ΔαΠτ(ISUUMO) ҎԼͷΑ͏ͳը໘͕͋Δ • Πεͷݕࡧ • ෺݅ͷݕࡧ •

    Πεɾ෺݅ͷৄࡉը໘ • ϚοϓΛϚ΢εͰͳͧͬͯൣғ಺ʹؚ·ΕΔ෺݅Λදࣔ
  27. ISUCON10 ༧બ֓ཁ • MySQLͱnginx͕࢖ΘΕ͍ͯΔ • Rust࣮૷΋͋Δ • αʔό͕3୆࢖͑Δ

  28. ISUCON༧બTrust RustνʔϜͰ΍ͬͨ͜ͱ • αʔόʹ͋ΔRustͷ࣮૷ΛGit؅ཧԼʹஔ͘ • alp΍pt-query-digestΛ࢖ͬͯɺ஗͍ΤϯυϙΠϯτ΍஗͍ΫΤϦΛಛఆ • MySQLͷINDEXΛ଍͢ͳͲ • طଘͷRustίʔυΛோΊ͖ͯ͋Β͔ʹඇޮ཰ͳ͜ͱ΍ͬͯͦ͏ͳ෦෼Έ

    ͚ͭͯվળ͠Α͏ͱͨ͠ • 3୆αʔό͕͋ΔͷͰΞϓϦαʔό1୆ + DBαʔό2୆ߏ੒ʹ
  29. ISUCON༧બTrust RustνʔϜͰ΍ͬͨ͜ͱ • αʔόʹ͋ΔRustͷ࣮૷ΛGit؅ཧԼʹஔ͘ • alp΍pt-query-digestΛ࢖ͬͯɺ஗͍ΤϯυϙΠϯτ΍஗͍ΫΤϦΛಛఆ • MySQLͷINDEXΛ଍͢ͳͲ • طଘͷRustίʔυΛோΊ͖ͯ͋Β͔ʹඇޮ཰ͳ͜ͱ΍ͬͯͦ͏ͳ෦෼Έ

    ͚ͭͯվળ͠Α͏ͱͨ͠ • 3୆αʔό͕͋ΔͷͰΞϓϦαʔό1୆ + DBαʔό2୆ߏ੒ʹ
  30. ISUCON༧બ վળྫᶃ ɾΠεͷॎԣ෯͕෺݅ͷυΞʹೖΔ ͔Ͳ͏͔ͷΫΤϦΛ૊Έཱ͍ͯͯΔ Օॴͷվળ https://github.com/dekokun/ ISUCON-2020-qualify/pull/12

  31. ISUCON༧બ վળྫᶄ ɾchairςʔϒϧͱestateςʔϒϧͰ ผʑͷMySQLPoolΛࢀর͢ΔΑ͏ ʹͯ͠ɺDBαʔόΛΘ͚ΒΕΔঢ় ଶʹ https://github.com/dekokun/ ISUCON-2020-qualify/pull/15

  32. ༧બͷ݁Ռ • զʑ͸༧બഊୀͨ͠ • ISUCON10 ΦϯϥΠϯ༧બͷར༻ݴޠൺ཰ http://isucon.net/ archives/55008738.html • Goݴޠ͕1൪ਓؾͰ6ׂۙ͘ͷνʔϜ͕࢖͍ͬͯͨ

    • Rust͸8૊Ͱ͏ͪ1૊͕ຊઓग़৔ (͍͢͝)
  33. ֶͼɾײ૝ • Rust͸ίϯύΠϧ࣌ʹஸೡʹ৭ʑͱౖͬͯ͘ΕΔͷͰ҆৺ײ͕͋Δ • ૉૣ͘վળ͍ͯ͘͠ඞཁͷ͋ΔISUCONʹؔͯ͠͸ίϯύΠϧ࣌ؒ΍ίʔ υॻ࣌͘ͷखܰ͞ͱ͍͏໘Ͱ͸Go͸࢖͍΍͍͢ͷͩΖ͏ͳͱ͸ࢥͬͨ • ࠓճͷ͏ͪͷνʔϜͷઓ͍ํ͸ΞϓϦͷίʔυΛΰϦΰϦॻ͘ͱ͍ ͏ײ͡Ͱ΋ͳ͔ͬͨͱ͍͏ͷ΋͋Γ·͢ •

    RustΛ৮Δػձʹ͸ͳͬͨ͠ɺ༧બ͸མͪͨ΋ͷͷ΍ͬͯΑ͔ͬͨ