Ejemplo n.º 1
0
    def test_reset_all(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 1, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 1, 0)
        view_est_mod.init_module = MagicMock()
        self.resolver.execute = MagicMock()

        self.assertEqual(pred_module.reset_all(), ViewEstablishmentEnums.RESET)
        # The views has been reset to DEFAULT view pair
        self.assertEqual(pred_module.views, [pred_module.RST_PAIR])
        # Assert that the init(reset) method at algorithm 1 and algorithm 3 are being called
        view_est_mod.init_module.assert_any_call()
        self.resolver.execute.assert_called_once_with(
            module=Module.REPLICATION_MODULE, func=Function.REP_REQUEST_RESET)
Ejemplo n.º 2
0
    def test_automaton_phase_0_actions(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)

        # Case 0 should call adopt and next_view in view Establishment module
        view_est_mod.next_phs = Mock()
        pred_module.adopt = Mock()
        pred_module.reset_v_change = Mock()
        pred_module.view_pair_to_adopt = pred_module.RST_PAIR
        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 0, 0),
            ViewEstablishmentEnums.NO_RETURN_VALUE)
        view_est_mod.next_phs.assert_any_call()
        #pred_module.reset_v_change.assert_any_call()
        pred_module.adopt.assert_called_once_with(pred_module.RST_PAIR)

        # Case 1a should call next_view and next_phs in view Establishment module
        pred_module.next_view = Mock()
        view_est_mod.next_phs = Mock()
        pred_module.vChange = [True for i in range(2)]
        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 0, 1),
            ViewEstablishmentEnums.NO_RETURN_VALUE)
        pred_module.next_view.assert_any_call()
        view_est_mod.next_phs.assert_any_call()

        # Case 1b should call next_phs in View Establishment module
        pred_module.vChange = [False for i in range(2)]
        view_est_mod.next_phs = Mock()
        pred_module.views[pred_module.id] = pred_module.RST_PAIR
        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 0, 1),
            ViewEstablishmentEnums.NO_RETURN_VALUE)
        view_est_mod.next_phs.assert_any_call()

        # Case 2 should return "No action" and call reset_v_change
        pred_module.reset_v_change = Mock()
        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 0, 2),
            ViewEstablishmentEnums.NO_ACTION)
        # pred_module.reset_v_change.assert_any_call()

        # Case 3 should return "Reset"
        pred_module.reset_all = MagicMock(
            return_value=ViewEstablishmentEnums.RESET)
        #pred_module.reset_v_change = Mock()
        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 0, 3),
            ViewEstablishmentEnums.RESET)
Ejemplo n.º 3
0
    def test_automaton_phase_1_actions(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)

        # Case 0 should call adopt and next_view in view Establishment module
        pred_module.adopt = Mock()
        pred_module.reset_v_change = Mock()
        pred_module.view_pair_to_adopt = pred_module.RST_PAIR

        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 1, 0),
            ViewEstablishmentEnums.NO_RETURN_VALUE)
        pred_module.adopt.assert_called_once_with(pred_module.RST_PAIR)
        pred_module.reset_v_change.assert_any_call()

        # Case 1 should call next_view and next_phs in view Establishment module
        view_est_mod.next_phs = Mock()
        pred_module.reset_v_change = Mock()
        pred_module.establish = Mock()
        self.resolver.execute = Mock()
        pred_module.views[pred_module.id] = pred_module.RST_PAIR

        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 1, 1),
            ViewEstablishmentEnums.NO_RETURN_VALUE)
        view_est_mod.next_phs.assert_any_call()
        pred_module.reset_v_change.assert_any_call()
        pred_module.establish.assert_any_call()
        # self.resolver.execute.assert_called_once_with(module=Module.REPLICATION_MODULE,
        #                 func=Function.REPLICA_FLUSH)

        # Case 2 should return "No action" and call reset_v_change
        pred_module.reset_v_change = Mock()
        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 1, 2),
            ViewEstablishmentEnums.NO_ACTION)
        pred_module.reset_v_change.assert_any_call()

        # Case 3 should return "Reset"
        pred_module.reset_all = MagicMock(
            return_value=ViewEstablishmentEnums.RESET)
        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 1, 3),
            ViewEstablishmentEnums.RESET)
Ejemplo n.º 4
0
class ViewEstablishmentModule(AlgorithmModule):
    """Models the View Establishment module."""
    def __init__(self, id, resolver, n, f):
        """Initializes the module."""
        self.resolver = resolver
        self.lock = resolver.view_est_lock
        self.phs = [0 for i in range(n)]
        self.witnesses = [False for i in range(n)]
        self.pred_and_action = PredicatesAndAction(self, id, self.resolver, n,
                                                   f)
        self.echo = [{
            VIEWS: {},
            PHASE: 0,
            WITNESSES: {},
            VCHANGE: None
        } for i in range(n)]

        self.number_of_nodes = n
        self.id = id
        self.number_of_byzantine = f
        self.witnesses_set = set()

        # Injection of starting state for integration tests
        if os.getenv("INTEGRATION_TEST") or os.getenv("INJECT_START_STATE"):
            start_state = conf.get_start_state()
            if (start_state is not {} and str(self.id) in start_state and
                    "VIEW_ESTABLISHMENT_MODULE" in start_state[str(self.id)]):
                data = start_state[str(self.id)]["VIEW_ESTABLISHMENT_MODULE"]
                if data is not None:
                    logger.warning("Injecting start state")
                    if "phs" in data:
                        self.phs = deepcopy(data["phs"])
                    if "views" in data:
                        self.pred_and_action.views = deepcopy(data["views"])
                    if "witnesses" in data:
                        self.witnesses = deepcopy(data["witnesses"])
                    if "echo" in data:
                        self.echo = deepcopy(data["echo"])
                    if "vChange" in data:
                        self.pred_and_action.vChange = deepcopy(
                            data["vChange"])

    def run(self, testing=False):
        """Called whenever the module is launched in a separate thread."""
        sec = os.getenv("INTEGRATION_TEST_SLEEP")
        time.sleep(int(sec) if sec is not None else 0)

        # block until system is ready
        while not testing and not self.resolver.system_running():
            time.sleep(0.1)

        while True:
            self.lock.acquire()
            # Metric time
            start_time = time.time()
            if (self.pred_and_action.need_reset()):
                self.pred_and_action.reset_all()
            self.witnesses[self.id] = self.noticed_recent_value()
            self.witnesses_set = self.witnesses_set.union(self.get_witnesses())
            if (self.witnes_seen()):
                case = 0
                # Find the current case by testing the predicates and
                # moving to next case if not fulfilled
                while (self.pred_and_action.auto_max_case(self.phs[self.id]) >=
                       case and not (self.pred_and_action.automation(
                           ViewEstablishmentEnums.PREDICATE, self.phs[self.id],
                           case))):
                    case += 1
                # Onces a predicates is fulfilled, perfom action if valid case
                if (self.pred_and_action.auto_max_case(self.phs[self.id]) >=
                        case):
                    self.pred_and_action.automation(
                        ViewEstablishmentEnums.ACTION, self.phs[self.id], case)

            # Emit run time metric
            run_time = time.time() - start_time
            run_method_time.labels(
                self.id, Module.VIEW_ESTABLISHMENT_MODULE).set(run_time)
            self.lock.release()
            # Stopping the while loop, used for testing purpose
            if testing:
                break

            # Send message to all other processors
            self.send_msg()
            throttle()