repl5_tot_protocol.c 25 KB

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