evseevnn-zz / php-cassandra-binary

PHP library for Cassandra database via a binary protocol.
http://evseevnn.github.io/php-cassandra-binary/
MIT License
69 stars 33 forks source link

BUG: can't use with credentials #61

Open Stajor opened 9 years ago

Stajor commented 9 years ago

can't connect to cassandra if I use host as a key and username, password as a value

$nodes = [
    '192.168.0.2:8882' => [ 'username' => 'admin', 'password' => 'pass']
];

The bug in Cassandra/Cluster.php in method getRandomNode

after shuffle($this->nodes); the host become zero

Stajor commented 9 years ago

My solution

    public function getRandomNode() {
        if (empty($this->nodes)) throw new ClusterException('Node list is empty.');

        $array_random_assoc = function($arr, $num = 1) {
            $keys = array_keys($arr);
            shuffle($keys);

            $r = array();
            for ($i = 0; $i < $num; $i++) {
                $r[$keys[$i]] = $arr[$keys[$i]];
            }
            return $r;
        };

        $this->nodes = $array_random_assoc($this->nodes, count($this->nodes));

//      shuffle($this->nodes);
        while(!empty($this->nodes)) {
...