Class Cdb
- All Implemented Interfaces:
MountIdInterface,AutoCloseable
ConfD/NCS built in
XML database.
A connection is established upon a new instance of this class providing
a Socket as its argument to the constructor. An object of this
class is usually called a "cdb socket".
There are mainly two things that can be achieved through
a connection to CDB:
Starting a CDB Session - A CDB session is used to read configuration data, or read/write operational data. These are short-lived sessions that are established though a call to
startSession(). The entire configuration part of CDB is locked for writing while any CDB read session is active. Use thestartSession(CdbDBType)method to create a new session to read configuration data and read and write operational data.It is important to consider that a
CdbSessionhas one-to-one relationship with aCdbso before opening additionalCdbSessionone has to close previous opened.Cdb cdbReadSocket = new Cdb ( "MyReadCdbSocket", new Socket ( host ,port) ); // Start Cdb session against running CdbSession cdbSession = cdbReadSocket.startSession ( CdbDBType.CDB_RUNNING ); // read some values from CDB . cdbSession.endSession(); cdbReadSocket.getSocket().close();CDB Subscription - The subscription functionality makes it possible to receive events/notifications of CDB configuration changes.
newSubscription()method to create a new Cdb subscription to subscribe on CDB configuration changes.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanvoidclose()Closes the resources held by thisCdbsocket.getCompactionInfo(CdbDbfileType dbfile) Retrieves compaction information on a CDB fileRetrieve the currentCdbSessionstarted on thisCdbsocket.getMountId(ConfPath path) getName()Retrieve the name of thisCdbsocket.getPhase()Returns the start-phase CDB database is currently in.Retrieve the underlying socket used by thisCdbinstancegetTxId()Returns a CdbTxid object which represents the last transaction id from CDB.voidInitiates compaction on CDB files:voidInitiates compaction on a CDB file.Creates a new CDB Subscription.voidsetTimeout(int timeoutSecs) A timeout for cdb client actions can be specified via the config file.voidSets this Cdb and the session it creates to be used for Cdb data upgrades.voidsetUseForCdbUpgrade(List<ConfNamespace> removedNs) Sets this Cdb and the session it creates to be used for Cdb data upgrades.Starts a new CDB Session on an already establishedCdbagainstCdbDBType.CDB_RUNNINGdatastore withCdbLockType.LOCK_SESSIONlock.startSession(CdbDBType dbtype) Starts a new CDB Session on an already establishedCdb.startSession(CdbDBType dbtype, EnumSet<CdbLockType> lockflags) Starts a new CDB Session on an already establishedCdb.Similar tostartSession()but always returns a CdbUpgradeSession.startUpgradeSession(CdbDBType dbtype) Similar tostartSession(CdbDBType)but always returns a CdbUpgradeSession.startUpgradeSession(CdbDBType dbtype, EnumSet<CdbLockType> lockflags) Similar tostartSession(CdbDBType, EnumSet)but always returns a CdbUpgradeSession.toString()voidtriggerOperSubscriptions(int[] spointArray) Function similar totriggerOperSubscriptions(int[], EnumSet)with the difference that this function will never wait to acquire a lock and therefore fail and throw an Exception if Cdb is locked.voidtriggerOperSubscriptions(int[] spointArray, EnumSet<CdbLockType> lockflags) Function to trigger operational subscriptions in similar totriggerSubscriptions(int[]).voidtriggerSubscriptions(int[] spointArray) Triggers Cdb subscription for configuration data.voidThis call waits until start-phase 1 is completed and CDB is available.
-
Constructor Details
-
Cdb
Creates a new instance of aCdbsocket supplying a established open socket to ConfD/NCS daemon.The
nameparameter is just a string which may occur in certain error/debug messages i.e for example in the devel.log.When establishing a connection to ConfD/NCS the
MaapiSchemaswill be loaded once automatically by the library.If encrypted communication towards ConfD/NCS is desired, an environment variable "CONFD_IPC_ACCESS_FILE" or "NCS_IPC_ACCESS_FILE" need to be set. This variable is expected to point to a file containing a secret salt. example: export CONFD_IPC_ACCESS_FILE=./secret_file.txt An alternative to the environment variable is to set an java system property with the same name pointing to the file.
- Parameters:
name- name of theCdbinstancesocket- A socket connected to ConfD/NCS- Throws:
ConfException- If ConfD/NCS refuses to establish connection the reason could be obtained throughThrowable.getMessage()IOException- signals I/O exception on the underlying socket
-
Cdb
Creates a new instance of aCdbsocket supplying an address to the ConfD/NCS server.The
nameparameter is just a string which may occur in certain error/debug messages i.e for example in the devel.log.When establishing a connection to ConfD/NCS the
MaapiSchemaswill be loaded once automatically by the library.If encrypted communication towards ConfD/NCS is desired, an environment variable "CONFD_IPC_ACCESS_FILE" or "NCS_IPC_ACCESS_FILE" need to be set. This variable is expected to point to a file containing a secret salt. example: export CONFD_IPC_ACCESS_FILE=./secret_file.txt An alternative to the environment variable is to set an java system property with the same name pointing to the file.
- Parameters:
name- name of theCdbinstanceaddress- An adddres to connect to- Throws:
ConfException- If ConfD/NCS refuses to establish connection the reason could be obtained throughThrowable.getMessage()IOException- signals I/O exception on the underlying socket
-
-
Method Details
-
setUseForCdbUpgrade
public void setUseForCdbUpgrade()Sets this Cdb and the session it creates to be used for Cdb data upgrades. This is a specific startphase 0 use case. -
setUseForCdbUpgrade
Sets this Cdb and the session it creates to be used for Cdb data upgrades. This is a specific startphase 0 use case. If a yang model is completely removed it needs to be temporarily reinstalled for CDB to be able to refer to it.- Parameters:
removedNs- List of removed ConfNamespace used earlier
-
getName
Retrieve the name of thisCdbsocket.- Returns:
- The name of this Cdb socket instance
-
getSocket
Retrieve the underlying socket used by thisCdbinstance- Returns:
- The underlying socket
-
getCurrentSession
Retrieve the currentCdbSessionstarted on thisCdbsocket.- Returns:
- The current CdbSession started on this Cdb, null if no current CdbSession is started on this Cdb Socket.
-
startSession
Starts a new CDB Session on an already establishedCdbagainstCdbDBType.CDB_RUNNINGdatastore withCdbLockType.LOCK_SESSIONlock.- Throws:
IOExceptionConfException
-
startSession
Starts a new CDB Session on an already establishedCdb.The method starts a new session against the datastore specified by
dbtype.- Parameters:
dbtype- Datastore to establish session to- Throws:
IOExceptionConfException
-
startSession
public CdbSession startSession(CdbDBType dbtype, EnumSet<CdbLockType> lockflags) throws ConfException, IOException Starts a new CDB Session on an already establishedCdb.The method starts a new session against the datastore specified by
dbtypewith the locktype specified by thelockflags.While it is possible to use this method to start a session towards a configuration database type with no locking at all, this is strongly discouraged in general, since it means that even the values read in a single multi-value request (e.g.
getObject()) may be inconsistent with each other. However it is necessary to do this if we want to have a session open during semantic validation, see the "Semantic Validation" chapter in the User Guide - and in this particular case it is safe, since the transaction lock prevents changes to CDB during validation.- Parameters:
dbtype- Datastore to establish session tolockflags- EnumSet of CdbLockType flags- Throws:
IOExceptionConfException
-
startUpgradeSession
Similar tostartSession()but always returns a CdbUpgradeSession.- Throws:
ConfExceptionIOException
-
startUpgradeSession
Similar tostartSession(CdbDBType)but always returns a CdbUpgradeSession.- Parameters:
dbtype- Datastore to establish session to- Throws:
ConfExceptionIOException
-
startUpgradeSession
public CdbUpgradeSession startUpgradeSession(CdbDBType dbtype, EnumSet<CdbLockType> lockflags) throws ConfException, IOException Similar tostartSession(CdbDBType, EnumSet)but always returns a CdbUpgradeSession.- Parameters:
dbtype- Datastore to establish session tolockflags- EnumSet of CdbLockType flags- Throws:
ConfExceptionIOException
-
newSubscription
Creates a new CDB Subscription. -
waitStart
This call waits until start-phase 1 is completed and CDB is available.If CDB already is available (i.e. start-phase >= 1) the call returns immediately. This can be used by a CDB client who is not synchronously started and only wants to wait until it can read its configuration.
- Throws:
CdbException- Failed to waitStart for some reasonIOException- Failed to read/write on the underlying socketConfException
-
getPhase
Returns the start-phase CDB database is currently in.Also if CDB is in phase 0 and has initiated an init transaction (to load any init files) the flag
CdbPhase.FLAG_INITis set in the flags field correspondingly if an upgrade session is started theCdbPhase.FLAG_UPGRADEis set.- Throws:
CdbException- Failed to get phaseIOException- Failed to read/write cdb socketConfException
-
getTxId
Returns a CdbTxid object which represents the last transaction id from CDB.This function can be used if we are forced to reconnect to CDB. If the transaction id we read is identical to the last id we had prior to loosing the CDB sockets we don't have to reload our managed object data.
- Throws:
CdbException- Failed to get the last transaction idIOException- Failed to read/write cdb socketConfException
-
triggerSubscriptions
Triggers Cdb subscription for configuration data.This method makes it possible to trigger CDB subscriptions for configuration data even though the configuration has not been modified.
The caller will trigger all subscription points passed in the
spointArrayarray (or all subscribers if the array is of zero length) in priority order, and the call will not return until the last subscriber has calledCdbSubscription.sync(CdbSubscriptionSyncType).The call is blocking and doesn't return until all subscribers have acknowledged the notification. That means that it is not possible to use the method in a cdb subscriber thread since it would cause a deadlock.
The subscription notification generated by this "synthetic" trigger will seem like a regular subscription notification to a subscription client. As such, it is possible to use
diffIterateto traverse the changeset. CDB will make up this changeset in which all leafs in the configuration will appear to be set, and all list entries and presence containers will appear as if they are created.If the client is a two-phase subscriber, a prepare notification will first be delivered and if any client aborts this synthetic transaction further delivery of subscription notification is suspended and an exception is returned to the caller of
triggerSubscriptions. The error is the result of mapping theCONFD_ERRCODEas set by the aborting client. Note however that the configuration is still the way it is - so it is up to the caller oftriggerSubscriptionsto take appropriate action (for example: raising an alarm, restarting a subsystem, or even rebooting the system).- Parameters:
spointArray- subscription points to trigger- Throws:
ConfException- If one or more subscription ids is passed in thesubidsarray that are not valid, anCdbExceptionwill be thrown with error code (getErrorCode) set toCONFD_ERR_PROTOUSAGEwill be returned and no subscriptions will be triggeredIOException- On I/O error.
-
triggerOperSubscriptions
Function similar totriggerOperSubscriptions(int[], EnumSet)with the difference that this function will never wait to acquire a lock and therefore fail and throw an Exception if Cdb is locked.- Parameters:
spointArray- int[] array of subscription points or null- Throws:
ConfExceptionIOException
-
triggerOperSubscriptions
public void triggerOperSubscriptions(int[] spointArray, EnumSet<CdbLockType> lockflags) throws ConfException, IOException Function to trigger operational subscriptions in similar totriggerSubscriptions(int[]). The caller will trigger all subscription points passed in the spointArray (or all operational data subscribers if this array is null), and the call will not return until the last subscriber has calledCdbSubscription.sync(CdbSubscriptionSyncType). Since the generation of subscription notifications for operational data requires that the subscription lock is taken, this function implicitly attempts to take a "global" subscription lock. If the subscription lock is already taken, the function will by default return an Exception. o instead have it wait until the lock becomes available, EnumSet.of(CdbLockType.LOCK_WAIT) can be passed as lockflags parameter.- Parameters:
spointArray- int[] array of subscription points or nulllockflags- null or EnumSet.of(CdbLockType.LOCK_WAIT)- Throws:
ConfExceptionIOException
-
setTimeout
A timeout for cdb client actions can be specified via the config file. This function can be used to dynamically extend (or shorten) the timeout for the current action. This function can be called either for a Cdb instance used for a cdb subscription or a Cdb instance used in an CdbSession. The timeout is given in seconds from the point in time when the function is called. Note The timeout for subscription delivery is common for all the subscribers receiving notifications at a given priority. Thus calling the function during subscription delivery changes the timeout for all the subscribers that are currently processing notifications.- Parameters:
timeoutSecs- timeout in seconds- Throws:
ConfExceptionIOException
-
toString
-
close
Closes the resources held by thisCdbsocket.- Specified by:
closein interfaceAutoCloseable- Throws:
IOException- If I/O Error when closing resources held by thisCdbsocket
-
getMountId
- Specified by:
getMountIdin interfaceMountIdInterface- Throws:
ConfException
-
acceptTagPath
public boolean acceptTagPath()- Specified by:
acceptTagPathin interfaceMountIdInterface
-
initiateCompaction
Initiates compaction on CDB files:The method initiates compaction on all CDB files defined as
CdbDbfileType.- Throws:
CdbException- Failed to initiate the compactionIOException- Failed to read/write on the cdb socketConfException
-
initiateDbfileCompaction
Initiates compaction on a CDB file.The method initiates compaction on the CDB file specified by
dbfile.- Parameters:
dbfile- CDB file to compact- Throws:
CdbException- Failed to initiate the compactionIOException- Failed to read/write on the cdb socketConfException
-
getCompactionInfo
Retrieves compaction information on a CDB fileThe method retrieves compaction information on the CDB file specified by
dbfile.- Parameters:
dbfile- CDB file to collect info- Throws:
CdbException- Failed to get compaction infoIOException- Failed to read/write on the cdb socketConfException
-