Commit 8a34e439 authored by Sebastian Kummer's avatar Sebastian Kummer

ZP-698 fixed typo in memcached provider filename, when waiting for a

mutex only log up to 5 messages (spread over the configured total amount
of time), if memcache is down don't retry in other connections for up to
30 seconds (configurable), removed TCP_NODELAY and NO_BLOCK options,
memcache servers are setup in a single string, renamed ReInitSharedMem()
to ReInitIPC(), fixed comments and public/private/protected visibility,
fixed upper/lowercasing of method names, added some dots.

Released under the Affero GNU General Public License (AGPL) version 3.
parent 3f17ba03
......@@ -42,10 +42,15 @@
* Consult LICENSE file for details
************************************************/
// The list of available memcache servers
// More servers can be added as 'hostname:port'
// if no port is specified, the default port 11211 will be used
$zpush_ipc_memcached_servers = array('localhost');
// Comma separated list of available memcache servers.
// Servers can be added as 'hostname:port,otherhost:port'
define('MEMCACHED_SERVERS','localhost:11211');
// Memcached down indicator
// In case memcached is not available, a lock file will be written to disk
define('MEMCACHED_DOWN_LOCK_FILE', '/tmp/z-push-memcache-down');
// indicates how long the lock file will be maintained (in seconds)
define('MEMCACHED_DOWN_LOCK_EXPIRATION', 30);
// Prefix to used for keys
define('MEMCACHED_PREFIX', 'z-push-ipc');
......@@ -53,8 +58,8 @@ define('MEMCACHED_PREFIX', 'z-push-ipc');
// Connection timeout in ms
define('MEMCACHED_TIMEOUT', 100);
// Mutex timeout in s
// Mutex timeout (in seconds)
define('MEMCACHED_MUTEX_TIMEOUT', 5);
// Waiting time before re-trying to aquire mutext (in millionth of sec)
define('MEMCACHED_BLOCK_USLEEP', 10000);
// Waiting time before re-trying to aquire mutex (in ms), must be higher than 0
define('MEMCACHED_BLOCK_WAIT', 10);
......@@ -58,33 +58,33 @@ class IpcSharedMemoryProvider implements IIpcProvider {
$this->type = $type;
$this->allocate = $allocate;
if ($this->InitSharedMem())
if ($this->initSharedMem())
ZLog::Write(LOGLEVEL_DEBUG, sprintf("%s(): Initialized mutexid %s and memid %s.", $class, $this->mutexid, $this->memid));
}
/**
* Allocates shared memory
* Allocates shared memory.
*
* @access private
* @return boolean
*/
private function InitSharedMem() {
private function initSharedMem() {
if (!function_exists('sem_get') || !function_exists('shm_attach') || !function_exists('sem_acquire')|| !function_exists('shm_get_var')) {
ZLog::Write(LOGLEVEL_INFO, "IpcSharedMemoryProvider->InitSharedMem(): PHP libraries for the use shared memory are not available. Check the isntalled php packages or use e.g. the memcache IPC provider.");
ZLog::Write(LOGLEVEL_INFO, "IpcSharedMemoryProvider->initSharedMem(): PHP libraries for the use shared memory are not available. Check the isntalled php packages or use e.g. the memcache IPC provider.");
return false;
}
// Create mutex
$this->mutexid = @sem_get($this->type, 1);
if ($this->mutexid === false) {
ZLog::Write(LOGLEVEL_ERROR, "IpcSharedMemoryProvider->InitSharedMem(): could not aquire semaphore");
ZLog::Write(LOGLEVEL_ERROR, "IpcSharedMemoryProvider->initSharedMem(): could not aquire semaphore");
return false;
}
// Attach shared memory
$this->memid = shm_attach($this->type+10, $this->allocate);
if ($this->memid === false) {
ZLog::Write(LOGLEVEL_ERROR, "IpcSharedMemoryProvider->InitSharedMem(): could not attach shared memory");
ZLog::Write(LOGLEVEL_ERROR, "IpcSharedMemoryProvider->initSharedMem(): could not attach shared memory");
@sem_remove($this->mutexid);
$this->mutexid = false;
return false;
......@@ -97,12 +97,12 @@ class IpcSharedMemoryProvider implements IIpcProvider {
}
/**
* Removes and detaches shared memory
* Removes and detaches shared memory.
*
* @access private
* @return boolean
*/
private function RemoveSharedMem() {
private function removeSharedMem() {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false)) {
@sem_acquire($this->mutexid);
$memid = $this->memid;
......@@ -121,17 +121,17 @@ class IpcSharedMemoryProvider implements IIpcProvider {
}
/**
* Reinitializes shared memory by removing, detaching and re-allocating it
* Reinitializes the IPC data by removing, detaching and re-allocating it.
*
* @access public
* @return boolean
*/
public function ReInitSharedMem() {
return ($this->RemoveSharedMem() && $this->InitSharedMem());
public function ReInitIPC() {
return ($this->removeSharedMem() && $this->initSharedMem());
}
/**
* Cleans up the shared memory block
* Cleans up the IPC data block.
*
* @access public
* @return boolean
......@@ -140,12 +140,12 @@ class IpcSharedMemoryProvider implements IIpcProvider {
$stat = false;
// exclusive block
if ($this->blockMutex()) {
$cleanuptime = ($this->hasData(1)) ? $this->getData(1) : false;
if ($this->BlockMutex()) {
$cleanuptime = ($this->HasData(1)) ? $this->GetData(1) : false;
// TODO implement Shared Memory cleanup
$this->releaseMutex();
$this->ReleaseMutex();
}
// end exclusive block
......@@ -153,7 +153,7 @@ class IpcSharedMemoryProvider implements IIpcProvider {
}
/**
* Indicates if the shared memory is active
* Indicates if the IPC is active.
*
* @access public
* @return boolean
......@@ -163,14 +163,14 @@ class IpcSharedMemoryProvider implements IIpcProvider {
}
/**
* Blocks the class mutex
* Blocks the class mutex.
* Method blocks until mutex is available!
* ATTENTION: make sure that you *always* release a blocked mutex!
*
* @access protected
* @access public
* @return boolean
*/
public function blockMutex() {
public function BlockMutex() {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
return @sem_acquire($this->mutexid);
......@@ -178,13 +178,13 @@ class IpcSharedMemoryProvider implements IIpcProvider {
}
/**
* Releases the class mutex
* After the release other processes are able to block the mutex themselfs
* Releases the class mutex.
* After the release other processes are able to block the mutex themselves.
*
* @access protected
* @access public
* @return boolean
*/
public function releaseMutex() {
public function ReleaseMutex() {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
return @sem_release($this->mutexid);
......@@ -192,19 +192,19 @@ class IpcSharedMemoryProvider implements IIpcProvider {
}
/**
* Indicates if the requested variable is available in shared memory
* Indicates if the requested variable is available in IPC data.
*
* @param int $id int indicating the variable
*
* @access protected
* @access public
* @return boolean
*/
public function hasData($id = 2) {
public function HasData($id = 2) {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false)) {
if (function_exists("shm_has_var"))
return @shm_has_var($this->memid, $id);
else {
$some = $this->getData($id);
$some = $this->GetData($id);
return isset($some);
}
}
......@@ -212,14 +212,14 @@ class IpcSharedMemoryProvider implements IIpcProvider {
}
/**
* Returns the requested variable from shared memory
* Returns the requested variable from IPC data.
*
* @param int $id int indicating the variable
*
* @access protected
* @access public
* @return mixed
*/
public function getData($id = 2) {
public function GetData($id = 2) {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
return @shm_get_var($this->memid, $id);
......@@ -227,16 +227,16 @@ class IpcSharedMemoryProvider implements IIpcProvider {
}
/**
* Writes the transmitted variable to shared memory
* Writes the transmitted variable to IPC data.
* Subclasses may never use an id < 2!
*
* @param mixed $data data which should be saved into shared memory
* @param mixed $data data which should be saved into IPC data
* @param int $id int indicating the variable (bigger than 2!)
*
* @access protected
* @access public
* @return boolean
*/
public function setData($data, $id = 2) {
public function SetData($data, $id = 2) {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
return @shm_put_var($this->memid, $id, $data);
......@@ -253,12 +253,12 @@ class IpcSharedMemoryProvider implements IIpcProvider {
$stat = false;
// exclusive block
if ($this->blockMutex()) {
if ($this->BlockMutex()) {
if ($this->hasData(1) == false)
$stat = $this->setData(time(), 1);
if ($this->HasData(1) == false)
$stat = $this->SetData(time(), 1);
$this->releaseMutex();
$this->ReleaseMutex();
}
// end exclusive block
......
......@@ -47,7 +47,7 @@
include_once('backend/ipcsharedmemory/ipcsharedmemoryprovider.php');
abstract class InterProcessData {
// Defines which IPC provider to laod, first has preference
// Defines which IPC provider to load, first has preference
// if IPC_PROVIDER in the main config is set, that class will be loaded
const PROVIDER_LOAD_ORDER = array('IpcMemcachedProvider', 'IpcSharedMemoryProvider');
const CLEANUPTIME = 1;
......@@ -100,12 +100,12 @@ abstract class InterProcessData {
}
/**
* Initializes internal parameters
* Initializes internal parameters.
*
* @access public
* @access protected
* @return boolean
*/
public function InitializeParams() {
protected function initializeParams() {
if (!isset(self::$devid)) {
self::$devid = Request::GetDeviceID();
self::$pid = @getmypid();
......@@ -116,7 +116,17 @@ abstract class InterProcessData {
}
/**
* Cleans up the shared memory block
* Reinitializes the IPC data by removing, detaching and re-allocating it.
*
* @access public
* @return boolean
*/
public function ReInitIPC() {
return $this->ipcProvider ? $this->ipcProvider->ReInitIPC() : false;
}
/**
* Cleans up the IPC data block.
*
* @access public
* @return boolean
......@@ -126,7 +136,7 @@ abstract class InterProcessData {
}
/**
* Indicates if the shared memory is active
* Indicates if the IPC is active.
*
* @access public
* @return boolean
......@@ -136,7 +146,7 @@ abstract class InterProcessData {
}
/**
* Blocks the class mutex
* Blocks the class mutex.
* Method blocks until mutex is available!
* ATTENTION: make sure that you *always* release a blocked mutex!
*
......@@ -144,22 +154,22 @@ abstract class InterProcessData {
* @return boolean
*/
protected function blockMutex() {
return $this->ipcProvider ? $this->ipcProvider->blockMutex() : false;
return $this->ipcProvider ? $this->ipcProvider->BlockMutex() : false;
}
/**
* Releases the class mutex
* After the release other processes are able to block the mutex themselfs
* Releases the class mutex.
* After the release other processes are able to block the mutex themselves.
*
* @access protected
* @return boolean
*/
protected function releaseMutex() {
return $this->ipcProvider ? $this->ipcProvider->releaseMutex() : false;
return $this->ipcProvider ? $this->ipcProvider->ReleaseMutex() : false;
}
/**
* Indicates if the requested variable is available in shared memory
* Indicates if the requested variable is available in IPC data.
*
* @param int $id int indicating the variable
*
......@@ -167,11 +177,11 @@ abstract class InterProcessData {
* @return boolean
*/
protected function hasData($id = 2) {
return $this->ipcProvider ? $this->ipcProvider->hasData($id) : false;
return $this->ipcProvider ? $this->ipcProvider->HasData($id) : false;
}
/**
* Returns the requested variable from shared memory
* Returns the requested variable from IPC data.
*
* @param int $id int indicating the variable
*
......@@ -179,20 +189,20 @@ abstract class InterProcessData {
* @return mixed
*/
protected function getData($id = 2) {
return $this->ipcProvider ? $this->ipcProvider->getData($id) : null;
return $this->ipcProvider ? $this->ipcProvider->GetData($id) : null;
}
/**
* Writes the transmitted variable to shared memory
* Writes the transmitted variable to IPC data.
* Subclasses may never use an id < 2!
*
* @param mixed $data data which should be saved into shared memory
* @param mixed $data data which should be saved into IPC data
* @param int $id int indicating the variable (bigger than 2!)
*
* @access protected
* @return boolean
*/
protected function setData($data, $id = 2) {
return $this->ipcProvider ? $this->ipcProvider->setData($data, $id) : false;
return $this->ipcProvider ? $this->ipcProvider->SetData($data, $id) : false;
}
}
......@@ -305,7 +305,7 @@ class LoopDetection extends InterProcessData {
*/
private function updateProcessStack() {
// initialize params
$this->InitializeParams();
$this->initializeParams();
if ($this->blockMutex()) {
$loopdata = ($this->hasData()) ? $this->getData() : array();
......@@ -354,7 +354,7 @@ class LoopDetection extends InterProcessData {
*/
private function getProcessStack() {
// initialize params
$this->InitializeParams();
$this->initializeParams();
$stack = array();
if ($this->blockMutex()) {
......@@ -401,7 +401,7 @@ class LoopDetection extends InterProcessData {
$brokenkey = self::BROKENMSGS ."-". $folderid;
// initialize params
$this->InitializeParams();
$this->initializeParams();
if ($this->blockMutex()) {
$loopdata = ($this->hasData()) ? $this->getData() : array();
......@@ -443,7 +443,7 @@ class LoopDetection extends InterProcessData {
$okIds = array();
// initialize params
$this->InitializeParams();
$this->initializeParams();
if ($this->blockMutex()) {
$loopdata = ($this->hasData()) ? $this->getData() : array();
......@@ -504,7 +504,7 @@ class LoopDetection extends InterProcessData {
*/
public function SetSyncStateUsage($folderid, $uuid, $counter) {
// initialize params
$this->InitializeParams();
$this->initializeParams();
ZLog::Write(LOGLEVEL_DEBUG, sprintf("LoopDetection->SetSyncStateUsage(): uuid: %s counter: %d", $uuid, $counter));
......@@ -551,7 +551,7 @@ class LoopDetection extends InterProcessData {
*/
public function IsSyncStateObsolete($folderid, $uuid, $counter) {
// initialize params
$this->InitializeParams();
$this->initializeParams();
$obsolete = false;
......@@ -636,7 +636,7 @@ class LoopDetection extends InterProcessData {
}
// initialize params
$this->InitializeParams();
$this->initializeParams();
$loop = false;
......
......@@ -89,7 +89,7 @@ class PingTracking extends InterProcessData {
$stat = false;
// initialize params
$this->InitializeParams();
$this->initializeParams();
// exclusive block
if ($this->blockMutex()) {
......
......@@ -63,7 +63,7 @@ class TopCollector extends InterProcessData {
parent::__construct();
// initialize params
$this->InitializeParams();
$this->initializeParams();
$this->preserved = array();
// static vars come from the parent class
......@@ -222,9 +222,10 @@ class TopCollector extends InterProcessData {
}
}
}
foreach ($toClear as $tc)
foreach ($toClear as $tc) {
unset($topdata[$tc[0]][$tc[1]][$tc[2]]);
}
}
$stat = $this->setData($topdata, self::TOPDATA);
$this->releaseMutex();
......@@ -260,6 +261,20 @@ class TopCollector extends InterProcessData {
return true;
}
/**
* Reinitializes the IPC data.
*
* @access public
* @return boolean
*/
public function ReInitIPC() {
$status = parent::ReInitIPC();
if (!status) {
$this->SetData(array(), self::TOPDATA);
}
return $status;
}
/**
* Indicates if top data should be saved or not
* Returns true for 10 seconds after the latest CollectData()
......
......@@ -74,7 +74,7 @@ class SimpleMutex extends InterProcessData {
/**
* Releases the mutex
* After the release other processes are able to block the mutex themselfs
* After the release other processes are able to block the mutex themselves
*
* @access public
* @return boolean
......
......@@ -54,7 +54,16 @@ interface IIpcProvider
public function __construct($type, $allocate, $class);
/**
* Cleans up the shared memory block
* Reinitializes the IPC data. If the provider has no way of performing
* this action, it should return 'false'.
*
* @access public
* @return boolean
*/
public function ReInitIPC();
/**
* Cleans up the IPC data block.
*
* @access public
* @return boolean
......@@ -62,7 +71,7 @@ interface IIpcProvider
public function Clean();
/**
* Indicates if the shared memory is active
* Indicates if the IPC is active.
*
* @access public
* @return boolean
......@@ -70,53 +79,53 @@ interface IIpcProvider
public function IsActive();
/**
* Blocks the class mutex
* Blocks the class mutex.
* Method blocks until mutex is available!
* ATTENTION: make sure that you *always* release a blocked mutex!
*
* @access protected
* @access public
* @return boolean
*/
public function blockMutex();
public function BlockMutex();
/**
* Releases the class mutex
* After the release other processes are able to block the mutex themselfs
* Releases the class mutex.
* After the release other processes are able to block the mutex themselves.
*
* @access protected
* @access public
* @return boolean
*/
public function releaseMutex();
public function ReleaseMutex();
/**
* Indicates if the requested variable is available in shared memory
* Indicates if the requested variable is available in IPC data.
*
* @param int $id int indicating the variable
*
* @access protected
* @access public
* @return boolean
*/
public function hasData($id = 2);
public function HasData($id = 2);
/**
* Returns the requested variable from shared memory
* Returns the requested variable from IPC data.
*
* @param int $id int indicating the variable
*
* @access protected
* @access public
* @return mixed
*/
public function getData($id = 2);
public function GetData($id = 2);
/**
* Writes the transmitted variable to shared memory
* Writes the transmitted variable to IPC data.
* Subclasses may never use an id < 2!
*
* @param mixed $data data which should be saved into shared memory
* @param mixed $data data which should be saved into IPC data
* @param int $id int indicating the variable (bigger than 2!)
*
* @access protected
* @access public
* @return boolean
*/
public function setData($data, $id = 2);
public function SetData($data, $id = 2);
}
......@@ -480,7 +480,7 @@ class ZPushTop {
else if ($cmds[0] == "clear" ) {
$this->topCollector->ClearLatest(true);
$this->topCollector->CollectData(true);
$this->topCollector->ReInitSharedMem();
$this->topCollector->ReInitIPC();
}
else if ($cmds[0] == "filter" || $cmds[0] == "f") {
if (!isset($cmds[1]) || $cmds[1] == "") {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment