GithubHelp home page GithubHelp logo

ultrabug / uhashring Goto Github PK

View Code? Open in Web Editor NEW
183.0 9.0 28.0 81 KB

Full featured consistent hashing python library compatible with ketama

License: BSD 3-Clause "New" or "Revised" License

Python 99.44% Makefile 0.56%
consistent-hashing python hash-table ketama

uhashring's People

Contributors

bjhockley avatar btall avatar dargor avatar krisdestruction avatar ramnes avatar sebageek avatar ultrabug avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

uhashring's Issues

Support bytes

def hashi(self, key, replica=0):
        """Returns a ketama compatible hash from the given key."""
        dh = self._listbytes(md5(str(key).encode("utf-8")).digest())
        ...

hashi(b'abc') returns in dh=md5(b"b'abc'") instead of dh=md5(b'abc').

The test 'test_distribution' failed sometimes

[ 37s] =================================== FAILURES ===================================
[ 37s] ______________________________ test_distribution _______________________________
[ 37s]
[ 37s] def test_distribution():
[ 37s] ring = HashRing()
[ 37s]
[ 37s] numnodes = 10
[ 37s] numhits = 1000
[ 37s] numvalues = 10000
[ 37s]
[ 37s] for i in range(1, numnodes + 1):
[ 37s] ring["node{}".format(i)] = {"instance": "node_value{}".format(i)}
[ 37s]
[ 37s] distribution = Counter()
[ 37s] for i in range(numhits):
[ 37s] key = str(randint(1, numvalues))
[ 37s] node = ring[key]
[ 37s] distribution[node] += 1
[ 37s]
[ 37s] # count of hits matches what is observed
[ 37s] assert sum(distribution.values()) == numhits
[ 37s]
[ 37s] # usually under 20
[ 37s] standard_dev = _pop_std_dev(distribution.values())
[ 37s] > assert standard_dev <= 20
[ 37s] E assert 21.109239683134017 <= 20
[ 37s]
[ 37s] tests/test_distribution.py:39: AssertionError

Crash in remove_node

Hi!

I encountered a crash due to a hash collision when removing a node in a test environment:

[ERROR]
Traceback (most recent call last):
  File "/usr/lib/python2.7/unittest/case.py", line 329, in run
    testMethod()
  File "uhashring/tests/test_implementation.py", line 249, in test_clashing_ring
    ring.remove_node(nodes[2])
  File "uhashring/ring.py", line 268, in __delitem__
    self._remove_node(nodename)
  File "uhashring/ring.py", line 236, in _remove_node
    del self._ring[h]
exceptions.KeyError: 1009926488

The following unit test (if added to test_implementation.py/TestUhashringImplementation) reproduces the issue:

    def test_clashing_ring(self):
        # Use carefully selected node names
        nodes = ["172.31.1.0", "172.31.1.125", "172.31.1.202"]
        ring = HashRing(nodes,
                        vnodes=40,
                        compat=True
        )
        ring.remove_node(nodes[1])
        ring.remove_node(nodes[2])
        ring.remove_node(nodes[0])
        self.assertEqual(ring.ring, {})

python3 monkey

The monkey patch not working on python3, the fix is very easy
monkey.py, line 29

if isinstance(key, types.TupleType):

types.TupleType must be tuple

if isinstance(key, tuple):

"In Python 2, it contained constants for all primitive types like dict and int. In Python 3, these constants have been eliminated; just use the primitive type name instead."

what is the difference between vnode and replica?

I don't understand the difference between vnode and replica, I thought vnodes are used to make the requests distribute more uniformly, how about replica? Is the same value stored on multiple (number of replica) machines?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.