Milvus1.1版本部署和使用小记

1,560 阅读13分钟

Milvus1.1版本部署和使用小记

背景:相关推荐业务需要根据用户点击的文章召回出一批同类型的文章。生成的 embedding 数据,存储和搜索是一大难题,借助 milvus 向量索引服务可以快速的解决这个问题,并且有着不错的搜索性能。

摘要:本文主要介绍了 milvus 基本知识, milvus 1.1 集群版本的搭建和 milvus 在使用中遇到的一些问题。

一、milvus 介绍

何谓向量数据库?

向量数据库是一个工具,可以存储并索引向量数据,能够实验快速检索和相似性搜索的功能。其存储一些非结构化数据,也具备传统结构型数据库(eg:MySQL)的一些基本功能。向量数据的两个核心功能:索引构建相似性搜索

milvus 是一个向量数据库,具有向量存储,搜索,索引等功能,支持浮点向量和二值向量,支持的索引类型包括: flat,inf_flat, hasw, annoy 等, 支持的距离计算公式包括:欧式距离,内积,汉明距离等,由于其优秀的实现方式,在索引构建和查询上具有非常好的性能表现。

milvus 开源时间并不长,但其在开源社区的活跃度非常高,目前算是开源的向量索引服务中一个非常棒的项目,并且在1.1版本之后的集群部署方案上,使得 milvus 能够更好的被互联网公司所使用。官方把其作为一个开源项目长期维护,对其的定义是:

Milvus 于 2019 年开源,致力于存储、索引和管理由深度神经网络学习与其他机器学习模型生成的海量 Embedding 向量,与现有的主要用作处理结构化数据的关系型数据库不同,Milvus 在底层设计上就是为了处理由各种非结构化数据转换而来的 Embedding 向量而生。

二、Milvus 1.1-cpu版本安装指南

1. 所需组件

本次安装用到的组件有: docker20.10, milvus:1.1.1-centos7-oss, mysql5.7, mishards1.1.1, milvus-em:v0.4.2

docker : 通过 docker 的形式安装,如通过 k8s 或者自行编译安装。

milvus:1.1.1-centos7-oss: docker 镜像,1.1.1版本,支持OSS功能(此功能为2022-03月支持)

mysql5.7: 用于 milvus 元数据管理和存储,milvus 会自动创建四张表: Environment, Fields, TableFileds, Tables, 主要是管理 milvus 文件分块的信息和 collection 的原始结构等,占用mysql资源很低。

mishars1.1.1: milvus1.1.1 的集群分片中间件,如果单机内存不足以支持 collection 的数据量,可以使用 mishards 进行分片查询操作,提高查询效率,这个适用于海量数据的场景。

milvus-em:v0.4.2: milvus1.1.1 可视乎组件,通过访问 milvus 暴露的 restAPI 接口可以查询 milvus 的数据库的基本信息,用于后台管理。

另外 milvus 也支持Prometheus的Pushgateway数据采集,若有监控需求,可参考:milvus.io/cn/docs/v1.…

2. 安装步骤

a. 安装 docker

参考此链接进行安装或者联系运维进行安装:docs.docker.com/engine/inst…

安装完成后查看 version

sudo docker version

b. 下载镜像

使用 docker pull 指令拉取镜像。如果是内网环境,可以将镜像包上传到主机,再使用 load 和 tag 指令加载镜像

# 拉取 milvus,这个用的是 milvus开发者打的一个含有OSS的镜像,也可以在docker hub寻找
sudo docker pull matrixji/milvus:1.1.1-centos7-oss
# 拉取 mishards
sudo docker pull milvusdb/mishards:1.1.1

c. 创建主机映射目录

sudo mkdir /data/milvus
# 持久化目录
sudo mkdir /data/milvus/db
sudo mkdir /home/milvus
# 配置文件
sudo mkdir /home/milvus/conf
# 日志目录
sudo mkdir /home/milvus/logs
# 写入缓存目录
sudo mkdir /home/milvus/wal

