Yuma Arakawa
    • 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
    • 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 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
    ###### tags: `TransactionalInformationSystems` # 17章-1 ステートレスなアプリケーションの回復 ## 17章概要 ここではサーバ側でなく、クライアント側のアプリケーションプログラムに着目する。 したがって、サーバ側のデータの整合性は保証されており、トランザクションをアトミックに実行してくれるものと仮定する。 このような状況で、サーバのエラー、ソフトウェアエラー、伝送エラーなどにより失敗したプロセスやメッセージの回復を考える。 17.2では、特別ケースとして、トランザクションがクライアント-サーバ間のリクエスト-リプライのペア1つからなる場合を考える。この場合は、メッセージを永続&復旧可能なメッセージキューで管理するのが解決策になる。 17.3では、複数のリクエスト-リプライペアからなる、ユーザとのやり取りを含んだステートフルなプログラムの回復を考える。クライアントプログラム側でローカルにセッション情報を管理し、サーバで実行されるトランザクションは実質的に複数に分割される。メッセージキューをこの場合にも拡張することができる。 17.4では、長大なワークフローを考える。この場合ACID特性のI(分離性)はもはや満たせないので諦めるが、原子性や、ただ一度だけ実行されることは保証したい。トランザクションをキューで管理することが鍵になる。 17.5では、さらに一般化して、多くの状態を持つ長大なプロセスを複数生成する巨大なサービスでの回復を考える。 ## 17.2 ステートレスなアプリケーション まずステートレス(サーバとのやり取りに関する情報を記憶する必要がない)なクライアントを考える。その実行ステップは以下のようになる。 1. ユーザがコンピュータになんらかの入力を与える。クライアントはその入力に基づいてプログラムを実行する。 2. プログラムがデータサーバにリクエストを送る。 3. データサーバがリクエストをトランザクションとして処理し、リプライをクライアントに送り返す。 4. クライアントがリプライを受け取り、出力をユーザに提示する。 リクエスト/リプライが一度しか行われないので、この一連の手順が終わった時点で、アプリケーションは履歴をすべて忘れてもよい(=ステートレス)。次にユーザが入力を与えたときは、新しくプログラムを実行することになる。 さらに議論をシンプルにするため、ここでは非同期リクエストを禁止することにする。つまり、クライアントはリクエストを送った時点でプログラムを一時停止し、リプライが返ってくるまで待つこととする。 このようなアプリケーションでの最大の問題は、クライアント-サーバ間のメッセージがどちらかのクラッシュにより失われても、最終的に正しい実行結果を出力できるかということである。これを、persistent, recoverable message queues(永続かつ回復可能なメッセージキュー)を用いて、送信者はキューにメッセージを入れ、受信者はキューから取り出す形で実現する。 ### メッセージキューの実装 キュー自体は障害回復や同時実行制御を備えたサーバのストレージ上に格納され、複数のクライアントからアクセスされる。このサーバをキューマネージャと呼ぶ。同時実行制御はnon-FIFOキューのセマンティクスを利用して緩和できる(7章参照)。障害回復は13-16章の方法を用いればよい。 キューマネージャをデータサーバ自体に組み込む場合と、独立したサーバで運用する場合が考えうる。前者ではメッセージキューに関する操作もトランザクション内に組み込んでACID特性を保証できる。後者の場合はトランザクションを第4部で導入する分散トランザクションとみなす必要がある。 ### リクエストが失われないことの保証 クライアントがユーザからの入力を得たら、リクエストを即座にenqueueしてトランザクションをcommitする。これにより、キューマネージャでenqueueに関するログがforceされるのでユーザ入力はストレージに記憶される。 あとは、このキューに記録されたリクエストが一度だけ実行され、リプライがクライアントに確実に送り返されることを保証すればよい。 ### リクエストが1度だけ実行されることの保証 データサーバでは、トランザクションの内部にリクエストのdequeueとリクエストの実行、生成したリプライのenqueueを組み込む。こうすることで、リクエストの実行が失敗した時点でキューの状態が元に戻ってリクエストに再挑戦することができる。実行が成功してcommitされたら、リクエストはキューから消え、リプライがキューに入るので、1度だけの実行が保証できる。 ### リプライが失われないことの保証 クライアントのリプライ受け取りも、トランザクションで処理する。リプライをdequeueしてトランザクションをcommitする。失敗したらキューにリプライが戻されるし、成功したらリプライの正しい受け取りができている。 --- 総括すると、このプロセスは3つのトランザクションからなる。 1. クライアントによるリクエストのenqueue 2. サーバによるリクエストのdequeueとリプライのenqueue 3. クライアントによるリプライのdequeue クライアントから送られるのがストアドプロシージャでなくSQL文の列だったりした場合、メッセージキューを使わず、アプリケーション・サーバ双方でトランザクションを走らせ、その内部で一連のやり取りを行う。 ここまで考えてきた手法は3層アーキテクチャ(クライアント-アプリケーションサーバ-データサーバ)においても同様で、クライアント-アプリケーション間、アプリケーション-データ間の双方についてキューを用いたやり取り、もしくは単一トランザクション内での一連のやり取りを行う。キューに関する操作は全てアプリケーションサーバで、永続データに関する操作はすべてデータサーバ上で実行される。ただしこのとき、トランザクションは第4部で導入される分散トランザクションになる。 --- キューによる管理は大きなオーバヘッドを引き起こす一方で、**ACIDよりもさらに良い特性**(**確実に一度実行される**)を持たせることができるのが美点である。ただしこれはユーザから見た観点では必ずしも徹底されていない。例えば、ユーザにリプライの内容が出力された直後にトランザクションが失敗し、リプライがキューに戻されて再実行された場合、ユーザは出力内容を2回見ることになる。それほど頻度の多い現象でもないし、クリティカルでもないのであまり問題にはならないが。 ### 出力の検証 もしこの出力の重複表示がクリティカルな問題であるならば(ATMにおける現金出力など)、ハードウェア実装したカウンタを利用して既に出力されたか検証する方法を取ることはできる。出力を行うたびに現在のカウンタの値をインクリメントし、ストレージのある領域に保存する。出力前に現在のカウンタとストレージに保存された値を比較し、異なっていたなら出力をやめて単にリプライのdequeueだけを行う。 このような検証が必要になるのは、**一般に「出力」は実世界に影響を及ぼす操作**(画面表示や現金の引き出しなど)であり、undoができないためである。無かったことにはできないが、既に一度行ったかどうか検証することはできる。 ### 定理17.1 ステートレスなアプリケーションについてのqueued transaction protocolは以下を保証する。 1. いちどユーザ入力トランザクションがcommitされたら、リクエストはサーバによってただ一度実行される。 2. いちどユーザ入力トランザクションがcommitされたら、ユーザ出力は少なくとも一度出力される。 3. ユーザ出力が検証可能ならば、いちどユーザ入力トランザクションがcommitされたら、ユーザ出力はただ一度出力される。 #### 証明 障害が起こるケースを場合分けする。 まず、ユーザ入力トランザクションがcommitされたが、その後にクライアントが落ちた場合を考える。サーバは影響を受けないので、単にリクエストを実行してリプライをenqueueする。回復したクライアントは、既にリプライがキューにあればdequeueするし、なければ待つ。どちらにせよ少なくとも一度リプライを受け取って出力する。出力が検証可能ならばただ一度だけ出力する。 次に、サーバがトランザクション中に落ちた場合を考える。それがリクエストをdequeueする前なら、回復に際して何も行う必要がない。リクエストのdequeue後かつcommit前に起きた場合は、サーバを回復する過程でデータを復元しつつリクエストがキューに戻され、リプライがキューにあったらそれは除かれる。したがって、改めてリクエストを受け取ってただ一度だけ実行でき、リプライは前者と同じく正しく出力される。$\square$ 2層アーキテクチャにおける通常動作中のクライアント・サーバの疑似コードを置いておく。 ```verilog // クライアントによるリクエストのenqueue user-input-processing-by-client(): begin transaction; enqueue(request); commit transaction; // クライアントによるリプライのdequeue user-output-processing-by-client(): wait until reply queue is not empty; begin transaction; dequeue(reply); // 既に出力したかの最低限の検証(もっと厳密に検証:演習17.1) while user has not acknowledged the reply or sent the next request do present reply to user; end commit transaction; // サーバによるリクエストの実行 request-reply-processing-by-data-server(): begin transaction; dequeue(request); perform data operations and generate reply; enqueue(reply); commit transaction; // クライアントの回復プロセス restart-client(): check reply queue; if not empty then process reply like during normal operation; end // サーバの回復プロセス restart-server(): check request queue; if not empty then initiate processing of requests like during normal operation end ```

    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