Milvus is an open-source vector database that facilitates similarity search and AI applications.
If you are not familiar with vector databases, check our tutorial https://cloudenv.io/what-vector-databases
Milvus supports various programming languages, such as Java, and offers a suite of APIs for interacting with the databases.
One of these valuable APIs is the getMetrics()
method in Java. This method is compelling as it allows us to fetch system-level metrics from a Milvus instance. We can use these metrics for performance monitoring, checking the cluster health, and debugging.
In this tutorial, we will walk you through configuring a Java application to interact with the Milvus instance and using the getMetrics()
method to fetch information about the cluster.
Requirements
Before diving into the tutorial, you will need to have a few things configured on your machine:
- Java Development Kit (JDK) 11 or above installed.
- Apache Maven for project management and building the Java project.
- Milvus server running locally or remotely.
- Java Milvus SDK installed in your project. You can add it as a dependency via Maven.
This tutorial will use a locally hosted Milvus instance running on Docker.
Milvus getMetrics() Method
The getMetrics()
method in the Milvus Java SDK provides detailed insights such as CPU usage, RAM usage, disk usage, and network traffic. In addition, it provides the metrics of Milvus components such as query nodes, index nodes, and data nodes.
The method definition is as shown below:
R<GetMetricsResponse> getMetrics(GetMetricsParam requestParam);
The method returns the information about runtime Milvus metrics in JSON format.
Project Setup
To better understand how the function works, let us learn how to use it in an application. Start by creating a new Maven project using your favorite IDE and tools.
Install Milvus SDK
Once we have created the project, we must install the Milvus SDK as a Maven dependency. First, open the pom.xml file and add the entry shown below in the maven dependencies block.
<dependency>
<groupId>io.milvus</groupId>
<artifactId>milvus-sdk-java</artifactId>
<version>2.2.5</version>
</dependency>
Save the maven pom.xml file and allow Maven to download and index the dependencies.
Create Milvus Client
Before we can use the getMetrics() function, we need to create a Milvus client. We will use this client to connect to the Milvus server and interact with the database.
In the Java source file, add the source code as shown:
package org.example;
import io.milvus.client.MilvusClient;
import io.milvus.client.MilvusServiceClient;
import io.milvus.param.ConnectParam;
public class Main {
public static void main(String[] args) {
final MilvusServiceClient milvusClient = new MilvusServiceClient(
ConnectParam.newBuilder()
.withHost("localhost")
.withPort(19530)
.build()
);
}
}
This should allow us to initialize a new Milvus client using the specified host and port. You can modify these values to fit the address and port of your Milvus instances.
Call the getMetrics() Function
Once we have configured the Milvus client, we can call the getMetrics() function to fetch the metrics. The function returns a GetMetricsResponse object. The code snippet is as shown below:
GetMetricsParam param = GetMetricsParam.newBuilder()
.withRequest("{\"metric_type\":\"system_info\"}")
.build();
R<GetMetricsResponse> response = milvusClient.getMetrics(param);
if (response.getStatus() != R.Status.Success.getCode()) {
System.out.println(response.getMessage());
}
System.out.println(response.getData().getResponse());
Once we run the code above, it should return a string containing information about the system. The resulting value is a JSON string that we can parse.
An example output is as shown:
{
"nodes_info": [
{
"identifier": 10,
"connected": null,
"infos": {
"has_error": false,
"error_reason": "",
"name": "querynode10",
"hardware_infos": {
"ip": "172.18.0.4:21123",
"cpu_core_count": 12,
"cpu_core_usage": 0,
"memory": 8251953152,
"memory_usage": 274948096,
"disk": 104857600,
"disk_usage": 2097152
},
"system_info": {
"system_version": "9ffcd53b",
"deploy_mode": "STANDALONE",
"build_version": "v2.2.9",
"build_time": "Fri Jun 2 09:38:35 UTC 2023",
"used_go_version": "go version go1.18.3 linux/amd64"
},
"created_time": "2023-06-02 15:44:38.8342373 +0000 UTC m=+0.598175001",
"updated_time": "2023-06-02 15:44:38.8342374 +0000 UTC m=+0.598175101",
"type": "querynode",
"id": 10,
"system_configurations": {
"simd_type": "AVX2"
},
---
It is good to remember that the resulting value is an extensive JSON string. Therefore, you can use JSON parsing tools such as GSON in Java to parse the object and access individual values.
Conclusion
In this tutorial, you learned how to fetch and parse system metrics from a Milvus instance using the Java SDK. Monitoring these metrics will help you keep track of your system's health and optimize its performance.