# 实现 Cluster Info 表优化集群管理，支持更改列的类型 | Greptime 双周精选

Source: https://greptime.cn/blogs/2024-05-08-biweekly

## 内容概述

作为一个成长中的开源项目，GreptimeDB 的进展离不开来自全球的社区贡献者们，感谢各位！

最近的内容更新如下：

- 实现 `information_schema` 的 `cluster_info` 表，用户能够通过 SQL 查询集群中各个节点的状态

- 支持对表中列类型进行变更，以满足用户对数据结构变更的需求

- 引入新的优化器规则，优化 `count(*)` 查询性能

## 社区贡献者名单

在过去的两周里，GreptimeDB 共合并了 62 个 PR，其中有 5 位独立贡献者，累计 13 个 PR 被成功合并，还有很多待合并的 PR。

祝贺以下各位在过去 2 周内成为我们活跃的贡献者：

- @[CookiePieWw](https://github.com/CookiePieWw) ([#3842](https://github.com/GreptimeTeam/greptimedb/pull/3842) [#3840](https://github.com/GreptimeTeam/greptimedb/pull/3840) [#3818](https://github.com/GreptimeTeam/greptimedb/pull/3818) [#3756](https://github.com/GreptimeTeam/greptimedb/pull/3756))

- @[dimbtp](https://github.com/dimbtp) ([#3820](https://github.com/GreptimeTeam/greptimedb/pull/3820))

- @[irenjj](https://github.com/irenjj) ([#3819](https://github.com/GreptimeTeam/greptimedb/pull/3819) [#3744](https://github.com/GreptimeTeam/greptimedb/pull/3744) [docs#894](https://github.com/GreptimeTeam/docs/pull/894))

- @[Kelvinyu1117](https://github.com/Kelvinyu1117) ([#3829](https://github.com/GreptimeTeam/greptimedb/pull/3829))

- @[KKould](https://github.com/KKould) ([docs#935](https://github.com/GreptimeTeam/docs/pull/935) [#3796](https://github.com/GreptimeTeam/greptimedb/pull/3796) [#3745](https://github.com/GreptimeTeam/greptimedb/pull/3745) [#3757](https://github.com/GreptimeTeam/greptimedb/pull/3757))

🎉 热烈欢迎 @[Kelvinyu1117](https://github.com/Kelvinyu1117) 作为新的贡献者加入社区并成功合并了首个 PR，还有更多来自其他独立贡献者的 PR 正在等待合并。

<img src="/zh/blogs/2024-05-08-biweekly/image1.png" alt="Contributor of GreptimeDB" style="width: 80%; margin: 0 auto"/>

同时衷心感谢我们所有的成员和贡献者！是你们的付出让我们的项目得以成功，也是你们让 GreptimeDB 成为一个更优质的产品。让我们一起努力，建立一个更棒的社区！

## PR 亮点

### [#3796](https://github.com/GreptimeTeam/greptimedb/pull/3796) [#3757](https://github.com/GreptimeTeam/greptimedb/pull/3757) [#3745](https://github.com/GreptimeTeam/greptimedb/pull/3745) 支持更改列的类型

该 pr 支持了列类型更改。可以让用户轻松地更改表中列的数据类型，无需重新建表或者手动迁移数据，提高了数据库的灵活性和可维护性。

例如，下面语句将 `monitor` 表的 `load_15` 列 变更为 `STRING` 类型：

```sql
ALTER TABLE monitor MODIFY COLUMN load_15 STRING;
```

注意：修改的列不能是 `tag`（主键）或 `time index`，并且必须可以为空以确保数据可以安全地转换（转换失败时返回 ` “NULL”`）。

### [#3709](https://github.com/GreptimeTeam/greptimedb/pull/3709) 在后台线程中写 manifest 文件

该 PR 支持了在后台线程写 manifest 文件，而不是在 region 工作线程中。如果 region 工作线程负责写入 manifest 文件，可能造成 region工作线程阻塞。所以我们将此操作移至后台线程，减少工作线程阻塞的时间。

### [#3832](https://github.com/GreptimeTeam/greptimedb/pull/3832) 实现 `information_schema` 的 `cluster_info` 表

该 PR 在 `information_schema` 中增加了 `cluster_info` 表。我们可以通过查询 `cluster_info` 表获取集群的信息。这个功能帮助管理员监控和管理数据库集群的健康状态，及时发现和解决问题。

以下是查询集群信息的例子：

```shell
mysql> USE INFORMATION_SCHEMA;
mysql> SELECT * FROM CLUSTER_INFO;
```

`Standalone` 模式下的查询结果:

```shell
mysql> SELECT * FROM CLUSTER_INFO;
+---------+------------+-----------+---------+------------+-------------------------+--------+-------------+
| peer_id | peer_type  | peer_addr | version | git_commit | start_time              | uptime | active_time |
+---------+------------+-----------+---------+------------+-------------------------+--------+-------------+
| 0       | STANDALONE |           | 0.7.2   | 86ab3d9    | 2024-04-30T06:40:02.074 | 18ms   |             |
+---------+------------+-----------+---------+------------+-------------------------+--------+-------------+
```

`Distributed` 模式下的查询结果：

```shell
+---------+-----------+----------------+---------+------------+-------------------------+----------+-------------+
| peer_id | peer_type | peer_addr      | version | git_commit | start_time              | uptime   | active_time |
+---------+-----------+----------------+---------+------------+-------------------------+----------+-------------+
| 1       | DATANODE  | 127.0.0.1:4101 | 0.7.2   | 86ab3d9    | 2024-04-30T06:40:04.791 | 4s 478ms | 1s 467ms    |
| 2       | DATANODE  | 127.0.0.1:4102 | 0.7.2   | 86ab3d9    | 2024-04-30T06:40:06.098 | 3s 171ms | 162ms       |
| 3       | DATANODE  | 127.0.0.1:4103 | 0.7.2   | 86ab3d9    | 2024-04-30T06:40:07.425 | 1s 844ms | 1s 839ms    |
| -1      | FRONTEND  | 127.0.0.1:4001 | 0.7.2   | 86ab3d9    | 2024-04-30T06:40:08.815 | 454ms    | 47ms        |
| 0       | METASRV   | 127.0.0.1:3002 | unknown | unknown    |                         |          |             |
+---------+-----------+----------------+---------+------------+-------------------------+----------+-------------+
```

### [#3845](https://github.com/GreptimeTeam/greptimedb/pull/3845) 优化 `count(*)` 查询的性能

`count()` 是常用的聚合函数，用于计算表中行的数量。为了提升查询性能，我们引入新的优化器规则，以减少查询时间并提升系统的响应速度。这对于处理大型数据集和频繁执行统计查询的场景尤其重要。

先说结论：`count(*)` 的性能提升了 5 倍。新的优化器规则将 `count()` 转换为 `count(<TIME INDEX>)`。这种优化基于：底层存储引擎按 `time index` 列的扫描速度比主键列的扫描速度更快。读取主键列需要解码,  而读取 `time index` 列无需解码。

### [#3858](https://github.com/GreptimeTeam/greptimedb/pull/3858) 数据写入请求同步到 flow 节点

- 在 `Inserter` 的 `do_request` 中将写入请求复制，镜像发送到 `flow` 节点；

- 在 `Inserter` 中添加了 `TableFlowManager`，以允许查询哪个写入请求应该去哪个 `flownode`；

- 一些小的重构，包括 `flownode` 的 `handle_request` 更改为 `handle_requests` 以用于批处理请求。

## Good First Issue

### [db#3336 为 GreptimeDB 的 gRPC 服务增加 TLS 支持](https://github.com/GreptimeTeam/greptimedb/issues/3336)

目前 GreptimeDB 的 gRPC 服务不支持加密，为 gRPC 服务增加 TLS 协议支持可以提高使用 gRPC 协议时的安全性。

关键词：gRPC，TLS，安全

难度：中等

### [db#3265 为 COPY FROM 添加更多的测试](https://github.com/GreptimeTeam/greptimedb/issues/3265)

为 `COPY FROM` 添加更多的测试，确保 `COPY FROM` 的行为符合预期。

关键词：测试

难度：简单

### [db#3004 给 manifest 增加校验和](https://github.com/GreptimeTeam/greptimedb/issues/3004) 

目前，`manifest` 没有任何用于数据验证的校验和。我们需要一种方法来对 `region manifests` 进行校验和验证。一种可能的方法是将校验和保存为清单文件名的一部分，例如 000000000001-{checksum}.json。读取文件内容后，我们可以通过 `CRC32` 或其他算法计算内容校验和，并确保该值等于文件名中的校验和。

关键词：manifests、CRC32

难度：中等

## 关于 Greptime

[Greptime 格睿科技](https://greptime.cn/)专注于为可观测、物联网及车联网等领域提供实时、高效的数据存储和分析服务，帮助客户挖掘数据的深层价值。目前基于云原生的时序数据库 [GreptimeDB](https://github.com/GreptimeTeam/greptimedb) 已经衍生出多款适合不同用户的解决方案，更多信息或 demo 展示请联系下方小助手（微信号：greptime）。

欢迎对开源感兴趣的朋友们参与贡献和讨论，从带有 good first issue 标签的 issue 开始你的开源之旅吧～期待在开源社群里遇见你！添加小助手微信即可加入“技术交流群”与志同道合的朋友们面对面交流哦~

Star us on GitHub Now: https://github.com/GreptimeTeam/greptimedb

官网：https://greptime.cn/

文档：https://docs.greptime.cn/

Twitter: https://twitter.com/Greptime

Slack: https://greptime.com/slack

LinkedIn: https://www.linkedin.com/company/greptime/
