Skip to content

Commit

Permalink
Merge pull request #39 from piwik/23
Browse files Browse the repository at this point in the history
Make sure sentinel implementation works
  • Loading branch information
tsteur committed Apr 19, 2016
2 parents a713764 + 7e43e07 commit 1d67d06
Show file tree
Hide file tree
Showing 19 changed files with 562 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ env:
global:
- PLUGIN_NAME=QueuedTracking
- PIWIK_ROOT_DIR=$TRAVIS_BUILD_DIR/piwik
- PIWIK_LATEST_STABLE_TEST_TARGET=2.15.0-rc4
- PIWIK_LATEST_STABLE_TEST_TARGET=2.16.2-b1
- secure: "XQHscVt/CuHd76xBnglzXyqB7jkYmab2sx7e4aEbCBfoTrEWGfd4w3IpV6NtGu2HcjvcLB3HNz0v5CyWCt8XFbABVMeqdWKF18MGyAn4pPYXh1F95PL5eRmcU1lcHFlAspUjVk6uy59YPomqElNfR474DQAMvk24MngyXhH81o4="
- secure: "MjZyWq/VgNCiIAHSJsENV6JzgGlljwjhXWauzp5xf8Le79XtCr5QTe1J540h0D9d0e3X28uIGqTvuZlZy+RPOdvsmcCaAdKk9j7cgfyzPPKNVPJcARfaB4W0mAalorn4CwZIZx+P2IdrZ8wqH+IwYaNEElcT0KBzEq8IuP1yeYU="
matrix:
Expand Down
21 changes: 16 additions & 5 deletions Commands/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$systemCheck = new SystemCheck();
$systemCheck->checkRedisIsInstalled();
try {
$systemCheck = new SystemCheck();
$systemCheck->checkRedisIsInstalled();

$extension = new \ReflectionExtension('redis');
$output->writeln('PHPRedis version: ' . $extension->getVersion());
} catch(\Exception $e) {
$output->writeln('No PHPRedis extension (not a problem if sentinel is used):' . $e->getMessage());
}

$trackerEnvironment = new Environment('tracker');
$trackerEnvironment->init();
Expand All @@ -69,17 +76,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln('NumRequestsToProcess: ' . $settings->numRequestsToProcess->getValue());
$output->writeln('ProcessDuringTrackingRequest: ' . (int) $settings->processDuringTrackingRequest->getValue());
$output->writeln('QueueEnabled: ' . (int) $settings->queueEnabled->getValue());
$output->writeln('UseSentinelBackend: ' . (int) $settings->useSentinelBackend->getValue());
$output->writeln('SentinelMasterName: ' . $settings->sentinelMasterName->getValue());

$output->writeln('');
$output->writeln('<comment>Version / stats:</comment>');

$output->writeln('PHP version: ' . phpversion());
$output->writeln('Uname: ' . php_uname());

$extension = new \ReflectionExtension('redis');
$output->writeln('PHPRedis version: ' . $extension->getVersion());

$backend = Queue\Factory::makeBackend();

if ($backend instanceof Queue\Backend\Sentinel) {
$output->writeln('Backend is using sentinel');
}

$output->writeln('Redis version: ' . $backend->getServerVersion());
$output->writeln('Memory: ' . var_export($backend->getMemoryStats(), 1));

Expand Down
44 changes: 39 additions & 5 deletions Queue/Backend/Sentinel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,58 @@
*/
namespace Piwik\Plugins\QueuedTracking\Queue\Backend;

use Piwik\Log;
use Piwik\Piwik;
use Piwik\Plugins\QueuedTracking\Queue\Backend;
use Piwik\Tracker;
use Exception;

include_once PIWIK_INCLUDE_PATH . '/plugins/QueuedTracking/libs/credis/Client.php';
include_once PIWIK_INCLUDE_PATH . '/plugins/QueuedTracking/libs/credis/Cluster.php';
include_once PIWIK_INCLUDE_PATH . '/plugins/QueuedTracking/libs/credis/Sentinel.php';