d. 如果是多台机器部署,申请共享文件存储映射到 /data/milvus 目录

e. 申请 mysql 资源,20G 即可,申请 MySQl 创建删除表的用户权限

e. 配置 server_config.yaml 文件

server_config.yaml 文件是 milvus 启动时的配置文件,里面有 milvus 一些常用配置,可参考:milvus.io/cn/docs/v1.…

官方参考格式

# Copyright (C) 2019-2020 Zilliz. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing permissions and limitations under the License.

version: 0.5

#----------------------+------------------------------------------------------------+------------+-----------------+
# Cluster Config       | Description                                                | Type       | Default         |
#----------------------+------------------------------------------------------------+------------+-----------------+
# enable               | If runinng with Mishards, set true, otherwise false.       | Boolean    | false           |
#----------------------+------------------------------------------------------------+------------+-----------------+
# role                 | Milvus deployment role: rw / ro                            | role       | rw              |
#----------------------+------------------------------------------------------------+------------+-----------------+
cluster:
  enable: true
  role: rw

#----------------------+------------------------------------------------------------+------------+-----------------+
# General Config       | Description                                                | Type       | Default         |
#----------------------+------------------------------------------------------------+------------+-----------------+
# time_zone            | Use UTC-x or UTC+x to specify a time zone.                 | Timezone   | UTC+8           |
#----------------------+------------------------------------------------------------+------------+-----------------+
# meta_uri             | URI for metadata storage, using SQLite (for single server  | URL        | sqlite://:@:/   |
#                      | Milvus) or MySQL (for distributed cluster Milvus).         |            |                 |
#                      | Format: dialect://username:password@host:port/database     |            |                 |
#                      | Keep 'dialect://:@:/', 'dialect' can be either 'sqlite' or |            |                 |
#                      | 'mysql', replace other texts with real values.             |            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
general:
  timezone: UTC+8
  meta_uri: mysql://xxx

#----------------------+------------------------------------------------------------+------------+-----------------+
# Network Config       | Description                                                | Type       | Default         |
#----------------------+------------------------------------------------------------+------------+-----------------+
# bind.address         | IP address that Milvus server monitors.                    | IP         | 0.0.0.0         |
#----------------------+------------------------------------------------------------+------------+-----------------+
# bind.port            | Port that Milvus server monitors. Port range (1024, 65535) | Integer    | 19530           |
#----------------------+------------------------------------------------------------+------------+-----------------+
# http.enable          | Enable web server or not.                                  | Boolean    | true            |
#----------------------+------------------------------------------------------------+------------+-----------------+
# http.port            | Port that Milvus web server monitors.                      | Integer    | 19121           |
#                      | Port range (1024, 65535)                                   |            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
network:
  bind.address: 0.0.0.0
  bind.port: 19530
  http.enable: true
  http.port: 19121

#----------------------+------------------------------------------------------------+------------+-----------------+
# Storage Config       | Description                                                | Type       | Default         |
#----------------------+------------------------------------------------------------+------------+-----------------+
# path                 | Path used to save meta data, vector data and index data.   | Path       | /var/lib/milvus |
#----------------------+------------------------------------------------------------+------------+-----------------+
# auto_flush_interval  | The interval, in seconds, at which Milvus automatically    | Integer    | 1 (s)           |
#                      | flushes data to disk.                                      |            |                 |
#                      | 0 means disable the regular flush.                         |            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
# s3_enabled           | If using s3 storage backend.                               | Boolean    | false           |
#----------------------+------------------------------------------------------------+------------+-----------------+
# s3_address           | The s3 server address, support domain/hostname/ipaddress   | String     | 127.0.0.1       |
#----------------------+------------------------------------------------------------+------------+-----------------+
# s3_port              | The s3 server port.                                        | Integer    | 80              |
#----------------------+------------------------------------------------------------+------------+-----------------+
# s3_access_key        | The access key for accessing s3 service.                   | String     | s3_access_key   |
#----------------------+------------------------------------------------------------+------------+-----------------+
# s3_secret_key        | The secrey key for accessing s3 service.                   | String     | s3_secret_key   |
#----------------------+------------------------------------------------------------+------------+-----------------+
# s3_bucket            | The s3 bucket name for store milvus's data.                | String     | s3_bucket       |
#                      | Note: please using differnet bucket for different milvus   |            |                 |
#                      |       cluster.                                             |            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
storage:
  path: /var/lib/milvus
  auto_flush_interval: 1

