Module ncs.cdb
CDB high level module.
This module implements a couple of classes for subscribing to CDB events.
Classes
class OperSubscriber (app=None, log=None, host='127.0.0.1', port=4569)
-
CDB Subscriber for oper data.
Use this class when subscribing on operational data. In all other means the behavior is the same as for Subscriber().
Initialize an OperSubscriber.
Ancestors
Inherited members
class Subscriber (app=None, log=None, host='127.0.0.1', port=4569, subtype=1, name='')
-
CDB Subscriber for config data.
Supports the pattern of collecting changes and then handle the changes in a separate thread. For each subscription point a handler object must be registered. The following methods will be called on the handler:
-
pre_iterate() (optional)
Called just before iteration starts, may return a state object which will be passed on to the iterate method. If not implemented, the state object will be None.
-
iterate(kp, op, oldv, newv, state) (mandatory)
Called for each change in the change set.
-
post_iterate(state) (optional)
Runs in a separate thread once iteration has finished and the subscription socket has been synced. Will receive the final state object from iterate() as an argument.
-
should_iterate() (optional)
Called to check if the subscriber wants to iterate. If this method returns False, neither pre_iterate() nor iterate() will be called. Can e.g. be used by HA slaves to skip iteration. If not implemented, pre_iterate() and iterate() will always be called.
-
should_post_iterate(state) (optional)
Called to determine whether post_iterate() should be called or not. It is recommended to implement this method to prevent the subscriber from calling post_iterate() when not needed. Should return True if post_iterate() should run, otherwise False. If not implemented, post_iterate() will always be called.
Example iterator object:
The same handler may be registered for multiple subscription points. In that case, pre_iterate() will only be called once, followed by iterate calls for all subscription points, and finally a single call to post_iterate().
Initialize a Subscriber.
Ancestors
Subclasses
Methods
def init(self)
-
Custom initialization.
Override this method to do custom initialization without needing to override init.
def register(self, path, iter_obj=None, iter_flags=1, priority=0, flags=0, subtype=None)
-
Register an iterator object at a specific path.
Setting 'iter_obj' to None will internally use 'self' as the iterator object which means that Subscriber needs to be sub-classed.
Operational and configuration subscriptions can be done on the same Subscriber, but in that case the notifications may be arbitrarily interleaved, including operational notifications arriving between different configuration notifications for the same transaction. If this is a problem, use separate Subscriber instances for operational and configuration subscriptions.
def run(self)
-
Main processing loop.
def start(self)
-
Start the subscriber.
def stop(self)
-
Stop the subscriber.
-
class TwoPhaseSubscriber (name, app=None, log=None, host='127.0.0.1', port=4569)
-
CDB Subscriber for config data with support for aborting transactions.
Subscriber that is capable of aborting transactions during the prepare phase of a transaction.
The following methods will be called on the handler in addition to the methods described in Subscriber:
-
prepare(kp, op, oldv, newv, state) (mandatory)
Called in the transaction prepare phase. If an exception occurs during the invocation of prepare the transaction is aborted.
-
cleanup(state) (optional)
Called after a prepare failure if available. Use to cleanup resources allocated by prepare.
-
abort(kp, op, oldv, newv, state) (mandatory)
Called if another subscriber aborts the transaction and this transaction has been prepared.
Methods are called in the following order:
- should_iterate -> pre_iterate ( -> cleanup, on exception)
- should_iterate -> iterate -> post_iterate
- should_iterate -> abort, if transaction is aborted by other subscriber
Initialize a Subscriber.
Ancestors
Methods
def register(self, path, iter_obj=None, iter_flags=1, priority=0, flags=0, subtype=None)
-
Register an iterator object at a specific path.
Setting 'iter_obj' to None will internally use 'self' as the iterator object which means that TwoPhaseSubscriber needs to be sub-classed.
Operational and configuration subscriptions can be done on the same TwoPhaseSubscriber, but in that case the notifications may be arbitrarily interleaved, including operational notifications arriving between different configuration notifications for the same transaction. If this is a problem, use separate TwoPhaseSubscriber instances for operational and configuration subscriptions.
Inherited members
-