To set a durability guarantee, use the
Durability
class. When you do this,
you must provide three pieces of information:
The acknowledgment policy.
A synchronization policy at the master node.
A synchronization policy at the replica nodes.
The combination of policies that you use is driven by how sensitive your application might be to potential data loss, and by your write performance requirements.
For example, the fastest possible write performance can be achieved through a durability policy that requires:
No acknowledgments.
NO_SYNC at the master.
NO_SYNC at the replicas.
However, this durability policy also leaves your data with the greatest risk of loss due to application or machine failure between the time the operation returns and the time when the data is written to stable storage.
On the other hand, if you want the highest possible durability guarantee, you can use:
All replicas must acknowledge the write operation.
SYNC at the master.
SYNC at the replicas.
Of course, this also results in the slowest possible write performance.
Most commonly, durability policies attempt to strike a balance between write performance and data durability guarantees. For example:
Simple majority of replicas must acknowledge the write.
SYNC at the master.
NO_SYNC at the replicas.
Note that you can set a default durability policy for your
Store
handle, but you can also
override the policy on a per-operation basis for those
situations where some of your data need not be as durable (or
needs to be MORE durable) than the default.
For example, suppose you want an intermediate durability policy for most of your data, but sometimes you have transient or easily re-created data whose durability really is not very important. Then you would do something like this:
First, set the default durability policy for the
Store
handle:
from nosqldb import Durability ### Constants needed for Durability from nosqldb import ONDB_AP_NONE from nosqldb import ONDB_AP_SIMPLE_MAJORITY from nosqldb import ONDB_MASTER_SYNC from nosqldb import ONDB_REPLICA_SYNC from nosqldb import ONDB_REPLICA_ACK from nosqldb import ONDB_SP_SYNC from nosqldb import ONDB_SP_NO_SYNC ## For Durability, could use one of ## COMMIT_SYNC, COMMIT_NO_SYNC, or ## COMMIT_WRITE_NO_SYNC from nosqldb import DurabilityException from nosqldb import Factory from nosqldb import IllegalArgumentException from nosqldb import ProxyConfig from nosqldb import RequestTimeoutException from nosqldb import Row from nosqldb import StoreConfig from nosqldb import WriteOptions ##### Constants needed for the write options from nosqldb import ONDB_DURABILITY from nosqldb import ONDB_TIMEOUT ... # locations where our store and proxy can be found kvlite = 'localhost:5000' proxy = 'localhost:7010' ... # configure and open the store def open_store(): dg = Durability({ONDB_MASTER_SYNC : ONDB_SP_SYNC, ONDB_REPLICA_SYNC : ONDB_SP_NO_SYNC, ONDB_REPLICA_ACK : ONDB_AP_SIMPLE_MAJORITY}) kvstoreconfig = StoreConfig('kvstore', [kvlite]) kvstoreconfig.set_durability(dg) return Factory.open(proxy, kvstoreconfig)
In another part of your code, for some unusual write operations, you might then want to relax the durability guarantee so as to speed up the write performance for those specific write operations:
def do_store_ops(store): row_d = { 'item' : 'bolts', 'description' : "Hex head, stainless", 'count' : 5, 'percentage' : 0.2173913} row = Row(row_d) ## Create the write options dur = Durability({ONDB_MASTER_SYNC : ONDB_SP_NO_SYNC, ONDB_REPLICA_SYNC : ONDB_SP_NO_SYNC, ONDB_REPLICA_ACK : ONDB_AP_NONE}) wo = WriteOptions({ONDB_DURABILITY : dur, ONDB_TIMEOUT : 600}) try: store.put("myTable", row, wo) logging.debug("Store write succeeded.") except IllegalArgumentException, iae: logging.error("Could not write table.") logging.error(iae.message) except DurabilityException, de: logging.error("Could not write table. Durability failure.") logging.error(de.message) except RequestTimeoutException, rte: logging.error("Could not write table. Exceeded timeout.") logging.error(rte.message)