test_rpc_tracker.py 3.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
17 18 19 20 21
import tvm
import logging
import numpy as np
import time
import multiprocessing
22
from tvm import rpc
23 24 25 26

def check_server_drop():
    """test when server drops"""
    try:
27 28
        from tvm.rpc import tracker, proxy, base
        from tvm.rpc.base import TrackerCode
29 30 31 32 33 34 35 36 37 38

        @tvm.register_func("rpc.test2.addone")
        def addone(x):
            return x + 1

        def _put(tclient, value):
            base.sendjson(tclient._sock, value)
            base.recvjson(tclient._sock)

        tserver = tracker.Tracker("localhost", 8888)
39 40
        tproxy = proxy.Proxy("localhost", 8881,
                             tracker_addr=("localhost", tserver.port))
41
        tclient = rpc.connect_tracker("localhost", tserver.port)
42 43 44 45 46

        server0 = rpc.Server(
            "localhost", port=9099,
            tracker_addr=("localhost", tserver.port),
            key="abc")
47 48 49 50 51
        server1 = rpc.Server(
            "localhost", port=9099,
            tracker_addr=("localhost", tserver.port),
            key="xyz")
        server2 = rpc.Server(
52
            "localhost", tproxy.port, is_proxy=True,
53
            key="xyz")
54 55 56
        server3 = rpc.Server(
            "localhost", tproxy.port, is_proxy=True,
            key="xyz1")
57

58 59 60 61
        # Fault tolerence to un-handled requested value
        _put(tclient, [TrackerCode.REQUEST, "abc", "", 1])
        _put(tclient, [TrackerCode.REQUEST, "xyz1", "", 1])

62 63 64
        # Fault tolerence to stale worker value
        _put(tclient, [TrackerCode.PUT, "xyz", (server1.port, "abc")])
        _put(tclient, [TrackerCode.PUT, "xyz", (server1.port, "abcxxx")])
65
        _put(tclient, [TrackerCode.PUT, "xyz", (tproxy.port, "abcxxx11")])
66 67 68

        # Fault tolerence server timeout
        def check_timeout(timeout, sleeptime):
69 70 71 72 73 74 75 76 77
            def myfunc(remote):
                time.sleep(sleeptime)
                f1 = remote.get_function("rpc.test2.addone")
                assert f1(10) == 11
            try:
                tclient.request_and_run("xyz", myfunc, session_timeout=timeout)
            except RuntimeError:
                pass
            print(tclient.text_summary())
78 79 80 81 82 83 84 85
            try:
                remote = tclient.request("xyz", priority=0, session_timeout=timeout)
                remote2 = tclient.request("xyz", session_timeout=timeout)
                time.sleep(sleeptime)
                f1 = remote.get_function("rpc.test2.addone")
                assert f1(10) == 11
                f1 = remote2.get_function("rpc.test2.addone")
                assert f1(10) == 11
86

87 88
            except tvm.TVMError as e:
                pass
89 90 91 92 93
            remote3 = tclient.request("abc")
            f1 = remote3.get_function("rpc.test2.addone")
            remote3 = tclient.request("xyz1")
            f1 = remote3.get_function("rpc.test2.addone")
            assert f1(10) == 11
94

95 96
        check_timeout(0.01, 0.1)
        check_timeout(2, 0)
97
        tserver.terminate()
98
        server0.terminate()
99
        server1.terminate()
100
        server2.terminate()
101 102
        server3.terminate()
        tproxy.terminate()
103 104 105 106 107 108 109
    except ImportError:
        print("Skip because tornado is not available")


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    check_server_drop()