Ho ngola mojule oa rona o felloang ke nako bakeng sa tarantool

Ho ngola mojule oa rona o felloang ke nako bakeng sa tarantool

Nakong e fetileng re ne re tobane le bothata ba ho hloekisa li-tuples libakeng tarantool. Ho hloekisa ho ne ho sa lokela ho qaloa eseng ha tarantool e se e felloa ke mohopolo, empa esale pele le ka makhetlo a itseng. Bakeng sa mosebetsi ona, tarantool e na le module e ngotsoeng ka Lua e bitsoang ho felloa ke nako. Ka mor'a ho sebelisa mojule ona ka nako e khutšoanyane, re ile ra hlokomela hore e ne e sa re tšoanele: ka lebaka la ho hloekisa kamehla palo e kholo ea data, Lua e fanyehiloe ho GC. Ka hona, re ile ra nahana ka ho iketsetsa mojule oa rona o felisitsoeng ke capped, re tšepile hore khoutu e ngotsoeng ka puo ea matsoalloa e tla rarolla mathata a rona ka tsela e molemohali.

Mohlala o motle ho rona e ne e le mojule oa tarantool o bitsoang hopotsoe. Mokhoa o sebelisoang ho eona o thehiloe tabeng ea hore sebaka se arohaneng se bōptjoa sebakeng, se bontšang nako ea bophelo ba tuple, ka mantsoe a mang, ttl. Mojule o ka morao o hlahloba sebaka, o bapisa TTL le nako ea hona joale mme o etsa qeto ea hore na o tla hlakola tuple kapa che. Khoutu ea mojule ea memcached e bonolo ebile e ntle, empa e tloaelehile haholo. Taba ea pele, ha e nahanele mofuta oa index e ntseng e khasoa le ho hlakoloa. Taba ea bobeli, papaling e 'ngoe le e' ngoe, li-tuples lia hlahlojoa, tseo palo ea tsona e ka bang kholo haholo. 'Me haeba mojuleng o felloang ke nako bothata ba pele bo rarollotsoe ( index ea sefate e arotsoe ka sehlopha se arohaneng), joale ea bobeli e ntse e sa fumane tlhokomelo leha e le efe. Lintlha tsena tse tharo li ile tsa rera esale pele khetho ea ho ngola khoutu ea ka.

tlhaloso

Litokomane tsa tarantool li na le molemo haholo thuto mabapi le mokhoa oa ho ngola mekhoa ea hau e bolokiloeng ho C. Pele ho tsohle, ke fana ka tlhahiso ea hore u tloaelane le eona e le hore u utloisise tse kentsoeng ka litaelo le khoutu e tla hlaha ka tlase. Hape ke habohlokoa ho ela hloko sesupo ho lintho tse fumanehang ha u ngola mojule oa hau oa capped, e leng lebokose le reng, faeba, index ea и txn.

Ha re qaleng ho tloha hole mme re shebe hore na mojule o felloang ke nako o shebahala joang ho tsoa kantle:

fiber = require('fiber')
net_box = require('net.box')
box.cfg{listen = 3300}
box.schema.func.create('libcapped-expirationd.start', {language = 'C'})
box.schema.user.grant('guest', 'execute', 'function', 'libcapped-expirationd.start')
box.schema.func.create('libcapped-expirationd.kill', {language = 'C'})
box.schema.user.grant('guest', 'execute', 'function', 'libcapped-expirationd.kill')
box.schema.space.create('tester')
box.space.tester:create_index('primary', {unique = true, parts = {1, 'unsigned'}})
capped_connection = net_box:new(3300)

Bakeng sa ho nolofatsa, re qala tarantool bukeng moo laeborari ea rona ea libcapped-expirationd.so e leng teng. Mesebetsi e 'meli e romeloa ka ntle ho laebrari: qala le ho bolaea. Mohato oa pele ke ho etsa hore mesebetsi ena e fumanehe ho tsoa ho Lua u sebelisa box.schema.func.create le box.schema.user.grant. Ebe u theha sebaka seo li-tuples li tla ba le likarolo tse tharo feela: ea pele ke sekhetho se ikhethileng, sa bobeli ke lengolo-tsoibila, 'me sa boraro ke nako ea bophelo ba tuple. Re haha ​​index ea lifate ka holim'a tšimo ea pele mme re e bitsa e ka sehloohong. Ka mor'a moo re fumana ntho ea ho hokahanya laebraring ea rona ea lehae.

