* xml-stats moved from clixon-config.yang as state data to an rpc datastatsin clixon-lib.yang

* Two new plugin callbacks added
  * ca_daemon: Called just after a server has "daemonized", ie put in background.
  * ca_trans_commit_done: Called when all plugin commits have been done.
This commit is contained in:
Olof hagsand 2020-04-23 22:58:57 +02:00
parent 65806f1ef2
commit 1c99bd6a9b
20 changed files with 290 additions and 112 deletions

View file

@ -269,67 +269,26 @@ clixon_stats_get_db(clicon_handle h,
char *dbname,
cbuf *cb)
{
int retval = -1;
cxobj *xt = NULL;
uint64_t nr = 0;
size_t sz = 0;
db_elmnt *de = NULL;
int retval = -1;
cxobj *xt = NULL;
uint64_t nr = 0;
size_t sz = 0;
db_elmnt *de = NULL;
/* This is the db cache */
if ((de = clicon_db_elmnt_get(h, dbname)) != NULL)
xt = de->de_xml;
xml_stats(xt, &nr, &sz);
cprintf(cb, "<datastore><name>%s</name><nr>%" PRIu64 "</nr>"
"<size>%" PRIu64 "</size></datastore>",
dbname, nr, sz);
retval = 0;
return retval;
}
/*! Get clixon stats
* @param[in] h Clicon handle
* @param[in] yspec Yang spec
* @param[in] xpath XML Xpath
* @param[in] nsc XML Namespace context for xpath
* @param[in,out] xret Existing XML tree, merge x into this
* @retval 0 OK
* @retval -1 Error
*/
int
clixon_stats_get(clicon_handle h,
yang_stmt *yspec,
char *xpath,
cvec *nsc,
cxobj **xret)
{
int retval = -1;
cbuf *cb = NULL;
uint64_t nr;
int ret;
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
if ((de = clicon_db_elmnt_get(h, dbname)) == NULL ||
(xt = de->de_xml) == NULL){
cprintf(cb, "<datastore><name>%s</name><nr>0</nr><size>0</size></datastore>", dbname);
}
cprintf(cb, "<clixon-stats xmlns=\"%s\">", CLIXON_CONF_NS);
nr=0;
xml_stats_global(&nr);
cprintf(cb, "<global><xmlnr>%" PRIu64 "</xmlnr></global>", nr);
clixon_stats_get_db(h, "running", cb);
clixon_stats_get_db(h, "candidate", cb);
clixon_stats_get_db(h, "startup", cb);
cprintf(cb, "</clixon-stats>");
if ((ret = clixon_xml_parse_string(cbuf_get(cb), YB_MODULE, yspec, xret, NULL)) < 0)
goto done;
if (ret == 0){
clicon_err(OE_XML, EINVAL, "Internal error");
goto done;
else{
if (xml_stats(xt, &nr, &sz) < 0)
goto done;
cprintf(cb, "<datastore><name>%s</name><nr>%" PRIu64 "</nr>"
"<size>%" PRIu64 "</size></datastore>",
dbname, nr, sz);
}
retval = 0;
done:
clicon_debug(1, "%s %d", __FUNCTION__, retval);
if (cb)
cbuf_free(cb);
return retval;
}
@ -408,11 +367,6 @@ client_statedata(clicon_handle h,
if (ret == 0)
goto fail;
}
/* Clixon-config has a state data, if yang is present */
if (yang_find(yspec, Y_MODULE, "clixon-config") != NULL){
if (clixon_stats_get(h, yspec, xpath, nsc, xret) < 0)
goto done;
}
if ((ret = clixon_plugin_statedata(h, yspec, nsc, xpath, xret)) < 0)
goto done;
if (ret == 0)
@ -1441,6 +1395,41 @@ from_client_ping(clicon_handle h,
return 0;
}
/*! Check liveness of backend daemon, just send a reply
* @param[in] h Clicon handle
* @param[in] xe Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
* @param[in] regarg User argument given at rpc_callback_register()
* @retval 0 OK
* @retval -1 Error
*/
static int
from_client_datastats(clicon_handle h,
cxobj *xe,
cbuf *cbret,
void *arg,
void *regarg)
{
int retval = -1;
uint64_t nr;
cprintf(cbret, "<rpc-reply>");
nr=0;
xml_stats_global(&nr);
cprintf(cbret, "<global><xmlnr>%" PRIu64 "</xmlnr></global>", nr);
if (clixon_stats_get_db(h, "running", cbret) < 0)
goto done;
if (clixon_stats_get_db(h, "candidate", cbret) < 0)
goto done;
if (clixon_stats_get_db(h, "startup", cbret) < 0)
goto done;
cprintf(cbret, "</rpc-reply>");
retval = 0;
done:
return retval;
}
/*! Verify nacm user with peer uid credentials
* @param[in] mode Peer credential mode: none, exact or except
* @param[in] peername Peer username if any
@ -1797,10 +1786,13 @@ backend_rpc_init(clicon_handle h)
goto done;
/* Clixon RPC */
if (rpc_callback_register(h, from_client_debug, NULL,
"http://clicon.org/lib", "debug") < 0)
CLIXON_LIB_NS, "debug") < 0)
goto done;
if (rpc_callback_register(h, from_client_ping, NULL,
"http://clicon.org/lib", "ping") < 0)
CLIXON_LIB_NS, "ping") < 0)
goto done;
if (rpc_callback_register(h, from_client_datastats, NULL,
CLIXON_LIB_NS, "datastats") < 0)
goto done;
retval =0;
done:

