Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xmldb_get0 returns invalid candidate on startup transaction callbacks #126

Closed
shmuelhazan opened this issue Aug 11, 2020 · 3 comments
Closed
Labels

Comments

@shmuelhazan
Copy link
Contributor

shmuelhazan commented Aug 11, 2020

Description

xmldb_get0 with "candidate" as db will yield invalid results if it has been called on a validate/commit callback in a context of the first backend transaction.

Instead of returning the startup config as expected, it will return the candidate state as it was on the previous backend run.

Example

Compile the following simple backend plugin:

#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <syslog.h>
#include <unistd.h>

/* clicon */
#include "cligen/cligen.h"

/* Clicon library functions. */
#include "clixon/clixon.h"

/* These include signatures for plugin and transaction callbacks. */
#include "clixon/clixon_backend.h"

void dump_datastore(clicon_handle h, const char *datastore) {
  cxobj *obj;
  ret = xmldb_get0(h, datastore, NULL, NULL, 0, &obj, NULL);

  clicon_xml2file(stdout, obj, 0, 1);
}

int main_validate(clicon_handle h, transaction_data td) {
  clicon_debug(1, "%s", __FUNCTION__);
  transaction_log(h, td, 1, __FUNCTION__);
  dump_datastore(h, "candidate");
  return 0;
}

/*! Example callback:
 *  This is called on commit. Identify modifications and adjust machine state
 */
int main_commit(clicon_handle h, transaction_data td) {
  clicon_debug(1, "%s", __FUNCTION__);
  transaction_log(h, td, 1, __FUNCTION__);
  dump_datastore(h, "candidate");

  return 0;
}

clixon_plugin_api *clixon_plugin_init(clicon_handle h);

static clixon_plugin_api api = {
    "core",             /* name */
    clixon_plugin_init, /* init - must be called clixon_plugin_init */
    .ca_trans_validate = main_validate, /* trans validate */
    .ca_trans_commit = main_commit,     /* trans commit */
};
clixon_plugin_api *clixon_plugin_init(clicon_handle h) { return &api; }

Open clixon cli, write:

set parameter A value B
commit
copy running startup
set parameter A value A

Restart backend.

Expected result:

Aug 11 07:03:20: transaction_log 0 main_validate add: <table xmlns="urn:example:clixon"><parameter><name>A</name><value>B</value></parameter></table>
<config>
   <table xmlns="urn:example:clixon">
      <parameter>
         <name>A</name>
         <value>B</value>
      </parameter>
   </table>
</config>
Aug 11 07:03:20: transaction_log 0 main_commit add: <table xmlns="urn:example:clixon"><parameter><name>A</name><value>B</value></parameter></table>
<config>
   <table xmlns="urn:example:clixon">
      <parameter>
         <name>A</name>
         <value>B</value>
      </parameter>
   </table>
</config>

Actual:

Aug 11 07:03:20: transaction_log 0 main_validate add: <table xmlns="urn:example:clixon"><parameter><name>A</name><value>B</value></parameter></table>
<config>
   <table xmlns="urn:example:clixon">
      <parameter>
         <name>A</name>
         <value>A</value>
      </parameter>
   </table>
</config>
Aug 11 07:03:20: transaction_log 0 main_commit add: <table xmlns="urn:example:clixon"><parameter><name>A</name><value>B</value></parameter></table>
<config>
   <table xmlns="urn:example:clixon">
      <parameter>
         <name>A</name>
         <value>A</value>
      </parameter>
   </table>
</config>

Notes

Looks like simply clearing the candidate database on plugin init solves the issue.

olofhagsand added a commit that referenced this issue Sep 7, 2020
…allbacks #126](#126). Always clear candidate-db before db initialization.
@olofhagsand
Copy link
Member

Fixed by always clearing db on startup.
Actually, this wasnt really an error, since candidate is not really a part of the startup mechanism, but I agree it could cause side-effect problems that candidate retained state from the previous run.
Please verify?

@dima1308
Copy link

dima1308 commented Sep 9, 2020

@shmuelhazan did you verified it?

@shmuelhazan
Copy link
Contributor Author

Looking good now, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants