repl5_tot_protocol.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* repl5_tot_protocol.c */
  42. /*
  43. The tot_protocol object implements the DS 5.0 multi-master total update
  44. replication protocol, used to (re)populate a replica.
  45. */
  46. #include "repl.h"
  47. #include "repl5.h"
  48. #include "repl5_prot_private.h"
  49. /* Private data structures */
  50. typedef struct repl5_tot_private
  51. {
  52. Repl_Protocol *rp;
  53. Repl_Agmt *ra;
  54. PRLock *lock;
  55. PRUint32 eventbits;
  56. } repl5_tot_private;
  57. typedef struct operation_id_list_item
  58. {
  59. int ldap_message_id;
  60. struct operation_id_list_item *next;
  61. } operation_id_list_item;
  62. typedef struct callback_data
  63. {
  64. Private_Repl_Protocol *prp;
  65. int rc;
  66. unsigned long num_entries;
  67. time_t sleep_on_busy;
  68. time_t last_busy;
  69. PRLock *lock; /* Lock to protect access to this structure, the message id list and to force memory barriers */
  70. PRThread *result_tid; /* The async result thread */
  71. operation_id_list_item *message_id_list; /* List of IDs for outstanding operations */
  72. int abort; /* Flag used to tell the sending thread asyncronously that it should abort (because an error came up in a result) */
  73. int stop_result_thread; /* Flag used to tell the result thread to exit */
  74. int last_message_id_sent;
  75. int last_message_id_received;
  76. } callback_data;
  77. /*
  78. * Number of window seconds to wait until we programmatically decide
  79. * that the replica has got out of BUSY state
  80. */
  81. #define SLEEP_ON_BUSY_WINDOW (10)
  82. /* Helper functions */
  83. static void get_result (int rc, void *cb_data);
  84. static int send_entry (Slapi_Entry *e, void *callback_data);
  85. static void repl5_tot_delete(Private_Repl_Protocol **prp);
  86. #define LOST_CONN_ERR(xx) ((xx == -2) || (xx == LDAP_SERVER_DOWN) || (xx == LDAP_CONNECT_ERROR))
  87. /*
  88. * Notes on the async version of this code:
  89. * First, we need to have the supplier and consumer both be async-capable.
  90. * This is for two reasons : 1) We won't do any testing with mixed releases,
  91. * so even if we think it might work, we can't be sure. 2) Actually it won't
  92. * work either because we can't be sure that the consumer will not re-order
  93. * operations. Also the pre-7.1 consumer had the evil LDAP_BUSY return code,
  94. * which is incompatible with pipelineing. The 7.1 consumer has interlocks
  95. * to only process operations in transport-order, and it blocks when the
  96. * import queue is full rather than returning the LDAP_BUSY return code.
  97. * Note that it's ok to have a 7.0 supplier talk to a 7.1 consumer because
  98. * the consumer-side changes are benign to the old supplier code.
  99. */
  100. /* Code for async result reading.
  101. * This allows use of full network throughput on high-delay links,
  102. * because we don't wait for the result PDU to come back before sending the
  103. * next entry. In order to do this we need to spin up a thread to read the
  104. * results and handle any errors.
  105. */
  106. static void
  107. repl5_tot_log_operation_failure(int ldap_error, char* ldap_error_string, const char *agreement_name)
  108. {
  109. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  110. "%s: Received error %d (%s): %s for total update operation\n",
  111. agreement_name,
  112. ldap_error, ldap_err2string(ldap_error), ldap_error_string ? ldap_error_string : "");
  113. }
  114. /* Thread that collects results from async operations sent to the consumer */
  115. static void repl5_tot_result_threadmain(void *param)
  116. {
  117. callback_data *cb = (callback_data*) param;
  118. ConnResult conres = 0;
  119. Repl_Connection *conn = cb->prp->conn;
  120. int finished = 0;
  121. int connection_error = 0;
  122. char *ldap_error_string = NULL;
  123. int operation_code = 0;
  124. while (!finished)
  125. {
  126. int message_id = 0;
  127. time_t time_now = 0;
  128. time_t start_time = time( NULL );
  129. int backoff_time = 1;
  130. /* Read the next result */
  131. /* We call the get result function with a short timeout (non-blocking)
  132. * this is so we don't block here forever, and can stop this thread when
  133. * the time comes. However, we do need to implement blocking with timeout
  134. * semantics here instead.
  135. */
  136. while (!finished)
  137. {
  138. conres = conn_read_result_ex(conn, NULL, NULL, NULL, LDAP_RES_ANY, &message_id, 0);
  139. /* Timeout here means that we didn't block, not a real timeout */
  140. if (CONN_TIMEOUT == conres)
  141. {
  142. /* We need to a) check that the 'real' timeout hasn't expired and
  143. * b) implement a backoff sleep to avoid spinning */
  144. /* Did the connection's timeout expire ? */
  145. time_now = time( NULL );
  146. if (conn_get_timeout(conn) <= ( time_now - start_time ))
  147. {
  148. /* We timed out */
  149. conres = CONN_TIMEOUT;
  150. break;
  151. }
  152. /* Otherwise we backoff */
  153. DS_Sleep(PR_MillisecondsToInterval(backoff_time));
  154. if (backoff_time < 1000)
  155. {
  156. backoff_time <<= 1;
  157. }
  158. /* Should we stop ? */
  159. PR_Lock(cb->lock);
  160. if (cb->stop_result_thread)
  161. {
  162. finished = 1;
  163. }
  164. PR_Unlock(cb->lock);
  165. } else
  166. {
  167. /* Something other than a timeout, so we exit the loop */
  168. break;
  169. }
  170. }
  171. if (message_id)
  172. {
  173. cb->last_message_id_received = message_id;
  174. }
  175. conn_get_error_ex(conn, &operation_code, &connection_error, &ldap_error_string);
  176. if (connection_error && connection_error != LDAP_TIMEOUT)
  177. {
  178. repl5_tot_log_operation_failure(connection_error,ldap_error_string,agmt_get_long_name(cb->prp->agmt));
  179. }
  180. /* Was the result itself an error ? */
  181. if (0 != conres)
  182. {
  183. /* If so then we need to take steps to abort the update process */
  184. PR_Lock(cb->lock);
  185. cb->abort = 1;
  186. PR_Unlock(cb->lock);
  187. }
  188. /* Should we stop ? */
  189. PR_Lock(cb->lock);
  190. /* if the connection is not connected, then we cannot read any more
  191. results - we are finished */
  192. if (cb->stop_result_thread || (conres == CONN_NOT_CONNECTED))
  193. {
  194. finished = 1;
  195. }
  196. PR_Unlock(cb->lock);
  197. }
  198. }
  199. static int repl5_tot_create_async_result_thread(callback_data *cb_data)
  200. {
  201. int retval = 0;
  202. PRThread *tid = NULL;
  203. /* Create a thread that reads results from the connection and stores status in the callback_data structure */
  204. tid = PR_CreateThread(PR_USER_THREAD,
  205. repl5_tot_result_threadmain, (void*)cb_data,
  206. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD,
  207. SLAPD_DEFAULT_THREAD_STACKSIZE);
  208. if (NULL == tid)
  209. {
  210. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  211. "repl5_tot_create_async_result_thread failed. "
  212. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  213. PR_GetError(), slapd_pr_strerror( PR_GetError() ));
  214. retval = -1;
  215. } else {
  216. cb_data->result_tid = tid;
  217. }
  218. return retval;
  219. }
  220. static int repl5_tot_destroy_async_result_thread(callback_data *cb_data)
  221. {
  222. int retval = 0;
  223. PRThread *tid = cb_data->result_tid;
  224. if (tid) {
  225. PR_Lock(cb_data->lock);
  226. cb_data->stop_result_thread = 1;
  227. PR_Unlock(cb_data->lock);
  228. (void)PR_JoinThread(tid);
  229. }
  230. return retval;
  231. }
  232. /* Called when in compatibility mode, to get the next result from the wire
  233. * The operation thread will not send a second operation until it has read the
  234. * previous result. */
  235. static int
  236. repl5_tot_get_next_result(callback_data *cb_data)
  237. {
  238. ConnResult conres = 0;
  239. int message_id = 0;
  240. int connection_error = 0;
  241. char *ldap_error_string = NULL;
  242. int operation_code = 0;
  243. /* Wait on the next result */
  244. conres = conn_read_result(cb_data->prp->conn, &message_id);
  245. conn_get_error_ex(cb_data->prp->conn, &operation_code, &connection_error, &ldap_error_string);
  246. if (connection_error)
  247. {
  248. repl5_tot_log_operation_failure(connection_error,ldap_error_string,agmt_get_long_name(cb_data->prp->agmt));
  249. }
  250. /* Return it to the caller */
  251. return conres;
  252. }
  253. static void
  254. repl5_tot_waitfor_async_results(callback_data *cb_data)
  255. {
  256. int done = 0;
  257. int loops = 0;
  258. /* Keep pulling results off the LDAP connection until we catch up to the last message id stored in the rd */
  259. while (!done)
  260. {
  261. /* Lock the structure to force memory barrier */
  262. PR_Lock(cb_data->lock);
  263. /* Are we caught up ? */
  264. slapi_log_error(SLAPI_LOG_REPL, NULL,
  265. "repl5_tot_waitfor_async_results: %d %d\n",
  266. cb_data->last_message_id_received, cb_data->last_message_id_sent);
  267. if (cb_data->last_message_id_received >= cb_data->last_message_id_sent)
  268. {
  269. /* If so then we're done */
  270. done = 1;
  271. }
  272. if (cb_data->abort && LOST_CONN_ERR(cb_data->rc))
  273. {
  274. done = 1; /* no connection == no more results */
  275. }
  276. PR_Unlock(cb_data->lock);
  277. /* If not then sleep a bit */
  278. DS_Sleep(PR_SecondsToInterval(1));
  279. loops++;
  280. /* If we sleep forever then we can conclude that something bad happened, and bail... */
  281. /* Arbitrary 30 second delay : basically we should only expect to wait as long as it takes to process a few operations, which should be on the order of a second at most */
  282. if (!done && (loops > 300))
  283. {
  284. /* Log a warning */
  285. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  286. "repl5_tot_waitfor_async_results timed out waiting for responses: %d %d\n",
  287. cb_data->last_message_id_received, cb_data->last_message_id_sent);
  288. done = 1;
  289. }
  290. }
  291. }
  292. /*
  293. * Completely refresh a replica. The basic protocol interaction goes
  294. * like this:
  295. * - Acquire Replica by sending a StartReplicationRequest extop, with the
  296. * total update protocol OID and supplier's ruv.
  297. * - Send a series of extended operations containing entries.
  298. * - send an EndReplicationRequest extended operation
  299. */
  300. static void
  301. repl5_tot_run(Private_Repl_Protocol *prp)
  302. {
  303. int rc;
  304. callback_data cb_data = {0};
  305. Slapi_PBlock *pb;
  306. LDAPControl **ctrls;
  307. char *hostname = NULL;
  308. int portnum = 0;
  309. Slapi_DN *area_sdn = NULL;
  310. CSN *remote_schema_csn = NULL;
  311. PR_ASSERT(NULL != prp);
  312. prp->stopped = 0;
  313. if (prp->terminate)
  314. {
  315. prp->stopped = 1;
  316. goto done;
  317. }
  318. conn_set_timeout(prp->conn, agmt_get_timeout(prp->agmt));
  319. /* acquire remote replica */
  320. agmt_set_last_init_start(prp->agmt, current_time());
  321. rc = acquire_replica (prp, REPL_NSDS50_TOTAL_PROTOCOL_OID, NULL /* ruv */);
  322. /* We never retry total protocol, even in case a transient error.
  323. This is because if somebody already updated the replica we don't
  324. want to do it again */
  325. if (rc != ACQUIRE_SUCCESS)
  326. {
  327. int optype, ldaprc;
  328. conn_get_error(prp->conn, &optype, &ldaprc);
  329. agmt_set_last_init_status(prp->agmt, ldaprc,
  330. prp->last_acquire_response_code, NULL);
  331. goto done;
  332. }
  333. else if (prp->terminate)
  334. {
  335. conn_disconnect(prp->conn);
  336. prp->stopped = 1;
  337. goto done;
  338. }
  339. hostname = agmt_get_hostname(prp->agmt);
  340. portnum = agmt_get_port(prp->agmt);
  341. agmt_set_last_init_status(prp->agmt, 0, 0, "Total schema update in progress");
  342. remote_schema_csn = agmt_get_consumer_schema_csn ( prp->agmt );
  343. rc = conn_push_schema(prp->conn, &remote_schema_csn);
  344. if (remote_schema_csn != agmt_get_consumer_schema_csn ( prp->agmt )) {
  345. csn_free(&remote_schema_csn);
  346. }
  347. if (CONN_SCHEMA_UPDATED != rc && CONN_SCHEMA_NO_UPDATE_NEEDED != rc)
  348. {
  349. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Warning: unable to "
  350. "replicate schema to host %s, port %d. Continuing with "
  351. "total update session.\n",
  352. hostname, portnum);
  353. /* But keep going */
  354. agmt_set_last_init_status(prp->agmt, 0, rc, "Total schema update failed");
  355. }
  356. else
  357. {
  358. agmt_set_last_init_status(prp->agmt, 0, 0, "Total schema update succeeded");
  359. }
  360. /* ONREPL - big assumption here is that entries a returned in the id order
  361. and that the order implies that perent entry is always ahead of the
  362. child entry in the list. Otherwise, the consumer would not be
  363. properly updated because bulk import at the moment skips orphand entries. */
  364. /* XXXggood above assumption may not be valid if orphaned entry moved???? */
  365. agmt_set_last_init_status(prp->agmt, 0, 0, "Total update in progress");
  366. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Beginning total update of replica "
  367. "\"%s\".\n", agmt_get_long_name(prp->agmt));
  368. pb = slapi_pblock_new ();
  369. /* RMREPL - need to send schema here */
  370. area_sdn = agmt_get_replarea(prp->agmt);
  371. /* we need to provide managedsait control so that referral entries can
  372. be replicated */
  373. ctrls = (LDAPControl **)slapi_ch_calloc (3, sizeof (LDAPControl *));
  374. ctrls[0] = create_managedsait_control ();
  375. ctrls[1] = create_backend_control(area_sdn);
  376. slapi_search_internal_set_pb (pb, slapi_sdn_get_dn (area_sdn),
  377. LDAP_SCOPE_SUBTREE, "(|(objectclass=ldapsubentry)(objectclass=nstombstone)(nsuniqueid=*))", NULL, 0, ctrls, NULL,
  378. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  379. cb_data.prp = prp;
  380. cb_data.rc = 0;
  381. cb_data.num_entries = 0UL;
  382. cb_data.sleep_on_busy = 0UL;
  383. cb_data.last_busy = current_time ();
  384. cb_data.lock = PR_NewLock();
  385. /* Before we get started on sending entries to the replica, we need to
  386. * setup things for async propagation:
  387. * 1. Create a thread that will read the LDAP results from the connection.
  388. * 2. Anything else ?
  389. */
  390. if (!prp->repl50consumer)
  391. {
  392. rc = repl5_tot_create_async_result_thread(&cb_data);
  393. if (rc) {
  394. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name, "%s: repl5_tot_run: "
  395. "repl5_tot_create_async_result_thread failed; error - %d\n",
  396. agmt_get_long_name(prp->agmt), rc);
  397. goto done;
  398. }
  399. }
  400. /* this search get all the entries from the replicated area including tombstones
  401. and referrals */
  402. slapi_search_internal_callback_pb (pb, &cb_data /* callback data */,
  403. get_result /* result callback */,
  404. send_entry /* entry callback */,
  405. NULL /* referral callback*/);
  406. /*
  407. * After completing the sending operation (or optionally failing), we need to clean up
  408. * the async propagation stuff:
  409. * 1. Stop the thread that collects LDAP results from the connection.
  410. * 2. Anything else ?
  411. */
  412. if (!prp->repl50consumer)
  413. {
  414. repl5_tot_waitfor_async_results(&cb_data);
  415. rc = repl5_tot_destroy_async_result_thread(&cb_data);
  416. if (rc) {
  417. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name, "%s: repl5_tot_run: "
  418. "repl5_tot_destroy_async_result_thread failed; error - %d\n",
  419. agmt_get_long_name(prp->agmt), rc);
  420. }
  421. }
  422. /* From here on, things are the same as in the old sync code :
  423. * the entire total update either succeeded, or it failed.
  424. * If it failed, then cb_data.rc contains the error code, and
  425. * suitable messages will have been logged to the error log about the failure.
  426. */
  427. slapi_pblock_destroy (pb);
  428. agmt_set_last_init_end(prp->agmt, current_time());
  429. rc = cb_data.rc;
  430. agmt_set_update_in_progress(prp->agmt, PR_FALSE);
  431. agmt_update_done(prp->agmt, 1);
  432. release_replica(prp);
  433. if (rc != LDAP_SUCCESS)
  434. {
  435. slapi_log_error (SLAPI_LOG_REPL, repl_plugin_name, "%s: repl5_tot_run: "
  436. "failed to obtain data to send to the consumer; LDAP error - %d\n",
  437. agmt_get_long_name(prp->agmt), rc);
  438. agmt_set_last_init_status(prp->agmt, rc, 0, "Total update aborted");
  439. } else {
  440. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Finished total update of replica "
  441. "\"%s\". Sent %lu entries.\n", agmt_get_long_name(prp->agmt), cb_data.num_entries);
  442. agmt_set_last_init_status(prp->agmt, 0, 0, "Total update succeeded");
  443. }
  444. done:
  445. slapi_sdn_free(&area_sdn);
  446. slapi_ch_free_string(&hostname);
  447. if (cb_data.lock)
  448. {
  449. PR_DestroyLock(cb_data.lock);
  450. }
  451. prp->stopped = 1;
  452. }
  453. static int
  454. repl5_tot_stop(Private_Repl_Protocol *prp)
  455. {
  456. int return_value;
  457. int seconds = 600;
  458. PRIntervalTime start, maxwait, now;
  459. prp->terminate = 1;
  460. maxwait = PR_SecondsToInterval(seconds);
  461. start = PR_IntervalNow();
  462. now = start;
  463. while (!prp->stopped && ((now - start) < maxwait))
  464. {
  465. DS_Sleep(PR_SecondsToInterval(1));
  466. now = PR_IntervalNow();
  467. }
  468. if (!prp->stopped)
  469. {
  470. /* Isn't listening. Disconnect from the replica. */
  471. slapi_log_error (SLAPI_LOG_REPL, repl_plugin_name, "repl5_tot_run: "
  472. "protocol not stopped after waiting for %d seconds "
  473. "for agreement %s\n", PR_IntervalToSeconds(now-start),
  474. agmt_get_long_name(prp->agmt));
  475. conn_disconnect(prp->conn);
  476. return_value = -1;
  477. }
  478. else
  479. {
  480. return_value = 0;
  481. }
  482. return return_value;
  483. }
  484. static int
  485. repl5_tot_status(Private_Repl_Protocol *prp)
  486. {
  487. int return_value = 0;
  488. return return_value;
  489. }
  490. static void
  491. repl5_tot_noop(Private_Repl_Protocol *prp)
  492. {
  493. /* noop */
  494. }
  495. Private_Repl_Protocol *
  496. Repl_5_Tot_Protocol_new(Repl_Protocol *rp)
  497. {
  498. repl5_tot_private *rip = NULL;
  499. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)slapi_ch_malloc(sizeof(Private_Repl_Protocol));
  500. prp->delete = repl5_tot_delete;
  501. prp->run = repl5_tot_run;
  502. prp->stop = repl5_tot_stop;
  503. prp->status = repl5_tot_status;
  504. prp->notify_update = repl5_tot_noop;
  505. prp->notify_agmt_changed = repl5_tot_noop;
  506. prp->notify_window_opened = repl5_tot_noop;
  507. prp->notify_window_closed = repl5_tot_noop;
  508. prp->update_now = repl5_tot_noop;
  509. prp->timeout = DEFAULT_PROTOCOL_TIMEOUT;
  510. if ((prp->lock = PR_NewLock()) == NULL)
  511. {
  512. goto loser;
  513. }
  514. if ((prp->cvar = PR_NewCondVar(prp->lock)) == NULL)
  515. {
  516. goto loser;
  517. }
  518. prp->stopped = 1;
  519. prp->terminate = 0;
  520. prp->eventbits = 0;
  521. prp->conn = prot_get_connection(rp);
  522. prp->agmt = prot_get_agreement(rp);
  523. rip = (void *)slapi_ch_malloc(sizeof(repl5_tot_private));
  524. rip->rp = rp;
  525. prp->private = (void *)rip;
  526. prp->replica_acquired = PR_FALSE;
  527. prp->repl50consumer = 0;
  528. prp->repl71consumer = 0;
  529. prp->repl90consumer = 0;
  530. return prp;
  531. loser:
  532. repl5_tot_delete(&prp);
  533. return NULL;
  534. }
  535. static void
  536. repl5_tot_delete(Private_Repl_Protocol **prpp)
  537. {
  538. /* First, stop the protocol if it isn't already stopped */
  539. if (!(*prpp)->stopped) {
  540. (*prpp)->stopped = 1;
  541. (*prpp)->stop(*prpp);
  542. }
  543. /* Then, delete all resources used by the protocol */
  544. if ((*prpp)->lock) {
  545. PR_DestroyLock((*prpp)->lock);
  546. (*prpp)->lock = NULL;
  547. }
  548. if ((*prpp)->cvar) {
  549. PR_DestroyCondVar((*prpp)->cvar);
  550. (*prpp)->cvar = NULL;
  551. }
  552. slapi_ch_free((void **)&(*prpp)->private);
  553. slapi_ch_free((void **)prpp);
  554. }
  555. static
  556. void get_result (int rc, void *cb_data)
  557. {
  558. PR_ASSERT (cb_data);
  559. ((callback_data*)cb_data)->rc = rc;
  560. }
  561. static
  562. int send_entry (Slapi_Entry *e, void *cb_data)
  563. {
  564. int rc;
  565. Private_Repl_Protocol *prp;
  566. BerElement *bere;
  567. struct berval *bv;
  568. unsigned long *num_entriesp;
  569. time_t *sleep_on_busyp;
  570. time_t *last_busyp;
  571. int message_id = 0;
  572. int retval = 0;
  573. char **frac_excluded_attrs = NULL;
  574. PR_ASSERT (cb_data);
  575. prp = ((callback_data*)cb_data)->prp;
  576. num_entriesp = &((callback_data *)cb_data)->num_entries;
  577. sleep_on_busyp = &((callback_data *)cb_data)->sleep_on_busy;
  578. last_busyp = &((callback_data *)cb_data)->last_busy;
  579. PR_ASSERT (prp);
  580. if (prp->terminate)
  581. {
  582. conn_disconnect(prp->conn);
  583. prp->stopped = 1;
  584. ((callback_data*)cb_data)->rc = -1;
  585. return -1;
  586. }
  587. /* see if the result reader thread encountered
  588. a fatal error */
  589. PR_Lock(((callback_data*)cb_data)->lock);
  590. rc = ((callback_data*)cb_data)->abort;
  591. PR_Unlock(((callback_data*)cb_data)->lock);
  592. if (rc)
  593. {
  594. conn_disconnect(prp->conn);
  595. prp->stopped = 1;
  596. ((callback_data*)cb_data)->rc = -1;
  597. return -1;
  598. }
  599. /* skip ruv tombstone - need to do this because it might be
  600. more up to date then the data we are sending to the client.
  601. RUV is sent separately via the protocol */
  602. if (is_ruv_tombstone_entry (e))
  603. return 0;
  604. /* ONREPL we would purge copiedFrom and copyingFrom here but I decided against it.
  605. Instead, it will get removed when this replica stops being 4.0 consumer and
  606. then propagated to all its consumer */
  607. if (agmt_is_fractional(prp->agmt))
  608. {
  609. frac_excluded_attrs = agmt_get_fractional_attrs_total(prp->agmt);
  610. }
  611. /* convert the entry to the on the wire format */
  612. bere = entry2bere(e,frac_excluded_attrs);
  613. if (frac_excluded_attrs)
  614. {
  615. slapi_ch_array_free(frac_excluded_attrs);
  616. }
  617. if (bere == NULL)
  618. {
  619. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "%s: send_entry: Encoding Error\n",
  620. agmt_get_long_name(prp->agmt));
  621. ((callback_data*)cb_data)->rc = -1;
  622. retval = -1;
  623. goto error;
  624. }
  625. rc = ber_flatten(bere, &bv);
  626. ber_free (bere, 1);
  627. if (rc != 0)
  628. {
  629. ((callback_data*)cb_data)->rc = -1;
  630. retval = -1;
  631. goto error;
  632. }
  633. do {
  634. /* push the entry to the consumer */
  635. rc = conn_send_extended_operation(prp->conn, REPL_NSDS50_REPLICATION_ENTRY_REQUEST_OID,
  636. bv /* payload */, NULL /* update_control */, &message_id);
  637. if (message_id)
  638. {
  639. ((callback_data*)cb_data)->last_message_id_sent = message_id;
  640. }
  641. /* If we are talking to a 5.0 type consumer, we need to wait here and retrieve the
  642. * response. Reason is that it can return LDAP_BUSY, indicating that its queue has
  643. * filled up. This completely breaks pipelineing, and so we need to fall back to
  644. * sync transmission for those consumers, in case they pull the LDAP_BUSY stunt on us :( */
  645. if (prp->repl50consumer)
  646. {
  647. /* Get the response here */
  648. rc = repl5_tot_get_next_result((callback_data*)cb_data);
  649. }
  650. if (rc == CONN_BUSY) {
  651. time_t now = current_time ();
  652. if ((now - *last_busyp) < (*sleep_on_busyp + 10)) {
  653. *sleep_on_busyp +=5;
  654. }
  655. else {
  656. *sleep_on_busyp = 5;
  657. }
  658. *last_busyp = now;
  659. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  660. "Replica \"%s\" is busy. Waiting %lds while"
  661. " it finishes processing its current import queue\n",
  662. agmt_get_long_name(prp->agmt), *sleep_on_busyp);
  663. DS_Sleep(PR_SecondsToInterval(*sleep_on_busyp));
  664. }
  665. }
  666. while (rc == CONN_BUSY);
  667. ber_bvfree(bv);
  668. (*num_entriesp)++;
  669. /* if the connection has been closed, we need to stop
  670. sending entries and set a special rc value to let
  671. the result reading thread know the connection has been
  672. closed - do not attempt to read any more results */
  673. if (CONN_NOT_CONNECTED == rc) {
  674. ((callback_data*)cb_data)->rc = -2;
  675. retval = -1;
  676. } else if (CONN_OPERATION_SUCCESS == rc) {
  677. retval = 0;
  678. } else {
  679. ((callback_data*)cb_data)->rc = rc;
  680. retval = -1;
  681. }
  682. error:
  683. return retval;
  684. }