repl5_tot_protocol.c 26 KB

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