Skip to content

Documentation for Stdcandidateselector Module

STDandidateSelector

Bases: CandidateSelector

Candidate selector for scenarios without a predefined structural topology.

In cases where the federation topology is not explicitly structured, this selector chooses candidates based on the average number of neighbors indicated in their offers. It selects approximately as many candidates as the average neighbor count, aiming to balance connectivity dynamically.

Attributes:

Name Type Description
candidates list

List of candidate nodes available for selection.

candidates_lock Locker

Async lock to ensure thread-safe access to candidates.

Methods:

Name Description
set_config

Optional configuration method.

add_candidate

Adds a candidate node to the candidate list.

select_candidates

Selects candidates based on the average neighbor count from offers.

remove_candidates

Clears the candidates list.

any_candidate

Returns True if there is at least one candidate available.

Inherits from

CandidateSelector: Base interface for candidate selection strategies.

Source code in nebula/core/situationalawareness/discovery/candidateselection/stdcandidateselector.py
 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
class STDandidateSelector(CandidateSelector):
    """
    Candidate selector for scenarios without a predefined structural topology.

    In cases where the federation topology is not explicitly structured,
    this selector chooses candidates based on the average number of neighbors 
    indicated in their offers. It selects approximately as many candidates as the 
    average neighbor count, aiming to balance connectivity dynamically.

    Attributes:
        candidates (list): List of candidate nodes available for selection.
        candidates_lock (Locker): Async lock to ensure thread-safe access to candidates.

    Methods:
        set_config(config): Optional configuration method.
        add_candidate(candidate): Adds a candidate node to the candidate list.
        select_candidates(): Selects candidates based on the average neighbor count from offers.
        remove_candidates(): Clears the candidates list.
        any_candidate(): Returns True if there is at least one candidate available.

    Inherits from:
        CandidateSelector: Base interface for candidate selection strategies.
    """

    def __init__(self):
        self.candidates = []
        self.candidates_lock = Locker(name="candidates_lock")

    async def set_config(self, config):
        pass

    async def add_candidate(self, candidate):
        self.candidates_lock.acquire()
        self.candidates.append(candidate)
        self.candidates_lock.release()

    async def select_candidates(self):
        """
        Select mean number of neighbors
        """
        self.candidates_lock.acquire()
        mean_neighbors = round(sum(n for _, n, _ in self.candidates) / len(self.candidates) if self.candidates else 0)
        logging.info(f"mean number of neighbors: {mean_neighbors}")
        cdts = self.candidates[:mean_neighbors]
        not_selected = set(self.candidates) - set(cdts)
        self.candidates_lock.release()
        return (cdts, not_selected)

    async def remove_candidates(self):
        self.candidates_lock.acquire()
        self.candidates = []
        self.candidates_lock.release()

    async def any_candidate(self):
        self.candidates_lock.acquire()
        any = True if len(self.candidates) > 0 else False
        self.candidates_lock.release()
        return any

select_candidates() async

Select mean number of neighbors

Source code in nebula/core/situationalawareness/discovery/candidateselection/stdcandidateselector.py
43
44
45
46
47
48
49
50
51
52
53
async def select_candidates(self):
    """
    Select mean number of neighbors
    """
    self.candidates_lock.acquire()
    mean_neighbors = round(sum(n for _, n, _ in self.candidates) / len(self.candidates) if self.candidates else 0)
    logging.info(f"mean number of neighbors: {mean_neighbors}")
    cdts = self.candidates[:mean_neighbors]
    not_selected = set(self.candidates) - set(cdts)
    self.candidates_lock.release()
    return (cdts, not_selected)