From: Yan Shtunder via Tarantool-patches <tarantool-patches@dev.tarantool.org> To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org, Yan Shtunder <ya.shtunder@gmail.com> Subject: [Tarantool-patches] [PATCH] net.box: add predefined system events for pub/sub Date: Thu, 16 Dec 2021 14:22:57 +0300 [thread overview] Message-ID: <20211216112257.81742-1-ya.shtunder@gmail.com> (raw) Adding predefined system event box.status --- Issue: https://github.com/tarantool/tarantool/issues/6260 Patch: https://github.com/tarantool/tarantool/tree/yshtunder/gh-6260-add-predefined-system-events src/box/box.cc | 41 +++++++++++++ .../gh_6260_add_predefined_events_test.lua | 61 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 test/app-luatest/gh_6260_add_predefined_events_test.lua diff --git a/src/box/box.cc b/src/box/box.cc index 0413cbf44..8529e6925 100644 --- a/src/box/box.cc +++ b/src/box/box.cc @@ -160,6 +160,9 @@ static struct fiber_pool tx_fiber_pool; */ static struct cbus_endpoint tx_prio_endpoint; +void +box_status_broadcast(void); + void box_update_ro_summary(void) { @@ -173,6 +176,8 @@ box_update_ro_summary(void) if (is_ro_summary) engine_switch_to_ro(); fiber_cond_broadcast(&ro_cond); + /* Checking box.info.ro change */ + box_status_broadcast(); } const char * @@ -377,9 +382,11 @@ box_set_orphan(bool orphan) if (is_orphan) { say_crit("entering orphan mode"); title("orphan"); + box_status_broadcast(); } else { say_crit("leaving orphan mode"); title("running"); + box_status_broadcast(); } } @@ -3508,6 +3515,7 @@ local_recovery(const struct tt_uuid *instance_uuid, */ if (wal_dir_lock < 0) { title("hot_standby"); + box_status_broadcast(); say_info("Entering hot standby mode"); engine_begin_hot_standby_xc(); recovery_follow_local(recovery, &wal_stream.base, "hot_standby", @@ -3643,6 +3651,7 @@ box_cfg_xc(void) } title("loading"); + box_status_broadcast(); struct tt_uuid instance_uuid, replicaset_uuid; box_check_instance_uuid(&instance_uuid); @@ -3766,6 +3775,7 @@ box_cfg_xc(void) diag_raise(); title("running"); + box_status_broadcast(); say_info("ready to accept requests"); if (!is_bootstrap_leader) { @@ -3800,6 +3810,8 @@ box_cfg_xc(void) assert(box_is_ro()); /* If anyone is waiting for ro mode. */ fiber_cond_broadcast(&ro_cond); + /* Checking box.cfg.read_only change */ + box_status_broadcast(); /* * Yield to let ro condition waiters to handle the event. * Without yield it may happen there won't be a context @@ -3903,3 +3915,32 @@ box_reset_stat(void) engine_reset_stat(); space_foreach(box_reset_space_stat, NULL); } + +void +box_status_broadcast(void) +{ + size_t size = 0; + + size += mp_sizeof_map(3); + size += mp_sizeof_str(strlen("is_ro")); + size += mp_sizeof_bool(box_is_ro()); + size += mp_sizeof_str(strlen("is_ro_cfg")); + size += mp_sizeof_bool(cfg_geti("read_only")); + size += mp_sizeof_str(strlen("status")); + size += mp_sizeof_str(strlen(box_status())); + + char buf[size]; + char *w = buf; + + w = mp_encode_map(w, 3); + w = mp_encode_str(w, "is_ro", strlen("is_ro")); + w = mp_encode_bool(w, box_is_ro()); + w = mp_encode_str(w, "is_ro_cfg", strlen("is_ro_cfg")); + w = mp_encode_bool(w, cfg_geti("read_only")); + w = mp_encode_str(w, "status", strlen("status")); + w = mp_encode_str(w, box_status(), strlen(box_status())); + + box_broadcast("box.status", strlen("box.status"), buf, w); + + assert(size == (size_t)(w - buf)); +} diff --git a/test/app-luatest/gh_6260_add_predefined_events_test.lua b/test/app-luatest/gh_6260_add_predefined_events_test.lua new file mode 100644 index 000000000..e6ff0e94e --- /dev/null +++ b/test/app-luatest/gh_6260_add_predefined_events_test.lua @@ -0,0 +1,61 @@ +local t = require('luatest') +local cluster = require('test.luatest_helpers.cluster') +local helpers = require('test.luatest_helpers') + +local g = t.group('gh_6260', {{engine = 'memtx'}, {engine = 'vinyl'}}) + +g.before_each(function(cg) + local engine = cg.params.engine + + cg.cluster = cluster:new({}) + + local box_cfg = { + replication = { + helpers.instance_uri('master') + }, + replication_timeout = 1, + read_only = false + } + + cg.master = cg.cluster:build_server({alias = 'master', engine = engine, box_cfg = box_cfg}) + cg.cluster:add_server(cg.master) + cg.cluster:start() +end) + + +g.after_each(function(cg) + cg.cluster.servers = nil + cg.cluster:drop() +end) + + +g.test_box_status_event = function(cg) + cg.master:eval([[ + i = '' + box.watch('box.status', function(...) i = {...} end) + ]]) + t.assert_equals(cg.master:eval("return i"), {"box.status", {is_ro = false, is_ro_cfg = false, status = "running"}}) + + cg.master:eval(([[ + box.cfg{ + replication = { + "%s", + "%s" + }, + replication_connect_timeout = 0.001, + } + ]]):format(helpers.instance_uri('master'), helpers.instance_uri('replica'))) + t.assert_equals(cg.master:eval("return i"), {"box.status", {is_ro = true, is_ro_cfg = false, status = "orphan"}}) + + cg.master:eval(([[ + box.cfg{ + replication = { + "%s", + }, + } + ]]):format(helpers.instance_uri('master'))) + t.assert_equals(cg.master:eval("return i"), {"box.status", {is_ro = false, is_ro_cfg = false, status = "running"}}) + + cg.master:eval("box.cfg{read_only = true}") + t.assert_equals(cg.master:eval("return i"), {"box.status", {is_ro = true, is_ro_cfg = true, status = "running"}}) +end -- 2.25.1
next reply other threads:[~2021-12-16 11:23 UTC|newest] Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-12-16 11:22 Yan Shtunder via Tarantool-patches [this message] 2021-12-17 23:31 ` Vladislav Shpilevoy via Tarantool-patches
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20211216112257.81742-1-ya.shtunder@gmail.com \ --to=tarantool-patches@dev.tarantool.org \ --cc=v.shpilevoy@tarantool.org \ --cc=ya.shtunder@gmail.com \ --subject='Re: [Tarantool-patches] [PATCH] net.box: add predefined system events for pub/sub' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox