田中愛大
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # チームA b3m1 最終発表 # まなと ## 概要 30000rpsを捌く計測サーバー **hakaru** を作成するために、現状の課題の確認、改善を行った。開発を円滑に進めるため、ロギング改善や自動デプロイなどの作業も進めた。 その他、DBとアプリケーションの設定やハンドラ処理の変更などを行い、無事30000rpsを達成することができた。 ## 最初の状態  1000rps 15sec error rate 50% を超えるとテストが終了する ![](https://i.imgur.com/D5eF56L.png) loader.ioのテストが最後まで実行されず、 はじめのうちは200を返せていたが、実行時間が長くなるにつれ500sエラーを多く返すようになった CloudWatchを確認したところ、アプリケーション側で `http: panic serving 10.1.3.59:34482: Error 1040: Too many connections` のエラーが大量に発生していた ## 課題 `http: panic serving 10.1.3.59:34482: Error 1040: Too many connections`エラーとタイムアウトによるテストの強制終了 ## やったこと ### Too many connectionsの調査 DBの同時接続数の上限を超えるとRDSが出す Too many connections が発生すると、データをDBに保存することができずhakaruが500エラーを返す ### 原因調査 RDB側で同時接続数の最大値を保持しているので確認 ``` sql show variables like "%max_connections%" -> 65 ``` 手軽に実行できそうなためRDB側のmax_connectionsを増やそうとしたが、[ドキュメント](https://aws.amazon.com/jp/premiumsupport/knowledge-center/rds-mysql-max-connections/)に非推奨とあったため断念 ## 結果 > max_connections の値をデフォルト値より大きくすることはできますが、ベストプラクティスではありません。これは、ワークロードが増加し、より多くのメモリが必要になると、インスタンスに問題が発生する可能性があるためです。接続数を増やすと、メモリ使用量が増加する可能性があります。メモリが不足しているインスタンスは、クラッシュする可能性があります。 → 非推奨のため、RDB側でmax_connectionsを操作しない # たくみ ## やったこと ### Open関数をハンドラの外に  [ドキュメント](https://pkg.go.dev/database/sql#DB)によると、Openはアプリケーションで一度だけ開けば良い リクエストのたびにOpenしていた実装を一度だけ実行するように変更 **テスト結果** 1000rps 15sec ![](https://user-images.githubusercontent.com/35529908/197369482-1b29a3c4-d5c8-4878-8c7d-b57cd81f2f64.png) - はじめの状態ではテストがAbortしていたが、最後まで実施できた - まだタイムアウトと500エラーは残っていた ### ロギング改善  現状のエラーログではGoのコードのどこでハンドリングしたエラーかの特定ができない -> エラーログの文言をこちらで設定 # いなたつ ### 自動デプロイ  デプロイ手順が煩雑なことや、手元の環境に依存するため、デプロイするにあたってバージョンのミスなどが考えらるためテストを行う度に手動でデプロイ作業を行うのは非効率的であると考えた、そのためデプロイ自動化による効率化を図る。 デプロイ自動化の方法として、github actions の設定ファイルがリポジトリにあったため活用し、S3へのアーティファクトのアップロードをmainにpushされた時にgithub actionsを用いて自動的に行うようにする。 以下の権限を与えたIAMユーザーを作成し、トークンをgithub actions の secretへと追加し、自動アップロードを行なった。 - S3 ListBucket - S3 GetObject - S3 PutObject # たくみ ### アプリケーション側のDB設定の変更  Go側の仕様ではmax_connectionsの設定はデフォルトでは無制限だった https://pkg.go.dev/database/sql#DB.SetMaxOpenConns > if n <= 0, then there is no limit on the number of open connections. The default is 0 (unlimited). -> DBの設定と合ってない - `db.SetMaxIdleConns(n)` - `db.SetMaxOpenConns(n)` より効果がありそうな`db.SetMaxOpenConns(30)`をdb.closeの後に追記 (数値は他グループを参考にした) **テスト結果** - 1000 rps では500エラーが発生しなくなった - Too many connectionsも発生せず - タイムアウトは健在 # りんりん ## 課題  - loader.ioとDBのレスポンスの合計が合わない redashで確認したInsertされた数より、Successが返った数が少ない →ALBのリクエスト数を確認したところ、サーバーはすべてのリクエストに対してレスポンスを返していて、DBにも保存されているため、それほど重要な問題ではないと判断 - 今後の方針決め 処理のどの部分に注力するか、決める ## やったこと ## 重い処理の同定 ### 目的 方針を決めるために実行時間を測定する。 その集計に用いるクエリの作成とログの構造化を行う。 ### 行ったこと - ログの構造化 https://github.com/VG-Tech-Dojo/hakaru2022-a/pull/14 クエリにて集計するため ログをJSON形式で出力した。 - log's insightでのクエリ作成 実行時間を測定するため、クエリを作成した。 → **実行時間の大半がExec部分であることが判明** #  いなたつ ## やったこと ### Goの理解 バルクインサートの要素であるキューイングのテスト (channel、goroutineを使用) ## キューイング、バルクインサート ### 課題 RDBへのinsertがリクエスト毎に実行されており、 アプリケーションとDBのネットワークを貼る時間が大きなボトルネックになっている ### 解決策 DBへのネットワークを張る回数を減らさなければいけない → まとめてinsertする ### 実装案 キューイングの実装で非同期にDBに書き込む処理を行なったので、 それを一度にまとめて書き込めるようにする sqlxを用いる 現状のドライバーを使ってもまとめてinsertすることはできた。 insertするためのクエリを文字列を結合して作成することで実装できるが、 SQLインジェクションの対策を自身で検討する必要があるが、時間がない sqlxというパッケージに差し替えて実装 採用理由 - まとめてinsertするための関数が用意されている - SQLインジェクションの対策がなされている #### 挿入条件 - キューに100個のeventが溜まる - 1秒経過 30000rps 1min ![](https://i.imgur.com/0I6mrgI.png) 問題なく捌けている 捌けるリクエスト数を増やすという新たな目標をもとに、検証を行なった 50000rps 20sec ![](https://i.imgur.com/CIGFMC5.png) 現状は100個のイベントが集まったら流していたが、それを10000個に変更 ``` http: Accept error: accept tcp [::]:8081: accept4: too many open files; retrying in XXms ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully