Skip to content

Commit

Permalink
Optimization [Performance: commit takes long time with many devices](#…
Browse files Browse the repository at this point in the history
…154)

Avoid copy in compare/ diffs
  • Loading branch information
olofhagsand committed Dec 19, 2024
1 parent 05a2796 commit 84afd71
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 89 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Expected: January 2025

### New features

* Optimization [Performance: commit takes long time with many devices](https://github.com/clicon/clixon-controller/issues/154)
* Avoid copy in compare/diff
* Show device state
* New CLI show device state command
* See [CLI show state does not return device state (config false) data](https://github.com/clicon/clixon-controller/issues/143)
Expand Down
6 changes: 2 additions & 4 deletions src/controller_backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,13 @@ controller_reset(clixon_handle h,

if ((nsc = xml_nsctx_init(NULL, CONTROLLER_NAMESPACE)) == NULL)
goto done;
if ((ret = xmldb_get0(h, "running", YB_MODULE, nsc, xpath, 1, 0, &xtop, NULL, NULL)) < 0)
if ((ret = xmldb_get_cache(h, "running", YB_MODULE, &xtop, NULL, NULL)) < 0)
goto done;
if (ret == 0){
clixon_err(OE_DB, 0, "Error when reading from running_db, unknown error");
goto done;
}
if ((xse = xpath_first(xtop, 0, "processes/services/enabled")) != NULL){
if ((xse = xpath_first(xtop, 0, "%s", xpath)) != NULL){
if (strcmp(xml_body(xse), "true") == 0)
if (clixon_process_operation(h, ACTION_PROCESS, PROC_OP_START, 0) < 0)
goto done;
Expand All @@ -519,8 +519,6 @@ controller_reset(clixon_handle h,
done:
if (nsc)
cvec_free(nsc);
if (xtop)
xml_free(xtop);
return retval;
}

Expand Down
68 changes: 61 additions & 7 deletions src/controller_device_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ device_config_write(clixon_handle h,
return retval;
}

/*! Get local (cached) device datastore
/*! Get local copy device datastore
*
* @param[in] h Clixon handle
* @param[in] name Device name
Expand Down Expand Up @@ -774,6 +774,63 @@ device_config_read(clixon_handle h,
goto done;
}

/*! Get local cache device datastore
*
* @param[in] h Clixon handle
* @param[in] name Device name
* @param[in] config_type Device config tyoe
* @param[out] xdatap Device config XML (if retval=1)
* @param[out] cberr Error message (if retval=0)
* @retval 1 OK
* @retval 0 Failed (No such device tree)
* @retval -1 Error
*/
int
device_config_read_cache(clixon_handle h,
char *devname,
char *config_type,
cxobj **xdatap,
cbuf **cberr)
{
int retval = -1;
cbuf *cb = NULL;
char *db;
cxobj *xt = NULL;
cxobj *xroot;

if (devname == NULL || config_type == NULL){
clixon_err(OE_UNIX, EINVAL, "devname or config_type is NULL");
goto done;
}
if ((cb = cbuf_new()) == NULL){
clixon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
cprintf(cb, "device-%s-%s", devname, config_type);
db = cbuf_get(cb);
if (xmldb_get_cache(h, db, YB_MODULE, &xt, NULL, NULL) < 0)
goto done;
if ((xroot = xpath_first(xt, NULL, "devices/device/config")) == NULL){
if ((*cberr = cbuf_new()) == NULL){
clixon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
cprintf(*cberr, "No such device tree");
goto failed;
}
if (xdatap){
*xdatap = xroot;
}
retval = 1;
done:
if (cb)
cbuf_free(cb);
return retval;
failed:
retval = 0;
goto done;
}

/*! Get local (cached) device datastore
*
* @param[in] h Clixon handle
Expand Down Expand Up @@ -843,9 +900,9 @@ device_config_compare(clixon_handle h,
int eq;
int ret;

if ((ret = device_config_read(h, name, "SYNCED", &x0, &cberr)) < 0)
if ((ret = device_config_read_cache(h, name, "SYNCED", &x0, &cberr)) < 0)
goto done;
if (ret && (ret = device_config_read(h, name, "TRANSIENT", &x1, &cberr)) < 0)
if (ret && (ret = device_config_read_cache(h, name, "TRANSIENT", &x1, &cberr)) < 0)
goto done;
if (ret == 0){
if (device_close_connection(dh, "%s", cbuf_get(cberr)) < 0)
Expand All @@ -867,10 +924,6 @@ device_config_compare(clixon_handle h,
done:
if (cberr)
cbuf_free(cberr);
if (x0)
xml_free(x0);
if (x1)
xml_free(x1);
return retval;
closed:
retval = 0;
Expand Down Expand Up @@ -1680,6 +1733,7 @@ device_state_handler(clixon_handle h,
goto done;
}
cprintf(cb, "devices/device[name='%s']/config", name);
/* xt is used to put which requires a copy */
if (ct->ct_actions_type == AT_NONE){
if (xmldb_get0(h, ct->ct_sourcedb, YB_MODULE, NULL, cbuf_get(cb), 1, WITHDEFAULTS_EXPLICIT, &xt, NULL, NULL) < 0)
goto done;
Expand Down
1 change: 1 addition & 0 deletions src/controller_device_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ int device_state_timeout_register(device_handle ch);
int device_state_timeout_unregister(device_handle ch);
int device_state_set(device_handle dh, conn_state state);
int device_config_read(clixon_handle h, char *devname, char *config_type, cxobj **xrootp, cbuf **cberr);
int device_config_read_cache(clixon_handle h, char *devname, char *config_type, cxobj **xrootp, cbuf **cberr);
int device_config_write(clixon_handle h, char *name, char *config_type, cxobj *xdata, cbuf *cbret);
int device_state_handler(clixon_handle h, device_handle ch, int s, cxobj *xmsg);
int devices_statedata(clixon_handle h, cvec *nsc, char *xpath, cxobj *xstate);
Expand Down
Loading

0 comments on commit 84afd71

Please sign in to comment.