]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/syncrepl.c
ITS#8789 avoid unnecessary writes of context entry
[openldap] / servers / slapd / syncrepl.c
index c020229528af3af19b4793b98534616adb095194..a0b87a9ec47be30a45b198d6b6ca3e487dd8044c 100644 (file)
@@ -46,11 +46,13 @@ struct nonpresent_entry {
 
 typedef struct cookie_state {
        ldap_pvt_thread_mutex_t cs_mutex;
+       ldap_pvt_thread_cond_t cs_cond;
        struct berval *cs_vals;
        int *cs_sids;
        int     cs_num;
        int cs_age;
        int cs_ref;
+       int cs_updating;
 
        /* pending changes, not yet committed */
        ldap_pvt_thread_mutex_t cs_pmutex;
@@ -121,6 +123,7 @@ typedef struct syncinfo_s {
        int                     si_got;
        int                     si_strict_refresh;      /* stop listening during fallback refresh */
        int                     si_too_old;
+       int                     si_has_syncprov;
        ber_int_t       si_msgid;
        Avlnode                 *si_presentlist;
        LDAP                    *si_ld;
@@ -150,7 +153,7 @@ static int syncrepl_entry(
                                        struct berval *cookieCSN );
 static int syncrepl_updateCookie(
                                        syncinfo_t *, Operation *,
-                                       struct sync_cookie * );
+                                       struct sync_cookie *, int save );
 static struct berval * slap_uuidstr_from_normalized(
                                        struct berval *, struct berval *, void * );
 static int syncrepl_add_glue_ancestors(
@@ -162,7 +165,6 @@ static int syncrepl_op_modify( Operation *op, SlapReply *rs );
 /* callback functions */
 static int dn_callback( Operation *, SlapReply * );
 static int nonpresent_callback( Operation *, SlapReply * );
-static int null_callback( Operation *, SlapReply * );
 
 static AttributeDescription *sync_descs[4];
 
@@ -176,6 +178,7 @@ typedef struct logschema {
        struct berval ls_newRdn;
        struct berval ls_delRdn;
        struct berval ls_newSup;
+       struct berval ls_controls;
 } logschema;
 
 static logschema changelog_sc = {
@@ -184,7 +187,8 @@ static logschema changelog_sc = {
        BER_BVC("changes"),
        BER_BVC("newRDN"),
        BER_BVC("deleteOldRDN"),
-       BER_BVC("newSuperior")
+       BER_BVC("newSuperior"),
+       BER_BVC("controls")
 };
 
 static logschema accesslog_sc = {
@@ -193,7 +197,8 @@ static logschema accesslog_sc = {
        BER_BVC("reqMod"),
        BER_BVC("reqNewRDN"),
        BER_BVC("reqDeleteOldRDN"),
-       BER_BVC("reqNewSuperior")
+       BER_BVC("reqNewSuperior"),
+       BER_BVC("reqControls")
 };
 
 static const char *
@@ -409,7 +414,7 @@ ldap_sync_search(
        int rc;
        int rhint;
        char *base;
-       char **attrs, *lattrs[8];
+       char **attrs, *lattrs[9];
        char *filter;
        int attrsonly;
        int scope;
@@ -438,8 +443,9 @@ ldap_sync_search(
                lattrs[3] = ls->ls_newRdn.bv_val;
                lattrs[4] = ls->ls_delRdn.bv_val;
                lattrs[5] = ls->ls_newSup.bv_val;
-               lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
-               lattrs[7] = NULL;
+               lattrs[6] = ls->ls_controls.bv_val;
+               lattrs[7] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
+               lattrs[8] = NULL;
 
                rhint = 0;
                base = si->si_logbase.bv_val;
@@ -558,6 +564,7 @@ check_syncprov(
                        ber_bvarray_free( a.a_nvals );
                }
                ber_bvarray_free( a.a_vals );
+               si->si_has_syncprov = 1;
        }
        /* See if the cookieState has changed due to anything outside
         * this particular consumer. That includes other consumers in
@@ -722,6 +729,8 @@ do_syncrep1(
                                        si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
                        }
                        ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
+                       /* Also look in syncprov overlay, if it was already active */
+                       check_syncprov( op, si );
                }
 
                ch_free( si->si_syncCookie.octet_str.bv_val );
@@ -1024,7 +1033,7 @@ do_syncrep2(
                                if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
                                        syncCookie.ctxcsn )
                                {
-                                       rc = syncrepl_updateCookie( si, op, &syncCookie );
+                                       rc = syncrepl_updateCookie( si, op, &syncCookie, 0 );
                                } else switch ( rc ) {
                                        case LDAP_ALREADY_EXISTS:
                                        case LDAP_NO_SUCH_OBJECT:
@@ -1052,7 +1061,7 @@ do_syncrep2(
                                        syncstate, syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
                                        syncCookie.ctxcsn )
                                {
-                                       rc = syncrepl_updateCookie( si, op, &syncCookie );
+                                       rc = syncrepl_updateCookie( si, op, &syncCookie, 0 );
                                }
                        }
                        if ( punlock >= 0 ) {
@@ -1198,7 +1207,7 @@ do_syncrep2(
                        }
                        if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
                        {
-                               rc = syncrepl_updateCookie( si, op, &syncCookie );
+                               rc = syncrepl_updateCookie( si, op, &syncCookie, 1 );
                        }
                        if ( si->si_refreshCount ) {
                                LDAP_SLIST_REMOVE( &op->o_extra, si->si_refreshTxn, OpExtra, oe_next );
@@ -1388,7 +1397,7 @@ do_syncrep2(
 
                                        if ( syncCookie.ctxcsn )
                                        {
-                                               rc = syncrepl_updateCookie( si, op, &syncCookie);
+                                               rc = syncrepl_updateCookie( si, op, &syncCookie, 1 );
                                        }
                                        if ( si->si_presentlist ) {
                                                presentlist_free( si->si_presentlist );
@@ -1795,6 +1804,11 @@ syncrepl_accesslog_mods(
                if ( !colon ) {
                        /* Invalid */
                        continue;
+               } else if ( colon == bv.bv_val ) {
+                       /* ITS#6545: An empty attribute signals that a new mod
+                        * is about to start */
+                       mod = NULL;
+                       continue;
                }
 
                bv.bv_len = colon - bv.bv_val;
@@ -2184,11 +2198,8 @@ syncrepl_op_modify( Operation *op, SlapReply *rs )
        if ( match < 0 ) {
                Operation op2 = *op;
                AttributeName an[2];
-               const char *text;
                struct berval bv;
-               char *ptr;
-               Modifications *ml;
-               int size, rc;
+               int size;
                SlapReply rs1 = {0};
                resolve_ctxt rx;
                slap_callback cb = { NULL, syncrepl_resolve_cb, NULL, NULL };
@@ -2275,6 +2286,34 @@ syncrepl_op_modify( Operation *op, SlapReply *rs )
        return SLAP_CB_CONTINUE;
 }
 
+static int
+syncrepl_null_callback(
+       Operation *op,
+       SlapReply *rs )
+{
+       /* If we're not the last callback in the chain, move to the end */
+       if ( op->o_callback->sc_next ) {
+               slap_callback **sc, *s1;
+               s1 = op->o_callback;
+               op->o_callback = op->o_callback->sc_next;
+               for ( sc = &op->o_callback; *sc; sc = &(*sc)->sc_next ) ;
+               *sc = s1;
+               s1->sc_next = NULL;
+               return SLAP_CB_CONTINUE;
+       }
+       if ( rs->sr_err != LDAP_SUCCESS &&
+               rs->sr_err != LDAP_REFERRAL &&
+               rs->sr_err != LDAP_ALREADY_EXISTS &&
+               rs->sr_err != LDAP_NO_SUCH_OBJECT &&
+               rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
+       {
+               Debug( LDAP_DEBUG_ANY,
+                       "syncrepl_null_callback : error code 0x%x\n",
+                       rs->sr_err, 0, 0 );
+       }
+       return LDAP_SUCCESS;
+}
+
 static int
 syncrepl_message_to_op(
        syncinfo_t      *si,
@@ -2286,7 +2325,7 @@ syncrepl_message_to_op(
        Modifications   *modlist = NULL;
        logschema *ls;
        SlapReply rs = { REP_RESULT };
-       slap_callback cb = { NULL, null_callback, NULL, NULL };
+       slap_callback cb = { NULL, syncrepl_null_callback, NULL, NULL };
 
        const char      *text;
        char txtbuf[SLAP_TEXT_BUFLEN];
@@ -2380,6 +2419,22 @@ syncrepl_message_to_op(
                        }
                } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
                        sup = bvals[0];
+               } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_controls ) ) {
+                       int i;
+                       struct berval rel_ctrl_bv;
+
+                       (void)ber_str2bv( "{" LDAP_CONTROL_RELAX, 0, 0, &rel_ctrl_bv );
+                       for ( i = 0; bvals[i].bv_val; i++ ) {
+                               struct berval cbv, tmp;
+
+                               ber_bvchr_post( &cbv, &bvals[i], '}' );
+                               ber_bvchr_post( &tmp, &cbv, '{' );
+                               ber_bvchr_pre( &cbv, &tmp, ' ' );
+                               if ( cbv.bv_len == tmp.bv_len )         /* control w/o value */
+                                       ber_bvchr_pre( &cbv, &tmp, '}' );
+                               if ( !ber_bvcmp( &cbv, &rel_ctrl_bv ) )
+                                       op->o_relax = SLAP_CONTROL_CRITICAL;
+                       }
                } else if ( !ber_bvstrcasecmp( &bv,
                        &slap_schema.si_ad_entryCSN->ad_cname ) )
                {
@@ -2398,6 +2453,9 @@ syncrepl_message_to_op(
        op->o_callback = &cb;
        slap_op_time( &op->o_time, &op->o_tincr );
 
+       Debug( LDAP_DEBUG_SYNC, "syncrepl_message_to_op: %s tid %x\n",
+               si->si_ridtxt, op->o_tid, 0 );
+
        switch( op->o_tag ) {
        case LDAP_REQ_ADD:
        case LDAP_REQ_MODIFY:
@@ -2905,8 +2963,8 @@ syncrepl_entry(
        int     freecsn = 1;
 
        Debug( LDAP_DEBUG_SYNC,
-               "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
-               si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
+               "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s) tid %x\n",
+               si->si_ridtxt, syncrepl_state2str( syncstate ), op->o_tid );
 
        if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
                if ( !si->si_refreshPresent && !si->si_refreshDone ) {
@@ -3001,7 +3059,7 @@ syncrepl_entry(
                slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
        }
 
-       cb.sc_response = null_callback;
+       cb.sc_response = syncrepl_null_callback;
        cb.sc_private = si;
 
        if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
@@ -3421,7 +3479,7 @@ retry_modrdn:;
                                op->o_delete_glue_parent = 0;
                                if ( !be_issuffix( be, &op->o_req_ndn ) ) {
                                        slap_callback cb = { NULL };
-                                       cb.sc_response = slap_null_cb;
+                                       cb.sc_response = syncrepl_null_callback;
                                        dnParent( &op->o_req_ndn, &pdn );
                                        op->o_req_dn = pdn;
                                        op->o_req_ndn = pdn;
@@ -3619,7 +3677,7 @@ syncrepl_del_nonpresent(
                        np_list = LDAP_LIST_NEXT( np_list, npe_link );
                        op->o_tag = LDAP_REQ_DELETE;
                        op->o_callback = &cb;
-                       cb.sc_response = null_callback;
+                       cb.sc_response = syncrepl_null_callback;
                        cb.sc_private = si;
                        op->o_req_dn = *np_prev->npe_name;
                        op->o_req_ndn = *np_prev->npe_nname;
@@ -3661,7 +3719,7 @@ syncrepl_del_nonpresent(
                                op->o_delete_glue_parent = 0;
                                if ( !be_issuffix( be, &op->o_req_ndn ) ) {
                                        slap_callback cb = { NULL };
-                                       cb.sc_response = slap_null_cb;
+                                       cb.sc_response = syncrepl_null_callback;
                                        dnParent( &op->o_req_ndn, &pdn );
                                        op->o_req_dn = pdn;
                                        op->o_req_ndn = pdn;
@@ -3714,7 +3772,7 @@ syncrepl_add_glue_ancestors(
 
        op->o_tag = LDAP_REQ_ADD;
        op->o_callback = &cb;
-       cb.sc_response = null_callback;
+       cb.sc_response = syncrepl_null_callback;
        cb.sc_private = NULL;
 
        dn = e->e_name;
@@ -3856,7 +3914,7 @@ syncrepl_add_glue(
 
        op->o_tag = LDAP_REQ_ADD;
        op->o_callback = &cb;
-       cb.sc_response = null_callback;
+       cb.sc_response = syncrepl_null_callback;
        cb.sc_private = NULL;
 
        op->o_req_dn = e->e_name;
@@ -3877,7 +3935,8 @@ static int
 syncrepl_updateCookie(
        syncinfo_t *si,
        Operation *op,
-       struct sync_cookie *syncCookie )
+       struct sync_cookie *syncCookie,
+       int save )
 {
        Backend *be = op->o_bd;
        Modifications mod;
@@ -3901,6 +3960,8 @@ syncrepl_updateCookie(
        mod.sml_next = NULL;
 
        ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
+       while ( si->si_cookieState->cs_updating )
+               ldap_pvt_thread_cond_wait( &si->si_cookieState->cs_cond, &si->si_cookieState->cs_mutex );
 
 #ifdef CHECK_CSN
        for ( i=0; i<syncCookie->numcsns; i++ ) {
@@ -3963,44 +4024,56 @@ syncrepl_updateCookie(
                ch_free( sc.sids );
                return 0;
        }
-       op->o_bd = si->si_wbe;
-       slap_queue_csn( op, &first );
 
-       op->o_tag = LDAP_REQ_MODIFY;
+       si->si_cookieState->cs_updating = 1;
+       ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
 
-       cb.sc_response = null_callback;
-       cb.sc_private = si;
+       if ( save || !si->si_has_syncprov ) {
+               op->o_bd = si->si_wbe;
+               slap_queue_csn( op, &first );
 
-       op->o_callback = &cb;
-       op->o_req_dn = si->si_contextdn;
-       op->o_req_ndn = si->si_contextdn;
+               op->o_tag = LDAP_REQ_MODIFY;
+
+               cb.sc_response = syncrepl_null_callback;
+               cb.sc_private = si;
 
-       /* update contextCSN */
-       op->o_dont_replicate = 1;
+               op->o_callback = &cb;
+               op->o_req_dn = si->si_contextdn;
+               op->o_req_ndn = si->si_contextdn;
 
-       mod.sml_numvals = sc.numcsns;
-       mod.sml_values = sc.ctxcsn;
+               /* update contextCSN */
+               op->o_dont_replicate = 1;
 
-       op->orm_modlist = &mod;
-       op->orm_no_opattrs = 1;
-       rc = op->o_bd->be_modify( op, &rs_modify );
+               mod.sml_numvals = sc.numcsns;
+               mod.sml_values = sc.ctxcsn;
 
-       if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
-               SLAP_SYNC_SUBENTRY( op->o_bd )) {
-               const char      *text;
-               char txtbuf[SLAP_TEXT_BUFLEN];
-               size_t textlen = sizeof txtbuf;
-               Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
-               rs_reinit( &rs_modify, REP_RESULT );
-               rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
-               op->ora_e = e;
-               rc = op->o_bd->be_add( op, &rs_modify );
-               if ( e == op->ora_e )
-                       be_entry_release_w( op, op->ora_e );
+               op->orm_modlist = &mod;
+               op->orm_no_opattrs = 1;
+               rc = op->o_bd->be_modify( op, &rs_modify );
+
+               if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
+                       SLAP_SYNC_SUBENTRY( op->o_bd )) {
+                       const char      *text;
+                       char txtbuf[SLAP_TEXT_BUFLEN];
+                       size_t textlen = sizeof txtbuf;
+                       Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
+                       rs_reinit( &rs_modify, REP_RESULT );
+                       rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
+                       op->ora_e = e;
+                       rc = op->o_bd->be_add( op, &rs_modify );
+                       if ( e == op->ora_e )
+                               be_entry_release_w( op, op->ora_e );
+               }
+
+               op->orm_no_opattrs = 0;
+               op->o_dont_replicate = 0;
+       } else {
+               rc = 0;
        }
 
        op->orm_no_opattrs = 0;
        op->o_dont_replicate = 0;
+       ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
 
        if ( rs_modify.sr_err == LDAP_SUCCESS ) {
                slap_sync_cookie_free( &si->si_syncCookie, 0 );
@@ -4033,10 +4106,13 @@ syncrepl_updateCookie(
        }
 #endif
 
+       si->si_cookieState->cs_updating = 0;
+       ldap_pvt_thread_cond_broadcast( &si->si_cookieState->cs_cond );
        ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
 
        op->o_bd = be;
-       op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
+       if ( op->o_csn.bv_val )
+               op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
        BER_BVZERO( &op->o_csn );
        if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
 
@@ -4461,24 +4537,6 @@ nonpresent_callback(
        return LDAP_SUCCESS;
 }
 
-static int
-null_callback(
-       Operation*      op,
-       SlapReply*      rs )
-{
-       if ( rs->sr_err != LDAP_SUCCESS &&
-               rs->sr_err != LDAP_REFERRAL &&
-               rs->sr_err != LDAP_ALREADY_EXISTS &&
-               rs->sr_err != LDAP_NO_SUCH_OBJECT &&
-               rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
-       {
-               Debug( LDAP_DEBUG_ANY,
-                       "null_callback : error code 0x%x\n",
-                       rs->sr_err, 0, 0 );
-       }
-       return LDAP_SUCCESS;
-}
-
 static struct berval *
 slap_uuidstr_from_normalized(
        struct berval* uuidstr,
@@ -4715,6 +4773,7 @@ syncinfo_free( syncinfo_t *sie, int free_all )
                        if ( !sie->si_cookieState->cs_ref ) {
                                ch_free( sie->si_cookieState->cs_sids );
                                ber_bvarray_free( sie->si_cookieState->cs_vals );
+                               ldap_pvt_thread_cond_destroy( &sie->si_cookieState->cs_cond );
                                ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
                                ch_free( sie->si_cookieState->cs_psids );
                                ber_bvarray_free( sie->si_cookieState->cs_pvals );
@@ -5521,6 +5580,7 @@ add_syncrepl(
                        si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
                        ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
                        ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_pmutex );
+                       ldap_pvt_thread_cond_init( &si->si_cookieState->cs_cond );
 
                        c->be->be_syncinfo = si;
                }
@@ -5769,8 +5829,7 @@ syncrepl_config( ConfigArgs *c )
                                                                ldap_pvt_runqueue_stoptask( &slapd_rq, re );
                                                                isrunning = 1;
                                                        }
-                                                       if ( ldap_pvt_thread_pool_retract( &connection_pool,
-                                                                       re->routine, re ) > 0 )
+                                                       if ( ldap_pvt_thread_pool_retract( re->pool_cookie ) > 0 )
                                                                isrunning = 0;
 
                                                        ldap_pvt_runqueue_remove( &slapd_rq, re );