Nan Tarantool, ou ka konbine yon baz done super-rapid ak yon aplikasyon pou travay avèk yo. Men ki jan li fasil pou fè

Senk ane de sa mwen te eseye travay ak Tarantool, men Lè sa a, li pa te travay pou mwen. Men, dènyèman mwen te fè yon webinar kote mwen te pale sou Hadoop, sou fason MapReduce travay. Gen mwen te poze kesyon an - "Poukisa pa sèvi ak Tarantool pou travay sa a?".

Pou dedomajman pou kiryozite, mwen deside retounen nan li, teste dènye vèsyon an - ak fwa sa a mwen reyèlman te renmen pwojè a. Koulye a, mwen pral montre ki jan yo ekri yon aplikasyon senp nan Tarantool, chaje li epi tcheke pèfòmans lan, epi ou pral wè ki jan fasil ak fre tout bagay ki la.

Nan Tarantool, ou ka konbine yon baz done super-rapid ak yon aplikasyon pou travay avèk yo. Men ki jan li fasil pou fè

Ki sa ki Tarantool

Tarantool pozisyone tèt li kòm yon baz done ultra-rapid. Ou ka mete nenpòt done ou vle nan la. Anplis de sa, replike yo, shard - sa vle di, divize yon gwo kantite done sou plizyè serveurs ak konbine rezilta yo nan yo - fè lyen mèt-mèt toleran fay.

Dezyèmman, li se yon sèvè aplikasyon. Ou ka ekri aplikasyon ou yo sou li, travay ak done, pou egzanp, efase ansyen antre nan background nan dapre sèten règ. Ou ka ekri yon sèvè Http dirèkteman nan Tarantula ki pral travay ak done: bay nimewo yo, ekri nouvo done la epi redwi li tout nan yon mèt.

Mwen li yon atik sou fason mesye yo te fè yon keu mesaj 300-liy, ki jis chire ak thrashes - yo gen yon pèfòmans minimòm de 20 mesaj pou chak segonn. Isit la ou ka reyèlman vire epi ekri yon aplikasyon trè gwo, epi sa yo pa pral estoke, tankou nan PostgreS.

Apeprè tankou yon sèvè, sèlman senp, mwen pral eseye dekri nan atik sa a.

Enstalasyon

Pou tès la, mwen te kòmanse twa machin vityèl estanda - yon kondwi difisil 20 jigokte, Ubuntu 18.04. 2 CPU vityèl ak 4 jigokte memwa.