Ka mor'a mosebetsi oa ho itokisa, tsamaisa mosebetsi oa ho qala:

capped_connection:call('libcapped-expirationd.start', {'non-indexed', box.space.tester.id, box.space.tester.index.primary, box.space.tester.index.primary, 3, 1024, 3600})

Mohlala ona o tla sebetsa nakong ea ho skena ka mokhoa o ts'oanang hantle le mojule o felloang ke nako, o ngotsoeng ka Lua. Khang ea pele mosebetsing oa ho qala ke lebitso le ikhethang la mosebetsi. Ea bobeli ke sekhetho sa sebaka. Ea boraro ke index e ikhethang eo ka eona li-tuples li tla hlakoloa. Ea bone ke index eo li-tuples li tla fetisoa ka eona. Ea bohlano ke palo ea tšimo ea tuple ka bophelo bohle (palo e qala ho tloha ho 1, eseng 0!). Ea botšelela le ea bosupa ke litlhophiso tsa ho hlahloba. 1024 ke palo e phahameng ka ho fetesisa ea li-tuples tse ka bonoang khoebong e le 'ngoe. 3600 - nako e felletseng ea tlhahlobo ka metsotsoana.

Hlokomela hore mohlala o sebelisa index e tšoanang bakeng sa ho khasa le ho hlakola. Haeba sena ke index ea sefate, joale ho tsamaea ho etsoa ho tloha ho senotlolo se senyenyane ho ea ho se seholoanyane. Haeba ho na le tse ling, ka mohlala, index ea hash, joale ho tsamaea ho etsoa, ​​​​e le molao, ka tatellano e sa reroang. Li-tuple tsa sebaka kaofela li hlahlojoa ka skena se le seng.

Ha re kenyeng likopi tse 'maloa sebakeng ka bophelo bohle ba metsotsoana e 60:

box.space.tester:insert{0, '[email protected]', math.floor(fiber.time()) + 60}
box.space.tester:insert{1, '[email protected]', math.floor(fiber.time()) + 60}
box.space.tester:insert{2, '[email protected]', math.floor(fiber.time()) + 60}

Ha re hlahlobe hore na ho kenya ho atlehile:

tarantool> box.space.tester.index.primary:select()
---
- - [0, '[email protected]', 1576418976]
  - [1, '[email protected]', 1576418976]
  - [2, '[email protected]', 1576418976]
...

Ha re phete khetho ka mor'a metsotsoana e 60+ (ho bala ho tloha qalong ea ho kenngoa ha tuple ea pele) 'me u bone hore mojule o felisitsoeng o se o sebelitse:

tarantool> box.space.tester.index.primary:select()
---
  - []
...

Ha re emise mosebetsi:

capped_connection:call('libcapped-expirationd.kill', {'non-indexed'})

Ha re shebeng mohlala oa bobeli moo index e arohaneng e sebelisoang bakeng sa khase:

fiber = require('fiber')
net_box = require('net.box')
box.cfg{listen = 3300}
box.schema.func.create('libcapped-expirationd.start', {language = 'C'})
box.schema.user.grant('guest', 'execute', 'function', 'libcapped-expirationd.start')
box.schema.func.create('libcapped-expirationd.kill', {language = 'C'})
box.schema.user.grant('guest', 'execute', 'function', 'libcapped-expirationd.kill')
box.schema.space.create('tester')
box.space.tester:create_index('primary', {unique = true, parts = {1, 'unsigned'}})
box.space.tester:create_index('exp', {unique = false, parts = {3, 'unsigned'}})
capped_connection = net_box:new(3300)

Ntho e 'ngoe le e' ngoe mona e tšoana le mohlala oa pele, ntle le mekhelo e seng mekae. Re haha ​​index ea lifate ka holim'a tšimo ea boraro 'me re e bitsa exp. Lenane lena ha lea lokela ho ikhetha, ho fapana le index e bitsoang primary. Traversal e tla etsoa ka exp index, 'me e hlakotsoe ke primary. Re hopola hore pele ka bobeli li ne li etsoa feela ho sebelisoa index ea mantlha.