#----------------------+------------------------------------------------------------+------------+-----------------+
# WAL Config           | Description                                                | Type       | Default         |
#----------------------+------------------------------------------------------------+------------+-----------------+
# enable               | Whether to enable write-ahead logging (WAL) in Milvus.     | Boolean    | true            |
#                      | If WAL is enabled, Milvus writes all data changes to log   |            |                 |
#                      | files in advance before implementing data changes. WAL     |            |                 |
#                      | ensures the atomicity and durability for Milvus operations.|            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
# recovery_error_ignore| Whether to ignore logs with errors that happens during WAL | Boolean    | false           |
#                      | recovery. If true, when Milvus restarts for recovery and   |            |                 |
#                      | there are errors in WAL log files, log files with errors   |            |                 |
#                      | are ignored. If false, Milvus does not restart when there  |            |                 |
#                      | are errors in WAL log files.                               |            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
# buffer_size          | Sum total of the read buffer and the write buffer in MBs.  | Integer    | 256 (MB)        |
#                      | buffer_size must be in range [64, 4096] (MB).              |            |                 |
#                      | If the value you specified is out of range, Milvus         |            |                 |
#                      | automatically uses the boundary value closest to the       |            |                 |
#                      | specified value. It is recommended you set buffer_size to  |            |                 |
#                      | a value greater than the inserted data size of a single    |            |                 |
#                      | insert operation for better performance.                   |            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
# path                 | Location of WAL log files.                                 | String     |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
wal:
  enable: true
  recovery_error_ignore: false
  buffer_size: 256MB
  path: /var/lib/milvus/wal

#----------------------+------------------------------------------------------------+------------+-----------------+
# Cache Config         | Description                                                | Type       | Default         |
#----------------------+------------------------------------------------------------+------------+-----------------+
# cache_size           | The size of CPU memory used for caching data for faster    | Integer    | 4 (GB)          |
#                      | query. The sum of 'cpu_cache_capacity' and                 |            |                 |
#                      | 'insert_buffer_size' must be less than system memory size. |            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
# insert_buffer_size   | Buffer size used for data insertion.                       | Integer    | 1 (GB)          |
#                      | The sum of 'insert_buffer_size' and 'cpu_cache_capacity'   |            |                 |
#                      | must be less than system memory size.                      |            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
# preload_collection   | A comma-separated list of collection names that need to    | StringList |                 |
#                      | be pre-loaded when Milvus server starts up.                |            |                 |
#                      | '*' means preload all existing tables (single-quote or     |            |                 |
#                      | double-quote required).                                    |            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
cache:
  cache_size: 4GB
  insert_buffer_size: 1GB
  preload_collection: 

#----------------------+------------------------------------------------------------+------------+-----------------+
# GPU Config           | Description                                                | Type       | Default         |
#----------------------+------------------------------------------------------------+------------+-----------------+
# enable               | Enable GPU resources or not.                               | Boolean    | false           |
#----------------------+------------------------------------------------------------+------------+-----------------+
# cache_size           | The size of GPU memory per card used for cache.            | Integer    | 1 (GB)          |
#----------------------+------------------------------------------------------------+------------+-----------------+
# gpu_search_threshold | A Milvus performance tuning parameter. This value will be  | Integer    | 1000            |
#                      | compared with 'nq' to decide if the search computation will|            |                 |
#                      | be executed on GPUs only.                                  |            |                 |
#                      | If nq >= gpu_search_threshold, the search computation will |            |                 |
#                      | be executed on GPUs only;                                  |            |                 |
#                      | if nq < gpu_search_threshold, the search computation will  |            |                 |
#                      | be executed on CPUs only.                                  |            |                 |
#                      | The SQ8H index is special, if nq < gpu_search_threshold,   |            |                 |
#                      | the search will be executed on both CPUs and GPUs.         |            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
# search_resources     | The list of GPU devices used for search computation.       | DeviceList | gpu0            |
#                      | Must be in format gpux.                                    |            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
# build_index_resources| The list of GPU devices used for index building.           | DeviceList | gpu0            |
#                      | Must be in format gpux.                                    |            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
gpu:
  enable: false
  cache_size: 1GB
  gpu_search_threshold: 1000
  search_devices:
    - gpu0
  build_index_devices:
    - gpu0

