{"action":"created","comment":{"url":"https://api.github.com/repos/bsc-s2/pykit/pulls/comments/190144506","pull_request_review_id":122447103,"id":190144506,"diff_hunk":"@@ -0,0 +1,423 @@\n+#!/usr/bin/env python\n+# coding: utf-8\n+\n+import copy\n+import logging\n+import threading\n+import time\n+\n+from kazoo.client import KazooState\n+from kazoo.exceptions import KazooException\n+from kazoo.exceptions import NoNodeError\n+\n+from pykit import rangeset\n+from pykit import zkutil\n+\n+from .exceptions import ConnectionLoss\n+from .exceptions import Deadlock\n+from .exceptions import HigherTXApplied\n+from .exceptions import RetriableError\n+from .exceptions import TXTimeout\n+from .exceptions import UserAborted\n+from .status import ABORTED\n+from .status import COMMITTED\n+from .zkstorage import ZKStorage\n+\n+logger = logging.getLogger(__name__)\n+\n+\n+class TXRecord(object):\n+\n+    def __init__(self, k, v, txid):\n+        self.k = k\n+        self.v = v\n+        self.txid = txid\n+\n+    def __str__(self):\n+        return '{k}={v}@{txid}'.format(\n+            k=self.k, v=self.v, txid=txidstr(self.txid))\n+\n+\n+class ZKTransaction(object):\n+\n+    def __init__(self, zk, timeout=None):\n+\n+        if timeout is None:\n+            timeout = 10\n+\n+        # Save the original arg for self.run()\n+        self._zk = zk\n+\n+        self.zke, self.owning_zk = zkutil.kazoo_client_ext(zk)\n+        self.timeout = timeout\n+\n+        self.txid = None\n+        self.expire_at = None\n+        self.tx_alive_lock = None\n+\n+        # User locked and required keys\n+        self.got_keys = {}\n+        # Keys user need to commit\n+        self.modifications = {}\n+\n+        self.state_lock = threading.RLock()\n+        self.connected = True\n+\n+        self.tx_status = None\n+\n+        self.zkstorage = ZKStorage(self.zke)\n+\n+        self.zke.add_listener(self._on_conn_change)\n+\n+    def run(self, func, args=(), kwargs=None):\n+\n+        if kwargs is None:\n+            kwargs = {}\n+\n+        expire_at = time.time() + self.timeout\n+\n+        while True:\n+\n+            tx = ZKTransaction(self._zk, timeout=expire_at - time.time())\n+\n+            try:\n+                with tx:\n+                    func(tx, *args, **kwargs)\n+\n+                if tx.tx_status == ABORTED:\n+                    return ABORTED, tx.txid\n+                else:\n+                    return COMMITTED, tx.txid\n+\n+            except RetriableError as e:\n+                logger.info('{tx} {e}'.format(tx=tx, e=repr(e)))\n+                continue\n+\n+    def _on_conn_change(self, state):\n+\n+        logger.debug('state changed: {state}'.format(state=state,))\n+\n+        with self.state_lock:\n+            if state == KazooState.LOST or state == KazooState.SUSPENDED:\n+                self.connected = False\n+\n+    def lock_get(self, key):\n+\n+        # We use persistent lock(ephemeral=False)\n+        # thus we do not need to care about connection loss during locking\n+        # phase.\n+        # But we still need to check connection when creating journal.\n+\n+        if key in self.got_keys:\n+            return copy.deepcopy(self.got_keys[key])\n+\n+        self.lock_key(key)\n+\n+        l, version = self.zkstorage.get_latest(key)\n+\n+        ltxid, lvalue = l.items()[0]\n+\n+        curr = TXRecord(k=key, v=lvalue, txid=ltxid)\n+        self.got_keys[key] = curr\n+\n+        if ltxid > self.txid:\n+            raise HigherTXApplied('{tx} seen a higher txid applied: {txid}'.format(\n+                tx=self, txid=ltxid))\n+\n+        rec = TXRecord(k=key, v=lvalue, txid=ltxid)\n+        return rec\n+\n+    def set(self, rec):\n+        logger.info('{tx} tx.set: {rec}'.format(tx=self, rec=rec))\n+        self.modifications[rec.k] = rec\n+\n+    def lock_key(self, key):\n+\n+        try:\n+            for other_txid, ver in self.zkstorage.watch_acquire_key(\n+                    self.txid, key, timeout=self.time_left()):\n+\n+                logger.info('{tx} ----[{key}]---> {other_txid} ver: {ver}'.format(\n+                    tx=self, key=key, other_txid=txidstr(other_txid), ver=ver))","path":"zktx/zktx.py","position":141,"original_position":141,"commit_id":"202a68e6ff221514ea527dfdddb40172c01675f4","original_commit_id":"202a68e6ff221514ea527dfdddb40172c01675f4","user":{"login":"myy2829","id":26829930,"avatar_url":"https://avatars0.githubusercontent.com/u/26829930?v=4","gravatar_id":"","url":"https://api.github.com/users/myy2829","html_url":"https://github.com/myy2829","followers_url":"https://api.github.com/users/myy2829/followers","following_url":"https://api.github.com/users/myy2829/following{/other_user}","gists_url":"https://api.github.com/users/myy2829/gists{/gist_id}","starred_url":"https://api.github.com/users/myy2829/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/myy2829/subscriptions","organizations_url":"https://api.github.com/users/myy2829/orgs","repos_url":"https://api.github.com/users/myy2829/repos","events_url":"https://api.github.com/users/myy2829/events{/privacy}","received_events_url":"https://api.github.com/users/myy2829/received_events","type":"User","site_admin":false},"body":"tx=self, self是个内存地址吧，这里应该是tx=self.txid?","created_at":"2018-05-23T07:19:46Z","updated_at":"2018-05-23T07:20:23Z","html_url":"https://github.com/bsc-s2/pykit/pull/254#discussion_r190144506","pull_request_url":"https://api.github.com/repos/bsc-s2/pykit/pulls/254","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/bsc-s2/pykit/pulls/comments/190144506"},"html":{"href":"https://github.com/bsc-s2/pykit/pull/254#discussion_r190144506"},"pull_request":{"href":"https://api.github.com/repos/bsc-s2/pykit/pulls/254"}}},"pull_request":{"url":"https://api.github.com/repos/bsc-s2/pykit/pulls/254","id":189599384,"html_url":"https://github.com/bsc-s2/pykit/pull/254","diff_url":"https://github.com/bsc-s2/pykit/pull/254.diff","patch_url":"https://github.com/bsc-s2/pykit/pull/254.patch","issue_url":"https://api.github.com/repos/bsc-s2/pykit/issues/254","number":254,"state":"open","locked":false,"title":"我发4, 最后1个pr了... zktx: zookeeper based transaction engine","user":{"login":"drmingdrmer","id":44069,"avatar_url":"https://avatars0.githubusercontent.com/u/44069?v=4","gravatar_id":"","url":"https://api.github.com/users/drmingdrmer","html_url":"https://github.com/drmingdrmer","followers_url":"https://api.github.com/users/drmingdrmer/followers","following_url":"https://api.github.com/users/drmingdrmer/following{/other_user}","gists_url":"https://api.github.com/users/drmingdrmer/gists{/gist_id}","starred_url":"https://api.github.com/users/drmingdrmer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drmingdrmer/subscriptions","organizations_url":"https://api.github.com/users/drmingdrmer/orgs","repos_url":"https://api.github.com/users/drmingdrmer/repos","events_url":"https://api.github.com/users/drmingdrmer/events{/privacy}","received_events_url":"https://api.github.com/users/drmingdrmer/received_events","type":"User","site_admin":false},"body":"-   Refined exception names.\r\n\r\n-   CAS in abstract storage layer support configurable CAS-conflict error.\r\n\r\n-   Accessor: add set_or_create(), to update a value without the need of\r\n    knowing if the value exists or not.\r\n\r\n-   Accessor.create() accept argument `ephemeral` and `sequence`.\r\n\r\n-   tx API: with-statement style or tx-function style.\r\n\r\n-   ZKStorage: add watch_acquire_key()","created_at":"2018-05-22T09:29:32Z","updated_at":"2018-05-23T07:20:23Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5ddd83f79c74ae8b67c1fb063c24b2f3acb680d2","assignee":null,"assignees":[],"requested_reviewers":[{"login":"sejust","id":2747551,"avatar_url":"https://avatars2.githubusercontent.com/u/2747551?v=4","gravatar_id":"","url":"https://api.github.com/users/sejust","html_url":"https://github.com/sejust","followers_url":"https://api.github.com/users/sejust/followers","following_url":"https://api.github.com/users/sejust/following{/other_user}","gists_url":"https://api.github.com/users/sejust/gists{/gist_id}","starred_url":"https://api.github.com/users/sejust/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sejust/subscriptions","organizations_url":"https://api.github.com/users/sejust/orgs","repos_url":"https://api.github.com/users/sejust/repos","events_url":"https://api.github.com/users/sejust/events{/privacy}","received_events_url":"https://api.github.com/users/sejust/received_events","type":"User","site_admin":false}],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/bsc-s2/pykit/pulls/254/commits","review_comments_url":"https://api.github.com/repos/bsc-s2/pykit/pulls/254/comments","review_comment_url":"https://api.github.com/repos/bsc-s2/pykit/pulls/comments{/number}","comments_url":"https://api.github.com/repos/bsc-s2/pykit/issues/254/comments","statuses_url":"https://api.github.com/repos/bsc-s2/pykit/statuses/202a68e6ff221514ea527dfdddb40172c01675f4","head":{"label":"bsc-s2:jour","ref":"jour","sha":"202a68e6ff221514ea527dfdddb40172c01675f4","user":{"login":"bsc-s2","id":36946445,"avatar_url":"https://avatars3.githubusercontent.com/u/36946445?v=4","gravatar_id":"","url":"https://api.github.com/users/bsc-s2","html_url":"https://github.com/bsc-s2","followers_url":"https://api.github.com/users/bsc-s2/followers","following_url":"https://api.github.com/users/bsc-s2/following{/other_user}","gists_url":"https://api.github.com/users/bsc-s2/gists{/gist_id}","starred_url":"https://api.github.com/users/bsc-s2/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bsc-s2/subscriptions","organizations_url":"https://api.github.com/users/bsc-s2/orgs","repos_url":"https://api.github.com/users/bsc-s2/repos","events_url":"https://api.github.com/users/bsc-s2/events{/privacy}","received_events_url":"https://api.github.com/users/bsc-s2/received_events","type":"Organization","site_admin":false},"repo":{"id":75464992,"name":"pykit","full_name":"bsc-s2/pykit","owner":{"login":"bsc-s2","id":36946445,"avatar_url":"https://avatars3.githubusercontent.com/u/36946445?v=4","gravatar_id":"","url":"https://api.github.com/users/bsc-s2","html_url":"https://github.com/bsc-s2","followers_url":"https://api.github.com/users/bsc-s2/followers","following_url":"https://api.github.com/users/bsc-s2/following{/other_user}","gists_url":"https://api.github.com/users/bsc-s2/gists{/gist_id}","starred_url":"https://api.github.com/users/bsc-s2/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bsc-s2/subscriptions","organizations_url":"https://api.github.com/users/bsc-s2/orgs","repos_url":"https://api.github.com/users/bsc-s2/repos","events_url":"https://api.github.com/users/bsc-s2/events{/privacy}","received_events_url":"https://api.github.com/users/bsc-s2/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/bsc-s2/pykit","description":"a collection of toolkit lib for distributed system development in python","fork":false,"url":"https://api.github.com/repos/bsc-s2/pykit","forks_url":"https://api.github.com/repos/bsc-s2/pykit/forks","keys_url":"https://api.github.com/repos/bsc-s2/pykit/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bsc-s2/pykit/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bsc-s2/pykit/teams","hooks_url":"https://api.github.com/repos/bsc-s2/pykit/hooks","issue_events_url":"https://api.github.com/repos/bsc-s2/pykit/issues/events{/number}","events_url":"https://api.github.com/repos/bsc-s2/pykit/events","assignees_url":"https://api.github.com/repos/bsc-s2/pykit/assignees{/user}","branches_url":"https://api.github.com/repos/bsc-s2/pykit/branches{/branch}","tags_url":"https://api.github.com/repos/bsc-s2/pykit/tags","blobs_url":"https://api.github.com/repos/bsc-s2/pykit/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bsc-s2/pykit/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bsc-s2/pykit/git/refs{/sha}","trees_url":"https://api.github.com/repos/bsc-s2/pykit/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bsc-s2/pykit/statuses/{sha}","languages_url":"https://api.github.com/repos/bsc-s2/pykit/languages","stargazers_url":"https://api.github.com/repos/bsc-s2/pykit/stargazers","contributors_url":"https://api.github.com/repos/bsc-s2/pykit/contributors","subscribers_url":"https://api.github.com/repos/bsc-s2/pykit/subscribers","subscription_url":"https://api.github.com/repos/bsc-s2/pykit/subscription","commits_url":"https://api.github.com/repos/bsc-s2/pykit/commits{/sha}","git_commits_url":"https://api.github.com/repos/bsc-s2/pykit/git/commits{/sha}","comments_url":"https://api.github.com/repos/bsc-s2/pykit/comments{/number}","issue_comment_url":"https://api.github.com/repos/bsc-s2/pykit/issues/comments{/number}","contents_url":"https://api.github.com/repos/bsc-s2/pykit/contents/{+path}","compare_url":"https://api.github.com/repos/bsc-s2/pykit/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bsc-s2/pykit/merges","archive_url":"https://api.github.com/repos/bsc-s2/pykit/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bsc-s2/pykit/downloads","issues_url":"https://api.github.com/repos/bsc-s2/pykit/issues{/number}","pulls_url":"https://api.github.com/repos/bsc-s2/pykit/pulls{/number}","milestones_url":"https://api.github.com/repos/bsc-s2/pykit/milestones{/number}","notifications_url":"https://api.github.com/repos/bsc-s2/pykit/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bsc-s2/pykit/labels{/name}","releases_url":"https://api.github.com/repos/bsc-s2/pykit/releases{/id}","deployments_url":"https://api.github.com/repos/bsc-s2/pykit/deployments","created_at":"2016-12-03T09:38:38Z","updated_at":"2018-05-22T12:50:53Z","pushed_at":"2018-05-23T06:13:30Z","git_url":"git://github.com/bsc-s2/pykit.git","ssh_url":"git@github.com:bsc-s2/pykit.git","clone_url":"https://github.com/bsc-s2/pykit.git","svn_url":"https://github.com/bsc-s2/pykit","homepage":null,"size":3396,"stargazers_count":10,"watchers_count":10,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":5,"mirror_url":null,"archived":false,"open_issues_count":13,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit"},"forks":5,"open_issues":13,"watchers":10,"default_branch":"master"}},"base":{"label":"bsc-s2:master","ref":"master","sha":"ab57aa1bdf2c02114d9285ede4a4471012e4e1cd","user":{"login":"bsc-s2","id":36946445,"avatar_url":"https://avatars3.githubusercontent.com/u/36946445?v=4","gravatar_id":"","url":"https://api.github.com/users/bsc-s2","html_url":"https://github.com/bsc-s2","followers_url":"https://api.github.com/users/bsc-s2/followers","following_url":"https://api.github.com/users/bsc-s2/following{/other_user}","gists_url":"https://api.github.com/users/bsc-s2/gists{/gist_id}","starred_url":"https://api.github.com/users/bsc-s2/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bsc-s2/subscriptions","organizations_url":"https://api.github.com/users/bsc-s2/orgs","repos_url":"https://api.github.com/users/bsc-s2/repos","events_url":"https://api.github.com/users/bsc-s2/events{/privacy}","received_events_url":"https://api.github.com/users/bsc-s2/received_events","type":"Organization","site_admin":false},"repo":{"id":75464992,"name":"pykit","full_name":"bsc-s2/pykit","owner":{"login":"bsc-s2","id":36946445,"avatar_url":"https://avatars3.githubusercontent.com/u/36946445?v=4","gravatar_id":"","url":"https://api.github.com/users/bsc-s2","html_url":"https://github.com/bsc-s2","followers_url":"https://api.github.com/users/bsc-s2/followers","following_url":"https://api.github.com/users/bsc-s2/following{/other_user}","gists_url":"https://api.github.com/users/bsc-s2/gists{/gist_id}","starred_url":"https://api.github.com/users/bsc-s2/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bsc-s2/subscriptions","organizations_url":"https://api.github.com/users/bsc-s2/orgs","repos_url":"https://api.github.com/users/bsc-s2/repos","events_url":"https://api.github.com/users/bsc-s2/events{/privacy}","received_events_url":"https://api.github.com/users/bsc-s2/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/bsc-s2/pykit","description":"a collection of toolkit lib for distributed system development in python","fork":false,"url":"https://api.github.com/repos/bsc-s2/pykit","forks_url":"https://api.github.com/repos/bsc-s2/pykit/forks","keys_url":"https://api.github.com/repos/bsc-s2/pykit/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bsc-s2/pykit/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bsc-s2/pykit/teams","hooks_url":"https://api.github.com/repos/bsc-s2/pykit/hooks","issue_events_url":"https://api.github.com/repos/bsc-s2/pykit/issues/events{/number}","events_url":"https://api.github.com/repos/bsc-s2/pykit/events","assignees_url":"https://api.github.com/repos/bsc-s2/pykit/assignees{/user}","branches_url":"https://api.github.com/repos/bsc-s2/pykit/branches{/branch}","tags_url":"https://api.github.com/repos/bsc-s2/pykit/tags","blobs_url":"https://api.github.com/repos/bsc-s2/pykit/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bsc-s2/pykit/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bsc-s2/pykit/git/refs{/sha}","trees_url":"https://api.github.com/repos/bsc-s2/pykit/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bsc-s2/pykit/statuses/{sha}","languages_url":"https://api.github.com/repos/bsc-s2/pykit/languages","stargazers_url":"https://api.github.com/repos/bsc-s2/pykit/stargazers","contributors_url":"https://api.github.com/repos/bsc-s2/pykit/contributors","subscribers_url":"https://api.github.com/repos/bsc-s2/pykit/subscribers","subscription_url":"https://api.github.com/repos/bsc-s2/pykit/subscription","commits_url":"https://api.github.com/repos/bsc-s2/pykit/commits{/sha}","git_commits_url":"https://api.github.com/repos/bsc-s2/pykit/git/commits{/sha}","comments_url":"https://api.github.com/repos/bsc-s2/pykit/comments{/number}","issue_comment_url":"https://api.github.com/repos/bsc-s2/pykit/issues/comments{/number}","contents_url":"https://api.github.com/repos/bsc-s2/pykit/contents/{+path}","compare_url":"https://api.github.com/repos/bsc-s2/pykit/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bsc-s2/pykit/merges","archive_url":"https://api.github.com/repos/bsc-s2/pykit/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bsc-s2/pykit/downloads","issues_url":"https://api.github.com/repos/bsc-s2/pykit/issues{/number}","pulls_url":"https://api.github.com/repos/bsc-s2/pykit/pulls{/number}","milestones_url":"https://api.github.com/repos/bsc-s2/pykit/milestones{/number}","notifications_url":"https://api.github.com/repos/bsc-s2/pykit/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bsc-s2/pykit/labels{/name}","releases_url":"https://api.github.com/repos/bsc-s2/pykit/releases{/id}","deployments_url":"https://api.github.com/repos/bsc-s2/pykit/deployments","created_at":"2016-12-03T09:38:38Z","updated_at":"2018-05-22T12:50:53Z","pushed_at":"2018-05-23T06:13:30Z","git_url":"git://github.com/bsc-s2/pykit.git","ssh_url":"git@github.com:bsc-s2/pykit.git","clone_url":"https://github.com/bsc-s2/pykit.git","svn_url":"https://github.com/bsc-s2/pykit","homepage":null,"size":3396,"stargazers_count":10,"watchers_count":10,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":5,"mirror_url":null,"archived":false,"open_issues_count":13,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit"},"forks":5,"open_issues":13,"watchers":10,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/bsc-s2/pykit/pulls/254"},"html":{"href":"https://github.com/bsc-s2/pykit/pull/254"},"issue":{"href":"https://api.github.com/repos/bsc-s2/pykit/issues/254"},"comments":{"href":"https://api.github.com/repos/bsc-s2/pykit/issues/254/comments"},"review_comments":{"href":"https://api.github.com/repos/bsc-s2/pykit/pulls/254/comments"},"review_comment":{"href":"https://api.github.com/repos/bsc-s2/pykit/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/bsc-s2/pykit/pulls/254/commits"},"statuses":{"href":"https://api.github.com/repos/bsc-s2/pykit/statuses/202a68e6ff221514ea527dfdddb40172c01675f4"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"myy2829"}}