def test_cas_argument(self): rst = {} def _get(*args, **kwargs): rst['get'] = args, kwargs return 'val', 'stat' def _set(*args, **kwargs): rst['set'] = args, kwargs for curr in txutil.cas_loop(_get, _set, ('foo', 'bar'), dict(a=1, b=2)): curr.v = 'uservalue' self.assertEqual((('foo', 'bar'), {"a": 1, "b": 2}), rst['get']) self.assertEqual((('foo', 'bar', 'uservalue', 'stat'), {"a": 1, "b": 2}), rst['set']) # empty arg rst = {} for curr in txutil.cas_loop(_get, _set): curr.v = 'uservalue' self.assertEqual(((), {}), rst['get']) self.assertEqual((('uservalue', 'stat'), {}), rst['set'])
def test_cas_argument(self): rst = {} def _get(*args, **kwargs): rst['get'] = args, kwargs return 'val', 'stat' def _set(*args, **kwargs): rst['set'] = args, kwargs for curr in txutil.cas_loop(_get, _set, ('foo', 'bar'), dict(a=1, b=2)): curr.v = 'uservalue' self.assertEqual((('foo', 'bar'), {"a": 1, "b": 2}), rst['get']) self.assertEqual((('foo', 'bar', 'uservalue', 'stat'), { "a": 1, "b": 2 }), rst['set']) # empty arg rst = {} for curr in txutil.cas_loop(_get, _set): curr.v = 'uservalue' self.assertEqual(((), {}), rst['get']) self.assertEqual((('uservalue', 'stat'), {}), rst['set'])
def test_cas_abort(self): for curr in txutil.cas_loop(self._get, self._set): curr.v += 2 break self.assertEqual((0, 0), (self.val, self.ver))
def test_cas_customed_error(self): for curr in txutil.cas_loop(self._get, self._set_raise_myerror, conflicterror=MyError): curr.v += 2 if curr.n == 5: break self.assertEqual((0, 0), (self.val, self.ver))
def test_cas_n(self): i = 0 for curr in txutil.cas_loop(self._get, self._set_raise_myerror, conflicterror=MyError): self.assertEqual(i, curr.n) i += 1 if i == 5: break
def cas_loop(zkclient, path, json=True): zkclient, owning_zk = kazoo_client_ext(zkclient, json=json) def setter(path, val, zstat): zkclient.set(path, val, version=zstat.version) try: for curr in txutil.cas_loop(zkclient.get, setter, args=(path, ), conflicterror=BadVersionError): yield curr finally: if owning_zk: zkutil.close_zk(zkclient)
def add_to_txidset(self, status, txid): if status not in STATUS: raise KeyError('invalid status: ' + repr(status)) logger.info('add {status}:{txid}' ' to txidset'.format(status=status, txid=txid)) for curr in txutil.cas_loop(self.txidset.get, self.txidset.set, conflicterror=self.conflicterror): for st in STATUS: if st not in curr.v: curr.v[st] = rangeset.RangeSet([]) curr.v[status].add([txid, txid + 1]) self.purge(curr.v)
def cas_loop(zkclient, path, json=True): zkclient, owning_zk = kazoo_client_ext(zkclient, json=json) def setter(path, val, zstat): zkclient.set(path, val, version=zstat.version) try: for curr in txutil.cas_loop(zkclient.get, setter, args=(path, ), conflicterror=BadVersionError): yield curr finally: if owning_zk: try: zkclient.stop() except Exception as e: logger.info(repr(e) + ' while stop owning zkclient')
def add_to_journal_id_set(self, status, journal_id): if status not in STATUS: raise KeyError('invalid status: ' + repr(status)) logger.info('add {st} journal id: {jid}'.format(st=status, jid=journal_id)) for curr in txutil.cas_loop(self.journal_id_set.get, self.journal_id_set.set, conflicterror=self.conflicterror): for st in STATUS: if st not in curr.v: curr.v[st] = rangeset.RangeSet([]) if status == COMMITTED: curr.v[status].add([0, int(journal_id) + 1]) else: curr.v[status].add([journal_id, int(journal_id) + 1]) self.purge(curr.v)
def apply_record(self, txid, key, value): # the data in underlying storage is multi-version record: # [ # [<txid>, <value>] # [<txid>, <value>] # ... # ] for curr in txutil.cas_loop(self.record.get, self.record.set_or_create, args=(key, ), conflicterror=self.conflicterror): max_txid = curr.v[-1][0] if max_txid >= txid: return False curr.v.append((txid, value)) while len(curr.v) > self.max_value_history: curr.v.pop(0) return True
def _update(): for ii in range(10): for curr in txutil.cas_loop(self._get, self._set): curr.v += 1
def test_cas(self): i = 0 for curr in txutil.cas_loop(self._get, self._set): self.assertEqual(i, curr.stat) curr.v += 2