From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 8AFFA6ECC0; Thu, 16 Dec 2021 14:23:01 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 8AFFA6ECC0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1639653781; bh=beccCqD9Mz56fFbVkOqQp1TJN8vB1xQ0agLWAMlka+s=; h=To:Cc:Date:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=usShXsIQ2dKorAax3+4wOCCu+YeRUK+/iOTHqKh8h9zjB0LWEY5dY/nM7JCxajpx3 ob6f0bgmenwvqj98Tt9p9PV8pd+3xihWPFHe/NndZD9P+VWZdZ7o1BS6Dg+uLu9bES 9s0JqpSUlXslLslbQ5+Q0acvLYFNdIpRFzaZL8/0= Received: from mail-lf1-f47.google.com (mail-lf1-f47.google.com [209.85.167.47]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 3F3516ECC0 for ; Thu, 16 Dec 2021 14:23:00 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3F3516ECC0 Received: by mail-lf1-f47.google.com with SMTP id b40so48988436lfv.10 for ; Thu, 16 Dec 2021 03:23:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=spPxYX7RnpYkW9YKULJlI7H300G7dQIU+Qae6PmJwDg=; b=AkYJHcVt0dzkg7YEZJINfpg1x+vHAqM3nokuCk7yrqV0sztSvQSJu62PenZPX7h8Kc VHV6RU8uv070h4PGnvDaKJw8+FVDtEBqf5rxPW5hEQwWbpKwSDRBYTR3q6QgfO5y07CE 6nD+q+xSpVTPbyj3BoTdIFw6oRq8FgUehoyUyPX6fQ4nKtgbOulmE/R5fmblkMfRBlVj J83deviDwTq8WrIiDNrAuLdc1g3kVYUAdvfPiaOA0M5VIF+67G0RYr4woRDGuANbmG3b w3N4Bhi0GN43q50aco2QmmIFFz6z9+GMpsauTvusZTi+yYlMoJtIn/1nqHcrH7GLtdDx l10A== X-Gm-Message-State: AOAM530KjkHAj0zZAQC50E3MI1TLZeT1MUmXQuEmu53JDIlgM3DVe1x+ AwsQPKMSQcBySnN+uJ46AD8= X-Google-Smtp-Source: ABdhPJyM4/TDUZeUmVd7q8Nim3ZnO1OUgtGXyFuqsgQVwldXzkDBPDnaIWa6YxMaePAn2uRQTzFdVQ== X-Received: by 2002:a05:6512:2038:: with SMTP id s24mr3545527lfs.319.1639653779619; Thu, 16 Dec 2021 03:22:59 -0800 (PST) Received: from localhost.localdomain (broadband-46-242-12-18.ip.moscow.rt.ru. [46.242.12.18]) by smtp.gmail.com with ESMTPSA id k5sm1072846lja.34.2021.12.16.03.22.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Dec 2021 03:22:59 -0800 (PST) To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org, Yan Shtunder Date: Thu, 16 Dec 2021 14:22:57 +0300 Message-Id: <20211216112257.81742-1-ya.shtunder@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH] net.box: add predefined system events for pub/sub X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Yan Shtunder via Tarantool-patches Reply-To: Yan Shtunder Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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