class Sentinel extends Redis
{
private $masterName = '';

protected function connect()
{
$client = new \Credis_Client($this->host, $this->port, $this->timeout, $persistent = false, $this->database, $this->password);
$this->redis = new \Credis_Sentinel($client);
$client->connect();
$hosts = explode(',', $this->host);
$ports = explode(',', $this->port);

if (count($hosts) !== count($ports)) {
throw new Exception(Piwik::translate('QueuedTracking_NumHostsNotMatchNumPorts'));
}

foreach ($hosts as $index => $host) { // Sort or randomize as appropriate
try {
$configuredClient = new \Credis_Client($host, $ports[$index], $timeout = 0.5, $persistent = false);
$configuredClient->forceStandalone();
$configuredClient->connect();
$configuredSentinel = new \Credis_Sentinel($configuredClient);
$master = $configuredSentinel->getMasterAddressByName($this->masterName);

if (!empty($master)) {

$client = new \Credis_Client($master[0], $master[1], $this->timeout, $persistent = false, $this->database, $this->password);
$client->connect();

$this->redis = $client;
$this->redis = $client;

return true;
return true;
}

} catch (Exception $e) {
Log::debug($e->getMessage());
}
}

throw new Exception('Could not receive an actual master from sentinel');
}

public function setSentinelMasterName($name)
{
$this->masterName = $name;
}

protected function evalScript($script, $keys, $args)
Expand Down
20 changes: 10 additions & 10 deletions Queue/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Piwik\Container\StaticContainer;
use Piwik\Plugins\QueuedTracking\Queue;
use Piwik\Plugins\QueuedTracking\Settings;
use Piwik\Tracker\SettingsStorage;
use Exception;

/**
* This class represents a page view, tracking URL, page title and generation time.
Expand Down Expand Up @@ -49,22 +49,22 @@ public static function getSettings()
return StaticContainer::get('Piwik\Plugins\QueuedTracking\Settings');
}

private static function getConfig()
{
return Config::getInstance();
}

private static function makeBackendFromSettings(Settings $settings)
public static function makeBackendFromSettings(Settings $settings)
{
$host = $settings->redisHost->getValue();
$port = $settings->redisPort->getValue();
$timeout = $settings->redisTimeout->getValue();
$password = $settings->redisPassword->getValue();
$database = $settings->redisDatabase->getValue();

$queuedTracking = self::getConfig()->QueuedTracking;
if (!empty($queuedTracking['backend']) && $queuedTracking['backend'] === 'sentinel') {
$redis = new Queue\Backend\Sentinel();
if ($settings->isUsingSentinelBackend()) {
$masterName = $settings->getSentinelMasterName();
if (empty($masterName)) {
throw new Exception('You must configure a sentinel master name via `sentinel_master_name="mymaster"` to use the sentinel backend');
} else {
$redis = new Queue\Backend\Sentinel();
$redis->setSentinelMasterName($masterName);
}
} else {
$redis = new Queue\Backend\Redis();
}
Expand Down
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,15 @@ __I am using the Log Importer in combination with Queued Tracking, is there some

Yes, we recommend to set the "Number of requests to process" to `1` as the log importer usually sends multiple requests at once using bulk tracking already.

__How can I configure the QueuedTracking plugin to use Sentinel?__
__How can I configure the QueuedTracking plugin to use Redis Sentinel?__

Add the following configuration to your `config/config.ini.php` to enable Sentinel feature:
You can enable the Sentinel in the plugin settings. Make sure to specify the correct Sentinel "master" name.

```
[QueuedTracking]
backend=sentinel
```
When using Sentinel, the `phpredis` extension is not needed as it uses a PHP class to connect to your Redis. Please note that calls to Redis might be a little bit slower.

In this case the `phpredis` extension is not needed as it uses a PHP class to connect to your Redis. Please note that calls to Redis might be a little bit slower.
__Can I configure multiple Sentinel servers?__

Yes, once Sentinel is enabled you can configure multiple servers by specifying multiple hosts and ports comma separated via the UI.

__Are there any known issues?__

Expand All @@ -145,6 +144,11 @@ __Are there any known issues?__

## Changelog

0.3.1

- Fixed Redis Sentinel was not working properly. Sentinel can be now configured via the UI and not via config. Also
multiple servers can be configured now.

0.3.0

- Added support to use Redis Sentinel for automatic failover
Expand Down
Loading

0 comments on commit 1d67d06

Please sign in to comment.