On 11/26/26 4:14 PM, Dumitru Ceara via dev wrote:
> An incremental processing graph needs independent run flags, counters,
> timers and runtime settings. Store them on an engine and pass that
> engine through the graph operations in all users of the library.
>
> Register graphs by name so the existing unixctl commands can select an
> engine when several are present. Keep implicit selection for a sole
> registered engine. Give each registration its own stopwatch keys
> because the OVS stopwatch records can outlast the graph.
>
> Exercise the commands and processing state with two graphs in one
> process, including cleanup of one graph while the other keeps running.
>
> Assisted-by: GPT-6.2-Sol, Codex
> Signed-off-by: Dumitru Ceara <[email protected]>
> ---
> .../incremental-processing.rst | 12 +-
> NEWS | 3 +
> br-controller/ovn-br-controller.8.xml | 9 +
> br-controller/ovn-br-controller.c | 15 +-
> controller/ovn-controller.8.xml | 9 +
> controller/ovn-controller.c | 44 +-
> ic/inc-proc-ic.c | 37 +-
> ic/inc-proc-ic.h | 26 +-
> ic/ovn-ic.8.xml | 9 +
> ic/ovn-ic.c | 44 +-
> lib/inc-proc-eng.c | 436 +++++++++++-------
> lib/inc-proc-eng.h | 56 ++-
> northd/inc-proc-northd.c | 33 +-
> northd/inc-proc-northd.h | 22 +-
> northd/ovn-northd.8.xml | 9 +
> northd/ovn-northd.c | 24 +-
> tests/automake.mk | 2 +
> tests/ovn-inc-proc-eng.at | 150 ++++++
> tests/test-inc-proc-eng.c | 185 ++++++++
> tests/testsuite.at | 1 +
> 20 files changed, 822 insertions(+), 304 deletions(-)
> create mode 100644 tests/ovn-inc-proc-eng.at
> create mode 100644 tests/test-inc-proc-eng.c
>
> diff --git
> a/Documentation/topics/incremental-processing/incremental-processing.rst
> b/Documentation/topics/incremental-processing/incremental-processing.rst
> index 7890594495..313c389d87 100644
> --- a/Documentation/topics/incremental-processing/incremental-processing.rst
> +++ b/Documentation/topics/incremental-processing/incremental-processing.rst
> @@ -268,8 +268,12 @@ Runtime Introspection
>
> `ovn-northd`, `ovn-controller`, `ovn-ic` and `ovn-br-controller` all register
> the same set of `ovn-appctl`/`ovs-appctl` commands for inspecting and
> -controlling the engine at runtime:
> +controlling the engine at runtime. Except for `inc-engine/list-engines`,
> +these commands accept `--engine=NAME` before their positional arguments.
> +The selector is required with multiple registered engines. With one
> +registered engine, omitting the selector selects that engine.
>
> +- `inc-engine/list-engines`: List the names of the registered engines.
> - `inc-engine/show-stats [node] [counter]`: Display the per-node
> `recompute`, `compute` and `cancel` counters. If `node` is given, only
> that node's counters are shown; `counter` further restricts the output to
> @@ -299,6 +303,7 @@ The engine core is implemented in `lib/inc-proc-eng.c` and
> Key Structures
> ~~~~~~~~~~~~~~
>
> +- `struct engine`: Holds the graph and its processing state.
> - `struct engine_node`: The base structure for a node. Contains inputs, data,
> state, callback functions (`run`, `init`, `cleanup`, `is_valid`,
> `clear_tracked_data`, `get_compute_failure_info`), the per-node `stats`
> @@ -313,6 +318,11 @@ Key Structures
> `controller_engine_ctx`) through to `run()` methods and change handlers.
> Accessed with `engine_get_context()`.
>
> +Call `engine_infra_init()` once in the main thread during startup to
> +install the process-wide unixctl commands. `engine_init()` registers the
> +named graph and creates its stopwatches. `engine_cleanup()` withdraws the
> +graph from runtime control.
> +
> Usage Guidelines
> ----------------
>
> diff --git a/NEWS b/NEWS
> index aeba954afa..e62bae22c5 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -1,5 +1,8 @@
> Post v26.09.0
> --------------
> + - Incremental processing commands in ovn-appctl can select an engine
> + by name with --engine=NAME. Use inc-engine/list-engines to discover
> + names.
> - Incremental processing stopwatches now use compact keys.
> Use inc-engine/list-stopwatches to find the keys for show and reset.
> - ovn-nbctl lr-policy-del, when not passed a --chain value, can now delete
> diff --git a/br-controller/ovn-br-controller.8.xml
> b/br-controller/ovn-br-controller.8.xml
> index 77370d977b..69272259e9 100644
> --- a/br-controller/ovn-br-controller.8.xml
> +++ b/br-controller/ovn-br-controller.8.xml
> @@ -83,6 +83,15 @@
> <code>ovn-br-controller</code> process. The currently supported
> commands are described below.
> <dl>
> + <dt><code>inc-engine/list-engines</code></dt>
> + <dd>
> + Lists the names of the registered incremental processing engines.
> + The other <code>inc-engine/</code> commands accept
> + <code>--engine=<var>name</var></code> before their positional
> + arguments. The selector is optional with one registered engine
> + and required with multiple registered engines.
> + </dd>
> +
> <dt><code>inc-engine/show-stats</code></dt>
> <dd>
> Display <code>ovn-br-controller</code> engine counters. For each
> diff --git a/br-controller/ovn-br-controller.c
> b/br-controller/ovn-br-controller.c
> index ca6db7db7c..e876833ddb 100644
> --- a/br-controller/ovn-br-controller.c
> +++ b/br-controller/ovn-br-controller.c
> @@ -142,6 +142,7 @@ main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
> if (retval) {
> exit(EXIT_FAILURE);
> }
> + engine_infra_init();
> unixctl_command_register("exit", "", 0, 1, ovn_exit_command_callback,
> &exit_args);
>
> @@ -215,7 +216,9 @@ main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
> .ovs_idl = ovs_idl_loop.idl,
> .ovnbr_idl = ovnbr_idl_loop.idl,
> };
> - engine_init(&en_br_controller_output, &engine_arg);
> + struct engine engine;
> + engine_init(&engine, "ovn-br-controller-i-p", &en_br_controller_output,
> + &engine_arg);
> engine_ovsdb_node_add_index(&en_ovs_bridge, "name",
> ovsrec_bridge_by_name);
>
> unsigned int ovnbr_expected_cond_seqno = UINT_MAX;
> @@ -227,7 +230,7 @@ main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
>
> /* Main loop. */
> while (!exit_args.exiting) {
> - engine_init_run();
> + engine_init_run(&engine);
>
> struct ovsdb_idl_txn *ovs_idl_txn =
> ovsdb_idl_loop_run(&ovs_idl_loop);
> unsigned int new_ovs_cond_seqno
> @@ -235,7 +238,7 @@ main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
> if (new_ovs_cond_seqno != ovs_cond_seqno) {
> if (!new_ovs_cond_seqno) {
> VLOG_INFO("OVS IDL reconnected, force recompute.");
> - engine_set_force_recompute();
> + engine_set_force_recompute(&engine);
> }
> ovs_cond_seqno = new_ovs_cond_seqno;
> }
> @@ -249,7 +252,7 @@ main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
> if (new_ovnbr_cond_seqno != ovnbr_cond_seqno) {
> if (!new_ovnbr_cond_seqno) {
> VLOG_INFO("OVNBR IDL reconnected, force recompute.");
> - engine_set_force_recompute();
> + engine_set_force_recompute(&engine);
> }
> ovnbr_cond_seqno = new_ovnbr_cond_seqno;
> }
> @@ -267,7 +270,7 @@ main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
> ovsrec_open_vswitch_table_first(ovs_table);
>
> if (ovsdb_idl_has_ever_connected(ovnbr_idl_loop.idl) && cfg) {
> - engine_run(true);
> + engine_run(&engine, true);
>
> br_data = engine_get_data(&en_bridge_data);
> if (br_data) {
> @@ -324,7 +327,7 @@ main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
> }
>
> engine_set_context(NULL);
> - engine_cleanup();
> + engine_cleanup(&engine);
>
> free(ovs_remote);
> ovn_exit_args_finish(&exit_args);
> diff --git a/controller/ovn-controller.8.xml b/controller/ovn-controller.8.xml
> index f1abfb4c5d..4c28fd901e 100644
> --- a/controller/ovn-controller.8.xml
> +++ b/controller/ovn-controller.8.xml
> @@ -1112,6 +1112,15 @@
> <code>lflow-cache/flush</code> instead.
> </dd>
>
> + <dt><code>inc-engine/list-engines</code></dt>
> + <dd>
> + Lists the names of the registered incremental processing engines.
> + The other <code>inc-engine/</code> commands accept
> + <code>--engine=<var>name</var></code> before their positional
> + arguments. The selector is optional with one registered engine
> + and required with multiple registered engines.
> + </dd>
> +
> <dt><code>inc-engine/show-stats</code></dt>
> <dd>
> Displays the following <code>ovn-controller</code> counters for each
> diff --git a/controller/ovn-controller.c b/controller/ovn-controller.c
> index 2f98003896..812995ec87 100644
> --- a/controller/ovn-controller.c
> +++ b/controller/ovn-controller.c
> @@ -147,6 +147,8 @@ static unixctl_cb_func debug_delay_nb_cfg_report;
> #define OVS_NB_CFG_SB_TS_NAME "ovn-nb-cfg-sb-ts"
> #define OVS_STARTUP_TS_NAME "ovn-startup-ts"
>
> +static struct engine engine;
> +
> struct br_int_remote {
> char *target;
> int probe_interval;
> @@ -797,7 +799,7 @@ update_sb_db(struct ovsdb_idl *ovs_idl, struct ovsdb_idl
> *ovnsb_idl,
> }
> if (reset_ovnsb_idl_min_index && *reset_ovnsb_idl_min_index) {
> VLOG_INFO("Resetting southbound database cluster state");
> - engine_set_force_recompute();
> + engine_set_force_recompute(&engine);
> ovsdb_idl_reset_min_index(ovnsb_idl);
> *reset_ovnsb_idl_min_index = false;
> }
> @@ -8197,7 +8199,8 @@ inc_proc_ovn_controller_init(
> .sb_idl = sb_idl_loop->idl,
> .ovs_idl = ovs_idl_loop->idl,
> };
> - engine_init(&en_controller_output, &engine_arg);
> + engine_init(&engine, "ovn-controller-i-p", &en_controller_output,
> + &engine_arg);
>
> engine_ovsdb_node_add_index(&en_sb_chassis, "name",
> sbrec_chassis_by_name);
> struct ovsdb_idl_index *sbrec_multicast_group_by_name_datapath
> @@ -8319,7 +8322,7 @@ check_northd_version(struct ovsdb_idl *ovs_idl, struct
> ovsdb_idl *ovnsb_idl,
> * full recompute.
> */
> if (version_mismatch) {
> - engine_set_force_recompute();
> + engine_set_force_recompute(&engine);
> }
> version_mismatch = false;
> return true;
> @@ -8390,6 +8393,7 @@ main(int argc, char *argv[])
> if (retval) {
> exit(EXIT_FAILURE);
> }
> + engine_infra_init();
> unixctl_command_register("exit", "", 0, 1, ovn_exit_command_callback,
> &exit_args);
>
> @@ -8733,7 +8737,7 @@ main(int argc, char *argv[])
> goto loop_done;
> }
>
> - engine_init_run();
> + engine_init_run(&engine);
>
> struct ovsdb_idl_txn *ovs_idl_txn =
> ovsdb_idl_loop_run(&ovs_idl_loop);
> unsigned int new_ovs_cond_seqno
> @@ -8741,7 +8745,7 @@ main(int argc, char *argv[])
> if (new_ovs_cond_seqno != ovs_cond_seqno) {
> if (!new_ovs_cond_seqno) {
> VLOG_INFO("OVS IDL reconnected, force recompute.");
> - engine_set_force_recompute();
> + engine_set_force_recompute(&engine);
> }
> ovs_cond_seqno = new_ovs_cond_seqno;
> }
> @@ -8761,7 +8765,7 @@ main(int argc, char *argv[])
> if (new_ovnsb_cond_seqno != ovnsb_cond_seqno) {
> if (!new_ovnsb_cond_seqno) {
> VLOG_INFO("OVNSB IDL reconnected, force recompute.");
> - engine_set_force_recompute();
> + engine_set_force_recompute(&engine);
> /* The monitor conditions are re-applied on reconnect, so the
> * previously scoped Advertised_Route condition no longer
> * holds. */
> @@ -8893,7 +8897,7 @@ main(int argc, char *argv[])
> br_int_remote.target,
> br_int_remote.probe_interval)) {
> VLOG_INFO("OVS feature set changed, force recompute.");
> - engine_set_force_recompute();
> + engine_set_force_recompute(&engine);
>
> struct ed_type_lflow_output *lflow_out_data =
> engine_get_internal_data(&en_lflow_output);
> @@ -8916,7 +8920,7 @@ main(int argc, char *argv[])
>
> VLOG_INFO_RL(&rl, "OVS OpenFlow connection reconnected,"
> "force recompute.");
> - engine_set_force_recompute();
> + engine_set_force_recompute(&engine);
> }
>
> const struct sbrec_sb_global *sbg =
> @@ -8990,7 +8994,7 @@ main(int argc, char *argv[])
>
> bool recompute_allowed = (ovnsb_idl_txn &&
> !ofctrl_has_backlog());
> - engine_run(recompute_allowed);
> + engine_run(&engine, recompute_allowed);
> tracked_acl_ids = engine_get_data(&en_acl_id);
>
> stopwatch_stop(CONTROLLER_LOOP_STOPWATCH_NAME,
> @@ -9240,22 +9244,22 @@ main(int argc, char *argv[])
> }
> }
>
> - if (!engine_has_run()) {
> - if (engine_need_run()) {
> + if (!engine_has_run(&engine)) {
> + if (engine_need_run(&engine)) {
> VLOG_DBG("engine did not run, force recompute next time:
> "
> "br_int %p, chassis %p", br_int, chassis);
> - engine_set_force_recompute_immediate();
> + engine_set_force_recompute_immediate(&engine);
> } else {
> VLOG_DBG("engine did not run, and it was not needed"
> " either: br_int %p, chassis %p",
> br_int, chassis);
> }
> - } else if (engine_canceled()) {
> + } else if (engine_canceled(&engine)) {
> VLOG_DBG("engine was canceled, force recompute next time: "
> "br_int %p, chassis %p", br_int, chassis);
> - engine_set_force_recompute_immediate();
> + engine_set_force_recompute_immediate(&engine);
> } else {
> - engine_clear_force_recompute();
> + engine_clear_force_recompute(&engine);
> }
>
> struct ed_type_northd_options *n_opts =
> @@ -9314,7 +9318,7 @@ main(int argc, char *argv[])
> ovnsb_txn_status = ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
> if (!ovnsb_txn_status) {
> VLOG_INFO("OVNSB commit failed, force recompute next time.");
> - engine_set_force_recompute_immediate();
> + engine_set_force_recompute_immediate(&engine);
> } else if (ovnsb_txn_status == 1) {
> ovsdb_idl_loop_next_cfg_inc(&ovnsb_idl_loop);
> } else if (ovnsb_txn_status == -1) {
> @@ -9438,7 +9442,7 @@ loop_done:
> statctrl_destroy();
>
> engine_set_context(NULL);
> - engine_cleanup();
> + engine_cleanup(&engine);
> ovn_extend_table_destroy(&group_table);
>
> free(ovn_version);
> @@ -9483,7 +9487,7 @@ inc_proc_graph_dump(const char *end_node)
> inc_proc_ovn_controller_init(&ovnsb_idl_loop, &ovs_idl_loop,
> NULL, NULL, NULL, NULL, NULL,
> NULL, NULL, NULL, NULL);
> - engine_dump_graph(end_node);
> + engine_dump_graph(&engine, end_node);
>
> ovsdb_idl_loop_destroy(&ovs_idl_loop);
> ovsdb_idl_loop_destroy(&ovnsb_idl_loop);
> @@ -9701,7 +9705,7 @@ static void
> engine_recompute_cmd(struct unixctl_conn *conn OVS_UNUSED, int argc
> OVS_UNUSED,
> const char *argv[] OVS_UNUSED, void *arg OVS_UNUSED)
> {
> - engine_trigger_recompute();
> + engine_trigger_recompute(&engine);
> unixctl_command_reply(conn, NULL);
> }
>
> @@ -9713,7 +9717,7 @@ lflow_cache_flush_cmd(struct unixctl_conn *conn
> OVS_UNUSED,
> VLOG_INFO("User triggered lflow cache flush.");
> struct lflow_output_persistent_data *fo_pd = arg_;
> lflow_cache_flush(fo_pd->lflow_cache);
> - engine_set_force_recompute_immediate();
> + engine_set_force_recompute_immediate(&engine);
> unixctl_command_reply(conn, NULL);
> }
>
> diff --git a/ic/inc-proc-ic.c b/ic/inc-proc-ic.c
> index af0060ae56..507b5c37c9 100644
> --- a/ic/inc-proc-ic.c
> +++ b/ic/inc-proc-ic.c
> @@ -42,6 +42,8 @@
>
> VLOG_DEFINE_THIS_MODULE(inc_proc_ic);
>
> +static struct engine engine;
> +
> #define NB_NODES \
> NB_NODE(nb_global, "nb_global") \
> NB_NODE(logical_router_static_route, "logical_router_static_route") \
> @@ -188,10 +190,11 @@ static ENGINE_NODE(service_monitor);
> static ENGINE_NODE(address_set);
> static ENGINE_NODE(ic);
>
> -void inc_proc_ic_init(struct ovsdb_idl_loop *nb,
> - struct ovsdb_idl_loop *sb,
> - struct ovsdb_idl_loop *icnb,
> - struct ovsdb_idl_loop *icsb)
> +struct engine *
> +inc_proc_ic_init(struct ovsdb_idl_loop *nb,
> + struct ovsdb_idl_loop *sb,
> + struct ovsdb_idl_loop *icnb,
> + struct ovsdb_idl_loop *icsb)
> {
> /* Define relationships between nodes where the first argument is
> dependent
> * on the second argument.
> @@ -406,7 +409,8 @@ void inc_proc_ic_init(struct ovsdb_idl_loop *nb,
> .icsb_idl = icsb->idl,
> };
>
> - engine_init(&en_ic, &engine_arg);
> + engine_init(&engine, "ovn-ic-i-p", &en_ic, &engine_arg);
> + return &engine;
> }
>
> /* Returns true if the incremental processing ended up updating nodes. */
> @@ -418,27 +422,27 @@ inc_proc_ic_run(struct ic_context *ctx,
> ctx->ovninb_txn && ctx->ovnisb_unlocked_txn);
>
> int64_t start = time_msec();
> - engine_init_run();
> + engine_init_run(&engine);
>
> struct engine_context eng_ctx = {
> .client_ctx = ctx,
> };
>
> engine_set_context(&eng_ctx);
> - engine_run(true);
> + engine_run(&engine, true);
>
> - if (!engine_has_run()) {
> - if (engine_need_run()) {
> + if (!engine_has_run(&engine)) {
> + if (engine_need_run(&engine)) {
> VLOG_DBG("engine did not run, force recompute next time.");
> - engine_set_force_recompute_immediate();
> + engine_set_force_recompute_immediate(&engine);
> } else {
> VLOG_DBG("engine did not run, and it was not needed");
> }
> - } else if (engine_canceled()) {
> + } else if (engine_canceled(&engine)) {
> VLOG_DBG("engine was canceled, force recompute next time.");
> - engine_set_force_recompute_immediate();
> + engine_set_force_recompute_immediate(&engine);
> } else {
> - engine_clear_force_recompute();
> + engine_clear_force_recompute(&engine);
> }
>
> int64_t now = time_msec();
> @@ -446,13 +450,13 @@ inc_proc_ic_run(struct ic_context *ctx,
> * by "ic-backoff-interval-ms" interval. */
> ic_eng_ctx->next_run_ms = now + MIN(now - start, ic_eng_ctx->backoff_ms);
>
> - return engine_has_updated();
> + return engine_has_updated(&engine);
> }
>
> void
> inc_proc_ic_cleanup(void)
> {
> - engine_cleanup();
> + engine_cleanup(&engine);
> engine_set_context(NULL);
> }
>
> @@ -466,7 +470,8 @@ inc_proc_ic_get_runned_az(void)
> bool
> inc_proc_ic_can_run(struct ic_engine_context *ctx)
> {
> - if (engine_get_force_recompute() || time_msec() >= ctx->next_run_ms ||
> + if (engine_get_force_recompute(&engine) ||
> + time_msec() >= ctx->next_run_ms ||
> ctx->nb_idl_duration_ms >= IDL_LOOP_MAX_DURATION_MS ||
> ctx->sb_idl_duration_ms >= IDL_LOOP_MAX_DURATION_MS ||
> ctx->inb_idl_duration_ms >= IDL_LOOP_MAX_DURATION_MS ||
> diff --git a/ic/inc-proc-ic.h b/ic/inc-proc-ic.h
> index 67565a0145..d138873895 100644
> --- a/ic/inc-proc-ic.h
> +++ b/ic/inc-proc-ic.h
> @@ -29,10 +29,10 @@ struct ic_engine_context {
> uint32_t backoff_ms;
> };
>
> -void inc_proc_ic_init(struct ovsdb_idl_loop *nb,
> - struct ovsdb_idl_loop *sb,
> - struct ovsdb_idl_loop *icnb,
> - struct ovsdb_idl_loop *icsb);
> +struct engine *inc_proc_ic_init(struct ovsdb_idl_loop *nb,
> + struct ovsdb_idl_loop *sb,
> + struct ovsdb_idl_loop *icnb,
> + struct ovsdb_idl_loop *icsb);
>
> bool inc_proc_ic_run(struct ic_context *ctx,
> struct ic_engine_context *ic_eng_ctx);
> @@ -47,22 +47,4 @@ struct icsbrec_availability_zone;
> * loop, which runs the AZ sequence-number bookkeeping outside the engine. */
> const struct icsbrec_availability_zone *inc_proc_ic_get_runned_az(void);
>
> -static inline void
> -inc_proc_ic_force_recompute(void)
> -{
> - engine_set_force_recompute();
> -}
> -
> -static inline void
> -inc_proc_ic_force_recompute_immediate(void)
> -{
> - engine_set_force_recompute_immediate();
> -}
> -
> -static inline bool
> -inc_proc_ic_get_force_recompute(void)
> -{
> - return engine_get_force_recompute();
> -}
> -
> #endif /* INC_PROC_IC */
> diff --git a/ic/ovn-ic.8.xml b/ic/ovn-ic.8.xml
> index 7e83d245cc..9fe86502e1 100644
> --- a/ic/ovn-ic.8.xml
> +++ b/ic/ovn-ic.8.xml
> @@ -115,6 +115,15 @@
> this instance is paused.
> </dd>
>
> + <dt><code>inc-engine/list-engines</code></dt>
> + <dd>
> + Lists the names of the registered incremental processing engines.
> + The other <code>inc-engine/</code> commands accept
> + <code>--engine=<var>name</var></code> before their positional
> + arguments. The selector is optional with one registered engine
> + and required with multiple registered engines.
> + </dd>
> +
> <dt><code>inc-engine/show-stats</code></dt>
> <dd>
> Display <code>ovn-ic</code> engine counters. For each engine
> diff --git a/ic/ovn-ic.c b/ic/ovn-ic.c
> index 87a266b44d..67f0862d58 100644
> --- a/ic/ovn-ic.c
> +++ b/ic/ovn-ic.c
> @@ -28,6 +28,7 @@
> #include "fatal-signal.h"
> #include "hash.h"
> #include "openvswitch/hmap.h"
> +#include "lib/inc-proc-eng.h"
> #include "lib/ovn-ic-nb-idl.h"
> #include "lib/ovn-ic-sb-idl.h"
> #include "lib/ovn-nb-idl.h"
> @@ -694,9 +695,10 @@ inc_proc_graph_dump(const char *end_node)
> struct ovsdb_idl_loop ovnisb_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
> ovsdb_idl_create_unconnected(&icsbrec_idl_class, true));
>
> - inc_proc_ic_init(&ovnnb_idl_loop, &ovnsb_idl_loop,
> - &ovninb_idl_loop, &ovnisb_idl_loop);
> - engine_dump_graph(end_node);
> + struct engine *engine =
> + inc_proc_ic_init(&ovnnb_idl_loop, &ovnsb_idl_loop,
> + &ovninb_idl_loop, &ovnisb_idl_loop);
> + engine_dump_graph(engine, end_node);
>
> ovsdb_idl_loop_destroy(&ovnnb_idl_loop);
> ovsdb_idl_loop_destroy(&ovnsb_idl_loop);
> @@ -905,6 +907,7 @@ main(int argc, char *argv[])
> if (retval) {
> exit(EXIT_FAILURE);
> }
> + engine_infra_init();
> unixctl_command_register("exit", "", 0, 0, ovn_ic_exit, &exiting);
> unixctl_command_register("pause", "", 0, 0, ovn_ic_pause, &state);
> unixctl_command_register("resume", "", 0, 0, ovn_ic_resume, &state);
> @@ -1239,9 +1242,10 @@ main(int argc, char *argv[])
> unixctl_command_register("ic-sb-connection-status", "", 0, 0,
> ovn_conn_show, ovnisb_idl_loop.idl);
>
> - /* Initialize incremental processing engine for ovn-northd */
> - inc_proc_ic_init(&ovnnb_idl_loop, &ovnsb_idl_loop,
> - &ovninb_idl_loop, &ovnisb_unlocked_idl_loop);
> + /* Initialize the incremental processing engine. */
> + struct engine *engine =
> + inc_proc_ic_init(&ovnnb_idl_loop, &ovnsb_idl_loop,
> + &ovninb_idl_loop, &ovnisb_unlocked_idl_loop);
>
> unsigned int ovnnb_cond_seqno = UINT_MAX;
> unsigned int ovnsb_cond_seqno = UINT_MAX;
> @@ -1303,7 +1307,7 @@ main(int argc, char *argv[])
> if (new_ovnnb_cond_seqno != ovnnb_cond_seqno) {
> if (!new_ovnnb_cond_seqno) {
> VLOG_INFO("OVN NB IDL reconnected, force recompute.");
> - inc_proc_ic_force_recompute();
> + engine_set_force_recompute(engine);
> }
> ovnnb_cond_seqno = new_ovnnb_cond_seqno;
> }
> @@ -1316,7 +1320,7 @@ main(int argc, char *argv[])
> if (new_ovnsb_cond_seqno != ovnsb_cond_seqno) {
> if (!new_ovnsb_cond_seqno) {
> VLOG_INFO("OVN SB IDL reconnected, force recompute.");
> - inc_proc_ic_force_recompute();
> + engine_set_force_recompute(engine);
> }
> ovnsb_cond_seqno = new_ovnsb_cond_seqno;
> }
> @@ -1348,7 +1352,7 @@ main(int argc, char *argv[])
> if (new_ovninb_cond_seqno != ovninb_cond_seqno) {
> if (!new_ovninb_cond_seqno) {
> VLOG_INFO("OVN INB IDL reconnected, force recompute.");
> - inc_proc_ic_force_recompute();
> + engine_set_force_recompute(engine);
> }
> ovninb_cond_seqno = new_ovninb_cond_seqno;
> }
> @@ -1361,7 +1365,7 @@ main(int argc, char *argv[])
> if (new_ovnisb_cond_seqno != ovnisb_cond_seqno) {
> if (!new_ovnisb_cond_seqno) {
> VLOG_INFO("OVN ISB IDL reconnected, force recompute.");
> - inc_proc_ic_force_recompute();
> + engine_set_force_recompute(engine);
> }
> ovnisb_cond_seqno = new_ovnisb_cond_seqno;
> }
> @@ -1375,7 +1379,7 @@ main(int argc, char *argv[])
> if (!new_ovnisb_unlocked_cond_seqno) {
> VLOG_INFO("OVN ISB IDL Unlocked reconnected, "
> "force recompute.");
> - inc_proc_ic_force_recompute();
> + engine_set_force_recompute(engine);
> }
> ovnisb_unlocked_cond_seqno = new_ovnisb_unlocked_cond_seqno;
> }
> @@ -1441,7 +1445,7 @@ main(int argc, char *argv[])
> VLOG_INFO("OVN ISB lock acquired. "
> "This ovn-ic instance is now active.");
> state.had_isb_lock = true;
> - inc_proc_ic_force_recompute_immediate();
> + engine_set_force_recompute_immediate(engine);
> } else if (state.had_isb_lock &&
> !ovsdb_idl_has_lock(ctx.ovnisb_idl)) {
> VLOG_INFO("OVN ISB lock lost.");
> @@ -1477,7 +1481,7 @@ main(int argc, char *argv[])
> update_sequence_numbers(&ctx, runned_az,
> &ovnisb_unlocked_idl_loop);
> }
> - } else if (!inc_proc_ic_get_force_recompute()) {
> + } else if (!engine_get_force_recompute(engine)) {
> clear_idl_track = false;
> }
> /* If there are any errors, we force a full recompute in
> order
> @@ -1485,25 +1489,25 @@ main(int argc, char *argv[])
> if (!ovsdb_idl_loop_commit_and_wait(&ovnnb_idl_loop)) {
> VLOG_INFO("OVNNB commit failed, "
> "force recompute next time.");
> - inc_proc_ic_force_recompute_immediate();
> + engine_set_force_recompute_immediate(engine);
> }
>
> if (!ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop)) {
> VLOG_INFO("OVNSB commit failed, "
> "force recompute next time.");
> - inc_proc_ic_force_recompute_immediate();
> + engine_set_force_recompute_immediate(engine);
> }
>
> if (!ovsdb_idl_loop_commit_and_wait(&ovninb_idl_loop)) {
> VLOG_INFO("OVNINB commit failed, "
> "force recompute next time.");
> - inc_proc_ic_force_recompute_immediate();
> + engine_set_force_recompute_immediate(engine);
> }
> if (!ovsdb_idl_loop_commit_and_wait(
> &ovnisb_unlocked_idl_loop)) {
> VLOG_INFO("OVNISB Unlocked commit failed, "
> "force recompute next time.");
> - inc_proc_ic_force_recompute_immediate();
> + engine_set_force_recompute_immediate(engine);
> }
>
> /*
> @@ -1514,7 +1518,7 @@ main(int argc, char *argv[])
> ovsdb_idl_has_lock(ovnisb_idl_loop.idl)) {
> VLOG_INFO("OVNISB commit failed, "
> "force recompute next time.");
> - inc_proc_ic_force_recompute_immediate();
> + engine_set_force_recompute_immediate(engine);
> }
> } else {
> /* Make sure we send any pending requests, e.g., lock. */
> @@ -1534,7 +1538,7 @@ main(int argc, char *argv[])
> */
> }
> /* Force a full recompute next time we become active. */
> - inc_proc_ic_force_recompute();
> + engine_set_force_recompute(engine);
> }
> } else {
> /* ovn-ic is paused
> @@ -1571,7 +1575,7 @@ main(int argc, char *argv[])
> ovsdb_idl_wait(ovnisb_unlocked_idl_loop.idl);
>
> /* Force a full recompute next time we become active. */
> - inc_proc_ic_force_recompute();
> + engine_set_force_recompute(engine);
> }
>
> if (clear_idl_track) {
> diff --git a/lib/inc-proc-eng.c b/lib/inc-proc-eng.c
> index 11285b300d..20cb3ad14e 100644
> --- a/lib/inc-proc-eng.c
> +++ b/lib/inc-proc-eng.c
> @@ -18,7 +18,6 @@
>
> #include <errno.h>
> #include <getopt.h>
> -#include <inttypes.h>
> #include <signal.h>
> #include <stdlib.h>
> #include <string.h>
> @@ -27,24 +26,33 @@
> #include "openvswitch/dynamic-string.h"
> #include "openvswitch/hmap.h"
> #include "openvswitch/poll-loop.h"
> -#include "openvswitch/shash.h"
> #include "openvswitch/vlog.h"
> #include "ovsdb-idl.h"
> #include "inc-proc-eng.h"
> #include "timeval.h"
> #include "unixctl.h"
> -#include "vec.h"
> #include "sset.h"
> #include "lib/stopwatch.h"
>
> VLOG_DEFINE_THIS_MODULE(inc_proc_eng);
>
> -static bool engine_force_recompute = false;
> -static bool engine_run_canceled = false;
> -static const struct engine_context *engine_context;
> +#define ENGINE_INITIALIZER(E, NAME) \
> + (struct engine) { \
> + .name = (NAME), \
> + .nodes = VECTOR_EMPTY_INITIALIZER(struct engine_node *), \
> + .stopwatch_keys = SHASH_INITIALIZER(&(E)->stopwatch_keys), \
> + .force_recompute = false, \
> + .run_canceled = false, \
> + .compute_log_timeout_msec = 500, \
> + }
>
> -static struct vector engine_nodes =
> - VECTOR_EMPTY_INITIALIZER(struct engine_node *);
> +/* Keep used names so a new graph cannot inherit the process-wide OVS
> + * stopwatch records of a retired graph. */
> +static struct shash registered_engines =
> + SHASH_INITIALIZER(®istered_engines);
> +static struct sset engine_names = SSET_INITIALIZER(&engine_names);
I just realized that engine_names is not needed, we can simply use the
registered_engines directly. But I'll wait for some reviews before
posting v2.
> +
> +static const struct engine_context *engine_context;
>
> static const char *engine_node_state_name[EN_STATE_MAX] = {
> [EN_STALE] = "Stale",
> @@ -53,35 +61,33 @@ static const char *engine_node_state_name[EN_STATE_MAX] =
> {
> [EN_CANCELED] = "Canceled",
> };
>
> -static long long engine_compute_log_timeout_msec = 500;
> -
> static void
> -engine_recompute(struct engine_node *node, bool allowed,
> - const char *reason_fmt, ...) OVS_PRINTF_FORMAT(3, 4);
> +engine_recompute(struct engine *, struct engine_node *, bool allowed,
> + const char *reason_fmt, ...) OVS_PRINTF_FORMAT(4, 5);
>
> void
> -engine_set_force_recompute(void)
> +engine_set_force_recompute(struct engine *e)
> {
> - engine_force_recompute = true;
> + e->force_recompute = true;
> }
>
> void
> -engine_set_force_recompute_immediate(void)
> +engine_set_force_recompute_immediate(struct engine *e)
> {
> - engine_force_recompute = true;
> + e->force_recompute = true;
> poll_immediate_wake();
> }
>
> void
> -engine_clear_force_recompute(void)
> +engine_clear_force_recompute(struct engine *e)
> {
> - engine_force_recompute = false;
> + e->force_recompute = false;
> }
>
> bool
> -engine_get_force_recompute(void)
> +engine_get_force_recompute(const struct engine *e)
> {
> - return engine_force_recompute;
> + return e->force_recompute;
> }
>
> const struct engine_context *
> @@ -120,26 +126,26 @@ engine_topo_sort(struct engine_node *node, struct
> vector *sorted_nodes)
> }
>
> static void
> -engine_clear_stats(struct unixctl_conn *conn, int argc OVS_UNUSED,
> - const char *argv[] OVS_UNUSED, void *arg OVS_UNUSED)
> +engine_clear_stats(struct engine *e, struct unixctl_conn *conn,
> + int argc OVS_UNUSED, const char *argv[] OVS_UNUSED)
> {
> struct engine_node *node;
> - VECTOR_FOR_EACH (&engine_nodes, node) {
> + VECTOR_FOR_EACH (&e->nodes, node) {
> memset(&node->stats, 0, sizeof node->stats);
> }
> unixctl_command_reply(conn, NULL);
> }
>
> static void
> -engine_dump_stats(struct unixctl_conn *conn, int argc,
> - const char *argv[], void *arg OVS_UNUSED)
> +engine_dump_stats(struct engine *e, struct unixctl_conn *conn, int argc,
> + const char *argv[])
> {
> struct ds dump = DS_EMPTY_INITIALIZER;
> - const char *dump_eng_node_name = (argc > 1 ? argv[1] : NULL);
> - const char *dump_stat_type = (argc > 2 ? argv[2] : NULL);
> + const char *dump_eng_node_name = argc > 0 ? argv[0] : NULL;
> + const char *dump_stat_type = argc > 1 ? argv[1] : NULL;
>
> struct engine_node *node;
> - VECTOR_FOR_EACH (&engine_nodes, node) {
> + VECTOR_FOR_EACH (&e->nodes, node) {
> if (dump_eng_node_name && strcmp(node->name, dump_eng_node_name)) {
> continue;
> }
> @@ -175,119 +181,250 @@ engine_dump_stats(struct unixctl_conn *conn, int argc,
> }
>
> static void
> -engine_trigger_recompute_cmd(struct unixctl_conn *conn, int argc OVS_UNUSED,
> - const char *argv[] OVS_UNUSED,
> - void *arg OVS_UNUSED)
> +engine_trigger_recompute_cmd(struct engine *e, struct unixctl_conn *conn,
> + int argc OVS_UNUSED,
> + const char *argv[] OVS_UNUSED)
> {
> - engine_trigger_recompute();
> + engine_trigger_recompute(e);
> unixctl_command_reply(conn, NULL);
> }
>
> static void
> -engine_set_log_timeout_cmd(struct unixctl_conn *conn, int argc OVS_UNUSED,
> - const char *argv[], void *arg OVS_UNUSED)
> +engine_set_log_timeout_cmd(struct engine *e, struct unixctl_conn *conn,
> + int argc OVS_UNUSED, const char *argv[])
> {
>
> unsigned int timeout;
> - if (!str_to_uint(argv[1], 10, &timeout)) {
> + if (!str_to_uint(argv[0], 10, &timeout)) {
> unixctl_command_reply_error(conn, "unsigned integer required");
> return;
> }
> - engine_compute_log_timeout_msec = timeout;
> + e->compute_log_timeout_msec = timeout;
> unixctl_command_reply(conn, NULL);
> }
>
> #define ENGINE_STOPWATCH_MAX_NAME 31
> #define ENGINE_STOPWATCH_PREFIX "@ip:"
>
> -BUILD_ASSERT_DECL(sizeof ENGINE_STOPWATCH_PREFIX "4294967295"
> +BUILD_ASSERT_DECL(sizeof ENGINE_STOPWATCH_PREFIX "4294967295:4294967295"
> <= ENGINE_STOPWATCH_MAX_NAME + 1);
>
> -static struct shash engine_stopwatch_keys =
> - SHASH_INITIALIZER(&engine_stopwatch_keys);
> -
> static void
> -engine_list_stopwatch_cmd(struct unixctl_conn *conn, int argc OVS_UNUSED,
> - const char *argv[], void *arg OVS_UNUSED)
> +engine_list_stopwatches_cmd(struct engine *e, struct unixctl_conn *conn,
> + int argc, const char *argv[])
> {
> - const char *node_name = argc > 1 ? argv[1] : NULL;
> + const char *node_name = argc ? argv[0] : NULL;
> + struct ds output = DS_EMPTY_INITIALIZER;
> +
> struct engine_node *node;
> - struct ds output_str = DS_EMPTY_INITIALIZER;
> - VECTOR_FOR_EACH (&engine_nodes, node) {
> + VECTOR_FOR_EACH (&e->nodes, node) {
> if (node_name && strcmp(node->name, node_name)) {
> continue;
> }
> - ds_put_format(&output_str, "%s -> %s\n", node->name,
> + ds_put_format(&output, "%s -> %s\n", node->name,
> node->stopwatch_key);
> for (size_t i = 0; i < node->n_inputs; i++) {
> - struct engine_node_input *input = &node->inputs[i];
> - if (!input->change_handler) {
> - continue;
> + const struct engine_node_input *input = &node->inputs[i];
> + if (input->change_handler) {
> + ds_put_format(&output, "%s -> %s\n",
> + input->change_handler_name,
> + input->stopwatch_key);
> }
> - ds_put_format(&output_str, "%s -> %s\n",
> - input->change_handler_name, input->stopwatch_key);
> }
> }
>
> - unixctl_command_reply(conn, ds_cstr(&output_str));
> - ds_destroy(&output_str);
> + unixctl_command_reply(conn, ds_cstr(&output));
> + ds_destroy(&output);
> }
>
> static void
> -engine_set_stopwatch_cmd(struct unixctl_conn *conn, int argc,
> - const char *argv[], bool enabled)
> +engine_set_stopwatch_cmd(struct engine *e, struct unixctl_conn *conn,
> + int argc, const char *argv[], bool enabled)
> {
> - const char *node_name = argc > 1 ? argv[1] : NULL;
> + const char *node_name = argc ? argv[0] : NULL;
>
> struct engine_node *node;
> - bool found = false;
> - VECTOR_FOR_EACH (&engine_nodes, node) {
> - if (node_name && strcmp(node->name, node_name)) {
> - continue;
> - }
> - node->stopwatch_enabled = enabled;
> - found = true;
> - if (node_name) {
> - break;
> + VECTOR_FOR_EACH (&e->nodes, node) {
> + if (!node_name || !strcmp(node->name, node_name)) {
> + node->stopwatch_enabled = enabled;
> + if (node_name) {
> + unixctl_command_reply(conn, NULL);
> + return;
> + }
> }
> }
> -
> - if (node_name && !found) {
> + if (node_name) {
> unixctl_command_reply_error(conn, "node not found");
> - return;
> + } else {
> + unixctl_command_reply(conn, NULL);
> }
> - unixctl_command_reply(conn, NULL);
> }
>
> static void
> -engine_enable_stopwatches_cmd(struct unixctl_conn *conn, int argc,
> - const char *argv[], void *arg OVS_UNUSED)
> +engine_enable_stopwatches_cmd(struct engine *e, struct unixctl_conn *conn,
> + int argc, const char *argv[])
> {
> - engine_set_stopwatch_cmd(conn, argc, argv, true);
> + engine_set_stopwatch_cmd(e, conn, argc, argv, true);
> }
>
> static void
> -engine_disable_stopwatches_cmd(struct unixctl_conn *conn, int argc,
> - const char *argv[], void *arg OVS_UNUSED)
> +engine_disable_stopwatches_cmd(struct engine *e, struct unixctl_conn *conn,
> + int argc, const char *argv[])
> {
> - engine_set_stopwatch_cmd(conn, argc, argv, false);
> + engine_set_stopwatch_cmd(e, conn, argc, argv, false);
> +}
> +
> +static void
> +engine_list_cmd(struct unixctl_conn *conn, int argc OVS_UNUSED,
> + const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
> +{
> + struct ds output = DS_EMPTY_INITIALIZER;
> + const struct shash_node **engines = shash_sort(®istered_engines);
> +
> + for (size_t i = 0; i < shash_count(®istered_engines); i++) {
> + ds_put_format(&output, "%s\n", engines[i]->name);
> + }
> + unixctl_command_reply(conn, ds_cstr(&output));
> + free(engines);
> + ds_destroy(&output);
> +}
> +
> +struct engine_command {
> + const char *name;
> + const char *usage;
> + int min_args;
> + int max_args;
> + void (*run)(struct engine *, struct unixctl_conn *,
> + int argc, const char *argv[]);
> +};
> +
> +static void
> +engine_dispatch_cmd(struct unixctl_conn *conn, int argc, const char *argv[],
> + void *aux)
> +{
> + const struct engine_command *command = aux;
> + struct engine *e;
> +
> + /* Skip the command name and consume an optional leading selector. */
> + argc--;
> + argv++;
> + const char selector[] = "--engine=";
> + const char *name = NULL;
> + for (int i = 0; i < argc; i++) {
> + bool has_value = !strncmp(argv[i], selector, sizeof selector - 1);
> + if (!strcmp(argv[i], "--engine")
> + || (has_value && (i || !argv[i][sizeof selector - 1]))) {
> + unixctl_command_reply_error(conn, "expected one leading "
> + "--engine=NAME selector");
> + return;
> + }
> + if (has_value) {
> + name = argv[i] + sizeof selector - 1;
> + }
> + }
> + if (name) {
> + e = shash_find_data(®istered_engines, name);
> + if (!e) {
> + unixctl_command_reply_error(conn, "unknown registered engine");
> + return;
> + }
> + argc--;
> + argv++;
> + } else if (shash_is_empty(®istered_engines)) {
> + unixctl_command_reply_error(conn, "no registered engines");
> + return;
> + } else if (shash_count(®istered_engines) > 1) {
> + unixctl_command_reply_error(conn, "multiple registered engines; use"
> + " inc-engine/list-engines and"
> + " --engine=NAME");
> + return;
> + } else {
> + e = shash_first(®istered_engines)->data;
> + }
> +
> + if (argc < command->min_args || argc > command->max_args) {
> + unixctl_command_reply_error(conn, "wrong number of arguments");
> + return;
> + }
> + command->run(e, conn, argc, argv);
> +}
> +
> +void
> +engine_infra_init(void)
> +{
> + static struct engine_command commands[] = {
> + { "inc-engine/show-stats", "[--engine=NAME] [NODE [COUNTER]]", 0, 2,
> + engine_dump_stats },
> + { "inc-engine/clear-stats", "[--engine=NAME]", 0, 0,
> + engine_clear_stats },
> + { "inc-engine/recompute", "[--engine=NAME]", 0, 0,
> + engine_trigger_recompute_cmd },
> + { "inc-engine/compute-log-timeout", "[--engine=NAME] MSEC", 1, 1,
> + engine_set_log_timeout_cmd },
> + { "inc-engine/list-stopwatches", "[--engine=NAME] [NODE]", 0, 1,
> + engine_list_stopwatches_cmd },
> + { "inc-engine/enable-stopwatch", "[--engine=NAME] [NODE]", 0, 1,
> + engine_enable_stopwatches_cmd },
> + { "inc-engine/disable-stopwatch", "[--engine=NAME] [NODE]", 0, 1,
> + engine_disable_stopwatches_cmd },
> + };
> +
> + unixctl_command_register("inc-engine/list-engines", "", 0, 0,
> + engine_list_cmd, NULL);
> + for (size_t i = 0; i < ARRAY_SIZE(commands); i++) {
> + struct engine_command *command = &commands[i];
> + unixctl_command_register(command->name, command->usage,
> + command->min_args, command->max_args + 1,
> + engine_dispatch_cmd, command);
> + }
> }
>
> static const char *
> -engine_register_stopwatch(const char *label)
> +engine_register_stopwatch(struct engine *e, uint32_t id, const char *label)
> {
> - char *key = shash_find_data(&engine_stopwatch_keys, label);
> + char *key = shash_find_data(&e->stopwatch_keys, label);
> if (!key) {
> - size_t n_keys = shash_count(&engine_stopwatch_keys);
> + size_t n_keys = shash_count(&e->stopwatch_keys);
> ovs_assert(n_keys <= UINT32_MAX);
> -
> - key = xasprintf(ENGINE_STOPWATCH_PREFIX"%"PRIuSIZE, n_keys);
> + key = xasprintf(ENGINE_STOPWATCH_PREFIX"%"PRIu32":%"PRIuSIZE,
> + id, n_keys);
> stopwatch_create(key, SW_MS);
> - shash_add(&engine_stopwatch_keys, label, key);
> + shash_add(&e->stopwatch_keys, label, key);
> }
> return key;
> }
>
> +static void
> +engine_register(struct engine *e)
> +{
> + if (!e->name || !e->name[0]) {
> + ovs_fatal(0, "Missing incremental processing engine name");
> + }
> + if (sset_contains(&engine_names, e->name)) {
> + ovs_fatal(0, "Could not register incremental processing engine '%s'",
> + e->name);
> + }
> +
> + size_t id = sset_count(&engine_names);
> + ovs_assert(id <= UINT32_MAX);
> + sset_add(&engine_names, e->name);
> +
> + struct engine_node *node;
> + VECTOR_FOR_EACH (&e->nodes, node) {
> + node->stopwatch_key =
> + engine_register_stopwatch(e, id, node->name);
> + for (size_t i = 0; i < node->n_inputs; i++) {
> + struct engine_node_input *input = &node->inputs[i];
> + if (input->change_handler) {
> + input->stopwatch_key =
> + engine_register_stopwatch(e, id,
> + input->change_handler_name);
> + }
> + }
> + }
> + shash_add(®istered_engines, e->name, e);
> +}
> +
> static void
> engine_get_compute_failure_info(struct engine_node *node)
> {
> @@ -295,12 +432,14 @@ engine_get_compute_failure_info(struct engine_node
> *node)
> }
>
> void
> -engine_init(struct engine_node *node, struct engine_arg *arg)
> +engine_init(struct engine *e, const char *name, struct engine_node *node,
> + struct engine_arg *arg)
> {
> - engine_topo_sort(node, &engine_nodes);
> + *e = ENGINE_INITIALIZER(e, name);
> + engine_topo_sort(node, &e->nodes);
>
> struct engine_node *sorted_node;
> - VECTOR_FOR_EACH (&engine_nodes, sorted_node) {
> + VECTOR_FOR_EACH (&e->nodes, sorted_node) {
> if (sorted_node->init) {
> sorted_node->data = sorted_node->init(sorted_node, arg);
> } else {
> @@ -313,39 +452,19 @@ engine_init(struct engine_node *node, struct engine_arg
> *arg)
> }
> }
>
> - VECTOR_FOR_EACH (&engine_nodes, sorted_node) {
> - sorted_node->stopwatch_key =
> - engine_register_stopwatch(sorted_node->name);
> - for (size_t i = 0; i < sorted_node->n_inputs; i++) {
> - struct engine_node_input *input = &sorted_node->inputs[i];
> - if (input->change_handler) {
> - input->stopwatch_key =
> - engine_register_stopwatch(input->change_handler_name);
> - }
> - }
> - }
> -
> - unixctl_command_register("inc-engine/show-stats", "", 0, 2,
> - engine_dump_stats, NULL);
> - unixctl_command_register("inc-engine/clear-stats", "", 0, 0,
> - engine_clear_stats, NULL);
> - unixctl_command_register("inc-engine/recompute", "", 0, 0,
> - engine_trigger_recompute_cmd, NULL);
> - unixctl_command_register("inc-engine/compute-log-timeout", "", 1, 1,
> - engine_set_log_timeout_cmd, NULL);
> - unixctl_command_register("inc-engine/list-stopwatches", "", 0, 1,
> - engine_list_stopwatch_cmd, NULL);
> - unixctl_command_register("inc-engine/enable-stopwatch", "[node]", 0, 1,
> - engine_enable_stopwatches_cmd, NULL);
> - unixctl_command_register("inc-engine/disable-stopwatch", "[node]", 0, 1,
> - engine_disable_stopwatches_cmd, NULL);
> + engine_register(e);
> }
>
> void
> -engine_cleanup(void)
> +engine_cleanup(struct engine *e)
> {
> + ovs_assert(e->name);
> + struct shash_node *registered = shash_find(®istered_engines, e->name);
> + ovs_assert(registered && registered->data == e);
> + shash_delete(®istered_engines, registered);
> +
> struct engine_node *node;
> - VECTOR_FOR_EACH (&engine_nodes, node) {
> + VECTOR_FOR_EACH (&e->nodes, node) {
> if (node->clear_tracked_data) {
> node->clear_tracked_data(node->data);
> }
> @@ -361,8 +480,8 @@ engine_cleanup(void)
> }
> }
>
> - shash_destroy_free_data(&engine_stopwatch_keys);
> - vector_destroy(&engine_nodes);
> + shash_destroy_free_data(&e->stopwatch_keys);
> + vector_destroy(&e->nodes);
> }
>
> struct engine_node *
> @@ -480,10 +599,10 @@ engine_node_changed(struct engine_node *node)
> }
>
> bool
> -engine_has_run(void)
> +engine_has_run(const struct engine *e)
> {
> struct engine_node *node;
> - VECTOR_FOR_EACH (&engine_nodes, node) {
> + VECTOR_FOR_EACH (&e->nodes, node) {
> if (node->state != EN_STALE) {
> return true;
> }
> @@ -492,10 +611,10 @@ engine_has_run(void)
> }
>
> bool
> -engine_has_updated(void)
> +engine_has_updated(const struct engine *e)
> {
> struct engine_node *node;
> - VECTOR_FOR_EACH (&engine_nodes, node) {
> + VECTOR_FOR_EACH (&e->nodes, node) {
> if (node->state == EN_UPDATED) {
> return true;
> }
> @@ -504,9 +623,9 @@ engine_has_updated(void)
> }
>
> bool
> -engine_canceled(void)
> +engine_canceled(const struct engine *e)
> {
> - return engine_run_canceled;
> + return e->run_canceled;
> }
>
> void *
> @@ -525,11 +644,11 @@ engine_get_internal_data(struct engine_node *node)
> }
>
> void
> -engine_init_run(void)
> +engine_init_run(struct engine *e)
> {
> - VLOG_DBG("Initializing new run");
> + VLOG_DBG("Initializing new run for engine %s", e->name);
> struct engine_node *node;
> - VECTOR_FOR_EACH (&engine_nodes, node) {
> + VECTOR_FOR_EACH (&e->nodes, node) {
> engine_set_node_state(node, EN_STALE, "engine_init_run");
>
> if (node->clear_tracked_data) {
> @@ -570,7 +689,7 @@ run_change_handler(struct engine_node *node, struct
> engine_node_input *input)
> * mark the node as "canceled".
> */
> static void
> -engine_recompute(struct engine_node *node, bool allowed,
> +engine_recompute(struct engine *e, struct engine_node *node, bool allowed,
> const char *reason_fmt, ...)
> {
> char *reason = NULL;
> @@ -581,7 +700,8 @@ engine_recompute(struct engine_node *node, bool allowed,
> va_end(reason_args);
>
> if (node->sb_write && !allowed) {
> - VLOG_DBG("node: %s, recompute (%s) canceled", node->name, reason);
> + VLOG_DBG("engine %s, node: %s, recompute (%s) canceled",
> + e->name, node->name, reason);
> engine_set_node_state(node, EN_CANCELED, "recompute not allowed");
> goto done;
> }
> @@ -598,13 +718,13 @@ engine_recompute(struct engine_node *node, bool allowed,
> "recompute run() result");
> node->stats.recompute++;
> long long int delta_time = time_msec() - now;
> - if (delta_time > engine_compute_log_timeout_msec) {
> + if (delta_time > e->compute_log_timeout_msec) {
> static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(20, 10);
> - VLOG_INFO_RL(&rl, "node: %s, recompute (%s) took %lldms", node->name,
> - reason, delta_time);
> + VLOG_INFO_RL(&rl, "engine %s, node: %s, recompute (%s) took %lldms",
> + e->name, node->name, reason, delta_time);
> } else {
> - VLOG_DBG("node: %s, recompute (%s) took %lldms", node->name, reason,
> - delta_time);
> + VLOG_DBG("engine %s, node: %s, recompute (%s) took %lldms",
> + e->name, node->name, reason, delta_time);
> }
> done:
> free(reason);
> @@ -612,7 +732,8 @@ done:
>
> /* Return true if the node could be computed, false otherwise. */
> static bool
> -engine_compute(struct engine_node *node, bool recompute_allowed)
> +engine_compute(struct engine *e, struct engine_node *node,
> + bool recompute_allowed)
> {
> for (size_t i = 0; i < node->n_inputs; i++) {
> struct engine_node *input_node = node->inputs[i].node;
> @@ -626,18 +747,20 @@ engine_compute(struct engine_node *node, bool
> recompute_allowed)
> enum engine_input_handler_result handled;
> handled = run_change_handler(node, &node->inputs[i]);
> long long int delta_time = time_msec() - now;
> - if (delta_time > engine_compute_log_timeout_msec) {
> + if (delta_time > e->compute_log_timeout_msec) {
> static struct vlog_rate_limit rl =
> VLOG_RATE_LIMIT_INIT(20, 10);
> - VLOG_INFO_RL(&rl, "node: %s, handler for input %s took
> %lldms",
> - node->name, input_node->name, delta_time);
> + VLOG_INFO_RL(&rl, "engine %s, node: %s, handler for input %s"
> + " took %lldms", e->name, node->name,
> + input_node->name, delta_time);
> } else {
> - VLOG_DBG("node: %s, handler for input %s took %lldms",
> - node->name, input_node->name, delta_time);
> + VLOG_DBG("engine %s, node: %s, handler for input %s took"
> + " %lldms", e->name, node->name, input_node->name,
> + delta_time);
> }
> if (handled == EN_UNHANDLED) {
> input_node->get_compute_failure_info(input_node);
> - engine_recompute(node, recompute_allowed,
> + engine_recompute(e, node, recompute_allowed,
> "failed handler for input %s",
> input_node->name);
> return (node->state != EN_CANCELED);
> @@ -657,7 +780,8 @@ engine_compute(struct engine_node *node, bool
> recompute_allowed)
> }
>
> static void
> -engine_run_node(struct engine_node *node, bool recompute_allowed)
> +engine_run_node(struct engine *e, struct engine_node *node,
> + bool recompute_allowed)
> {
> if (!node->n_inputs) {
> /* Run the node handler which might change state. */
> @@ -667,8 +791,8 @@ engine_run_node(struct engine_node *node, bool
> recompute_allowed)
> return;
> }
>
> - if (engine_force_recompute) {
> - engine_recompute(node, recompute_allowed, "forced");
> + if (e->force_recompute) {
> + engine_recompute(e, node, recompute_allowed, "forced");
> return;
> }
>
> @@ -683,7 +807,7 @@ engine_run_node(struct engine_node *node, bool
> recompute_allowed)
>
> /* Trigger a recompute if we don't have a change handler. */
> if (!node->inputs[i].change_handler) {
> - engine_recompute(node, recompute_allowed,
> + engine_recompute(e, node, recompute_allowed,
> "missing handler for input %s",
> input_node->name);
> input_node->get_compute_failure_info(input_node);
> @@ -696,7 +820,7 @@ engine_run_node(struct engine_node *node, bool
> recompute_allowed)
> /* If we couldn't compute the node we either canceled or triggered
> * a full recompute. In any case, stop processing.
> */
> - if (!engine_compute(node, recompute_allowed)) {
> + if (!engine_compute(e, node, recompute_allowed)) {
> return;
> }
> }
> @@ -710,37 +834,37 @@ engine_run_node(struct engine_node *node, bool
> recompute_allowed)
> }
>
> void
> -engine_run(bool recompute_allowed)
> +engine_run(struct engine *e, bool recompute_allowed)
> {
> /* If the last run was canceled skip the incremental run because a
> * recompute is needed first.
> */
> - if (!recompute_allowed && engine_run_canceled) {
> + if (!recompute_allowed && e->run_canceled) {
> return;
> }
>
> struct ovsdb_idl_txn *sb_txn = engine_get_context()->ovnsb_idl_txn;
>
> - engine_run_canceled = false;
> + e->run_canceled = false;
> struct engine_node *node;
> - VECTOR_FOR_EACH (&engine_nodes, node) {
> + VECTOR_FOR_EACH (&e->nodes, node) {
> ovsdb_idl_txn_assert_read_only(sb_txn, !node->sb_write);
> - engine_run_node(node, recompute_allowed);
> + engine_run_node(e, node, recompute_allowed);
> ovsdb_idl_txn_assert_read_only(sb_txn, false);
>
> if (node->state == EN_CANCELED) {
> node->stats.cancel++;
> - engine_run_canceled = true;
> + e->run_canceled = true;
> return;
> }
> }
> }
>
> bool
> -engine_need_run(void)
> +engine_need_run(struct engine *e)
> {
> struct engine_node *node;
> - VECTOR_FOR_EACH (&engine_nodes, node) {
> + VECTOR_FOR_EACH (&e->nodes, node) {
> /* Check only leaf nodes for updates. */
> if (node->n_inputs) {
> continue;
> @@ -759,10 +883,12 @@ engine_need_run(void)
> }
>
> void
> -engine_trigger_recompute(void)
> +engine_trigger_recompute(struct engine *e)
> {
> - VLOG_INFO("User triggered force recompute.");
> - engine_set_force_recompute_immediate();
> + static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(20, 10);
> + VLOG_INFO_RL(&rl, "User triggered force recompute for engine %s.",
> + e->name);
> + engine_set_force_recompute_immediate(e);
> }
>
> static void
> @@ -790,14 +916,14 @@ engine_dump_node(struct engine_node *node, struct sset
> *visited_nodes)
> }
>
> void
> -engine_dump_graph(const char *node_name)
> +engine_dump_graph(const struct engine *e, const char *node_name)
> {
> printf("digraph \"Incremental-Processing-Engine\" {\n");
> printf("\trankdir=LR;\n");
>
> struct sset visited_nodes = SSET_INITIALIZER(&visited_nodes);
> struct engine_node *node;
> - VECTOR_FOR_EACH (&engine_nodes, node) {
> + VECTOR_FOR_EACH (&e->nodes, node) {
> if (node_name && strcmp(node->name, node_name)) {
> continue;
> }
> diff --git a/lib/inc-proc-eng.h b/lib/inc-proc-eng.h
> index 754002f847..d76f8efe04 100644
> --- a/lib/inc-proc-eng.h
> +++ b/lib/inc-proc-eng.h
> @@ -152,6 +152,9 @@
> #include <stdint.h>
>
> #include "compiler.h"
> +#include "openvswitch/shash.h"
> +
> +#include "vec.h"
>
> struct engine_context {
> struct ovsdb_idl_txn *ovs_idl_txn;
> @@ -174,6 +177,18 @@ struct engine_arg {
> struct ovsdb_idl *ovs_idl;
> };
>
> +/* Per-graph incremental processing state. */
> +struct engine {
> + const char *name;
> +
> + struct vector nodes;
> + struct shash stopwatch_keys;
> +
> + bool force_recompute;
> + bool run_canceled;
> + long long compute_log_timeout_msec;
> +};
> +
> struct engine_node;
>
> enum engine_node_state {
> @@ -289,30 +304,31 @@ struct engine_node {
> const char *stopwatch_key;
> };
>
> -/* Initialize the data for the engine nodes. It calls each node's
> - * init() method if not NULL passing the user supplied 'arg'.
> - * It should be called before the main loop. */
> -void engine_init(struct engine_node *node, struct engine_arg *arg);
> +/* Set up the process-wide unixctl commands.
> + * Call once on the main thread during startup. */
> +void engine_infra_init(void);
> +
> +/* Initialize and register the incremental processing engine. */
> +void engine_init(struct engine *e, const char *name, struct engine_node
> *node,
> + struct engine_arg *arg);
>
> /* Initialize the engine nodes for a new run. It should be called in the
> * main processing loop before every potential engine_run().
> */
> -void engine_init_run(void);
> +void engine_init_run(struct engine *);
>
> /* Execute the processing, which should be called in the main loop.
> * Updates the engine node's states accordingly. If 'recompute_allowed' is
> * false and a recompute is required by the current engine run then the
> engine
> * cancels its processing.
> */
> -void engine_run(bool recompute_allowed);
> +void engine_run(struct engine *, bool recompute_allowed);
>
> -/* Clean up the data for the engine nodes. It calls each node's
> - * cleanup() method if not NULL. It should be called before the program
> - * terminates. */
> -void engine_cleanup(void);
> +/* Remove this engine's registration and clean up its graph. */
> +void engine_cleanup(struct engine *);
>
> /* Check if engine needs to run but didn't. */
> -bool engine_need_run(void);
> +bool engine_need_run(struct engine *);
>
> /* Get the input node with <name> for <node> */
> struct engine_node * engine_get_input(const char *input_name,
> @@ -348,20 +364,20 @@ void engine_add_input_with_compute_debug_impl(
> * in circumstances when we are not sure there is change or not, or
> * when there is change but the engine couldn't be executed in that
> * iteration, and the change can't be tracked across iterations. */
> -void engine_set_force_recompute(void);
> +void engine_set_force_recompute(struct engine *);
>
> -void engine_dump_graph(const char *node_name);
> +void engine_dump_graph(const struct engine *, const char *node_name);
>
> /* Same as "engine_set_force_recompute()", but the poll_loop is woken up
> * immediately and the next engine run is not delayed. */
> -void engine_set_force_recompute_immediate(void);
> +void engine_set_force_recompute_immediate(struct engine *);
>
> /* Clear the force flag for the next run so the engine does the
> * usual processing without forced full recompute. */
> -void engine_clear_force_recompute(void);
> +void engine_clear_force_recompute(struct engine *);
>
> /* Returns whether next engine_run() is forced to rempute. */
> -bool engine_get_force_recompute(void);
> +bool engine_get_force_recompute(const struct engine *);
>
> /* Return the current engine_context. The values in the context can be NULL
> * if the engine is run with allow_recompute == false in the current
> @@ -377,14 +393,14 @@ void engine_set_context(const struct engine_context *);
> bool engine_node_changed(struct engine_node *node);
>
> /* Return true if the engine has run in the last iteration. */
> -bool engine_has_run(void);
> +bool engine_has_run(const struct engine *);
>
> /* Return true if the engine has any update in any node, i.e. any input
> * has changed; false if nothing has changed. */
> -bool engine_has_updated(void);
> +bool engine_has_updated(const struct engine *);
>
> /* Returns true if during the last engine run we had to cancel processing. */
> -bool engine_canceled(void);
> +bool engine_canceled(const struct engine *);
>
> /* Return a pointer to node data accessible for users outside the processing
> * engine. If the node data is not valid (e.g., last engine_run() failed or
> @@ -406,7 +422,7 @@ void *engine_get_data(struct engine_node *node);
> void *engine_get_internal_data(struct engine_node *node);
>
> /* Trigger a full recompute. */
> -void engine_trigger_recompute(void);
> +void engine_trigger_recompute(struct engine *);
>
> struct ed_ovsdb_index {
> const char *name;
> diff --git a/northd/inc-proc-northd.c b/northd/inc-proc-northd.c
> index 49348e42f3..0415663548 100644
> --- a/northd/inc-proc-northd.c
> +++ b/northd/inc-proc-northd.c
> @@ -59,6 +59,8 @@ VLOG_DEFINE_THIS_MODULE(inc_proc_northd);
>
> static unixctl_cb_func chassis_features_list;
>
> +static struct engine engine;
> +
> #define NB_NODES \
> NB_NODE(nb_global) \
> NB_NODE(logical_switch) \
> @@ -198,8 +200,9 @@ static ENGINE_NODE(datapath_synced_logical_router,
> CLEAR_TRACKED_DATA);
> static ENGINE_NODE(datapath_synced_logical_switch, CLEAR_TRACKED_DATA);
> static ENGINE_NODE(ic_learned_svc_monitors, SB_WRITE);
>
> -void inc_proc_northd_init(struct ovsdb_idl_loop *nb,
> - struct ovsdb_idl_loop *sb)
> +struct engine *
> +inc_proc_northd_init(struct ovsdb_idl_loop *nb,
> + struct ovsdb_idl_loop *sb)
> {
> /* Define relationships between nodes where first argument is dependent
> * on the second argument */
> @@ -547,7 +550,7 @@ void inc_proc_northd_init(struct ovsdb_idl_loop *nb,
> struct ovsdb_idl_index *fdb_by_dp_key =
> ovsdb_idl_index_create1(sb->idl, &sbrec_fdb_col_dp_key);
>
> - engine_init(&en_northd_output, &engine_arg);
> + engine_init(&engine, "ovn-northd-i-p", &en_northd_output, &engine_arg);
>
> engine_ovsdb_node_add_index(&en_sb_chassis,
> "sbrec_chassis_by_name",
> @@ -635,6 +638,7 @@ void inc_proc_northd_init(struct ovsdb_idl_loop *nb,
> unixctl_command_register("debug/chassis-features-list", "", 0, 0,
> chassis_features_list,
> &global_config->features);
> + return &engine;
> }
>
> /* Returns true if the incremental processing ended up updating nodes. */
> @@ -644,7 +648,7 @@ bool inc_proc_northd_run(struct ovsdb_idl_txn *ovnnb_txn,
> ovs_assert(ovnnb_txn && ovnsb_txn);
>
> int64_t start = time_msec();
> - engine_init_run();
> + engine_init_run(&engine);
>
> struct engine_context eng_ctx = {
> .ovnnb_idl_txn = ovnnb_txn,
> @@ -652,20 +656,20 @@ bool inc_proc_northd_run(struct ovsdb_idl_txn
> *ovnnb_txn,
> };
>
> engine_set_context(&eng_ctx);
> - engine_run(true);
> + engine_run(&engine, true);
>
> - if (!engine_has_run()) {
> - if (engine_need_run()) {
> + if (!engine_has_run(&engine)) {
> + if (engine_need_run(&engine)) {
> VLOG_DBG("engine did not run, force recompute next time.");
> - engine_set_force_recompute_immediate();
> + engine_set_force_recompute_immediate(&engine);
> } else {
> VLOG_DBG("engine did not run, and it was not needed");
> }
> - } else if (engine_canceled()) {
> + } else if (engine_canceled(&engine)) {
> VLOG_DBG("engine was canceled, force recompute next time.");
> - engine_set_force_recompute_immediate();
> + engine_set_force_recompute_immediate(&engine);
> } else {
> - engine_clear_force_recompute();
> + engine_clear_force_recompute(&engine);
> }
>
> int64_t now = time_msec();
> @@ -673,19 +677,20 @@ bool inc_proc_northd_run(struct ovsdb_idl_txn
> *ovnnb_txn,
> * by "northd-backoff-interval-ms" interval. */
> ctx->next_run_ms = now + MIN(now - start, ctx->backoff_ms);
>
> - return engine_has_updated();
> + return engine_has_updated(&engine);
> }
>
> void inc_proc_northd_cleanup(void)
> {
> - engine_cleanup();
> engine_set_context(NULL);
> + engine_cleanup(&engine);
> }
>
> bool
> inc_proc_northd_can_run(struct northd_engine_context *ctx)
> {
> - if (engine_get_force_recompute() || time_msec() >= ctx->next_run_ms ||
> + if (engine_get_force_recompute(&engine) ||
> + time_msec() >= ctx->next_run_ms ||
> ctx->nb_idl_duration_ms >= IDL_LOOP_MAX_DURATION_MS ||
> ctx->sb_idl_duration_ms >= IDL_LOOP_MAX_DURATION_MS) {
> return true;
> diff --git a/northd/inc-proc-northd.h b/northd/inc-proc-northd.h
> index ab2ea3ff47..31b2ce3d53 100644
> --- a/northd/inc-proc-northd.h
> +++ b/northd/inc-proc-northd.h
> @@ -13,30 +13,12 @@ struct northd_engine_context {
> uint32_t backoff_ms;
> };
>
> -void inc_proc_northd_init(struct ovsdb_idl_loop *nb,
> - struct ovsdb_idl_loop *sb);
> +struct engine *inc_proc_northd_init(struct ovsdb_idl_loop *nb,
> + struct ovsdb_idl_loop *sb);
> bool inc_proc_northd_run(struct ovsdb_idl_txn *ovnnb_txn,
> struct ovsdb_idl_txn *ovnsb_txn,
> struct northd_engine_context *ctx);
> void inc_proc_northd_cleanup(void);
> bool inc_proc_northd_can_run(struct northd_engine_context *ctx);
>
> -static inline void
> -inc_proc_northd_force_recompute(void)
> -{
> - engine_set_force_recompute();
> -}
> -
> -static inline void
> -inc_proc_northd_force_recompute_immediate(void)
> -{
> - engine_set_force_recompute_immediate();
> -}
> -
> -static inline bool
> -inc_proc_northd_get_force_recompute(void)
> -{
> - return engine_get_force_recompute();
> -}
> -
> #endif /* INC_PROC_NORTHD */
> diff --git a/northd/ovn-northd.8.xml b/northd/ovn-northd.8.xml
> index f4ad26073f..bbe9a774fe 100644
> --- a/northd/ovn-northd.8.xml
> +++ b/northd/ovn-northd.8.xml
> @@ -204,6 +204,15 @@
> the capabilities reported by the connected chassis.
> </dd>
>
> + <dt><code>inc-engine/list-engines</code></dt>
> + <dd>
> + Lists the names of the registered incremental processing engines.
> + The other <code>inc-engine/</code> commands accept
> + <code>--engine=<var>name</var></code> before their positional
> + arguments. The selector is optional with one registered engine
> + and required with multiple registered engines.
> + </dd>
> +
> <dt><code>inc-engine/show-stats</code></dt>
> <dd>
> <p>
> diff --git a/northd/ovn-northd.c b/northd/ovn-northd.c
> index 8c4bd124e2..f7a8021f2d 100644
> --- a/northd/ovn-northd.c
> +++ b/northd/ovn-northd.c
> @@ -23,6 +23,7 @@
> #include "daemon.h"
> #include "fatal-signal.h"
> #include "inc-proc-northd.h"
> +#include "lib/inc-proc-eng.h"
> #include "lib/ip-mcast-index.h"
> #include "lib/mcast-group-index.h"
> #include "lib/memory-trim.h"
> @@ -599,8 +600,9 @@ static void inc_proc_graph_dump(const char *end_node)
> struct ovsdb_idl_loop ovnsb_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
> ovsdb_idl_create_unconnected(&sbrec_idl_class, true));
>
> - inc_proc_northd_init(&ovnnb_idl_loop, &ovnsb_idl_loop);
> - engine_dump_graph(end_node);
> + struct engine *engine =
> + inc_proc_northd_init(&ovnnb_idl_loop, &ovnsb_idl_loop);
> + engine_dump_graph(engine, end_node);
>
> ovsdb_idl_loop_destroy(&ovnnb_idl_loop);
> ovsdb_idl_loop_destroy(&ovnsb_idl_loop);
> @@ -847,6 +849,7 @@ main(int argc, char *argv[])
> if (retval) {
> exit(EXIT_FAILURE);
> }
> + engine_infra_init();
> unixctl_command_register("exit", "", 0, 0, ovn_exit_command_callback,
> &exit_args);
> unixctl_command_register("pause", "", 0, 0, ovn_northd_pause, &state);
> @@ -1015,7 +1018,8 @@ main(int argc, char *argv[])
> stopwatch_create(LFLOWS_TO_SB_STOPWATCH_NAME, SW_MS);
>
> /* Initialize incremental processing engine for ovn-northd */
> - inc_proc_northd_init(&ovnnb_idl_loop, &ovnsb_idl_loop);
> + struct engine *engine =
> + inc_proc_northd_init(&ovnnb_idl_loop, &ovnsb_idl_loop);
>
> unsigned int ovnnb_cond_seqno = UINT_MAX;
> unsigned int ovnsb_cond_seqno = UINT_MAX;
> @@ -1058,7 +1062,7 @@ main(int argc, char *argv[])
> if (new_ovnnb_cond_seqno != ovnnb_cond_seqno) {
> if (!new_ovnnb_cond_seqno) {
> VLOG_INFO("OVN NB IDL reconnected, force recompute.");
> - inc_proc_northd_force_recompute();
> + engine_set_force_recompute(engine);
> }
> ovnnb_cond_seqno = new_ovnnb_cond_seqno;
> }
> @@ -1071,7 +1075,7 @@ main(int argc, char *argv[])
> if (new_ovnsb_cond_seqno != ovnsb_cond_seqno) {
> if (!new_ovnsb_cond_seqno) {
> VLOG_INFO("OVN SB IDL reconnected, force recompute.");
> - inc_proc_northd_force_recompute();
> + engine_set_force_recompute(engine);
> }
> ovnsb_cond_seqno = new_ovnsb_cond_seqno;
> }
> @@ -1118,7 +1122,7 @@ main(int argc, char *argv[])
> ovnsb_idl_loop.idl,
> ovnnb_txn, ovnsb_txn,
> &ovnsb_idl_loop);
> - } else if (!inc_proc_northd_get_force_recompute()) {
> + } else if (!engine_get_force_recompute(engine)) {
> clear_idl_track = false;
> }
>
> @@ -1143,14 +1147,14 @@ main(int argc, char *argv[])
> if (!ovsdb_idl_loop_commit_and_wait(&ovnnb_idl_loop)) {
> VLOG_INFO("OVNNB commit failed, "
> "force recompute next time.");
> - inc_proc_northd_force_recompute_immediate();
> + engine_set_force_recompute_immediate(engine);
> }
>
> if (!ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop) &&
> !ovnsb_txn_aborted_intentionally) {
> VLOG_INFO("OVNSB commit failed, "
> "force recompute next time.");
> - inc_proc_northd_force_recompute_immediate();
> + engine_set_force_recompute_immediate(engine);
> }
> run_memory_trimmer(ovnnb_idl_loop.idl, activity);
> } else {
> @@ -1159,7 +1163,7 @@ main(int argc, char *argv[])
> ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
>
> /* Force a full recompute next time we become active. */
> - inc_proc_northd_force_recompute();
> + engine_set_force_recompute(engine);
> }
> } else {
> /* ovn-northd is paused
> @@ -1183,7 +1187,7 @@ main(int argc, char *argv[])
> ovsdb_idl_wait(ovnsb_idl_loop.idl);
>
> /* Force a full recompute next time we become active. */
> - inc_proc_northd_force_recompute();
> + engine_set_force_recompute(engine);
> }
>
> if (clear_idl_track) {
> diff --git a/tests/automake.mk b/tests/automake.mk
> index 6fef874a21..0f23168eb7 100644
> --- a/tests/automake.mk
> +++ b/tests/automake.mk
> @@ -47,6 +47,7 @@ TESTSUITE_AT = \
> tests/ovn-vif-plug.at \
> tests/ovn-util.at \
> tests/ovn-br-controller.at \
> + tests/ovn-inc-proc-eng.at \
> tests/ovn-inc-proc-graph-dump.at \
> tests/ovn-ctl.at
>
> @@ -282,6 +283,7 @@ tests_ovstest_SOURCES = \
> tests/test-utils.c \
> tests/test-utils.h \
> tests/test-ovn.c \
> + tests/test-inc-proc-eng.c \
> tests/test-sparse-array.c \
> tests/test-spsc-ring.c \
> tests/test-vector.c \
> diff --git a/tests/ovn-inc-proc-eng.at b/tests/ovn-inc-proc-eng.at
> new file mode 100644
> index 0000000000..4ae1ae8d3b
> --- /dev/null
> +++ b/tests/ovn-inc-proc-eng.at
> @@ -0,0 +1,150 @@
> +AT_SETUP([incremental processing - independent engines])
> +AT_KEYWORDS([inc-engine])
> +ovstest test-inc-proc-eng "$PWD/engine.ctl" &
> +engine_pid=$!
> +on_exit 'test -z "$engine_pid" || kill "$engine_pid" 2>/dev/null || :'
> +OVS_WAIT_UNTIL([test -S engine.ctl])
> +
> +appctl () {
> + ovn-appctl -T 5 -t "$PWD/engine.ctl" "$@"
> +}
> +
> +check appctl inc-engine/list-engines
> +AT_CHECK([appctl inc-engine/recompute], [2], [], [stderr])
> +AT_CHECK([grep -Fx 'no registered engines' stderr], [0], [ignore])
> +check appctl test/step start-left
> +AT_CHECK([appctl inc-engine/list-engines], [0], [left
> +])
> +AT_CHECK([appctl inc-engine/show-stats sink recompute], [0], [dnl
> +0
> +])
> +check appctl test/step start-right
> +AT_CHECK([appctl inc-engine/list-engines], [0], [left
> +right
> +])
> +AT_CHECK([appctl inc-engine/show-stats], [2], [ignore], [ignore])
> +AT_CHECK([appctl inc-engine/show-stats --engine=], [2], [ignore], [ignore])
> +AT_CHECK([appctl inc-engine/show-stats --engine=other], [2], [ignore],
> [ignore])
> +AT_CHECK([appctl inc-engine/show-stats --engine=left --engine=right],
> + [2], [ignore], [ignore])
> +AT_CHECK([appctl inc-engine/show-stats --engine=left sink --engine=right],
> + [2], [ignore], [ignore])
> +AT_CHECK([appctl inc-engine/list-stopwatches --engine=left --engine=right],
> + [2], [ignore], [ignore])
> +AT_CHECK([appctl inc-engine/list-stopwatches --engine=left --engine],
> + [2], [ignore], [ignore])
> +AT_CHECK([appctl inc-engine/clear-stats --engine=left extra], [2], [ignore],
> + [ignore])
> +AT_CHECK([appctl inc-engine/compute-log-timeout --engine=left -1], [2],
> + [ignore], [ignore])
> +check appctl inc-engine/compute-log-timeout --engine=left 25
> +check appctl inc-engine/recompute --engine=left
> +check appctl inc-engine/enable-stopwatch --engine=left sink
> +check appctl inc-engine/enable-stopwatch --engine=right sink
> +check appctl test/step run
> +AT_CHECK([appctl inc-engine/show-stats --engine=left sink recompute], [0],
> [dnl
> +1
> +])
> +AT_CHECK([appctl inc-engine/show-stats --engine=left sink cancel], [0], [dnl
> +1
> +])
> +AT_CHECK([appctl inc-engine/show-stats --engine=right sink compute], [0],
> [dnl
> +1
> +])
> +AT_CHECK([appctl inc-engine/show-stats --engine=right sink recompute], [0],
> [dnl
> +0
> +])
> +
> +AT_CHECK([appctl inc-engine/list-stopwatches --engine=left sink > left-keys])
> +AT_CHECK([appctl inc-engine/list-stopwatches --engine=right sink >
> right-keys])
> +AT_CHECK([head -1 left-keys | awk '{print $1, $2}'], [0], [dnl
> +sink ->
> +])
> +AT_CHECK([awk '{print $NF}' left-keys right-keys | sort -u | wc -l], [0],
> [dnl
> +4
> +])
> +AT_CHECK([awk '{print $NF}' left-keys right-keys | \
> + grep -cE '^@ip:[[0-9a-f]]+:[[0-9a-f]]+$'], [0], [dnl
> +4
> +])
> +left_sink=$(awk '/^sink / {print $NF}' left-keys)
> +right_sink=$(awk '/^sink / {print $NF}' right-keys)
> +left_handler=$(awk '/^test_engine_change_handler_with_long_name / {print
> $NF}' left-keys)
> +right_handler=$(awk '/^test_engine_change_handler_with_long_name / {print
> $NF}' right-keys)
> +AT_CHECK([appctl stopwatch/show sink], [2], [ignore], [ignore])
> +AT_CHECK([appctl stopwatch/show "$left_sink" | grep -i 'samples.*1'],
> + [0], [ignore])
> +AT_CHECK([appctl stopwatch/show "$right_sink" | grep -i 'samples.*0'],
> + [0], [ignore])
> +AT_CHECK([appctl stopwatch/show "$left_handler" | grep -i 'samples.*0'],
> + [0], [ignore])
> +AT_CHECK([appctl stopwatch/show "$right_handler" | grep -i 'samples.*1'],
> + [0], [ignore])
> +check appctl stopwatch/reset "$left_sink"
> +check appctl test/step sync
> +AT_CHECK([appctl stopwatch/show "$left_sink" | grep -i 'samples.*0'],
> + [0], [ignore])
> +AT_CHECK([appctl stopwatch/show "$right_handler" | grep -i 'samples.*1'],
> + [0], [ignore])
> +
> +check appctl inc-engine/clear-stats --engine=left
> +AT_CHECK([appctl inc-engine/show-stats --engine=left sink cancel], [0], [dnl
> +0
> +])
> +AT_CHECK([appctl inc-engine/show-stats --engine=right sink compute], [0],
> [dnl
> +1
> +])
> +check appctl inc-engine/disable-stopwatch --engine=right sink
> +check appctl test/step retire-left
> +AT_CHECK([appctl inc-engine/show-stats --engine=left], [2], [ignore],
> [ignore])
> +AT_CHECK([appctl inc-engine/show-stats sink compute], [0], [dnl
> +2
> +])
> +AT_CHECK([appctl inc-engine/show-stats sink --engine=right], [2], [ignore],
> + [ignore])
> +AT_CHECK([appctl inc-engine/list-stopwatches sink --engine=right], [2],
> + [ignore], [ignore])
> +AT_CHECK([appctl inc-engine/compute-log-timeout 25 --engine=right], [2],
> + [ignore], [ignore])
> +AT_CHECK([appctl stopwatch/show "$right_handler" | grep -i 'samples.*1'],
> + [0], [ignore])
> +check appctl test/step retire-right
> +check appctl inc-engine/list-engines
> +AT_CHECK([appctl inc-engine/recompute], [2], [ignore], [ignore])
> +
> +dnl Long engine names and labels share a prefix. Their compact keys must
> still
> +dnl be distinct and the two edges sharing one handler must use one timer.
> +dnl Reuse the retired engine objects with new names.
> +check appctl test/step start-long-graphs
> +long_left=shared_engine_name_more_than_thirty_one_bytes_left
> +long_right=shared_engine_name_more_than_thirty_one_bytes_right
> +AT_CHECK([appctl inc-engine/list-stopwatches "--engine=$long_left" >
> left-keys])
> +AT_CHECK([appctl inc-engine/list-stopwatches "--engine=$long_right" >
> right-keys])
> +AT_CHECK([awk '{print $NF}' left-keys right-keys | sort -u | wc -l], [0],
> [dnl
> +8
> +])
> +AT_CHECK([awk '{print $NF}' left-keys right-keys | \
> + grep -cE '^@ip:[[0-9a-f]]+:[[0-9a-f]]+$'], [0], [10
> +])
> +AT_CHECK([awk '{print $NF}' left-keys right-keys | awk 'length > 31'], [0],
> [])
> +left_source=$(awk '/^long_source_label_with_a_common_prefix_alpha / {print
> $NF}' left-keys)
> +right_source=$(awk '/^long_source_label_with_a_common_prefix_alpha / {print
> $NF}' right-keys)
> +left_handler=$(awk '/^test_engine_change_handler_with_long_name / {print
> $NF}' left-keys | sort -u)
> +right_handler=$(awk '/^test_engine_change_handler_with_long_name / {print
> $NF}' right-keys | sort -u)
> +
> +check appctl inc-engine/enable-stopwatch "--engine=$long_left"
> +check appctl test/step run-long-left
> +AT_CHECK([appctl stopwatch/show "$left_source" | grep -i 'samples.*1'],
> + [0], [ignore])
> +AT_CHECK([appctl stopwatch/show "$right_source" | grep -i 'samples.*0'],
> + [0], [ignore])
> +AT_CHECK([appctl stopwatch/show "$left_handler" | grep -i 'samples.*2'],
> + [0], [ignore])
> +AT_CHECK([appctl stopwatch/show "$right_handler" | grep -i 'samples.*0'],
> + [0], [ignore])
> +check appctl exit
> +wait "$engine_pid"
> +engine_status=$?
> +engine_pid=
> +check test "$engine_status" = 0
> +AT_CLEANUP
> diff --git a/tests/test-inc-proc-eng.c b/tests/test-inc-proc-eng.c
> new file mode 100644
> index 0000000000..4a40c084e5
> --- /dev/null
> +++ b/tests/test-inc-proc-eng.c
> @@ -0,0 +1,185 @@
> +/* Copyright (c) 2021, Red Hat, Inc.
> + *
> + * Licensed under the Apache License, Version 2.0 (the "License");
> + * you may not use this file except in compliance with the License.
> + * You may obtain a copy of the License at:
> + *
> + * http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + * See the License for the specific language governing permissions and
> + * limitations under the License.
> + */
> +
> +#include <config.h>
> +
> +#include <string.h>
> +
> +#include "lib/inc-proc-eng.h"
> +#include "openvswitch/poll-loop.h"
> +#include "stopwatch.h"
> +#include "tests/ovstest.h"
> +#include "unixctl.h"
> +#include "util.h"
> +
> +struct test_graph {
> + struct engine engine;
> + struct engine_node source;
> + struct engine_node second_source;
> + struct engine_node sink;
> + bool registered;
> +};
> +
> +static enum engine_node_state
> +test_engine_run(struct engine_node *node OVS_UNUSED, void *data OVS_UNUSED)
> +{
> + return EN_UPDATED;
> +}
> +
> +static enum engine_input_handler_result
> +test_engine_change(struct engine_node *node OVS_UNUSED, void *data
> OVS_UNUSED)
> +{
> + return EN_HANDLED_UPDATED;
> +}
> +
> +static void
> +test_graph_run(struct test_graph *graph, bool allow)
> +{
> + const struct engine_context ctx = { 0 };
> +
> + engine_init_run(&graph->engine);
> + engine_set_context(&ctx);
> + engine_run(&graph->engine, allow);
> + engine_set_context(NULL);
> +}
> +
> +static void
> +test_graph_cleanup(struct test_graph *graph)
> +{
> + engine_cleanup(&graph->engine);
> + graph->registered = false;
> +}
> +
> +static void
> +test_graph_init(struct test_graph *graph, const char *name, bool long_labels)
> +{
> + ovs_assert(!graph->registered);
> + graph->source = (struct engine_node) {
> + .name = "source",
> + .run = test_engine_run,
> + };
> + graph->sink = (struct engine_node) {
> + .name = "sink",
> + .run = test_engine_run,
> + .sb_write = true,
> + };
> +
> + const char *handler_label = "test_engine_change_handler_with_long_name";
> + engine_add_input_impl(&graph->sink, &graph->source, test_engine_change,
> + handler_label);
> + if (long_labels) {
> + graph->source.name = "long_source_label_with_a_common_prefix_alpha";
> + graph->second_source = (struct engine_node) {
> + .name = "long_source_label_with_a_common_prefix_beta",
> + .run = test_engine_run,
> + };
> + engine_add_input_impl(&graph->sink, &graph->second_source,
> + test_engine_change, handler_label);
> + }
> + engine_init(&graph->engine, name, &graph->sink, NULL);
> + ovs_assert(!engine_get_force_recompute(&graph->engine));
> + ovs_assert(!engine_canceled(&graph->engine));
> + ovs_assert(graph->engine.compute_log_timeout_msec == 500);
> + graph->registered = true;
> +}
> +
> +static void
> +test_engine_step(struct unixctl_conn *conn, int argc OVS_UNUSED,
> + const char *argv[], void *aux)
> +{
> + struct test_graph *graphs = aux;
> + struct test_graph *left = &graphs[0];
> + struct test_graph *right = &graphs[1];
> +
> + if (!strcmp(argv[1], "start-left")) {
> + test_graph_init(left, "left", false);
> + } else if (!strcmp(argv[1], "start-right")) {
> + test_graph_init(right, "right", false);
> + } else if (!strcmp(argv[1], "start-long-graphs")) {
> + test_graph_init(left,
> + "shared_engine_name_more_than_thirty_one_bytes_"
> + "left", true);
> + test_graph_init(right,
> + "shared_engine_name_more_than_thirty_one_bytes_"
> + "right", true);
> + } else if (!strcmp(argv[1], "run-long-left")) {
> + test_graph_run(left, true);
> + stopwatch_sync();
> + } else if (!strcmp(argv[1], "run")) {
> + ovs_assert(engine_get_force_recompute(&left->engine));
> + ovs_assert(!engine_get_force_recompute(&right->engine));
> + ovs_assert(left->engine.compute_log_timeout_msec == 25);
> + ovs_assert(right->engine.compute_log_timeout_msec == 500);
> + test_graph_run(left, false);
> + test_graph_run(right, true);
> + ovs_assert(engine_canceled(&left->engine));
> + ovs_assert(!engine_canceled(&right->engine));
> + test_graph_run(left, true);
> + ovs_assert(!engine_canceled(&left->engine));
> + stopwatch_sync();
> + } else if (!strcmp(argv[1], "sync")) {
> + stopwatch_sync();
> + } else if (!strcmp(argv[1], "retire-left")) {
> + test_graph_cleanup(left);
> + test_graph_run(right, true);
> + stopwatch_sync();
> + } else if (!strcmp(argv[1], "retire-right")) {
> + test_graph_cleanup(right);
> + } else {
> + OVS_NOT_REACHED();
> + }
> + unixctl_command_reply(conn, NULL);
> +}
> +
> +static void
> +test_engine_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
> + const char *argv[] OVS_UNUSED, void *aux)
> +{
> + bool *exiting = aux;
> +
> + *exiting = true;
> + unixctl_command_reply(conn, NULL);
> +}
> +
> +static void
> +test_inc_proc_eng_main(int argc, char *argv[])
> +{
> + ovs_assert(argc == 2);
> + struct test_graph graphs[2] = { 0 };
> + bool exiting = false;
> + struct unixctl_server *server;
> +
> + ovs_assert(!unixctl_server_create(argv[1], &server));
> + engine_infra_init();
> + unixctl_command_register("test/step", "STEP", 1, 1, test_engine_step,
> + graphs);
> + unixctl_command_register("exit", "", 0, 0, test_engine_exit, &exiting);
> +
> + while (!exiting) {
> + unixctl_server_run(server);
> + if (!exiting) {
> + unixctl_server_wait(server);
> + poll_block();
> + }
> + }
> + for (size_t i = 0; i < ARRAY_SIZE(graphs); i++) {
> + if (graphs[i].registered) {
> + test_graph_cleanup(&graphs[i]);
> + }
> + }
> + unixctl_server_destroy(server);
> +}
> +
> +OVSTEST_REGISTER("test-inc-proc-eng", test_inc_proc_eng_main);
> diff --git a/tests/testsuite.at b/tests/testsuite.at
> index 8ec5e239cd..178aa7ac23 100644
> --- a/tests/testsuite.at
> +++ b/tests/testsuite.at
> @@ -42,5 +42,6 @@ m4_include([tests/ovn-ipsec.at])
> m4_include([tests/ovn-vif-plug.at])
> m4_include([tests/ovn-util.at])
> m4_include([tests/ovn-br-controller.at])
> +m4_include([tests/ovn-inc-proc-eng.at])
> m4_include([tests/ovn-inc-proc-graph-dump.at])
> m4_include([tests/ovn-ctl.at])
_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev