#include <stdio.h>
+#include <ac/errno.h>
#include <ac/socket.h>
#include <ac/string.h>
meta_back_bind( Operation *op, SlapReply *rs )
{
metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
- metaconn_t *mc;
+ metaconn_t *mc = NULL;
int rc = LDAP_OTHER,
i, gotit = 0, isroot = 0;
mc->mc_auth_target = META_BOUND_ALL;
}
+ meta_back_release_conn( op, mc );
+
/*
* rc is LDAP_SUCCESS if at least one bind succeeded,
* err is the last error that occurred during a bind;
rc = slap_map_api2result( rs );
if ( rs->sr_err == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
- ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
- msc->msc_ld = NULL;
- msc->msc_bound = 0;
+retry_lock:;
+ switch ( ldap_pvt_thread_mutex_trylock( &mi->mi_conn_mutex ) ) {
+ case LDAP_PVT_THREAD_EBUSY:
+ default:
+ ldap_pvt_thread_yield();
+ goto retry_lock;
+
+ case 0:
+ break;
+ }
+
+ if ( mc->mc_refcnt == 1 ) {
+ ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
+ msc->msc_ld = NULL;
+ msc->msc_bound = 0;
+
+ /* mc here must be the regular mc, reset and ready for init */
+ rc = meta_back_init_one_conn( op, rs, mt, msc,
+ LDAP_BACK_DONTSEND );
+
+ } else {
+ /* can't do anything about it */
+ rc = 0;
+ }
+
+ ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
- /* mc here must be the regular mc, reset and ready for init */
- rc = meta_back_init_one_conn( op, rs, mt, msc,
- LDAP_BACK_DONTSEND );
if ( rc ) {
if ( nretries > 0 ) {
nretries--;
metaconn_t *mc,
int candidate,
ldap_back_send_t sendok,
- int nretries )
+ int nretries,
+ int dolock )
{
metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
metatarget_t *mt = &mi->mi_targets[ candidate ];
rc = slap_map_api2result( rs );
if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
- ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
- msc->msc_ld = NULL;
- msc->msc_bound = 0;
+ if ( dolock ) {
+retry_lock:;
+ switch ( ldap_pvt_thread_mutex_trylock( &mi->mi_conn_mutex ) ) {
+ case LDAP_PVT_THREAD_EBUSY:
+ default:
+ ldap_pvt_thread_yield();
+ goto retry_lock;
+
+ case 0:
+ break;
+ }
+ }
+
+ if ( mc->mc_refcnt == 1 ) {
+ ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
+ msc->msc_ld = NULL;
+ msc->msc_bound = 0;
- /* mc here must be the regular mc, reset and ready for init */
- rc = meta_back_init_one_conn( op, rs, mt, msc, LDAP_BACK_DONTSEND );
+ /* mc here must be the regular mc, reset and ready for init */
+ rc = meta_back_init_one_conn( op, rs, mt, msc, LDAP_BACK_DONTSEND );
+
+
+ } else {
+ /* can't do anything about it */
+ rc = LDAP_UNAVAILABLE;
+ }
+
+ if ( dolock ) {
+ ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
+ }
if ( rc == LDAP_SUCCESS ) {
ldap_pvt_thread_yield();
}
rc = meta_back_single_dobind( op, rs, mc, i,
- LDAP_BACK_DONTSEND, mt->mt_nretries );
+ LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
if ( rc != LDAP_SUCCESS ) {
rs->sr_err = slap_map_api2result( rs );
"%s meta_back_dobind: conn=%ld bound=%d\n",
op->o_log_prefix, mc->mc_conn->c_connid, bound );
- if ( bound == 0 && ( sendok & LDAP_BACK_SENDERR ) ) {
- if ( rs->sr_err == LDAP_SUCCESS ) {
- rs->sr_err = LDAP_BUSY;
+ if ( bound == 0 ) {
+ meta_back_release_conn( op, mc );
+
+ if ( sendok & LDAP_BACK_SENDERR ) {
+ if ( rs->sr_err == LDAP_SUCCESS ) {
+ rs->sr_err = LDAP_BUSY;
+ }
+ send_ldap_result( op, rs );
}
- send_ldap_result( op, rs );
}
return( bound > 0 );
*
* clears a metaconn
*/
+
void
meta_back_conn_free(
metaconn_t *mc )
{
- if ( mc == NULL ) {
- return;
- }
+ assert( mc != NULL );
+ assert( mc->mc_refcnt == 0 );
ldap_pvt_thread_mutex_destroy( &mc->mc_mutex );
-
free( mc );
}
+static void
+meta_back_freeconn(
+ Operation *op,
+ metaconn_t *mc )
+{
+ metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
+
+ assert( mc != NULL );
+
+retry_lock:;
+ switch ( ldap_pvt_thread_mutex_trylock( &mi->mi_conn_mutex ) ) {
+ case LDAP_PVT_THREAD_EBUSY:
+ default:
+ ldap_pvt_thread_yield();
+ goto retry_lock;
+
+ case 0:
+ break;
+ }
+
+ if ( --mc->mc_refcnt == 0 ) {
+ meta_back_conn_free( mc );
+ }
+
+ ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
+}
+
/*
* meta_back_init_one_conn
*
{
metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
metatarget_t *mt = &mi->mi_targets[ candidate ];
- int rc;
+ int rc = LDAP_UNAVAILABLE;
metasingleconn_t *msc = &mc->mc_conns[ candidate ];
- ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
+retry_lock:;
+ switch ( ldap_pvt_thread_mutex_trylock( &mi->mi_conn_mutex ) ) {
+ case LDAP_PVT_THREAD_EBUSY:
+ default:
+ ldap_pvt_thread_yield();
+ goto retry_lock;
+
+ case 0:
+ break;
+ }
- ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
- msc->msc_ld = NULL;
- msc->msc_bound = 0;
+ assert( mc->mc_refcnt > 0 );
- /* mc here must be the regular mc, reset and ready for init */
- rc = meta_back_init_one_conn( op, rs, mt, msc, sendok );
+ if ( mc->mc_refcnt == 1 ) {
+ ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
- if ( rc == LDAP_SUCCESS ) {
- rc = meta_back_single_dobind( op, rs, mc, candidate,
- sendok, mt->mt_nretries );
- }
+ ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
+ msc->msc_ld = NULL;
+ msc->msc_bound = 0;
- ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
+ /* mc here must be the regular mc, reset and ready for init */
+ rc = meta_back_init_one_conn( op, rs, mt, msc, sendok );
+
+ if ( rc == LDAP_SUCCESS ) {
+ rc = meta_back_single_dobind( op, rs, mc, candidate,
+ sendok, mt->mt_nretries, 0 );
+ }
+
+ ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
+ }
+
+ ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
return rc == LDAP_SUCCESS ? 1 : 0;
}
ldap_back_send_t sendok )
{
metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
- metaconn_t *mc, mc_curr;
+ metaconn_t *mc = NULL,
+ mc_curr = { 0 };
int cached = META_TARGET_NONE,
i = META_TARGET_NONE,
err = LDAP_SUCCESS,
}
mc = (metaconn_t *)avl_find( mi->mi_conntree,
(caddr_t)&mc_curr, meta_back_conn_cmp );
+ if ( mc ) {
+ mc->mc_refcnt++;
+ }
ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
switch ( op->o_tag ) {
if ( op_type == META_OP_REQUIRE_ALL ) {
/* Looks like we didn't get a bind. Open a new session... */
- if ( !mc ) {
+ if ( mc == NULL ) {
mc = metaconn_alloc( op );
mc->mc_conn = op->o_conn;
new_conn = 1;
if ( ncandidates == 0 ) {
if ( new_conn ) {
- meta_back_conn_free( mc );
+ meta_back_freeconn( op, mc );
+
+ } else {
+ meta_back_release_conn( op, mc );
}
rs->sr_err = LDAP_NO_SUCH_OBJECT;
"==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n",
i, op->o_req_ndn.bv_val, 0 );
- /* Retries searching for a metaconn in the avl tree */
- mc_curr.mc_conn = op->o_conn;
+ if ( mc == NULL ) {
+ /* Retries searching for a metaconn in the avl tree
+ * the reason is that the connection might have been
+ * created by meta_back_get_candidate() */
+ mc_curr.mc_conn = op->o_conn;
retry_lock2:;
- switch ( ldap_pvt_thread_mutex_trylock( &mi->mi_conn_mutex ) ) {
- case LDAP_PVT_THREAD_EBUSY:
- default:
- ldap_pvt_thread_yield();
- goto retry_lock2;
-
- case 0:
- break;
- }
- mc = (metaconn_t *)avl_find( mi->mi_conntree,
- (caddr_t)&mc_curr, meta_back_conn_cmp );
- ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
+ switch ( ldap_pvt_thread_mutex_trylock( &mi->mi_conn_mutex ) ) {
+ case LDAP_PVT_THREAD_EBUSY:
+ default:
+ ldap_pvt_thread_yield();
+ goto retry_lock2;
+
+ case 0:
+ break;
+ }
+ mc = (metaconn_t *)avl_find( mi->mi_conntree,
+ (caddr_t)&mc_curr, meta_back_conn_cmp );
+ if ( mc != NULL ) {
+ mc->mc_refcnt++;
+ }
+ ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
- /* Looks like we didn't get a bind. Open a new session... */
- if ( !mc ) {
- mc = metaconn_alloc( op );
- mc->mc_conn = op->o_conn;
- new_conn = 1;
+ /* Looks like we didn't get a bind. Open a new session... */
+ if ( mc == NULL ) {
+ mc = metaconn_alloc( op );
+ mc->mc_conn = op->o_conn;
+ new_conn = 1;
+ }
}
/*
*/
candidates[ i ].sr_tag = META_NOT_CANDIDATE;
if ( new_conn ) {
- ( void )meta_clear_one_candidate( &mc->mc_conns[ i ] );
- meta_back_conn_free( mc );
+ (void)meta_clear_one_candidate( &mc->mc_conns[ i ] );
+ meta_back_freeconn( op, mc );
+
+ } else {
+ meta_back_release_conn( op, mc );
}
return NULL;
}
} else {
/* Looks like we didn't get a bind. Open a new session... */
- if ( !mc ) {
+ if ( mc == NULL ) {
mc = metaconn_alloc( op );
mc->mc_conn = op->o_conn;
new_conn = 1;
if ( ncandidates == 0 ) {
if ( new_conn ) {
- meta_back_conn_free( mc );
+ meta_back_freeconn( op, mc );
+
+ } else {
+ meta_back_release_conn( op, mc );
}
rs->sr_err = LDAP_NO_SUCH_OBJECT;
/*
* Err could be -1 in case a duplicate metaconn is inserted
+ *
+ * FIXME: what if the same client issues more than one
+ * asynchronous operations?
*/
if ( err != 0 ) {
Debug( LDAP_DEBUG_ANY,
rs->sr_err = LDAP_OTHER;
rs->sr_text = "Internal server error";
- meta_back_conn_free( mc );
+ meta_back_freeconn( op, mc );
if ( sendok & LDAP_BACK_SENDERR ) {
send_ldap_result( op, rs );
rs->sr_text = NULL;
return mc;
}
+void
+meta_back_release_conn(
+ Operation *op,
+ metaconn_t *mc )
+{
+ metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
+
+ assert( mc != NULL );
+
+retry_lock:;
+ switch ( ldap_pvt_thread_mutex_trylock( &mi->mi_conn_mutex ) ) {
+ case LDAP_PVT_THREAD_EBUSY:
+ default:
+ ldap_pvt_thread_yield();
+ goto retry_lock;
+
+ case 0:
+ break;
+ }
+
+ assert( mc->mc_refcnt > 0 );
+ mc->mc_refcnt--;
+ ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
+}