Esempio n. 1
0
    def call(self):
        """
        Process the message.

        Calls application callbacks and then updates the global state.
        """
        thread = threading.current_thread()
        logging.info('{0} invokes message: {1}'.format(
            str(thread), self._message))
        from_state = self._message['simpleFields']['FROM_STATE']
        to_state = self._message['simpleFields']['TO_STATE']
        session_id = self._participant.get_session_id()
        resource_name = self._message['simpleFields']['RESOURCE_NAME']
        partition_name = self._message['simpleFields']['PARTITION_NAME']
        try:
            parser = self._state_model_parser
            method_to_invoke = parser.get_method_for_transition(
                self._state_model, from_state, to_state)
            logging.info('method_to_invoke: {0}'.format(str(method_to_invoke)))
            method_to_invoke(self._message)
        except Exception as e:
            logging.error('{0}-{1} transition failed, {2}'.format(
                from_state, to_state, e))
            to_state = 'ERROR'
            error_key = self._builder.error(
                self._participant_id, session_id, resource_name,
                partition_name)
            error_node = znode.get_empty_znode(partition_name)
            error_node['simpleFields']['ERROR'] = str(e)
            self._accessor.update(error_key, error_node)
        self._state_model._current_state = to_state

        # update current-state, then remove message
        sub = False
        current_state = znode.get_empty_znode(resource_name)
        current_state['mapFields'][partition_name] = {}
        if to_state != 'DROPPED':
            # update the current state
            current_state['mapFields'][partition_name]['CURRENT_STATE'] = (
                to_state)
            current_state['simpleFields']['STATE_MODEL_DEF'] = (
                self._message['simpleFields']['STATE_MODEL_DEF'])
            current_state['simpleFields']['SESSION_ID'] = session_id
        else:
            # drop the partition from the current state
            sub = True
        self._accessor.update(self._builder.current_state(
            self._participant_id, session_id,
            resource_name), current_state, sub=sub)
        self._accessor.remove(self._builder.message(
            self._participant_id, self._message['id']))
Esempio n. 2
0
    def _ensure_participant_config(self):
        """
        Ensure that ZNodes for a participant all exist, non-atomic (private)

        Returns:
            True if everything was persisted, False otherwise
        """
        exists = self._accessor.exists(
            self._builder.participant_config(self._participant_id))
        if not exists and self._auto_join_allowed():
            node = znode.get_empty_znode(self._participant_id)
            node['simpleFields'] = {
                'HELIX_HOST': self._host, 'HELIX_PORT': str(self._port),
                'HELIX_ENABLED': 'true'}
            self._accessor.create(
                self._builder.participant_config(self._participant_id), node)
            self._accessor.create(
                self._builder.instance(self._participant_id), b'')
            self._accessor.create(
                self._builder.current_states(self._participant_id), b'')
            self._accessor.create(
                self._builder.errors(self._participant_id), b'')
            self._accessor.create(
                self._builder.health_report(self._participant_id), b'')
            self._accessor.create(
                self._builder.messages(self._participant_id), b'')
            self._accessor.create(
                self._builder.status_updates(self._participant_id), b'')
        elif not exists:
            return False
        return True
Esempio n. 3
0
    def _ensure_participant_config(self):
        """
        Ensure that ZNodes for a participant all exist, non-atomic (private)

        Returns:
            True if everything was persisted, False otherwise
        """
        exists = self._accessor.exists(
            self._builder.participant_config(self._participant_id))
        if not exists and self._auto_join_allowed():
            node = znode.get_empty_znode(self._participant_id)
            node['simpleFields'] = {
                'HELIX_HOST': self._host,
                'HELIX_PORT': str(self._port),
                'HELIX_ENABLED': 'true'
            }
            self._accessor.create(
                self._builder.participant_config(self._participant_id), node)
            self._accessor.create(self._builder.instance(self._participant_id),
                                  b'')
            self._accessor.create(
                self._builder.current_states(self._participant_id), b'')
            self._accessor.create(self._builder.errors(self._participant_id),
                                  b'')
            self._accessor.create(
                self._builder.health_report(self._participant_id), b'')
            self._accessor.create(self._builder.messages(self._participant_id),
                                  b'')
            self._accessor.create(
                self._builder.status_updates(self._participant_id), b'')
        elif not exists:
            return False
        return True
Esempio n. 4
0
    def _create_live_instance_node(self):
        """
        Create a live instance ZNode for this participant (private)

        Returns:
            True if created, False otherwise
        """
        node = znode.get_empty_znode(self._participant_id)
        node['simpleFields'] = {
            'HELIX_VERSION': 'pyhelix-{0}'.format(constants.CURRENT_VERSION),
            'SESSION_ID': str(self._client.client_id[0]),
            'LIVE_INSTANCE': '{0}@{1}'.format(os.getpid(), self._host)}
        return self._accessor.create(
            self._builder.live_instance(self._participant_id), node)
Esempio n. 5
0
    def _create_live_instance_node(self):
        """
        Create a live instance ZNode for this participant (private)

        Returns:
            True if created, False otherwise
        """
        node = znode.get_empty_znode(self._participant_id)
        node['simpleFields'] = {
            'HELIX_VERSION': 'pyhelix-{0}'.format(constants.CURRENT_VERSION),
            'SESSION_ID': str(self._client.client_id[0]),
            'LIVE_INSTANCE': '{0}@{1}'.format(os.getpid(), self._host)
        }
        return self._accessor.create(
            self._builder.live_instance(self._participant_id), node)