Eli

To content | To menu | To search

Tag - php - Tag - php - Eli - page 4

Tag - php - Tag - php - Eli - page 4

Thursday, May 6 2010

phpMemCacheAdmin 1.1.2 released

New version of memcached monitoring administration is released and available from http://code.google.com/p/phpmemcacheadmin/

Release Notes

  • Fixed the 1.0 rates in live stats
  • Added the version number

If you have any suggestion or bugs, you can contact me by mail




Wednesday, May 5 2010

Using cas() command from PECL Memcached

Here a simple example on how using cas() command from PECL Memcached to update data that must be at all time up to date

In Memcached, series of commands are not atomic :

A series of commands is not atomic. If you issue a 'get' against an item, operate on the data, then wish to 'set' it back into memcached, you are not guaranteed to be the only process working on that value. In parallel, you could end up overwriting a value set by something else.

But PECL Memcached come with a useful function : cas()

The principle is :

  • Make a get() to get a key value and a cas token (checksum of the value that the get return)
  • Make some update on the value
  • Ask memcached to update the value with cas() method with the cas token as parameter, to let memcached see if the actual value of the key you want to update is the same as he give before or if another script has changed it

Here the code example, we update an index array, adding values in it when not present

# Initializing Memcached connection
$memcache = new Memcached();
$memcache->addServer('localhost', 11211);
 
$indexKey = 'Our index key';
$newValue = 'Some new value to add to index';
$cas = null;
 
# Starting index updating
do 
{
  # Getting data, with parameter $cas to get as we want a cas token
  $indexData = $memcache->get($indexKey, null, $cas);
 
  # Index does not exists, initializing it
  if($indexData === false)
  {
    $indexData = array($newValue);
    $memcache->add($indexKey, $data, 1800);
  }
  # Index exists, updating it
  else
  {
    # Adding new value in index
    $indexData[] = $newValue;
    $memCache->cas($cas, $indexKey, $indexData, 1800);
  }
}
# If result is not stored, or data was updated by another thread
while(($memCache->getResultCode() == Memcached::RES_NOTSTORED) 
      || ($memCache->getResultCode() == Memcached::RES_DATA_EXISTS));

With that little script, we are sure that :

  • If no index are present at start of our script but the index is created in another thread, we retrieve it before adding our value because we use the add() command (Memcached::RES_NOTSTORED)
  • If the index is modified by another script while we are working on it, we get the up to date version before trying to add our new data, thanks to the cas() method (Memcached::RES_DATA_EXISTS)


Remember to be careful with cas(), under heavy concurrent updates, the do ... while() can easily take a lot of execution time, you must add a maximum try count break condition.

And remember to not check command success in a while loop with something like

$memCache->getResultCode() != Memcached::RES_SUCCESS

Memcached::RES_SERVER_ERROR or anything like this and your script will loop forever




Tuesday, April 20 2010

Handling fatal error in PHP with register_shutdown_function

PHP 5.2.0 bring us a new function : error_get_last()

It return an associative array describing the last error with keys "type", "message", "file" and "line" about the last error that occurred.

We can now use it with register_shutdown_function to handle fatal error, log, redirect, html page, instead of the infamous white page.

/**
 * Handling fatal error
 *
 * @return void
 */
function fatalErrorHandler()
{
    # Getting last error
    $error = error_get_last();
 
    # Checking if last error is a fatal error 
    if(($error['type'] === E_ERROR) || ($error['type'] === E_USER_ERROR))
    {
         # Here we handle the error, displaying HTML, logging, ...
         echo 'Sorry, a serious error has occured in ' . $error['file'];
    }
}
 
# Registering shutdown function
register_shutdown_function('fatalErrorHandler');

Resources :

  • http://www.php.net/manual/en/function.error-get-last.php
  • http://php.net/manual/en/errorfunc.constants.php



Monday, April 19 2010

phpMemCacheAdmin version 1.1.1 released

New version is released and available from http://code.google.com/p/phpmemcacheadmin/ and is now compatible with Memcached 1.4.5

Release Notes

  • Added the reclaimed stats and reclaimed rate (New in Memcached 1.4.5)
  • Added the eviction rate
  • Added a "Are you sure ?" for flush_all command
  • Fixed the eviction rate bug on live stats
  • Fixed the PECL Memcache/Memcached API bug with get commands
  • Fixed the flush_all link not working from slabs stats

If you have any suggestion or bugs, you can contact me by mail




Wednesday, April 14 2010

phpMemCacheAdmin version 1.1.0 released

Graphic stand-alone administration for memcached to monitor and debug purpose

This program allows to see in real-time or from the start of the server, stats for get, set, delete, increment, decrement, cas command, as well as server stats (network, items, server version) with googlecharts

You can go further to see each server slabs, occupation, memory wasted and items (key & value).

To extract these informations, phpMemCacheAdmin uses, as you wish, direct communication with server, PECL Memcache or PECL Memcached API.

Download it here : http://code.google.com/p/phpmemcacheadmin/

Do not hesitate to give your comments, ideas and corrections.

Feature list

Statistics

  • Stats for each or all memcached servers
  • Stats for every command : set, get, delete, incr, decr, cas, ...
  • Slabs stats (Memory, pages, wasted, items)
  • Items stats (View items in slabs, then data for each key)
  • Network stats (Traffic, bandwidth)

Commands

  • Execute commands : get, set, delete, flush_all on servers to administrate or debug it
  • Get data with key on servers
  • Delete keys on servers
  • Flush servers

Live Stats

  • Top-like real time stats with configurable alerts
Last update : Tue, 13 Apr 2010 16:12:36 +0200   (refresh rate : 2 sec)
SERVER:PORT                      %MEM   %HIT  HIT/s  CONN   GET/s   SET/s   DEL/s    EVI/s    READ/s   WRITE/s
XXXXXXXXXXXXXXXXXX:11211          0.6   96.8   15.0    39     5.0     5.8     0.0      0.0    7.9 Kb    7.9 Kb
XXXXXXXXXXXXXXX:11211            50.8   95.7  279.5   485   279.5    12.5     0.0      0.0   34.0 Kb  203.5 Kb
XXXXXXXXXXXXXXXX:11211           48.1   95.1  259.3   464   259.5    13.0     0.0      0.0   29.9 Kb  116.3 Kb
XXXXXXXXXXXXXXXX:11211           49.8   96.0  314.8   463   314.5    13.3     0.0      0.0   68.0 Kb  108.5 Kb

Configuration

  • Edit configuration directly from web page
  • phpMemCacheAdmin can use socket communication, PECL Memcache or Memcached API

Compatibility

  • PHP 5.1, 5.2, 5.3 (Linux & Windows)
  • Memcache 1.4+ (Linux & Windows), not compatible with 1.3 or fewer
  • Firefox, Opera, Chrome

Known Bugs

  • Some part may not be very resistant to error
  • Live stats can sometime generate errors, it will be fixed



- page 4 of 5 -