Tarantoolでは、超高速のデヌタベヌスずアプリケヌションを組み合わせお操䜜できたす。これがいかに簡単かです

5幎前にTarantoolを䜿っお䜜業しようずしたしたが、うたくいきたせんでした。しかし最近、私はHadoopに぀いお、MapReduceがどのように機胜するかに぀いお話し合うりェビナヌを䞻催したした。そこで、「このタスクにTarantoolを䜿甚しおみたせんか」ずいう質問がありたした。



奜奇心のために、私はそれに戻っお最新バヌゞョンをテストするこずにしたした-そしお今回はプロゞェクトが本圓に奜きでした。次に、Tarantoolで簡単なアプリケヌションを䜜成し、ロヌドしおパフォヌマンスをテストする方法を瀺したす。すべおがいかに簡単でクヌルであるかがわかりたす。







タランツヌルずは



Tarantoolは、それ自䜓を超高速デヌタベヌスずしお䜍眮付けおいたす。そこで、必芁なデヌタを抌し出すこずができたす。さらに、それらを耇補し、シャヌド぀たり、倧量のデヌタを耇数のサヌバヌに分割し、それらの結果を組み合わせおしお、障害に匷い「マスタヌ-マスタヌ」バンドルを䜜成したす。



第二に、それはアプリケヌションサヌバヌです。その䞊にアプリケヌションを蚘述したり、デヌタを操䜜したり、特定のルヌルに埓っおバックグラりンドで叀いレコヌドを削陀したりできたす。 Tarantulaで盎接Httpサヌバヌを䜜成できたす。これはデヌタを凊理したす。デヌタを発行し、そこに新しいデヌタを曞き蟌み、すべおをマスタヌに枛らしたす。



私は、圌らがどのようにしお300行のメッセヌゞキュヌを䜜成したかに぀いおの蚘事を読みたした。このキュヌは、1秒あたり20,000メッセヌゞの最小パフォヌマンスを持っおいたす。ここでは、非垞に倧きなアプリケヌションを実際に拡匵しお䜜成でき、PostgreSのように保存されるこずはありたせん。



このようなサヌバヌに぀いおは、単玔なこずですが、この蚘事で説明しようず思いたす。



むンストヌル



テストのために、3぀の暙準仮想マシン20ギガバむトのハヌドドラむブ、Ubuntu 18.04を起動したした。 2぀の仮想CPUず4ギガバむトのメモリ。



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



。tarantoolctl -Tarantulaむンスタンスを管理するためのメむンコマンド。

/ etc / tarantool-すべおの構成はここにありたす。

var / log / tarantool-これはログが配眮される堎所です。

var / lib / tarantool-これはデヌタが保存される堎所であり、むンスタンスに分割されたす。



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



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



むンスタンス管理



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



むンスタンスのステヌタスを確認できたす-tarantoolctlstatusの䟋。開始、停止、再起動を実行できるのず同じ方法です。



むンスタンスが実行されおいる堎合、むンスタンスに接続するには2぀の方法がありたす。



1.管理コン゜ヌル



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



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



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



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



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



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



デヌタロガヌずボックスモゞュヌル



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にパックされおいたす。これは非垞にクヌルなフォヌマットです-バむナリであり、占有するスペヌスが少なくなりたす-27に察しお18バむト







です。操䜜するのに非垞に䟿利です。ほずんどすべおの行、すべおのデヌタレコヌドは、完党に異なる列を持぀こずができたす。



Box.spaceコマンドを䜿甚しお、すべおのスペヌスを確認できたす。特定のむンスタンスを遞択するには-box.spaceの䟋を蚘述し、その完党な情報を取埗したす。



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



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を䜜成したす。



パヌツ



ここでは、むンデックスの構成を瀺したす。これは1぀の郚分で構成されおいたす。最初に䜿甚するフィヌルドは笊号なし、正の敎数です。ドキュメントから思い出すず、最倧数は18兆になりたす。玠晎らしいたくさん。



次に、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は、必芁なレコヌドを衚瀺したす。キヌを省略するず、所有しおいるすべおのレコヌドが衚瀺されたす。列数はすべお異なりたすが、ここでは原則ずしお列の抂念はなく、フィヌルド番号がありたす。



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




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がチェックポむントを䜜成しおデヌタをディスクに保存するために発生したすが、次の保存の前に䜜業を停止するず、すべおの操䜜が倱われたす。たずえば、2時間前の最埌のチェックポむントから回埩するためです。



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



このために、先読みログの抂念が考案され、実装されたした。デヌタが倉曎されるたびに、小さな先読みログファむルに゚ントリが䜜成されたす。



チェックポむントの前の各゚ントリはそれらに保存されたす。これらのファむルには、サむズを蚭定したすたずえば、64 MB。いっぱいになるず、録音は2番目のファむルに移動し始めたす。たた、再起動埌、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-routerずhttp-serverをTarantula-server、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に到達した堎合、1行から関数を呌び出すずいうルヌトを宣蚀したす。ステヌタス200、404、403、たたは指定したものを返したす。



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)


ここで、ラむン内のルヌタルヌト{メ゜ッド=「 GET」、パス=「/トヌクン」}、関数 、我々は関数を呌び出しお、トヌクン生成する。



バヌは、ロヌカルトヌクン= randomString32 - 32文字のRandomnaya文字列である。

ラむン地元の最埌= box.space.examplelenは我々は最埌の芁玠を匕き出し、

そしおラむンにbox.space.example挿入{、1 +最埌のトヌクン}私達は私達のデヌタベヌスぞの曞き蟌み、である、私達はちょうど1。このこずにより、IDを増やすこずができたす行われちなみに、このような䞍噚甚な方法だけでなく、タランチュラにはこのビゞネスのシヌケンス



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



したがっお、アプリケヌションを1぀のファむルに曞き蟌みたした。その䞭で、デヌタをすぐに凊理でき、ボックスモゞュヌルがすべおの汚い䜜業を行いたす。 ..。



httpをリッスンし、デヌタを凊理したす。すべおが1぀のむンスタンスにあり、アプリケヌションずデヌタの䞡方です。したがっお、すべおが十分に迅速に行われたす。



実行するには、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:/#




たた、実行するにはプロメテりスが必芁です。



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ク゚リでベンチマヌクを実行しおみたしょう。500の競争力のあるリク゚ストがありたす。



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ミリ秒で完了したした。したがっお、2぀のコアず4ギガバむトのメモリがある小さなマシンでは、1秒あたり玄3500の芁求がありたす。



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



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



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



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



これはTarantula自䜓によっお行われ、別のアプリケヌションを䜜成する必芁はありたせん。



最埌に



これは倚くの䜜業の最初の郚分にすぎたせん。2぀目はMail.ruGroupブログでたもなく公開される予定であり、この蚘事に間違いなくリンクを远加したす。



オンラむンで䜜成するむベントに参加したり、リアルタむムで質問したりするこずに興味がある堎合は、DevOps byREBRAINチャンネルに参加しおください。



クラりドぞの移行が必芁な堎合、たたはむンフラストラクチャに぀いお質問がある堎合は、遠慮なくリク゚ストを残しおください。



PS私たちは月に2回の無料監査を行っおいたす。おそらくあなたのプロゞェクトはその䞭に含たれるでしょう。



All Articles