### Install PySyncObj with pip Source: https://github.com/bakwc/pysyncobj/blob/master/README.md Install the PySyncObj package using pip. ```bash pip install pysyncobj ``` -------------------------------- ### Install cryptography for encryption Source: https://github.com/bakwc/pysyncobj/blob/master/README.md Optionally install the cryptography package to enable encryption support. ```bash pip install cryptography ``` -------------------------------- ### Simple KVStorage class with @replicated set and get methods Source: https://github.com/bakwc/pysyncobj/wiki/Zero-downtime-deploy Webpage shows a Python class named KVStorage that subclasses SyncObj. It contains an initializer and @replicated set method plus a non-replicated get method with return self.__data.get(key, None). ```python class KVStorage(SyncObj): def __init__(self, selfAddress, partnerAddrs): super(KVStorage, self).__init__(selfAddress, partnerAddrs) self.__data = {} @replicated def set(self, key, value): self.__data[key] = value def get(self, key): return self.__data.get(key, None) ``` -------------------------------- ### Use ReplCounter and ReplDict with SyncObj Source: https://github.com/bakwc/pysyncobj/blob/master/README.md Shows how to use ReplCounter and ReplDict with a SyncObj instance. The 'sync=True' parameter makes operations blocking, while dict assignment is asynchronous. The example sets and increments counters and sets dictionary values. ```python from pysyncobj import SyncObj from pysyncobj.batteries import ReplCounter, ReplDict counter1 = ReplCounter() counter2 = ReplCounter() dict1 = ReplDict() syncObj = SyncObj('serverA:4321', ['serverB:4321', 'serverC:4321'], consumers=[counter1, counter2, dict1]) counter1.set(42, sync=True) # set initial value to 42, 'sync' means that operation is blocking counter1.add(10, sync=True) # add 10 to counter value counter2.inc(sync=True) # increment counter value by one dict1.set('testKey1', 'testValue1', sync=True) dict1['testKey2'] = 'testValue2' # this is basically the same as previous, but asynchronous (non-blocking) print(counter1, counter2, dict1['testKey1'], dict1.get('testKey2')) ``` -------------------------------- ### Update without migration: backward-compatible set and get with version checking Source: https://github.com/bakwc/pysyncobj/wiki/Zero-downtime-deploy Adds a new set method with ver=2 that handles both old format (non-tuple values) and new tuple format. The get method returns the first element of a tuple if present, or the value itself for backward compatibility. ```python class KVStorage(SyncObj): def __init__(self, selfAddress, partnerAddrs): super(KVStorage, self).__init__(selfAddress, partnerAddrs) self.__data = {} @replicated def set(self, key, value): self.__data[key] = value @replicated(ver=2) def set(self, key, value): currData = self.__data.get(key) if isinstance(currData, tuple): self.__data[key] = (value, currData[1] + 1) elif currData is not None: self.__data[key] = (value, 2) else: self.__data[key] = (value, 1) @replicated(ver=1) def pop(self, key): self.__data.pop(key, None) def get(self, key): currData = self.__data.get(key, None) if isinstance(currData, tuple): return currData[0] return currData ``` -------------------------------- ### Simple update: add @replicated(ver=1) pop method to KVStorage Source: https://github.com/bakwc/pysyncobj/wiki/Zero-downtime-deploy Adds a new replicated method 'pop' with version 1 to the existing KVStorage class. The existing set and get methods remain unchanged. ```python class KVStorage(SyncObj): def __init__(self, selfAddress, partnerAddrs): super(KVStorage, self).__init__(selfAddress, partnerAddrs) self.__data = {} @replicated def set(self, key, value): self.__data[key] = value @replicated(ver=1) def pop(self, key): self.__data.pop(key, None) def get(self, key): return self.__data.get(key, None) ``` -------------------------------- ### Update with migration: add runMigration, set config.onCodeVersionChanged, new set with ver=2 Source: https://github.com/bakwc/pysyncobj/wiki/Zero-downtime-deploy Adds a migration function called runMigration, assigns it to SyncObjConf().onCodeVersionChanged, and adds a new replicated set method with ver=2 that increments a version counter. It also includes the old set and pop methods for compatibility. ```python class KVStorage(SyncObj): def __init__(self, selfAddress, partnerAddrs): cfg = SyncObjConf() cfg.onCodeVersionChanged = self.runMigration super(KVStorage, self).__init__(selfAddress, partnerAddrs, cfg) self.__data = {} def runMigration(self, verFrom, verTo): assert verFrom == 0 and verTo == 1 newData = {} for key, value in self.__data.items(): newData[key] = (value, 1) self.__data = newData @replicated def set(self, key, value): self.__data[key] = value @replicated(ver=2) def set(self, key, value): currVer = self.__data.get(key, (0, 0))[1] self.__data[key] = (value, currVer + 1) @replicated(ver=1) def pop(self, key): self.__data.pop(key, None) def get(self, key): return self.__data.get(key, None) ``` -------------------------------- ### Add a new node with syncobj_admin -add Source: https://github.com/bakwc/pysyncobj/wiki/syncobj_admin Use this command to add a new node to the cluster. The first argument is an existing cluster node, and the second is the new node to add. After adding, you must turn on the new node and pass all current cluster nodes to it. ```bash syncobj_admin -conn localhost:1180 -add localhost:1181 ``` -------------------------------- ### Check node status with syncobj_admin -status Source: https://github.com/bakwc/pysyncobj/wiki/syncobj_admin Use this command to check the status of a node. It connects to a node and displays parameters such as state, leader, partner_nodes_count, commit_idx, last_applied, version, revision, and uptime. ```bash syncobj_admin -conn localhost:1180 -status ``` -------------------------------- ### Remove an existing node with syncobj_admin -remove Source: https://github.com/bakwc/pysyncobj/wiki/syncobj_admin Use this command to remove a node from the cluster. The first argument is a node that remains in the cluster, and the second is the node to remove. The node to be removed must be turned off first. ```bash syncobj_admin -conn localhost:1180 -remove localhost:1181 ``` -------------------------------- ### Replicated counter with SyncObj Source: https://github.com/bakwc/pysyncobj/blob/master/README.md Replicated counter class: inherit from SyncObj, initialize with self address and partner addresses, and mark mutating methods with @replicated. ```python class MyCounter(SyncObj): def __init__(self): super(MyCounter, self).__init__('serverA:4321', ['serverB:4321', 'serverC:4321']) self.__counter = 0 @replicated def incCounter(self): self.__counter += 1 def getCounter(self): return self.__counter ``` -------------------------------- ### Use ReplLockManager for distributed locking Source: https://github.com/bakwc/pysyncobj/blob/master/README.md Demonstrates using ReplLockManager to acquire and release a distributed lock. The autoUnlockTime parameter (75 seconds) releases the lock if the connection is dropped for longer than that. The tryAcquire call is blocking due to sync=True. ```python from pysyncobj import SyncObj from pysyncobj.batteries import ReplLockManager lockManager = ReplLockManager(autoUnlockTime=75) # Lock will be released if connection dropped for more than 75 seconds syncObj = SyncObj('serverA:4321', ['serverB:4321', 'serverC:4321'], consumers=[lockManager]) if lockManager.tryAcquire('testLockName', sync=True): # do some actions lockManager.release('testLockName') ``` -------------------------------- ### Basic counter class Source: https://github.com/bakwc/pysyncobj/blob/master/README.md A simple counter class that will be transformed into a replicated one. ```python class MyCounter(object): def __init__(self): self.__counter = 0 def incCounter(self): self.__counter += 1 def getCounter(self): return self.__counter ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.