Nou enstale Tarantool - kouri yon script bash oswa ajoute yon repozitwa epi fè apt jwenn enstale Tarantool. Link nan script la - (boukl -L https://tarantool.io/installer.sh | VER=2.4 sudo -E bash). Nou gen kòmandman tankou:

tarantoolctl se kòmandman prensipal la pou jere ka Tarantula.
/etc/tarantool - isit la manti tout konfigirasyon an.
var/log/tarantool - Men mòso bwa yo.
var/lib/tarantool - isit la bay manti done yo, ak Lè sa a, yo divize an ka.

Gen dosye ki disponib pou egzanp ak dosye ki pèmèt egzanp - li gen sa ki pral lanse - dosye a konfigirasyon egzanp ak kòd lua, ki dekri ki pò li koute sou, ki memwa ki disponib pou li, anviwònman motè Vinyl, kòd ki travay nan demaraj. sèvè, sharding, ke moun kap kriye, efase done demode, ak sou sa.

Enstans yo travay tankou nan PostgreS. Pou egzanp, ou vle kouri plizyè kopi nan yon baz done ki pandye sou pò diferan. Li sanble ke plizyè ka baz done yo te lanse sou yon sèl sèvè, ki pandye sou pò diferan. Yo ka gen yon anviwònman konplètman diferan - yon egzanp aplike yon lojik, dezyèm lan - yon lòt.

Jesyon enstans

Nou gen kòmandman tarantoolctl ki pèmèt nou jere egzanp Tarantula. Pou egzanp, egzanp chèk tarantoolctl pral tcheke dosye konfigirasyon an epi li di fichye a se ok si pa gen okenn erè sentaks.

Ou ka wè estati egzanp lan - egzanp estati tarantoolctl. Nan menm fason an, ou ka fè kòmanse, sispann, rekòmanse.

Yon fwa yon egzanp ap kouri, gen de fason pou konekte avèk li.

1. Administratif konsole

Pa default, Tarantool louvri yon priz epi voye tèks ASCII plenn pou kontwole Tarantula la. Koneksyon nan konsole a toujou fèt anba itilizatè a admin, pa gen okenn otantifikasyon, kidonk ou pa bezwen pran pò a konsole kontwole Tarantula deyò a.

Pou konekte nan fason sa a, ou bezwen antre Tarantoolctl antre non egzanp lan. Kòmandman an pral lanse konsole a epi konekte kòm itilizatè admin. Pa janm ekspoze pò konsole a deyò - li pi bon kite li kòm yon priz inite. Lè sa a, sèlman moun ki gen aksè ekri nan priz la yo pral kapab konekte ak Tarantula la.

Metòd sa a nesesè pou bagay administratif yo. Pou travay ak done, sèvi ak dezyèm metòd la - pwotokòl la binè.

2. Sèvi ak yon pwotokòl binè pou konekte avèk yon pò espesifik

Gen yon direktiv koute nan konfigirasyon an, ki ouvè pò a pou kominikasyon ekstèn. Yo itilize pò sa a ak pwotokòl binè epi otantifikasyon pèmèt la.

Pou koneksyon sa a, yo itilize tarantoolctl konekte ak nimewo pò. Lè l sèvi avèk li, ou ka konekte ak sèvè aleka, sèvi ak otantifikasyon ak bay dwa aksè divès kalite.

Anrejistreman done ak modil bwat

Depi Tarantool se tou de yon baz done ak yon sèvè aplikasyon, li gen divès modil. Nou enterese nan modil bwat la - li aplike travay ak done. Lè ou ekri yon bagay nan yon bwat, Tarantool ekri done yo sou disk, estoke li nan memwa, oswa fè yon lòt bagay ak li.

Ekri

Pou egzanp, nou antre nan modil la bwat epi rele fonksyon an box.once. Li pral fòse Tarantool kouri kòd nou an lè sèvè a inisyalize. Nou kreye yon espas kote done nou yo pral estoke.

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

Apre sa, nou kreye yon endèks prensipal - prensipal - kote nou ka chèche done. Pa default, si yo pa espesifye okenn paramèt, yo pral itilize premye jaden an nan chak antre pou endèks la.

Lè sa a, nou fè yon sibvansyon itilizatè a envite, anba ki nou konekte atravè yon pwotokòl binè. Nou pèmèt li, ekri ak egzekite nan tout egzanp lan.

Konpare ak baz done konvansyonèl yo, tout bagay se byen senp isit la. Nou gen espas - yon zòn kote done nou yo tou senpleman estoke. Chak antre yo rele yon tuple. Li pake nan yon MessagePack. Sa a se yon fòma trè fre - li se binè epi li pran mwens espas - 18 bytes kont 27.

Nan Tarantool, ou ka konbine yon baz done super-rapid ak yon aplikasyon pou travay avèk yo. Men ki jan li fasil pou fè

Li se byen pratik yo travay avè l '. Prèske chak liy, chak antre done ka gen kolòn konplètman diferan.

Nou ka wè tout espas lè l sèvi avèk lòd Box.space. Pou chwazi yon egzanp espesifik, nou ekri box.space egzanp epi jwenn enfòmasyon konplè sou li.

Gen de kalite motè ki bati nan Tarantool: memwa ak vinil. Memwa estoke tout done nan memwa. Se poutèt sa, tout bagay ap travay tou senpleman epi byen vit. Done yo jete nan disk, epi gen tou yon mekanis ekri an avans, kidonk nou pa pral pèdi anyen si sèvè a aksidan.

Vinyl magazen done sou disk nan yon fòm ki pi abitye - se sa ki, ou ka estoke plis done pase nou gen memwa, ak Tarantula pral li li nan disk.

Koulye a, nou pral sèvi ak memwa.

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>

Index:

Yo dwe kreye yon endèks prensipal pou nenpòt espas, paske pa gen anyen ki pral travay san li. Kòm nan nenpòt baz done, nou kreye premye jaden an - ID dosye a.

Pati:

Sa a se kote nou presize ki sa endèks nou an konsiste de. Li konsiste de yon pati - premye jaden an ke nou pral itilize, tape unsigned - yon nonb antye relatif pozitif. Jan mwen sonje nan dokiman an, kantite maksimòm ki kapab se 18 kintillion. Yon anpil nan awizom.

Lè sa a, nou ka mete done lè l sèvi avèk kòmandman an 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>

Premye jaden an itilize kòm kle prensipal la, kidonk li dwe inik. Nou pa limite pa kantite kolòn, kidonk nou ka mete done anpil jan nou renmen la. Yo espesifye nan fòma MessagePack, ke mwen te dekri pi wo a.

Done pwodiksyon

Lè sa a, nou ka montre done yo ak lòd la chwazi.

Bwat.egzanp.select ak kle {1} pral montre antre vle a. Si nou kite kle a, nou pral wè tout dosye nou genyen yo. Yo tout diferan nan kantite kolòn, men isit la, nan prensip, pa gen okenn konsèp nan kolòn - gen nimewo jaden.

Ka gen absoliman done anpil jan ou renmen. E pou egzanp, nou bezwen chèche yo nan dezyèm jaden an. Pou fè sa, nou fè yon nouvo endèks segondè.


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

Nou itilize lòd Create_index la.
Nou rele li Segondè.

Apre sa, ou bezwen presize paramèt yo. Kalite endèks la se TREE. Li ka pa inik, kidonk nou antre Inik = fo.

Lè sa a, nou endike nan ki pati endèks nou an konsiste. Field se nimewo jaden an kote nou mare endèks la, epi li presize kalite fisèl la. Se konsa li te kreye.

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>

Koulye a, men ki jan nou ka rele li:

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

Prezèvasyon

Si nou rekòmanse egzanp lan epi eseye rele done yo ankò, nou pral wè ke yo pa la - tout bagay se vid. Sa rive paske Tarantool fè pòs ak sove done yo sou disk, men si nou sispann travay anvan pwochen sove a, nou pral pèdi tout operasyon - paske nou pral refè soti nan pòs nan dènye, ki te, pou egzanp, de zè de tan de sa.

Ekonomize chak segonn pa pral travay tou - paske toujou ap jete 20 GB sou disk se yon lide konsa konsa.

Pou sa, yo te envante ak aplike konsèp ekri an avans. Avèk èd li, pou chak chanjman nan done yo, yo kreye yon dosye nan yon ti fichye ekri an avans.

Chak antre jiska pòs la estoke nan yo. Pou dosye sa yo, nou mete gwosè a - pou egzanp, 64 mb. Lè li ranpli, anrejistreman an kòmanse ale nan dezyèm dosye a. Epi apre rekòmanse a, Tarantool refè soti nan dènye pòs kontwòl la ak Lè sa a, woule sou tout tranzaksyon pita yo jiskaske li sispann.

Nan Tarantool, ou ka konbine yon baz done super-rapid ak yon aplikasyon pou travay avèk yo. Men ki jan li fasil pou fè

Pou fè yon anrejistreman konsa, ou bezwen presize yon opsyon nan paramèt box.cfg (nan fichye example.lua):

wal_mode = “write”;

itilizasyon done

Avèk sa nou te ekri kounye a, ou ka itilize Tarantula pou konsève done epi li pral travay trè vit kòm yon baz done. Epi, koulye a Cherry la sou gato a - ki sa ou ka fè ak li tout.

Ekri yon aplikasyon

Pou egzanp, se pou yo ekri tankou yon aplikasyon pou Tarantula la

Gade aplikasyon an anba spoiler la

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()

Nou deklare kèk tab nan lua ki defini senbòl. Tablo sa a nesesè pou jenere yon fisèl o aza.

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

Apre sa, nou deklare yon fonksyon - randomString epi bay valè longè a nan parantèz.

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

Lè sa a, nou konekte routeur http ak sèvè http nan sèvè Tarantula nou an, JSON, ke nou pral bay kliyan an.

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

Apre sa, nou kòmanse sou pò 8080 sou tout entèfas sèvè http, ki pral konekte tout demann ak erè.

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

Apre sa, nou deklare yon wout ke si yon demann ak metòd la GET rive sou pò 8080 / konte, Lè sa a, nou rele fonksyon an soti nan yon liy. Li retounen yon estati - 200, 404, 403 oswa kèlkeswa sa nou presize.

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

Nan kò a, nou retounen json.encode, nou presize konte ak getcount nan li, ki rele epi ki montre kantite dosye nan baz done nou an.

Dezyèm metòd la

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)

