Merge pull request #17663 from owncloud/occ-memcache

Missing memcache should not cause occ hard-fail
This commit is contained in:
Morris Jobke 2015-07-17 13:15:47 +02:00
commit f7a78cf650
3 changed files with 48 additions and 18 deletions

View File

@ -29,6 +29,7 @@
namespace OC\Memcache; namespace OC\Memcache;
use \OCP\ICacheFactory; use \OCP\ICacheFactory;
use \OCP\ILogger;
class Factory implements ICacheFactory { class Factory implements ICacheFactory {
const NULL_CACHE = '\\OC\\Memcache\\NullCache'; const NULL_CACHE = '\\OC\\Memcache\\NullCache';
@ -38,6 +39,11 @@ class Factory implements ICacheFactory {
*/ */
private $globalPrefix; private $globalPrefix;
/**
* @var ILogger $logger
*/
private $logger;
/** /**
* @var string $localCacheClass * @var string $localCacheClass
*/ */
@ -55,13 +61,15 @@ class Factory implements ICacheFactory {
/** /**
* @param string $globalPrefix * @param string $globalPrefix
* @param ILogger $logger
* @param string|null $localCacheClass * @param string|null $localCacheClass
* @param string|null $distributedCacheClass * @param string|null $distributedCacheClass
* @param string|null $lockingCacheClass * @param string|null $lockingCacheClass
*/ */
public function __construct($globalPrefix, public function __construct($globalPrefix, ILogger $logger,
$localCacheClass = null, $distributedCacheClass = null, $lockingCacheClass = null) $localCacheClass = null, $distributedCacheClass = null, $lockingCacheClass = null)
{ {
$this->logger = $logger;
$this->globalPrefix = $globalPrefix; $this->globalPrefix = $globalPrefix;
if (!$localCacheClass) { if (!$localCacheClass) {
@ -71,22 +79,43 @@ class Factory implements ICacheFactory {
$distributedCacheClass = $localCacheClass; $distributedCacheClass = $localCacheClass;
} }
$missingCacheMessage = 'Memcache {class} not available for {use} cache';
$missingCacheHint = 'Is the matching PHP module installed and enabled?';
if (!$localCacheClass::isAvailable()) { if (!$localCacheClass::isAvailable()) {
throw new \OC\HintException( if (\OC::$CLI && !defined('PHPUNIT_RUN')) {
'Missing memcache class ' . $localCacheClass . ' for local cache', // CLI should not hard-fail on broken memcache
'Is the matching PHP module installed and enabled ?' $this->logger->info($missingCacheMessage, [
); 'class' => $localCacheClass,
'use' => 'local',
'app' => 'cli'
]);
$localCacheClass = self::NULL_CACHE;
} else {
throw new \OC\HintException(strtr($missingCacheMessage, [
'{class}' => $localCacheClass, '{use}' => 'local'
]), $missingCacheHint);
}
} }
if (!$distributedCacheClass::isAvailable()) { if (!$distributedCacheClass::isAvailable()) {
throw new \OC\HintException( if (\OC::$CLI && !defined('PHPUNIT_RUN')) {
'Missing memcache class ' . $distributedCacheClass . ' for distributed cache', // CLI should not hard-fail on broken memcache
'Is the matching PHP module installed and enabled ?' $this->logger->info($missingCacheMessage, [
); 'class' => $distributedCacheClass,
'use' => 'distributed',
'app' => 'cli'
]);
$distributedCacheClass = self::NULL_CACHE;
} else {
throw new \OC\HintException(strtr($missingCacheMessage, [
'{class}' => $distributedCacheClass, '{use}' => 'distributed'
]), $missingCacheHint);
}
} }
if (!($lockingCacheClass && $lockingCacheClass::isAvailable())) { if (!($lockingCacheClass && $lockingCacheClass::isAvailable())) {
// dont fallback since the fallback might not be suitable for storing lock // dont fallback since the fallback might not be suitable for storing lock
$lockingCacheClass = '\OC\Memcache\NullCache'; $lockingCacheClass = self::NULL_CACHE;
} }
$this->localCacheClass = $localCacheClass; $this->localCacheClass = $localCacheClass;
$this->distributedCacheClass = $distributedCacheClass; $this->distributedCacheClass = $distributedCacheClass;
$this->lockingCacheClass = $lockingCacheClass; $this->lockingCacheClass = $lockingCacheClass;

View File

@ -50,7 +50,6 @@ use OC\Http\Client\ClientService;
use OC\Lock\MemcacheLockingProvider; use OC\Lock\MemcacheLockingProvider;
use OC\Lock\NoopLockingProvider; use OC\Lock\NoopLockingProvider;
use OC\Mail\Mailer; use OC\Mail\Mailer;
use OC\Memcache\ArrayCache;
use OC\Memcache\NullCache; use OC\Memcache\NullCache;
use OC\Security\CertificateManager; use OC\Security\CertificateManager;
use OC\Security\Crypto; use OC\Security\Crypto;
@ -234,17 +233,17 @@ class Server extends SimpleContainer implements IServerContainer {
$instanceId = \OC_Util::getInstanceId(); $instanceId = \OC_Util::getInstanceId();
$path = \OC::$SERVERROOT; $path = \OC::$SERVERROOT;
$prefix = md5($instanceId.'-'.$version.'-'.$path); $prefix = md5($instanceId.'-'.$version.'-'.$path);
return new \OC\Memcache\Factory($prefix, return new \OC\Memcache\Factory($prefix, $c->getLogger(),
$config->getSystemValue('memcache.local', null), $config->getSystemValue('memcache.local', null),
$config->getSystemValue('memcache.distributed', null), $config->getSystemValue('memcache.distributed', null),
$config->getSystemValue('memcache.locking', null) $config->getSystemValue('memcache.locking', null)
); );
} }
return new \OC\Memcache\Factory('', return new \OC\Memcache\Factory('', $c->getLogger(),
new ArrayCache(), '\\OC\\Memcache\\ArrayCache',
new ArrayCache(), '\\OC\\Memcache\\ArrayCache',
new ArrayCache() '\\OC\\Memcache\\ArrayCache'
); );
}); });
$this->registerService('ActivityManager', function (Server $c) { $this->registerService('ActivityManager', function (Server $c) {

View File

@ -114,7 +114,8 @@ class Test_Factory extends \Test\TestCase {
*/ */
public function testCacheAvailability($localCache, $distributedCache, $lockingCache, public function testCacheAvailability($localCache, $distributedCache, $lockingCache,
$expectedLocalCache, $expectedDistributedCache, $expectedLockingCache) { $expectedLocalCache, $expectedDistributedCache, $expectedLockingCache) {
$factory = new \OC\Memcache\Factory('abc', $localCache, $distributedCache, $lockingCache); $logger = $this->getMockBuilder('\OCP\ILogger')->getMock();
$factory = new \OC\Memcache\Factory('abc', $logger, $localCache, $distributedCache, $lockingCache);
$this->assertTrue(is_a($factory->createLocal(), $expectedLocalCache)); $this->assertTrue(is_a($factory->createLocal(), $expectedLocalCache));
$this->assertTrue(is_a($factory->createDistributed(), $expectedDistributedCache)); $this->assertTrue(is_a($factory->createDistributed(), $expectedDistributedCache));
$this->assertTrue(is_a($factory->createLocking(), $expectedLockingCache)); $this->assertTrue(is_a($factory->createLocking(), $expectedLockingCache));
@ -125,6 +126,7 @@ class Test_Factory extends \Test\TestCase {
* @expectedException \OC\HintException * @expectedException \OC\HintException
*/ */
public function testCacheNotAvailableException($localCache, $distributedCache) { public function testCacheNotAvailableException($localCache, $distributedCache) {
new \OC\Memcache\Factory('abc', $localCache, $distributedCache); $logger = $this->getMockBuilder('\OCP\ILogger')->getMock();
new \OC\Memcache\Factory('abc', $logger, $localCache, $distributedCache);
} }
} }