repl5_tot_protocol.c 22 KB

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