1
/* $OpenLDAP: pkg/ldap/servers/slapd/overlays/pcache.c,v 1.88.2.17 2008/07/08 21:09:37 quanah Exp $ */
1
/* $OpenLDAP: pkg/ldap/servers/slapd/overlays/pcache.c,v 1.88.2.34 2009/06/16 19:13:09 quanah Exp $ */
2
2
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4
* Copyright 2003-2008 The OpenLDAP Foundation.
4
* Copyright 2003-2009 The OpenLDAP Foundation.
5
5
* Portions Copyright 2003 IBM Corporation.
6
6
* Portions Copyright 2003 Symas Corporation.
7
7
* All rights reserved.
182
182
unsigned long num_cached_queries; /* total number of cached queries */
183
183
unsigned long max_queries; /* upper bound on # of cached queries */
184
184
int save_queries; /* save cached queries across restarts */
185
int check_cacheability; /* check whether a query is cacheable */
185
186
int numattrsets; /* number of attribute sets */
186
187
int cur_entries; /* current number of entries cached */
187
188
int max_entries; /* max number of entries cached */
265
266
struct berval bv_scope,
267
char attrset_buf[ 32 ],
268
char attrset_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
269
expiry_buf[ LDAP_PVT_INTTYPE_CHARS( unsigned long ) ],
270
271
ber_len_t attrset_len,
273
274
ldap_pvt_scope2bv( q->scope, &bv_scope );
274
275
filter2bv_x( op, q->filter, &bv_filter );
275
attrset_len = snprintf( attrset_buf, sizeof( attrset_buf ),
276
attrset_len = sprintf( attrset_buf,
276
277
"%lu", (unsigned long)q->qtemp->attr_set_index );
277
expiry_len = snprintf( expiry_buf, sizeof( expiry_buf ),
278
expiry_len = sprintf( expiry_buf,
278
279
"%lu", (unsigned long)q->expiry_time );
280
281
urlbv->bv_len = STRLENOF( "ldap:///" )
590
/* compare the first value in each filter */
591
static int pcache_filter_cmp( const void *v1, const void *v2 )
591
/* compare the current value in each filter */
592
static int pcache_filter_cmp( Filter *f1, Filter *f2 )
593
const CachedQuery *q1 = v1, *q2 =v2;
594
594
int rc, weight1, weight2;
596
switch( q1->first->f_choice ) {
596
switch( f1->f_choice ) {
597
597
case LDAP_FILTER_PRESENT:
620
620
rc = weight1 - weight2;
622
622
switch( weight1 ) {
625
rc = lex_bvcmp( &q1->first->f_av_value, &q2->first->f_av_value );
626
rc = lex_bvcmp( &f1->f_av_value, &f2->f_av_value );
628
if ( q1->first->f_choice == LDAP_FILTER_SUBSTRINGS ) {
629
if ( f1->f_choice == LDAP_FILTER_SUBSTRINGS ) {
630
if ( !BER_BVISNULL( &q1->first->f_sub_initial )) {
631
if ( !BER_BVISNULL( &q2->first->f_sub_initial )) {
632
rc = lex_bvcmp( &q1->first->f_sub_initial,
633
&q2->first->f_sub_initial );
637
} else if ( !BER_BVISNULL( &q2->first->f_sub_initial )) {
641
if ( q1->first->f_sub_any ) {
642
if ( q2->first->f_sub_any ) {
643
rc = lex_bvcmp( q1->first->f_sub_any,
644
q2->first->f_sub_any );
648
} else if ( q2->first->f_sub_any ) {
652
if ( !BER_BVISNULL( &q1->first->f_sub_final )) {
653
if ( !BER_BVISNULL( &q2->first->f_sub_final )) {
654
rc = lex_bvcmp( &q1->first->f_sub_final,
655
&q2->first->f_sub_final );
659
} else if ( !BER_BVISNULL( &q2->first->f_sub_final )) {
631
if ( !BER_BVISNULL( &f1->f_sub_initial )) {
632
if ( !BER_BVISNULL( &f2->f_sub_initial )) {
633
rc = lex_bvcmp( &f1->f_sub_initial,
634
&f2->f_sub_initial );
638
} else if ( !BER_BVISNULL( &f2->f_sub_initial )) {
642
if ( f1->f_sub_any ) {
643
if ( f2->f_sub_any ) {
644
rc = lex_bvcmp( f1->f_sub_any,
649
} else if ( f2->f_sub_any ) {
653
if ( !BER_BVISNULL( &f1->f_sub_final )) {
654
if ( !BER_BVISNULL( &f2->f_sub_final )) {
655
rc = lex_bvcmp( &f1->f_sub_final,
660
} else if ( !BER_BVISNULL( &f2->f_sub_final )) {
663
rc = lex_bvcmp( &q1->first->f_mr_value,
664
&q2->first->f_mr_value );
664
rc = lex_bvcmp( &f1->f_mr_value,
678
while ( f1->f_choice == LDAP_FILTER_AND || f1->f_choice == LDAP_FILTER_OR )
680
while ( f2->f_choice == LDAP_FILTER_AND || f2->f_choice == LDAP_FILTER_OR )
682
rc = pcache_filter_cmp( f1, f2 );
690
/* compare filters in each query */
691
static int pcache_query_cmp( const void *v1, const void *v2 )
693
const CachedQuery *q1 = v1, *q2 =v2;
694
return pcache_filter_cmp( q1->first, q2->first );
673
697
/* add query on top of LRU list */
675
699
add_query_on_top (query_manager* qm, CachedQuery* qc)
921
945
ptr = tavl_end( root, 1 );
922
946
dir = TAVL_DIR_LEFT;
924
ptr = tavl_find3( root, &cq, pcache_filter_cmp, &ret );
948
ptr = tavl_find3( root, &cq, pcache_query_cmp, &ret );
925
949
dir = (first->f_choice == LDAP_FILTER_GE) ? TAVL_DIR_LEFT :
1226
1252
new_cached_query->prev = NULL;
1227
1253
new_cached_query->qbase = qbase;
1228
1254
rc = tavl_insert( &qbase->scopes[query->scope], new_cached_query,
1229
pcache_filter_cmp, avl_dup_error );
1255
pcache_query_cmp, avl_dup_error );
1230
1256
if ( rc == 0 ) {
1231
1257
qbase->queries++;
1232
1258
if (templ->query == NULL)
1240
1266
new_cached_query = find_filter( op, qbase->scopes[query->scope],
1241
1267
query->filter, first );
1242
1268
filter_free( query->filter );
1269
query->filter = NULL;
1244
1271
Debug( pcache_debug, "TEMPLATE %p QUERIES++ %d\n",
1245
1272
(void *) templ, templ->no_of_queries, 0 );
1272
1299
qc->next->prev = qc->prev;
1273
1300
qc->prev->next = qc->next;
1275
tavl_delete( &qc->qbase->scopes[qc->scope], qc, pcache_filter_cmp );
1302
tavl_delete( &qc->qbase->scopes[qc->scope], qc, pcache_query_cmp );
1276
1303
qc->qbase->queries--;
1277
1304
if ( qc->qbase->queries == 0 ) {
1278
1305
avl_delete( &template->qbase, qc->qbase, pcache_dn_cmp );
1960
1988
* limit, empty the chain and ignore the rest.
1962
1990
if ( !si->over ) {
1991
/* check if the entry contains undefined
1992
* attributes/objectClasses (ITS#5680) */
1993
if ( cm->check_cacheability && test_filter( op, rs->sr_entry, si->query.filter ) != LDAP_COMPARE_TRUE ) {
1994
Debug( pcache_debug, "%s: query not cacheable because of schema issues in DN \"%s\"\n",
1995
op->o_log_prefix, rs->sr_entry->e_name.bv_val, 0 );
1999
/* check for malformed entries: attrs with no values */
2001
Attribute *a = rs->sr_entry->e_attrs;
2002
for (; a; a=a->a_next) {
2003
if ( !a->a_numvals ) {
2004
Debug( pcache_debug, "%s: query not cacheable because of attrs without values in DN \"%s\" (%s)\n",
2005
op->o_log_prefix, rs->sr_entry->e_name.bv_val,
2006
a->a_desc->ad_cname.bv_val );
1963
2012
if ( si->count < si->max ) {
1965
2014
e = entry_dup( rs->sr_entry );
2134
2184
(*new_attrs)[i].an_desc = attrs->attrs[i].an_desc;
2136
2186
BER_BVZERO( &(*new_attrs)[i].an_name );
2137
alluser = an_find(*new_attrs, &AllUser);
2138
allop = an_find(*new_attrs, &AllOper);
2187
alluser = an_find( *new_attrs, slap_bv_all_user_attrs );
2188
allop = an_find( *new_attrs, slap_bv_all_operational_attrs );
2141
2191
for ( i=0; i<fattr_cnt; i++ ) {
2305
2355
return rs->sr_err;
2358
/* pickup runtime ACL changes */
2359
cm->db.be_acl = op->o_bd->be_acl;
2308
2361
tempstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len+1, op->o_tmpmemctx );
2309
2362
tempstr.bv_len = 0;
2310
2363
if ( filter2template( op, op->ors_filter, &tempstr, &filter_attrs,
2360
2411
send_ldap_result( op, rs );
2362
2413
op->o_bd = &cm->db;
2363
op->o_callback = NULL;
2414
if ( cm->response_cb == PCACHE_RESPONSE_CB_TAIL ) {
2415
/* The cached entry was already processed by any
2416
* other overlays, so don't let it get processed again.
2418
op->o_callback = NULL;
2364
2420
i = cm->db.bd_info->bi_op_search( op, rs );
2366
2422
ldap_pvt_thread_rdwr_runlock(&answerable->rwlock);
2410
2466
si->slimit = 0;
2411
2467
si->slimit_exceeded = 0;
2412
2468
si->caching_reason = PC_IGNORE;
2413
if ( op->ors_slimit && op->ors_slimit < cm->num_entries_limit ) {
2469
if ( op->ors_slimit > 0 && op->ors_slimit < cm->num_entries_limit ) {
2414
2470
si->slimit = op->ors_slimit;
2415
2471
op->ors_slimit = cm->num_entries_limit;
2417
2473
si->head = NULL;
2418
2474
si->tail = NULL;
2475
si->swap_saved_attrs = 1;
2419
2476
si->save_attrs = op->ors_attrs;
2421
2478
op->ors_attrs = qtemp->t_attrs.attrs;
2634
2691
"( OLcfgOvAt:2.6 NAME 'olcProxySaveQueries' "
2635
2692
"DESC 'Save cached queries for hot restart' "
2636
2693
"SYNTAX OMsBoolean )", NULL, NULL },
2638
{ NULL, NULL, 0, 0, 0, ARG_IGNORED }
2641
/* Need to no-op this keyword for dynamic config */
2642
static ConfigTable pcdummy[] = {
2643
{ "", "", 0, 0, 0, ARG_IGNORED,
2644
NULL, "( OLcfgGlAt:13 NAME 'olcDatabase' "
2645
"DESC 'The backend type for a database instance' "
2646
"SUP olcBackend SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
2694
{ "proxyCheckCacheability", "TRUE|FALSE",
2695
2, 2, 0, ARG_ON_OFF|ARG_OFFSET, (void *)offsetof(cache_manager, check_cacheability),
2696
"( OLcfgOvAt:2.7 NAME 'olcProxyCheckCacheability' "
2697
"DESC 'Check whether the results of a query are cacheable, e.g. for schema issues' "
2698
"SYNTAX OMsBoolean )", NULL, NULL },
2647
2700
{ NULL, NULL, 0, 0, 0, ARG_IGNORED }
2653
2706
"DESC 'ProxyCache configuration' "
2654
2707
"SUP olcOverlayConfig "
2655
2708
"MUST ( olcProxyCache $ olcProxyAttrset $ olcProxyTemplate ) "
2656
"MAY ( olcProxyResponseCB $ olcProxyCacheQueries $ olcProxySaveQueries ) )",
2709
"MAY ( olcProxyResponseCB $ olcProxyCacheQueries $ olcProxySaveQueries $ olcProxyCheckCacheability ) )",
2657
2710
Cft_Overlay, pccfg, NULL, pc_cfadd },
2658
2711
{ "( OLcfgOvOc:2.2 "
2659
2712
"NAME 'olcPcacheDatabase' "
2660
2713
"DESC 'Cache database configuration' "
2661
"AUXILIARY )", Cft_Misc, pcdummy, pc_ldadd },
2714
"AUXILIARY )", Cft_Misc, olcDatabaseDummy, pc_ldadd },
2662
2715
{ NULL, 0, NULL }
2704
2757
/* FIXME: should not hardcode "olcDatabase" here */
2705
2758
bv.bv_len = snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
2706
"olcDatabase=%s", cm->db.bd_info->bi_type );
2707
if ( bv.bv_len < 0 || bv.bv_len >= sizeof( ca->cr_msg ) ) {
2759
"olcDatabase=" SLAP_X_ORDERED_FMT "%s",
2760
0, cm->db.bd_info->bi_type );
2761
if ( bv.bv_len >= sizeof( ca->cr_msg ) ) {
2710
2764
bv.bv_val = ca->cr_msg;
3249
3304
BerVarray vals = NULL;
3250
3305
Filter f = { 0 }, f2 = { 0 };
3251
3306
AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
3252
AttributeName attrs[ 2 ] = { 0 };
3307
AttributeName attrs[ 2 ] = {{{ 0 }}};
3254
3309
connection_fake_init( &conn, &opbuf, thrctx );
3255
3310
op = &opbuf.ob_op;
3807
3862
assert( !BER_BVISNULL( &op->o_req_ndn ) );
3808
3863
len = snprintf( buf, sizeof( buf ), " dn=\"%s\"", op->o_req_ndn.bv_val );
3810
if ( !BER_BVISNULL( &uuid ) ) {
3865
if ( !BER_BVISNULL( &uuid ) && len < sizeof( buf ) ) {
3811
3866
snprintf( &buf[ len ], sizeof( buf ) - len, " queryId=\"%s\"", uuid.bv_val );