repl5_tot_protocol.c 29 KB

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