summaryrefslogtreecommitdiff
path: root/kafka/streams/processor/assignment/client_state.py
blob: cec8d68bd08ac377da12d7ff0cbf645f482c7455 (plain)
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
"""
 * 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.
"""
from __future__ import absolute_import

import copy

class ClientState(object):

    COST_ACTIVE = 0.1
    COST_STANDBY = 0.2
    COST_LOAD = 0.5

    def __init__(self, active_tasks=None, assigned_tasks=None,
                 prev_active_tasks=None, prev_assigned_tasks=None,
                 capacity=0.0):
        self.active_tasks = active_tasks if active_tasks else set([])
        self.assigned_tasks = assigned_tasks if assigned_tasks else set([])
        self.prev_active_tasks = prev_active_tasks if prev_active_tasks else set([])
        self.prev_assigned_tasks = prev_assigned_tasks if prev_assigned_tasks else set([])
        self.capacity = capacity
        self.cost = 0.0

    def copy(self):
        return ClientState(copy.deepcopy(self.active_tasks),
                           copy.deepcopy(self.assigned_tasks),
                           copy.deepcopy(self.prev_active_tasks),
                           copy.deepcopy(self.prev_assigned_tasks),
                           self.capacity)

    def assign(self, task_id, active):
        if active:
            self.active_tasks.add(task_id)

        self.assigned_tasks.add(task_id)

        cost = self.COST_LOAD
        try:
            self.prev_assigned_tasks.remove(task_id)
            cost = self.COST_STANDBY
        except KeyError:
            pass
        try:
            self.prev_active_tasks.remove(task_id)
            cost = self.COST_ACTIVE
        except KeyError:
            pass

        self.cost += cost

    def __str__(self):
        return "[active_tasks: (%s) assigned_tasks: (%s) prev_active_tasks: (%s) prev_assigned_tasks: (%s) capacity: (%s) cost: (%s)]" % (
            self.active_tasks, self.assigned_tasks, self.prev_active_tasks, self.prev_assigned_tasks, self.capacity, self.cost)