#----------------------+------------------------------------------------------------+------------+-----------------+
# FPGA Config           | Description                                               | Type       | Default         |
#----------------------+------------------------------------------------------------+------------+-----------------+
# enable               | Use FPGA devices or not.                                   | Boolean    | false           |
#----------------------+------------------------------------------------------------+------------+-----------------+
# search_devices       | The list of FPGA devices used for search computation.      | DeviceList | fpga0           |
#                      | Must be in format fpgax.                                   |            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
fpga:
  enable: false
  search_devices:
    - fpga0

#----------------------+------------------------------------------------------------+------------+-----------------+
# Logs Config          | Description                                                | Type       | Default         |
#----------------------+------------------------------------------------------------+------------+-----------------+
# level                | Log level in Milvus. Must be one of debug, info, warning,  | String     | debug           |
#                      | error, fatal                                               |            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
# trace.enable         | Whether to enable trace level logging in Milvus.           | Boolean    | true            |
#----------------------+------------------------------------------------------------+------------+-----------------+
# path                 | Absolute path to the folder holding the log files.         | String     |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
# max_log_file_size    | The maximum size of each log file, size range [512, 4096]  | Integer    | 1024 (MB)       |
#----------------------+------------------------------------------------------------+------------+-----------------+
# log_rotate_num       | The maximum number of log files that Milvus keeps for each | Integer    | 0               |
#                      | logging level, num range [0, 1024], 0 means unlimited.     |            |                 |
#----------------------+------------------------------------------------------------+------------+-----------------+
# log_to_stdout        | Whether to write logs to standard output in Milvus.        | Boolean    | false           |
#----------------------+------------------------------------------------------------+------------+-----------------+
# log_to_file          | Whether to write logs to files in Milvus                   | Boolean    | true            |
#----------------------+------------------------------------------------------------+------------+-----------------+
logs:
  level: debug
  trace.enable: true
  path: /var/lib/milvus/logs
  max_log_file_size: 1024MB
  log_rotate_num: 0
  log_to_stdout: false
  log_to_file: true

#----------------------+------------------------------------------------------------+------------+-----------------+
# Metric Config        | Description                                                | Type       | Default         |
#----------------------+------------------------------------------------------------+------------+-----------------+
# enable               | Enable monitoring function or not.                         | Boolean    | false           |
#----------------------+------------------------------------------------------------+------------+-----------------+
# address              | Pushgateway address                                        | IP         | 127.0.0.1       +
#----------------------+------------------------------------------------------------+------------+-----------------+
# port                 | Pushgateway port, port range (1024, 65535)                 | Integer    | 9091            |
#----------------------+------------------------------------------------------------+------------+-----------------+
metric:
  enable: false
  address: 127.0.0.1
  port: 9091

个人配置

version: 0.5
cluster:
  # 开启集群
  enable: true
  # rw 表示读写 ro 表示只读
  role: rw
general:
  timezone: UTC+8
  meta_uri: mysql://username:password@host:port/database
network:
  bind.address: 0.0.0.0
  # grcp 端口,为 mivlus client连接的端口
  bind.port: 19530
  # 开启 http 端口, 这个打开可以使用http请求调用milvus,也可以使用 milvus-em 可视化维护milvus,建议打开
  # 具体http restAPI 可参考 https://github.com/milvus-io/milvus/tree/1.x/core/src/server/web_impl
  http.enable: true
  http.port: 19121
