Skip to content
Snippets Groups Projects
README.md 6.98 KiB
Newer Older
thijsheijden's avatar
thijsheijden committed
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
[![Pipeline Status main](https://git.science.uu.nl/GraphPolaris/microservices-backbone/query-service/badges/main/pipeline.svg?key_text=pipeline%20main&key_width=90)](https://git.science.uu.nl/GraphPolaris/microservices-backbone/query-service/-/pipelines)
[![Pipeline Status develop](https://git.science.uu.nl/GraphPolaris/microservices-backbone/query-service/badges/develop/pipeline.svg?key_text=pipeline%20develop&key_width=105)](https://git.science.uu.nl/GraphPolaris/microservices-backbone/query-service/-/pipelines)
[![Coverage develop](https://git.science.uu.nl/GraphPolaris/microservices-backbone/query-service/badges/develop/coverage.svg?key_text=coverage%20develop&key_width=110)](https://git.science.uu.nl/GraphPolaris/microservices-backbone/query-service/-/commits/develop)
[![Coverage main](https://git.science.uu.nl/GraphPolaris/microservices-backbone/query-service/badges/main/coverage.svg?key_text=coverage%20main&key_width=94)](https://git.science.uu.nl/GraphPolaris/microservices-backbone/query-service/-/commits/main)
sivan's avatar
sivan committed

thijsheijden's avatar
thijsheijden committed
<div align='center'>
<img src="https://git.science.uu.nl/GraphPolaris/frontend/-/raw/develop/src/presentation/view/navbar/logogp.png" align="center" width="150" alt="Project icon">
thijsheijden's avatar
thijsheijden committed
</div>

## 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](https://git.science.uu.nl/GraphPolaris/query-conversion) and query execution code lives in [this repo](https://git.science.uu.nl/GraphPolaris/query-execution).
thijsheijden's avatar
thijsheijden committed

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](https://git.science.uu.nl/GraphPolaris/microservices-backbone/query-service/-/blob/develop/internal/usecases/consume/consume.go) on line 41.
thijsheijden's avatar
thijsheijden committed

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
<div align='center'>
<img src="images/query_service.png" align="center" width="75%" alt="Query service flow">
</div>

### Service Input
Here is the input message format.
```javascript
{
	"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.
```javascript
{
	"sessionID": "12345",
	"clientID": "12345",
	"queryID": "12345"
}
```

### Service Output

#### Successful Execution
```javascript
{
  "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:
```javascript
{
	"status": "query_processing_database",
	"queryID": "12345"
}
```

#### Errors
If the service encounters an error, a message of the following form will be sent:
```javascript
{
	"queryID": "12345",
	"type": "query_database_error",
	"value": "invalid database credentials"
}
```