Hāʻawi ʻo Tarantool iā ʻoe e hoʻohui i kahi waihona wikiwiki wikiwiki a me kahi noi e hana pū me lākou. ʻO kēia ka maʻalahi o ka hana

ʻElima mau makahiki i hala aku nei ua hoʻāʻo wau e hana me Tarantool, akā ʻaʻole ia i hana iaʻu. Akā i kēia manawa ua paʻa wau i kahi webinar kahi aʻu i kamaʻilio ai e pili ana iā Hadoop a pehea e hana ai ʻo MapReduce. Ma laila lākou i nīnau mai ai iaʻu i kahi nīnau: "No ke aha e hoʻohana ʻole ai iā Tarantool no kēia hana?"

Ma muli o ka hoihoi, ua hoʻoholo wau e hoʻi i laila, e hoʻāʻo i ka mana hou loa - a i kēia manawa makemake nui wau i ka papahana. I kēia manawa, e hōʻike wau iā ʻoe pehea e kākau ai i kahi noi maʻalahi ma Tarantool, e hoʻouka a nānā i kāna hana, a ʻike ʻoe i ka maʻalahi a me ka ʻoluʻolu o nā mea āpau.

Hāʻawi ʻo Tarantool iā ʻoe e hoʻohui i kahi waihona wikiwiki wikiwiki a me kahi noi e hana pū me lākou. ʻO kēia ka maʻalahi o ka hana

He aha ka Tarantool

Hoʻonoho ʻo Tarantool iā ia iho me kahi waihona ultra-wikiwiki. Hiki iā ʻoe ke waiho i nā ʻikepili āu e makemake ai ma laila. Hoʻohui hou, e hoʻopili iā lākou, shard - ʻo ia hoʻi, e hoʻokaʻawale i ka nui o ka ʻikepili ma waena o nā kikowaena a hoʻohui i nā hopena mai ia mau mea - e hana i nā pilina haku-kūpono hewa.

ʻO ka lua, he kikowaena noi kēia. Hiki iā ʻoe ke kākau i kāu mau noi ma luna o ia mea, e hana me ka ʻikepili, no ka laʻana, holoi i nā moʻolelo kahiko ma ke kua e like me kekahi mau lula. Hiki iā ʻoe ke kākau pololei i kahi kikowaena Http ma Tarantula e hana me ka ʻikepili: hāʻawi i kā lākou nui, kākau i nā ʻikepili hou ma laila a hoʻemi i nā mea āpau i ka haku.

Heluhelu wau i kahi ʻatikala e pili ana i ka hana ʻana o nā kāne i ka laina memo o 300 mau laina, ʻo ia wale nō ka pohā a me ka wikiwiki - loaʻa iā lākou ka hana haʻahaʻa o 20 mau leka i kekona. Maanei hiki iā ʻoe ke huli a kākau i kahi noi nui loa, ʻaʻole ia e waiho ʻia, e like me PostgreS.

E ho'āʻo wau e wehewehe i kahi mea e like me kēia kikowaena, maʻalahi wale nō, ma kēia ʻatikala.

Kāu Mau Koho Paʻamau

No ka hoʻāʻo, ua hoʻomaka wau i ʻekolu mau mīkini virtual maʻamau - he 20 GB hard drive, Ubuntu 18.04. 2 mau CPU virtual a me 4 gigs o ka hoʻomanaʻo.

