def cluster_wait():
    ''' Wait for operations based on modulo distribution

    Use the distributed_wait function to determine how long to wait before
    running an operation like restart or cluster join. By setting modulo to
    the exact number of nodes in the cluster we get serial operations.

    Check for explicit configuration parameters for modulo distribution.
    The config setting modulo-nodes has first priority. If modulo-nodes is not
    set, check min-cluster-size. Finally, if neither value is set, determine
    how many peers there are from the cluster relation.

    @side_effect: distributed_wait is called which calls time.sleep()
    @return: None
    '''
    wait = config('known-wait')
    if config('modulo-nodes') is not None:
        # modulo-nodes has first priority
        num_nodes = config('modulo-nodes')
    elif config('min-cluster-size'):
        # min-cluster-size is consulted next
        num_nodes = config('min-cluster-size')
    else:
        # If nothing explicit is configured, determine cluster size based on
        # peer relations
        num_nodes = 1
        for rid in relation_ids('cluster'):
            num_nodes += len(related_units(rid))
    distributed_wait(modulo=num_nodes, wait=wait)
def cluster_wait():
    ''' Wait for operations based on modulo distribution

    Use the distributed_wait function to determine how long to wait before
    running an operation like restart or cluster join. By setting modulo to
    the exact number of nodes in the cluster we get serial operations.

    Check for explicit configuration parameters for modulo distribution.
    The config setting modulo-nodes has first priority. If modulo-nodes is not
    set, check min-cluster-size. Finally, if neither value is set, determine
    how many peers there are from the cluster relation.

    @side_effect: distributed_wait is called which calls time.sleep()
    @return: None
    '''
    wait = config('known-wait')
    if config('modulo-nodes') is not None:
        # modulo-nodes has first priority
        num_nodes = config('modulo-nodes')
    elif config('min-cluster-size'):
        # min-cluster-size is consulted next
        num_nodes = config('min-cluster-size')
    else:
        # If nothing explicit is configured, determine cluster size based on
        # peer relations
        num_nodes = 1
        for rid in relation_ids('cluster'):
            num_nodes += len(related_units(rid))
    distributed_wait(modulo=num_nodes, wait=wait)
Esempio n. 3
0
    def test_distributed_wait(self, log, modulo_distribution, sleep,
                              status_set):
        conf = {
            'modulo-nodes': 7,
            'known-wait': 10,
        }

        def _fake_config_get(setting):
            return conf[setting]

        self.config_get.side_effect = _fake_config_get

        # Uses config values
        cluster_utils.distributed_wait()
        modulo_distribution.assert_called_with(modulo=7, wait=10)

        # Uses passed values
        cluster_utils.distributed_wait(modulo=3, wait=45)
        modulo_distribution.assert_called_with(modulo=3, wait=45)
Esempio n. 4
0
    def test_distributed_wait(self, log, modulo_distribution, sleep,
                              status_set, is_leader):

        # Leader regardless of modulo should not wait
        is_leader.return_value = True
        cluster_utils.distributed_wait(modulo=9, wait=23)
        modulo_distribution.assert_not_called()
        sleep.assert_called_with(0)

        # The rest of the tests are non-leader units
        is_leader.return_value = False

        def _fake_config_get(setting):
            return conf[setting]

        # Uses fallback defaults
        conf = {
            'modulo-nodes': None,
            'known-wait': None,
        }
        self.config_get.side_effect = _fake_config_get
        cluster_utils.distributed_wait()
        modulo_distribution.assert_called_with(modulo=3,
                                               wait=30,
                                               non_zero_wait=True)

        # Uses config values
        conf = {
            'modulo-nodes': 7,
            'known-wait': 10,
        }
        self.config_get.side_effect = _fake_config_get
        cluster_utils.distributed_wait()
        modulo_distribution.assert_called_with(modulo=7,
                                               wait=10,
                                               non_zero_wait=True)

        # Uses passed values
        cluster_utils.distributed_wait(modulo=5, wait=45)
        modulo_distribution.assert_called_with(modulo=5,
                                               wait=45,
                                               non_zero_wait=True)