View file

@ -349,6 +349,9 @@ startup_commit(clicon_handle h,
/* 8. Call plugin transaction commit callbacks */
if (plugin_transaction_commit(h, td) < 0)
goto done;
/* After commit, make a post-commit call (sure that all plugins have committed) */
if (plugin_transaction_commit_done(h, td) < 0)
goto done;
/* Clear cached trees from default values and marking */
if (xmldb_get0_clear(h, td->td_target) < 0)
goto done;
@ -538,6 +541,9 @@ candidate_commit(clicon_handle h,
/* 7. Call plugin transaction commit callbacks */
if (plugin_transaction_commit(h, td) < 0)
goto done;
/* After commit, make a post-commit call (sure that all plugins have committed) */
if (plugin_transaction_commit_done(h, td) < 0)
goto done;
/* Clear cached trees from default values and marking */
if (xmldb_get0_clear(h, td->td_target) < 0)

View file

@ -885,6 +885,10 @@ main(int argc,
}
}
/* Call plugin callbacks when in background and before dropped privileges */
if (clixon_plugin_daemon(h) < 0)
goto done;
/* Write pid-file */
if ((pid = pidfile_write(pidfile)) < 0)
goto done;

View file

@ -92,6 +92,32 @@ clixon_plugin_reset(clicon_handle h,
return retval;
}
/*! Request plugins to reset system state
* The system 'state' should be the same as the contents of running_db
* @param[in] h Clicon handle
* @param[in] db Name of database
* @retval 0 OK
* @retval -1 Error
*/
int
clixon_plugin_daemon(clicon_handle h)
{
clixon_plugin *cp = NULL;
plgdaemon_t *daemonfn; /* Plugin auth */
int retval = 1;
while ((cp = clixon_plugin_each(h, cp)) != NULL) {
if ((daemonfn = cp->cp_api.ca_daemon) == NULL)
continue;
if ((retval = daemonfn(h)) < 0) {
clicon_debug(1, "plugin_daemon() failed");
return -1;
}
break;
}
return retval;
}
/*! Go through all backend statedata callbacks and collect state data
* This is internal system call, plugin is invoked (does not call) this function
* Backend plugins can register
@ -345,6 +371,36 @@ plugin_transaction_commit(clicon_handle h,
return retval;
}
/*! Call transaction_commit_done callbacks in all backend plugins
* @param[in] h Clicon handle
* @param[in] td Transaction data
* @retval 0 OK
* @retval -1 Error: one of the plugin callbacks returned error
* @note no revert is done
*/
int
plugin_transaction_commit_done(clicon_handle h,
transaction_data_t *td)
{
int retval = 0;
clixon_plugin *cp = NULL;
trans_cb_t *fn;
int i=0;
while ((cp = clixon_plugin_each(h, cp)) != NULL) {
i++;
if ((fn = cp->cp_api.ca_trans_commit_done) == NULL)
continue;
if ((retval = fn(h, (transaction_data)td)) < 0){
if (!clicon_errno) /* sanity: log if clicon_err() is not called ! */
clicon_log(LOG_NOTICE, "%s: Plugin '%s' trans_commit_done callback does not make clicon_err call on error",
__FUNCTION__, cp->cp_name);
break;
}
}
return retval;
}
/*! Call transaction_end() in all plugins after a successful commit.
* @param[in] h Clicon handle
* @param[in] td Transaction data

View file

@ -2,7 +2,9 @@
*
***** BEGIN LICENSE BLOCK *****
Copyright (C) 2009-2019 Olof Hagsand and Benny Holmgren
Copyright (C) 2009-2016 Olof Hagsand and Benny Holmgren
Copyright (C) 2017-2019 Olof Hagsand
Copyright (C) 2020 Olof Hagsand and Rubicon Communications, LLC(Netgate)
This file is part of CLIXON.
@ -69,6 +71,8 @@ typedef struct {
*/
int clixon_plugin_reset(clicon_handle h, char *db);
int clixon_plugin_daemon(clicon_handle h);
int clixon_plugin_statedata(clicon_handle h, yang_stmt *yspec, cvec *nsc,
char *xpath, cxobj **xtop);
transaction_data_t * transaction_new(void);
@ -78,6 +82,7 @@ int plugin_transaction_begin(clicon_handle h, transaction_data_t *td);
int plugin_transaction_validate(clicon_handle h, transaction_data_t *td);
int plugin_transaction_complete(clicon_handle h, transaction_data_t *td);
int plugin_transaction_commit(clicon_handle h, transaction_data_t *td);
int plugin_transaction_commit_done(clicon_handle h, transaction_data_t *td);
int plugin_transaction_end(clicon_handle h, transaction_data_t *td);
int plugin_transaction_abort(clicon_handle h, transaction_data_t *td);