Ka mor'a mosebetsi oa ho itokisa, re tsamaisa mosebetsi oa ho qala ka likhang tse ncha:

capped_connection:call('libcapped-expirationd.start', {'indexed', box.space.tester.id, box.space.tester.index.primary, box.space.tester.index.exp, 3, 1024, 3600})

Ha re kenyeng likopi tse 'maloa sebakeng hape ka bophelo bohle ba metsotsoana e 60:

box.space.tester:insert{0, '[email protected]', math.floor(fiber.time()) + 60}
box.space.tester:insert{1, '[email protected]', math.floor(fiber.time()) + 60}
box.space.tester:insert{2, '[email protected]', math.floor(fiber.time()) + 60}

Kamora metsotsoana e 30, ka papiso, re tla eketsa tse ling tse 'maloa:

box.space.tester:insert{3, '[email protected]', math.floor(fiber.time()) + 60}
box.space.tester:insert{4, '[email protected]', math.floor(fiber.time()) + 60}
box.space.tester:insert{5, '[email protected]', math.floor(fiber.time()) + 60}

Ha re hlahlobe hore na ho kenya ho atlehile:

tarantool> box.space.tester.index.primary:select()
---
- - [0, '[email protected]', 1576421257]
  - [1, '[email protected]', 1576421257]
  - [2, '[email protected]', 1576421257]
  - [3, '[email protected]', 1576421287]
  - [4, '[email protected]', 1576421287]
  - [5, '[email protected]', 1576421287]
...

Ha re phete khetho ka mor'a metsotsoana e 60+ (ho bala ho tloha qalong ea ho kenngoa ha tuple ea pele) 'me u bone hore mojule o felisitsoeng o se o sebelitse:

tarantool> box.space.tester.index.primary:select()
---
- - [3, '[email protected]', 1576421287]
  - [4, '[email protected]', 1576421287]
  - [5, '[email protected]', 1576421287]
...

Ho ntse ho na le li-tuples tse setseng sebakeng tse tla ba le metsotsoana e meng e 30 ho phela. Ho feta moo, scan ninete e ile ea emisa ha ho tloha tuple e nang le ID ea 2 le bophelo bohle ba 1576421257 ho tuple e nang le ID ea 3 le bophelo bohle ba 1576421287. linotlolo tsa index ea exp. Ena ke chelete eo re neng re batla ho e fumana qalong.

Ha re emise mosebetsi:

capped_connection:call('libcapped-expirationd.kill', {'indexed'})

Ts'ebetsong

Mokhoa o motle oa ho bolela ka likarolo tsohle tsa projeke ke mohloli oa eona oa mantlha. khoutu! E le karolo ea khatiso, re tla tsepamisa maikutlo feela lintlheng tsa bohlokoa ka ho fetisisa, e leng, li-algorithms tsa sebaka sa sebaka.

Likhang tseo re li fetisetsang ho mokhoa oa ho qala li bolokiloe ka sebopeho se bitsoang expirationd_task:

struct expirationd_task
{
  char name[256];
  uint32_t space_id;
  uint32_t rm_index_id;
  uint32_t it_index_id;
  uint32_t it_index_type; 
  uint32_t field_no;
  uint32_t scan_size;
  uint32_t scan_time;
};

Lebitso la tšobotsi ke lebitso la mosebetsi. The space_id tšobotsi ke sekhetho sa sebaka. rm_index_id tšobotsi ke sesupo sa index e ikhethang eo li-tuples li tla hlakoloa ka eona. Sebopeho sa it_index_id ke sesupo sa index eo li-tuples li tla fetisoa ka eona. Sebopeho sa it_index_type ke mofuta oa index eo li-tuple li tla fetisoa ka eona. The filed_no tšobotsi ke palo ea tšimo ea tuple ka bophelo bohle. The scan_size attribute ke palo e phahameng ka ho fetesisa ea li-tuples tse hlahlobiloeng khoebong e le 'ngoe. The scan_time tšobotsi ke nako e felletseng ea ho skena ka metsotsoana.

