windows_tot_protocol.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * do so, delete this exception statement from your version.
  30. *
  31. *
  32. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  33. * Copyright (C) 2005 Red Hat, Inc.
  34. * All rights reserved.
  35. * END COPYRIGHT BLOCK **/
  36. /* windows_tot_protocol.c */
  37. /*
  38. The tot_protocol object implements the DS 5.0 multi-master total update
  39. replication protocol, used to (re)populate a replica.
  40. */
  41. #include "repl.h"
  42. #include "repl5.h"
  43. #include "windowsrepl.h"
  44. #include "windows_prot_private.h"
  45. #include "slap.h"
  46. /* Private data structures */
  47. typedef struct windows_tot_private
  48. {
  49. Repl_Protocol *rp;
  50. Repl_Agmt *ra;
  51. PRLock *lock;
  52. PRUint32 eventbits;
  53. } windows_tot_private;
  54. typedef struct callback_data
  55. {
  56. Private_Repl_Protocol *prp;
  57. int rc;
  58. unsigned long num_entries;
  59. time_t sleep_on_busy;
  60. time_t last_busy;
  61. } callback_data;
  62. /*
  63. * Number of window seconds to wait until we programmatically decide
  64. * that the replica has got out of BUSY state
  65. */
  66. #define SLEEP_ON_BUSY_WINDOW (10)
  67. /* Helper functions */
  68. static void get_result (int rc, void *cb_data);
  69. static int send_entry (Slapi_Entry *e, void *callback_data);
  70. static void windows_tot_delete(Private_Repl_Protocol **prp);
  71. #if 0
  72. /* DBDB: this is all wrong. Need to fix this */
  73. object_release ( replica_get_ruv ( replica ) );
  74. cons_ruv_obj = agmt_get_consumer_ruv(prp->agmt);
  75. #endif
  76. /*
  77. * Completely refresh a replica. The basic protocol interaction goes
  78. * like this:
  79. * - Acquire Replica by sending a StartReplicationRequest extop, with the
  80. * total update protocol OID and supplier's ruv.
  81. * - Send a series of extended operations containing entries.
  82. * - send an EndReplicationRequest extended operation
  83. */
  84. static void
  85. windows_tot_run(Private_Repl_Protocol *prp)
  86. {
  87. int rc;
  88. callback_data cb_data;
  89. Slapi_PBlock *pb;
  90. const char* dn;
  91. CSN *remote_schema_csn = NULL;
  92. PRBool cookie_has_more = PR_TRUE;
  93. RUV *ruv = NULL;
  94. RUV *starting_ruv = NULL;
  95. Replica *replica = NULL;
  96. Object *local_ruv_obj = NULL;
  97. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_tot_run\n", 0, 0, 0 );
  98. PR_ASSERT(NULL != prp);
  99. prp->stopped = 0;
  100. if (prp->terminate)
  101. {
  102. prp->stopped = 1;
  103. goto done;
  104. }
  105. conn_set_timeout(prp->conn, agmt_get_timeout(prp->agmt));
  106. /* acquire remote replica */
  107. agmt_set_last_init_start(prp->agmt, current_time());
  108. rc = windows_acquire_replica (prp, &ruv, 0 /* don't check RUV for total protocol */);
  109. /* We never retry total protocol, even in case a transient error.
  110. This is because if somebody already updated the replica we don't
  111. want to do it again */
  112. if (rc != ACQUIRE_SUCCESS)
  113. {
  114. int optype, ldaprc;
  115. conn_get_error(prp->conn, &optype, &ldaprc);
  116. agmt_set_last_init_status(prp->agmt, ldaprc,
  117. prp->last_acquire_response_code, NULL);
  118. goto done;
  119. }
  120. else if (prp->terminate)
  121. {
  122. conn_disconnect(prp->conn);
  123. prp->stopped = 1;
  124. goto done;
  125. }
  126. /* Get the current replica RUV.
  127. * If the total update succeeds, we will set the consumer RUV to this value.
  128. */
  129. replica = object_get_data(prp->replica_object);
  130. local_ruv_obj = replica_get_ruv (replica);
  131. starting_ruv = ruv_dup((RUV*) object_get_data ( local_ruv_obj ));
  132. object_release (local_ruv_obj);
  133. agmt_set_last_init_status(prp->agmt, 0, 0, "Total schema update in progress");
  134. remote_schema_csn = agmt_get_consumer_schema_csn ( prp->agmt );
  135. agmt_set_last_init_status(prp->agmt, 0, 0, "Total update in progress");
  136. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name, "Beginning total update of replica "
  137. "\"%s\".\n", agmt_get_long_name(prp->agmt));
  138. windows_private_null_dirsync_cookie(prp->agmt);
  139. /* get everything */
  140. windows_dirsync_inc_run(prp);
  141. cookie_has_more = windows_private_dirsync_has_more(prp->agmt);
  142. windows_private_save_dirsync_cookie(prp->agmt);
  143. /* send everything */
  144. dn = slapi_sdn_get_dn( windows_private_get_directory_subtree(prp->agmt));
  145. pb = slapi_pblock_new ();
  146. slapi_search_internal_set_pb (pb, dn, /* XXX modify the searchfilter and scope? */
  147. LDAP_SCOPE_ONELEVEL, "(|(objectclass=ntuser)(objectclass=ntgroup)(nsuniqueid=*))", NULL, 0, NULL, NULL,
  148. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  149. cb_data.prp = prp;
  150. cb_data.rc = 0;
  151. cb_data.num_entries = 0UL;
  152. cb_data.sleep_on_busy = 0UL;
  153. cb_data.last_busy = current_time ();
  154. /* this search get all the entries from the replicated area including tombstones
  155. and referrals */
  156. slapi_search_internal_callback_pb (pb, &cb_data /* callback data */,
  157. get_result /* result callback */,
  158. send_entry /* entry callback */,
  159. NULL /* referral callback*/);
  160. slapi_pblock_destroy (pb);
  161. agmt_set_last_init_end(prp->agmt, current_time());
  162. rc = cb_data.rc;
  163. windows_release_replica(prp);
  164. if (rc != LDAP_SUCCESS)
  165. {
  166. slapi_log_error (SLAPI_LOG_REPL, windows_repl_plugin_name, "%s: windows_tot_run: "
  167. "failed to obtain data to send to the consumer; LDAP error - %d\n",
  168. agmt_get_long_name(prp->agmt), rc);
  169. agmt_set_last_init_status(prp->agmt, rc, 0, "Total update aborted");
  170. } else {
  171. slapi_log_error(SLAPI_LOG_FATAL, windows_repl_plugin_name, "Finished total update of replica "
  172. "\"%s\". Sent %d entries.\n", agmt_get_long_name(prp->agmt), cb_data.num_entries);
  173. agmt_set_last_init_status(prp->agmt, 0, 0, "Total update succeeded");
  174. /* Now update our consumer RUV for this agreement.
  175. * This ensures that future incrememental updates work.
  176. */
  177. agmt_set_consumer_ruv(prp->agmt, starting_ruv );
  178. }
  179. done:
  180. if (starting_ruv)
  181. {
  182. ruv_destroy(&starting_ruv);
  183. }
  184. prp->stopped = 1;
  185. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_tot_run\n", 0, 0, 0 );
  186. }
  187. static int
  188. windows_tot_stop(Private_Repl_Protocol *prp)
  189. {
  190. int return_value;
  191. int seconds = 600;
  192. PRIntervalTime start, maxwait, now;
  193. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_tot_stop\n", 0, 0, 0 );
  194. prp->terminate = 1;
  195. maxwait = PR_SecondsToInterval(seconds);
  196. start = PR_IntervalNow();
  197. now = start;
  198. while (!prp->stopped && ((now - start) < maxwait))
  199. {
  200. DS_Sleep(PR_SecondsToInterval(1));
  201. now = PR_IntervalNow();
  202. }
  203. if (!prp->stopped)
  204. {
  205. /* Isn't listening. Disconnect from the replica. */
  206. slapi_log_error (SLAPI_LOG_REPL, windows_repl_plugin_name, "windows_tot_run: "
  207. "protocol not stopped after waiting for %d seconds "
  208. "for agreement %s\n", PR_IntervalToSeconds(now-start),
  209. agmt_get_long_name(prp->agmt));
  210. conn_disconnect(prp->conn);
  211. return_value = -1;
  212. }
  213. else
  214. {
  215. return_value = 0;
  216. }
  217. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_tot_stop\n", 0, 0, 0 );
  218. return return_value;
  219. }
  220. static int
  221. windows_tot_status(Private_Repl_Protocol *prp)
  222. {
  223. int return_value = 0;
  224. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_tot_status\n", 0, 0, 0 );
  225. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_tot_status\n", 0, 0, 0 );
  226. return return_value;
  227. }
  228. static void
  229. windows_tot_noop(Private_Repl_Protocol *prp)
  230. {
  231. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_tot_noop\n", 0, 0, 0 );
  232. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_tot_noop\n", 0, 0, 0 );
  233. /* noop */
  234. }
  235. Private_Repl_Protocol *
  236. Windows_Tot_Protocol_new(Repl_Protocol *rp)
  237. {
  238. windows_tot_private *rip = NULL;
  239. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)slapi_ch_malloc(sizeof(Private_Repl_Protocol));
  240. LDAPDebug( LDAP_DEBUG_TRACE, "=> Windows_Tot_Protocol_new\n", 0, 0, 0 );
  241. prp->delete = windows_tot_delete;
  242. prp->run = windows_tot_run;
  243. prp->stop = windows_tot_stop;
  244. prp->status = windows_tot_status;
  245. prp->notify_update = windows_tot_noop;
  246. prp->notify_agmt_changed = windows_tot_noop;
  247. prp->notify_window_opened = windows_tot_noop;
  248. prp->notify_window_closed = windows_tot_noop;
  249. prp->replica_object = prot_get_replica_object(rp);
  250. prp->update_now = windows_tot_noop;
  251. if ((prp->lock = PR_NewLock()) == NULL)
  252. {
  253. goto loser;
  254. }
  255. if ((prp->cvar = PR_NewCondVar(prp->lock)) == NULL)
  256. {
  257. goto loser;
  258. }
  259. prp->stopped = 1;
  260. prp->terminate = 0;
  261. prp->eventbits = 0;
  262. prp->conn = prot_get_connection(rp);
  263. prp->agmt = prot_get_agreement(rp);
  264. rip = (void *)slapi_ch_malloc(sizeof(windows_tot_private));
  265. rip->rp = rp;
  266. prp->private = (void *)rip;
  267. prp->replica_acquired = PR_FALSE;
  268. LDAPDebug( LDAP_DEBUG_TRACE, "<= Windows_Tot_Protocol_new\n", 0, 0, 0 );
  269. return prp;
  270. loser:
  271. windows_tot_delete(&prp);
  272. LDAPDebug( LDAP_DEBUG_TRACE, "<= Windows_Tot_Protocol_new - loser\n", 0, 0, 0 );
  273. return NULL;
  274. }
  275. static void
  276. windows_tot_delete(Private_Repl_Protocol **prp)
  277. {
  278. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_tot_delete\n", 0, 0, 0 );
  279. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_tot_delete\n", 0, 0, 0 );
  280. }
  281. static
  282. void get_result (int rc, void *cb_data)
  283. {
  284. LDAPDebug( LDAP_DEBUG_TRACE, "=> get_result\n", 0, 0, 0 );
  285. PR_ASSERT (cb_data);
  286. ((callback_data*)cb_data)->rc = rc;
  287. LDAPDebug( LDAP_DEBUG_TRACE, "<= get_result\n", 0, 0, 0 );
  288. }
  289. static
  290. int send_entry (Slapi_Entry *e, void *cb_data)
  291. {
  292. int rc;
  293. Private_Repl_Protocol *prp;
  294. unsigned long *num_entriesp;
  295. time_t *sleep_on_busyp;
  296. time_t *last_busyp;
  297. LDAPDebug( LDAP_DEBUG_TRACE, "=> send_entry\n", 0, 0, 0 );
  298. PR_ASSERT (cb_data);
  299. prp = ((callback_data*)cb_data)->prp;
  300. num_entriesp = &((callback_data *)cb_data)->num_entries;
  301. sleep_on_busyp = &((callback_data *)cb_data)->sleep_on_busy;
  302. last_busyp = &((callback_data *)cb_data)->last_busy;
  303. PR_ASSERT (prp);
  304. if (prp->terminate)
  305. {
  306. conn_disconnect(prp->conn);
  307. prp->stopped = 1;
  308. ((callback_data*)cb_data)->rc = -1;
  309. LDAPDebug( LDAP_DEBUG_TRACE, "<= send_entry\n", 0, 0, 0 );
  310. return -1;
  311. }
  312. /* skip ruv tombstone - not relvant to Active Directory */
  313. if (is_ruv_tombstone_entry (e)) {
  314. LDAPDebug( LDAP_DEBUG_TRACE, "<= send_entry\n", 0, 0, 0 );
  315. return 0;
  316. }
  317. /* push the entry to the consumer */
  318. rc = windows_process_total_entry(prp,e);
  319. (*num_entriesp)++;
  320. LDAPDebug( LDAP_DEBUG_TRACE, "<= send_entry\n", 0, 0, 0 );
  321. if (CONN_OPERATION_SUCCESS == rc) {
  322. return 0;
  323. } else {
  324. ((callback_data*)cb_data)->rc = rc;
  325. return -1;
  326. }
  327. }