分片/分发功能(一致哈希)?
我考虑过制作一个类似轻量级一致散列的 PHP 函数来在不同服务器之间分片上传的文件。
显然, rand() 可以在服务器之间均匀地分配文件,但是在请求文件时,没有人会知道哪个文件位于哪个服务器上......
我知道有一些广泛的库可以创建一致的哈希,但我想知道这些是如何工作的,以及我如何才能推出我自己的、非常轻量级的?
注意:我没有考虑到服务器将被删除,而是进一步将更多服务器添加到池中。
更新:
这是一行简短的伪代码:
$config['shards'] = array('192.168.1.1, 192.168.1.2');
function shard ($filename) {
$servers = $config['shards'];
// do lookup in some magic way to decide which server to return.
return $appropriateserver;
}
echo shard('filename.jpg'); // returns the appropriate server to distribute the file.
I have thought a bit about making a somewhat lightweight consistent-hashing-like PHP function to shard uploaded files between different servers.
Obviously, rand() would work to distribute the files among the servers somewhat evenly, but when requesting the files, no one will know which file lies on what server...
I know that there's some extensive libraries out there to create consistent-hashing, but I wonder how these works and how I can do to roll out my own, very lightweight one?
Note: I do not take into account that servers will be removed, but instead more ones added to the pool further on.
Update:
Here's a quick line of psuedocode:
$config['shards'] = array('192.168.1.1, 192.168.1.2');
function shard ($filename) {
$servers = $config['shards'];
// do lookup in some magic way to decide which server to return.
return $appropriateserver;
}
echo shard('filename.jpg'); // returns the appropriate server to distribute the file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,你可以做的一件事是使用 crc32...
它应该相当一致(意味着均匀平衡),并且 100% 可重现...
Well, one thing you could do would be to use crc32...
It should be fairly consistent (meaning evenly balanced), and 100% reproducible...
我建议使用 MurmurHash3:它比加密哈希函数快得多,同时保留 类似的随机性。 MurmurHash 速度接近 CRC32 甚至更好。有 PHP 实现。
I recommend using MurmurHash3: it is much faster than cryptographic hash functions, while preserves similar randomness. MurmurHash speed is close to CRC32 or even better. There is PHP implementation.
最终的解决方案是:
CRC32(key) % 4 当您只有 4 个服务器
并且想要重新平衡时,您可以在迁移时使用 2 个不同的哈希函数
例如:
an eventual solution would be:
CRC32(key) % 4 when you only have 4 servers
and when you want to rebalance you can use 2 different hash functions while migration
ex: