Skip to content
Snippets Groups Projects

License Pipeline Status main Pipeline Status develop Coverage develop Coverage main

Project icon

Query Service

This service handles the conversion and execution of queries sent from the frontend. The conversion and execution code was moved into packages, to be able to re-use this service as the shell. Query conversion code (to convert from our JSON format to AQL or Cypher) lives in this repo and query execution code lives in this repo.

The reason the conversion and execution code are seperated is because there can be multiple databases using the same query language, while they each have their own driver.

Dev Dependencies

Here are the developer dependencies. Coding can be done without these tools, but they sure make your life easier.

  • Make
  • Docker

Dependencies

This service depends on quite some other services, as it is essentially just a shell for the query conversion and execution packages.

  • RabbitMQ
  • Redis
  • Minio
  • User management service (for database credentials)

Environment Variables

To decide which conversion and execution packages will be used the service uses environment variables. The QUERY_CONVERSION_LANGUAGE variable determines which query conversion package will be used. Currently only aql and cypher are supported. The QUERY_EXECUTION variable determines the executor that will be used. This variable also changes the queue and routing key the service listens to, as can be seen here on line 41.

There are some more environment variables that are used in this service, which are listed below:

  • RABBIT_HOST address of the RabbitMQ instance (string)
  • RABBIT_PORT RabbitMQ port (int)
  • RABBIT_USER RabbitMQ username (string)
  • RABBIT_PASSWORD RabbitMQ password (string)
  • REDIS_ADDRESS Redis address (string)
  • LOG_MESSAGES Whether to log messages (bool)
  • MINIO_ADDRESS Minio address (string)
  • MINIO_ACCESSKEYID Minio username (string)
  • MINIO_ACCESSKEY Minio password (string)

Building the Service

To compile the service into a single binary, the following commands can be used. Go can build for many platforms, so we have three build commands, for the three main platforms.

make linux
make windows
make macos

To build the service and push it to the Docker registry the make docker command can be used.

Testing and Coverage

To test the make test command can be used. It will run every test in the repo. To get the code coverage the command make coverage can be used. This command will run all tests and display total coverage over all files. It will also generate an html file which displays exactly which lines have been covered.

Kubernetes

All the Kubernetes config files live in the /deployments directory. This service has two deployments, which only differ in the environment variables being set for query conversion and execution. This service also has two horizontal pod autoscalers, which will scale the service based on CPU load.

Service Flow

Query service flow

Service Input

Here is the input message format.

{
	"databaseName": "test_db",
  "return": {
    "entities": [
      0
    ],
    "relations": [
      0
    ]
  },
  "entities": [
    {
      "type": "airports",
      "constraints": [
        {
          "attribute": "city",
          "value": "New York",
          "dataType": "text",
          "matchType": "exact"
        }
      ]
    }
  ],
  "relations": [
    {
      "type": "flights",
      "depth": {
        "min": 1,
        "max": 1
      },
      "entityFrom": 0,
      "entityTo": -1,
      "constraints": [
        {
          "attribute": "Month",
          "value": "1",
          "dataType": "number",
          "matchType": "exact"
        }
      ]
    }
  ],
  "limit": 1000,
  "modifiers": [
    {
      "type": "COUNT",
      "selectedType": "entity",
      "id": 0,
      "attributeIndex": -1
    }
  ] 
}

Some additional headers also have to be set in the RabbitMQ message, these should be set by the query orchestrator.

{
	"sessionID": "12345",
	"clientID": "12345",
	"queryID": "12345"
}

Service Output

Successful Execution

{
  "type": "query_result",
  "value": {
    "edges": [
      {
        "_from": "airports/JFK",
        "_id": "flights/286552",
        "_key": "286552",
        "_rev": "_cLqg0be--U",
        "_to": "airports/SFO",
        "attributes": {
          "ArrTime": 2332,
          "ArrTimeUTC": "2008-01-16T07:32:00.000Z",
          "Day": 15,
          "DayOfWeek": 2,
          "DepTime": 2006,
          "DepTimeUTC": "2008-01-16T01:06:00.000Z",
          "Distance": 2586,
          "FlightNum": 649,
          "Month": 1,
          "TailNum": "N597JB",
          "UniqueCarrier": "B6",
          "Year": 2008
        }
      }
    ],
    "nodes": [
      {
        "_id": "airports/SFO",
        "_key": "SFO",
        "_rev": "_cLp3eL6-_G",
        "attributes": {
          "city": "San Francisco",
          "country": "USA",
          "lat": 37.61900194,
          "long": -122.3748433,
          "name": "San Francisco International",
          "state": "CA",
          "vip": true
        }
      }
    ]
  }
}

Status Updates

The service will also publish updates regarding certain queries. These follow the following pattern:

{
	"status": "query_processing_database",
	"queryID": "12345"
}

Errors

If the service encounters an error, a message of the following form will be sent:

{
	"queryID": "12345",
	"type": "query_database_error",
	"value": "invalid database credentials"
}