Hoʻokomo mākou iā Tarantool - holo i ka script bash a hoʻohui i kahi waihona a hana kūpono e hoʻokomo iā Tarantool. Ka loulou i ka palapala - (curl -L https://tarantool.io/installer.sh | VER=2.4 sudo -E bash). Loaʻa iā mākou nā kauoha e like me:

tarantoolctl - ke kauoha nui no ka mālama ʻana i nā hihia Tarantula.
/etc/tarantool - eia ka hoʻonohonoho holoʻokoʻa.
var/log/tarantool - eia nā lāʻau.
var/lib/tarantool - aia ka ʻikepili ma ʻaneʻi, a laila ua māhele ʻia i mau manawa.

Aia nā waihona instance-available a me instance-enable - aia i loko o ka mea e hoʻokuʻu ʻia - kahi faila hoʻonohonoho hoʻonohonoho me ka lua code, e wehewehe ana i nā awa e hoʻolohe ai, he aha ka hoʻomanaʻo i loaʻa iā ia, nā hoʻonohonoho ʻenekini Vinyl, code e holo ana i ka hoʻomaka ʻana. nā kikowaena, sharding, queues, holoi i nā ʻikepili kahiko, a pēlā aku.

E like me ka PostgreS. No ka laʻana, makemake ʻoe e holo i kekahi mau kope o kahi waihona e kau ana ma nā awa like ʻole. Ua ʻike ʻia ua hoʻomaka ʻia kekahi mau waihona waihona ma kahi kikowaena hoʻokahi, e kau ana ma nā awa like ʻole. Loaʻa paha iā lākou nā hoʻonohonoho ʻokoʻa - hoʻokahi kumu hoʻokō hoʻokahi loiloi, ʻo ka lua - kekahi.

Hoʻoponopono i nā mea hoʻohālike

Loaʻa iā mākou ke kauoha tarantoolctl, hiki iā ʻoe ke hoʻokele i nā manawa Tarantula. No ka laʻana, e nānā ʻo tarantoolctl check example i ka faila hoʻonohonoho a ʻōlelo - ua maikaʻi ka faila inā ʻaʻohe hewa syntax ma laila.

Hiki iā ʻoe ke ʻike i ke kūlana o ka laʻana - tarantoolctl status example. Ma ke ala like hiki iā ʻoe ke hoʻomaka, hoʻōki, hoʻomaka hou.

Ke holo nei ka laʻana, hiki iā ʻoe ke hoʻohui iā ia ma nā ala ʻelua.

1. ʻOihana hoʻokele

Ma ka maʻamau, wehe ʻo Tarantool i kahi kumu, hoʻouna ʻia ka kikokikona ASCII maʻamau i laila e hoʻokele iā Tarantool. Hoʻohui mau ʻia ka pili ʻana i ka console ma lalo o ka mea hoʻohana admin, ʻaʻohe hōʻoia, no laila ʻaʻohe pono e hoʻopuka i waho o ka console port e hoʻokele iā Tarantula.

No ka hoʻohana ʻana i kēia ʻano hana, pono ʻoe e hoʻokomo iā Tarantoolctl e hoʻokomo i ka inoa instance. E hoʻomaka ke kauoha i ka console a hoʻohui i ka mea hoʻohana admin. Mai hōʻike i ke awa console i waho - ʻoi aku ka maikaʻi o ka waiho ʻana ma ke ʻano he kumu ʻāpana. A laila ʻo ka poʻe i hiki ke kākau i ke kumu e hiki ke hoʻopili iā Tarantula.

Pono kēia ʻano no nā mea hoʻokele. No ka hana me ka ʻikepili, e hoʻohana i ke ala ʻelua - ka protocol binary.

2. Ke hoʻohana nei i kahi protocol binary e hoʻopili i kahi awa kikoʻī

Aia i loko o ka hoʻonohonoho kahi kuhikuhi hoʻolohe, e wehe ana i kahi awa no nā kamaʻilio waho. Hoʻohana ʻia kēia awa me ka protocol binary a hiki i ka hōʻoia ʻana ma laila.

No kēia pili, hoʻohana ʻia ʻo tarantoolctl connect i ka helu port. Ke hoʻohana nei, hiki iā ʻoe ke hoʻopili i nā kikowaena mamao, hoʻohana i ka hōʻoia a hāʻawi i nā kuleana komo like ʻole.

Hoʻopaʻa ʻikepili a me ka Module pahu

No ka mea ʻo Tarantool kahi waihona a me kahi kikowaena noi, loaʻa iā ia nā modula like ʻole. Makemake mākou i ka module pahu - hoʻokō ia i ka hana me ka ʻikepili. Ke kākau ʻoe i kahi mea i ka pahu, kākau ʻo Tarantool i ka ʻikepili i ka disk, mālama iā ia i ka hoʻomanaʻo, a i ʻole hana i kekahi mea ʻē aʻe me ia.

Hoʻopaʻa leo

No ka laʻana, hele mākou i loko o ka module pahu a kāhea i ka hana box.once. E koi kēia iā Tarantool e holo i kā mākou code i ka wā i hoʻomaka ai ke kikowaena. Hana mākou i kahi e mālama ʻia ai kā mākou ʻikepili.

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

Ma hope o kēia, hana mākou i kahi kuhikuhi mua - kumu - kahi e hiki ai iā mākou ke ʻimi i ka ʻikepili. ʻO ka paʻamau, inā ʻaʻole ʻoe e kuhikuhi i nā ʻāpana, e hoʻohana ʻia ka māla mua i kēlā me kēia moʻolelo no ka papa kuhikuhi mua.

A laila hāʻawi mākou i kahi hāʻawi i ka mea hoʻohana malihini, kahi e hoʻopili ai mākou ma o ka protocol binary. ʻAe mākou i ka heluhelu ʻana, ke kākau ʻana a me ka hoʻokō ʻana i ka manawa holoʻokoʻa.

Ke hoʻohālikelike ʻia me nā ʻikepili maʻamau, maʻalahi nā mea āpau ma aneʻi. Loaʻa iā mākou ka hakahaka - kahi wahi i mālama ʻia ai kā mākou ʻikepili. Kapa ʻia kēlā me kēia moʻolelo he tuple. Hoʻopili ʻia ia ma MessagePack. He ʻano ʻano ʻoluʻolu loa kēia - he binary a lawe liʻiliʻi i ka lumi - 18 bytes versus 27.

Hāʻawi ʻo Tarantool iā ʻoe e hoʻohui i kahi waihona wikiwiki wikiwiki a me kahi noi e hana pū me lākou. ʻO kēia ka maʻalahi o ka hana

He mea maʻalahi ke hana pū me ia. Aneane i kēlā me kēia laina, hiki i kēlā me kēia moʻolelo ʻikepili ke loaʻa nā kolamu ʻokoʻa loa.

Hiki iā mākou ke nānā i nā wahi āpau me ka hoʻohana ʻana i ke kauoha Box.space. No ke koho ʻana i kahi laʻana, e kākau i ka laʻana box.space a loaʻa ka ʻike piha e pili ana.

ʻElua mau mīkini i kūkulu ʻia ʻo Tarantool: Memory a me Vinyl. Mālama ka hoʻomanaʻo i nā ʻikepili āpau i ka hoʻomanaʻo. No laila, hana maʻalahi a wikiwiki nā mea a pau. Hoʻolei ʻia ka ʻikepili i ka disk, a aia pū kekahi me kahi ʻōnaehana log kākau mua, no laila ʻaʻole e nalowale kekahi mea inā hāʻule ke kikowaena.

Mālama ʻo Vinyl i ka ʻikepili ma ka disk ma kahi ʻano i kamaʻāina iā mākou - ʻo ia hoʻi, hiki iā ʻoe ke mālama i nā ʻikepili hou aʻe ma mua o kā mākou hoʻomanaʻo, a e heluhelu ʻo Tarantula mai ka disk.

I kēia manawa e hoʻohana mākou i ka Memory.

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>

Papa kuhikuhi:

Pono e hana ʻia kahi papa kuhikuhi mua no kēlā me kēia wahi, no ka mea, ʻaʻohe mea e hana. E like me kekahi waihona, hana mākou i ka kahua mua - record ID.

Nā hapa:

Maanei mākou e hōʻike ai i ke ʻano o kā mākou index. Hoʻokahi ʻāpana ia - ʻo ke kahua mua a mākou e hoʻohana ai he ʻano ʻano ʻole i kau inoa ʻia - he integer maikaʻi. I koʻu hoʻomanaʻo ʻana mai ka palapala, ʻo ka helu kiʻekiʻe loa e hiki ke loaʻa he 18 quintillion. Nui kēlā.

A laila hiki iā mākou ke hoʻokomo i ka ʻikepili me ka hoʻohana ʻana i ke kauoha 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>

Hoʻohana ʻia ka māla mua ma ke ʻano he kī mua, no laila pono ke ʻano ʻokoʻa. ʻAʻole mākou i kaupalena ʻia e ka helu o nā kolamu, no laila hiki iā mākou ke hoʻokomo i nā ʻikepili e like me kā mākou makemake ma laila. Ua kuhikuhi ʻia lākou i ka format MessagePack, aʻu i wehewehe ai ma luna.

Hoʻopuka ʻikepili

A laila hiki iā mākou ke hōʻike i ka ʻikepili me ka hoʻohana ʻana i ke kauoha koho.

E hōʻike ana ʻo Box.example.select me ke kī {1} i ke komo i makemake ʻia. Inā mākou e hoʻohaʻahaʻa i ke kī, e ʻike mākou i nā moʻolelo āpau i loaʻa iā mākou. ʻOkoʻa lākou a pau i ka helu o nā kolamu, akā ma ʻaneʻi, ma ke kumu, ʻaʻohe manaʻo o nā kolamu - aia nā helu kahua.

Hiki ke loa'a ka nui o ka 'ikepili. A no ka laʻana, pono mākou e ʻimi iā lākou ma ke kahua lua. No ka hana ʻana i kēia, hana mākou i kahi papa kuhikuhi lua hou.


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

Hoʻohana mākou i ke kauoha Create_index.
E kapa aku kakou ia Secondary.

Ma hope o kēia pono ʻoe e kuhikuhi i nā ʻāpana. ʻO TREE ka ʻano papa kuhikuhi. ʻAʻole paha ia he ʻokoʻa, no laila e hoʻokomo i ka Unique = false.

A laila hōʻike mākou i nā ʻāpana o kā mākou index. ʻO ka māla ka helu o ke kahua kahi a mākou e hoʻopaʻa ai i ka index, a kuhikuhi i ke ʻano o ke kaula. A pēlā i hana ʻia ai.

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>

I kēia manawa pehea mākou e kapa ai:

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

Hoʻopakele

Inā mākou e hoʻomaka hou i ka hihia a hoʻāʻo e kāhea hou i ka ʻikepili, e ʻike mākou ʻaʻole i laila - nele nā ​​mea āpau. Hana ʻia kēia ma muli o ka hana ʻana o Tarantool i nā wahi hōʻoia a mālama i ka ʻikepili i ka disk, akā inā hoʻōki mākou i ka hana a hiki i ka hoʻopakele aʻe, e nalowale mākou i nā hana āpau - no ka mea, e hoʻōla mākou mai ka helu hope loa, ʻo ia hoʻi, ʻelua mau hola i hala.

ʻAʻole pono e mālama i kēlā me kēia kekona, no ka mea ʻaʻole maikaʻi ka hoʻolei mau ʻana i ka 20 GB ma ka disk.

No kēia kumu, ua haku ʻia a hoʻokō ʻia ka manaʻo o ka log write-ahead. Me kona kōkua, no kēlā me kēia hoʻololi ʻana i ka ʻikepili, hana ʻia kahi komo i loko o kahi faila log kākau mua.

Hoʻopaʻa ʻia kēlā me kēia komo ʻana a hiki i ka māka i loko o lākou. No kēia mau faila hoʻonohonoho mākou i ka nui - no ka laʻana, 64 MB. Ke piha, hoʻomaka ka hoʻopaʻa ʻana e hele i ka faila lua. A ma hope o ka hoʻomaka hou ʻana, hoʻihoʻi ʻia ʻo Tarantool mai ka helu hope loa a laila ʻōwili i nā hana hope a pau a hiki i ka pau ʻana.

Hāʻawi ʻo Tarantool iā ʻoe e hoʻohui i kahi waihona wikiwiki wikiwiki a me kahi noi e hana pū me lākou. ʻO kēia ka maʻalahi o ka hana

No ka hoʻokō ʻana i ia hoʻopaʻa ʻana, pono ʻoe e kuhikuhi i ke koho ma nā hoʻonohonoho box.cfg (ma ka file example.lua):

wal_mode = “write”;

hoʻohana ʻikepili

Me nā mea a mākou i kākau ai i kēia manawa, hiki iā ʻoe ke hoʻohana iā Tarantula e mālama i ka ʻikepili a e hana wikiwiki loa ia ma ke ʻano he waihona. A i kēia manawa ʻo ka icing ma ka pōpō ka mea hiki iā ʻoe ke hana me ia mea āpau.

Ke kākau ʻana i kahi noi

No ka laʻana, e kākau kākou i kēia palapala noi no Tarantula

E ʻike i ka noi ma lalo o ka spoiler

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

Hōʻike mākou i kekahi papa ma lua e wehewehe i nā huaʻōlelo. Pono kēia pā no ka hana ʻana i kahi kaula maʻamau.

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

Ma hope o kēlā, haʻi mākou i ka hana - randomString a hāʻawi i ka waiwai lōʻihi ma nā pale.

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

A laila hoʻohui mākou i ka mea hoʻokele http a me ka server http i kā mākou kikowaena Tarantula, JSON, a mākou e hoʻouna aku ai i ka mea kūʻai.

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

Ma hope o kēia, hoʻomaka mākou ma ke awa 8080 ma nā kikowaena kikowaena http āpau, e hoʻopaʻa inoa i nā noi a me nā hewa.

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

A laila, haʻi mākou i ke ala, no laila inā hiki mai kahi noi me ke ala GET ma ke awa 8080 / helu, a laila kāhea mākou i ka hana mai kahi laina. Hoʻihoʻi ia i ke kūlana - 200, 404, 403 a i ʻole nā ​​​​mea ʻē aʻe a mākou e kuhikuhi ai.

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

I loko o ke kino e hoʻihoʻi mākou i ka json.encode, i loko e hōʻike mākou i ka helu a me ka helu, i kapa ʻia a hōʻike i ka helu o nā moʻolelo i kā mākou waihona.

ʻO ka hanaʻelua

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)

