- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读一篇关于 Pinterest 如何通过跨多个服务器和这些服务器中的数据库共享数据来设法使用 MySQL 进行扩展的文章。
我遇到的一个问题是他们如何通过位操作创建他们的全局 ID。
我理解运算符如何喜欢 & 和 |作品。而且我了解左移 << 和右移 >> 的工作原理。但我不明白他们在这个例子中是如何一起工作的。
文章中说:
We created a 64 bit ID that contains the shard ID, the type of the containing data, and where this data is in the table (local ID). The shard ID is 16 bits, type ID is 10 bits and local ID is 36 bits. The savvy additionology experts out there will notice that only adds to 62 bits. My past in compiler and chip design has taught me that reserve bits are worth their weight in gold. So we have two (set to zero).
因此分片ID为16位,最大值为65536 - 1,类型ID为10位,最大值为1024 - 1,本地ID为36位,最大值为68719476736 - 1。
最后一句是什么意思?
My past in compiler and chip design has taught me that reserve bits are worth their weight in gold. So we have two (set to zero).
下一行向我们展示了一种获取 64 位 ID 的方法:
ID = (shard ID << 46) | (type ID << 36) | (local ID<<0)
我很难理解这一点。为什么分片 ID 左移 46?为什么类型 ID 左移 36 位,本地 ID 左移 0 位,为什么我们在所有这些之间使用 OR?
下一部分是他们从全局 ID 241294492511762325 中提取本地 ID、类型 ID 和本地 ID:
Shard ID = (241294492511762325 >> 46) & 0xFFFF = 3429
Type ID = (241294492511762325 >> 36) & 0x3FF = 1
Local ID = (241294492511762325 >> 0) & 0xFFFFFFFFF = 7075733
所以这里他们做相反的事情,他们向右移动,与原来的左移相同的量,我不知道为什么要移动这些量,但我可以看到模式。但是现在他们用一些十六进制数来做&,这个我也不明白。
我正在尝试学习如何在 MySQL 中手动分片数据,如果我能理解他们为什么这样做的逻辑以及它是如何工作的,那就太好了。我明白 & 和 | 有多简单<< 和 >> 运算符起作用,我是通过阅读这篇文章了解到的:
https://code.tutsplus.com/articles/understanding-bitwise-operators--active-11301
但我不明白这一切是如何协同工作的,我不明白他们为什么使用那些移动的数字,以及为什么他们在末尾使用 & 和十六进制数字。
如果我能理解这个就好了。
最佳答案
Next line shows us a way to get a 64 bit ID:
ID = (shard ID << 46) | (type ID << 36) | (local ID<<0)
I am struggling to understand this. Why is the shard ID left shifted by 46? And why is the type ID left shifted by 36 and why is the local ID left shifted by 0, and why do we have an OR between all of these?
所以重申一下:
完整 ID 包含顺序为“分片、类型、本地”的 ID。由于本地 ID 有 36 位,因此您必须将类型 ID 移动 36 位。类型 ID 有 10 位,因此您必须将类型 ID 额外移动 10 位(本地 ID 的 36 位 + 类型 ID 的 10 位)。
按位或 (|
) 会将所有位设置为 1,这些位至少在一个操作数中设置为 1。
例如:
shard ID = 0b 1011_1100_1001_0010
type ID = 0b 11_1010_0110
local ID = 0b1010_0100_1111_1101_1110_0011_1000_0000_0111
如果将这些值移动指定的位,则:
shard ID << 46 = 0b10_1111_0010_0100_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000
type ID << 36 = 0b 11_1010_0110_0000_0000_0000_0000_0000_0000_0000_0000_0000
local ID << 0 = 0b 1010_0100_1111_1101_1110_0011_1000_0000_0111
如果你用 OR 那,你会得到结果:
0b10_1111_0010_0100_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000
| 0b 11_1010_0110_0000_0000_0000_0000_0000_0000_0000_0000_0000
| 0b 1010_0100_1111_1101_1110_0011_1000_0000_0111
= 0b10_1111_0010_0100_1011_1010_0110_1010_0100_1111_1101_1110_0011_1000_0000_0111
要再次提取 ID,您必须使用按位 AND (&
) 运算符应用位掩码。它与 OR 运算符相反:只有当两个操作数都设置了该位时,它才会将位设置为 1。所以以你的例子为例:
Type ID = (241294492511762325 >> 36) & 0x3FF = 1
让我们先转换为二进制,以便运算符及其操作数的依赖关系变得清晰:
0b11_0101_1001_0100_0000_0001_0000_0000_0000_0110_1011_1111_0111_1001_0101
右移 36 位:
0b 11_0101_1001_0100_0000_0001
应用 10 位的位掩码 (0x3FF = 0b11_1111_1111):
0b11_0101_1001_0100_0000_0001
& 0b 11_1111_1111
= 0b 00_0000_0001
用十进制表示法,也是 1。
关于mysql - Pinterests 使用位操作来创建全局 ID 以在 MySQL 上对其数据进行分片的原因是什么?它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47296264/
我是ElasticSearch的新手,并且一直在使用它的功能。 我在单个计算机上运行单个节点(master = true,data = true)。我创建一个索引,其中包含3个分片和每个分片1个副本。
我们在14个节点的集群中具有嵌入式Elasticsearch的Java应用程序。所有数据都驻留在中央数据库中,并在elasticsearch中对其进行索引以进行查询。完整的索引编制可以随时进行。 系统
我是 MongoDB 的新手,我想了解这两种技术如何协同工作: 当为你的数据库使用复制时,你有一个主节点和一堆辅助节点。为了保证一致性,建议大家始终从主节点读取,对吧? 因此,当您将复制与分片结合使用
有人告诉我,在一台更强大的机器上使用 100 个分片实现 mongodb 分片以实现更高的并发写入数据库是否有意义,每个 monogod.exe 进程都有一个全局锁?假设这是可能的,这种方法会给我更高
我有一个带有 4x 分片的生产 mongodb 部署 (3.6),它运行良好。我想向集群中添加 4 倍以上的分片。一次添加多个分片是否可以,或者这会引起戏剧性事件吗?我发现添加分片是一项昂贵的操作,在
假设在您的 Web 应用程序中,您需要调用多个 Redis 来呈现页面,例如获取一堆用户哈希。为了加快速度,您可以将 redis 命令包装在 MULTI/EXEC 部分,从而使用流水线,从而避免进行多
我正在查看ES文档,并看到以下内容 Each shard is in itself a fully-functional and independent "index" that can be hos
我有一个 Multi-Tenancy 系统,并且我正在尝试设计ElasticSearch以支持 Multi-Tenancy 。我已经在网上搜索过,但是我发现的所有帖子在实践中均未指定具体操作方法。 基
我已经在 kuberenetes 中使用散列分片设置了分片 MongoDB 集群。我首先创建了配置服务器 Replicaset,然后创建了 2 个分片副本集。最后创建了mongos来连接分片集群。 我
我在调试父子关系查询时遇到问题。我想知道调试问题的方法,而不是简单地发布我的映射、数据、查询并询问问题所在(但我最终保留这样做的权利!)。 为此,首先要检查我的 child 和关联的 parent 是
我正在启动一个 Django 项目,需要对多个可能包含太多行的表进行分片。我浏览了这里和其他地方的线程,并遵循了 Django 多数据库文档,但我仍然不确定它们是如何缝合在一起的。我的模型具有会被分片
我正在尝试使用 docker 创建 mongo 分片.我有: 配置服务器: docker run -it --rm --net=xnet -p 27016:27016 \ --hostname
我正在尝试在一台机器上配置分片,但在尝试添加分片时我不断收到错误消息。我使用以下网站作为引用:http://www.javahotchocolate.com/notes/mongodb-shardin
我刚接触 mongodb。由于我必须存储 +-5000 万份文档,我不得不设置一个带有两个副本集的 mongodb 分片集群 文档看起来像这样: { "_id" : "predefined_u
目前我们运行一个包含 2 个服务器 + 1 个仲裁器的 MongoDB 复制集。 我们在副本集的数据库中存储了大约 150 GB 的数据。 现在我们正在考虑何时开始分片。因为我们想知道是否存在不能再开
我需要并行处理某些项目,所以我使用 TPL Dataflow。 .要注意的是,共享相同键(类似于字典)的项目应按 FIFO 顺序处理,而不是彼此平行(它们可以与具有不同值的其他项目平行)。 正在完成的
问题涉及redis的分片配置。我已经用 Java 实现了一个小型测试应用程序,它以 user:userID 的形式在 Jedis 上创建了 100.000 个用户哈希。每个散列都有元素:姓名、电话、部
您好,我将使用多个 Redis 实例和实例之间的一些分片。 我的问题是,如果加载网页需要访问多个分片,性能是否会受到[明显的影响]。 我的基本概述是在多个 Redis 分片之间实现负载平衡*下面的脚注
Predis 声称拥有客户端分片(支持键的一致性散列)。 http://github.com/nrk/predis 我可以使用连接到一组配置文件(节点)来进行分片,但它不是一致的哈希。当我将另一个节点
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我是一名优秀的程序员,十分优秀!