예제 #1
0
class Logging(object):

    """
    Author: Adarsh Trivedi
    This class creates a timed rotating file logger. The rotation policies are picked
    from the provided configuration file.
    """

    handler = TimedRotatingFileHandler(
        filename=ConfigurationManager.get_configuration().get_log_file(),
        when=ConfigurationManager.get_configuration().get_log_frequency_interval_unit(),
        interval=ConfigurationManager.get_configuration().get_log_frequency_interval(),
        backupCount=ConfigurationManager.get_configuration().get_log_frequency_backup_count())

    console_handler = StreamHandler(stream=sys.stdout)

    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    @staticmethod
    def get_logger(name):

        logger = logging.getLogger(name)
        logger.addHandler(Logging.handler)
        if ConfigurationManager.get_configuration().get_should_log_to_console():
            print(ConfigurationManager.get_configuration().get_should_log_to_console())
            logger.addHandler(Logging.console_handler)
        logger.setLevel(logging.INFO)
        Logging.handler.setFormatter(Logging.formatter)
        return logger
예제 #2
0
    def get_logger(name):

        logger = logging.getLogger(name)
        logger.addHandler(Logging.handler)
        if ConfigurationManager.get_configuration().get_should_log_to_console():
            print(ConfigurationManager.get_configuration().get_should_log_to_console())
            logger.addHandler(Logging.console_handler)
        logger.setLevel(logging.INFO)
        Logging.handler.setFormatter(Logging.formatter)
        return logger
예제 #3
0
파일: rpc.py 프로젝트: adarshtri/chord-dht
    def start_server(chord_node):

        ip = ConfigurationManager.get_configuration().get_advertised_ip()
        port = ConfigurationManager.get_configuration().get_socket_port()

        if not XMLRPCChordServerManager.server and not XMLRPCChordServerManager.server_thread:
            XMLRPCChordServerManager.server = AsyncXMLRPCServer(
                (ip, port),
                ChordRPCRequestHandler,
                allow_none=True,
                logRequests=False)
            XMLRPCChordServerManager.server.register_instance(chord_node)
            XMLRPCChordServerManager.server_thread = \
                threading.Thread(target=XMLRPCChordServerManager.server.serve_forever)
            XMLRPCChordServerManager.server_thread.daemon = True
            XMLRPCChordServerManager.server_thread.start()
예제 #4
0
    def start_nodes(self):

        for i in range(len(self._config)):

            fp = open(self._config_file, 'w')
            fp.write(json.dumps(self._config[i]))
            fp.close()

            ConfigurationManager.reset_configuration()

            server_ip = self._config[i]["ip"]
            server_port = self._config[i]["socket_port"]
            server_id = self._node_ids[i]
            bootstrap_server = self._config[i]["bootstrap_server"]

            self._nodes[i] = TestNode(node_id=server_id,
                                      node_ip=server_ip,
                                      bootstrap_node=bootstrap_server)

            self.start_chord_node(self._nodes[i], i)
            self._nodes[i].join()
            self.scheduler[i].enter(
                ConfigurationManager.get_configuration().
                get_stabilize_interval(), 1, stabilize_call, (
                    self._nodes[i],
                    self.scheduler[i],
                ))
            self.stabilization_thread[i] = threading.Thread(
                target=self.scheduler[i].run, args=(True, ))
            self.stabilization_thread[i].start()
            print("Started node {}.".format(i + 1))
            self._state[i] = True
            time.sleep(3)
예제 #5
0
파일: node.py 프로젝트: adarshtri/chord-dht
    def __init__(self, node_id, node_ip, bootstrap_node=None):
        """
        Author: Adarsh Trivedi
        :param node_id: Hash node id, represents id on the chord ring.
        :param node_ip: IP of the node.
        :param bootstrap_node: Bootstrap server to start with. Can be None for new chord ring formation.
        """

        self._node_id = node_id
        self._node_ip = node_ip
        self._config = ConfigurationManager.get_configuration()
        self._port = self._config.get_socket_port()
        self._bootstrap_server = bootstrap_node
        self._finger_table = FingerTable(size=self._config.get_m_bits())
        self.predecessor = None
        self.successor = None
        self.successor_list = [
            (self.get_node_id(), self.get_connection_string())
        ] * 3

        if not bootstrap_node:
            logger.info("Creating new node, with no bootstrap server.")
            logger.info("Node ip: {}".format(node_ip))
            logger.info("Node id: {}".format(str(node_id)))
        else:
            logger.info("Creating new node. Node ip {}. Node id {}.".format(
                node_ip, node_id))

        self._set_default_node_parameters()

        self._store = {}
