File size: 4,256 Bytes
079c32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import random
import time
import socket
import pytest
import multiprocessing as mp
from ditk import logging
from ding.framework import task
from ding.framework.parallel import Parallel
from ding.framework.context import OnlineRLContext
from ding.framework.middleware.barrier import Barrier

PORTS_LIST = ["1235", "1236", "1237"]


class EnvStepMiddleware:

    def __call__(self, ctx):
        yield
        ctx.env_step += 1


class SleepMiddleware:

    def __init__(self, node_id):
        self.node_id = node_id

    def random_sleep(self, diection, step):
        random.seed(self.node_id + step)
        sleep_second = random.randint(1, 5)
        logging.info("Node:[{}] env_step:[{}]-{} will sleep:{}s".format(self.node_id, step, diection, sleep_second))
        for i in range(sleep_second):
            time.sleep(1)
            print("Node:[{}] sleepping...".format(self.node_id))
        logging.info("Node:[{}] env_step:[{}]-{} wake up!".format(self.node_id, step, diection))

    def __call__(self, ctx):
        self.random_sleep("forward", ctx.env_step)
        yield
        self.random_sleep("backward", ctx.env_step)


def star_barrier():
    with task.start(ctx=OnlineRLContext()):
        node_id = task.router.node_id
        if node_id == 0:
            attch_from_nums = 3
        else:
            attch_from_nums = 0
        barrier = Barrier(attch_from_nums)
        task.use(barrier, lock=False)
        task.use(SleepMiddleware(node_id), lock=False)
        task.use(barrier, lock=False)
        task.use(EnvStepMiddleware(), lock=False)
        try:
            task.run(2)
        except Exception as e:
            logging.error(e)
            assert False


def mesh_barrier():
    with task.start(ctx=OnlineRLContext()):
        node_id = task.router.node_id
        attch_from_nums = 3 - task.router.node_id
        barrier = Barrier(attch_from_nums)
        task.use(barrier, lock=False)
        task.use(SleepMiddleware(node_id), lock=False)
        task.use(barrier, lock=False)
        task.use(EnvStepMiddleware(), lock=False)
        try:
            task.run(2)
        except Exception as e:
            logging.error(e)
            assert False


def unmatch_barrier():
    with task.start(ctx=OnlineRLContext()):
        node_id = task.router.node_id
        attch_from_nums = 3 - task.router.node_id
        task.use(Barrier(attch_from_nums, 5), lock=False)
        if node_id != 2:
            task.use(Barrier(attch_from_nums, 5), lock=False)
        try:
            task.run(2)
        except TimeoutError as e:
            assert node_id != 2
            logging.info("Node:[{}] timeout with barrier".format(node_id))
        else:
            time.sleep(5)
            assert node_id == 2
            logging.info("Node:[{}] finish barrier".format(node_id))


def launch_barrier(args):
    i, topo, fn, test_id = args
    address = socket.gethostbyname(socket.gethostname())
    topology = "alone"
    attach_to = []
    port_base = PORTS_LIST[test_id]
    port = port_base + str(i)
    if topo == 'star':
        if i != 0:
            attach_to = ['tcp://{}:{}{}'.format(address, port_base, 0)]
    elif topo == 'mesh':
        for j in range(i):
            attach_to.append('tcp://{}:{}{}'.format(address, port_base, j))

    Parallel.runner(
        node_ids=i,
        ports=int(port),
        attach_to=attach_to,
        topology=topology,
        protocol="tcp",
        n_parallel_workers=1,
        startup_interval=0
    )(fn)


@pytest.mark.unittest
def test_star_topology_barrier():
    ctx = mp.get_context("spawn")
    with ctx.Pool(processes=4) as pool:
        pool.map(launch_barrier, [[i, 'star', star_barrier, 0] for i in range(4)])
        pool.close()
        pool.join()


@pytest.mark.unittest
def test_mesh_topology_barrier():
    ctx = mp.get_context("spawn")
    with ctx.Pool(processes=4) as pool:
        pool.map(launch_barrier, [[i, 'mesh', mesh_barrier, 1] for i in range(4)])
        pool.close()
        pool.join()


@pytest.mark.unittest
def test_unmatch_barrier():
    ctx = mp.get_context("spawn")
    with ctx.Pool(processes=4) as pool:
        pool.map(launch_barrier, [[i, 'mesh', unmatch_barrier, 2] for i in range(4)])
        pool.close()
        pool.join()