Giter Site home page Giter Site logo

Named Connections about predis HOT 4 CLOSED

predis avatar predis commented on June 2, 2024
Named Connections

from predis.

Comments (4)

nrk avatar nrk commented on June 2, 2024

Adding support for named connections that uses an alias instead of the ip:port pair for the hashring should be quite easy with the development version of Predis by creating a subclass of PredisCluster that overrides Predis\Network\PredisCluster::add() to use the already existing (but optional) alias property for a connection. The configuration of the client would then look something like the following snippet:

SNIPPET REMOVED

Might end up adding this one as an optional cluster connection since it's quite easy.

UPDATE: well, I got it wrong... but it's still something easy to implement :-) See my next comment for more details.

Regarding a transparent master/slave configuration (@Seldaek's request), I'm still not sure how to implement this in a good way and, more than anything else, I don't think it could work decently alongside a clustered scenario (at least due to how Predis is structured right now).

Comments?

EDIT: I forgot to give you a clear answer about the case when the cluster configuration is changed! With the default implementation of a client-side cluster, when you change an ip:port pair of one of your Redis instances then you inevitably incur in the rehashing of some keys. With the proposed named-nodes approach, reashing occurs only when the number of nodes in your cluster configuration changes.

from predis.

 avatar commented on June 2, 2024

Thanks for the response, and I'm glad to hear it's not a hard thing to do - That would be awesome!

Any estimate on when we'd see the cluster functionality released?

from predis.

nrk avatar nrk commented on June 2, 2024

Heh I got things a bit wrong, that's how it goes when spending a whole month without touching a single line of code :-) The distribution algorithm used by Predis\Network\PredisCluster can be already customized using different distribution strategies, by default it's Predis\Distribution\HashRing which uses the classic ip:port pair.

After pushing this slight change it's now possible to reuse most of the code of Predis\Distribution\HashRing when, in a subclass, we just need to change how we get the hash of a node. With this change we can implement our strategy that calculates the hash of a node against its alias with very few lines of code:

<?php

class AliasBasedHashRing extends Predis\Distribution\HashRing {
    public function add($node, $weight = null) {
        $parameters = $node->getParameters();
        if (!$parameters->isSetByUser('alias')) {
            throw new \InvalidArgumentException(
                "The 'alias' property must be set for {$node} to use " .
                "this kind of distribution strategy"
            );
        }
        parent::add($node, $weight);
    }

    protected function getNodeHash($nodeObject) {
        return $nodeObject->getParameters()->alias;
    }
}

You can find a complete and usable example in this gist. If you change the configuration of the nodes (e.g. you move one of them to a new port, or a completely new host) you'll notice that the number of keys for each node won't change.

I'm still not sure if, when and how I will merge this into the main repository (btw, the name AliasBasedHashRing is terrible!). It's something quite small and very specific that it could live in a future side-project related to Predis. In the meanwhile you can just get this class and use it in your project just like I did in the linked example.

I'll just leave this issue open for now until I decide how to proceed.

from predis.

nrk avatar nrk commented on June 2, 2024

It's been a while but I finally came up with something easy enough to set up in user's code and way more flexible than my previously proposed solution.

As explained in the message of 09de7be, you can now instruct the client to use the alias parameter instead the default ip:port pair using options and without the need to extend a base class:

use Predis\Cluster\Distribution\HashRing;
use Predis\Connection\PredisCluster;

$servers = array(
    'tcp://10.0.0.1?alias=node01',
    'tcp://10.0.0.2?alias=node02',
);

$options = array(
    'nodehash' => function ($connection) {
        return $connection->getParameters()->alias;
    },
    'cluster' => function ($options) {
        $replicas = HashRing::DEFAULT_REPLICAS;
        $hashring = new HashRing($replicas, $options->nodehash);
        $cluster  = new PredisCluster($hashring);

        return $cluster;
    }
);

$client = new Predis\Client($servers, $options);

This change will make it into Predis v0.8.1.

from predis.

Related Issues (20)

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.