分片/分发功能(一致哈希)?

发布于 2024-09-12 06:57:00 字数 613 浏览 4 评论 0原文

我考虑过制作一个类似轻量级一致散列的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌化 2024-09-19 06:57:00

好吧,你可以做的一件事是使用 crc32...

$crc = crc32($mykey);
$serverNo = $crc % count($servers);

它应该相当一致(意味着均匀平衡),并且 100% 可重现...

Well, one thing you could do would be to use crc32...

$crc = crc32($mykey);
$serverNo = $crc % count($servers);

It should be fairly consistent (meaning evenly balanced), and 100% reproducible...

朕就是辣么酷 2024-09-19 06:57:00

我建议使用 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.

就此别过 2024-09-19 06:57:00

最终的解决方案是:

CRC32(key) % 4 当您只有 4 个服务器

并且想要重新平衡时,您可以在迁移时使用 2 个不同的哈希函数

例如:

$server_hash1 = crc32($key) % 4
$result = $db->search($server_hash1, $key);

if ($result == false)
{
    $server_hash2 = crc32($key) % 8
    $result = $db->search($server_hash2, $key);
}
  • 您必须对插入/更新执行相同的操作(使用移动函数)从 config1 到 config2)
  • 您可以异步移动(批处理方式)

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:

$server_hash1 = crc32($key) % 4
$result = $db->search($server_hash1, $key);

if ($result == false)
{
    $server_hash2 = crc32($key) % 8
    $result = $db->search($server_hash2, $key);
}
  • You have to do the same for insert/update (with a move function from config1 to config2)
  • You can do the move async (batched way)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文