PORK
    • 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
    • Engagement control
    • 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 Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    ## ISUCONチュートリアル - 未経験を前提 - 未経験者にとってISUCONの難しい部分:**体制作り** - どのようにツールを使ってチューニングサイクルを回していくか - 三人でどうやって効率的に分業するか - これをしっかりやらないと、勘でN+1っぽい部分を修正してscoreをあげる、より先にいけない - 今回はチューニングを実施する前のインフラ・オペレーション部分の構築を経験してもらう - ISUCON8の環境を使用 - 1つの環境を用いたモブプログラミング - 3時間くらいで終わることを想定 - 終わったあとは実践形式でチューニング - カンバンボードTrelloを使用 - (時間があったらアプリのコードに目を通してみてもいいかも) ## EC2インスタンスのセットアップをする (事前実行済み) - 提供されているAMIを使用 - https://github.com/matsuu/aws-isucon - 2コア, メモリ1G, 帯域1Gbpsなので、t3.microかな - Firewall rules - TCPの22, 80, 443, 3306, 5000, 8080, 19999番を外に解放 - 一応ICMPも許可 ## isuconユーザの作成 (事前実行済み) - ユーザをisuconに切り替え ``` sudo -i -u isucon ``` - isuconユーザを作成 ``` sudo useradd -m isucon sudo usermod -aG wheel isucon sudo visudo (コメントアウトしてパスワードなしでwheelグループがsudoできるようにする) ``` - sshでisuconユーザにログインできるようにする ``` sudo -i cp -r /home/centos/.ssh /home/isucon/ cd /home/isucon chown -R isucon:isucon /home/isucon/.ssh ``` ## サーバ群にsshできるようにする 全員が4つのサーバ(isucon-server[1-3], isucon-bench)にsshできるようにする - 公開鍵の登録 - @tkygtr6 に各自の公開鍵を共有 ``` ssh-copy-id -i ~/.ssh/id_rsa.pub -f isucon@isucon-server1 ``` - ローカルマシンの `/.ssh/config` の設定 ``` Host isucon-server1 HostName isucon-server1 Port 22 User isucon ... ``` - ローカルマシンの `/etc/hosts` の設定 ``` <ip address> isucon-server1 <ip address> isucon-server2 <ip address> isucon-server3 <ip address> isucon-bench ``` ## webappのフォルダをローカルに落としてくる ``` # 圧縮 tar -zcvf xxxx.tar.gz directory # 解凍 tar -zxvf xxxx.tar.gz ``` ## サーバで動いているサービスを確認する - サービス一覧の取得 ``` sudo systemctl list-unit-files --type=service ``` - 開いているポートの確認 ``` sudo netstat -an | grep "LISTEN " ``` - ポートがどのプロセスに使われているのか調査 ``` sudo lsof -i:80 sudo lsof -i:8080 ``` - ブラウザからメイン画面にアクセスできることを確認 ``` open http://isucon-server1 ``` ## 全てのサーバで/etc/hosts の設定をする ``` <ip address> isucon-server1 <ip address> isucon-server2 <ip address> isucon-server3 <ip address> isucon-bench ``` ## マニュアルを読む - 特にレギュレーションが大切 ## アプリのコードをGitリポジトリで管理する - 共有リポジトリを作成 - webapp内の必要最低限のファイルのみをgit管理下に置く - リモートで必要なファイルを `git add` して push - ローカルでは解凍後のwebapp以下に、共有リポジトリの `.git` のみをコピーするのが手っ取り早い ## ベンチを動かす - `torb/bench/README.md` には `See ../README.me` と書いてあるが存在しない - https://github.com/isucon/isucon8-qualify を参照してビルド ``` ./bin/bench -remotes=isucon-server1:80 -output result.json ``` ## 雑多な設定をする - パッケージのインストール ``` sudo yum install gcc lsof ``` - SELinuxの無効化 ``` sudo setenforce 0 sudo vi /etc/selinux/config # SELINUX=disabledに変更 ``` ## 実装をPythonに変更する - 実装をPythonにしたい - `torb/doc/MANUAL.md` にやり方が書いてある ``` sudo systemctl stop torb.perl sudo systemctl disable torb.perl sudo systemctl start torb.python sudo systemctl enable torb.python ``` ## Server3でDBを動かす - CentOSの標準であるMariaDBを用いる ``` sudo yum install mariadb-server sudo systemctl start mariadb sudo systemctl enable mariadb ``` - server1からDBをマイグレする - 今回に関しては初期化スクリプトがdb以下に存在しているのでそれを利用 ``` cd torb/db ./init-user.sh ./init.sh ``` - (もし手動でマイグレするなら次のようにやる) ``` # dump mysqldump -u isucon -p isuda > torb.dump.sql ``` ``` # DBの作成 isucon@ip-172-31-42-87:~$ cat init_mysql.sh #!/bin/bash cat <<'EOF' | mysql -uroot CREATE USER 'isucon'@'%' IDENTIFIED BY 'isucon'; GRANT ALL ON torb.* TO 'isucon'@'%'; CREATE USER 'isucon'@'localhost' IDENTIFIED BY 'isucon'; GRANT ALL ON torb.* TO 'isucon'@'localhost'; CREATE DATABASE torb; EOF ``` ``` # restore mysql -u isucon -p torb < torb.dump.sql ``` - `/etc/my.cnf` に以下を追記 - (なぜかシンボリックリンクを貼ると設定が反映されなくなるので、直接編集する) ``` [mysqld] ... general_log = 1 general_log_file = /var/log/mariadb/mysql.log slow_query_log = 1 long_query_time = 0 slow_query_log_file = /var/log/mariadb/mysql-slow.sql ``` - slow logが反映されるか確認 ``` echo 'show variables like "%slow%";' | mysql ``` - 各種コマンドを打ってみる ``` mysql -u isucon -p isucon # リモートのサーバにアクセスする場合 mysql -h $REMOTE_HOST -uisucon -p isucon mysql> show databases; mysql> use <db_name> mysql> show tables; ``` ## Server2でappを動かす ``` sudo yum install python3 python3-devel mysql mysql-devel gcc ``` - appはsystemdで動かす - `/etc/systemd/system/torb.python.service` をserver1から持ってくる必要がある - `confs/systemd` の中に入れてGit管理して、シンボリックリンクを貼って管理 - 適切に `/etc/systemd/system/torb.python.service` のパスを設定 - 他のサーバからアクセスできるように、gnicornの設定には `-b '0.0.0.0:8080'` を付与 ``` ./setup.sh ``` - systemdを有効にする ``` sudo systemctl enable torb.python sudo systemctl start torb.python ``` - エラーログは以下のように取得可能 ``` journalctl -u torb.python.service | tail -n 10 ``` ## Server1でNginxを動かす - デフォルトのH2Oをアンインストール - (やり方は調べる) - Nginxのインストール - `/etc/nginx/nginx.conf`, `/etc/nginx/conf.d/default.conf`, `/etc/nginx/log_format.conf` は[事前準備](https://github.com/tkygtr6/isucon8-qualify/tree/84b0604aceedef328149382abea3e2dd2036beb0/confs/nginx)して持ってくる ``` yum install nginx sudo service nginx start # 事前準備したファイルをGitリポジトリで管理 mkdir -p /home/isucon/torb/confs/nginx/conf.d vim /home/isucon/torb/confs/nginx/nginx.conf vim /home/isucon/torb/confs/nginx/conf.d/default.conf vim /home/isucon/torb/confs/nginx/conf.d/log_format.conf # シンボリックリンクを貼る ln -s /home/isucon/torb/confs/nginx/conf.d /etc/nginx/conf.d ln -s /home/isucon/torb/confs/nginx/nginx.conf /etc/nginx/nginx.conf nginx -s reload ``` - curlで挙動を確認 ## ローカルでapp+DBを動かす - 基本的には各自はローカルで作業してサーバにプッシュするスタンスでいく ``` # mysqlのセットアップ brew install mysql mysql.server start cd db ./init-user.sh ./init.sh # Pythonのセットアップ ./setup.sh source venv/bin/activate # 環境変数の読み込み cd webapp/python vim load_env.sh # 後述 source load_env.sh gunicorn app:app -b localhost:8080 --log-level debug ``` ``` # load_env.sh set -a DB_DATABASE=torb DB_HOST=localhost DB_PORT=3306 DB_USER=isucon DB_PASS=isucon ``` - `set -a` は変数をexportするために付与 ## 監視ツールnetdataを入れる - netdataのインストール ``` bash <(curl -Ss https://my-netdata.io/kickstart.sh) all --dont-wait sudo systemctl stop firewalld.service sudo systemctl disable firewalld.service ``` - 19999番ポートにブラウザでアクセス ## デプロイスクリプトを用意する - 「git pull && logの退避 && サービスの再起動」をローカルから行うスクリプト - [こちら](https://github.com/tkygtr6/isucon8-qualify/tree/cb360cbbe325a4e0ded0817122d13b90589512fd/scripts)を参照 ## サーバ分散に伴う設定ファイルの微変更 - `/initialize` が思った通りに機能しなくなる - `./init.sh` の変更 - `-h $REMOTE_HOST` をつける ## N+1問題の解決 - 本当は測定してからやるべきだが、初期状態ではscoreが出ないので最低限の修正をする - mysql.logを見ると `get_event` 内のクエリでN+1が生じていることが容易にわかる - 最低限の修正の例 - https://github.com/tkygtr6/isucon8-qualify/commit/d3680cb74b917ef112b43c52b50fb077675331bd ## ベンチを走らせる - scoreが算出されることを確認 - 走らせた後に、`run_after_bench.sh` を実行 ## alpで重いエンドポイントを特定する - ローカルで動かすのであれば、`brew install alp` ``` cd scripts cat logs/access.log | alp ltsv -m "/admin/api/reports/events/.*,/api/events/.*,/api/users/.*" ``` ## pt-query-digestで重いSQLクエリを特定する - ローカルで動かすのであれば、 `brew install percona-toolkit` ``` cd scripts pt-query-digest logs/mysql-slow.sql > logs/pt.result ```

    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