Ki kote nan liy lan routeur:route({metòd = 'GET', chemen = '/token'}, fonksyon () nou rele fonksyon an epi jenere yon siy.

Fisèl siy lokal = randomString (32) se yon kòd o aza ki gen 32 karaktè.
Nan liy lokal dènye = box.space.example:len() nou rale dènye eleman an.
Ak nan liy lan box.space.example:insert{ dènye + 1, siy } nou ekri done yo nan baz done nou an, se sa ki, nou tou senpleman ogmante ID a pa 1. Sa a ka fè, nan chemen an, pa sèlman nan yon fason maladwa. Tarantula gen sekans pou ka sa a.

Nou ekri siy la la.

Kidonk, nou te ekri yon aplikasyon nan yon sèl dosye. Ou ka jwenn aksè nan done yo touswit, ak modil bwat la pral fè tout travay la sal pou ou.

Li koute http ak travay ak done, tout bagay se nan yon sèl egzanp - tou de aplikasyon an ak done yo. Se poutèt sa, tout bagay rive byen vit.

Pou kouri, nou enstale modil http:

Ki jan nou fè li, wè anba spoiler la

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:/#

Nou bezwen tou prometheus pou kouri:

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:/#

Nou kòmanse epi nou ka jwenn aksè nan modil

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 ban nou estati 200.
/token bay yon siy epi li ekri siy sa a nan baz done a.

Vitès tès la

Ann kouri yon referans pou 50 demann. Demann konpetitif yo pral 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:/#

Yo bay marqueur. Epi nou toujou ap anrejistre done. 99% nan demann yo te konplete nan 42 milisgond. An konsekans, nou gen apeprè 3500 demann pou chak segonn sou yon ti machin, kote gen 2 nwayo ak 4 jigokte memwa.

Ou ka chwazi tou kèk siy 50000 epi wè valè li yo.

Ou ka itilize pa sèlman http, kouri fonksyon background ki trete done ou yo. Anplis de sa, gen divès kalite deklanche. Pou egzanp, ou ka rele fonksyon sou mizajou, tcheke yon bagay - ranje konfli.

Ou ka ekri aplikasyon script dirèkteman nan sèvè baz done a tèt li, epi yo pa dwe limite pa anyen, konekte nenpòt modil epi aplike nenpòt lojik.

Sèvè aplikasyon an ka jwenn aksè nan sèvè ekstèn, kolekte done epi ajoute li nan baz done li yo. Done ki soti nan baz done sa a pral itilize pa lòt aplikasyon.

Sa a pral fè pa Tarantula nan tèt li, epi pa gen okenn bezwen ekri yon aplikasyon separe.

Nan konklizyon

Sa a se jis premye pati nan yon gwo travay. Dezyèm lan pral pibliye trè byento sou blog Mail.ru Group, epi nou pral definitivman ajoute yon lyen nan li nan materyèl sa a.

Si w enterese patisipe nan evènman kote nou kreye bagay sa yo sou entènèt epi poze kesyon an tan reyèl, konekte DevOps pa chanèl REBRAIN.

Si w bezwen ale nan nwaj la oswa si w gen kesyon sou enfrastrikti w la, Ou lib pou w soumèt yon demann.

PS Nou gen 2 odit gratis pa mwa, petèt pwojè ou a pral youn nan yo.

Sous: www.habr.com

Add nouvo kòmantè