奜å¥å¿ã®ããã«ãç§ã¯ããã«æ»ã£ãŠææ°ããŒãžã§ã³ããã¹ãããããšã«ããŸãã-ãããŠä»åã¯ãããžã§ã¯ããæ¬åœã«å¥œãã§ããã次ã«ã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åã®ç¡æç£æ»ãè¡ã£ãŠããŸããããããããªãã®ãããžã§ã¯ãã¯ãã®äžã«å«ãŸããã§ãããã