当前位置: 首页>>代码示例>>C++>>正文


C++ Hasher::do_hash方法代码示例

本文整理汇总了C++中Hasher::do_hash方法的典型用法代码示例。如果您正苦于以下问题:C++ Hasher::do_hash方法的具体用法?C++ Hasher::do_hash怎么用?C++ Hasher::do_hash使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Hasher的用法示例。


在下文中一共展示了Hasher::do_hash方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: calculate_rendezvous_hash

static void calculate_rendezvous_hash(std::vector<std::string> cluster,
                                      std::vector<uint32_t> cluster_rendezvous_hashes,
                                      TimerID id,
                                      uint32_t replication_factor,
                                      std::vector<std::string>& replicas,
                                      Hasher* hasher)
{
  if (replication_factor == 0u)
  {
    return;
  }

  std::map<uint32_t, size_t> hash_to_idx;
  std::vector<std::string> ordered_cluster;

  // Do a rendezvous hash, by hashing this timer repeatedly, seeded by a
  // different per-server value each time. Rank the servers for this timer
  // based on this hash output.

  for (unsigned int ii = 0;
       ii < cluster.size();
       ++ii)
  {
    uint32_t server_hash = cluster_rendezvous_hashes[ii];
    uint32_t hash = hasher->do_hash(id, server_hash);

    // Deal with hash collisions by incrementing the hash. For
    // example, if I have server hashes A, B, C, D which cause
    // this timer to hash to 10, 40, 10, 30:
    // hash_to_idx[10] = 0 (A's index)
    // hash_to_idx[40] = 1 (B's index)
    // hash_to_idx[10] exists, increment C's hash
    // hash_to_idx[11] = 2 (C's index)
    // hash_to_idx[30] = 3 (D's index)
    //
    // Iterating over hash_to_idx then gives (10, 0), (11, 2), (40, 1)
    // and (30, 3), so the ordered list is A, C, B, D. Effectively, the
    // first entry in the original list consistently wins.
    //
    // This doesn't work perfectly in the edge case
    // If I have servers A, B, C, D which cause this
    // timer to hash to 10, 11, 10, 11:
    // hash_to_idx[10] = 0 (A's index)
    // hash_to_idx[11] = 1 (B's index)
    // hash_to_idx[10] exists, increment C's hash
    // hash_to_idx[11] exists, increment C's hash
    // hash_to_idx[12] = 2 (C's index)
    // hash_to_idx[11] exists, increment D's hash
    // hash_to_idx[12] exists, increment D's hash
    // hash_to_idx[13] = 3 (D's index)
    //
    // Iterating over hash_to_idx then gives (10, 0), (11, 1), (12, 2)
    // and (13, 3), so the ordered list is A, B, C, D. This is wrong,
    // but deterministic - the only problem in this very rare case is that
    // more timers will be moved around when scaling.
    while (hash_to_idx.find(hash) != hash_to_idx.end())
    {
      // LCOV_EXCL_START
      hash++;
      // LCOV_EXCL_STOP
    }

    hash_to_idx[hash] = ii;
  }

  // Pick the lowest hash value as the primary replica.
  for (std::map<uint32_t, size_t>::iterator ii = hash_to_idx.begin();
       ii != hash_to_idx.end();
       ii++)
  {
    ordered_cluster.push_back(cluster[ii->second]);
  }

  replicas.push_back(ordered_cluster.front());

  // Pick the (N-1) highest hash values as the backup replicas.
  replication_factor = replication_factor > ordered_cluster.size() ?
                       ordered_cluster.size() : replication_factor;

  for (size_t jj = 1;
       jj < replication_factor;
       jj++)
  {
    replicas.push_back(ordered_cluster.back());
    ordered_cluster.pop_back();
  }
}
开发者ID:AiprNick,项目名称:chronos,代码行数:87,代码来源:timer.cpp


注:本文中的Hasher::do_hash方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。