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. * 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. if (slapi_is_loglevel_set(SLAPI_LOG_REPL))
  175. {
  176. slapi_log_error(SLAPI_LOG_REPL, NULL, "total update setting consumer RUV:\n");
  177. ruv_dump (starting_ruv, "consumer", NULL);
  178. }
  179. agmt_set_consumer_ruv(prp->agmt, starting_ruv );
  180. }
  181. done:
  182. if (starting_ruv)
  183. {
  184. ruv_destroy(&starting_ruv);
  185. }
  186. prp->stopped = 1;
  187. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_tot_run\n", 0, 0, 0 );
  188. }
  189. static int
  190. windows_tot_stop(Private_Repl_Protocol *prp)
  191. {
  192. int return_value;
  193. int seconds = 600;
  194. PRIntervalTime start, maxwait, now;
  195. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_tot_stop\n", 0, 0, 0 );
  196. prp->terminate = 1;
  197. maxwait = PR_SecondsToInterval(seconds);
  198. start = PR_IntervalNow();
  199. now = start;
  200. while (!prp->stopped && ((now - start) < maxwait))
  201. {
  202. DS_Sleep(PR_SecondsToInterval(1));
  203. now = PR_IntervalNow();
  204. }
  205. if (!prp->stopped)
  206. {
  207. /* Isn't listening. Disconnect from the replica. */
  208. slapi_log_error (SLAPI_LOG_REPL, windows_repl_plugin_name, "windows_tot_run: "
  209. "protocol not stopped after waiting for %d seconds "
  210. "for agreement %s\n", PR_IntervalToSeconds(now-start),
  211. agmt_get_long_name(prp->agmt));
  212. conn_disconnect(prp->conn);
  213. return_value = -1;
  214. }
  215. else
  216. {
  217. return_value = 0;
  218. }
  219. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_tot_stop\n", 0, 0, 0 );
  220. return return_value;
  221. }
  222. static int
  223. windows_tot_status(Private_Repl_Protocol *prp)
  224. {
  225. int return_value = 0;
  226. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_tot_status\n", 0, 0, 0 );
  227. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_tot_status\n", 0, 0, 0 );
  228. return return_value;
  229. }
  230. static void
  231. windows_tot_noop(Private_Repl_Protocol *prp)
  232. {
  233. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_tot_noop\n", 0, 0, 0 );
  234. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_tot_noop\n", 0, 0, 0 );
  235. /* noop */
  236. }
  237. Private_Repl_Protocol *
  238. Windows_Tot_Protocol_new(Repl_Protocol *rp)
  239. {
  240. windows_tot_private *rip = NULL;
  241. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)slapi_ch_malloc(sizeof(Private_Repl_Protocol));
  242. LDAPDebug( LDAP_DEBUG_TRACE, "=> Windows_Tot_Protocol_new\n", 0, 0, 0 );
  243. prp->delete = windows_tot_delete;
  244. prp->run = windows_tot_run;
  245. prp->stop = windows_tot_stop;
  246. prp->status = windows_tot_status;
  247. prp->notify_update = windows_tot_noop;
  248. prp->notify_agmt_changed = windows_tot_noop;
  249. prp->notify_window_opened = windows_tot_noop;
  250. prp->notify_window_closed = windows_tot_noop;
  251. prp->replica_object = prot_get_replica_object(rp);
  252. prp->update_now = windows_tot_noop;
  253. if ((prp->lock = PR_NewLock()) == NULL)
  254. {
  255. goto loser;
  256. }
  257. if ((prp->cvar = PR_NewCondVar(prp->lock)) == NULL)
  258. {
  259. goto loser;
  260. }
  261. prp->stopped = 1;
  262. prp->terminate = 0;
  263. prp->eventbits = 0;
  264. prp->conn = prot_get_connection(rp);
  265. prp->agmt = prot_get_agreement(rp);
  266. rip = (void *)slapi_ch_malloc(sizeof(windows_tot_private));
  267. rip->rp = rp;
  268. prp->private = (void *)rip;
  269. prp->replica_acquired = PR_FALSE;
  270. LDAPDebug( LDAP_DEBUG_TRACE, "<= Windows_Tot_Protocol_new\n", 0, 0, 0 );
  271. return prp;
  272. loser:
  273. windows_tot_delete(&prp);
  274. LDAPDebug( LDAP_DEBUG_TRACE, "<= Windows_Tot_Protocol_new - loser\n", 0, 0, 0 );
  275. return NULL;
  276. }
  277. static void
  278. windows_tot_delete(Private_Repl_Protocol **prp)
  279. {
  280. LDAPDebug( LDAP_DEBUG_TRACE, "=> windows_tot_delete\n", 0, 0, 0 );
  281. LDAPDebug( LDAP_DEBUG_TRACE, "<= windows_tot_delete\n", 0, 0, 0 );
  282. }
  283. static
  284. void get_result (int rc, void *cb_data)
  285. {
  286. LDAPDebug( LDAP_DEBUG_TRACE, "=> get_result\n", 0, 0, 0 );
  287. PR_ASSERT (cb_data);
  288. ((callback_data*)cb_data)->rc = rc;
  289. LDAPDebug( LDAP_DEBUG_TRACE, "<= get_result\n", 0, 0, 0 );
  290. }
  291. static
  292. int send_entry (Slapi_Entry *e, void *cb_data)
  293. {
  294. int rc;
  295. Private_Repl_Protocol *prp;
  296. unsigned long *num_entriesp;
  297. time_t *sleep_on_busyp;
  298. time_t *last_busyp;
  299. LDAPDebug( LDAP_DEBUG_TRACE, "=> send_entry\n", 0, 0, 0 );
  300. PR_ASSERT (cb_data);
  301. prp = ((callback_data*)cb_data)->prp;
  302. num_entriesp = &((callback_data *)cb_data)->num_entries;
  303. sleep_on_busyp = &((callback_data *)cb_data)->sleep_on_busy;
  304. last_busyp = &((callback_data *)cb_data)->last_busy;
  305. PR_ASSERT (prp);
  306. if (prp->terminate)
  307. {
  308. conn_disconnect(prp->conn);
  309. prp->stopped = 1;
  310. ((callback_data*)cb_data)->rc = -1;
  311. LDAPDebug( LDAP_DEBUG_TRACE, "<= send_entry\n", 0, 0, 0 );
  312. return -1;
  313. }
  314. /* skip ruv tombstone - not relvant to Active Directory */
  315. if (is_ruv_tombstone_entry (e)) {
  316. LDAPDebug( LDAP_DEBUG_TRACE, "<= send_entry\n", 0, 0, 0 );
  317. return 0;
  318. }
  319. /* push the entry to the consumer */
  320. rc = windows_process_total_entry(prp,e);
  321. (*num_entriesp)++;
  322. LDAPDebug( LDAP_DEBUG_TRACE, "<= send_entry\n", 0, 0, 0 );
  323. if (CONN_OPERATION_SUCCESS == rc) {
  324. return 0;
  325. } else {
  326. ((callback_data*)cb_data)->rc = rc;
  327. return -1;
  328. }
  329. }