Ha re na ho nahana ka ho fetisa likhang. Ona ke mosebetsi o boima empa o bonolo, oo laebrari e tla u thusa ka oona msgpuck. Mathata a ka hlaha feela ka li-index tse fetisitsoeng ho tloha Lua e le mokhoa o rarahaneng oa data ka mofuta oa mp_map, le ho se sebelise mefuta e bonolo ea mp_bool, mp_double, mp_int, mp_uint le mp_array. Empa ha ho na tlhoko ea ho bapisa index eohle. U hloka feela ho hlahloba ho ikhetha ha eona, ho bala mofuta le ho ntša sekhetho.

Re thathamisa li-prototype tsa mesebetsi eohle e sebelisetsoang ho arola:

bool expirationd_parse_name(struct expirationd_task *task, const char **pos);
bool expirationd_parse_space_id(struct expirationd_task *task, const char **pos);
bool expirationd_parse_rm_index_id(struct expirationd_task *task, const char **pos);
bool expirationd_parse_rm_index_unique(struct expirationd_task *task, const char **pos);
bool expirationd_parse_rm_index(struct expirationd_task *task, const char **pos);
bool expirationd_parse_it_index_id(struct expirationd_task *task, const char **pos);
bool expirationd_parse_it_index_type(struct expirationd_task *task, const char **pos);
bool expirationd_parse_it_index(struct expirationd_task *task, const char **pos);
bool expirationd_parse_field_no(struct expirationd_task *task, const char **pos);
bool expirationd_parse_scan_size(struct expirationd_task *task, const char **pos);
bool expirationd_parse_scan_time(struct expirationd_task *task, const char **pos);

Joale a re tsoeleng pele ho ntho ea bohlokoa ka ho fetisisa - logic ea ho feta sebaka le ho tlosa li-tuples. Sebaka se seng le se seng sa li-tuples tse seng kholo ho feta scan_size se hlahlojoa 'me se fetotsoe tlas'a transaction e le' ngoe. Haeba e atlehile, khoebo ena e etsoa, ​​​​haeba phoso e etsahala, e khutlisetsoa morao. Khang ea ho qetela ea tšebetso ea expirationd_iterate ke sesupa ho iterator moo ho qala kapa ho tsoelang pele ho hlahloba. Iterator ena e eketsoa ka hare ho fihlela phoso e etsahala, sebaka se fela, kapa ha ho khonehe ho emisa ts'ebetso esale pele. The function expirationd_expired e lekola bophelo ba tuple, expirationd_delete e hlakola tuple, expirationd_breakable e hlahloba hore na re hloka ho tsoela pele.

Expirationd_terate code code:

static bool
expirationd_iterate(struct expirationd_task *task, box_iterator_t **iterp)
{
  box_iterator_t *iter = *iterp;
  box_txn_begin();
  for (uint32_t i = 0; i < task->scan_size; ++i) {
    box_tuple_t *tuple = NULL;
    if (box_iterator_next(iter, &tuple) < 0) {
      box_iterator_free(iter);
      *iterp = NULL;
      box_txn_rollback();
      return false;
    }
    if (!tuple) {
      box_iterator_free(iter);
      *iterp = NULL;
      box_txn_commit();
      return true;
    }
    if (expirationd_expired(task, tuple))
      expirationd_delete(task, tuple);
    else if (expirationd_breakable(task))
      break;
  }
  box_txn_commit();
  return true;
}

Khouto ea tšebetso expiration_expired:

static bool
expirationd_expired(struct expirationd_task *task, box_tuple_t *tuple)
{
  const char *buf = box_tuple_field(tuple, task->field_no - 1);
  if (!buf || mp_typeof(*buf) != MP_UINT)
    return false;
  uint64_t val = mp_decode_uint(&buf);
  if (val > fiber_time64() / 1000000)
    return false;
  return true;
}

Expirationd_delete khoutu ya tshebetso:

static void
expirationd_delete(struct expirationd_task *task, box_tuple_t *tuple)
{
  uint32_t len;
  const char *str = box_tuple_extract_key(tuple, task->space_id, task->rm_index_id, &len);
  box_delete(task->space_id, task->rm_index_id, str, str + len, NULL);
}

Khoutu ea tšebetso expiration_breakable:

static bool
expirationd_breakable(struct expirationd_task *task)
{
  return task->it_index_id != task->rm_index_id && task->it_index_type == ITER_GT;
}

Kopo

U ka sheba khoutu ea mohloli ho mona!

Source: www.habr.com

Eketsa ka tlhaloso