mollydb hooks in action
Along this post we will be guided on how to proceed to take advantage of mollydb hooks.
A couple of scenarios that we usually face when we are working with micro-services are:
- Try to increase the log level to the services with no down time(and this could be a pain in the neck).
- Being able to change database connection without restarting the application.
MollyDB in action
We are going to create a storage, named ms, and this storage will host a document for our application, the document for the application (ms-users) will look like this:
ms-users.yml
logLevel: debug
db:
connection: mongodb://mongodb.wesovilabs.com:27017/users
Launch a real scenario
To run the sample we will take advantage of docker-compose and the descriptor will look like this:
version: '3'
services:
mollydb:
image: wesovilabs/mollydb:0.0.1-alpha
volumes:
- "./mollydb/data:/var/mollydb/storage/ms"
ports:
- 7000:9090
users:
image: golang:1.9
container_name: ms-users
volumes:
- "./ms-users:/go/src/ms-users"
ports:
- 7001:3333
working_dir: /go/src/ms-users
depends_on:
- mollydb
links:
- mollydb
entrypoint: go run main.go
initializer:
image: centos:7
volumes:
- "./initializer/setup.sh:/setup.sh"
depends_on:
- mollydb
entrypoint: sh /setup.sh
As we can observe in the above docker-compose.yml, we launch a service, named ms-users, mollydb and also an ephemeral container that will be only used to register a storage in mollydb
We can get the environment up an running by:
git clone https://github.com/wesovilabs/mollydb.git
cd mollydb/samples/001-microservices_configuration
docker-compose up -d
The application ms-users make an initial connection to mollydb to take their configuration. (have a look at function init in files ./ms-users/main.go. It makes the below GraphQL queries:
query FindProperties {
properties(storage: "ms", document: "ms-users) {
path
key
value
}
}
Once the docker-compose is up and running we can monitor the docker-compose logs by running:
docker-compose logs -f users
then, from a another terminal we can create a user
curl http://localhost:7001/users \
-d '{"name":"john.doe"}' \
-H "Contenty-type:application/json" -i
and the logs displayed will look like these (important the line in blond style)
ms-users | 2018/04/02 05:55:21 “PUT http://users:3333/configuration HTTP/1.1” from 172.29.0.2:56836–405 0B in 32.284µs
ms-users | time=”2018–04–02T05:55:30Z” level=debug msg=”retrieving request from localhost:7001"
ms-users | time=”2018–04–02T05:55:30Z” level=debug msg=”read request body”
ms-users | time=”2018–04–02T05:55:30Z” level=debug msg=”unmarshal request body”
ms-users | time=”2018–04–02T05:55:30Z” level=info msg=”establishing connection wtih mongodb://mongodb.wesovilabs.com:27017/users”
ms-users | 2018/04/02 05:55:30 “POST http://localhost:7001/users HTTP/1.1" from 172.29.0.1:46412–200 13B in 593.603µs
ms-users | time=”2018–04–02T05:55:30Z” level=info msg=”user john.doe was created successfully”
Deep into mollydb
In the previous step, we said that an ephemeral container was registering a storage in mollydb. The registered storage is hosted in path ./mollydb/data. ( We mount a volume in docker-compose) We can interact with mollydb by running GraphQL queries, for example the list of properties grouped by document and storage:
curl -XPOST http://localhost:7000/graphql \
-H 'Content-Type: application/graphql' \
-d "query StorageQuery {
storageList {
name
len
documents {
name
len
properties {
key
value
path
}
}
}
}
" \
-i
and we’ll get the response
{
"data": {
"storageList": [
{
"documents": [
{
"len": 2,
"name": "ms-users",
"properties": [
{
"key": "logLevel",
"path": "mollydb://ms/ms-users?key=logLevel",
"value": "DEBUG"
},
{
"key": "db.connection",
"path": "mollydb://ms/ms-users?key=db.connection",
"value": "mongodb://mongodb.wesovilabs.com:27017/users"
}
]
}
],
"len": 1,
"name": "ms"
}
]
}
}
Full GraphQL API can be found on https://github.com/wesovilabs/mollydb#graphql
Creating a hook
So now, we are going to register a hook that will notify service ms-users when db.connection is changed.
curl -XPOST http://localhost:7000/graphql \
-H 'Content-Type: application/graphql' \
-d "mutation AddHook {
propertyRestHook(
uri: \"http://users:3333/configuration\",
verb: \"POST\",
path: \"mollydb://ms/ms-users?key=db.connection\"
)
}" \
-i
What we are saying with the previous script can be translated as “When property logLevel in document ms-user from storage ms changes, then mollydb will make a POST request to URI http://users:3333/configuration
so if we modify file mollydb/data/ms-users.yml and update property db.connection to mongodb://mongodb.wesovilabs.com:27030/users
then when creating a user again
curl http://localhost:7001/users \
-d '{"name":"john.doe"}' \
-H "Contenty-type:application/json" -i
we will observe that the database connection url in the log has changed.
ms-users | 2018/04/02 06:06:46 "POST http://localhost:7001/users HTTP/1.1" from 172.29.0.1:46454 - 200 13B in 174.213µs
ms-users | time="2018-04-02T06:06:46Z" level=debug msg="retrieving request from localhost:7001"
ms-users | time="2018-04-02T06:06:46Z" level=debug msg="read request body"
ms-users | time="2018-04-02T06:06:46Z" level=debug msg="unmarshal request body"
ms-users | time="2018-04-02T06:06:46Z" level=info msg="establishing connection wtih mongodb://mongodb.wesovilabs.com:27030/users"
ms-users | time="2018-04-02T06:06:46Z" level=info msg="user john.doe was created successfully"
Challenge
Feel free to fork this sample and make the changes you prefer. Yo could add a new service or maybe modify the logLevel by hooks registration!
External links
- MollyDB code can be found on https://github.com/wesovilabs/mollydb
Any feedback will be more than welcome!!