ఐదేళ్ల క్రితం నేను టరాన్టూల్తో కలిసి పనిచేయడానికి ప్రయత్నించాను, కానీ అది నాకు పని చేయలేదు. కానీ ఇటీవల నేను హడూప్ గురించి, MapReduce ఎలా పనిచేస్తుందనే దాని గురించి ఒక వెబ్నార్ని నిర్వహించాను. అక్కడ నన్ను ప్రశ్న అడిగారు - “ఈ పని కోసం టరాన్టూల్ను ఎందుకు ఉపయోగించకూడదు?”.
ఉత్సుకత కోసం, నేను దానికి తిరిగి రావాలని నిర్ణయించుకున్నాను, తాజా సంస్కరణను పరీక్షించాను - మరియు ఈసారి నేను ప్రాజెక్ట్ను నిజంగా ఇష్టపడ్డాను. ఇప్పుడు నేను టరాన్టూల్లో సరళమైన అప్లికేషన్ను ఎలా వ్రాయాలో చూపుతాను, దానిని లోడ్ చేసి పనితీరును తనిఖీ చేయండి మరియు ప్రతిదీ ఎంత సులభంగా మరియు చల్లగా ఉందో మీరు చూస్తారు.
టరాన్టూల్ అంటే ఏమిటి
టరాన్టూల్ తనను తాను అల్ట్రా-ఫాస్ట్ డేటాబేస్గా ఉంచుతుంది. మీకు కావలసిన డేటాను అక్కడ ఉంచవచ్చు. అదనంగా, వాటిని పునరావృతం చేయండి, షార్డ్ - అంటే, అనేక సర్వర్లలో భారీ మొత్తంలో డేటాను విభజించి, వాటి నుండి ఫలితాలను కలపండి - తప్పు-తట్టుకునే మాస్టర్-మాస్టర్ లింక్లను చేయండి.
రెండవది, ఇది అప్లికేషన్ సర్వర్. మీరు దానిపై మీ అప్లికేషన్లను వ్రాయవచ్చు, డేటాతో పని చేయవచ్చు, ఉదాహరణకు, కొన్ని నిబంధనల ప్రకారం నేపథ్యంలో పాత ఎంట్రీలను తొలగించండి. మీరు నేరుగా టరాన్టులాలో Http సర్వర్ని వ్రాయవచ్చు, అది డేటాతో పని చేస్తుంది: వారి నంబర్ను ఇవ్వండి, అక్కడ కొత్త డేటాను వ్రాయండి మరియు అన్నింటినీ మాస్టర్గా తగ్గించండి.
కుర్రాళ్ళు 300-లైన్ మెసేజ్ క్యూను ఎలా తయారు చేశారనే దాని గురించి నేను ఒక కథనాన్ని చదివాను, అది కేవలం కన్నీళ్లు మరియు త్రాష్లను చేస్తుంది - వారు సెకనుకు కనీసం 20 సందేశాలను కలిగి ఉంటారు. ఇక్కడ మీరు నిజంగా తిరగవచ్చు మరియు చాలా పెద్ద అప్లికేషన్ను వ్రాయవచ్చు మరియు పోస్ట్గ్రెస్లో వలె ఇవి నిల్వ చేయబడవు.
సుమారుగా అటువంటి సర్వర్, సరళమైనది మాత్రమే, నేను ఈ వ్యాసంలో వివరించడానికి ప్రయత్నిస్తాను.
సెట్టింగ్
పరీక్ష కోసం, నేను మూడు ప్రామాణిక వర్చువల్ మిషన్లను ప్రారంభించాను - 20 గిగాబైట్ హార్డ్ డ్రైవ్, ఉబుంటు 18.04. 2 వర్చువల్ CPU మరియు 4 గిగాబైట్ల మెమరీ.
మేము టరాన్టూల్ని ఇన్స్టాల్ చేస్తాము - బాష్ స్క్రిప్ట్ను రన్ చేయండి లేదా రిపోజిటరీని జోడించండి మరియు టరాన్టూల్ని ఇన్స్టాల్ చేయండి. స్క్రిప్ట్కి లింక్ - (కర్ల్ -ఎల్
tarantoolctl టరాన్టులా ఉదాహరణలను నిర్వహించడానికి ప్రధాన ఆదేశం.
/etc/tarantool - ఇక్కడ మొత్తం కాన్ఫిగరేషన్ ఉంది.
var/log/tarantool - ఇక్కడ లాగ్లు ఉన్నాయి.
var/lib/tarantool - ఇక్కడ డేటా ఉంది, ఆపై అవి ఉదాహరణలుగా విభజించబడ్డాయి.
ఇన్స్టాన్స్-అందుబాటు మరియు ఇన్స్టాన్స్-ఎనేబుల్ ఫోల్డర్లు ఉన్నాయి - ఇది లాంచ్ చేయబడే వాటిని కలిగి ఉంటుంది - లువా కోడ్తో ఇన్స్టాన్స్ కాన్ఫిగరేషన్ ఫైల్, ఇది ఏ పోర్ట్లను వింటుంది, దానికి ఏ మెమరీ అందుబాటులో ఉంది, వినైల్ ఇంజిన్ సెట్టింగ్లు, స్టార్టప్లో పనిచేసే కోడ్ వివరిస్తుంది సర్వర్లు, షార్డింగ్, క్యూలు, వాడుకలో లేని డేటా తొలగింపు మొదలైనవి.
పోస్ట్గ్రెస్లో లాగా ఉదాహరణలు పని చేస్తాయి. ఉదాహరణకు, మీరు వివిధ పోర్ట్లలో వేలాడుతున్న డేటాబేస్ యొక్క బహుళ కాపీలను అమలు చేయాలనుకుంటున్నారు. ఒక సర్వర్లో అనేక డేటాబేస్ ఉదంతాలు ప్రారంభించబడిందని తేలింది, ఇవి వేర్వేరు పోర్ట్లలో వేలాడతాయి. వారు పూర్తిగా భిన్నమైన సెట్టింగులను కలిగి ఉంటారు - ఒక ఉదాహరణ ఒక తర్కాన్ని అమలు చేస్తుంది, రెండవది - మరొకటి.
ఉదాహరణ నిర్వహణ
మేము టరాన్టులా ఇన్స్టాన్స్లను నిర్వహించడానికి అనుమతించే tarantoolctl కమాండ్ని కలిగి ఉంది. ఉదాహరణకు, tarantoolctl చెక్ ఉదాహరణ కాన్ఫిగరేషన్ ఫైల్ని తనిఖీ చేస్తుంది మరియు సింటాక్స్ లోపాలు లేకుంటే ఫైల్ సరేనని చెబుతుంది.
మీరు ఉదాహరణ స్థితిని చూడవచ్చు - tarantoolctl స్థితి ఉదాహరణ. అదే విధంగా, మీరు స్టార్ట్, స్టాప్, రీస్టార్ట్ చేయవచ్చు.
ఒక ఉదాహరణ రన్ అయిన తర్వాత, దానికి కనెక్ట్ చేయడానికి రెండు మార్గాలు ఉన్నాయి.
1. అడ్మినిస్ట్రేటివ్ కన్సోల్
డిఫాల్ట్గా, టరాన్టూల్ ఒక సాకెట్ను తెరుస్తుంది మరియు టరాన్టులాను నియంత్రించడానికి సాదా ASCII వచనాన్ని పంపుతుంది. కన్సోల్కు కనెక్షన్ ఎల్లప్పుడూ అడ్మిన్ వినియోగదారు కింద జరుగుతుంది, ప్రామాణీకరణ లేదు, కాబట్టి మీరు బయట టరాన్టులాను నియంత్రించడానికి కన్సోల్ పోర్ట్ను తీసుకోవలసిన అవసరం లేదు.
ఈ విధంగా కనెక్ట్ చేయడానికి, మీరు Tarantoolctl ఎంటర్ ఇన్స్టాన్స్ పేరును నమోదు చేయాలి. కమాండ్ కన్సోల్ను ప్రారంభించి, నిర్వాహక వినియోగదారుగా కనెక్ట్ చేస్తుంది. కన్సోల్ పోర్ట్ను బయటికి ఎప్పుడూ బహిర్గతం చేయవద్దు - దానిని యూనిట్ సాకెట్గా వదిలివేయడం మంచిది. అప్పుడు సాకెట్కు రైట్ యాక్సెస్ ఉన్నవారు మాత్రమే టరాన్టులాకు కనెక్ట్ చేయగలుగుతారు.
పరిపాలనా విషయాలకు ఈ పద్ధతి అవసరం. డేటాతో పని చేయడానికి, రెండవ పద్ధతిని ఉపయోగించండి - బైనరీ ప్రోటోకాల్.
2. ఒక నిర్దిష్ట పోర్ట్కు కనెక్ట్ చేయడానికి బైనరీ ప్రోటోకాల్ను ఉపయోగించడం
కాన్ఫిగరేషన్లో వినండి డైరెక్టివ్ ఉంది, ఇది బాహ్య కమ్యూనికేషన్ల కోసం పోర్ట్ను తెరుస్తుంది. ఈ పోర్ట్ బైనరీ ప్రోటోకాల్తో ఉపయోగించబడుతుంది మరియు అక్కడ ప్రామాణీకరణ ప్రారంభించబడుతుంది.
ఈ కనెక్షన్ కోసం, tarantoolctl కనెక్ట్ పోర్ట్ నంబర్ ఉపయోగించబడుతుంది. దీన్ని ఉపయోగించి, మీరు రిమోట్ సర్వర్లకు కనెక్ట్ చేయవచ్చు, ప్రామాణీకరణను ఉపయోగించవచ్చు మరియు వివిధ యాక్సెస్ హక్కులను ఇవ్వవచ్చు.
డేటా రికార్డింగ్ మరియు బాక్స్ మాడ్యూల్
టరాన్టూల్ డేటాబేస్ మరియు అప్లికేషన్ సర్వర్ అయినందున, దీనికి వివిధ మాడ్యూల్స్ ఉన్నాయి. బాక్స్ మాడ్యూల్పై మాకు ఆసక్తి ఉంది - ఇది డేటాతో పనిని అమలు చేస్తుంది. మీరు ఒక పెట్టెలో ఏదైనా వ్రాసినప్పుడు, Tarantool డేటాను డిస్క్కి వ్రాస్తాడు, మెమరీలో నిల్వ చేస్తుంది లేదా దానితో మరేదైనా చేస్తుంది.
రికార్డు
ఉదాహరణకు, మేము బాక్స్ మాడ్యూల్లోకి వెళ్లి 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
ఆ తర్వాత, మేము ప్రాథమిక సూచికను సృష్టిస్తాము - ప్రైమరీ - దీని ద్వారా మేము డేటా కోసం శోధించవచ్చు. డిఫాల్ట్గా, పారామీటర్లు ఏవీ పేర్కొనబడకపోతే, ప్రైమర్ ఇండెక్స్ కోసం ప్రతి ఎంట్రీలోని మొదటి ఫీల్డ్ ఉపయోగించబడుతుంది.
అప్పుడు మేము అతిథి వినియోగదారుకు మంజూరు చేస్తాము, దాని క్రింద మేము బైనరీ ప్రోటోకాల్ ద్వారా కనెక్ట్ చేస్తాము. మేము మొత్తం సందర్భంలో చదవడానికి, వ్రాయడానికి మరియు అమలు చేయడానికి అనుమతిస్తాము.
సాంప్రదాయ డేటాబేస్లతో పోలిస్తే, ఇక్కడ ప్రతిదీ చాలా సులభం. మాకు స్థలం ఉంది - మా డేటా కేవలం నిల్వ చేయబడిన ప్రాంతం. ప్రతి ప్రవేశాన్ని టుపుల్ అంటారు. ఇది మెసేజ్ప్యాక్లో ప్యాక్ చేయబడింది. ఇది చాలా కూల్ ఫార్మాట్ - ఇది బైనరీ మరియు తక్కువ స్థలాన్ని తీసుకుంటుంది - 18 బైట్లు మరియు 27.
అతనితో పనిచేయడం చాలా సౌకర్యవంతంగా ఉంటుంది. దాదాపు ప్రతి లైన్, ప్రతి డేటా ఎంట్రీ పూర్తిగా భిన్నమైన నిలువు వరుసలను కలిగి ఉంటుంది.
మేము Box.space కమాండ్ని ఉపయోగించి అన్ని స్పేస్లను చూడవచ్చు. నిర్దిష్ట ఉదాహరణను ఎంచుకోవడానికి, మేము box.space ఉదాహరణ వ్రాసి దానిపై పూర్తి సమాచారాన్ని పొందుతాము.
టరాన్టూల్లో రెండు రకాల ఇంజిన్లు నిర్మించబడ్డాయి: మెమరీ మరియు వినైల్. మెమరీ మొత్తం డేటాను మెమరీలో నిల్వ చేస్తుంది. అందువలన, ప్రతిదీ సులభంగా మరియు త్వరగా పనిచేస్తుంది. డేటా డిస్క్కి డంప్ చేయబడింది మరియు రైట్ ఎహెడ్ లాగ్ మెకానిజం కూడా ఉంది, కాబట్టి సర్వర్ క్రాష్ అయినట్లయితే మేము ఏమీ కోల్పోము.
వినైల్ డిస్క్లో డేటాను మరింత సుపరిచితమైన రూపంలో నిల్వ చేస్తుంది - అంటే, మీరు మా వద్ద ఉన్న మెమరీ కంటే ఎక్కువ డేటాను నిల్వ చేయవచ్చు మరియు టరాన్టులా దానిని డిస్క్ నుండి చదువుతుంది.
ఇప్పుడు మనం మెమరీని ఉపయోగిస్తాము.
unix/:/var/run/tarantool/example.control> box.space.example
---
- engine: memtx
before_replace: 'function: 0x41eb02c8'
on_replace: 'function: 0x41eb0568'
ck_constraint: []
field_count: 0
temporary: false
index:
0: &0
unique: true
parts:
- type: unsigned
is_nullable: false
fieldno: 1
id: 0
space_id: 512
type: TREE
name: primary
primary: *0
is_local: false
enabled: true
name: example
id: 512
...
unix/:/var/run/tarantool/example.control>
సూచిక:
ఏదైనా స్థలం కోసం ప్రాథమిక సూచిక తప్పనిసరిగా సృష్టించబడాలి, ఎందుకంటే అది లేకుండా ఏదీ పని చేయదు. ఏదైనా డేటాబేస్లో వలె, మేము మొదటి ఫీల్డ్ను సృష్టిస్తాము - రికార్డ్ ID.
భాగాలు:
ఇక్కడే మన ఇండెక్స్ దేనిని కలిగి ఉందో తెలియజేస్తాము. ఇది ఒక భాగాన్ని కలిగి ఉంటుంది - మేము ఉపయోగించే మొదటి ఫీల్డ్, సైన్ చేయని టైప్ చేయండి - సానుకూల పూర్ణాంకం. డాక్యుమెంటేషన్ నుండి నాకు గుర్తున్నంత వరకు, గరిష్ట సంఖ్య 18 క్వింటిలియన్లు. చాలా అద్భుతం.
మేము ఇన్సర్ట్ కమాండ్ ఉపయోగించి డేటాను ఇన్సర్ట్ చేయవచ్చు.
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>
మొదటి ఫీల్డ్ ప్రాథమిక కీగా ఉపయోగించబడుతుంది, కనుక ఇది ప్రత్యేకంగా ఉండాలి. మేము నిలువు వరుసల సంఖ్యతో పరిమితం కాదు, కాబట్టి మనకు నచ్చినంత డేటాను అక్కడ చేర్చవచ్చు. అవి నేను పైన వివరించిన మెసేజ్ప్యాక్ ఫార్మాట్లో పేర్కొనబడ్డాయి.
డేటా అవుట్పుట్
అప్పుడు మనం సెలెక్ట్ కమాండ్తో డేటాను ప్రదర్శించవచ్చు.
Box.example.select కీతో {1} కావలసిన ఎంట్రీని ప్రదర్శిస్తుంది. మేము కీని వదిలివేస్తే, మన వద్ద ఉన్న అన్ని రికార్డులను చూస్తాము. అవి అన్ని నిలువు వరుసల సంఖ్యలో భిన్నంగా ఉంటాయి, కానీ ఇక్కడ, సూత్రప్రాయంగా, నిలువు వరుసల భావన లేదు - ఫీల్డ్ నంబర్లు ఉన్నాయి.
మీకు కావలసినంత ఎక్కువ డేటా ఉండవచ్చు. మరియు ఉదాహరణకు, మేము వాటిని రెండవ ఫీల్డ్లో వెతకాలి. దీన్ని చేయడానికి, మేము కొత్త ద్వితీయ సూచికను తయారు చేస్తాము.
box.space.example:create_index( ‘secondary’, { type = ‘TREE’, unique = false, parts = {{field = 2, type =’string’} }})
మేము Create_index ఆదేశాన్ని ఉపయోగిస్తాము.
మేము దానిని సెకండరీ అని పిలుస్తాము.
ఆ తరువాత, మీరు పారామితులను పేర్కొనాలి. సూచిక రకం TREE. ఇది ప్రత్యేకంగా ఉండకపోవచ్చు, కాబట్టి మనం Unique = false అని నమోదు చేస్తాము.
అప్పుడు మన సూచిక ఏ భాగాలను కలిగి ఉందో మేము సూచిస్తాము. ఫీల్డ్ అనేది మనం ఇండెక్స్ను బంధించే ఫీల్డ్ యొక్క సంఖ్య మరియు స్ట్రింగ్ రకాన్ని నిర్దేశిస్తుంది. అందువలన ఇది సృష్టించబడింది.
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 చెక్పాయింట్లను తయారు చేసి, డేటాను డిస్క్లో సేవ్ చేయడం వలన ఇది జరుగుతుంది, అయితే తదుపరి సేవ్కు ముందు మేము పని చేయడం ఆపివేస్తే, మేము అన్ని కార్యకలాపాలను కోల్పోతాము - ఎందుకంటే మేము చివరి చెక్పాయింట్ నుండి కోలుకుంటాము, ఉదాహరణకు, రెండు గంటల క్రితం.
ప్రతి సెకనును ఆదా చేయడం కూడా పని చేయదు - ఎందుకంటే నిరంతరం 20 GBని డిస్క్కి డంప్ చేయడం అనేది చాలా ఆలోచన.
దీని కోసం రైట్-ఎహెడ్ లాగ్ అనే కాన్సెప్ట్ని కనిపెట్టి అమలు చేశారు. దాని సహాయంతో, డేటాలోని ప్రతి మార్పు కోసం, చిన్న రైట్-ఎహెడ్ లాగ్ ఫైల్లో రికార్డ్ సృష్టించబడుతుంది.
తనిఖీ కేంద్రం వరకు ప్రతి ఎంట్రీ వాటిలో నిల్వ చేయబడుతుంది. ఈ ఫైళ్ళ కోసం, మేము పరిమాణాన్ని సెట్ చేసాము - ఉదాహరణకు, 64 mb. అది నిండినప్పుడు, రికార్డింగ్ రెండవ ఫైల్కు వెళ్లడం ప్రారంభమవుతుంది. మరియు పునఃప్రారంభించిన తర్వాత, Tarantool చివరి చెక్పాయింట్ నుండి కోలుకుంటుంది మరియు అది ఆగిపోయే వరకు అన్ని తదుపరి లావాదేవీలను రోల్ చేస్తుంది.
అటువంటి రికార్డింగ్ని నిర్వహించడానికి, మీరు box.cfg సెట్టింగ్లలో (example.lua ఫైల్లో) ఒక ఎంపికను పేర్కొనాలి:
wal_mode = “write”;
డేటా వినియోగం
మేము ఇప్పుడు వ్రాసిన దానితో, మీరు డేటాను నిల్వ చేయడానికి టరాన్టులాని ఉపయోగించవచ్చు మరియు ఇది డేటాబేస్ వలె చాలా వేగంగా పని చేస్తుంది. మరియు ఇప్పుడు కేక్ మీద చెర్రీ - మీరు అన్ని ఏమి చేయవచ్చు.
అప్లికేషన్ రాయడం
ఉదాహరణకు, టరాన్టులా కోసం అటువంటి అప్లికేషన్ను వ్రాద్దాం
స్పాయిలర్ కింద ఉన్న అప్లికేషన్ను చూడండి
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()
మేము చిహ్నాలను నిర్వచించే కొన్ని పట్టికలను లువాలో ప్రకటిస్తాము. యాదృచ్ఛిక స్ట్రింగ్ను రూపొందించడానికి ఈ పట్టిక అవసరం.
local charset = {} do -- [0-9a-zA-Z]
for c = 48, 57 do table.insert(charset, string.char(c)) end
for c = 65, 90 do table.insert(charset, string.char(c)) end
for c = 97, 122 do table.insert(charset, string.char(c)) end
end
ఆ తరువాత, మేము ఒక ఫంక్షన్ - randomString డిక్లేర్ చేస్తాము మరియు బ్రాకెట్లలో పొడవు విలువను ఇస్తాము.
local function randomString(length)
if not length or length <= 0 then return '' end
math.randomseed(os.clock()^5)
return randomString(length - 1) .. charset[math.random(1, #charset)]
end
అప్పుడు మేము http రౌటర్ మరియు http సర్వర్ను మా టరాన్టులా సర్వర్, 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
})
తర్వాత, పోర్ట్ 8080 /కౌంట్లో GET పద్ధతితో అభ్యర్థన వస్తే, మేము ఒక లైన్ నుండి ఫంక్షన్ని పిలుస్తాము. ఇది స్థితిని అందిస్తుంది - 200, 404, 403 లేదా మనం పేర్కొన్నది.
router:route({method = 'GET', path = '/count'}, function()
return {status = 200, body = json.encode({count = get_count()})}
end)
శరీరంలో, మేము json.encodeని తిరిగి ఇస్తాము, మేము దానిలో గణన మరియు గెట్కౌంట్ని నిర్దేశిస్తాము, దీనిని మా డేటాబేస్లోని రికార్డ్ల సంఖ్య అని పిలుస్తారు మరియు చూపుతుంది.
రెండవ పద్ధతి
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)
లైన్లో ఎక్కడ రూటర్:రూట్({మెథడ్ = 'గెట్', పాత్ = '/టోకెన్'}, ఫంక్షన్() మేము ఫంక్షన్ని పిలుస్తాము మరియు టోకెన్ను రూపొందిస్తాము.
వరుసగా స్థానిక టోకెన్ = రాండమ్ స్ట్రింగ్(32) 32 అక్షరాల యాదృచ్ఛిక స్ట్రింగ్.
లైన్ లో స్థానిక గత = box.space.example:len() మేము చివరి మూలకాన్ని బయటకు తీస్తాము.
మరియు లైన్ లో box.space.example:insert{ last + 1, token } మేము మా డేటాబేస్కు డేటాను వ్రాస్తాము, అంటే, మేము IDని 1 ద్వారా పెంచుతాము. ఇది వికృతమైన రీతిలో మాత్రమే కాకుండా, మార్గం ద్వారా కూడా చేయవచ్చు. టరాన్టులా ఈ కేసుకు సంబంధించిన సన్నివేశాలను కలిగి ఉంది.
మేము అక్కడ టోకెన్ వ్రాస్తాము.
ఈ విధంగా, మేము ఒక ఫైల్లో అప్లికేషన్ను వ్రాసాము. మీరు వెంటనే డేటాను యాక్సెస్ చేయవచ్చు మరియు బాక్స్ మాడ్యూల్ మీ కోసం అన్ని మురికి పనిని చేస్తుంది.
ఇది http వింటుంది మరియు డేటాతో పని చేస్తుంది, ప్రతిదీ ఒకే సందర్భంలో ఉంటుంది - అప్లికేషన్ మరియు డేటా రెండూ. అందువలన, ప్రతిదీ చాలా త్వరగా జరుగుతుంది.
అమలు చేయడానికి, మేము 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:/#
/గణన మాకు స్థితి 200 ఇస్తుంది.
/టోకెన్ టోకెన్ను జారీ చేస్తుంది మరియు ఈ టోకెన్ను డేటాబేస్కు వ్రాస్తుంది.
పరీక్ష వేగం
50 అభ్యర్థనల కోసం బెంచ్మార్క్ని అమలు చేద్దాం. పోటీ అభ్యర్థనలు 000 ఉంటాయి.
root@test2:/# ab -c 500 -n 50000 http://127.0.0.1:8080/token
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Completed 50000 requests
Finished 50000 requests
Server Software: Tarantool
Server Hostname: 127.0.0.1
Server Port: 8080
Document Path: /token
Document Length: 44 bytes
Concurrency Level: 500
Time taken for tests: 14.578 seconds
Complete requests: 50000
Failed requests: 0
Total transferred: 7950000 bytes
HTML transferred: 2200000 bytes
Requests per second: 3429.87 [#/sec] (mean)
Time per request: 145.778 [ms] (mean)
Time per request: 0.292 [ms] (mean, across all concurrent requests)
Transfer rate: 532.57 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 10 103.2 0 3048
Processing: 12 69 685.1 15 13538
Waiting: 12 69 685.1 15 13538
Total: 12 78 768.2 15 14573
Percentage of the requests served within a certain time (ms)
50% 15
66% 15
75% 16
80% 16
90% 16
95% 16
98% 21
99% 42
100% 14573 (longest request)
root@test2:/#
టోకెన్లు జారీ చేస్తారు. మరియు మేము నిరంతరం డేటాను రికార్డ్ చేస్తున్నాము. 99% అభ్యర్థనలు 42 మిల్లీసెకన్లలో పూర్తయ్యాయి. దీని ప్రకారం, మేము ఒక చిన్న మెషీన్లో సెకనుకు దాదాపు 3500 అభ్యర్థనలను కలిగి ఉన్నాము, ఇక్కడ 2 కోర్లు మరియు 4 గిగాబైట్ల మెమరీ ఉన్నాయి.
మీరు కొన్ని 50000 టోకెన్ని కూడా ఎంచుకోవచ్చు మరియు దాని విలువను చూడవచ్చు.
మీరు మీ డేటాను ప్రాసెస్ చేసే http, రన్ బ్యాక్గ్రౌండ్ ఫంక్షన్లను మాత్రమే ఉపయోగించవచ్చు. అదనంగా, వివిధ ట్రిగ్గర్లు ఉన్నాయి. ఉదాహరణకు, మీరు అప్డేట్లలో ఫంక్షన్లకు కాల్ చేయవచ్చు, ఏదైనా తనిఖీ చేయవచ్చు - వైరుధ్యాలను పరిష్కరించండి.
మీరు డేటాబేస్ సర్వర్లోనే నేరుగా స్క్రిప్ట్ అప్లికేషన్లను వ్రాయవచ్చు మరియు దేనికీ పరిమితం కాకూడదు, ఏదైనా మాడ్యూల్లను కనెక్ట్ చేయండి మరియు ఏదైనా లాజిక్ను అమలు చేయండి.
అప్లికేషన్ సర్వర్ బాహ్య సర్వర్లను యాక్సెస్ చేయగలదు, డేటాను సేకరించి దాని డేటాబేస్కు జోడించగలదు. ఈ డేటాబేస్ నుండి డేటా ఇతర అనువర్తనాల ద్వారా ఉపయోగించబడుతుంది.
ఇది టరాన్టులా ద్వారానే చేయబడుతుంది మరియు ప్రత్యేక అప్లికేషన్ వ్రాయవలసిన అవసరం లేదు.
ముగింపులో
ఇది పెద్ద ఉద్యోగంలో మొదటి భాగం మాత్రమే. రెండవది Mail.ru గ్రూప్ బ్లాగ్లో అతి త్వరలో ప్రచురించబడుతుంది మరియు మేము ఖచ్చితంగా ఈ మెటీరియల్లో దానికి లింక్ను జోడిస్తాము.
మేము వీటిని ఆన్లైన్లో సృష్టించే ఈవెంట్లకు హాజరు కావడానికి మరియు నిజ సమయంలో ప్రశ్నలు అడగడానికి మీకు ఆసక్తి ఉంటే, కనెక్ట్ చేయండి
మీరు క్లౌడ్కి వెళ్లాలనుకుంటే లేదా మీ మౌలిక సదుపాయాల గురించి ఏవైనా ప్రశ్నలు ఉంటే,
PS మాకు నెలకు 2 ఉచిత ఆడిట్లు ఉన్నాయి, బహుశా మీ ప్రాజెక్ట్ వాటిలో ఒకటి కావచ్చు.
మూలం: www.habr.com