2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2003-2006 The OpenLDAP Foundation.
5 * Portions Copyright 2003 IBM Corporation.
6 * Portions Copyright 2003 Symas Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Apurva Kumar for inclusion
19 * in OpenLDAP Software and subsequently rewritten by Howard Chu.
24 #ifdef SLAPD_OVER_PROXYCACHE
28 #include <ac/string.h>
37 /* query cache structs */
40 typedef struct Query_s {
41 Filter* filter; /* Search Filter */
42 AttributeName* attrs; /* Projected attributes */
43 AttributeName* save_attrs; /* original attributes, saved for response */
44 struct berval base; /* Search Base */
45 int scope; /* Search scope */
48 struct query_template_s;
50 /* struct representing a cached query */
51 typedef struct cached_query_s {
52 Query query; /* LDAP query */
53 struct berval q_uuid; /* query identifier */
54 struct query_template_s *qtemp; /* template of the query */
55 time_t expiry_time; /* time till the query is considered valid */
56 struct cached_query_s *next; /* next query in the template */
57 struct cached_query_s *prev; /* previous query in the template */
58 struct cached_query_s *lru_up; /* previous query in the LRU list */
59 struct cached_query_s *lru_down; /* next query in the LRU list */
63 * Represents a set of projected attributes.
67 struct query_template_s *templates;
68 AttributeName* attrs; /* specifies the set */
70 #define PC_CONFIGURED (0x1)
71 #define PC_REFERENCED (0x2)
72 #define PC_GOT_OC (0x4)
73 int count; /* number of attributes */
76 /* struct representing a query template
77 * e.g. template string = &(cn=)(mail=)
79 typedef struct query_template_s {
80 struct query_template_s *qtnext;
81 struct query_template_s *qmnext;
83 CachedQuery* query; /* most recent query cached for the template */
84 CachedQuery* query_last; /* oldest query cached for the template */
85 ldap_pvt_thread_rdwr_t *t_rwlock; /* Rd/wr lock for accessing queries in the template */
86 struct berval querystr; /* Filter string corresponding to the QT */
88 int attr_set_index; /* determines the projected attributes */
89 int no_of_queries; /* Total number of queries in the template */
90 time_t ttl; /* TTL for the queries of this template */
91 time_t negttl; /* TTL for negative results */
92 struct attr_set t_attrs; /* filter attrs + attr_set */
95 struct query_manager_s;
97 /* prototypes for functions for 1) query containment
98 * 2) query addition, 3) cache replacement
100 typedef CachedQuery * (QCfunc)(Operation *op, struct query_manager_s*, Query*, QueryTemplate*);
101 typedef void (AddQueryfunc)(struct query_manager_s*, Query*, QueryTemplate*, struct berval*);
102 typedef void (CRfunc)(struct query_manager_s*, struct berval * );
104 /* LDAP query cache */
105 typedef struct query_manager_s {
106 struct attr_set* attr_sets; /* possible sets of projected attributes */
107 QueryTemplate* templates; /* cacheable templates */
109 CachedQuery* lru_top; /* top and bottom of LRU list */
110 CachedQuery* lru_bottom;
112 ldap_pvt_thread_mutex_t lru_mutex; /* mutex for accessing LRU list */
114 /* Query cache methods */
115 QCfunc *qcfunc; /* Query containment*/
116 CRfunc *crfunc; /* cache replacement */
117 AddQueryfunc *addfunc; /* add query */
120 /* LDAP query cache manager */
121 typedef struct cache_manager_s {
122 BackendDB db; /* underlying database */
123 unsigned long num_cached_queries; /* total number of cached queries */
124 unsigned long max_queries; /* upper bound on # of cached queries */
125 int numattrsets; /* number of attribute sets */
126 int cur_entries; /* current number of entries cached */
127 int max_entries; /* max number of entries cached */
128 int num_entries_limit; /* max # of entries in a cacheable query */
130 char response_cb; /* install the response callback
131 * at the tail of the callback list */
132 #define PCACHE_RESPONSE_CB_HEAD 0
133 #define PCACHE_RESPONSE_CB_TAIL 1
135 time_t cc_period; /* interval between successive consistency checks (sec) */
139 ldap_pvt_thread_mutex_t cache_mutex;
141 query_manager* qm; /* query cache managed by the cache manager */
144 static int pcache_debug;
146 static AttributeDescription *ad_queryid;
147 static char *queryid_schema = "( 1.3.6.1.4.1.4203.666.1.12 NAME 'queryid' "
148 "DESC 'list of queries the entry belongs to' "
149 "EQUALITY octetStringMatch "
150 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
151 "NO-USER-MODIFICATION USAGE directoryOperation )";
153 /* Return 1 for an added entry, else 0 */
158 struct berval* query_uuid )
161 Modifications* modlist = NULL;
162 const char* text = NULL;
164 char textbuf[SLAP_TEXT_BUFLEN];
165 size_t textlen = sizeof(textbuf);
167 SlapReply sreply = {REP_RESULT};
169 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
174 /* add queryid attribute */
175 attr_merge_one( e, ad_queryid, query_uuid, NULL );
177 /* append the attribute list from the fetched entry */
178 e->e_attrs->a_next = attr;
180 op->o_tag = LDAP_REQ_ADD;
181 op->o_protocol = LDAP_VERSION3;
182 op->o_callback = &cb;
183 op->o_time = slap_get_time();
184 op->o_do_not_cache = 1;
187 op->o_req_dn = e->e_name;
188 op->o_req_ndn = e->e_nname;
189 rc = op->o_bd->be_add( op, &sreply );
191 if ( rc != LDAP_SUCCESS ) {
192 if ( rc == LDAP_ALREADY_EXISTS ) {
193 slap_entry2mods( e, &modlist, &text, textbuf, textlen );
194 modlist->sml_op = LDAP_MOD_ADD;
195 op->o_tag = LDAP_REQ_MODIFY;
196 op->orm_modlist = modlist;
197 op->o_bd->be_modify( op, &sreply );
198 slap_mods_free( modlist, 1 );
199 } else if ( rc == LDAP_REFERRAL ||
200 rc == LDAP_NO_SUCH_OBJECT ) {
201 syncrepl_add_glue( op, e );
210 be_entry_release_w( op, e );
217 /* compare base and scope of incoming and cached queries */
218 static int base_scope_compare(
219 struct berval* ndn_stored,
220 struct berval* ndn_incoming,
224 struct berval pdn_incoming = BER_BVNULL;
226 if (scope_stored < scope_incoming)
229 if ( !dnIsSuffix(ndn_incoming, ndn_stored))
232 switch(scope_stored) {
233 case LDAP_SCOPE_BASE:
234 return (ndn_incoming->bv_len == ndn_stored->bv_len);
236 case LDAP_SCOPE_ONELEVEL:
237 switch(scope_incoming){
238 case LDAP_SCOPE_BASE:
239 dnParent(ndn_incoming, &pdn_incoming);
240 return (pdn_incoming.bv_len == ndn_stored->bv_len);
242 case LDAP_SCOPE_ONELEVEL:
243 return (ndn_incoming->bv_len == ndn_stored->bv_len);
248 case LDAP_SCOPE_SUBTREE:
257 /* add query on top of LRU list */
259 add_query_on_top (query_manager* qm, CachedQuery* qc)
261 CachedQuery* top = qm->lru_top;
262 Query* q = (Query*)qc;
273 Debug( pcache_debug, "Base of added query = %s\n",
274 q->base.bv_val, 0, 0 );
277 /* remove_query from LRU list */
280 remove_query (query_manager* qm, CachedQuery* qc)
303 qc->lru_up = qc->lru_down = NULL;
306 /* find and remove string2 from string1
307 * from start if position = 1,
308 * from end if position = 3,
309 * from anywhere if position = 2
310 * string1 is overwritten if position = 2.
314 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
325 if ( ber1->bv_len >= ber2->bv_len && !memcmp( ber1->bv_val,
326 ber2->bv_val, ber2->bv_len )) {
328 ber1->bv_val += ber2->bv_len;
329 ber1->bv_len -= ber2->bv_len;
334 ber1->bv_val[ber1->bv_len] = '\0';
335 temp = strstr( ber1->bv_val, ber2->bv_val );
337 strcpy( temp, temp+ber2->bv_len );
338 ber1->bv_len -= ber2->bv_len;
344 if ( ber1->bv_len >= ber2->bv_len &&
345 !memcmp( ber1->bv_val+ber1->bv_len-ber2->bv_len, ber2->bv_val,
348 ber1->bv_len -= ber2->bv_len;
356 static struct berval*
357 merge_init_final(Operation *op, struct berval* init, struct berval* any,
358 struct berval* final)
360 struct berval* merged, *temp;
361 int i, any_count, count;
363 for (any_count=0; any && any[any_count].bv_val; any_count++)
373 merged = (struct berval*)op->o_tmpalloc( (count+1)*sizeof(struct berval),
378 ber_dupbv_x( temp, init, op->o_tmpmemctx );
382 for (i=0; i<any_count; i++) {
383 ber_dupbv_x( temp, any, op->o_tmpmemctx );
388 ber_dupbv_x( temp, final, op->o_tmpmemctx );
395 /* Each element in stored must be found in incoming. Incoming is overwritten.
398 strings_containment(struct berval* stored, struct berval* incoming)
400 struct berval* element;
404 for ( element=stored; element->bv_val != NULL; element++ ) {
405 for (j = k; incoming[j].bv_val != NULL; j++) {
406 if (find_and_remove(&(incoming[j]), element, 2)) {
423 substr_containment_substr(Operation *op, Filter* stored, Filter* incoming)
427 struct berval init_incoming;
428 struct berval final_incoming;
429 struct berval *remaining_incoming = NULL;
431 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
432 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
435 init_incoming = incoming->f_sub_initial;
436 final_incoming = incoming->f_sub_final;
438 if (find_and_remove(&init_incoming,
439 &(stored->f_sub_initial), 1) && find_and_remove(&final_incoming,
440 &(stored->f_sub_final), 3))
442 if (stored->f_sub_any == NULL) {
446 remaining_incoming = merge_init_final(op, &init_incoming,
447 incoming->f_sub_any, &final_incoming);
448 rc = strings_containment(stored->f_sub_any, remaining_incoming);
449 ber_bvarray_free_x( remaining_incoming, op->o_tmpmemctx );
456 substr_containment_equality(Operation *op, Filter* stored, Filter* incoming)
458 struct berval incoming_val[2];
461 incoming_val[1] = incoming->f_av_value;
463 if (find_and_remove(incoming_val+1,
464 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val+1,
465 &(stored->f_sub_final), 3)) {
466 if (stored->f_sub_any == NULL){
470 ber_dupbv_x( incoming_val, incoming_val+1, op->o_tmpmemctx );
471 BER_BVZERO( incoming_val+1 );
472 rc = strings_containment(stored->f_sub_any, incoming_val);
473 op->o_tmpfree( incoming_val[0].bv_val, op->o_tmpmemctx );
479 /* check whether query is contained in any of
480 * the cached queries in template
483 query_containment(Operation *op, query_manager *qm,
485 QueryTemplate *templa)
489 Filter* inputf = query->filter;
490 struct berval* base = &(query->base);
491 int scope = query->scope;
498 MatchingRule* mrule = NULL;
499 if (inputf != NULL) {
500 Debug( pcache_debug, "Lock QC index = %p\n",
502 ldap_pvt_thread_rdwr_rlock(templa->t_rwlock);
503 for(qc=templa->query; qc != NULL; qc= qc->next) {
505 if(base_scope_compare(&(q->base), base, q->scope, scope)) {
510 switch (fs->f_choice) {
511 case LDAP_FILTER_EQUALITY:
512 if (fi->f_choice == LDAP_FILTER_EQUALITY)
513 mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
519 mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
525 rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
526 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
527 &(fi->f_ava->aa_value),
528 &(fs->f_ava->aa_value), &text);
529 if (rc != LDAP_SUCCESS) {
530 ldap_pvt_thread_rdwr_runlock(templa->t_rwlock);
532 "Unlock: Exiting QC index=%p\n",
537 switch (fs->f_choice) {
539 case LDAP_FILTER_AND:
544 case LDAP_FILTER_SUBSTRINGS:
545 /* check if the equality query can be
546 * answered with cached substring query */
547 if ((fi->f_choice == LDAP_FILTER_EQUALITY)
548 && substr_containment_equality( op,
551 /* check if the substring query can be
552 * answered with cached substring query */
553 if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
554 ) && substr_containment_substr( op,
560 case LDAP_FILTER_PRESENT:
565 case LDAP_FILTER_EQUALITY:
572 if (mrule && ret >= 0)
578 if (mrule && ret <= 0)
583 case LDAP_FILTER_NOT:
589 } while((res) && (fi != NULL) && (fs != NULL));
592 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
593 if (qm->lru_top != qc) {
594 remove_query(qm, qc);
595 add_query_on_top(qm, qc);
597 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
603 "Not answerable: Unlock QC index=%p\n",
605 ldap_pvt_thread_rdwr_runlock(templa->t_rwlock);
611 free_query (CachedQuery* qc)
613 Query* q = (Query*)qc;
615 free(qc->q_uuid.bv_val);
616 filter_free(q->filter);
617 free (q->base.bv_val);
623 /* Add query to query cache */
624 static void add_query(
627 QueryTemplate *templ,
630 CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
632 new_cached_query->qtemp = templ;
634 new_cached_query->q_uuid = *uuid;
635 new_cached_query->expiry_time = slap_get_time() + templ->ttl;
637 BER_BVZERO( &new_cached_query->q_uuid );
638 new_cached_query->expiry_time = slap_get_time() + templ->negttl;
640 new_cached_query->lru_up = NULL;
641 new_cached_query->lru_down = NULL;
642 Debug( pcache_debug, "Added query expires at %ld\n",
643 (long) new_cached_query->expiry_time, 0, 0 );
644 new_query = (Query*)new_cached_query;
646 ber_dupbv(&new_query->base, &query->base);
647 new_query->scope = query->scope;
648 new_query->filter = query->filter;
649 new_query->attrs = query->attrs;
652 Debug( pcache_debug, "Lock AQ index = %p\n",
654 ldap_pvt_thread_rdwr_wlock(templ->t_rwlock);
655 if (templ->query == NULL)
656 templ->query_last = new_cached_query;
658 templ->query->prev = new_cached_query;
659 new_cached_query->next = templ->query;
660 new_cached_query->prev = NULL;
661 templ->query = new_cached_query;
662 templ->no_of_queries++;
663 Debug( pcache_debug, "TEMPLATE %p QUERIES++ %d\n",
664 templ, templ->no_of_queries, 0 );
666 Debug( pcache_debug, "Unlock AQ index = %p \n",
668 ldap_pvt_thread_rdwr_wunlock(templ->t_rwlock);
670 /* Adding on top of LRU list */
671 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
672 add_query_on_top(qm, new_cached_query);
673 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
677 remove_from_template (CachedQuery* qc, QueryTemplate* template)
679 if (!qc->prev && !qc->next) {
680 template->query_last = template->query = NULL;
681 } else if (qc->prev == NULL) {
682 qc->next->prev = NULL;
683 template->query = qc->next;
684 } else if (qc->next == NULL) {
685 qc->prev->next = NULL;
686 template->query_last = qc->prev;
688 qc->next->prev = qc->prev;
689 qc->prev->next = qc->next;
692 template->no_of_queries--;
695 /* remove bottom query of LRU list from the query cache */
696 static void cache_replacement(query_manager* qm, struct berval *result)
701 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
702 bottom = qm->lru_bottom;
704 result->bv_val = NULL;
708 Debug ( pcache_debug,
709 "Cache replacement invoked without "
710 "any query in LRU list\n", 0, 0, 0 );
711 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
715 temp = bottom->qtemp;
716 remove_query(qm, bottom);
717 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
719 *result = bottom->q_uuid;
720 bottom->q_uuid.bv_val = NULL;
722 Debug( pcache_debug, "Lock CR index = %p\n", temp, 0, 0 );
723 ldap_pvt_thread_rdwr_wlock(temp->t_rwlock);
724 remove_from_template(bottom, temp);
725 Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
726 temp, temp->no_of_queries, 0 );
727 Debug( pcache_debug, "Unlock CR index = %p\n", temp, 0, 0 );
728 ldap_pvt_thread_rdwr_wunlock(temp->t_rwlock);
733 struct query_info *next;
745 struct query_info *qi;
748 if ( rs->sr_type != REP_SEARCH ) return 0;
750 for (attr = rs->sr_entry->e_attrs; attr!= NULL; attr = attr->a_next) {
751 if (attr->a_desc == ad_queryid) {
752 for (count=0; attr->a_vals[count].bv_val; count++)
757 if ( count == 0 ) return 0;
758 qi = op->o_tmpalloc( sizeof( struct query_info ), op->o_tmpmemctx );
759 qi->next = op->o_callback->sc_private;
760 op->o_callback->sc_private = qi;
761 ber_dupbv_x( &qi->xdn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
762 qi->del = ( count == 1 );
771 struct berval* query_uuid)
773 struct query_info *qi, *qnext;
775 #ifdef LDAP_COMP_MATCH
776 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
778 AttributeAssertion ava = { NULL, BER_BVNULL };
780 Filter filter = {LDAP_FILTER_EQUALITY};
781 SlapReply sreply = {REP_RESULT};
782 slap_callback cb = { NULL, remove_func, NULL, NULL };
785 sreply.sr_entry = NULL;
786 sreply.sr_nentries = 0;
787 op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
788 "(%s=%s)", ad_queryid->ad_cname.bv_val, query_uuid->bv_val);
790 filter.f_av_desc = ad_queryid;
791 filter.f_av_value = *query_uuid;
793 op->o_tag = LDAP_REQ_SEARCH;
794 op->o_protocol = LDAP_VERSION3;
795 op->o_callback = &cb;
796 op->o_time = slap_get_time();
797 op->o_do_not_cache = 1;
799 op->o_req_dn = op->o_bd->be_suffix[0];
800 op->o_req_ndn = op->o_bd->be_nsuffix[0];
801 op->ors_scope = LDAP_SCOPE_SUBTREE;
802 op->ors_deref = LDAP_DEREF_NEVER;
803 op->ors_slimit = SLAP_NO_LIMIT;
804 op->ors_tlimit = SLAP_NO_LIMIT;
805 op->ors_filter = &filter;
806 op->ors_filterstr.bv_val = filter_str;
807 op->ors_filterstr.bv_len = strlen(filter_str);
808 op->ors_attrs = NULL;
809 op->ors_attrsonly = 0;
811 op->o_bd->be_search( op, &sreply );
813 for ( qi=cb.sc_private; qi; qi=qnext ) {
816 op->o_req_dn = qi->xdn;
817 op->o_req_ndn = qi->xdn;
820 Debug( pcache_debug, "DELETING ENTRY TEMPLATE=%s\n",
821 query_uuid->bv_val, 0, 0 );
823 op->o_tag = LDAP_REQ_DELETE;
825 if (op->o_bd->be_delete(op, &sreply) == LDAP_SUCCESS) {
830 struct berval vals[2];
832 vals[0] = *query_uuid;
833 vals[1].bv_val = NULL;
835 mod.sml_op = LDAP_MOD_DELETE;
837 mod.sml_desc = ad_queryid;
838 mod.sml_type = ad_queryid->ad_cname;
839 mod.sml_values = vals;
840 mod.sml_nvalues = NULL;
843 "REMOVING TEMP ATTR : TEMPLATE=%s\n",
844 query_uuid->bv_val, 0, 0 );
846 op->orm_modlist = &mod;
848 op->o_bd->be_modify( op, &sreply );
850 op->o_tmpfree( qi->xdn.bv_val, op->o_tmpmemctx );
851 op->o_tmpfree( qi, op->o_tmpmemctx );
858 AttributeName* attrs,
868 AttributeName** filter_attrs,
872 AttributeDescription *ad;
874 switch ( f->f_choice ) {
875 case LDAP_FILTER_EQUALITY:
877 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
878 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
883 sprintf( fstr->bv_val+fstr->bv_len, "(%s>=)", ad->ad_cname.bv_val);
884 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(>=)") - 1 );
889 sprintf( fstr->bv_val+fstr->bv_len, "(%s<=)", ad->ad_cname.bv_val);
890 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(<=)") - 1 );
893 case LDAP_FILTER_APPROX:
895 sprintf( fstr->bv_val+fstr->bv_len, "(%s~=)", ad->ad_cname.bv_val);
896 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(~=)") - 1 );
899 case LDAP_FILTER_SUBSTRINGS:
901 sprintf( fstr->bv_val+fstr->bv_len, "(%s=)", ad->ad_cname.bv_val );
902 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=)") - 1 );
905 case LDAP_FILTER_PRESENT:
907 sprintf( fstr->bv_val+fstr->bv_len, "(%s=*)", ad->ad_cname.bv_val );
908 fstr->bv_len += ad->ad_cname.bv_len + ( sizeof("(=*)") - 1 );
911 case LDAP_FILTER_AND:
913 case LDAP_FILTER_NOT: {
915 sprintf( fstr->bv_val+fstr->bv_len, "(%c",
916 f->f_choice == LDAP_FILTER_AND ? '&' :
917 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
918 fstr->bv_len += sizeof("(%") - 1;
920 for ( f = f->f_list; f != NULL; f = f->f_next ) {
921 rc = filter2template( op, f, fstr, filter_attrs, filter_cnt,
925 sprintf( fstr->bv_val+fstr->bv_len, ")" );
926 fstr->bv_len += sizeof(")") - 1;
932 strcpy( fstr->bv_val, "(?=?)" );
933 fstr->bv_len += sizeof("(?=?)")-1;
937 *filter_attrs = (AttributeName *)op->o_tmprealloc(*filter_attrs,
938 (*filter_cnt + 2)*sizeof(AttributeName), op->o_tmpmemctx);
940 (*filter_attrs)[*filter_cnt].an_desc = ad;
941 (*filter_attrs)[*filter_cnt].an_name = ad->ad_cname;
942 (*filter_attrs)[*filter_cnt].an_oc = NULL;
943 (*filter_attrs)[*filter_cnt].an_oc_exclude = 0;
944 BER_BVZERO( &(*filter_attrs)[*filter_cnt+1].an_name );
946 if ( ad == slap_schema.si_ad_objectClass )
954 QueryTemplate *qtemp;
965 struct berval *query_uuid)
967 struct search_info *si = op->o_callback->sc_private;
968 slap_overinst *on = si->on;
969 cache_manager *cm = on->on_bi.bi_private;
970 query_manager* qm = cm->qm;
973 struct berval crp_uuid;
974 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
975 Operation op_tmp = *op;
977 query_uuid->bv_len = lutil_uuidstr(uuidbuf, sizeof(uuidbuf));
978 ber_str2bv(uuidbuf, query_uuid->bv_len, 1, query_uuid);
980 op_tmp.o_bd = &cm->db;
981 op_tmp.o_dn = cm->db.be_rootdn;
982 op_tmp.o_ndn = cm->db.be_rootndn;
984 Debug( pcache_debug, "UUID for query being added = %s\n",
987 for ( e=si->head; e; e=si->head ) {
988 si->head = e->e_private;
990 while ( cm->cur_entries > (cm->max_entries) ) {
991 qm->crfunc(qm, &crp_uuid);
992 if (crp_uuid.bv_val) {
994 "Removing query UUID %s\n",
995 crp_uuid.bv_val, 0, 0 );
996 return_val = remove_query_data(&op_tmp, rs, &crp_uuid);
998 "QUERY REMOVED, SIZE=%d\n",
1000 ldap_pvt_thread_mutex_lock(
1002 cm->cur_entries -= return_val;
1003 cm->num_cached_queries--;
1004 Debug( pcache_debug,
1005 "STORED QUERIES = %lu\n",
1006 cm->num_cached_queries, 0, 0 );
1007 ldap_pvt_thread_mutex_unlock(
1009 Debug( pcache_debug,
1010 "QUERY REMOVED, CACHE ="
1012 cm->cur_entries, 0, 0 );
1016 return_val = merge_entry(&op_tmp, e, query_uuid);
1017 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1018 cm->cur_entries += return_val;
1019 Debug( pcache_debug,
1020 "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
1021 cm->cur_entries, 0, 0 );
1023 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1034 struct search_info *si = op->o_callback->sc_private;
1035 slap_overinst *on = si->on;
1036 cache_manager *cm = on->on_bi.bi_private;
1037 query_manager* qm = cm->qm;
1040 if ( si->query.save_attrs != NULL ) {
1041 rs->sr_attrs = si->query.save_attrs;
1042 op->ors_attrs = si->query.save_attrs;
1043 si->query.save_attrs = NULL;
1046 if ( rs->sr_type == REP_SEARCH ) {
1048 /* If we haven't exceeded the limit for this query,
1049 * build a chain of answers to store. If we hit the
1050 * limit, empty the chain and ignore the rest.
1053 if ( si->count < si->max ) {
1055 e = entry_dup( rs->sr_entry );
1056 if ( !si->head ) si->head = e;
1057 if ( si->tail ) si->tail->e_private = e;
1062 for (;si->head; si->head=e) {
1063 e = si->head->e_private;
1064 si->head->e_private = NULL;
1065 entry_free(si->head);
1071 } else if ( rs->sr_type == REP_RESULT ) {
1072 QueryTemplate* templ = si->qtemp;
1073 if (( si->count && cache_entries( op, rs, &uuid ) == 0 ) ||
1074 ( templ->negttl && !si->count && !si->over &&
1075 rs->sr_err == LDAP_SUCCESS )) {
1076 qm->addfunc(qm, &si->query, si->qtemp,
1077 si->count ? &uuid : NULL);
1079 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1080 cm->num_cached_queries++;
1081 Debug( pcache_debug, "STORED QUERIES = %lu\n",
1082 cm->num_cached_queries, 0, 0 );
1083 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1085 /* If the consistency checker suspended itself,
1088 if ( cm->cc_paused ) {
1089 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1090 if ( cm->cc_paused ) {
1092 ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
1094 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1097 free( si->query.attrs );
1098 filter_free( si->query.filter );
1102 op->o_callback->sc_cleanup = slap_freeself_cb;
1104 return SLAP_CB_CONTINUE;
1110 AttributeName** new_attrs,
1111 struct attr_set *attrs,
1112 AttributeName* filter_attrs,
1122 /* duplicate attrs */
1123 count = attrs->count + fattr_cnt;
1124 if ( !fattr_got_oc && !(attrs->flags & PC_GOT_OC)) {
1129 *new_attrs = (AttributeName*)ch_malloc((count+1)*
1130 sizeof(AttributeName));
1131 for (i=0; i<attrs->count; i++) {
1132 (*new_attrs)[i].an_name = attrs->attrs[i].an_name;
1133 (*new_attrs)[i].an_desc = attrs->attrs[i].an_desc;
1135 BER_BVZERO( &(*new_attrs)[i].an_name );
1136 alluser = an_find(*new_attrs, &AllUser);
1137 allop = an_find(*new_attrs, &AllOper);
1140 for ( i=0; i<fattr_cnt; i++ ) {
1141 if ( an_find(*new_attrs, &filter_attrs[i].an_name ))
1143 if ( is_at_operational(filter_attrs[i].an_desc->ad_type) ) {
1148 (*new_attrs)[j].an_name = filter_attrs[i].an_name;
1149 (*new_attrs)[j].an_desc = filter_attrs[i].an_desc;
1150 (*new_attrs)[j].an_oc = NULL;
1151 (*new_attrs)[j].an_oc_exclude = 0;
1155 (*new_attrs)[j].an_name = slap_schema.si_ad_objectClass->ad_cname;
1156 (*new_attrs)[j].an_desc = slap_schema.si_ad_objectClass;
1157 (*new_attrs)[j].an_oc = NULL;
1158 (*new_attrs)[j].an_oc_exclude = 0;
1161 BER_BVZERO( &(*new_attrs)[j].an_name );
1164 /* NOTE: this is a quick workaround to let pcache minimally interact
1165 * with pagedResults. A more articulated solutions would be to
1166 * perform the remote query without control and cache all results,
1167 * performing the pagedResults search only within the client
1168 * and the proxy. This requires pcache to understand pagedResults. */
1170 pcache_chk_controls(
1174 const char *non = "";
1175 const char *stripped = "";
1177 switch( op->o_pagedresults ) {
1178 case SLAP_CONTROL_NONCRITICAL:
1180 stripped = "; stripped";
1183 case SLAP_CONTROL_CRITICAL:
1184 Debug( pcache_debug, "%s: "
1185 "%scritical pagedResults control "
1186 "disabled with proxy cache%s.\n",
1187 op->o_log_prefix, non, stripped );
1189 slap_remove_control( op, rs, slap_cids.sc_pagedResults, NULL );
1193 rs->sr_err = SLAP_CB_CONTINUE;
1205 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1206 cache_manager *cm = on->on_bi.bi_private;
1207 query_manager* qm = cm->qm;
1211 AttributeName *filter_attrs = NULL;
1214 QueryTemplate *qtemp = NULL;
1217 CachedQuery *answerable = NULL;
1220 int fattr_got_oc = 0;
1222 struct berval tempstr;
1224 tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1, op->o_tmpmemctx );
1226 if ( filter2template( op, op->ors_filter, &tempstr, &filter_attrs,
1227 &fattr_cnt, &fattr_got_oc )) {
1228 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1229 return SLAP_CB_CONTINUE;
1232 Debug( pcache_debug, "query template of incoming query = %s\n",
1233 tempstr.bv_val, 0, 0 );
1235 /* FIXME: cannot cache/answer requests with pagedResults control */
1239 attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
1241 query.filter = op->ors_filter;
1242 query.attrs = op->ors_attrs;
1243 query.save_attrs = NULL;
1244 query.base = op->o_req_ndn;
1245 query.scope = op->ors_scope;
1247 /* check for query containment */
1248 if (attr_set > -1) {
1249 QueryTemplate *qt = qm->attr_sets[attr_set].templates;
1250 for (; qt; qt = qt->qtnext ) {
1251 /* find if template i can potentially answer tempstr */
1252 if (qt->querystr.bv_len != tempstr.bv_len ||
1253 strcasecmp( qt->querystr.bv_val, tempstr.bv_val ))
1257 Debug( LDAP_DEBUG_NONE, "Entering QC, querystr = %s\n",
1258 op->ors_filterstr.bv_val, 0, 0 );
1259 answerable = (*(qm->qcfunc))(op, qm, &query, qt);
1265 op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
1267 query.save_attrs = op->ors_attrs;
1271 /* Need to clear the callbacks of the original operation,
1272 * in case there are other overlays */
1273 BackendDB *save_bd = op->o_bd;
1274 slap_callback *save_cb = op->o_callback;
1276 Debug( pcache_debug, "QUERY ANSWERABLE\n", 0, 0, 0 );
1277 op->o_tmpfree( filter_attrs, op->o_tmpmemctx );
1278 ldap_pvt_thread_rdwr_runlock(qtemp->t_rwlock);
1279 if ( BER_BVISNULL( &answerable->q_uuid )) {
1280 /* No entries cached, just an empty result set */
1282 send_ldap_result( op, rs );
1285 op->o_callback = NULL;
1286 i = cm->db.bd_info->bi_op_search( op, rs );
1289 op->o_callback = save_cb;
1293 Debug( pcache_debug, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
1295 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1296 if (cm->num_cached_queries >= cm->max_queries) {
1299 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1301 if (op->ors_attrsonly)
1306 struct search_info *si;
1308 Debug( pcache_debug, "QUERY CACHEABLE\n", 0, 0, 0 );
1309 query.filter = filter_dup(op->ors_filter, NULL);
1310 add_filter_attrs(op, &query.attrs, &qm->attr_sets[attr_set],
1311 filter_attrs, fattr_cnt, fattr_got_oc);
1313 op->ors_attrs = query.attrs;
1315 cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx);
1316 cb->sc_response = pcache_response;
1317 cb->sc_cleanup = NULL;
1318 cb->sc_private = (cb+1);
1319 si = cb->sc_private;
1323 si->max = cm->num_entries_limit ;
1329 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
1330 cb->sc_next = op->o_callback;
1331 op->o_callback = cb;
1334 slap_callback **pcb;
1336 /* need to move the callback at the end, in case other
1337 * overlays are present, so that the final entry is
1338 * actually cached */
1340 for ( pcb = &op->o_callback; *pcb; pcb = &(*pcb)->sc_next );
1345 Debug( pcache_debug, "QUERY NOT CACHEABLE\n",
1349 op->o_tmpfree( filter_attrs, op->o_tmpmemctx );
1351 return SLAP_CB_CONTINUE;
1356 AttributeName* attrs,
1364 for ( ; attrs[count].an_name.bv_val; count++ );
1367 for (i=0; i<num; i++) {
1371 if ( count > qm->attr_sets[i].count )
1374 if ( !qm->attr_sets[i].count )
1378 for ( a2 = attrs; a2->an_name.bv_val; a2++ ) {
1379 if ( !an_find( qm->attr_sets[i].attrs, &a2->an_name )) {
1397 struct re_s *rtask = arg;
1398 slap_overinst *on = rtask->arg;
1399 cache_manager *cm = on->on_bi.bi_private;
1400 query_manager *qm = cm->qm;
1401 Connection conn = {0};
1402 OperationBuffer opbuf;
1405 SlapReply rs = {REP_RESULT};
1406 CachedQuery* query, *query_prev;
1407 int i, return_val, pause = 1;
1408 QueryTemplate* templ;
1410 op = (Operation *) &opbuf;
1411 connection_fake_init( &conn, op, ctx );
1414 op->o_dn = cm->db.be_rootdn;
1415 op->o_ndn = cm->db.be_rootndn;
1419 for (templ = qm->templates; templ; templ=templ->qmnext) {
1420 query = templ->query_last;
1421 if ( query ) pause = 0;
1422 op->o_time = slap_get_time();
1423 while (query && (query->expiry_time < op->o_time)) {
1424 Debug( pcache_debug, "Lock CR index = %d\n",
1426 ldap_pvt_thread_rdwr_wlock(templ->t_rwlock);
1427 remove_from_template(query, templ);
1428 Debug( pcache_debug, "TEMPLATE %d QUERIES-- %d\n",
1429 i, templ->no_of_queries, 0 );
1430 Debug( pcache_debug, "Unlock CR index = %d\n",
1432 ldap_pvt_thread_rdwr_wunlock(templ->t_rwlock);
1433 ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
1434 remove_query(qm, query);
1435 ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
1436 if ( BER_BVISNULL( &query->q_uuid ))
1439 return_val = remove_query_data(op, &rs, &query->q_uuid);
1440 Debug( pcache_debug, "STALE QUERY REMOVED, SIZE=%d\n",
1442 ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
1443 cm->cur_entries -= return_val;
1444 cm->num_cached_queries--;
1445 Debug( pcache_debug, "STORED QUERIES = %lu\n",
1446 cm->num_cached_queries, 0, 0 );
1447 ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
1448 Debug( pcache_debug,
1449 "STALE QUERY REMOVED, CACHE ="
1451 cm->cur_entries, 0, 0 );
1453 query = query->prev;
1454 free_query(query_prev);
1457 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1458 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1459 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1461 /* If there were no queries, defer processing for a while */
1462 cm->cc_paused = pause;
1463 ldap_pvt_runqueue_resched( &slapd_rq, rtask, pause );
1465 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1470 #define MAX_ATTR_SETS 500
1480 static ConfigDriver pc_cf_gen;
1481 static ConfigLDAPadd pc_ldadd;
1482 static ConfigCfAdd pc_cfadd;
1484 static ConfigTable pccfg[] = {
1485 { "proxycache", "backend> <max_entries> <numattrsets> <entry limit> "
1487 6, 6, 0, ARG_MAGIC|ARG_NO_DELETE|PC_MAIN, pc_cf_gen,
1488 "( OLcfgOvAt:2.1 NAME 'olcProxyCache' "
1489 "DESC 'ProxyCache basic parameters' "
1490 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
1491 { "proxyattrset", "index> <attributes...",
1492 2, 0, 0, ARG_MAGIC|PC_ATTR, pc_cf_gen,
1493 "( OLcfgOvAt:2.2 NAME 'olcProxyAttrset' "
1494 "DESC 'A set of attributes to cache' "
1495 "SYNTAX OMsDirectoryString )", NULL, NULL },
1496 { "proxytemplate", "filter> <attrset-index> <TTL> <negTTL",
1497 4, 5, 0, ARG_MAGIC|PC_TEMP, pc_cf_gen,
1498 "( OLcfgOvAt:2.3 NAME 'olcProxyTemplate' "
1499 "DESC 'Filter template, attrset, cache TTL, optional negative TTL' "
1500 "SYNTAX OMsDirectoryString )", NULL, NULL },
1501 { "response-callback", "head|tail(default)",
1502 2, 2, 0, ARG_MAGIC|PC_RESP, pc_cf_gen,
1503 "( OLcfgOvAt:2.4 NAME 'olcProxyResponseCB' "
1504 "DESC 'Response callback position in overlay stack' "
1505 "SYNTAX OMsDirectoryString )", NULL, NULL },
1506 { "proxyCacheQueries", "queries",
1507 2, 2, 0, ARG_INT|ARG_MAGIC|PC_QUERIES, pc_cf_gen,
1508 "( OLcfgOvAt:2.5 NAME 'olcProxyCacheQueries' "
1509 "DESC 'Maximum number of queries to cache' "
1510 "SYNTAX OMsInteger )", NULL, NULL },
1512 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1515 static ConfigOCs pcocs[] = {
1516 { "( OLcfgOvOc:2.1 "
1517 "NAME 'olcPcacheConfig' "
1518 "DESC 'ProxyCache configuration' "
1519 "SUP olcOverlayConfig "
1520 "MUST ( olcProxyCache $ olcProxyAttrset $ olcProxyTemplate ) "
1521 "MAY ( olcProxyResponseCB $ olcProxyCacheQueries ) )", Cft_Overlay, pccfg, NULL, pc_cfadd },
1522 { "( OLcfgOvOc:2.2 "
1523 "NAME 'olcPcacheDatabase' "
1524 "DESC 'Cache database configuration' "
1525 "AUXILIARY )", Cft_Misc, pccfg, pc_ldadd },
1530 pc_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
1535 if ( p->ce_type != Cft_Overlay || !p->ce_bi ||
1536 p->ce_bi->bi_cf_ocs != pcocs )
1537 return LDAP_CONSTRAINT_VIOLATION;
1539 on = (slap_overinst *)p->ce_bi;
1540 cm = on->on_bi.bi_private;
1542 return LDAP_SUCCESS;
1546 pc_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
1548 CfEntryInfo *pe = p->e_private;
1549 slap_overinst *on = (slap_overinst *)pe->ce_bi;
1550 cache_manager *cm = on->on_bi.bi_private;
1553 /* FIXME: should not hardcode "olcDatabase" here */
1554 bv.bv_len = sprintf( ca->msg, "olcDatabase=%s", cm->db.bd_info->bi_type );
1555 bv.bv_val = ca->msg;
1558 /* We can only create this entry if the database is table-driven
1560 if ( cm->db.bd_info->bi_cf_ocs )
1561 config_build_entry( op, rs, pe, ca, &bv, cm->db.bd_info->bi_cf_ocs,
1568 pc_cf_gen( ConfigArgs *c )
1570 slap_overinst *on = (slap_overinst *)c->bi;
1571 cache_manager* cm = on->on_bi.bi_private;
1572 query_manager* qm = cm->qm;
1573 QueryTemplate* temp;
1574 AttributeName* attr_name;
1575 AttributeName* attrarray;
1576 const char* text=NULL;
1581 if ( c->op == SLAP_CONFIG_EMIT ) {
1585 bv.bv_len = snprintf( c->msg, sizeof( c->msg ), "%s %d %d %d %ld",
1586 cm->db.bd_info->bi_type, cm->max_entries, cm->numattrsets,
1587 cm->num_entries_limit, cm->cc_period );
1589 value_add_one( &c->rvalue_vals, &bv );
1592 for (i=0; i<cm->numattrsets; i++) {
1593 if ( !qm->attr_sets[i].count ) continue;
1595 bv.bv_len = snprintf( c->msg, sizeof( c->msg ), "%d", i );
1597 /* count the attr length */
1598 for ( attr_name = qm->attr_sets[i].attrs;
1599 attr_name->an_name.bv_val; attr_name++ )
1600 bv.bv_len += attr_name->an_name.bv_len + 1;
1602 bv.bv_val = ch_malloc( bv.bv_len+1 );
1603 ptr = lutil_strcopy( bv.bv_val, c->msg );
1604 for ( attr_name = qm->attr_sets[i].attrs;
1605 attr_name->an_name.bv_val; attr_name++ ) {
1607 ptr = lutil_strcopy( ptr, attr_name->an_name.bv_val );
1609 ber_bvarray_add( &c->rvalue_vals, &bv );
1611 if ( !c->rvalue_vals )
1615 for (temp=qm->templates; temp; temp=temp->qmnext) {
1616 if ( temp->negttl ) {
1617 bv.bv_len = snprintf( c->msg, sizeof( c->msg ),
1619 temp->attr_set_index,
1623 bv.bv_len = snprintf( c->msg, sizeof( c->msg ), " %d %ld",
1624 temp->attr_set_index,
1627 bv.bv_len += temp->querystr.bv_len + 2;
1628 bv.bv_val = ch_malloc( bv.bv_len+1 );
1631 ptr = lutil_strcopy( ptr, temp->querystr.bv_val );
1633 strcpy( ptr, c->msg );
1634 ber_bvarray_add( &c->rvalue_vals, &bv );
1636 if ( !c->rvalue_vals )
1640 if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
1641 BER_BVSTR( &bv, "head" );
1643 BER_BVSTR( &bv, "tail" );
1645 value_add_one( &c->rvalue_vals, &bv );
1648 c->value_int = cm->max_queries;
1652 } else if ( c->op == LDAP_MOD_DELETE ) {
1653 return 1; /* FIXME */
1665 if ( cm->numattrsets > 0 ) {
1666 snprintf( c->msg, sizeof( c->msg ), "\"proxycache\" directive already provided" );
1667 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1671 if ( lutil_atoi( &cm->numattrsets, c->argv[3] ) != 0 ) {
1672 snprintf( c->msg, sizeof( c->msg ), "unable to parse num attrsets=\"%s\" (arg #3)",
1674 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1677 if ( cm->numattrsets <= 0 ) {
1678 snprintf( c->msg, sizeof( c->msg ), "numattrsets (arg #3) must be positive" );
1679 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1682 if ( cm->numattrsets > MAX_ATTR_SETS ) {
1683 snprintf( c->msg, sizeof( c->msg ), "numattrsets (arg #3) must be <= %d", MAX_ATTR_SETS );
1684 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1688 if ( !backend_db_init( c->argv[1], &cm->db )) {
1689 snprintf( c->msg, sizeof( c->msg ), "unknown backend type (arg #1)" );
1690 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1694 if ( lutil_atoi( &cm->max_entries, c->argv[2] ) != 0 ) {
1695 snprintf( c->msg, sizeof( c->msg ), "unable to parse max entries=\"%s\" (arg #2)",
1697 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1700 if ( cm->max_entries <= 0 ) {
1701 snprintf( c->msg, sizeof( c->msg ), "max entries (arg #2) must be positive.\n" );
1702 Debug( LDAP_DEBUG_CONFIG, "%s: %s\n", c->log, c->msg, 0 );
1706 if ( lutil_atoi( &cm->num_entries_limit, c->argv[4] ) != 0 ) {
1707 snprintf( c->msg, sizeof( c->msg ), "unable to parse entry limit=\"%s\" (arg #4)",
1709 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1712 if ( cm->num_entries_limit <= 0 ) {
1713 snprintf( c->msg, sizeof( c->msg ), "entry limit (arg #4) must be positive" );
1714 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1717 if ( cm->num_entries_limit > cm->max_entries ) {
1718 snprintf( c->msg, sizeof( c->msg ), "entry limit (arg #4) must be less than max entries %d (arg #2)", cm->max_entries );
1719 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1723 if ( lutil_parse_time( c->argv[5], &t ) != 0 ) {
1724 snprintf( c->msg, sizeof( c->msg ), "unable to parse period=\"%s\" (arg #5)",
1726 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1729 cm->cc_period = (time_t)t;
1730 Debug( pcache_debug,
1731 "Total # of attribute sets to be cached = %d.\n",
1732 cm->numattrsets, 0, 0 );
1733 qm->attr_sets = ( struct attr_set * )ch_calloc( cm->numattrsets,
1734 sizeof( struct attr_set ) );
1737 if ( cm->numattrsets == 0 ) {
1738 snprintf( c->msg, sizeof( c->msg ), "\"proxycache\" directive not provided yet" );
1739 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1742 if ( lutil_atoi( &num, c->argv[1] ) != 0 ) {
1743 snprintf( c->msg, sizeof( c->msg ), "unable to parse attrset #=\"%s\"",
1745 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1749 if ( num < 0 || num >= cm->numattrsets ) {
1750 snprintf( c->msg, sizeof( c->msg ), "attrset index %d out of bounds (must be %s%d)",
1751 num, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
1752 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1755 qm->attr_sets[num].flags |= PC_CONFIGURED;
1756 if ( c->argc > 2 && strcmp( c->argv[2], "*" ) ) {
1757 qm->attr_sets[num].count = c->argc - 2;
1758 qm->attr_sets[num].attrs = (AttributeName*)ch_malloc(
1759 (c->argc-1) * sizeof( AttributeName ));
1760 attr_name = qm->attr_sets[num].attrs;
1761 for ( i = 2; i < c->argc; i++ ) {
1762 attr_name->an_desc = NULL;
1763 if ( slap_str2ad( c->argv[i],
1764 &attr_name->an_desc, &text ) )
1766 strcpy( c->msg, text );
1767 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1768 ch_free( qm->attr_sets[num].attrs );
1769 qm->attr_sets[num].attrs = NULL;
1770 qm->attr_sets[num].count = 0;
1773 attr_name->an_name = attr_name->an_desc->ad_cname;
1774 attr_name->an_oc = NULL;
1775 attr_name->an_oc_exclude = 0;
1776 if ( attr_name->an_desc == slap_schema.si_ad_objectClass )
1777 qm->attr_sets[num].flags |= PC_GOT_OC;
1779 BER_BVZERO( &attr_name->an_name );
1784 if ( cm->numattrsets == 0 ) {
1785 snprintf( c->msg, sizeof( c->msg ), "\"proxycache\" directive not provided yet" );
1786 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1789 if ( lutil_atoi( &i, c->argv[2] ) != 0 ) {
1790 snprintf( c->msg, sizeof( c->msg ), "unable to parse template #=\"%s\"",
1792 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1796 if ( i < 0 || i >= cm->numattrsets ||
1797 !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
1798 snprintf( c->msg, sizeof( c->msg ), "template index %d invalid (%s%d)",
1799 i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
1800 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1803 temp = ch_calloc( 1, sizeof( QueryTemplate ));
1804 temp->qmnext = qm->templates;
1805 qm->templates = temp;
1806 temp->t_rwlock = ch_malloc( sizeof( ldap_pvt_thread_rdwr_t ) );
1807 ldap_pvt_thread_rdwr_init( temp->t_rwlock );
1808 temp->query = temp->query_last = NULL;
1809 if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
1810 snprintf( c->msg, sizeof( c->msg ), "unable to parse template ttl=\"%s\"",
1812 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1815 temp->ttl = (time_t)t;
1816 if ( c->argc == 5 ) {
1817 if ( lutil_parse_time( c->argv[4], &t ) != 0 ) {
1818 snprintf( c->msg, sizeof( c->msg ),
1819 "unable to parse template negttl=\"%s\"",
1821 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1824 temp->negttl = (time_t)t;
1829 temp->no_of_queries = 0;
1831 ber_str2bv( c->argv[1], 0, 1, &temp->querystr );
1832 Debug( pcache_debug, "Template:\n", 0, 0, 0 );
1833 Debug( pcache_debug, " query template: %s\n",
1834 temp->querystr.bv_val, 0, 0 );
1835 temp->attr_set_index = i;
1836 qm->attr_sets[i].flags |= PC_REFERENCED;
1837 temp->qtnext = qm->attr_sets[i].templates;
1838 qm->attr_sets[i].templates = temp;
1839 Debug( pcache_debug, " attributes: \n", 0, 0, 0 );
1840 if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
1841 for ( i=0; attrarray[i].an_name.bv_val; i++ )
1842 Debug( pcache_debug, "\t%s\n",
1843 attrarray[i].an_name.bv_val, 0, 0 );
1846 temp->querystr.bv_val = NULL;
1849 if ( strcasecmp( c->argv[1], "head" ) == 0 ) {
1850 cm->response_cb = PCACHE_RESPONSE_CB_HEAD;
1852 } else if ( strcasecmp( c->argv[1], "tail" ) == 0 ) {
1853 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
1856 snprintf( c->msg, sizeof( c->msg ), "unknown specifier" );
1857 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1862 if ( c->value_int <= 0 ) {
1863 snprintf( c->msg, sizeof( c->msg ), "max queries must be positive" );
1864 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
1867 cm->max_queries = c->value_int;
1882 slap_overinst *on = (slap_overinst *)be->bd_info;
1883 cache_manager* cm = on->on_bi.bi_private;
1885 /* Something for the cache database? */
1886 if ( cm->db.bd_info && cm->db.bd_info->bi_db_config )
1887 return cm->db.bd_info->bi_db_config( &cm->db, fname, lineno,
1889 return SLAP_CONF_UNKNOWN;
1897 slap_overinst *on = (slap_overinst *)be->bd_info;
1901 cm = (cache_manager *)ch_malloc(sizeof(cache_manager));
1902 on->on_bi.bi_private = cm;
1904 qm = (query_manager*)ch_malloc(sizeof(query_manager));
1907 SLAP_DBFLAGS(&cm->db) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1908 cm->db.be_private = NULL;
1909 cm->db.be_pcl_mutexp = &cm->db.be_pcl_mutex;
1911 cm->numattrsets = 0;
1912 cm->num_entries_limit = 5;
1913 cm->num_cached_queries = 0;
1914 cm->max_entries = 0;
1915 cm->cur_entries = 0;
1916 cm->max_queries = 10000;
1917 cm->response_cb = PCACHE_RESPONSE_CB_TAIL;
1918 cm->cc_period = 1000;
1921 qm->attr_sets = NULL;
1922 qm->templates = NULL;
1924 qm->lru_bottom = NULL;
1926 qm->qcfunc = query_containment;
1927 qm->crfunc = cache_replacement;
1928 qm->addfunc = add_query;
1929 ldap_pvt_thread_mutex_init(&qm->lru_mutex);
1931 ldap_pvt_thread_mutex_init(&cm->cache_mutex);
1940 slap_overinst *on = (slap_overinst *)be->bd_info;
1941 cache_manager *cm = on->on_bi.bi_private;
1942 query_manager* qm = cm->qm;
1943 int i, ncf = 0, rf = 0, nrf = 0, rc = 0;
1945 /* check attr sets */
1946 for ( i = 0; i < cm->numattrsets; i++) {
1947 if ( !( qm->attr_sets[i].flags & PC_CONFIGURED ) ) {
1948 if ( qm->attr_sets[i].flags & PC_REFERENCED ) {
1949 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d not configured but referenced.\n", i, 0, 0 );
1953 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, attr set #%d not configured.\n", i, 0, 0 );
1957 } else if ( !( qm->attr_sets[i].flags & PC_REFERENCED ) ) {
1958 Debug( LDAP_DEBUG_CONFIG, "pcache: attr set #%d configured but not referenced.\n", i, 0, 0 );
1963 if ( ncf || rf || nrf ) {
1964 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets configured but not referenced.\n", nrf, 0, 0 );
1965 Debug( LDAP_DEBUG_CONFIG, "pcache: warning, %d attr sets not configured.\n", ncf, 0, 0 );
1966 Debug( LDAP_DEBUG_CONFIG, "pcache: %d attr sets not configured but referenced.\n", rf, 0, 0 );
1973 /* need to inherit something from the original database... */
1974 cm->db.be_def_limit = be->be_def_limit;
1975 cm->db.be_limits = be->be_limits;
1976 cm->db.be_acl = be->be_acl;
1977 cm->db.be_dfltaccess = be->be_dfltaccess;
1979 rc = backend_startup_one( &cm->db );
1981 /* There is no runqueue in TOOL mode */
1982 if ( slapMode & SLAP_SERVER_MODE ) {
1983 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1984 ldap_pvt_runqueue_insert( &slapd_rq, cm->cc_period,
1985 consistency_check, on,
1986 "pcache_consistency", be->be_suffix[0].bv_val );
1987 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1989 /* Cached database must have the rootdn */
1990 if ( BER_BVISNULL( &cm->db.be_rootndn )
1991 || BER_BVISEMPTY( &cm->db.be_rootndn ) )
1993 Debug( LDAP_DEBUG_ANY, "pcache_db_open(): "
1994 "underlying database of type \"%s\"\n"
1995 " serving naming context \"%s\"\n"
1996 " has no \"rootdn\", required by \"proxycache\".\n",
1997 on->on_info->oi_orig->bi_type,
1998 cm->db.be_suffix[0].bv_val, 0 );
2011 slap_overinst *on = (slap_overinst *)be->bd_info;
2012 cache_manager *cm = on->on_bi.bi_private;
2013 query_manager *qm = cm->qm;
2014 QueryTemplate *tm = qm->templates;
2017 /* cleanup stuff inherited from the original database... */
2018 cm->db.be_limits = NULL;
2019 cm->db.be_acl = NULL;
2021 if ( cm->db.bd_info->bi_db_close ) {
2022 rc = cm->db.bd_info->bi_db_close( &cm->db );
2024 for ( ; tm; tm=tm->qmnext ) {
2025 CachedQuery *qc, *qn;
2026 for ( qc = tm->query; qc; qc = qn ) {
2030 free( tm->querystr.bv_val );
2031 ldap_pvt_thread_rdwr_destroy( tm->t_rwlock );
2032 ch_free( tm->t_rwlock );
2034 free( qm->templates );
2035 qm->templates = NULL;
2037 for ( i=0; i<cm->numattrsets; i++ ) {
2038 free( qm->attr_sets[i].attrs );
2040 free( qm->attr_sets );
2041 qm->attr_sets = NULL;
2051 slap_overinst *on = (slap_overinst *)be->bd_info;
2052 cache_manager *cm = on->on_bi.bi_private;
2053 query_manager *qm = cm->qm;
2055 /* cleanup stuff inherited from the original database... */
2056 cm->db.be_suffix = NULL;
2057 cm->db.be_nsuffix = NULL;
2058 BER_BVZERO( &cm->db.be_rootdn );
2059 BER_BVZERO( &cm->db.be_rootndn );
2060 BER_BVZERO( &cm->db.be_rootpw );
2061 /* FIXME: there might be more... */
2063 if ( cm->db.be_private != NULL ) {
2064 backend_destroy_one( &cm->db, 0 );
2067 ldap_pvt_thread_mutex_destroy( &qm->lru_mutex );
2068 ldap_pvt_thread_mutex_destroy( &cm->cache_mutex );
2075 static slap_overinst pcache;
2077 static char *obsolete_names[] = {
2082 int pcache_initialize()
2084 LDAPAttributeType *at;
2087 struct berval debugbv = BER_BVC("pcache");
2089 if (( code = slap_loglevel_get( &debugbv, &pcache_debug )))
2092 at = ldap_str2attributetype( queryid_schema, &code, &err,
2093 LDAP_SCHEMA_ALLOW_ALL );
2095 Debug( LDAP_DEBUG_ANY,
2096 "pcache_initialize: ldap_str2attributetype failed %s %s\n",
2097 ldap_scherr2str(code), err, 0 );
2100 code = at_add( at, 0, NULL, &err );
2102 slap_str2ad( at->at_names[0], &ad_queryid, &err );
2106 Debug( LDAP_DEBUG_ANY,
2107 "pcache_initialize: at_add failed %s %s\n",
2108 scherr2str(code), err, 0 );
2112 pcache.on_bi.bi_type = "pcache";
2113 pcache.on_bi.bi_obsolete_names = obsolete_names;
2114 pcache.on_bi.bi_db_init = pcache_db_init;
2115 pcache.on_bi.bi_db_config = pcache_db_config;
2116 pcache.on_bi.bi_db_open = pcache_db_open;
2117 pcache.on_bi.bi_db_close = pcache_db_close;
2118 pcache.on_bi.bi_db_destroy = pcache_db_destroy;
2120 pcache.on_bi.bi_op_search = pcache_op_search;
2122 pcache.on_bi.bi_chk_controls = pcache_chk_controls;
2124 pcache.on_bi.bi_cf_ocs = pcocs;
2126 code = config_register_schema( pccfg, pcocs );
2127 if ( code ) return code;
2129 return overlay_register( &pcache );
2132 #if SLAPD_OVER_PROXYCACHE == SLAPD_MOD_DYNAMIC
2133 int init_module(int argc, char *argv[]) {
2134 return pcache_initialize();
2138 #endif /* defined(SLAPD_OVER_PROXYCACHE) */