$darkmode
Elektra 0.11.0
Change Tracking

Currently, a range of Elektra plugins are implementing some sort of change tracking for configuration data. This includes, but is not limited to, the internalnotification and dbus plugins. In the near future, Elektra shall also be extended with session recording.

KDB itself also has some rudimentary change tracking (via the keyNeedsSync flag) to determine whether kdbSet needs to actually do something.

These competing change tracking implementations create multiple problems:

  1. pointless waste of resources, as data is duplicated in each plugin,
  2. multiplication of code, generating a maintenance burden.
  3. various subtle differences in change tracking behavior, e.g., kdbSet might write a config file but notification is not sent out
  4. the current approach to change tracking in plugins is fragile, which is outlined in a separate decision about valid kdbGet/kdbSet sequences.

For KeySet we need to track which of the keys:

For Key we need to track:

Change tracking must:

We only want to track changes that are done by the user, not changes that are done by plugins. I.e. the scope of change tracking is on what happens outside of kdbGet and kdbSet.

The library libelektra-core must be kept minimal.

We already store which keys have been returned by kdbGet for each backend within KDB. Currently, those are shallow copies. To be useful for changetracking, we need to perform deep copies instead. As keys and keysets are already utilizing copy-on-write, this would not add too much memory overhead.

A problem with this approach is that the internally stored keys are recreated as new instances every time kdbGet is called.

We already decided that we want to have an internal deep-duped keyset of all the keys we returned. See internal cache decision.

The difference to backendData->keys is that this cache is not recreated each time kdbGet is called.

Have a global keyset with deep-duped keys that is purely used for changetracking and nothing else.

When something changes for the first time, store the original value as a metakey to every key. Not yet clear how we handle changes to metadata then. Also not really possible for keysets.

The idea here is that we extend the KeySet and Key structs with additional fields.

The tracking itself would be done within the ks* and key* methods, after checking if it is enabled. It would also transparently work for metadata, as metadata itself is implemented as a keyset with keys.

Downsides of this approach:

Implement all the logic for changetracking directly within libelektra-kdb.

Implement changetracking as a hooks plugin that will be called within kdbGet and kdbSet accordingly.

The following hooks will be needed:

As every hook plugin can define its own contract, in theory all storage forms mentioned in the previous chapter should be possible to implement. We could just point the plugin to backendsData->keys or the internal cache if we go down that route.

The API should be usable by plugins and by applications utilizing Elektra. It does not matter whether the changetracking is implemented as part of libelektra-kdb or as a separate plugin. The API may look something like this:

bool elektraChangeTrackingIsEnabled (KDB * kdb);
ChangeTrackingContext * elektraChangeTrackingGetContext (KDB * kdb, Key * parentKey);
KeySet * elektraChangeTrackingGetAddedKeys (ChangeTrackingContext * context);
KeySet * elektraChangeTrackingGetRemovedKeys (ChangeTrackingContext * context);
KeySet * elektraChangeTrackingGetModifiedKeys (ChangeTrackingContext * context); // Returns old keys (pre-modification)
bool elektraChangeTrackingValueChanged (ChangeTrackingContext * context, Key * key);
bool elektraChangeTrackingMetaChanged (ChangeTrackingContext * context, Key * key);
KeySet * elektraChangeTrackingGetAddedMetaKeys (ChangeTrackingContext * context, Key * key);
KeySet * elektraChangeTrackingGetRemovedMetaKeys (ChangeTrackingContext * context, Key * key);
KeySet * elektraChangeTrackingGetModifiedMetaKeys (ChangeTrackingContext * context, Key * key); // Returns old meta keys (pre-modification)

This solution only makes sense if changetrackig is implemented as part of a seperate plugin. It will be a bit challenging to use for applications, as it would require that applications have access to the plugin contracts.

The changetracking plugin needs to export at least functions for the following things in its contract:

Plugin and application developers can declare that changetracking is required via a contract. Store deep-duped copy-on-write returned keys in a separate keyset, which we might also use as internal cache. The whole changetracking logic lives within libelektra-kdb. We provide an API for developers with libelektra-kdb.

This decision meets all the constraints.

We are not altering the behavior of already existing functions, so everything is transparent for applications using the public Elektra API. As changetracking is an opt-in feature, it will create zero runtime overhead and negligible memory overhead in usecases where it is not needed. There is only a single implementation and storage, so no duplication for each plugin that wants change tracking. As we are storing our own tracking data, all sequences of kdbGet and kdbSet should work.