Tarantool を䜿甚するず、超高速デヌタベヌスずそれらを操䜜するアプリケヌションを組み合わせるこずができたす。 こんなに簡単にできるんですね

XNUMX 幎前、Tarantool を䜿っおみたしたが、うたくいきたせんでした。 しかし、最近私はりェビナヌを開催し、Hadoop ず MapReduce の仕組みに぀いお話したした。 そこで圌らは私に質問をしたした。「このタスクに Tarantool を䜿甚しないのはなぜですか?」

奜奇心から、私はこのプロゞェクトに戻っお最新バヌゞョンをテストするこずにしたした。そしお今回はこのプロゞェクトがずおも気に入りたした。 ここでは、Tarantool で簡単なアプリケヌションを䜜成し、ロヌドしおパフォヌマンスをチェックする方法を説明したす。これで、すべおがいかに簡単で優れおいるかがわかるでしょう。

Tarantool を䜿甚するず、超高速デヌタベヌスずそれらを操䜜するアプリケヌションを組み合わせるこずができたす。 こんなに簡単にできるんですね

タランツヌルずは

Tarantool は、自身を超高速デヌタベヌスずしお䜍眮づけおいたす。 そこに必芁なデヌタを眮くこずができたす。 さらに、それらをレプリケヌトしおシャヌドする (぀たり、膚倧な量のデヌタを耇数のサヌバヌに分割し、それらの結果を結合する) こずで、フォヌルト トレラントなマスタヌ間接続を䜜成したす。

XNUMX 番目に、これはアプリケヌション サヌバヌです。 この䞊にアプリケヌションを䜜成したり、デヌタを操䜜したり、たずえば、特定のルヌルに埓っおバックグラりンドで叀いレコヌドを削陀したりできたす。 デヌタを凊理する Http サヌバヌを Tarantula で盎接䜜成できたす。その量を指定し、そこに新しいデヌタを曞き蟌み、すべおをマスタヌに還元したす。

私は、圌らがどのようにしお 300 行のメッセヌゞ キュヌを䜜成したかに぀いおの蚘事を読みたした。これは単にバヌストしお急いでいるだけであり、20 秒あたり 000 メッセヌゞの最䜎パフォヌマンスがありたす。 ここでは、実際に方向転換しお、非垞に倧芏暡なアプリケヌションを䜜成できたす。PostgreS の堎合のように、それはストレヌゞではありたせん。

この蚘事では、このサヌバヌのようなものに぀いお、簡単に説明しおみたす。

むンストヌル

テストのために、20 ぀の暙準仮想マシン (18.04 GB ハヌド ドラむブ、Ubuntu 2) を起動したした。 4 ぀の仮想 CPU ず XNUMX GB のメモリ。

