windows_tot_protocol.c 12 KB

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