php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #58135 lock state of fetch, store & delete
Submitted: 2008-04-01 20:45 UTC Modified: 2009-06-14 00:10 UTC
From: daniz at rocketmail dot com Assigned:
Status: Closed Package: APC (PECL)
PHP Version: 5.2.5 OS: ubuntu
Private report: No CVE-ID: None
 [2008-04-01 20:45 UTC] daniz at rocketmail dot com
Description:
------------
I've never understood why apc user cache won't work where I currently use memcache. My site does some heavy caching with small amount of data that gets fetched quite often. So no large blob texts and not that big arrays.

My problem is that somehow apc looses track of it's cache. It stores it, fetches it but after a couple of seconds it freaks out and starts fetching deleted (apc_delete) cache? I'm not sure if this is normal behavior or not but I feel that if memcache can handle it without mixing data, so should apc as it would be much faster for me since I'm on 1 server.

I've actually seen apc-info admin show different dates/values on the same SH key (eg SH=6ba6575713261521bf976db277089757) and when I hit f5 another value/date would come up using that same hash even thought it was previously deleted.

This only happens on my live environment where there is a lot going on, I cannot reproduce this anywhere atm but I'm sure this is not how it's supposed to work?

This is what it looked like after ~ 1min with apc cache on. Hopefully I'm just having my config all wrong for this.

Cached Variables	1122 ( 4.7 MBytes)
Hits	6977630
Misses	0
Request Rate (hits, misses)	117.07 cache requests/second
Hit Rate	117.07 cache requests/second
Miss Rate	0.00 cache requests/second
Insert Rate	0.02 cache requests/second
Cache full count	0


Shared Memory	1 Segment(s) with 128.0 MBytes
(mmap memory, pthread mutex locking)

Uptime	 16 hours and 33 minutes


Runtime Settings
apc.cache_by_default	1
apc.coredump_unmap	0
apc.enable_cli	0
apc.enabled	1
apc.file_update_protection	2
apc.filters	
apc.gc_ttl	3600
apc.include_once_override	1
apc.max_file_size	1M
apc.mmap_file_mask	
apc.num_files_hint	512
apc.report_autofilter	0
apc.shm_segments	1
apc.shm_size	128
apc.slam_defense	0
apc.stat	1
apc.stat_ctime	0
apc.ttl	7200
apc.user_entries_hint	4096
apc.user_ttl	7200
apc.write_lock	1


specs:
intel xenon quad cpu
ubuntu 32bit



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-02 14:08 UTC] gopalv82 at yahoo dot com
fastcgi?
 [2008-04-02 16:06 UTC] daniz at rocketmail dot com
Ah my bad, I'm using Litespeed with the Litespeed sApi
 [2009-03-24 14:38 UTC] shire@php.net
I'm not sure I understand the problem here, are you saying that APC is fetching deleted entries from the cache and returning them?
 [2009-03-24 15:10 UTC] daniz at rocketmail dot com
Hey shire :) Long time since I submitted this bug. But what I recall was that 'random' cache entries that were apc_delete:ed we're still in the cache. I used the cache to store a sql call that checks if a user had new messages or not... When a user then clicks on his inbox it deletes the cache + sets it to 0 in the db. Then it's of cource supposed to re-cache the 0 into apc, but many users experienced this bit very flaky. And I saw it too in the apc.php user_cache entries. I even found a cache entry and when hitting the reload button the cache was randomly switched from 0 to 5 (or the amount of new messages) in the apc.php ... I should have taken screenshots as it's hard to believe :).

I will be releasing a new version of my site where I can retry this part of my code, since it's random it's hard to reproduce and does not give you guys much to go after.. So I'll test this on my new setup using nginx -> php-fpm + latest apc and php version just to be sure it wasn't a random incident. I can report back when it's tested.
 [2009-03-24 18:26 UTC] shire@php.net
Hey thanks for the quick response.  Yeah sorry for the delay on getting back to this we have some delinquent bugs ;-)

It sounds to me like this is likely to be some application race-condition related bug, rather than something internal to APC.  

So you're storing these values and then using the apc_delete() call to delete them from the cache until they get re-inserted by apc_store() again on another request? (which should then be 0 ?).  Is it possible another process is running at the same time that's modifying these values? ie: updating it with a value while after the value is deleted from apc, but before it's updated in the database?

It would be great if you could do the retest as you suggest and let us know what you find, or if you're lucky enough to come across a reproduction.  Thanks!
 [2009-03-25 01:35 UTC] daniz at rocketmail dot com
I'm sure it can be a race condition problem. Each update is made from another process. 

1. recieving user has an ajax call asking the server if any new messages have arrived.
2. recieving user deletes cache when hitting the inbox button.
3. senders delete the recievers cache when sending him a message to make his ajax call show correct amount of new messages.

That's basically it, the sucky part is that when switching to memcached without modifying anything other than the cache class, this procedure worked 100% and still does today.. That's why I figured it must be APC related. And since I'm on 1 server, this gives me a huge performance loss because of not being able to use APC cache :)
 [2009-03-25 14:03 UTC] shire@php.net
Have you taken a look at the apc_add() functionality?  http://us.php.net/manual/en/function.apc-add.php

This allows you to add an entry, but only if it's not already in the cache.  Perhaps this, or some other combination of functionality, would help you solve your race condition?  We've also added a set of atomic increment and decrement functions, these might be useful as well.  (I don't think they are documented yet so you can dig into php_apc.c for prototypes or ping me and I'll get them for you).

This isn't sounding like an APC bug so I'll proabbly mark this as bogus unless we come across something else, but let's try to see if we can resolve your problem here as we should be able to make this work with APC.
 [2009-03-30 02:02 UTC] daniz at rocketmail dot com
yea the apc_add won't help as I do a if(!apc_get) then mysq_get & apc_store.. So it's basically an apc_add :) 

Anyways I tested it yday on my old code just to see if it had been fixed. And it seemed like it did. The reason could be that I'm not on litespeed sapi anymore, or a newer version of php/apc, or that i turned of the require_once apc feature. 
But whatever it was it seems to be gone and I can finally have apc only cache! Nice to see you active shire, love reading your blog :)

You may set the ticket to closed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 00:01:31 2024 UTC