storage:
  # 自动持久化时间,单位s,不建议设置的太小,影响服务性能
  auto_flush_interval: 10
  path: /var/lib/milvus
wal:
  # 写入数据时的缓冲,大小根据机器内存设置
  enable: true
  recovery_error_ignore: true
  buffer_size: 1GB
  path: /var/lib/milvus/wal
cache:
  # milvus 查询时能够使用的内存最大值
  cache_size: 128GB
  insert_buffer_size: 16GB
  # milvus 启动和重启时将哪些collection加载到内存中
  preload_collection: "*"
gpu:
  enable: false
fpga:
  enable: false
logs:
  # 日志等级,上线后建议调整成error,并持续关注机器内存,因为milvus服务会打出很多debug和info日志,要配合shell脚本定期清理
  level: error
  trace.enable: true
  path: /var/lib/milvus/logs
  max_log_file_size: 1024MB
  log_rotate_num: 1024
  log_to_stdout: false
  log_to_file: true
metric:
  # 数据上报到pushgateway,使用Prometheus时可以打开,不使用建议关闭
  enable: true
  address: 0.0.0.0
  port: 9091

配置建议

  1. milvus 的查询的原理是先将索引加载到内存,在内存中进行查询,可以将 cache_size 调整的略高一些,提高内存利用率。
  2. rw 节点设置一个即可,可以做读写分离,因为 milvus 在构建索引时,非常消耗 cpu 性能,此时在查询将变得非常慢
  3. auto_flush_interval 不建议设置的太高,除非想要刚插入的数据就能被查询到,但是不建议这样做,查询会非常慢且影响性能

f. 安装 milvus (主机为 32逻辑核心和188GB内存), 这里设置的 env OMP_NUM_THREADS=24 是 milvus 占用的 cpu 核数,可以在检索不稳定时使用

sudo docker run -d \
--name milvus_rw \
--net=host \
--env OMP_NUM_THREADS=24 \
-v /data/milvus/db:/var/lib/milvus/db \
-v /home/milvus/conf:/var/lib/milvus/conf \
-v /home/milvus/logs:/var/lib/milvus/logs \
-v /home/milvus/wal:/var/lib/milvus/wal \
matrixji/milvus:1.1.1-centos7-oss

如果是多台机器的话,每台机器都要安装,只需区别 ro 和 rw。安装完成后可以使用指令来查看安装是否正常

sudo docker logs milvus_rw

g. 安装 mishards,参考:milvus.io/cn/docs/v1.…

sudo docker run -d \
--name milvus_mishards \
--net=host \
--env Debug=False \
--env TIMEZONE=Asia/Shanghai \
--env WOSERVER=tcp://10.xxx.xxx.xxx:19530 \
--env SQLALCHEMY_DATABASE_URI=mysql://username:password@host:port/database?charset=utf8mb4 \
--env DISCOVERY_STATIC_HOSTS=10.xxx.xxx.xxx,10.xxx.xxx.xxx,10.xxx.xxx.xxx \
--env LOG_LEVEL=ERROR \
-v /home/milvus/mishards:/tmp/mishards \
milvusdb/mishards:1.1.1

其中 WOSERVER 为master 节点, DISCOVERY_STATIC_HOSTS 为所有机器节点

h. milvus-em 本地安装

milvus-em 需要安装在本地或者测试环境,其工作原理是调用milvus提供的http接口展示前端页面,所以此服务不需要部署在线上服务。

sudo docker rm -d -p 3000:80 milvusdb/milvus-em:v0.4.2

然后打开 localhost:3000, 在url 处输入 host:19121 即可访问 milvus 数据库管理页面。如果是内网,可以通过nginx代理19121端口进行访问

至此,milvus:1.1.1 版本所需要的组件都已经安装完成。

三、java连接milvus数据库

引用maven

<dependency>
    <groupId>io.milvus</groupId>
    <artifactId>milvus-sdk-java</artifactId>
    <version>1.1.1</version>