예제 #6
0
def stabilize_call(chord_node) -> None:
    try:
        chord_node.stabilize()
        chord_node.fix_fingers()
        #chord_node.replication_stabilization()
    except Exception as e:
        logger.exception("Something went wrong with stabilization.")
    scheduler.enter(
        ConfigurationManager.get_configuration().get_stabilize_interval(), 1,
        stabilize_call, (chord_node, ))
예제 #7
0
    def start_server():
        """
        Start the socket server to listen on the specified port.
        Sets the attribute "open_server" of the class which is used to later close the server on stop_server() method.
        :return: None
        """

        ip = ConfigurationManager.get_configuration().get_advertised_ip()
        port = ConfigurationManager.get_configuration().get_socket_port()

        if not ChordSocketServerThreadManager.server:
            ChordSocketServerThreadManager.server = \
                ThreadedChordTCPServer((ip, port), ChordSocketServerHandler)

            ChordSocketServerThreadManager.server_thread = \
                threading.Thread(target=ChordSocketServerThreadManager.server.serve_forever)

            ChordSocketServerThreadManager.server_thread.daemon = True
            ChordSocketServerThreadManager.server_thread.start()
예제 #8
0
def stabilize_call(chord_node, schedule) -> None:
    try:
        chord_node.stabilize()
        chord_node.fix_fingers()
        #chord_node.replication_stabilization()
    except Exception as e:
        print(e)
    schedule.enter(
        ConfigurationManager.get_configuration().get_stabilize_interval(), 1,
        stabilize_call, (
            chord_node,
            schedule,
        ))
예제 #9
0
파일: node.py 프로젝트: adarshtri/chord-dht
    def __init__(self, ip, identifier, port, finger_number,
                 my_chord_server_node_id):
        """
        Author: Adarsh Trivedi
        :param ip: Ip of the finger pointing to node.
        :param identifier: Hash ID of the finger pointing to node.
        :param port: Port of the finger pointing to node.
        :param finger_number: Finger number in the finger table.
        :param my_chord_server_node_id: Node id of the chord node of which this finger is in.
        """
        self._my_chord_server_node_id = my_chord_server_node_id
        self._config = ConfigurationManager.get_configuration()
        self._ip = ip
        self._identifier = identifier
        self._port = port
        self._finger_number = finger_number
        self.start = (self._my_chord_server_node_id +
                      (2**(self._finger_number - 1))) % (
                          2**self._config.get_m_bits())

        # should be successor of _start
        self.node = None
        self.xml_client = None
        self._connection_string = ip + ":" + str(port)
예제 #10
0
    return configuration_file, bootstrap_server, server_id, no_hash


if __name__ == "__main__":

    configuration_file, bootstrap_server, server_id, no_hash = get_arguments()

    os.environ[ConfigurationConstants.
               CHORD_CONFIGURATION_FILE_ENV_VARIABLE] = configuration_file
    ConfigurationManager.reset_configuration()
    from chord.node import Node
    from utilities.app_logging import Logging
    logger = Logging.get_logger(__name__)

    server_ip = ConfigurationManager.get_configuration().get_chord_server_ip()
    server_port = ConfigurationManager.get_configuration().get_socket_port()
    if not server_id:
        server_id = Consistent_Hashing.get_modulo_hash(
            server_ip + ":" + str(server_port),
            ConfigurationManager.get_configuration().get_m_bits())
    node = Node(node_id=server_id,
                node_ip=server_ip,
                bootstrap_node=bootstrap_server)

    start_chord_node(node)
    node.join()
    scheduler.enter(
        ConfigurationManager.get_configuration().get_stabilize_interval(), 1,
        stabilize_call, (node, ))
    stabilization_thread = threading.Thread(target=scheduler.run,