1 /* cache.c - routines to maintain an in-core cache of entries */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/string.h>
14 #include <ac/socket.h>
20 /* BDB backend specific entry info -- visible only to the cache */
21 typedef struct bdb_entry_info {
23 ldap_pvt_thread_rdwr_t bei_rdwr; /* reader/writer lock */
27 * remaining fields require backend cache lock to access
28 * These items are specific to the BDB backend and should
31 int bei_state; /* for the cache */
32 #define CACHE_ENTRY_UNDEFINED 0
33 #define CACHE_ENTRY_CREATING 1
34 #define CACHE_ENTRY_READY 2
35 #define CACHE_ENTRY_DELETED 3
36 #define CACHE_ENTRY_COMMITTED 4
38 int bei_refcnt; /* # threads ref'ing this entry */
39 Entry *bei_lrunext; /* for cache lru list */
43 #define BEI(e) ((EntryInfo *) ((e)->e_private))
45 static int bdb_cache_delete_entry_internal(Cache *cache, Entry *e);
47 static void bdb_lru_print(Cache *cache);
52 bdb_cache_entry_rdwr_lock(Entry *e, int rw)
55 LDAP_LOG( CACHE, ENTRY,
56 "bdb_cache_entry_rdwr_lock: %s lock on ID %ld\n",
57 rw ? "w" : "r", e->e_id, 0 );
59 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %ld\n",
60 rw ? "w" : "r", e->e_id, 0);
64 return ldap_pvt_thread_rdwr_wlock(&BEI(e)->bei_rdwr);
66 return ldap_pvt_thread_rdwr_rlock(&BEI(e)->bei_rdwr);
70 bdb_cache_entry_rdwr_trylock(Entry *e, int rw)
73 LDAP_LOG( CACHE, ENTRY,
74 "bdb_cache_entry_rdwr_trylock: try %s lock on ID: %ld.\n",
75 rw ? "w" : "r", e->e_id, 0 );
77 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%strylock: ID: %ld\n",
78 rw ? "w" : "r", e->e_id, 0);
82 return ldap_pvt_thread_rdwr_wtrylock(&BEI(e)->bei_rdwr);
84 return ldap_pvt_thread_rdwr_rtrylock(&BEI(e)->bei_rdwr);
88 bdb_cache_entry_rdwr_unlock(Entry *e, int rw)
91 LDAP_LOG( CACHE, ENTRY,
92 "bdb_cache_entry_rdwr_unlock: remove %s lock on ID %ld.\n",
93 rw ? "w" : "r", e->e_id, 0 );
95 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
96 rw ? "w" : "r", e->e_id, 0);
100 return ldap_pvt_thread_rdwr_wunlock(&BEI(e)->bei_rdwr);
102 return ldap_pvt_thread_rdwr_runlock(&BEI(e)->bei_rdwr);
108 bdb_cache_entry_rdwr_init(Entry *e)
110 return ldap_pvt_thread_rdwr_init( &BEI(e)->bei_rdwr );
114 bdb_cache_entry_rdwr_destroy(Entry *e)
116 return ldap_pvt_thread_rdwr_destroy( &BEI(e)->bei_rdwr );
121 bdb_cache_entry_private_init( Entry *e )
123 assert( e->e_private == NULL );
125 if( e->e_private != NULL ) {
126 /* this should never happen */
130 e->e_private = ch_calloc(1, sizeof(struct bdb_entry_info));
133 if( bdb_cache_entry_rdwr_init( e ) != 0 ) {
144 bdb_cache_entry_db_lock
145 ( DB_ENV *env, u_int32_t locker, Entry *e, int rw, u_int32_t flags, DB_LOCK *lock )
155 db_rw = DB_LOCK_WRITE;
157 db_rw = DB_LOCK_READ;
159 lockobj.data = e->e_nname.bv_val;
160 lockobj.size = e->e_nname.bv_len;
161 rc = LOCK_GET(env, locker, flags | DB_LOCK_NOWAIT,
162 &lockobj, db_rw, lock);
165 LDAP_LOG( CACHE, DETAIL1,
166 "bdb_cache_entry_db_lock: entry %s, rw %d, rc %d\n",
167 e->e_nname.bv_val, rw, rc );
169 Debug( LDAP_DEBUG_TRACE,
170 "bdb_cache_entry_db_lock: entry %s, rw %d, rc %d\n",
171 e->e_nname.bv_val, rw, rc );
175 #endif /* NO_THREADS */
179 bdb_cache_entry_db_unlock
180 ( DB_ENV *env, DB_LOCK *lock )
187 rc = LOCK_PUT ( env, lock );
193 * marks an entry in CREATING state as committed, so it is really returned
194 * to the cache. Otherwise an entry in CREATING state is removed.
195 * Makes e_private be destroyed at the following cache_return_entry_w,
196 * but lets the entry untouched (owned by someone else)
199 bdb_cache_entry_commit( Entry *e )
202 assert( e->e_private );
203 assert( BEI(e)->bei_state == CACHE_ENTRY_CREATING );
204 /* assert( BEI(e)->bei_refcnt == 1 ); */
206 BEI(e)->bei_state = CACHE_ENTRY_COMMITTED;
210 bdb_cache_entry_private_destroy( Entry *e )
212 assert( e->e_private );
215 bdb_cache_entry_rdwr_destroy( e );
218 free( e->e_private );
224 bdb_unlocked_cache_return_entry_rw( Cache *cache, Entry *e, int rw )
228 int refcnt, freeit = 1;
230 /* set cache write lock */
231 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
233 assert( e->e_private );
236 bdb_cache_entry_rdwr_unlock(e, rw);
240 refcnt = --BEI(e)->bei_refcnt;
243 * if the entry is returned when in CREATING state, it is deleted
244 * but not freed because it may belong to someone else (do_add,
247 if ( BEI(e)->bei_state == CACHE_ENTRY_CREATING ) {
249 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
250 bdb_cache_delete_entry_internal( cache, e );
252 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
254 /* now the entry is in DELETED state */
257 if ( BEI(e)->bei_state == CACHE_ENTRY_COMMITTED ) {
258 BEI(e)->bei_state = CACHE_ENTRY_READY;
260 /* free cache write lock */
261 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
264 LDAP_LOG( CACHE, DETAIL1,
265 "bdb_unlocked_cache_return_entry_rw: return (%ld):%s, refcnt=%d\n",
266 id, rw ? "w" : "r", refcnt );
268 Debug( LDAP_DEBUG_TRACE,
269 "====> bdb_unlocked_cache_return_entry_%s( %ld ): created (%d)\n",
270 rw ? "w" : "r", id, refcnt );
274 } else if ( BEI(e)->bei_state == CACHE_ENTRY_DELETED ) {
276 /* free cache write lock */
277 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
280 LDAP_LOG( CACHE, DETAIL1,
281 "bdb_unlocked_cache_return_entry_rw: %ld, delete pending (%d).\n",
284 Debug( LDAP_DEBUG_TRACE,
285 "====> bdb_unlocked_cache_return_entry_%s( %ld ): delete pending (%d)\n",
286 rw ? "w" : "r", id, refcnt );
290 bdb_cache_entry_private_destroy( e );
292 bdb_entry_return( e );
295 /* free cache write lock */
296 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
299 LDAP_LOG( CACHE, DETAIL1,
300 "bdb_unlocked_cache_return_entry_rw: (%ld): deleted (%d)\n",
303 Debug( LDAP_DEBUG_TRACE,
304 "====> bdb_unlocked_cache_return_entry_%s( %ld ): deleted (%d)\n",
305 rw ? "w" : "r", id, refcnt );
310 /* free cache write lock */
311 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
314 LDAP_LOG( CACHE, DETAIL1,
315 "bdb_unlocked_cache_return_entry_rw: ID %ld:%s returned (%d)\n",
316 id, rw ? "w": "r", refcnt );
318 Debug( LDAP_DEBUG_TRACE,
319 "====> bdb_unlocked_cache_return_entry_%s( %ld ): returned (%d)\n",
320 rw ? "w" : "r", id, refcnt);
326 bdb_cache_return_entry_rw
327 ( DB_ENV *env, Cache *cache, Entry *e, int rw, DB_LOCK *lock )
330 int refcnt, freeit = 1;
332 /* set cache write lock */
333 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
335 assert( e->e_private );
337 bdb_cache_entry_db_unlock( env, lock );
339 bdb_cache_entry_rdwr_unlock(e, rw);
343 refcnt = --BEI(e)->bei_refcnt;
346 * if the entry is returned when in CREATING state, it is deleted
347 * but not freed because it may belong to someone else (do_add,
350 if ( BEI(e)->bei_state == CACHE_ENTRY_CREATING ) {
352 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
353 bdb_cache_delete_entry_internal( cache, e );
355 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
357 /* now the entry is in DELETED state */
360 if ( BEI(e)->bei_state == CACHE_ENTRY_COMMITTED ) {
361 BEI(e)->bei_state = CACHE_ENTRY_READY;
363 /* free cache write lock */
364 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
367 LDAP_LOG( CACHE, DETAIL1,
368 "bdb_cache_return_entry_rw: return (%ld):%s, refcnt=%d\n",
369 id, rw ? "w" : "r", refcnt );
371 Debug( LDAP_DEBUG_TRACE,
372 "====> bdb_cache_return_entry_%s( %ld ): created (%d)\n",
373 rw ? "w" : "r", id, refcnt );
377 } else if ( BEI(e)->bei_state == CACHE_ENTRY_DELETED ) {
379 /* free cache write lock */
380 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
383 LDAP_LOG( CACHE, DETAIL1,
384 "bdb_cache_return_entry_rw: %ld, delete pending (%d).\n",
387 Debug( LDAP_DEBUG_TRACE,
388 "====> bdb_cache_return_entry_%s( %ld ): delete pending (%d)\n",
389 rw ? "w" : "r", id, refcnt );
393 bdb_cache_entry_private_destroy( e );
395 bdb_entry_return( e );
398 /* free cache write lock */
399 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
402 LDAP_LOG( CACHE, DETAIL1,
403 "bdb_cache_return_entry_rw: (%ld): deleted (%d)\n",
406 Debug( LDAP_DEBUG_TRACE,
407 "====> bdb_cache_return_entry_%s( %ld ): deleted (%d)\n",
408 rw ? "w" : "r", id, refcnt );
413 /* free cache write lock */
414 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
417 LDAP_LOG( CACHE, DETAIL1,
418 "bdb_cache_return_entry_rw: ID %ld:%s returned (%d)\n",
419 id, rw ? "w": "r", refcnt );
421 Debug( LDAP_DEBUG_TRACE,
422 "====> bdb_cache_return_entry_%s( %ld ): returned (%d)\n",
423 rw ? "w" : "r", id, refcnt);
428 #define LRU_DELETE( cache, e ) do { \
429 if ( BEI(e)->bei_lruprev != NULL ) { \
430 BEI(BEI(e)->bei_lruprev)->bei_lrunext = BEI(e)->bei_lrunext; \
432 (cache)->c_lruhead = BEI(e)->bei_lrunext; \
434 if ( BEI(e)->bei_lrunext != NULL ) { \
435 BEI(BEI(e)->bei_lrunext)->bei_lruprev = BEI(e)->bei_lruprev; \
437 (cache)->c_lrutail = BEI(e)->bei_lruprev; \
441 #define LRU_ADD( cache, e ) do { \
442 BEI(e)->bei_lrunext = (cache)->c_lruhead; \
443 if ( BEI(e)->bei_lrunext != NULL ) { \
444 BEI(BEI(e)->bei_lrunext)->bei_lruprev = (e); \
446 (cache)->c_lruhead = (e); \
447 BEI(e)->bei_lruprev = NULL; \
448 if ( (cache)->c_lrutail == NULL ) { \
449 (cache)->c_lrutail = (e); \
454 * cache_add_entry_rw - create and lock an entry in the cache
455 * returns: 0 entry has been created and locked
456 * 1 entry already existed
457 * -1 something bad happened
458 * other Berkeley DB locking error code
461 bdb_cache_add_entry_rw(
474 LDAP_LOG( CACHE, ENTRY,
475 "bdb_cache_add_entry_rw: add (%s):%s to cache\n",
476 e->e_dn, rw ? "w" : "r", 0 );
478 /* set cache write lock */
479 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
481 assert( e->e_private == NULL );
483 if( bdb_cache_entry_private_init(e) != 0 ) {
484 /* free cache write lock */
485 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
488 LDAP_LOG( CACHE, ERR,
489 "bdb_cache_add_entry_rw: add (%s):%ld private init failed!\n",
490 e->e_dn, e->e_id, 0 );
492 Debug( LDAP_DEBUG_ANY,
493 "====> bdb_cache_add_entry( %ld ): \"%s\": private init failed!\n",
494 e->e_id, e->e_dn, 0 );
501 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
502 entry_dn_cmp, avl_dup_error ) != 0 )
504 /* free cache write lock */
505 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
508 LDAP_LOG( CACHE, DETAIL1,
509 "bdb_cache_add_entry: (%s):%ld already in cache.\n",
510 e->e_dn, e->e_id, 0 );
512 Debug( LDAP_DEBUG_TRACE,
513 "====> bdb_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
514 e->e_id, e->e_dn, 0 );
517 bdb_cache_entry_private_destroy(e);
523 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
524 entry_id_cmp, avl_dup_error ) != 0 )
527 LDAP_LOG( CACHE, DETAIL1,
528 "bdb_cache_add_entry: (%s):%ls already in cache.\n",
529 e->e_dn, e->e_id, 0 );
531 Debug( LDAP_DEBUG_ANY,
532 "====> bdb_cache_add_entry( %ld ): \"%s\": already in id cache\n",
533 e->e_id, e->e_dn, 0 );
536 /* delete from dn tree inserted above */
537 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
538 entry_dn_cmp ) == NULL )
541 LDAP_LOG( CACHE, INFO,
542 "bdb_cache_add_entry: can't delete (%s) from cache.\n",
545 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
550 bdb_cache_entry_private_destroy(e);
552 /* free cache write lock */
553 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
557 rc = bdb_cache_entry_db_lock( env, locker, e, rw, 0, lock );
561 case DB_LOCK_DEADLOCK :
562 case DB_LOCK_NOTGRANTED :
563 /* undo avl changes immediately */
564 if ( avl_delete( &cache->c_idtree, (caddr_t) e,
565 entry_id_cmp ) == NULL ) {
567 LDAP_LOG( CACHE, INFO,
568 "bdb_cache_add_entry: can't delete (%s) from cache.\n",
571 Debug( LDAP_DEBUG_ANY, "====> can't delete from id cache\n", 0, 0, 0 );
574 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
575 entry_dn_cmp ) == NULL ) {
577 LDAP_LOG( CACHE, INFO,
578 "bdb_cache_add_entry: can't delete (%s) from cache.\n",
581 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n", 0, 0, 0 );
586 bdb_cache_entry_private_destroy(e);
587 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
591 /* put the entry into 'CREATING' state */
592 /* will be marked after when entry is returned */
593 BEI(e)->bei_state = CACHE_ENTRY_CREATING;
594 BEI(e)->bei_refcnt = 1;
597 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
600 if ( ++cache->c_cursize > cache->c_maxsize ) {
602 * find the lru entry not currently in use and delete it.
603 * in case a lot of entries are in use, only look at the
604 * first 10 on the tail of the list.
607 while ( cache->c_lrutail != NULL &&
608 BEI(cache->c_lrutail)->bei_refcnt != 0 &&
611 /* move this in-use entry to the front of the q */
612 ee = cache->c_lrutail;
613 LRU_DELETE( cache, ee );
614 LRU_ADD( cache, ee );
619 * found at least one to delete - try to get back under
620 * the max cache size.
622 while ( cache->c_lrutail != NULL &&
623 BEI(cache->c_lrutail)->bei_refcnt == 0 &&
624 cache->c_cursize > cache->c_maxsize )
626 e = cache->c_lrutail;
628 /* delete from cache and lru q */
629 /* XXX do we need rc ? */
630 rc = bdb_cache_delete_entry_internal( cache, e );
631 bdb_cache_entry_private_destroy( e );
632 bdb_entry_return( e );
637 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
638 /* free cache write lock */
639 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
644 * cache_update_entry - update a LOCKED entry which has been deleted.
645 * returns: 0 entry has been created and locked
646 * 1 entry already existed
647 * -1 something bad happened
650 bdb_cache_update_entry(
658 /* set cache write lock */
659 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
661 assert( e->e_private );
663 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
664 entry_dn_cmp, avl_dup_error ) != 0 )
667 LDAP_LOG( CACHE, DETAIL1,
668 "bdb_cache_update_entry: (%s):%ld already in dn cache\n",
669 e->e_dn, e->e_id, 0 );
671 Debug( LDAP_DEBUG_TRACE,
672 "====> bdb_cache_update_entry( %ld ): \"%s\": already in dn cache\n",
673 e->e_id, e->e_dn, 0 );
676 /* free cache write lock */
677 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
682 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
683 entry_id_cmp, avl_dup_error ) != 0 )
686 LDAP_LOG( CACHE, DETAIL1,
687 "bdb_cache_update_entry: (%s)%ld already in id cache\n",
688 e->e_dn, e->e_id, 0 );
690 Debug( LDAP_DEBUG_ANY,
691 "====> bdb_cache_update_entry( %ld ): \"%s\": already in id cache\n",
692 e->e_id, e->e_dn, 0 );
695 /* delete from dn tree inserted above */
696 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
697 entry_dn_cmp ) == NULL )
700 LDAP_LOG( CACHE, INFO,
701 "bdb_cache_update_entry: can't delete (%s)%ld from dn cache.\n",
702 e->e_dn, e->e_id, 0 );
704 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
709 /* free cache write lock */
710 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
715 /* put the entry into 'CREATING' state */
716 /* will be marked after when entry is returned */
717 BEI(e)->bei_state = CACHE_ENTRY_CREATING;
720 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
723 if ( ++cache->c_cursize > cache->c_maxsize ) {
725 * find the lru entry not currently in use and delete it.
726 * in case a lot of entries are in use, only look at the
727 * first 10 on the tail of the list.
730 while ( cache->c_lrutail != NULL &&
731 BEI(cache->c_lrutail)->bei_refcnt != 0 &&
734 /* move this in-use entry to the front of the q */
735 ee = cache->c_lrutail;
736 LRU_DELETE( cache, ee );
737 LRU_ADD( cache, ee );
742 * found at least one to delete - try to get back under
743 * the max cache size.
745 while ( cache->c_lrutail != NULL &&
746 BEI(cache->c_lrutail)->bei_refcnt == 0 &&
747 cache->c_cursize > cache->c_maxsize )
749 e = cache->c_lrutail;
751 /* delete from cache and lru q */
752 /* XXX do we need rc ? */
753 rc = bdb_cache_delete_entry_internal( cache, e );
754 bdb_cache_entry_private_destroy( e );
755 bdb_entry_return( e );
760 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
761 /* free cache write lock */
762 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
767 bdb_cache_find_entry_ndn2id(
777 /* this function is always called with normalized DN */
781 /* set cache read lock */
782 ldap_pvt_thread_rdwr_rlock( &cache->c_rwlock );
784 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
785 entry_dn_cmp )) != NULL )
791 * ep now points to an unlocked entry
792 * we do not need to lock the entry if we only
793 * check the state, refcnt, LRU, and id.
796 assert( ep->e_private );
800 state = BEI(ep)->bei_state;
803 * entry is deleted or not fully created yet
805 if ( state != CACHE_ENTRY_READY && state != CACHE_ENTRY_COMMITTED ) {
806 assert(state != CACHE_ENTRY_UNDEFINED);
808 /* free cache read lock */
809 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
812 LDAP_LOG( CACHE, INFO,
813 "bdb_cache_find_entry_dn2id: (%s) %ld not ready: %d\n",
814 ndn->bv_val, id, state );
816 Debug(LDAP_DEBUG_TRACE,
817 "====> bdb_cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
818 ndn->bv_val, id, state);
822 ldap_pvt_thread_yield();
826 /* free cache read lock */
827 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
830 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
833 LRU_DELETE( cache, ep );
834 LRU_ADD( cache, ep );
837 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
840 LDAP_LOG( CACHE, DETAIL1,
841 "bdb_cache_find_entry_dn2id: (%s): %ld %d tries\n",
842 ndn->bv_val, id, count );
844 Debug(LDAP_DEBUG_TRACE,
845 "====> bdb_cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
846 ndn->bv_val, id, count);
850 /* free cache read lock */
851 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
860 * cache_find_entry_id - find an entry in the cache, given id
864 bdb_cache_find_entry_id(
881 /* set cache read lock */
882 ldap_pvt_thread_rdwr_rlock( &cache->c_rwlock );
884 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
885 entry_id_cmp )) != NULL )
892 assert( ep->e_private );
895 state = BEI(ep)->bei_state;
898 * entry is deleted or not fully created yet
900 if ( state != CACHE_ENTRY_READY && state != CACHE_ENTRY_COMMITTED ) {
902 assert(state != CACHE_ENTRY_UNDEFINED);
904 /* free cache read lock */
905 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
908 LDAP_LOG( CACHE, INFO,
909 "bdb_cache_find_entry_id: (%ld)->%ld not ready (%d).\n",
913 Debug(LDAP_DEBUG_TRACE,
914 "====> bdb_cache_find_entry_id( %ld ): %ld (not ready) %d\n",
918 ldap_pvt_thread_yield();
922 /* acquire reader lock */
923 rc = bdb_cache_entry_db_lock ( env, locker, ep, rw, 0, lock );
926 if ( bdb_cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
929 if ( rc ) { /* will be changed to retry beyond threshold */
930 /* could not acquire entry lock...
931 * owner cannot free as we have the cache locked.
932 * so, unlock the cache, yield, and try again.
935 /* free cache read lock */
936 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
939 LDAP_LOG( CACHE, INFO,
940 "bdb_cache_find_entry_id: %ld -> %ld (busy) %d.\n",
943 Debug(LDAP_DEBUG_TRACE,
944 "====> bdb_cache_find_entry_id( %ld ): %ld (busy) %d\n",
946 Debug(LDAP_DEBUG_TRACE,
951 ldap_pvt_thread_yield();
955 /* free cache read lock */
956 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
958 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
960 LRU_DELETE( cache, ep );
961 LRU_ADD( cache, ep );
963 BEI(ep)->bei_refcnt++;
966 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
969 LDAP_LOG( CACHE, DETAIL1,
970 "bdb_cache_find_entry_id: %ld -> %s found %d tries.\n",
971 ep_id, ep->e_dn, count );
973 Debug(LDAP_DEBUG_TRACE,
974 "====> bdb_cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
975 ep_id, ep->e_dn, count);
982 /* free cache read lock */
983 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
989 * cache_delete_entry - delete the entry e from the cache. the caller
990 * should have obtained e (increasing its ref count) via a call to one
991 * of the cache_find_* routines. the caller should *not* call the
992 * cache_return_entry() routine prior to calling cache_delete_entry().
993 * it performs this function.
995 * returns: 0 e was deleted ok
996 * 1 e was not in the cache
997 * -1 something bad happened
1000 bdb_cache_delete_entry(
1007 /* set cache write lock */
1008 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1010 assert( e->e_private );
1013 LDAP_LOG( CACHE, ENTRY,
1014 "bdb_cache_delete_entry: delete %ld.\n", e->e_id, 0, 0 );
1016 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete_entry( %ld )\n",
1021 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1022 rc = bdb_cache_delete_entry_internal( cache, e );
1023 /* free lru mutex */
1024 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1026 /* free cache write lock */
1027 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1032 bdb_cache_delete_entry_internal(
1037 int rc = 0; /* return code */
1040 if ( avl_delete( &cache->c_dntree, (caddr_t) e, entry_dn_cmp ) == NULL )
1046 if ( avl_delete( &cache->c_idtree, (caddr_t) e, entry_id_cmp ) == NULL )
1056 LRU_DELETE( cache, e );
1060 * flag entry to be freed later by a call to cache_return_entry()
1062 BEI(e)->bei_state = CACHE_ENTRY_DELETED;
1068 bdb_cache_release_all( Cache *cache )
1073 /* set cache write lock */
1074 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1076 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1079 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
1081 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1084 while ( (e = cache->c_lrutail) != NULL && BEI(e)->bei_refcnt == 0 ) {
1085 #ifdef LDAP_RDWR_DEBUG
1086 assert(!ldap_pvt_thread_rdwr_active(&BEI(e)->bei_rdwr));
1089 /* delete from cache and lru q */
1090 /* XXX do we need rc ? */
1091 rc = bdb_cache_delete_entry_internal( cache, e );
1092 bdb_cache_entry_private_destroy( e );
1093 bdb_entry_return( e );
1096 if ( cache->c_cursize ) {
1098 LDAP_LOG( CACHE, INFO,
1099 "bdb_cache_release_all: Entry cache could not be emptied.\n", 0, 0, 0 );
1101 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
1106 /* free lru mutex */
1107 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1108 /* free cache write lock */
1109 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1114 bdb_lru_print( Cache *cache )
1118 fprintf( stderr, "LRU queue (head to tail):\n" );
1119 for ( e = cache->c_lruhead; e != NULL; e = BEI(e)->bei_lrunext ) {
1120 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
1121 e->e_dn, e->e_id, BEI(e)->bei_refcnt );
1123 fprintf( stderr, "LRU queue (tail to head):\n" );
1124 for ( e = cache->c_lrutail; e != NULL; e = BEI(e)->bei_lruprev ) {
1125 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
1126 e->e_dn, e->e_id, BEI(e)->bei_refcnt );
1131 #ifdef BDB_REUSE_LOCKERS
1133 bdb_locker_id_free( void *key, void *data )
1136 int lockid = (int) data;
1138 XLOCK_ID_FREE( env, lockid );
1142 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
1148 if ( !env || !locker ) return -1;
1150 /* If no op was provided, try to find the ctx anyway... */
1152 ctx = op->o_threadctx;
1154 ctx = ldap_pvt_thread_pool_context( &connection_pool );
1157 /* Shouldn't happen unless we're single-threaded */
1163 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1164 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1165 rc = XLOCK_ID( env, &lockid );
1166 if (rc) ldap_pvt_thread_yield();
1171 data = (void *)lockid;
1172 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1173 data, bdb_locker_id_free ) ) ) {
1174 XLOCK_ID_FREE( env, lockid );
1176 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
1177 db_strerror(rc), rc, 0 );
1179 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1180 db_strerror(rc), rc, 0 );