/micronaut-arangodb

⚙️ Integration between Micronaut and ArangoDB.

Primary LanguageJavaApache License 2.0Apache-2.0

Micronaut ArangoDB Configuration

Java CI Quality Gate Status Coverage Maintainability Rating

This project includes integration between Micronaut and ArangoDB.

Dependency 🚀

Starting from version 2.0.0 library ships for Micronaut 2.

Starting from version 2.1.0 Java 11+ is required (previous version 1.8+ compatible).

Last release for Micronaut 1 is version 1.2.1.

Gradle

dependencies {
    compile 'com.github.goodforgod:micronaut-arangodb:2.1.0'
}

Maven

<dependency>
    <groupId>com.github.goodforgod</groupId>
    <artifactId>micronaut-arangodb</artifactId>
    <version>2.1.0</version>
</dependency>

Configuration

Includes a configuration to automatically configure the native ArangoDB Java drive. Just configure the host, port, credentials (if needed) of the ArangoDB accessor in application.yml.

arangodb:
  host: localhost     # default
  port: 8529          # default
  database: _system   # default (is used for health check)
  user: root          # default
  password: 1234      # or no pass if auth is not required

To use the drivers, just add a dependency to your application.

compile 'com.arangodb:arangodb-java-driver'

Accessors

Both async ArangoDBAsync and sync ArangoDB accessors are then available for dependency injection.

Accessors injected as prototypes beans remember that while using them.

@Inject
private ArangoDBAsync async;

@Inject
private ArangoDB sync;

Clients

Configuration supports setup database for your application (ArangoDB accessors do not require or have database config).

So in order to use database specified as per configuration inject provided Arango Clients instead.

Clients injected as singletons beans remember that while using them.

@Inject
private ArangoClientAsync asyncClient;

@Inject
private ArangoClient syncClient;

Both clients provide as sync and async implementation and are same accessors but with knowledge about database specified per config. So you can use connection with knowledge about database your app is working with.

@MicronautTest
class ArangoClientTests {

    @Inject
    private ArangoClientAsync asyncClient;    

    @Inject
    private ArangoClient syncClient;    

    void checkConfiguredDatabase() {
        final String databaseAsync = asyncClient.getDatabase(); // Database as per config
        final String databaseSync = syncClient.getDatabase(); // Database as per config
        assertEquals(database, database);
    
        final ArangoDBAsync async = asyncClient.accessor();
        final ArangoDB sync = syncClient.accessor();
    }
}

In case you want inject clients as prototypes you can use named bean injection.

@Named("prototype")
@Inject
private ArangoClientAsync asyncClient;    

@Named("prototype")
@Inject
private ArangoClient syncClient;    

Configuring ArangoDB Driver

All accessors and clients are provided as refreshable with arangodb key for bean refresh.

Configuration supports all available ArangoDB driver settings.

Configuring timeout, chunksize, maxConnections, connectionTtl, acquireHostList, loadBalancingStrategy for clients & accessors

Check ArangoDB official info about each parameter.

arangodb:
  timeout: 3000                         # default - 0 in milliseconds
  chunksize: 3000                       # default - 30000
  useSsl: true                          # default - false
  maxConnections: 30                    # default - 1
  connectionTtl: 200                    # default - null
  acquireHostList: true                 # default - false
  loadBalancingStrategy: ONE_RANDOM     # default - NONE (check LoadBalancingStrategy for more)

Database Initialization

There is an option to initialize database if it doesn't exist on startup via createDatabaseIfNotExist option.

Use this option if your service is lazy initialized, to set up database for HealthCheck.

arangodb:
  create-database-if-not-exist: true    # default - false

Default timeout for operation set to 10000 millis, if you want to specify timeout in seconds for database creation on startup you can set it via property.

arangodb:
  create-database-timeout-in-millis: 500 # default - 10000

Health Check

Health check for ArangoDB is provided and is turned on by default. HeathCheck is active for database that is specified in configuration.

ArangoDB health check is part of Micronaut Health Endpoint.

Example of ArangoDB health:

{
  "name": "service",
  "status": "UP",
  "details": {
    "arangodb": {
      "name": "arangodb",
      "status": "UP",
      "details": {
        "version": "3.6.1",
        "database": "_system"
      }
    }
  }
}

Where database version is specified and database name service is connected to as per configuration.

You can explicitly turn off health check.

arangodb:
  health:
    enabled: false      # default - true 

Cluster Health Check

There is also available ArangoDB Cluster Health Check that monitors cluster health (if service is connected to cluster ArangoDB) and reports is nodes that can not be deleted according to documentation from cluster are down, so application is also down.

In other case application will be UP and running with errors like various connection issues due to unstable cluster.

Both health checks will be present in health output if all are enabled.

ArangoDB Cluster Health output example:

{
  "name": "service",
  "status": "UP",
  "details": {
    "arangodb (cluster)": {
      "name": "arangodb (cluster)",
      "status": "UP",
      "details": {
        "clusterId": "89b7e1a8-53f5-44ea-bb5c-9e7cb201417c",
        "nodes": [
          {
            "name": "Coordinator0002",
            "status": "GOOD"
          },
          {
            "name": "Coordinator0001",
            "status": "GOOD"
          },
          {
            "name": "DBServer0001",
            "status": "GOOD"
          },
          {
            "name": "DBServer0002",
            "status": "GOOD"
          },
          {
            "name": "AGENT (AGNT-4206f181-8791-4c3f-952d-79d9aa58b7c2)",
            "leading": true,
            "status": "GOOD"
          },
          {
            "name": "AGENT (AGNT-2cc832bf-a8b7-4f8a-823a-15d778594bc1)",
            "status": "GOOD"
          },
          {
            "name": "AGENT (AGNT-78172be0-1adc-47ea-8152-3a3b0ab0b10e)",
            "status": "GOOD"
          }
        ]
      }
    }
  }
}

HealthCheck provides status of each node in cluster and their ShortName for DBServer and Coordinator or NodeID for Agent nodes and flag for leading Agent node.

You can turn on Cluster Health Check via configuration:

arangodb:
  health-cluster:
    enabled: true      # default - false

Testing

For testing purposes it is recommended to use ArangoDB TestContainer library (this project tested via that library).

TestContainers allows you to use integration tests against real database in all docker friendly environments, check here for TestContainers.

Version History

2.1.0 - Java updated to 11, Micronaut updated to 2.1.1, improved config autocompletion.

2.0.0 - Improved database handling exceptions, improved ArangoClusterHealth.

2.0.0 - Micronaut 2 support, database init timeout property added, dependency updated.

1.2.1 - Minor HealthCheck improvements, dependencies updated.

1.2.0 - Improved ArangoDB Health Check (more accurate db state check).

1.1.0 - Client as prototype injection added, health indicators accessors instead of clients, sync client renamed.

1.0.0 - Initial version, sync and async clients and drivers injection, database initialization, health check, cluster health check.

License

This project licensed under the Apache License 2.0 - see the LICENSE file for details.