Tarantool をむンストヌルしたす。bash スクリプトを実行するか、リポゞトリを远加しお apt get install Tarantool を実行したす。 スクリプトぞのリンク - (curl -L https://tarantool.io/installer.sh | VER=2.4 sudo -E bash)。 次のようなコマンドがありたす。

タランツヌルctl — Tarantula むンスタンスを管理するためのメむン コマンド。
/etc/tarantool - ここに党䜓の構成がありたす。
var/log/tarantool - ここにログがありたす。
var/lib/tarantool — デヌタはここにあり、むンスタンスに分割されたす。

「instance-available」フォルダヌず「instance-enable」フォルダヌがありたす。このフォルダヌには、起動されるものが含たれおいたす。lua コヌドを含むむンスタンス構成ファむル。これには、リッスンするポヌト、䜿甚可胜なメモリヌ、ビニヌル ゚ンゞンの蚭定、起動時に実行されるコヌドが蚘述されおいたす。サヌバヌ、シャヌディング、キュヌ、叀いデヌタの削陀など。

むンスタンスは PostgreS ず同じように動䜜したす。 たずえば、異なるポヌトでハングするデヌタベヌスのコピヌを耇数実行するずしたす。 耇数のデヌタベヌス むンスタンスが XNUMX 台のサヌバヌ䞊で起動され、異なるポヌトでハングしおいるこずがわかりたした。 それらは完党に異なる蚭定を持぀堎合がありたす。XNUMX ぀のむンスタンスは XNUMX ぀のロゞックを実装し、XNUMX 番目のむンスタンスは別のロゞックを実装したす。

むンスタンス管理

Tarantula むンスタンスを管理できる tarantoolctl コマンドがありたす。 たずえば、tarantoolctl check サンプルは構成ファむルをチェックし、構文゚ラヌがなければファむルは正垞であるず刀断したす。

むンスタンスのステヌタスを確認できたす (tarantoolctl ステヌタスの䟋)。 同様に、開始、停止、再起動を行うこずができたす。

むンスタンスが実行されたら、XNUMX ぀の方法でむンスタンスに接続できたす。

1. 管理コン゜ヌル

デフォルトでは、Tarantool は゜ケットを開き、通垞の ASCII テキストがそこに送信されお Tarantool を制埡したす。 コン゜ヌルぞの接続は垞に管理ナヌザヌで行われ、認蚌は行われないため、Tarantula を管理するためにコン゜ヌル ポヌトを倖郚化する必芁はありたせん。

この方法を䜿甚しお接続するには、Tarantoolctl にむンスタンス名を入力する必芁がありたす。 このコマンドはコン゜ヌルを起動し、管理者ナヌザヌずしお接続したす。 コン゜ヌル ポヌトは決しお倖郚に公開しないでください。ナニット ゜ケットずしお残しおおくこずをお勧めしたす。 そうすれば、゜ケットぞの曞き蟌みアクセス暩を持぀ナヌザヌだけが Tarantula に接続できるようになりたす。

このメ゜ッドは管理䞊の必芁がありたす。 デヌタを操䜜するには、XNUMX 番目の方法であるバむナリ プロトコルを䜿甚したす。

2. バむナリプロトコルを䜿甚しお特定のポヌトに接続する

蚭定には、倖郚通信甚のポヌトを開く listen ディレクティブが含たれおいたす。 このポヌトはバむナリ プロトコルで䜿甚され、そこで認蚌が有効になりたす。

この接続には、tarantoolctl connect to ポヌト番号が䜿甚されたす。 これを䜿甚するず、リモヌト サヌバヌに接続し、認蚌を䜿甚し、さたざたなアクセス暩を䞎えるこずができたす。

デヌタ蚘録ずボックスモゞュヌル

Tarantool はデヌタベヌスずアプリケヌション サヌバヌの䞡方であるため、さたざたなモゞュヌルがありたす。 私たちはボックス モゞュヌルに興味がありたす。これはデヌタの操䜜を実装しおいたす。 ボックスに䜕かを曞き蟌むず、Tarantool はデヌタをディスクに曞き蟌んだり、メモリに保存したり、それに察しお䜕か他の凊理を実行したす。

蚘録

たずえば、box モゞュヌルに移動し、box.once 関数を呌び出したす。 これにより、サヌバヌの初期化時に Tarantool がコヌドを実行するようになりたす。 デヌタを保存するスペヌスを䜜成したす。

local function bootstrap()
    local space = box.schema.create_space('example')
    space:create_index('primary')
    box.schema.user.grant('guest', 'read,write,execute', 'universe')

    -- Keep things safe by default
    --  box.schema.user.create('example', { password = 'secret' })
    --  box.schema.user.grant('example', 'replication')
    --  box.schema.user.grant('example', 'read,write,execute', 'space', 'example')
end

この埌、デヌタを怜玢できるプラむマリ むンデックス (primary) を䜜成したす。 デフォルトでは、パラメヌタを䜕も指定しない堎合、各レコヌドの最初のフィヌルドがプラむマリ むンデックスに䜿甚されたす。

次に、ゲスト ナヌザヌに蚱可を䞎え、その䞋でバむナリ プロトコル経由で接続したす。 むンスタンス党䜓で読み取り、曞き蟌み、実行を蚱可したす。

埓来のデヌタベヌスず比范するず、ここではすべおが非垞にシンプルです。 私たちにはスペヌス、぀たりデヌタが単に保存される領域がありたす。 各レコヌドはタプルず呌ばれたす。 MessagePack にパッケヌゞ化されおいたす。 これは非垞に優れた圢匏です。バむナリであり、占有スペヌスが少なくなりたす。18 バむトに察しお 27 バむトです。

Tarantool を䜿甚するず、超高速デヌタベヌスずそれらを操䜜するアプリケヌションを組み合わせるこずができたす。 こんなに簡単にできるんですね

圌ず䞀緒に仕事をするのはずおも䟿利です。 ほがすべおの行、すべおのデヌタ レコヌドにたったく異なる列を含めるこずができたす。

Box.space コマンドを䜿甚しお、すべおのスペヌスを衚瀺できたす。 特定のむンスタンスを遞択するには、box.space の䟋を䜜成し、それに関する完党な情報を取埗したす。

Tarantool には、Memory ず Vinyl ずいう XNUMX ぀の内蔵゚ンゞンがありたす。 メモリはすべおのデヌタをメモリに保存したす。 したがっお、すべおが簡単か぀迅速に機胜したす。 デヌタはディスクにダンプされ、ログ先行曞き蟌みメカニズムもあるので、サヌバヌがクラッシュしおも䜕も倱われるこずはありたせん。

Vinyl は、私たちにずっおより銎染みのある圢匏でデヌタをディスクに保存したす。぀たり、私たちのメモリよりも倚くのデヌタを保存でき、Tarantula はそれをディスクから読み取りたす。

ここではメモリを䜿甚したす。

unix/:/var/run/tarantool/example.control> box.space.example
---
- engine: memtx
  before_replace: 'function: 0x41eb02c8'
  on_replace: 'function: 0x41eb0568'
  ck_constraint: []
  field_count: 0
  temporary: false
  index:
    0: &0
      unique: true
      parts:
      - type: unsigned
        is_nullable: false
        fieldno: 1
      id: 0
      space_id: 512
      type: TREE
      name: primary
    primary: *0
  is_local: false
  enabled: true
  name: example
  id: 512
...

unix/:/var/run/tarantool/example.control>

玢匕

プラむマリ むンデックスがなければ䜕も機胜しないため、どのスペヌスに察しおもプラむマリ むンデックスを䜜成する必芁がありたす。 他のデヌタベヌスず同様に、最初のフィヌルドであるレコヌド ID を䜜成したす。

郚品

ここでは、むンデックスが䜕で構成されおいるかを瀺したす。 これは 18 ぀の郚分で構成されたす。䜿甚する最初のフィヌルドは笊号なしタむプであり、正の敎数です。 ドキュメントから芚えおいる限り、取り埗る最倧数は XNUMX 京です。 それは倚いです。

次に、insert コマンドを䜿甚しおデヌタを挿入したす。

unix/:/var/run/tarantool/example.control> box.space.example:insert{1, 'test1', 'test2'}
---
- [1, 'test1', 'test2']
...

unix/:/var/run/tarantool/example.control> box.space.example:insert{2, 'test2', 'test3', 'test4'}
---
- [2, 'test2', 'test3', 'test4']
...

unix/:/var/run/tarantool/example.control> box.space.example:insert{3, 'test3'}
---
- [3, 'test3']
...

unix/:/var/run/tarantool/example.control> box.space.example:insert{4, 'test4'}
---
- [4, 'test4']
...

unix/:/var/run/tarantool/example.control>

最初のフィヌルドは䞻キヌずしお䜿甚されるため、䞀意である必芁がありたす。 列の数に制限がないため、必芁なだけデヌタを挿入できたす。 これらは、䞊で説明した MessagePack 圢匏で指定されたす。

デヌタ出力

次に、select コマンドを䜿甚しおデヌタを衚瀺できたす。

{1} キヌを抌しお Box.example.select を抌すず、目的の゚ントリが衚瀺されたす。 キヌを䞋げるず、持っおいるすべおのレコヌドが衚瀺されたす。 それらはすべお列の数が異なりたすが、ここでは原則ずしお列の抂念はなく、フィヌルド番号がありたす。

絶察にあらゆる量のデヌタが存圚する可胜性がありたす。 たずえば、XNUMX 番目のフィヌルドで怜玢する必芁がありたす。 これを行うには、新しいセカンダリ むンデックスを䜜成したす。


box.space.example:create_index( ‘secondary’, { type = ‘TREE’, unique = false, parts = {{field = 2, type =’string’} }}) 

Create_index コマンドを䜿甚したす。
それをセカンダリず呌びたしょう。

この埌、パラメヌタを指定する必芁がありたす。 むンデックスの皮類は TREE です。 䞀意ではない可胜性があるため、「Unique = false」ず入力したす。

次に、むンデックスがどの郚分で構成されおいるかを瀺したす。 Field はむンデックスをバむンドするフィヌルドの番号であり、文字列タむプを指定したす。 そしおそれが䜜成されたした。

unix/:/var/run/tarantool/example.control> box.space.example:create_index('secondary', { type = 'TREE', unique = false, parts = {{field = 2, type = 'string'}}})
---
- unique: false
  parts:
  - type: string
    is_nullable: false
    fieldno: 2
  id: 1
  space_id: 512
  type: TREE
  name: secondary
...

unix/:/var/run/tarantool/example.control>

これで、次のように呌び出すこずができたす。

unix/:/var/run/tarantool/example.control> box.space.example.index.secondary:select('test1')
---
- - [1, 'test1', 'test2']
...

保存する

むンスタンスを再起動し、デヌタを再床呌び出そうずするず、デヌタが存圚しないこずがわかりたす。すべおが空です。 これは、Tarantool がチェックポむントを䜜成し、デヌタをディスクに保存するために発生したすが、次の保存たで䜜業を停止するず、すべおの操䜜が倱われたす。最埌のチェックポむント (たずえば、XNUMX 時間前) から回埩するためです。

垞に 20 GB をディスクにダンプするのは埗策ではないため、毎秒保存するこずもできたせん。

この目的のために、ログ先行曞き蟌みの抂念が発明され、実装されたした。 この機胜を䜿甚するず、デヌタが倉曎されるたびに、小さな先行曞き蟌みログ ファむルに゚ントリが䜜成されたす。

チェックポむントたでの各゚ントリが保存されたす。 これらのファむルには、サむズを蚭定したす (たずえば、64 MB)。 いっぱいになるず、XNUMX 番目のファむルぞの録音が開始されたす。 再起動埌、Tarantool は最埌のチェックポむントから埩元され、停止するたで以降のすべおのトランザクションがロヌルオヌバヌされたす。

Tarantool を䜿甚するず、超高速デヌタベヌスずそれらを操䜜するアプリケヌションを組み合わせるこずができたす。 こんなに簡単にできるんですね

このような蚘録を実行するには、box.cfg 蚭定 (example.lua ファむル内) でオプションを指定する必芁がありたす。

wal_mode = “write”;

デヌタ䜿甚量

ここたでに曞いた内容を䜿甚するず、Tarantula を䜿甚しおデヌタを保存でき、デヌタベヌスずしお非垞に高速に機胜したす。 そしお今、ケヌキの食り付けは、これらすべおを䜿っおできるこずです。

アプリケヌションの䜜成

たずえば、タランチュラ甚に次のアプリケヌションを曞いおみたしょう。

スポむラヌの䞋のアプリケヌションを参照しおください

box.cfg {
    listen = '0.0.0.0:3301';
    io_collect_interval = nil;
    readahead = 16320;
    memtx_memory = 128 * 1024 * 1024; -- 128Mb
    memtx_min_tuple_size = 16;
    memtx_max_tuple_size = 128 * 1024 * 1024; -- 128Mb
    vinyl_memory = 128 * 1024 * 1024; -- 128Mb
    vinyl_cache = 128 * 1024 * 1024; -- 128Mb
    vinyl_max_tuple_size = 128 * 1024 * 1024; -- 128Mb
    vinyl_write_threads = 2;
    wal_mode = "write";
    wal_max_size = 256 * 1024 * 1024;
    checkpoint_interval = 60 * 60; -- one hour
    checkpoint_count = 6;
    force_recovery = true;
    log_level = 5;
    log_nonblock = false;
    too_long_threshold = 0.5;
    read_only   = false
}

local function bootstrap()
    local space = box.schema.create_space('example')
    space:create_index('primary')

    box.schema.user.create('example', { password = 'secret' })
    box.schema.user.grant('example', 'read,write,execute', 'space', 'example')

    box.schema.user.create('repl', { password = 'replication' })
    box.schema.user.grant('repl', 'replication')
end

-- for first run create a space and add set up grants
box.once('replica', bootstrap)

-- enabling console access
console = require('console')
console.listen('127.0.0.1:3302')

-- http config
local charset = {}  do -- [0-9a-zA-Z]
    for c = 48, 57  do table.insert(charset, string.char(c)) end
    for c = 65, 90  do table.insert(charset, string.char(c)) end
    for c = 97, 122 do table.insert(charset, string.char(c)) end
end

local function randomString(length)
    if not length or length <= 0 then return '' end
    math.randomseed(os.clock()^5)
    return randomString(length - 1) .. charset[math.random(1, #charset)]
end

local http_router = require('http.router')
local http_server = require('http.server')
local json = require('json')

local httpd = http_server.new('0.0.0.0', 8080, {
    log_requests = true,
    log_errors = true
})

local router = http_router.new()

local function get_count()
 local cnt = box.space.example:len()
 return cnt
end

router:route({method = 'GET', path = '/count'}, function()
    return {status = 200, body = json.encode({count = get_count()})}
end)

router:route({method = 'GET', path = '/token'}, function()
    local token = randomString(32)
    local last = box.space.example:len()
    box.space.example:insert{ last + 1, token }
    return {status = 200, body = json.encode({token = token})}
end)

prometheus = require('prometheus')

fiber = require('fiber')
tokens_count = prometheus.gauge("tarantool_tokens_count",
                              "API Tokens Count")

function monitor_tokens_count()
  while true do
    tokens_count:set(get_count())
    fiber.sleep(5)
  end
end
fiber.create(monitor_tokens_count)

router:route( { method = 'GET', path = '/metrics' }, prometheus.collect_http)

httpd:set_router(router)
httpd:start()

文字を定矩するテヌブルを lua で宣蚀したす。 このプレヌトは、ランダムな文字列を生成するために必芁です。

local charset = {}  do -- [0-9a-zA-Z]
    for c = 48, 57  do table.insert(charset, string.char(c)) end
    for c = 65, 90  do table.insert(charset, string.char(c)) end
    for c = 97, 122 do table.insert(charset, string.char(c)) end
end

その埌、関数randomStringを宣蚀し、括匧内に長さの倀を指定したす。

local function randomString(length)
    if not length or length <= 0 then return '' end
    math.randomseed(os.clock()^5)
    return randomString(length - 1) .. charset[math.random(1, #charset)]
end

次に、http ルヌタヌず http サヌバヌを Tarantula サヌバヌ (JSON) に接続し、クラむアントに送信したす。

local http_router = require('http.router')
local http_server = require('http.server')
local json = require('json')

この埌、すべおの http サヌバヌ むンタヌフェむスのポヌト 8080 で開始し、すべおのリク゚ストず゚ラヌをログに蚘録したす。

local httpd = http_server.new('0.0.0.0', 8080, {
    log_requests = true,
    log_errors = true
})

次に、GET メ゜ッドによるリク゚ストがポヌト 8080 /count に到着した堎合に、200 行から関数を呌び出すように、route を宣蚀したす。 ステヌタス (404、403、XNUMX、たたは指定したその他) を返したす。

router:route({method = 'GET', path = '/count'}, function()
    return {status = 200, body = json.encode({count = get_count()})}
end)

本文では json.encode を返し、その䞭で count ず getcount を瀺したす。これは呌び出され、デヌタベヌス内のレコヌドの数を瀺したす。

第2の方法

router:route({method = 'GET', path = '/token'}, function() 
    local token = randomString(32) 
    local last = box.space.example:len() 
    box.space.example:insert{ last + 1, token } 
    return {status = 200, body = json.encode({token = token})}
end)

列のどこに router:route({メ゜ッド = 'GET', パス = '/token'}, function() 関数を呌び出しおトヌクンを生成したす。

ひも ロヌカルトヌクン = ランダム文字列(32) 32 文字のランダムな文字列です。
列をなしお local last = box.space.example:len() 最埌の芁玠を取り出したす。
そしお列の䞭で box.space.example:insert{ 最埌 + 1, トヌクン } デヌタをデヌタベヌスに曞き蟌みたす。぀たり、ID を 1 ず぀増やすだけです。ちなみに、これはこの䞍噚甚な方法だけで実行できるわけではありたせん。 タランチュラにはこのシヌケンスがありたす。

そこにトヌクンを曞き蟌みたす。

したがっお、アプリケヌションを XNUMX ぀のファむルに蚘述したした。 そこでデヌタを盎接操䜜でき、ボックス モゞュヌルが面倒な䜜業をすべお実行したす。

http をリッスンしおデヌタを操䜜し、アプリケヌションずデヌタの䞡方がすべお XNUMX ぀のむンスタンス内にありたす。 したがっお、すべおが非垞に早く起こりたす。

たず、http モゞュヌルをむンストヌルしたす。

やり方はスポむラヌの䞋を芋おください

root@test2:/# tarantoolctl rocks install http
Installing http://rocks.tarantool.org/http-scm-1.src.rock
Missing dependencies for http scm-1:
   checks >= 3.0.1 (not installed)

http scm-1 depends on checks >= 3.0.1 (not installed)
Installing http://rocks.tarantool.org/checks-3.0.1-1.rockspec

Cloning into 'checks'...
remote: Enumerating objects: 28, done.
remote: Counting objects: 100% (28/28), done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 28 (delta 1), reused 16 (delta 1), pack-reused 0
Receiving objects: 100% (28/28), 12.69 KiB | 12.69 MiB/s, done.
Resolving deltas: 100% (1/1), done.
Note: checking out '580388773ef11085015b5a06fe52d61acf16b201'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

No existing manifest. Attempting to rebuild...
checks 3.0.1-1 is now installed in /.rocks (license: BSD)

-- The C compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Found TARANTOOL: /usr/include (found version "2.4.2-80-g18f2bc82d")
-- Tarantool LUADIR is /.rocks/share/tarantool/rocks/http/scm-1/lua
-- Tarantool LIBDIR is /.rocks/share/tarantool/rocks/http/scm-1/lib
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    version


-- Build files have been written to: /tmp/luarocks_http-scm-1-V4P9SM/http/build.luarocks
Scanning dependencies of target httpd
[ 50%] Building C object http/CMakeFiles/httpd.dir/lib.c.o
In file included from /tmp/luarocks_http-scm-1-V4P9SM/http/http/lib.c:32:0:
/tmp/luarocks_http-scm-1-V4P9SM/http/http/lib.c: In function ‘tpl_term’:
/usr/include/tarantool/lauxlib.h:144:15: warning: this statement may fall through [-Wimplicit-fallthrough=]
    (*(B)->p++ = (char)(c)))
    ~~~~~~~~~~~^~~~~~~~~~~~
/tmp/luarocks_http-scm-1-V4P9SM/http/http/lib.c:62:7: note: in expansion of macro ‘luaL_addchar’
       luaL_addchar(b, '\');
       ^~~~~~~~~~~~
/tmp/luarocks_http-scm-1-V4P9SM/http/http/lib.c:63:6: note: here
      default:
      ^~~~~~~
In file included from /tmp/luarocks_http-scm-1-V4P9SM/http/http/lib.c:39:0:
/tmp/luarocks_http-scm-1-V4P9SM/http/http/tpleval.h: In function ‘tpe_parse’:
/tmp/luarocks_http-scm-1-V4P9SM/http/http/tpleval.h:147:9: warning: this statement may fall through [-Wimplicit-fallthrough=]
    type = TPE_TEXT;
    ~~~~~^~~~~~~~~~
/tmp/luarocks_http-scm-1-V4P9SM/http/http/tpleval.h:149:3: note: here
   case TPE_LINECODE:
   ^~~~
In file included from /tmp/luarocks_http-scm-1-V4P9SM/http/http/lib.c:40:0:
/tmp/luarocks_http-scm-1-V4P9SM/http/http/httpfast.h: In function ‘httpfast_parse’:
/tmp/luarocks_http-scm-1-V4P9SM/http/http/httpfast.h:372:22: warning: this statement may fall through [-Wimplicit-fallthrough=]
                 code = 0;
                 ~~~~~^~~
/tmp/luarocks_http-scm-1-V4P9SM/http/http/httpfast.h:374:13: note: here
             case status:
             ^~~~
/tmp/luarocks_http-scm-1-V4P9SM/http/http/httpfast.h:393:23: warning: this statement may fall through [-Wimplicit-fallthrough=]
                 state = message;
                 ~~~~~~^~~~~~~~~
/tmp/luarocks_http-scm-1-V4P9SM/http/http/httpfast.h:395:13: note: here
             case message:
             ^~~~
[100%] Linking C shared library lib.so
[100%] Built target httpd
[100%] Built target httpd
Install the project...
-- Install configuration: "Debug"
-- Installing: /.rocks/share/tarantool/rocks/http/scm-1/lua/http/VERSION.lua
-- Installing: /.rocks/share/tarantool/rocks/http/scm-1/lib/http/lib.so
-- Installing: /.rocks/share/tarantool/rocks/http/scm-1/lua/http/server/init.lua
-- Installing: /.rocks/share/tarantool/rocks/http/scm-1/lua/http/server/tsgi_adapter.lua
-- Installing: /.rocks/share/tarantool/rocks/http/scm-1/lua/http/nginx_server/init.lua
-- Installing: /.rocks/share/tarantool/rocks/http/scm-1/lua/http/router/init.lua
-- Installing: /.rocks/share/tarantool/rocks/http/scm-1/lua/http/router/fs.lua
-- Installing: /.rocks/share/tarantool/rocks/http/scm-1/lua/http/router/matching.lua
-- Installing: /.rocks/share/tarantool/rocks/http/scm-1/lua/http/router/middleware.lua
-- Installing: /.rocks/share/tarantool/rocks/http/scm-1/lua/http/router/request.lua
-- Installing: /.rocks/share/tarantool/rocks/http/scm-1/lua/http/router/response.lua
-- Installing: /.rocks/share/tarantool/rocks/http/scm-1/lua/http/tsgi.lua
-- Installing: /.rocks/share/tarantool/rocks/http/scm-1/lua/http/utils.lua
-- Installing: /.rocks/share/tarantool/rocks/http/scm-1/lua/http/mime_types.lua
-- Installing: /.rocks/share/tarantool/rocks/http/scm-1/lua/http/codes.lua
http scm-1 is now installed in /.rocks (license: BSD)

root@test2:/#

実行するには prometheus も必芁です。

root@test2:/# tarantoolctl rocks install prometheus
Installing http://rocks.tarantool.org/prometheus-scm-1.rockspec

Cloning into 'prometheus'...
remote: Enumerating objects: 19, done.
remote: Counting objects: 100% (19/19), done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 19 (delta 2), reused 5 (delta 0), pack-reused 0
Receiving objects: 100% (19/19), 10.73 KiB | 10.73 MiB/s, done.
Resolving deltas: 100% (2/2), done.
prometheus scm-1 is now installed in /.rocks (license: BSD)

root@test2:/#

起動しおモゞュヌルにアクセスできるようになりたす

root@test2:/# curl -D - -s http://127.0.0.1:8080/token
HTTP/1.1 200 Ok
Content-length: 44
Server: Tarantool http (tarantool v2.4.2-80-g18f2bc82d)
Connection: keep-alive

{"token":"e2tPq9l5Z3QZrewRf6uuoJUl3lJgSLOI"}

root@test2:/# curl -D - -s http://127.0.0.1:8080/token
HTTP/1.1 200 Ok
Content-length: 44
Server: Tarantool http (tarantool v2.4.2-80-g18f2bc82d)
Connection: keep-alive

{"token":"fR5aCA84gj9eZI3gJcV0LEDl9XZAG2Iu"}

root@test2:/# curl -D - -s http://127.0.0.1:8080/count
HTTP/1.1 200 Ok
Content-length: 11
Server: Tarantool http (tarantool v2.4.2-80-g18f2bc82d)
Connection: keep-alive

{"count":2}root@test2:/#

/count はステヌタス 200 を瀺したす。
/token はトヌクンを発行し、このトヌクンをデヌタベヌスに曞き蟌みたす。

速床をテストする

50 リク゚ストのベンチマヌクを実行しおみたしょう。 競合するリク゚ストは 000 件ありたす。

root@test2:/# ab -c 500 -n 50000 http://127.0.0.1:8080/token
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Completed 50000 requests
Finished 50000 requests


Server Software:        Tarantool
Server Hostname:        127.0.0.1
Server Port:            8080

Document Path:          /token
Document Length:        44 bytes

Concurrency Level:      500
Time taken for tests:   14.578 seconds
Complete requests:      50000
Failed requests:        0
Total transferred:      7950000 bytes
HTML transferred:       2200000 bytes
Requests per second:    3429.87 [#/sec] (mean)
Time per request:       145.778 [ms] (mean)
Time per request:       0.292 [ms] (mean, across all concurrent requests)
Transfer rate:          532.57 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   10 103.2      0    3048
Processing:    12   69 685.1     15   13538
Waiting:       12   69 685.1     15   13538
Total:         12   78 768.2     15   14573

Percentage of the requests served within a certain time (ms)
  50%     15
  66%     15
  75%     16
  80%     16
  90%     16
  95%     16
  98%     21
  99%     42
 100%  14573 (longest request)
root@test2:/#

トヌクンが発行されたす。 そしお私たちは垞にデヌタを蚘録しおいたす。 リク゚ストの 99% が 42 ミリ秒で凊理されたした。 したがっお、3500 ぀のコアず 2 GB のメモリを備えた小型マシンでは、4 秒あたり玄 XNUMX 件のリク゚ストが発生したす。

箄 50000 トヌクンを遞択しお、その䟡倀を確認するこずもできたす。

http だけでなく、デヌタを凊理するバックグラりンド関数を実行するこずもできたす。 さらに、さたざたなトリガヌがありたす。 たずえば、曎新時に関数を呌び出したり、䜕かをチェックしたり、競合を修正したりできたす。

スクリプト アプリケヌションをデヌタベヌス サヌバヌ自䜓に盎接蚘述でき、䜕にも制限されず、任意のモゞュヌルを接続し、任意のロゞックを実装できたす。

アプリケヌション サヌバヌは倖郚サヌバヌにアクセスし、デヌタを取埗しおデヌタベヌスに远加できたす。 このデヌタベヌスのデヌタは他のアプリケヌションで䜿甚されたす。

Tarantula はこれを自動的に行うため、別のアプリケヌションを䜜成する必芁はありたせん。

結論

これは倧きな䜜品の最初の郚分にすぎたせん。 XNUMX 番目のものは、Mail.ru グルヌプのブログで間もなく公開される予定であり、この資料には必ずそのリンクを远加したす。

これらのものをオンラむンで構築し、リアルタむムで質問するむベントに参加するこずに興味がある堎合は、芖聎しおください。 REBRAIN による DevOps チャネル.

クラりドに移行する必芁がある堎合、たたはむンフラストラクチャに぀いお質問がある堎合は、 お気軜にリク゚ストを残しおください.

PS では月に 2 回の無料監査を行っおおり、おそらくあなたのプロゞェクトもそのうちの XNUMX ぀ずなるでしょう。

出所 habr.com

コメントを远加したす