kv_client.py 2.9 KB
Newer Older
K
kuizhiqing 已提交
1
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2
#
K
kuizhiqing 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
6
#
K
kuizhiqing 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
K
kuizhiqing 已提交
9 10 11 12 13 14 15 16
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import time

17
import httpx
18

K
kuizhiqing 已提交
19

20
class KVClient:
K
kuizhiqing 已提交
21
    def __init__(self, endpoint='localhost:2379'):
22
        self.endpoint = (
23
            endpoint if endpoint.startswith("http://") else f"http://{endpoint}"
24
        )
K
kuizhiqing 已提交
25 26

    def put(self, key, value):
27 28
        key = key if key.startswith('/') else f"/{key}"
        u = f"{self.endpoint}{key}"
K
kuizhiqing 已提交
29
        try:
30
            r = httpx.post(u, data=value, timeout=None, follow_redirects=True)
K
kuizhiqing 已提交
31 32 33 34 35 36 37 38
            if r.status_code == 200:
                return True
            else:
                return False
        except:
            return False

    def get(self, key):
39 40
        key = key if key.startswith('/') else f"/{key}"
        u = f"{self.endpoint}{key}"
K
kuizhiqing 已提交
41
        try:
42
            r = httpx.get(u, timeout=None, follow_redirects=True)
K
kuizhiqing 已提交
43 44 45 46 47 48 49 50 51
            if r.status_code == 200:
                ret = r.json()
                return ret.get(key, '')
            else:
                return "error"
        except:
            return ""

    def get_prefix(self, key):
52 53
        key = key if key.startswith('/') else f"/{key}"
        u = f"{self.endpoint}{key}"
K
kuizhiqing 已提交
54
        try:
55
            r = httpx.get(u, timeout=None, follow_redirects=True)
K
kuizhiqing 已提交
56 57 58 59 60 61
            if r.status_code == 200:
                return r.json()
        except:
            return ""

    def delete(self, key):
62 63
        key = key if key.startswith('/') else f"/{key}"
        u = f"{self.endpoint}{key}"
K
kuizhiqing 已提交
64
        try:
65
            r = httpx.delete(u, timeout=None, follow_redirects=True)
K
kuizhiqing 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
            if r.status_code == 200:
                return True
            else:
                return False
        except:
            return False

    def wait_server_ready(self, timeout=3):
        end = time.time() + timeout
        while time.time() < end:
            if self.get("/healthy") == "ok":
                return True


if __name__ == '__main__':
81
    cli = KVClient("http://localhost:8090")
K
kuizhiqing 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    data = {"/workers/1": "rank1", "/workers/2": "rank2"}
    for k, v in data.items():
        cli.put(k, v)
    x = cli.get_prefix("/workers")
    print(x)
    for k, v in data.items():
        assert x[k] == v

    cli.put("key", "value")
    print(cli.get("key"))
    assert cli.get("key") == "value"
    cli.delete("key")
    print(cli.get("/key"))
    print(cli.get("/healthy"))
    assert cli.get("/healthy") == "ok"