Cockroach Design 翻译 ( 十七) key前缀记帐和地域

本文介绍了一种利用key前缀实现细粒度记帐的方法,并详细阐述了如何通过这种方式来跟踪系统状态和性能指标。此外,还讨论了如何使用Zone配置来指定数据复制策略,以确保数据的可用性和可靠性。

1  Key-prefix Accounting and Zones(key前缀记帐和地域

Arbitrarilyfine-grained accounting is specified via key prefixes. Key prefixes canoverlap, as is necessary for capturing hierarchical relationships. Forillustrative purposes, let’s say keys specifying rows in a set of databaseshave the following format:

通过key前缀可以指定记录任意精细的粒度。Key前缀可以重叠,这是捕捉层次关系所必需的。为了解释说明,举个例子,比如说:用key指定数据库集合中的行,格式如下:

<db>:<table>:<primary-key>[:<secondary-key>]

In this case,we might collect accounting with key prefixes:

在该场景下,我们可以收集到以下key前缀的记帐信息:

db1, db1:user, db1:order,

Accounting iskept for the entire map by default.

默认情况下,保持对整个map进行记帐。

1.1 Accounting记帐

to keepaccounting for a range defined by a key prefix, an entry is created in theaccounting system table. The format of accounting table keys is:

为了保持对一个key前缀定义的range记帐,会在记帐系统表中增加一个条目。记帐表中key的格式是:

\0acct<key-prefix>

In practice,we assume each node is capable of caching the entire accounting table as it islikely to be relatively small.

实践中,我们假设每个节点都有能力缓存整个记帐表,因为它可能相对较小。

Accounting iskept for key prefix ranges with eventual consistency for efficiency. There aretwo types of values which comprise accounting: counts and occurrences, for lackof better terms. Counts describe system state, such as the total number ofbytes, rows, etc. Occurrences include transient performance and load metrics.Both types of accounting are captured as time series with minute granularity.The length of time accounting metrics are kept is configurable. Below areexamples of each type of accounting value.

保持对最终一致性rangekey前缀进行记帐的目的是为了更高效。记帐信息包含两种类型的值:总数counts和当前值occurrences,这里没有更好的术语来表达。总数描述了系统的状态,如:字节、行的总数等等。当前值包含了临时性能和负载的指标。这两种记帐类型以分钟粒度作为时间序列被采集。记帐时长是可配置的。下面是每种记帐类型值的例子:

System StateCounters/Performance

系统状态计数/性能

l  Count of items (e.g. rows) 条目数(如:行)

l  Total bytes 字节总数

l  Total key bytes (key字节总数)

l  Total value length 值总长度

l  Queued message count 队列消息总数

l  Queued message total bytes 队列消息总字节数

l  Count of values < 16B 值总数<16B

l  Count of values < 64B值总数<64B

l  Count of values < 256B值总数<256B

l  Count of values < 1K 值总数<1K

l  Count of values < 4K值总数<4K

l  Count of values < 16K值总数<16K

l  Count of values < 64K值总数<64K

l  Count of values < 256K值总数<256K

l  Count of values < 1M值总数<1M

l  Count of values > 1M值总数<1M

l  Total bytes of accounting 记帐总字节数

Load Occurrences

负载当前值

l  Get op count (get操作总数)

l  Get total MB (get 总值MB)

l  Put op count (put操作总数)

l  Put total MB (put总值MB)

l  Delete op count (delete删除操作总数)

l  Delete total MB (delete删除总值MB)

l  Delete range op count (delete删除range总操作数)

l  Delete range total MB (delete删除range总值MB)

l  Scan op count (scan扫描操作总数)

l  Scan op MB   (scan扫描操作总值MB)

l  Split count    (split拆分总数)

l  Merge count  (merge合并总数)

Becauseaccounting information is kept as time series and over many possible metrics ofinterest, the data can become numerous. Accounting data are stored in the mapnear the key prefix described, in order to distribute load (for bothaggregation and storage).

因为记帐信息作为时间序列被保存并覆盖许多感兴趣的指标,所以数据量可能变得巨大。记帐数据存储在其key前缀附近的map中,目的是分散负载(对聚集体和存储)。

Accountingkeys for system state have the form: <key-prefix>|acctd<metric-name>*. Notice the leading ‘pipe’ character. It’s meant to sort the rootlevel account AFTER any other system tables. They must increment the sameunderlying values as they are permanent counts, and not transient activity. Logicat the node takes care of snapshotting the value into anappropriately suffixed (e.g. with timestamp hour) multi-value time seriesentry.

系统状态的记帐key形式为: <key-prefix>|acctd<metric-name>*。注意,前导“管道”符号,它的意义是为了把root层级的记帐排在其他系统表之后。当这些值是持久化总数并且不是临时活动时,它们必须高过相同的基础值。节点上的逻辑必须考虑选取多值时间序列条目中的一个合适后缀(例如:带有时间戳小时)值做为快照值。

Keys forperf/loadmetrics: <key-prefix>acctd<metric-name><hourly-timestamp>.

<hourly-timestamp>-suffixed accounting entries are multi-valued, containing a varint64entry for each minute with activity during the specified hour.

性能/负载指标的key <key-prefix>acctd<metric-name><hourly-timestamp><hourly-timestamp>-suffixed记帐条目是多值的,包含:每分钟一个varint64条目,排满指定的小时期间。

Toefficiently keep accounting over large key ranges, the task of aggregation mustbe distributed. If activity occurs within the same range as the key prefix foraccounting, the updates are made as part of the consensus write.If the ranges differ, then a message is sent to the parent range to incrementthe accounting. If upon receiving the message, the parent range also does notinclude the key prefix, it in turn forwards it to its parent or left child inthe balanced binary tree which is maintained to describe the range hierarchy.This limits the number of messages before an update is visible at the rootto 2*logN, where N is the number ofranges in the key prefix.

为高效地保持对巨大数量的keyrange的记帐,聚集任务必须被分布执行。如果活动发生的range与记帐的key前缀range相同,更新则作为一致性写的一部分来执行。如果range不同,那么一个消息被发送到其父range来提升该记帐。如果据收到的消息,父range也不包含key前缀,它将依次传递消息到其父range或者平衡二叉树中的左孩子,该平衡二叉树被维护用来描述range的层次结构。这限制了一个更新前消息的数量,到根时的数量是2*logNN是该key前缀内range的数量。

1.2 Zones地域

zones arestored in the map with keys prefixed by \0zone followedby the key prefix to which the zone configuration applies. Zone values specifya protobuf containing the datacenters from which replicas for ranges which fallunder the zone must be chosen.

地域信息存储在map中,map中的key前面追加\0zone+key前缀,key前缀由zone配置指定。Zone值指定了一个protobuf(google 的一种数据交换的格式),包含了range的副本所在的数据中心,当本地域失效时,会选择这些zone来接替。

Pleasesee pkg/config/config.proto for up-to-datedata structures used, the best entry point being message ZoneConfig.

最新使用的数据结构请参阅pkg/config/config.proto  源码,最佳进入点是消息ZoneConfig

If zones aremodified in situ, each node verifies the existing zones for its ranges againstthe zone configuration. If it discovers differences, it reconfigures ranges inthe same way that it rebalances away from busy nodes, via special-case 1:1 splitto a duplicate range comprising the new configuration.

如果地域在原位置被修改,每个节点都会验证其上的ranges正使用的地域与zone配置是否匹配。如果发现不同,它将重新配置ranges,方式与它从忙碌节点进行重平衡一样,通过特殊情况11拆分成重复的包含新配置的range

### CockroachDB、PostgreSQL 与 MySQL 的区别对比 #### 架构设计 CockroachDB 是一种分布式数据库,设计用于大规模集群部署,支持自动分片、复制负载均衡,采用 Sharded-Nothing 架构[^2]。PostgreSQL MySQL 是传统的单节点关系型数据库系统,虽然可以通过第三方工具或插件实现一定程度的分布式部署,但其核心架构并不原生支持分布式事务自动分片。 CockroachDB 的设计目标是实现全球范围内的高可用性强一致性,适用于需要水平扩展能力的应用场景[^5]。PostgreSQL MySQL 通常用于中小规模的数据存储处理,适合本地或单数据中心部署。 #### 协议与兼容性 CockroachDB 的 SQL 层基于 PostgreSQL 的协议实现,并支持 PostgreSQL 的客户端连接,同时也兼容 MySQL 协议,但主要推荐使用 PostgreSQL 兼容模式[^1]。MySQL 仅支持其原生协议,而 PostgreSQL 支持标准的 SQL 语法扩展功能,具有较高的灵活性可扩展性。 #### 一致性与事务支持 CockroachDB 支持 ACID 事务,并通过 Raft 协议保证数据的强一致性[^5]。PostgreSQL 也支持 ACID 事务,并提供多种隔离级别,但其事务机制在分布式环境下需要依赖外部协调服务。MySQL 的事务支持依赖于存储引擎,如 InnoDB,但其在分布式事务处理方面的能力不如 CockroachDB 强大。 CockroachDB 的设计目标之一是实现无锁的高并发事务处理,这使其在大规模并发访问场景下具有更高的性能[^3]。PostgreSQL 使用多版本并发控制(MVCC)来处理并发事务,而 MySQL 使用行级锁机制来管理并发访问。 #### 数据模型与扩展性 CockroachDB 支持 JSON 数据类型,允许存储半结构化数据,同时保持对 SQL 查询的支持[^5]。PostgreSQL 提供了丰富的数据类型支持,包括 JSON、数组、范围类型等,并支持用户自定义函数扩展。MySQL 的数据模型较为传统,主要支持基本的 SQL 数据类型,但在 JSON 类型的支持上也逐步增强。 CockroachDB 的分布式架构使其可以轻松实现水平扩展,通过添加更多节点即可提升系统整体性能[^5]。PostgreSQL MySQL 的扩展性主要依赖于垂直扩展(增加单节点性能)或使用分库分表等技术。 #### 部署与运维 CockroachDB 的部署相对复杂,需要考虑节点间的通信、数据分布一致性维护[^5]。PostgreSQL MySQL 的部署较为简单,通常只需安装数据库服务并配置相关参数即可启动。 CockroachDB 提供了内置的备份恢复机制,并支持跨数据中心的灾难恢复[^5]。PostgreSQL MySQL 的备份与恢复通常依赖于外部工具,如 `pg_dump` 或 `mysqldump`,以及二进制日志。 #### 安全性与生产环境适用性 CockroachDB 提供了 TLS 加密、基于角色的访问控制审计日志等安全功能,适合在生产环境中使用[^5]。PostgreSQL 提供了丰富的安全特性,包括 SSL 连接、行级安全策略细粒度的权限控制。MySQL 也支持 SSL 加密基于角色的权限管理,但在某些高级安全功能上可能不如 PostgreSQL 完善。 #### 开源与商业支持 CockroachDB 是开源数据库,同时提供企业版支持[^5]。PostgreSQL 是完全开源的数据库系统,拥有活跃的社区丰富的第三方工具。MySQL 是开源数据库,但由 Oracle 提供商业支持。 ### 示例代码:CockroachDB 创建表并插入数据 ```sql CREATE TABLE users ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name STRING, email STRING UNIQUE ); INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com'), ('Bob', 'bob@example.com'); ``` ### 示例代码:PostgreSQL 创建表并插入数据 ```sql CREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR(100), email VARCHAR(100) UNIQUE ); INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com'), ('Bob', 'bob@example.com'); ``` ### 示例代码:MySQL 创建表并插入数据 ```sql CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), email VARCHAR(100) UNIQUE ); INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com'), ('Bob', 'bob@example.com'); ``` ### 总结 CockroachDB、PostgreSQL MySQL 各有优势,适用于不同的应用场景。CockroachDB 更适合需要分布式架构强一致性的大规模系统,而 PostgreSQL MySQL 则更适合传统的关系型数据库应用场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值