pod.py 5.1 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 17 18 19 20 21 22
# 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.

from .container import Container

from .status import Status

import random
import time


23
class PodSepc:
K
kuizhiqing 已提交
24 25
    def __init__(self):
        self._name = ''.join(
26 27
            random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(6)
        )
K
kuizhiqing 已提交
28 29 30 31 32

        # by controller
        self._init_containers: List[Container] = []
        self._containers: List[Container] = []

33 34
        # self.resource: Resource = None
        # self.status: Status = None
K
kuizhiqing 已提交
35 36

        self._rank = -1
37
        self._init_timeout = None
K
kuizhiqing 已提交
38 39 40 41 42 43 44 45 46 47
        self._restart = -1
        self._replicas = 0  # number of containers
        self._exit_code = 0


class Pod(PodSepc):
    def __init__(self):
        super().__init__()

    def __str__(self):
48 49 50
        return "Pod: {}, replicas {}, status {}".format(
            self.name, self.replicas, self.status
        )
K
kuizhiqing 已提交
51 52

    def failed_container(self):
53
        cs = []
K
kuizhiqing 已提交
54
        for c in self._containers:
55 56 57
            if c.status == Status.FAILED:
                cs.append(c)
        return cs
K
kuizhiqing 已提交
58 59 60 61 62 63 64 65 66 67 68

    @property
    def name(self):
        return self._name

    @property
    def replicas(self):
        return self._replicas

    @replicas.setter
    def replicas(self, r):
69
        self._replicas = max(r, 1)
K
kuizhiqing 已提交
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

    @property
    def rank(self):
        return self._rank

    @rank.setter
    def rank(self, r):
        self._rank = r

    @property
    def restart(self):
        return self._restart

    @property
    def containers(self):
        return self._containers

    def add_container(self, c):
        c.rank = len(self._containers)
        self._containers.append(c)

    @property
    def init_containers(self):
        return self._init_containers

    def add_init_container(self, c):
        c.rank = len(self._init_containers)
        self._init_containers.append(c)

    @property
    def exit_code(self):
        for c in self._containers:
102 103
            if c.exit_code != 0:
                return c.exit_code
K
kuizhiqing 已提交
104 105 106
        return 0

    def deploy(self):
107
        # init container should stop before run containers
K
kuizhiqing 已提交
108
        for i in self._init_containers:
109 110
            i.start()
            i.wait(self._init_timeout)
K
kuizhiqing 已提交
111 112 113 114 115 116

        for c in self._containers:
            c.start()

        self._restart += 1

K
kuizhiqing 已提交
117
    def stop(self, sigint=15, timeout=None):
K
kuizhiqing 已提交
118
        for c in self._containers:
K
kuizhiqing 已提交
119 120 121 122 123 124 125 126 127 128 129 130
            if isinstance(sigint, int) and timeout is None:
                c.send_signal(sigint)
            else:
                c.terminate()

        if isinstance(timeout, int):
            if not self.join(timeout):
                for c in self._containers:
                    c.terminate(force=True)
                return False
            else:
                return True
K
kuizhiqing 已提交
131

K
kuizhiqing 已提交
132
    def join(self, timeout=None):
K
kuizhiqing 已提交
133
        for c in self._containers:
K
kuizhiqing 已提交
134 135 136
            if not c.wait(timeout):
                return False
        return True
K
kuizhiqing 已提交
137

138
    @property
K
kuizhiqing 已提交
139 140 141 142 143 144 145
    def status(self):
        if self.is_failed():
            return Status.FAILED

        if self.is_completed():
            return Status.COMPLETED

146 147 148
        if self.is_running():
            return Status.RUNNING

K
kuizhiqing 已提交
149 150 151 152 153 154 155 156
        return Status.READY

    def reset(self):
        self._init_containers = []
        self._containers = []

    def is_failed(self):
        for c in self._containers:
157
            if c.status == Status.FAILED:
K
kuizhiqing 已提交
158 159 160 161 162
                return True
        return False

    def is_completed(self):
        for c in self._containers:
163 164 165 166 167 168 169
            if c.status != Status.COMPLETED:
                return False
        return True

    def is_running(self):
        for c in self._containers:
            if c.status != Status.RUNNING:
K
kuizhiqing 已提交
170 171 172 173 174
                return False
        return True

    def logs(self, idx=None):
        if idx is None:
175
            self._containers[0].logs()
K
kuizhiqing 已提交
176 177 178 179 180
        else:
            self._containers[idx].logs()

    def tail(self, idx=None):
        if idx is None:
181
            self._containers[0].tail()
K
kuizhiqing 已提交
182 183 184
        else:
            self._containers[idx].tail()

185 186 187 188 189 190 191
    def watch(
        self,
        all_list=[Status.COMPLETED],
        any_list=[Status.FAILED],
        interval=1,
        timeout=-1,
    ):
K
kuizhiqing 已提交
192 193 194 195 196 197 198
        '''
        watch return if any container status in any_list
        or all container status in all_list
        '''
        end = time.time() + timeout
        while timeout < 0 or time.time() < end:
            for c in self._containers:
199 200
                if c.status in any_list:
                    return c.status
K
kuizhiqing 已提交
201

202
            s = [c.status for c in self._containers]
K
kuizhiqing 已提交
203 204 205 206
            if len(set(s)) == 1 and s[0] in all_list:
                return s[0]

            time.sleep(interval)