Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions cluster_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -1527,25 +1527,23 @@ static int cluster_update_slot(redisCluster *c) {
/* If the redirected node is a replica of the previous slot owner, a failover has taken place.
We must then remap the cluster's keyspace in order to update the cluster's topology. */
redisClusterNode *prev_master = cluster_slot(c,c->redir_slot);
if (NULL == prev_master->slaves) {
CLUSTER_THROW_EXCEPTION("Redis Cluster's master data is incomplete. Cluster is in invalid state.", 0);
return FAILURE;
}
redisClusterNode *slave;
ZEND_HASH_FOREACH_PTR(prev_master->slaves, slave) {
if (slave == NULL) {
continue;
}
if (cluster_redir_matches(c, slave->sock)) {
// Detected a failover, the redirected node was a replica
// Remap the cluster's keyspace
if (cluster_map_keyspace(c) == FAILURE) {
CLUSTER_THROW_EXCEPTION("Failed to remap cluster keyspace after failover", 0);
return FAILURE;
if (prev_master->slaves) {
redisClusterNode *slave;
ZEND_HASH_FOREACH_PTR(prev_master->slaves, slave) {
if (slave == NULL) {
continue;
}
return SUCCESS;
}
} ZEND_HASH_FOREACH_END();
if (cluster_redir_matches(c, slave->sock)) {
// Detected a failover, the redirected node was a replica
// Remap the cluster's keyspace
if (cluster_map_keyspace(c) == FAILURE) {
CLUSTER_THROW_EXCEPTION("Failed to remap cluster keyspace after failover", 0);
return FAILURE;
}
return SUCCESS;
}
} ZEND_HASH_FOREACH_END();
}

/* Create our node */
node = cluster_node_create(c, c->redir_host, c->redir_host_len,
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
</dir>
<!-- liblzf -->
<dir name="tests">
<file role="test" name="ClusterSlotsFixture.php"/>
<file role="test" name="RedisArrayTest.php"/>
<file role="test" name="RedisClusterTest.php"/>
<file role="test" name="RedisSentinelTest.php"/>
Expand Down
43 changes: 43 additions & 0 deletions tests/ClusterSlotsFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/* One-shot seed mapping every slot to a master without replicas. Data commands use real nodes. */
$slots = [[0, 16383, [$argv[1], (int)$argv[2]]]];
$server = stream_socket_server('tcp://127.0.0.1:0', $errno, $error);
if (!$server) throw new RuntimeException($error, $errno);
echo stream_socket_get_name($server, false), "\n";
flush();

$socket = stream_socket_accept($server, 5);
if (!$socket) throw new RuntimeException('Seed connection timed out');
stream_set_timeout($socket, 5);

$encode = function ($value) use (&$encode) {
if (is_array($value)) {
$reply = '*' . count($value) . "\r\n";
foreach ($value as $item) $reply .= $encode($item);
return $reply;
}
if (is_int($value)) return ":$value\r\n";
return '$' . strlen($value) . "\r\n$value\r\n";
};

while (($line = fgets($socket)) !== false) {
$command = [];
for ($i = 0, $count = (int)substr($line, 1); $i < $count; $i++) {
$length = (int)substr(fgets($socket), 1) + 2;
$argument = '';
while (strlen($argument) < $length) {
$chunk = fread($socket, $length - strlen($argument));
if ($chunk === false || $chunk === '') throw new RuntimeException('Incomplete command');
$argument .= $chunk;
}
$command[] = substr($argument, 0, -2);
}
if (array_map('strtoupper', $command) === ['CLUSTER', 'SLOTS']) {
fwrite($socket, $encode($slots));
break;
}
if (strtoupper($command[0]) !== 'AUTH') throw new RuntimeException('Unexpected seed command');
fwrite($socket, "+OK\r\n");
}
fclose($socket);
fclose($server);
50 changes: 50 additions & 0 deletions tests/RedisClusterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,56 @@ public function testDirectedCommandsRejectUncoveredSlot() {
}
}

public function testMovedFromMasterWithoutReplicas() {
if (!function_exists('proc_open'))
$this->markTestSkipped('proc_open is required for the stale slot-map seed');

$masters = $this->redis->_masters();
if (count($masters) < 2) $this->markTestSkipped('At least two masters are required');
$source = $masters[0];
$ranges = $this->redis->rawCommand($source, 'CLUSTER', 'SLOTS');
$owner = null;
for ($i = 0; $i < 256; $i++) {
$key = "{unmapped-master-$i}key";
$slot = $this->redis->cluster($source, 'KEYSLOT', $key);
foreach ($ranges as $range) {
if ($slot >= $range[0] && $slot <= $range[1]) {
$owner = [$range[2][0], $range[2][1]];
break;
}
}
if ($owner !== $source) break;
}
if ($i === 256) throw new RuntimeException('Unable to find a slot on another master');

$class = $this->getRedisClusterClass();
$cache = ini_get('redis.clusters.cache_slots');
$client = null;
$process = proc_open([PHP_BINARY, '-n', __DIR__ . '/ClusterSlotsFixture.php', $source[0], $source[1]],
[['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], $pipes);
if (!is_resource($process)) throw new RuntimeException('Unable to start slot-map seed');

try {
ini_set('redis.clusters.cache_slots', 0);
fclose($pipes[0]);
$seed = trim(fgets($pipes[1]));
if ($seed === '') throw new RuntimeException(stream_get_contents($pipes[2]));
$client = new $class(null, [$seed], 1, 1, false, $this->getAuth());
$this->redis->set($key, 'moved-value');
$this->assertEquals('moved-value', $client->get($key));
$this->assertEquals('moved-value', $client->get($key));
$this->assertTrue($client->set($key, 'updated-value'));
$this->assertEquals('updated-value', $this->redis->get($key));
} finally {
if ($client) $client->close();
$this->redis->del($key);
foreach ($pipes as $pipe) if (is_resource($pipe)) fclose($pipe);
proc_terminate($process);
proc_close($process);
ini_set('redis.clusters.cache_slots', $cache);
}
}

public function testRandomKey() {
$key = 'randomkey-test';
$this->redis->set($key, 'value');
Expand Down
Loading