Aia ma ka laina router: alahele ({ʻano = 'GET', ala = '/token'}, hana () kapa mākou i ka hana a hana i kahi hōʻailona.

Nā laina hōʻailona kūloko = randomString(32) he kaula maʻamau o 32 huapalapala.
I ka laina kūloko hope = box.space.example:len() lawe mākou i ka mea hope loa.
A ma ka laina box.space.example:hoʻokomo { hope + 1, hōʻailona } kākau mākou i ka ʻikepili i loko o kā mākou waihona, ʻo ia hoʻi, hoʻonui mākou i ka ID ma 1. Hiki ke hana i kēia, ma ke ala, ʻaʻole wale i kēia ala clumsy. Aia nā kaʻina no kēia ma Tarantula.

Kākau mākou i ka hōʻailona ma laila.

No laila, kākau mākou i ka palapala noi i hoʻokahi faila. Hiki iā ʻoe ke hoʻoponopono pololei i ka ʻikepili ma laila, a na ka pahu pahu e hana i nā hana lepo a pau iā ʻoe.

Hoʻolohe ʻo ia i ka http a hana pū me ka ʻikepili, aia nā mea āpau i kahi manawa hoʻokahi - ʻo ka noi a me ka ʻikepili. No laila, hiki wawe nā mea a pau.

No ka hoʻomaka, hoʻokomo mākou i ka module http:

Pehea mākou e hana ai i kēia, e nānā ma lalo o ka mea hao

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

Pono mākou e holo i ka 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:/#

Hoʻomaka mākou a hiki ke komo i nā modules

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

Hāʻawi ka /count iā mākou i ke kūlana 200.
Hoʻopuka ka /token i kahi hōʻailona a kākau i kēia hōʻailona i ka waihona.

Ke ho'āʻo nei i ka wikiwiki

E holo kāua i kahi hōʻailona no 50 noi. He 000 mau noi hoʻokūkū.

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

Hāʻawi ʻia nā hōʻailona. A ke hoʻopaʻa mau nei mākou i ka ʻikepili. 99% o nā noi i hana ʻia ma 42 milliseconds. No laila, loaʻa iā mākou ma kahi o 3500 mau noi i kēlā me kēia kekona ma kahi mīkini liʻiliʻi me 2 cores a me 4 gigabytes o ka hoʻomanaʻo.

Hiki iā ʻoe ke koho i kahi 50000 token a ʻike i kona waiwai.

ʻAʻole hiki iā ʻoe ke hoʻohana i ka http wale nō, akā e holo pū kekahi i nā hana hope e hana i kāu ʻikepili. Eia kekahi, aia nā ʻano hoʻohālikelike. No ka laʻana, hiki iā ʻoe ke kāhea i nā hana ma nā mea hou, e nānā i kekahi mea - nā hakakā kūpono.

Hiki iā ʻoe ke kākau pololei i nā palapala palapala i loko o ka waihona kikowaena ponoʻī, a ʻaʻole i kaupalena ʻia e kekahi mea, hoʻohui i nā modules a hoʻokō i kekahi loiloi.

Hiki i ke kikowaena noi ke komo i nā kikowaena waho, kiʻi i ka ʻikepili a hoʻohui i kāna waihona. E hoʻohana ʻia nā ʻikepili mai kēia waihona e nā noi ʻē aʻe.

E hana ʻo Tarantula iā ia iho, ʻaʻole pono ʻoe e kākau i kahi noi kaʻawale.

I ka hopena

ʻO kēia wale nō ka hapa mua o kahi hana nui. E paʻi koke ʻia ka lua ma ka blog Mail.ru Group, a e hoʻohui mākou i kahi loulou i kēia mea.

Inā makemake ʻoe e hele i nā hanana kahi e kūkulu ai mākou i kēia mau mea ma ka pūnaewele a nīnau i nā nīnau i ka manawa maoli, e hoʻolohe channel DevOps na REBRAIN.

Inā pono ʻoe e neʻe i ke ao a i ʻole he nīnau e pili ana i kāu ʻōnaehana, e ʻoluʻolu e waiho i kahi noi.

PS Loaʻa iā mākou 2 mau loiloi manuahi i kēlā me kēia mahina, malia paha ʻo kāu papahana kekahi o lākou.

Source: www.habr.com

Pākuʻi i ka manaʻo hoʻopuka