</dependency>

编写 client bean

@Getter
@Configuration
public class MilvusClientConfig {

	@Value("${milvus.client.host}")
	private String host;

	@Value("${milvus.client.port:19531}")
	private Integer port;

	@Bean("milvusClient")
	public MilvusClient milvusClient() {
		ConnectParam connectParam = new ConnectParam.Builder()
				.withHost(host)
				.withPort(port)
				.build();
		return new MilvusGrpcClient(connectParam);
	}
}

注意事项

  1. milvus 的 rpc 服务的端口号默认是 19530,mishards 默认是 19531,可以连接 mishards 进而连接到集群上,也可以连接单台 milvus 服务。

  2. 连接时如果出现 grpc 错误,可以检查部署时端口号是否正常,如果发现端口号没有占用,则是异常

    sudo lsof -i:19530
    sudo lsof -i:19531
    sudo netstat -tunlp | grep 19531
    sudo netstat -tunlp | grep 19530
    
  3. 如果出现 ClassNotFound 错误,可能是 guava 版本不一致,milvusSDK 使用的版本是 20.0,可以解决下 maven 版本冲突

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>20.0</version>
    </dependency>
    
  4. 建立的连接是长连接,如果直连 milvus 服务的话,一个连接只能够连接到一台机器上,而且一旦连接出现问题,也不会重连,将导致线上服务异常。可以选择连接到 mishards 上进而连接到集群,也可以自定义 grpc 连接池

增删改查

  1. milvus 增删改查包括 collection 的增删,index 的增删,partition 的增删,vector 的增删查。可以参考:github.com/milvus-io/m…

  2. 可以在 milvus-em 可视化界面创建和 collection,partitions,index,也可以通过脚本进行创建。

  3. 注意 collection 创建后,查询的距离算法,维度,单个索引文件大小不可修改。

  4. partitions 作为分区,可以作为类型的区分,也可以按照时间进行区分,每个分区下面对应一些 segement,当 segement 的 size 大于 index_file_size 时,将会创建一个索引文件。milvus 的索引文件是以 segement 为单位,如下图。如果创建的 partition 过多或者 index_file_size 设置的过大,索引可能未能创建成功,会降低查询效率。

milvus集合关系.png 5. 可以在 milvus-em 查看 segement 的大小和索引是否构建,这样能够帮助我们判断 index_file_size 应该设置多少比较合理

image-20220428115732526-16511182641202.png ​ 此图中看到一个 segement 的 size 和 vectorCnt ,发现 index 类型是 IDMAP,即未构建成索引

image-20220428115900458.png ​ 此图中可以看到 index 类型是 HNSW,索引是构建好的

  1. 索引类型的选择,可以参考官方文档和支持的索引的相关资料,我这里选择的是HNSW,主要原因是查询速度快
  2. milvus 创建的 collection 目前只支持一个 vectors 字段,插入的主键不支持 string,并且插入的数据并不会做唯一性校验,如果数据的 id 是 string 类型,需要拿到 milvus 插入后返回的 id 再做一个关联。

其他函数补充说明

  1. load_collection 和 release_collection: 这两个函数一个是将collection全部加载到内存,一个是从内存中释放出去,在大批量插入后可以调用load_collection,提高查询性能。这两个函数不需要频繁调用,milvus 的内存管理方案是:搜索时会把数据加载到内存,内存使用达到cache_size时会淘汰最先加载进内存里的数据,一般使用时不用担心数据加载和内存的释放。
  2. flush:这个是数据持久化的操作,在插入数据时并不会直接写入磁盘,会先存在缓冲区,根据配置文件的配置定期写入。一般auto_flush_interval都是有配置的,频繁的调用此函数会影响 milvus 的性能。
  3. compact:这个是磁盘整理的函数。milvus 的删除属于软删除,数据还会保留在磁盘,要等到一个 segement 的数据都被删除了才会自动清理磁盘。如果每次有大批量的删除操作时,可以在删除结束后调用此函数来归整磁盘,而且清理后的数据达到建立索引的大小时也会重新创建索引,能够提高查询的效率。在每天都有数据删除的情况下,可以根据删除数据的比例,设置定时任务来清理磁盘。

