ncs.cdb Module
CDB high level module.
This module implements a couple of classes for subscribing to CDB events.
Classes
class OperSubscriber
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().
OperSubscriber(app=None, log=None, host='127.0.0.1', port=4569, path=None)
Initialize an OperSubscriber.
Members:
class Subscriber
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 secondary nodes 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:
class MyIter(object):
def pre_iterate(self):
return []
def iterate(self, kp, op, oldv, newv, state):
if op is ncs.MOP_VALUE_SET:
state.append(newv)
return ncs.ITER_RECURSE
def post_iterate(self, state):
for item in state:
print(item)
def should_post_iterate(self, state):
return state != []
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().
Subscriber(app=None, log=None, host='127.0.0.1', port=4569, subtype=1, name='', path=None)
Initialize a Subscriber.
Members:
class TwoPhaseSubscriber
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
TwoPhaseSubscriber(name, app=None, log=None, host='127.0.0.1', port=4569, path=None)
Members:
Predefined Values
A_CDB = 1
DATA_SOCKET = 2
DONE_OPERATIONAL = 4
DONE_PRIORITY = 1
DONE_SOCKET = 2
DONE_TRANSACTION = 3
FLAG_INIT = 1
FLAG_UPGRADE = 2
GET_MODS_CLI_NO_BACKQUOTES = 8
GET_MODS_INCLUDE_LISTS = 1
GET_MODS_INCLUDE_MOVES = 16
GET_MODS_REVERSE = 2
GET_MODS_SUPPRESS_DEFAULTS = 4
GET_MODS_WANT_ANCESTOR_DELETE = 32
LOCK_PARTIAL = 8
LOCK_REQUEST = 4
LOCK_SESSION = 2
LOCK_WAIT = 1
OPERATIONAL = 3
O_CDB = 2
PRE_COMMIT_RUNNING = 4
READ_COMMITTED = 16
READ_SOCKET = 0
RUNNING = 1
STARTUP = 2
SUBSCRIPTION_SOCKET = 1
SUB_ABORT = 3
SUB_COMMIT = 2
SUB_FLAG_HA_IS_SECONDARY = 16
SUB_FLAG_HA_IS_SLAVE = 16
SUB_FLAG_HA_SYNC = 8
SUB_FLAG_IS_LAST = 1
SUB_FLAG_REVERT = 4
SUB_FLAG_TRIGGER = 2
SUB_OPER = 4
SUB_OPERATIONAL = 3
SUB_PREPARE = 1
SUB_RUNNING = 1
SUB_RUNNING_TWOPHASE = 2
SUB_WANT_ABORT_ON_ABORT = 1
S_CDB = 3
Last updated
Was this helpful?