Skip to content

Developer Guide

Preetam Keshari Nahak edited this page Aug 16, 2019 · 8 revisions

If you're a passionate developer and open-source contributor, if you wish to suggest and code new features, bug fixes etc to sharkradar, then you're in the right place. This developer's guide will navigate you through the existing folder structure, purpose and code implementation in details.

So what're you waiting for!!! Let's dive in :D.

The following demonstrates the root folder structure of sharkradar repo.

Root folder structure of sharkradar project

Major components are:-

  • Controller.py
  • Health.py
  • Discovery.py
  • Main.py
  • sharkradarDbutils.py

  1. Controller.py
  • Controller.py creates a flask app and defines two routes/api endpoints, /health and /discovery/<service_name> which are directly coupled with Health.py and Discovery.py class.

  • /health route prepares the required json object and send it as input parameter to health API defined in Health.py.

  • It collects the response from health API and sends a bool value True | False to the client (microservice) depending upon whether the request was successfully processed by health API or not.

  • /discovery/<service_name> route sends the ip and port of the micro-service instance requested by service_name parameter.

  1. sharkradarDbutils.py
  • This file contains all the functions required to communicate with database and perform necessary CRUD operations. The available functionalities are:-

    • createTableIfNotExists() - Creates a table in db with required fields (Details of table schema are included in Usage Guide.)
    • findServiceByNameAndIpAndPort(service_name, ip, port) - Fetch a record of service name from db by service_name, ip and port
    • findServiceByName(service_name) - Fetch record by service_name
    • updateServiceByAll(current_time_stamp, health_interval, mem_usage, cpu_usage, nw_tput_bw_ratio, req_active_ratio, success_rate, service_name, ip, port) - Updates the details of a record pointed by the service_name, ip and port.
    • insertServiceByAll(service_name, ip, port, current_time_stamp, health_interval, mem_usage, cpu_usage, nw_tput_bw_ratio, req_active_ratio, success_rate) - Insert a new record in the table with the required input params
    • deleteServiceByNameAndIpAndPort(service_name, ip, port) - Deletes a record by service_name, ip, port
    • deleteServiceByNameAndTimestampDifferenceWithHealthInterval(service_name) - Deletes a service record by checking if a client (micro-service instance) fails to send health report regularly or not.
  1. Health.py
  • This class implements a static method named health(req_body) which takes a json object named req_body as its input parameter. Details of the keys of this json object are mentioned in [https://github.com/bmonikraj/sharkradar/wiki/Usage-Guide](Usage Guide).

  • When a client (micro-service) sends its health report to sharkradar by making a PUT request to the health(req_body) method, the method decides whether to register, update or unregister the client by applying the following checks:-

    • If status sent by the client is "up" and it sent the report before its specified health_interval expires, then the health(req_body) method maintains the client's record in db updated. If the client sends health report for the first time, then a new record is inserted. Else existing record is updated.
    • Else the client will be unregistered from sharkradar and deleted from db.
  1. Discovery.py
  • This class implements a GET method named discovery(service_name) which takes the name of the service as its input parameter.
  • This method first removes all the micro-service instances which have failed to send health report in their specified health_interval and the difference (current_timestamp - previous_time_stamp) > health_interval.
  • Then it finds the list of all instances of required service_name.
  • It calculates the priority score of each instance based on different weights assigned to different input parameters. A priority based algorithm is used
  • It then sends a tuple containing the ip and port of the instance with highest score, e.g ("192.168.233.33", "9090").
  • If no instance of the requested service_name is found, then this function returns a empty tuple, e.g ("", "")
  1. Main.py
  • It runs the sharkradar server at a specified ip and port.
Clone this wiki locally