四、浅谈性能分析及1.1版本存在的问题

milvus 官网给出了各个索引和配置参数的性能分析报告:milvus.io/cn/docs/v1.…

milvus 的安装方式并不会对 milvus 的性能造成特别大的影响,查询和插入的性能决定因素主要取决于创建集合,索引以及查询时的参数。可以准备一份和业务相关的 Embedding 数据,测试不同的索引参数下的插入和查询的性能,并根据业务自身来调整参数。比如离线的任务,考虑的是召回率和吞吐量,可以选择 annoy 索引并且通过批量查询的方式提高 cpu 的利用率,线上实时的任务,主要考虑查询速度,也就是响应时间,可以选择 HNSW 索引等等。

1.1版本存在的问题

  1. milvus 的 java 客户端不支持连接池,可以自己实现
  2. milvus 的查询请求是单线程,当遇到并发特别高的情况,查询的耗时会上升的特别严重。拿自身的业务举例,并发在 40 的情况下,单次查询的平均耗时在 30ms,并发在 400 的情况下,单次查询的平均耗时能够达到 1000ms,基本等同于不可用状态。业务方可以使用限流策略来避免高并发场景下 milvus 不可用的状态。
  3. ID 不支持 string 类型(2.1 版本将会支持)。
  4. milvus 集群可用性不高,需要自己做多集群保障,以保障业务的可用性。
  5. milvus 1.x 版本虽是长期维护版本,但是新的功能都会放到 2.x 版本,1.x 版本迭代进度可能跟不上2.x。
  6. 1.1版本并不支持 OSS,截止22年3月,社区已经有伙伴提交了支持 OSS 的代码。
  7. 数据类型只支持单一向量数据,也就是一个 collection 只有两个字段,一个 id 一个 vector,在2.x版本将支持标量数据。
  8. 不支持传入 milvusId 查询 topK 数据,每次查询需要输入对应的向量。

五、Milvus 2.X 探索

milvus 2.x 全面拥抱云原生,使用 k8s 部署安装更加友好,且支持更多的索引类型和数据类型,在稳定性和可用性,性能等多个方面要优于1.x的版本。

milvus系统架构.png

从上图可以看到,milvus 2.x版本除了 milvus 服务本身,还依赖了云原生的其他服务。

  1. 使用 etcd 作为服务发现和元数据存储
  2. 使用 Pulsar 作为消息中间件,目前不支持 kafka 以及权限认证
  3. 使用 Minio/S3 等作为数据存储服务,负责数据持久化

milvus 本身有八个微服务组件,参考:milvus.io/cn/docs/v2.…

可以使用 attu 来可视化 milvus 集群,与 1.1 版本类似

部署时遇到的问题

  1. 不支持 kafka, Pulsar不支持认证,Pulsar需自行搭建维护。
  2. OSS 无法直接连接,需要通过 minio 代理连接。
  3. 使用 docker 部署时,需要部署 etcd 集群,etcd 挂掉,整个 milvus 处于不可用状态。
  4. milvus 一个节点挂掉,依赖于他的其他节点将产生大量 error 日志,并且也处于不可用状态,需要运维及时维护。
  5. docker 部署方式比较复杂,可用性不高,不如 1.1 版本。

六、总结

milvus 是一个高性能向量数据库,在插入和查询上有着不错的性能表现,并且 SDK 覆盖的语言很全,使用非常的方便,极大减轻了业务对于向量数据的维护。

目前 milvus 的使用还算稳定,但还是存在很多小的问题。咨询开源社区回复的很及时,一些问题也列入了迭代优化计划中,相信在 2022 年可以从 1.x 版本切换到云原生的 2.x 版本,体验更强大的功能。

七、参考资料

  1. milvus.io/
  2. github.com/milvus-io