repl5_inc_protocol.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382
  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_inc_protocol.c */
  42. /*
  43. The Prot_Incremental object implements the DS 5.0 multi-master incremental
  44. replication protocol.
  45. Stuff to do:
  46. - Need to figure out how asynchronous events end up in here. They are:
  47. - entry updated in replicated area.
  48. - backoff timeout
  49. - enter/leave.
  50. Perhaps these events should be properties of the main protocol.
  51. */
  52. #include <plstr.h>
  53. #include "repl.h"
  54. #include "repl5.h"
  55. #include "repl5_ruv.h"
  56. #include "repl5_prot_private.h"
  57. #include "cl5_api.h"
  58. extern int slapi_log_urp;
  59. /*** from proto-slap.h ***/
  60. void ava_done(struct ava *ava);
  61. typedef struct repl5_inc_private
  62. {
  63. char *ruv; /* RUV on remote replica (use diff type for this? - ggood */
  64. Backoff_Timer *backoff;
  65. Repl_Protocol *rp;
  66. PRLock *lock;
  67. PRUint32 eventbits;
  68. } repl5_inc_private;
  69. /* Structures used to communicate with the result reading thread */
  70. #ifndef UIDSTR_SIZE
  71. #define UIDSTR_SIZE 35 /* size of the string representation of the id */
  72. #endif
  73. typedef struct repl5_inc_operation
  74. {
  75. int ldap_message_id;
  76. unsigned long operation_type;
  77. char csn_str[CSN_STRSIZE];
  78. char uniqueid[UIDSTR_SIZE+1];
  79. ReplicaId replica_id;
  80. struct repl5_inc_operation *next;
  81. } repl5_inc_operation;
  82. typedef struct result_data
  83. {
  84. Private_Repl_Protocol *prp;
  85. int rc;
  86. PRLock *lock; /* Lock to protect access to this structure, the message id list and to force memory barriers */
  87. PRThread *result_tid; /* The async result thread */
  88. repl5_inc_operation *operation_list_head; /* List of IDs for outstanding operations */
  89. repl5_inc_operation *operation_list_tail; /* List of IDs for outstanding operations */
  90. int abort; /* Flag used to tell the sending thread asyncronously that it should abort (because an error came up in a result) */
  91. PRUint32 num_changes_sent;
  92. int stop_result_thread; /* Flag used to tell the result thread to exit */
  93. int last_message_id_sent;
  94. int last_message_id_received;
  95. int flowcontrol_detection;
  96. int result; /* The UPDATE_TRANSIENT_ERROR etc */
  97. } result_data;
  98. /* Various states the incremental protocol can pass through */
  99. #define STATE_START 0 /* ONREPL - should we rename this - we don't use it just to start up? */
  100. #define STATE_WAIT_WINDOW_OPEN 1
  101. #define STATE_WAIT_CHANGES 2
  102. #define STATE_READY_TO_ACQUIRE 3
  103. #define STATE_BACKOFF_START 4 /* ONREPL - can we combine BACKOFF_START and BACKOFF states? */
  104. #define STATE_BACKOFF 5
  105. #define STATE_SENDING_UPDATES 6
  106. #define STATE_STOP_FATAL_ERROR 7
  107. #define STATE_STOP_FATAL_ERROR_PART2 8
  108. #define STATE_STOP_NORMAL_TERMINATION 9
  109. /* Events (synchronous and asynchronous; these are bits) */
  110. #define EVENT_WINDOW_OPENED 1
  111. #define EVENT_WINDOW_CLOSED 2
  112. #define EVENT_TRIGGERING_CRITERIA_MET 4 /* ONREPL - should we rename this to EVENT_CHANGE_AVAILABLE */
  113. #define EVENT_BACKOFF_EXPIRED 8
  114. #define EVENT_REPLICATE_NOW 16
  115. #define EVENT_PROTOCOL_SHUTDOWN 32
  116. #define EVENT_AGMT_CHANGED 64
  117. #define UPDATE_NO_MORE_UPDATES 201
  118. #define UPDATE_TRANSIENT_ERROR 202
  119. #define UPDATE_FATAL_ERROR 203
  120. #define UPDATE_SCHEDULE_WINDOW_CLOSED 204
  121. #define UPDATE_CONNECTION_LOST 205
  122. #define UPDATE_TIMEOUT 206
  123. #define UPDATE_YIELD 207
  124. /* Return codes from examine_update_vector */
  125. #define EXAMINE_RUV_PRISTINE_REPLICA 401
  126. #define EXAMINE_RUV_GENERATION_MISMATCH 402
  127. #define EXAMINE_RUV_REPLICA_TOO_OLD 403
  128. #define EXAMINE_RUV_OK 404
  129. #define EXAMINE_RUV_PARAM_ERROR 405
  130. #define MAX_CHANGES_PER_SESSION 10000
  131. /*
  132. * Maximum time to wait between replication sessions. If we
  133. * don't see any updates for a period equal to this interval,
  134. * we go ahead and start a replication session, just to be safe
  135. */
  136. #define MAX_WAIT_BETWEEN_SESSIONS PR_SecondsToInterval(60 * 5) /* 5 minutes */
  137. /*
  138. * tests if the protocol has been shutdown and we need to quit
  139. * event_occurred resets the bits in the bit flag, so whoever tests for shutdown
  140. * resets the flags, so the next one who tests for shutdown won't get it, so we
  141. * also look at the terminate flag
  142. */
  143. #define PROTOCOL_IS_SHUTDOWN(prp) (event_occurred(prp, EVENT_PROTOCOL_SHUTDOWN) || prp->terminate)
  144. /* mods should be LDAPMod **mods */
  145. #define MODS_ARE_EMPTY(mods) ((mods == NULL) || (mods[0] == NULL))
  146. /* Forward declarations */
  147. static PRUint32 event_occurred(Private_Repl_Protocol *prp, PRUint32 event);
  148. static void reset_events (Private_Repl_Protocol *prp);
  149. static void protocol_sleep(Private_Repl_Protocol *prp, PRIntervalTime duration);
  150. static int send_updates(Private_Repl_Protocol *prp, RUV *ruv, PRUint32 *num_changes_sent);
  151. static void repl5_inc_backoff_expired(time_t timer_fire_time, void *arg);
  152. static int examine_update_vector(Private_Repl_Protocol *prp, RUV *ruv);
  153. static PRBool ignore_error_and_keep_going(int error);
  154. static const char* state2name (int state);
  155. static const char* event2name (int event);
  156. static const char* op2string (int op);
  157. static int repl5_inc_update_from_op_result(Private_Repl_Protocol *prp, ConnResult replay_crc, int connection_error, char *csn_str, char *uniqueid, ReplicaId replica_id, int* finished, PRUint32 *num_changes_sent);
  158. /* Push a newly sent operation onto the tail of the list */
  159. static void repl5_int_push_operation(result_data *rd, repl5_inc_operation *it)
  160. {
  161. repl5_inc_operation *tail = NULL;
  162. PR_Lock(rd->lock);
  163. tail = rd->operation_list_tail;
  164. if (tail)
  165. {
  166. tail->next = it;
  167. }
  168. if (NULL == rd->operation_list_head)
  169. {
  170. rd->operation_list_head = it;
  171. }
  172. rd->operation_list_tail = it;
  173. PR_Unlock(rd->lock);
  174. }
  175. /* Pop the next operation in line to respond from the list */
  176. /* The caller is expected to free the operation item */
  177. static repl5_inc_operation *repl5_inc_pop_operation(result_data *rd)
  178. {
  179. repl5_inc_operation *head = NULL;
  180. repl5_inc_operation *ret = NULL;
  181. PR_Lock(rd->lock);
  182. head = rd->operation_list_head;
  183. if (head)
  184. {
  185. ret = head;
  186. rd->operation_list_head = head->next;
  187. if (rd->operation_list_tail == head)
  188. {
  189. rd->operation_list_tail = NULL;
  190. }
  191. }
  192. PR_Unlock(rd->lock);
  193. return ret;
  194. }
  195. static void
  196. repl5_inc_op_free(repl5_inc_operation *op)
  197. {
  198. slapi_ch_free((void**)&op);
  199. }
  200. static repl5_inc_operation *repl5_inc_operation_new()
  201. {
  202. repl5_inc_operation *ret = NULL;
  203. ret = (repl5_inc_operation *) slapi_ch_calloc(1,sizeof(repl5_inc_operation));
  204. return ret;
  205. }
  206. /* Called when in compatibility mode, to get the next result from the wire
  207. * The operation thread will not send a second operation until it has read the
  208. * previous result. */
  209. static int
  210. repl5_inc_get_next_result(result_data *rd)
  211. {
  212. ConnResult conres = 0;
  213. int message_id = 0;
  214. /* Wait on the next result */
  215. conres = conn_read_result(rd->prp->conn, &message_id);
  216. /* Return it to the caller */
  217. return conres;
  218. }
  219. #if NEEDED_FOR_DEBUGGING
  220. static void
  221. repl5_inc_log_operation_failure(int operation_code, int ldap_error, char* ldap_error_string, const char *agreement_name)
  222. {
  223. char *op_string = slapi_op_type_to_string(operation_code);
  224. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  225. "%s: Received error %d: %s for %s operation\n",
  226. agreement_name,
  227. ldap_error, ldap_error_string ? ldap_error_string : "NULL",
  228. op_string ? op_string : "NULL");
  229. }
  230. #endif
  231. /* Thread that collects results from async operations sent to the consumer */
  232. static void
  233. repl5_inc_result_threadmain(void *param)
  234. {
  235. result_data *rd = (result_data*) param;
  236. ConnResult conres = 0;
  237. Repl_Connection *conn = rd->prp->conn;
  238. int finished = 0;
  239. int message_id = 0;
  240. slapi_log_error(SLAPI_LOG_REPL, NULL, "repl5_inc_result_threadmain starting\n");
  241. while (!finished)
  242. {
  243. repl5_inc_operation *op = NULL;
  244. int connection_error = 0;
  245. char *csn_str = NULL;
  246. char *uniqueid = NULL;
  247. ReplicaId replica_id = 0;
  248. int operation_code = 0;
  249. char *ldap_error_string = NULL;
  250. time_t time_now = 0;
  251. time_t start_time = time( NULL );
  252. int backoff_time = 1;
  253. /* Read the next result */
  254. /* We call the get result function with a short timeout (non-blocking)
  255. * this is so we don't block here forever, and can stop this thread when
  256. * the time comes. However, we do need to implement blocking with timeout
  257. * semantics here instead.
  258. */
  259. while (!finished)
  260. {
  261. conres = conn_read_result_ex(conn, NULL, NULL, NULL, LDAP_RES_ANY, &message_id, 0);
  262. slapi_log_error(SLAPI_LOG_REPL, NULL, "repl5_inc_result_threadmain: read result for message_id %d\n", message_id);
  263. /* Timeout here means that we didn't block, not a real timeout */
  264. if (CONN_TIMEOUT == conres)
  265. {
  266. /* We need to a) check that the 'real' timeout hasn't expired and
  267. * b) implement a backoff sleep to avoid spinning */
  268. /* Did the connection's timeout expire ? */
  269. time_now = time( NULL );
  270. if (conn_get_timeout(conn) <= ( time_now - start_time ))
  271. {
  272. /* We timed out */
  273. conres = CONN_TIMEOUT;
  274. break;
  275. }
  276. /* Otherwise we backoff */
  277. DS_Sleep(PR_MillisecondsToInterval(backoff_time));
  278. if (backoff_time < 1000)
  279. {
  280. backoff_time <<= 1;
  281. }
  282. /* Should we stop ? */
  283. PR_Lock(rd->lock);
  284. if (rd->stop_result_thread)
  285. {
  286. finished = 1;
  287. }
  288. PR_Unlock(rd->lock);
  289. } else
  290. {
  291. /* Something other than a timeout, so we exit the loop */
  292. break;
  293. }
  294. }
  295. if (conres != CONN_TIMEOUT)
  296. {
  297. int return_value;
  298. int should_finish = 0;
  299. if (message_id)
  300. {
  301. rd->last_message_id_received = message_id;
  302. }
  303. /* Handle any error etc */
  304. /* Get the stored operation details from the queue, unless we timed out... */
  305. op = repl5_inc_pop_operation(rd);
  306. if (op)
  307. {
  308. csn_str = op->csn_str;
  309. replica_id = op->replica_id;
  310. uniqueid = op->uniqueid;
  311. }
  312. conn_get_error_ex(conn, &operation_code, &connection_error, &ldap_error_string);
  313. slapi_log_error(SLAPI_LOG_REPL, NULL, "repl5_inc_result_threadmain: result %d, %d, %d, %d, %s\n", operation_code,connection_error,conres,message_id,ldap_error_string);
  314. return_value = repl5_inc_update_from_op_result(rd->prp, conres, connection_error, csn_str, uniqueid, replica_id, &should_finish, &(rd->num_changes_sent));
  315. if (return_value || should_finish)
  316. {
  317. slapi_log_error(SLAPI_LOG_REPL, NULL, "repl5_inc_result_threadmain: got op result %d should finish %d\n", return_value, should_finish);
  318. /* If so then we need to take steps to abort the update process */
  319. PR_Lock(rd->lock);
  320. rd->result = return_value;
  321. rd->abort = 1;
  322. PR_Unlock(rd->lock);
  323. /* We also need to log the error, including details stored from when the operation was sent */
  324. /* we cannot finish yet - we still need to waitfor the pending results, then
  325. the main repl code will shut down this thread */
  326. /* we can finish if we have disconnected - in that case, there will be nothing
  327. to read */
  328. if (return_value == UPDATE_CONNECTION_LOST) {
  329. finished = 1;
  330. }
  331. } else {
  332. /* old semantics had result set outside of lock */
  333. rd->result = return_value;
  334. }
  335. }
  336. /* Should we stop ? */
  337. PR_Lock(rd->lock);
  338. if (rd->stop_result_thread)
  339. {
  340. finished = 1;
  341. }
  342. PR_Unlock(rd->lock);
  343. if (op)
  344. {
  345. repl5_inc_op_free(op);
  346. }
  347. }
  348. slapi_log_error(SLAPI_LOG_REPL, NULL, "repl5_inc_result_threadmain exiting\n");
  349. }
  350. static result_data *
  351. repl5_inc_rd_new(Private_Repl_Protocol *prp)
  352. {
  353. result_data *res = NULL;
  354. res = (result_data *) slapi_ch_calloc(1,sizeof(result_data));
  355. if (res) {
  356. res->prp = prp;
  357. res->lock = PR_NewLock();
  358. if (NULL == res->lock) {
  359. slapi_ch_free((void **)&res);
  360. res = NULL;
  361. }
  362. }
  363. return res;
  364. }
  365. static void
  366. repl5_inc_rd_list_destroy(repl5_inc_operation *op)
  367. {
  368. while (op) {
  369. repl5_inc_operation *next = op->next;
  370. repl5_inc_op_free(op);
  371. op = next;
  372. }
  373. }
  374. static void
  375. repl5_inc_rd_destroy(result_data **pres)
  376. {
  377. result_data *res = *pres;
  378. if (res->lock) {
  379. PR_DestroyLock(res->lock);
  380. }
  381. /* Delete the linked list if we have one */
  382. /* Begin at the head */
  383. repl5_inc_rd_list_destroy(res->operation_list_head);
  384. slapi_ch_free((void **)pres);
  385. }
  386. static int
  387. repl5_inc_create_async_result_thread(result_data *rd)
  388. {
  389. int retval = 0;
  390. PRThread *tid = NULL;
  391. /* Create a thread that reads results from the connection and stores status in the callback_data structure */
  392. tid = PR_CreateThread(PR_USER_THREAD,
  393. repl5_inc_result_threadmain, (void*)rd,
  394. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD,
  395. SLAPD_DEFAULT_THREAD_STACKSIZE);
  396. if (NULL == tid)
  397. {
  398. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  399. "repl5_tot_create_async_result_thread failed. "
  400. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  401. PR_GetError(), slapd_pr_strerror( PR_GetError() ));
  402. retval = -1;
  403. } else {
  404. rd->result_tid = tid;
  405. }
  406. return retval;
  407. }
  408. static int
  409. repl5_inc_destroy_async_result_thread(result_data *rd)
  410. {
  411. int retval = 0;
  412. PRThread *tid = rd->result_tid;
  413. if (tid) {
  414. PR_Lock(rd->lock);
  415. rd->stop_result_thread = 1;
  416. PR_Unlock(rd->lock);
  417. (void)PR_JoinThread(tid);
  418. }
  419. return retval;
  420. }
  421. /* The interest of this routine is to give time to the consumer
  422. * to apply the sent updates and return the acks.
  423. * So the caller should not hold the replication connection lock
  424. * to let the RA.reader receives the acks.
  425. */
  426. static void
  427. repl5_inc_flow_control_results(Repl_Agmt *agmt, result_data *rd)
  428. {
  429. PR_Lock(rd->lock);
  430. if ((rd->last_message_id_received <= rd->last_message_id_sent) &&
  431. ((rd->last_message_id_sent - rd->last_message_id_received) >= agmt_get_flowcontrolwindow(agmt))) {
  432. rd->flowcontrol_detection++;
  433. DS_Sleep(PR_MillisecondsToInterval(agmt_get_flowcontrolpause(agmt)));
  434. }
  435. PR_Unlock(rd->lock);
  436. }
  437. static void
  438. repl5_inc_waitfor_async_results(result_data *rd)
  439. {
  440. int done = 0;
  441. int loops = 0;
  442. /* Keep pulling results off the LDAP connection until we catch up to the last message id stored in the rd */
  443. while (!done && !slapi_is_shutting_down())
  444. {
  445. /* Lock the structure to force memory barrier */
  446. PR_Lock(rd->lock);
  447. /* Are we caught up ? */
  448. slapi_log_error(SLAPI_LOG_REPL, NULL,
  449. "repl5_inc_waitfor_async_results: %d %d\n",
  450. rd->last_message_id_received, rd->last_message_id_sent);
  451. if (rd->last_message_id_received >= rd->last_message_id_sent)
  452. {
  453. /* If so then we're done */
  454. done = 1;
  455. }
  456. if (rd->abort && (rd->result == UPDATE_CONNECTION_LOST))
  457. {
  458. done = 1; /* no connection == no more results */
  459. }
  460. PR_Unlock(rd->lock);
  461. /* If not then sleep a bit */
  462. DS_Sleep(PR_SecondsToInterval(1));
  463. loops++;
  464. /* If we sleep forever then we can conclude that something bad happened, and bail... */
  465. /* 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 */
  466. if (!done && (loops > 300))
  467. {
  468. /* Log a warning */
  469. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  470. "repl5_inc_waitfor_async_results timed out waiting for responses: %d %d\n",
  471. rd->last_message_id_received, rd->last_message_id_sent);
  472. done = 1;
  473. }
  474. }
  475. }
  476. /*
  477. * It's specifically ok to delete a protocol instance that
  478. * is currently running. The instance will be shut down, and
  479. * then resources will be freed. Since a graceful shutdown is
  480. * attempted, this function may take some time to complete.
  481. */
  482. static void
  483. repl5_inc_delete(Private_Repl_Protocol **prpp)
  484. {
  485. repl5_inc_private *prp_priv = (repl5_inc_private *)(*prpp)->private;
  486. /* if backoff is set, delete it (from EQ, as well) */
  487. if (prp_priv->backoff) {
  488. backoff_delete(&prp_priv->backoff);
  489. }
  490. /* First, stop the protocol if it isn't already stopped */
  491. if (!(*prpp)->stopped) {
  492. (*prpp)->stopped = 1;
  493. (*prpp)->stop(*prpp);
  494. }
  495. /* Then, delete all resources used by the protocol */
  496. if ((*prpp)->lock) {
  497. PR_DestroyLock((*prpp)->lock);
  498. (*prpp)->lock = NULL;
  499. }
  500. if ((*prpp)->cvar) {
  501. PR_DestroyCondVar((*prpp)->cvar);
  502. (*prpp)->cvar = NULL;
  503. }
  504. slapi_ch_free((void **)&(*prpp)->private);
  505. slapi_ch_free((void **)prpp);
  506. }
  507. /* helper function */
  508. void
  509. set_pause_and_busy_time(Private_Repl_Protocol *prp, long *pausetime, long *busywaittime)
  510. {
  511. /* If neither are set, set busy time to its default */
  512. if (!*pausetime && !*busywaittime)
  513. {
  514. *busywaittime = repl5_get_backoff_min(prp);
  515. }
  516. /* pause time must be at least 1 more than the busy backoff time */
  517. if (*pausetime && !*busywaittime)
  518. {
  519. /*
  520. * user specified a pause time but no busy wait time - must
  521. * set busy wait time to 1 less than pause time - if pause
  522. * time is 1, we must set it to 2
  523. */
  524. if (*pausetime < 2)
  525. {
  526. *pausetime = 2;
  527. }
  528. *busywaittime = *pausetime - 1;
  529. }
  530. else if (!*pausetime && *busywaittime)
  531. {
  532. /*
  533. * user specified a busy wait time but no pause time - must
  534. * set pause time to 1 more than busy wait time
  535. */
  536. *pausetime = *busywaittime + 1;
  537. }
  538. else if (*pausetime && *busywaittime && *pausetime <= *busywaittime)
  539. {
  540. /*
  541. * user specified both pause and busy wait times, but the pause
  542. * time was <= busy wait time - pause time must be at least
  543. * 1 more than the busy wait time
  544. */
  545. *pausetime = *busywaittime + 1;
  546. }
  547. }
  548. /*
  549. * Do the incremental protocol.
  550. *
  551. * What's going on here? This thing is a state machine. It has the
  552. * following states:
  553. *
  554. * State transition table:
  555. *
  556. * Curr State Condition/Event Next State
  557. * ---------- ------------ -----------
  558. * START schedule window is open ACQUIRE_REPLICA
  559. * schedule window is closed WAIT_WINDOW_OPEN
  560. * WAIT_WINDOW_OPEN schedule change START
  561. * replicate now ACQUIRE_REPLICA
  562. * schedule window opens ACQUIRE_REPLICA
  563. * ACQUIRE_REPLICA acquired replica SEND_CHANGES
  564. * failed to acquire - transient error START_BACKOFF
  565. * failed to acquire - fatal error STOP_FATAL_ERROR
  566. * SEND_CHANGES can't update CONSUMER_NEEDS_REINIT
  567. * no changes to send WAIT_CHANGES
  568. * can't send - thransient error START_BACKOF
  569. * can't send - window closed WAIT_WINDOW_OPEN
  570. * can'r send - fatal error STOP_FATAL_ERROR
  571. * START_BACKOF replicate now ACQUIRE_REPLICA
  572. * schedule changes START
  573. * schedule window closes WAIT_WINDOW_OPEN
  574. * backoff expires & can acquire SEND_CHANGES
  575. * backoff expires & can't acquire-trans BACKOFF
  576. * backoff expires & can't acquire-fatal STOP_FATAL_ERROR
  577. * BACKOF replicate now ACQUIRE_REPLICA
  578. * schedule changes START
  579. * schedule window closes WAIT_WINDOW_OPEN
  580. * backoff expires & can acquire SEND_CHANGES
  581. * backoff expires & can't acquire-trans BACKOFF
  582. * backoff expires & can't acquire-fatal STOP_FATAL_ERROR
  583. * WAIT_CHANGES schedule window closes WAIT_WINDOW_OPEN
  584. * replicate_now ACQUIRE_REPLICA
  585. * change available ACQUIRE_REPLICA
  586. * schedule_change START
  587. */
  588. /*
  589. * Main state machine for the incremental protocol. This routine will,
  590. * under normal circumstances, not return until the protocol is shut
  591. * down.
  592. */
  593. static void
  594. repl5_inc_run(Private_Repl_Protocol *prp)
  595. {
  596. repl5_inc_private *prp_priv = (repl5_inc_private *)prp->private;
  597. Replica *replica = NULL;
  598. CSN *cons_schema_csn;
  599. RUV *ruv = NULL;
  600. PRUint32 num_changes_sent;
  601. /* use a different backoff timer strategy for ACQUIRE_REPLICA_BUSY errors */
  602. PRBool use_busy_backoff_timer = PR_FALSE;
  603. time_t next_fire_time;
  604. time_t now;
  605. long busywaittime = 0;
  606. long pausetime = 0;
  607. long loops = 0;
  608. int wait_change_timer_set = 0;
  609. int current_state = STATE_START;
  610. int next_state = STATE_START;
  611. int optype, ldaprc;
  612. int done;
  613. int e1;
  614. prp->stopped = 0;
  615. prp->terminate = 0;
  616. /* establish_protocol_callbacks(prp); */
  617. done = 0;
  618. do {
  619. int rc;
  620. /* Take action, based on current state, and compute new state. */
  621. switch (current_state)
  622. {
  623. case STATE_START:
  624. dev_debug("repl5_inc_run(STATE_START)");
  625. if (PROTOCOL_IS_SHUTDOWN(prp)){
  626. done = 1;
  627. break;
  628. }
  629. /*
  630. * Our initial state. See if we're in a schedule window. If
  631. * so, then we're ready to acquire the replica and see if it
  632. * needs any updates from us. If not, then wait for the window
  633. * to open.
  634. */
  635. if (agmt_schedule_in_window_now(prp->agmt)){
  636. next_state = STATE_READY_TO_ACQUIRE;
  637. } else {
  638. next_state = STATE_WAIT_WINDOW_OPEN;
  639. }
  640. /* we can get here from other states because some events happened and were
  641. * not cleared. For instance when we wake up in STATE_WAIT_CHANGES state.
  642. * Since this is a fresh start state, we should clear all events */
  643. /* ONREPL - this does not feel right - we should take another look
  644. * at this state machine */
  645. reset_events (prp);
  646. /* Cancel any linger timer that might be in effect... */
  647. conn_cancel_linger(prp->conn);
  648. /* ... and disconnect, if currently connected */
  649. conn_disconnect(prp->conn);
  650. /* get the new pause time, if any */
  651. pausetime = agmt_get_pausetime(prp->agmt);
  652. /* get the new busy wait time, if any */
  653. busywaittime = agmt_get_busywaittime(prp->agmt);
  654. if (pausetime || busywaittime){
  655. /* helper function to make sure they are set correctly */
  656. set_pause_and_busy_time(prp, &pausetime, &busywaittime);
  657. }
  658. break;
  659. case STATE_WAIT_WINDOW_OPEN:
  660. /*
  661. * We're waiting for a schedule window to open. If one did,
  662. * or we receive a "replicate now" event, then start a protocol
  663. * session immediately. If the replication schedule changed, go
  664. * back to start. Otherwise, go back to sleep.
  665. */
  666. dev_debug("repl5_inc_run(STATE_WAIT_WINDOW_OPEN)");
  667. if (PROTOCOL_IS_SHUTDOWN(prp)){
  668. done = 1;
  669. break;
  670. } else if (event_occurred(prp, EVENT_WINDOW_OPENED)){
  671. next_state = STATE_READY_TO_ACQUIRE;
  672. } else if (event_occurred(prp, EVENT_REPLICATE_NOW)){
  673. next_state = STATE_READY_TO_ACQUIRE;
  674. } else if (event_occurred(prp, EVENT_AGMT_CHANGED)){
  675. next_state = STATE_START;
  676. conn_set_agmt_changed(prp->conn);
  677. } else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET)){ /* change available */
  678. /* just ignore it and go to sleep */
  679. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  680. } else if ((e1 = event_occurred(prp, EVENT_WINDOW_CLOSED)) ||
  681. event_occurred(prp, EVENT_BACKOFF_EXPIRED)){
  682. /* this events - should not occur - log a warning and go to sleep */
  683. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  684. "%s: Incremental protocol: "
  685. "event %s should not occur in state %s; going to sleep\n",
  686. agmt_get_long_name(prp->agmt), e1 ? event2name(EVENT_WINDOW_CLOSED) :
  687. event2name(EVENT_BACKOFF_EXPIRED), state2name(current_state));
  688. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  689. } else {
  690. /* wait until window opens or an event occurs */
  691. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  692. "%s: Incremental protocol: "
  693. "waiting for update window to open\n", agmt_get_long_name(prp->agmt));
  694. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  695. }
  696. break;
  697. case STATE_WAIT_CHANGES:
  698. /*
  699. * We're in a replication window, but we're waiting for more
  700. * changes to accumulate before we actually hook up and send
  701. * them.
  702. */
  703. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES)");
  704. if (PROTOCOL_IS_SHUTDOWN(prp)){
  705. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): PROTOCOL_IS_SHUTING_DOWN -> end repl5_inc_run\n");
  706. done = 1;
  707. break;
  708. } else if (event_occurred(prp, EVENT_REPLICATE_NOW)){
  709. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_REPLICATE_NOW received -> STATE_READY_TO_ACQUIRE\n");
  710. next_state = STATE_READY_TO_ACQUIRE;
  711. wait_change_timer_set = 0;
  712. } else if (event_occurred(prp, EVENT_AGMT_CHANGED)){
  713. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_AGMT_CHANGED received -> STATE_START\n");
  714. next_state = STATE_START;
  715. conn_set_agmt_changed(prp->conn);
  716. wait_change_timer_set = 0;
  717. } else if (event_occurred(prp, EVENT_WINDOW_CLOSED)){
  718. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_WINDOW_CLOSED received -> STATE_WAIT_WINDOW_OPEN\n");
  719. next_state = STATE_WAIT_WINDOW_OPEN;
  720. wait_change_timer_set = 0;
  721. } else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET)){
  722. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_TRIGGERING_CRITERIA_MET received -> STATE_READY_TO_ACQUIRE\n");
  723. next_state = STATE_READY_TO_ACQUIRE;
  724. wait_change_timer_set = 0;
  725. } else if ((e1 = event_occurred(prp, EVENT_WINDOW_OPENED)) || event_occurred(prp, EVENT_BACKOFF_EXPIRED)){
  726. /* this events - should not occur - log a warning and clear the event */
  727. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s: Incremental protocol: "
  728. "event %s should not occur in state %s\n",agmt_get_long_name(prp->agmt),
  729. e1 ? event2name(EVENT_WINDOW_OPENED) : event2name(EVENT_BACKOFF_EXPIRED),
  730. state2name(current_state));
  731. wait_change_timer_set = 0;
  732. } else {
  733. if (wait_change_timer_set){
  734. /* We are here because our timer expired */
  735. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): wait_change_timer_set expired -> STATE_START\n");
  736. next_state = STATE_START;
  737. wait_change_timer_set = 0;
  738. } else {
  739. /*
  740. * We are here because the last replication session
  741. * finished or aborted.
  742. */
  743. wait_change_timer_set = 1;
  744. protocol_sleep(prp, MAX_WAIT_BETWEEN_SESSIONS);
  745. }
  746. }
  747. break;
  748. case STATE_READY_TO_ACQUIRE:
  749. dev_debug("repl5_inc_run(STATE_READY_TO_ACQUIRE)");
  750. if (PROTOCOL_IS_SHUTDOWN(prp)){
  751. done = 1;
  752. break;
  753. }
  754. /* ONREPL - at this state we unconditionally acquire the replica
  755. ignoring all events. Not sure if this is good */
  756. object_acquire(prp->replica_object);
  757. rc = acquire_replica(prp, REPL_NSDS50_INCREMENTAL_PROTOCOL_OID, &ruv);
  758. use_busy_backoff_timer = PR_FALSE; /* default */
  759. if (rc == ACQUIRE_SUCCESS){
  760. next_state = STATE_SENDING_UPDATES;
  761. } else if (rc == ACQUIRE_REPLICA_BUSY){
  762. next_state = STATE_BACKOFF_START;
  763. use_busy_backoff_timer = PR_TRUE;
  764. } else if (rc == ACQUIRE_CONSUMER_WAS_UPTODATE){
  765. next_state = STATE_WAIT_CHANGES;
  766. } else if (rc == ACQUIRE_TRANSIENT_ERROR){
  767. next_state = STATE_BACKOFF_START;
  768. } else if (rc == ACQUIRE_FATAL_ERROR){
  769. next_state = STATE_STOP_FATAL_ERROR;
  770. }
  771. if (rc != ACQUIRE_SUCCESS){
  772. int optype, ldaprc;
  773. conn_get_error(prp->conn, &optype, &ldaprc);
  774. agmt_set_last_update_status(prp->agmt, ldaprc,
  775. prp->last_acquire_response_code, "Unable to acquire replica");
  776. }
  777. object_release(prp->replica_object);
  778. break;
  779. case STATE_BACKOFF_START:
  780. dev_debug("repl5_inc_run(STATE_BACKOFF_START)");
  781. if (PROTOCOL_IS_SHUTDOWN(prp)){
  782. done = 1;
  783. break;
  784. }
  785. if (event_occurred(prp, EVENT_REPLICATE_NOW)){
  786. next_state = STATE_READY_TO_ACQUIRE;
  787. } else if (event_occurred(prp, EVENT_AGMT_CHANGED)){
  788. next_state = STATE_START;
  789. conn_set_agmt_changed(prp->conn);
  790. } else if (event_occurred (prp, EVENT_WINDOW_CLOSED)){
  791. next_state = STATE_WAIT_WINDOW_OPEN;
  792. } else if (event_occurred (prp, EVENT_TRIGGERING_CRITERIA_MET)){
  793. /* consume and ignore */
  794. } else if ((e1 = event_occurred (prp, EVENT_WINDOW_OPENED)) ||
  795. event_occurred (prp, EVENT_BACKOFF_EXPIRED)){
  796. /* This should never happen */
  797. /* this events - should not occur - log a warning and go to sleep */
  798. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  799. "%s: Incremental protocol: event %s should not occur in state %s\n",
  800. agmt_get_long_name(prp->agmt),
  801. e1 ? event2name(EVENT_WINDOW_OPENED) : event2name(EVENT_BACKOFF_EXPIRED),
  802. state2name(current_state));
  803. } else {
  804. /* Set up the backoff timer to wake us up at the appropriate time */
  805. /* if previous backoff set up, delete it. */
  806. if (prp_priv->backoff) {
  807. backoff_delete(&prp_priv->backoff);
  808. }
  809. if (use_busy_backoff_timer){
  810. /* we received a busy signal from the consumer, wait for a while */
  811. if (!busywaittime){
  812. busywaittime = repl5_get_backoff_min(prp);
  813. }
  814. prp_priv->backoff = backoff_new(BACKOFF_FIXED, busywaittime, busywaittime);
  815. } else {
  816. prp_priv->backoff = backoff_new(BACKOFF_EXPONENTIAL, repl5_get_backoff_min(prp),
  817. repl5_get_backoff_max(prp));
  818. }
  819. next_state = STATE_BACKOFF;
  820. backoff_reset(prp_priv->backoff, repl5_inc_backoff_expired, (void *)prp);
  821. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  822. use_busy_backoff_timer = PR_FALSE;
  823. }
  824. break;
  825. case STATE_BACKOFF:
  826. /*
  827. * We're in a backoff state.
  828. */
  829. dev_debug("repl5_inc_run(STATE_BACKOFF)");
  830. if (PROTOCOL_IS_SHUTDOWN(prp)){
  831. if (prp_priv->backoff)
  832. backoff_delete(&prp_priv->backoff);
  833. done = 1;
  834. break;
  835. } else if (event_occurred(prp, EVENT_REPLICATE_NOW)){
  836. next_state = STATE_READY_TO_ACQUIRE;
  837. } else if (event_occurred(prp, EVENT_AGMT_CHANGED)){
  838. next_state = STATE_START;
  839. conn_set_agmt_changed(prp->conn);
  840. /* Destroy the backoff timer, since we won't need it anymore */
  841. if (prp_priv->backoff)
  842. backoff_delete(&prp_priv->backoff);
  843. } else if (event_occurred(prp, EVENT_WINDOW_CLOSED)){
  844. next_state = STATE_WAIT_WINDOW_OPEN;
  845. /* Destroy the backoff timer, since we won't need it anymore */
  846. if (prp_priv->backoff)
  847. backoff_delete(&prp_priv->backoff);
  848. } else if (event_occurred(prp, EVENT_BACKOFF_EXPIRED)){
  849. rc = acquire_replica(prp, REPL_NSDS50_INCREMENTAL_PROTOCOL_OID, &ruv);
  850. use_busy_backoff_timer = PR_FALSE;
  851. if (rc == ACQUIRE_SUCCESS){
  852. next_state = STATE_SENDING_UPDATES;
  853. } else if (rc == ACQUIRE_REPLICA_BUSY){
  854. next_state = STATE_BACKOFF;
  855. use_busy_backoff_timer = PR_TRUE;
  856. } else if (rc == ACQUIRE_CONSUMER_WAS_UPTODATE){
  857. next_state = STATE_WAIT_CHANGES;
  858. } else if (rc == ACQUIRE_TRANSIENT_ERROR){
  859. next_state = STATE_BACKOFF;
  860. } else if (rc == ACQUIRE_FATAL_ERROR){
  861. next_state = STATE_STOP_FATAL_ERROR;
  862. }
  863. if (rc != ACQUIRE_SUCCESS){
  864. conn_get_error(prp->conn, &optype, &ldaprc);
  865. agmt_set_last_update_status(prp->agmt, ldaprc, prp->last_acquire_response_code, "Unable to acquire replica");
  866. }
  867. /*
  868. * We either need to step the backoff timer, or
  869. * destroy it if we don't need it anymore
  870. */
  871. if (STATE_BACKOFF == next_state){
  872. /* Step the backoff timer */
  873. time(&now);
  874. next_fire_time = backoff_step(prp_priv->backoff);
  875. /* And go back to sleep */
  876. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  877. "%s: Replication session backing off for %ld seconds\n",
  878. agmt_get_long_name(prp->agmt),next_fire_time - now);
  879. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  880. } else {
  881. /* Destroy the backoff timer, since we won't need it anymore */
  882. backoff_delete(&prp_priv->backoff);
  883. }
  884. } else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET)){
  885. /* changes are available */
  886. if ( prp_priv->backoff == NULL || backoff_expired (prp_priv->backoff, 60)){
  887. /*
  888. * Have seen cases that the agmt stuck here forever since
  889. * somehow the backoff timer was not in event queue anymore.
  890. * If the backoff timer has expired more than 60 seconds, destroy it.
  891. */
  892. if ( prp_priv->backoff )
  893. backoff_delete(&prp_priv->backoff);
  894. next_state = STATE_READY_TO_ACQUIRE;
  895. } else {
  896. /* ignore changes and go to sleep */
  897. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  898. }
  899. } else if (event_occurred(prp, EVENT_WINDOW_OPENED)){
  900. /* this should never happen - log an error and go to sleep */
  901. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s: Incremental protocol: "
  902. "event %s should not occur in state %s; going to sleep\n",
  903. agmt_get_long_name(prp->agmt), event2name(EVENT_WINDOW_OPENED),
  904. state2name(current_state));
  905. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  906. }
  907. break;
  908. case STATE_SENDING_UPDATES:
  909. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES)");
  910. num_changes_sent = 0;
  911. /*
  912. * We've acquired the replica, and are ready to send any needed updates.
  913. */
  914. if (PROTOCOL_IS_SHUTDOWN(prp)){
  915. release_replica (prp);
  916. done = 1;
  917. agmt_set_update_in_progress(prp->agmt, PR_FALSE);
  918. agmt_set_last_update_end(prp->agmt, current_time());
  919. /* MAB: I don't find the following status correct. How do we know it has
  920. * been stopped by an admin and not by a total update request, for instance?
  921. * In any case, how is this protocol shutdown situation different from all the
  922. * other ones that are present in this state machine? */
  923. /* richm: We at least need to let monitors know that the protocol has been
  924. * shutdown - maybe they can figure out why */
  925. agmt_set_last_update_status(prp->agmt, 0, 0, "Protocol stopped");
  926. agmt_update_done(prp->agmt, 0);
  927. break;
  928. }
  929. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update started");
  930. /* ONREPL - in this state we send changes no matter what other events occur.
  931. * This is because we can get because of the REPLICATE_NOW event which
  932. * has high priority. Is this ok? */
  933. /* First, push new schema to the consumer if needed */
  934. /* ONREPL - should we push schema after we examine the RUV? */
  935. /*
  936. * GGOOREPL - I don't see why we should wait until we've
  937. * examined the RUV. The schema entry has its own CSN that is
  938. * used to decide if the remote schema needs to be updated.
  939. */
  940. cons_schema_csn = agmt_get_consumer_schema_csn ( prp->agmt );
  941. rc = conn_push_schema(prp->conn, &cons_schema_csn);
  942. if ( cons_schema_csn != agmt_get_consumer_schema_csn ( prp->agmt )){
  943. agmt_set_consumer_schema_csn ( prp->agmt, cons_schema_csn );
  944. }
  945. if (CONN_SCHEMA_UPDATED != rc && CONN_SCHEMA_NO_UPDATE_NEEDED != rc){
  946. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  947. "%s: Warning: unable to replicate schema: rc=%d\n", agmt_get_long_name(prp->agmt), rc);
  948. /* But keep going */
  949. }
  950. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> examine_update_vector");
  951. rc = examine_update_vector(prp, ruv);
  952. /*
  953. * Decide what to do next - proceed with incremental, backoff, or total update
  954. */
  955. switch (rc){
  956. case EXAMINE_RUV_PARAM_ERROR:
  957. /* this is really bad - we have NULL prp! */
  958. next_state = STATE_STOP_FATAL_ERROR;
  959. break;
  960. case EXAMINE_RUV_PRISTINE_REPLICA:
  961. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  962. "%s: Replica has no update vector. It has never been initialized.\n",
  963. agmt_get_long_name(prp->agmt));
  964. agmt_set_last_update_status(prp->agmt, 0, rc, "Replica is not initialized");
  965. next_state = STATE_BACKOFF_START;
  966. break;
  967. case EXAMINE_RUV_GENERATION_MISMATCH:
  968. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  969. "%s: The remote replica has a different database generation ID than "
  970. "the local database. You may have to reinitialize the remote replica, "
  971. "or the local replica.\n", agmt_get_long_name(prp->agmt));
  972. agmt_set_last_update_status(prp->agmt, 0, rc, "Replica has different database "
  973. "generation ID, remote replica may need to be initialized");
  974. next_state = STATE_BACKOFF_START;
  975. break;
  976. case EXAMINE_RUV_REPLICA_TOO_OLD:
  977. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  978. "%s: Replica update vector is too out of date to bring "
  979. "into sync using the incremental protocol. The replica "
  980. "must be reinitialized.\n", agmt_get_long_name(prp->agmt));
  981. agmt_set_last_update_status(prp->agmt, 0, rc, "Replica needs to be reinitialized");
  982. next_state = STATE_BACKOFF_START;
  983. break;
  984. case EXAMINE_RUV_OK:
  985. /* update our csn generator state with the consumer's ruv data */
  986. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> examine_update_vector OK");
  987. object_acquire(prp->replica_object);
  988. replica = object_get_data(prp->replica_object);
  989. rc = replica_update_csngen_state (replica, ruv);
  990. object_release (prp->replica_object);
  991. replica = NULL;
  992. if (rc == CSN_LIMIT_EXCEEDED) /* too much skew */ {
  993. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  994. "%s: Incremental protocol: fatal error - too much time skew between replicas!\n",
  995. agmt_get_long_name(prp->agmt));
  996. next_state = STATE_STOP_FATAL_ERROR;
  997. } else if (rc != 0) /* internal error */ {
  998. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  999. "%s: Incremental protocol: fatal internal error updating the CSN generator!\n",
  1000. agmt_get_long_name(prp->agmt));
  1001. next_state = STATE_STOP_FATAL_ERROR;
  1002. } else {
  1003. /*
  1004. * Reset our update times and status
  1005. */
  1006. agmt_set_last_update_start(prp->agmt, current_time());
  1007. agmt_set_last_update_end(prp->agmt, 0);
  1008. agmt_set_update_in_progress(prp->agmt, PR_TRUE);
  1009. /*
  1010. * Send the updates
  1011. */
  1012. rc = send_updates(prp, ruv, &num_changes_sent);
  1013. if (rc == UPDATE_NO_MORE_UPDATES){
  1014. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_NO_MORE_UPDATES -> STATE_WAIT_CHANGES");
  1015. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update succeeded");
  1016. next_state = STATE_WAIT_CHANGES;
  1017. } else if (rc == UPDATE_YIELD){
  1018. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_YIELD -> STATE_BACKOFF_START");
  1019. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update succeeded and yielded");
  1020. next_state = STATE_BACKOFF_START;
  1021. } else if (rc == UPDATE_TRANSIENT_ERROR){
  1022. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_TRANSIENT_ERROR -> STATE_BACKOFF_START");
  1023. agmt_set_last_update_status(prp->agmt, 0, rc, "Incremental update transient error. Backing off, will retry update later.");
  1024. next_state = STATE_BACKOFF_START;
  1025. } else if (rc == UPDATE_FATAL_ERROR){
  1026. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_FATAL_ERROR -> STATE_STOP_FATAL_ERROR");
  1027. next_state = STATE_STOP_FATAL_ERROR;
  1028. } else if (rc == UPDATE_SCHEDULE_WINDOW_CLOSED){
  1029. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_SCHEDULE_WINDOW_CLOSED -> STATE_WAIT_WINDOW_OPEN");
  1030. /*
  1031. * ONREPL - I don't think we should check this. We might be
  1032. * here because of replicate_now event - so we don't care
  1033. * about the schedule
  1034. */
  1035. next_state = STATE_WAIT_WINDOW_OPEN;
  1036. /* ONREPL - do we need to release the replica here ? */
  1037. conn_disconnect (prp->conn);
  1038. } else if (rc == UPDATE_CONNECTION_LOST){
  1039. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_CONNECTION_LOST -> STATE_BACKOFF_START");
  1040. agmt_set_last_update_status(prp->agmt, 0, rc, "Incremental update connection error. Backing off, will retry update later.");
  1041. next_state = STATE_BACKOFF_START;
  1042. } else if (rc == UPDATE_TIMEOUT){
  1043. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_TIMEOUT -> STATE_BACKOFF_START");
  1044. agmt_set_last_update_status(prp->agmt, 0, rc, "Incremental update timeout error. Backing off, will retry update later.");
  1045. next_state = STATE_BACKOFF_START;
  1046. }
  1047. /* Set the updates times based off the result of send_updates() */
  1048. if(rc == UPDATE_NO_MORE_UPDATES){
  1049. /* update successful, set the end time */
  1050. agmt_set_last_update_end(prp->agmt, current_time());
  1051. } else {
  1052. /* Failed to send updates, reset the start time to zero */
  1053. agmt_set_last_update_start(prp->agmt, 0);
  1054. }
  1055. agmt_set_update_in_progress(prp->agmt, PR_FALSE);
  1056. }
  1057. break;
  1058. }
  1059. if (NULL != ruv){
  1060. ruv_destroy(&ruv); ruv = NULL;
  1061. }
  1062. agmt_update_done(prp->agmt, 0);
  1063. /* If timed out, close the connection after released the replica */
  1064. release_replica(prp);
  1065. if (rc == UPDATE_TIMEOUT) {
  1066. conn_disconnect(prp->conn);
  1067. }
  1068. if (rc == UPDATE_NO_MORE_UPDATES && num_changes_sent > 0){
  1069. if (pausetime > 0){
  1070. /* richm - 20020219 - If we have acquired the consumer, and another master has gone
  1071. * into backoff waiting for us to release it, we may acquire the replica sooner
  1072. * than the other master has a chance to, and the other master may not be able
  1073. * to acquire the consumer for a long time (hours, days?) if this server is
  1074. * under a heavy load (see reliab06 et. al. system tests)
  1075. * So, this sleep gives the other master(s) a chance to acquire the consumer replica */
  1076. loops = pausetime;
  1077. /* the while loop is so that we don't just sleep and sleep if an
  1078. * event comes in that we should handle immediately (like shutdown) */
  1079. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1080. "%s: Pausing updates for %ld seconds to allow other suppliers to update consumer\n",
  1081. agmt_get_long_name(prp->agmt), pausetime);
  1082. while (loops-- && !(PROTOCOL_IS_SHUTDOWN(prp))){
  1083. DS_Sleep(PR_SecondsToInterval(1));
  1084. }
  1085. } else if (num_changes_sent > 10){
  1086. /* wait for consumer to write its ruv if the replication was busy */
  1087. /* When asked, consumer sends its ruv in cache to the supplier. */
  1088. /* DS_Sleep ( PR_SecondsToInterval(1) ); */
  1089. }
  1090. }
  1091. break;
  1092. case STATE_STOP_FATAL_ERROR:
  1093. /*
  1094. * We encountered some sort of a fatal error. Suspend.
  1095. */
  1096. /* XXXggood update state in replica */
  1097. agmt_set_last_update_status(prp->agmt, -1, 0, "Incremental update has failed and requires administrator action");
  1098. dev_debug("repl5_inc_run(STATE_STOP_FATAL_ERROR)");
  1099. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1100. "%s: Incremental update failed and requires administrator action\n",
  1101. agmt_get_long_name(prp->agmt));
  1102. next_state = STATE_STOP_FATAL_ERROR_PART2;
  1103. break;
  1104. case STATE_STOP_FATAL_ERROR_PART2:
  1105. if (PROTOCOL_IS_SHUTDOWN(prp)){
  1106. done = 1;
  1107. break;
  1108. }
  1109. /* MAB: This state is the FATAL state where we are supposed to get
  1110. * as a result of a FATAL error on send_updates. But, as bug
  1111. * states, send_updates was always returning TRANSIENT errors and never
  1112. * FATAL... In other words, this code has never been tested before...
  1113. *
  1114. * As of 01/16/01, this piece of code was in a very dangerous state. In particular,
  1115. * 1) it does not catch any events
  1116. * 2) it is a terminal state (once reached it never transitions to a different state)
  1117. *
  1118. * Both things combined make this state to become a consuming infinite loop
  1119. * that is useless after all (we are in a fatal place requiring manual admin jobs */
  1120. /* MAB: The following lines fix problem number 1 above... When the code gets
  1121. * into this state, it should only get a chance to get out of it by an
  1122. * EVENT_AGMT_CHANGED event... All other events should be ignored */
  1123. else if (event_occurred(prp, EVENT_AGMT_CHANGED)){
  1124. dev_debug("repl5_inc_run(STATE_STOP_FATAL_ERROR): EVENT_AGMT_CHANGED received\n");
  1125. /* Chance to recover for the EVENT_AGMT_CHANGED event.
  1126. * This is not mandatory, but fixes problem 2 above */
  1127. next_state = STATE_STOP_NORMAL_TERMINATION;
  1128. } else {
  1129. dev_debug("repl5_inc_run(STATE_STOP_FATAL_ERROR): Event received. Clearing it\n");
  1130. reset_events (prp);
  1131. }
  1132. protocol_sleep (prp, PR_INTERVAL_NO_TIMEOUT);
  1133. break;
  1134. case STATE_STOP_NORMAL_TERMINATION:
  1135. /*
  1136. * We encountered some sort of a fatal error. Return.
  1137. */
  1138. /* XXXggood update state in replica */
  1139. dev_debug("repl5_inc_run(STATE_STOP_NORMAL_TERMINATION)");
  1140. done = 1;
  1141. break;
  1142. }
  1143. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,"%s: State: %s -> %s\n",
  1144. agmt_get_long_name(prp->agmt),state2name(current_state), state2name(next_state));
  1145. current_state = next_state;
  1146. } while (!done);
  1147. /* remove_protocol_callbacks(prp); */
  1148. prp->stopped = 1;
  1149. /* Cancel any linger timer that might be in effect... */
  1150. conn_cancel_linger(prp->conn);
  1151. /* ... and disconnect, if currently connected */
  1152. conn_disconnect(prp->conn);
  1153. }
  1154. /*
  1155. * Go to sleep until awakened.
  1156. */
  1157. static void
  1158. protocol_sleep(Private_Repl_Protocol *prp, PRIntervalTime duration)
  1159. {
  1160. PR_ASSERT(NULL != prp);
  1161. PR_Lock(prp->lock);
  1162. /* we should not go to sleep if there are events available to be processed.
  1163. Otherwise, we can miss the event that suppose to wake us up */
  1164. if (prp->eventbits == 0)
  1165. PR_WaitCondVar(prp->cvar, duration);
  1166. else
  1167. {
  1168. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1169. "%s: Incremental protocol: can't go to sleep: event bits - %x\n",
  1170. agmt_get_long_name(prp->agmt), prp->eventbits);
  1171. }
  1172. PR_Unlock(prp->lock);
  1173. }
  1174. /*
  1175. * Notify the protocol about some event. Signal the condition
  1176. * variable in case the protocol is sleeping. Multiple occurences
  1177. * of a single event type are not remembered (e.g. no stack
  1178. * of events is maintained).
  1179. */
  1180. static void
  1181. event_notify(Private_Repl_Protocol *prp, PRUint32 event)
  1182. {
  1183. PR_ASSERT(NULL != prp);
  1184. PR_Lock(prp->lock);
  1185. prp->eventbits |= event;
  1186. PR_NotifyCondVar(prp->cvar);
  1187. PR_Unlock(prp->lock);
  1188. }
  1189. /*
  1190. * Test to see if an event occurred. The event is cleared when
  1191. * read.
  1192. */
  1193. static PRUint32
  1194. event_occurred(Private_Repl_Protocol *prp, PRUint32 event)
  1195. {
  1196. PRUint32 return_value;
  1197. PR_ASSERT(NULL != prp);
  1198. PR_Lock(prp->lock);
  1199. return_value = (prp->eventbits & event);
  1200. prp->eventbits &= ~event; /* Clear event */
  1201. PR_Unlock(prp->lock);
  1202. return return_value;
  1203. }
  1204. static void
  1205. reset_events (Private_Repl_Protocol *prp)
  1206. {
  1207. PR_ASSERT(NULL != prp);
  1208. PR_Lock(prp->lock);
  1209. prp->eventbits = 0;
  1210. PR_Unlock(prp->lock);
  1211. }
  1212. /*
  1213. * Replay the actual update to the consumer. Construct an appropriate LDAP
  1214. * operation, attach the baggage LDAPv3 control that contains the CSN, etc.,
  1215. * and send the operation to the consumer.
  1216. */
  1217. ConnResult
  1218. replay_update(Private_Repl_Protocol *prp, slapi_operation_parameters *op, int *message_id)
  1219. {
  1220. ConnResult return_value = CONN_OPERATION_FAILED;
  1221. LDAPControl *update_control;
  1222. char *parentuniqueid;
  1223. LDAPMod **modrdn_mods = NULL;
  1224. char csn_str[CSN_STRSIZE]; /* For logging only */
  1225. if (message_id) {
  1226. /* if we get out of this function without setting message_id, it means
  1227. we didn't send an op, so no result needs to be processed */
  1228. *message_id = 0;
  1229. }
  1230. /* Construct the replication info control that accompanies the operation */
  1231. if (SLAPI_OPERATION_ADD == op->operation_type)
  1232. {
  1233. parentuniqueid = op->p.p_add.parentuniqueid;
  1234. }
  1235. else if (SLAPI_OPERATION_MODRDN == op->operation_type)
  1236. {
  1237. /*
  1238. * For modrdn operations, we need to send along modified attributes, e.g.
  1239. * modifytimestamp.
  1240. * And the superior_uniqueid !
  1241. */
  1242. modrdn_mods = op->p.p_modrdn.modrdn_mods;
  1243. parentuniqueid = op->p.p_modrdn.modrdn_newsuperior_address.uniqueid;
  1244. }
  1245. else
  1246. {
  1247. parentuniqueid = NULL;
  1248. }
  1249. if (create_NSDS50ReplUpdateInfoControl(op->target_address.uniqueid,
  1250. parentuniqueid, op->csn, modrdn_mods, &update_control) != LDAP_SUCCESS)
  1251. {
  1252. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1253. "%s: replay_update: Unable to create NSDS50ReplUpdateInfoControl "
  1254. "for operation with csn %s. Skipping update.\n",
  1255. agmt_get_long_name(prp->agmt), csn_as_string(op->csn, PR_FALSE, csn_str));
  1256. }
  1257. else
  1258. {
  1259. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1260. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1261. "%s: replay_update: Sending %s operation (dn=\"%s\" csn=%s)\n",
  1262. agmt_get_long_name(prp->agmt),
  1263. op2string(op->operation_type), REPL_GET_DN(&op->target_address),
  1264. csn_as_string(op->csn, PR_FALSE, csn_str));
  1265. }
  1266. /* What type of operation is it? */
  1267. switch (op->operation_type)
  1268. {
  1269. case SLAPI_OPERATION_ADD:
  1270. {
  1271. LDAPMod **entryattrs;
  1272. /* Convert entry to mods */
  1273. (void)slapi_entry2mods (op->p.p_add.target_entry,
  1274. NULL /* &entrydn : We don't need it */,
  1275. &entryattrs);
  1276. if (NULL == entryattrs)
  1277. {
  1278. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1279. "%s: replay_update: Cannot convert entry to LDAPMods.\n",
  1280. agmt_get_long_name(prp->agmt));
  1281. return_value = CONN_LOCAL_ERROR;
  1282. }
  1283. else
  1284. {
  1285. /* If fractional agreement, trim down the entry */
  1286. if (agmt_is_fractional(prp->agmt))
  1287. {
  1288. repl5_strip_fractional_mods(prp->agmt,entryattrs);
  1289. }
  1290. if (MODS_ARE_EMPTY(entryattrs)) {
  1291. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1292. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1293. "%s: replay_update: %s operation (dn=\"%s\" csn=%s) "
  1294. "not sent - empty\n",
  1295. agmt_get_long_name(prp->agmt),
  1296. op2string(op->operation_type),
  1297. REPL_GET_DN(&op->target_address),
  1298. csn_as_string(op->csn, PR_FALSE, csn_str));
  1299. }
  1300. return_value = CONN_OPERATION_SUCCESS;
  1301. } else {
  1302. return_value = conn_send_add(prp->conn, REPL_GET_DN(&op->target_address),
  1303. entryattrs, update_control, message_id);
  1304. }
  1305. ldap_mods_free(entryattrs, 1);
  1306. }
  1307. break;
  1308. }
  1309. case SLAPI_OPERATION_MODIFY:
  1310. /* If fractional agreement, trim down the mods */
  1311. if (agmt_is_fractional(prp->agmt))
  1312. {
  1313. repl5_strip_fractional_mods(prp->agmt,op->p.p_modify.modify_mods);
  1314. }
  1315. if (MODS_ARE_EMPTY(op->p.p_modify.modify_mods)) {
  1316. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1317. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1318. "%s: replay_update: %ss operation (dn=\"%s\" csn=%s) "
  1319. "not sent - empty\n",
  1320. agmt_get_long_name(prp->agmt),
  1321. op2string(op->operation_type),
  1322. REPL_GET_DN(&op->target_address),
  1323. csn_as_string(op->csn, PR_FALSE, csn_str));
  1324. }
  1325. return_value = CONN_OPERATION_SUCCESS;
  1326. } else {
  1327. return_value = conn_send_modify(prp->conn, REPL_GET_DN(&op->target_address),
  1328. op->p.p_modify.modify_mods, update_control, message_id);
  1329. }
  1330. break;
  1331. case SLAPI_OPERATION_DELETE:
  1332. return_value = conn_send_delete(prp->conn, REPL_GET_DN(&op->target_address),
  1333. update_control, message_id);
  1334. break;
  1335. case SLAPI_OPERATION_MODRDN:
  1336. /* XXXggood need to pass modrdn mods in update control! */
  1337. return_value = conn_send_rename(prp->conn, REPL_GET_DN(&op->target_address),
  1338. op->p.p_modrdn.modrdn_newrdn,
  1339. REPL_GET_DN(&op->p.p_modrdn.modrdn_newsuperior_address),
  1340. op->p.p_modrdn.modrdn_deloldrdn,
  1341. update_control, message_id);
  1342. break;
  1343. default:
  1344. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s: replay_update: Unknown "
  1345. "operation type %lu found in changelog - skipping change.\n",
  1346. agmt_get_long_name(prp->agmt), op->operation_type);
  1347. }
  1348. destroy_NSDS50ReplUpdateInfoControl(&update_control);
  1349. }
  1350. if (CONN_OPERATION_SUCCESS == return_value)
  1351. {
  1352. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1353. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1354. "%s: replay_update: Consumer successfully sent operation with csn %s\n",
  1355. agmt_get_long_name(prp->agmt), csn_as_string(op->csn, PR_FALSE, csn_str));
  1356. }
  1357. }
  1358. else
  1359. {
  1360. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1361. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1362. "%s: replay_update: Consumer could not replay operation with csn %s\n",
  1363. agmt_get_long_name(prp->agmt), csn_as_string(op->csn, PR_FALSE, csn_str));
  1364. }
  1365. }
  1366. return return_value;
  1367. }
  1368. static PRBool
  1369. is_dummy_operation (const slapi_operation_parameters *op)
  1370. {
  1371. return (strcmp (op->target_address.uniqueid, START_ITERATION_ENTRY_UNIQUEID) == 0);
  1372. }
  1373. void
  1374. cl5_operation_parameters_done (struct slapi_operation_parameters *sop)
  1375. {
  1376. if(sop!=NULL) {
  1377. switch(sop->operation_type)
  1378. {
  1379. case SLAPI_OPERATION_BIND:
  1380. slapi_ch_free((void **)&(sop->p.p_bind.bind_saslmechanism));
  1381. if (sop->p.p_bind.bind_creds)
  1382. ber_bvecfree((struct berval**)&(sop->p.p_bind.bind_creds));
  1383. if (sop->p.p_bind.bind_ret_saslcreds)
  1384. ber_bvecfree((struct berval**)&(sop->p.p_bind.bind_ret_saslcreds));
  1385. sop->p.p_bind.bind_creds = NULL;
  1386. sop->p.p_bind.bind_ret_saslcreds = NULL;
  1387. break;
  1388. case SLAPI_OPERATION_COMPARE:
  1389. ava_done((struct ava *)&(sop->p.p_compare.compare_ava));
  1390. break;
  1391. case SLAPI_OPERATION_SEARCH:
  1392. slapi_ch_free((void **)&(sop->p.p_search.search_strfilter));
  1393. charray_free(sop->p.p_search.search_attrs);
  1394. slapi_filter_free(sop->p.p_search.search_filter,1);
  1395. break;
  1396. case SLAPI_OPERATION_MODRDN:
  1397. sop->p.p_modrdn.modrdn_deloldrdn = 0;
  1398. break;
  1399. case SLAPI_OPERATION_EXTENDED:
  1400. slapi_ch_free((void **)&(sop->p.p_extended.exop_oid));
  1401. if (sop->p.p_extended.exop_value)
  1402. ber_bvecfree((struct berval**)&(sop->p.p_extended.exop_value));
  1403. sop->p.p_extended.exop_value = NULL;
  1404. break;
  1405. default:
  1406. break;
  1407. }
  1408. }
  1409. operation_parameters_done(sop);
  1410. }
  1411. /* Helper to update the agreement state based on a the result of a replay operation */
  1412. static int
  1413. repl5_inc_update_from_op_result(Private_Repl_Protocol *prp, ConnResult replay_crc, int connection_error, char *csn_str, char *uniqueid, ReplicaId replica_id, int* finished, PRUint32 *num_changes_sent)
  1414. {
  1415. int return_value = 0;
  1416. /* Indentation is wrong here so we can get a sensible cvs diff */
  1417. if (CONN_OPERATION_SUCCESS != replay_crc)
  1418. {
  1419. /* Figure out what to do next */
  1420. if (CONN_OPERATION_FAILED == replay_crc)
  1421. {
  1422. /* Map ldap error code to return value */
  1423. if (!ignore_error_and_keep_going(connection_error))
  1424. {
  1425. return_value = UPDATE_TRANSIENT_ERROR;
  1426. *finished = 1;
  1427. }
  1428. else
  1429. {
  1430. agmt_inc_last_update_changecount (prp->agmt, replica_id, 1 /*skipped*/);
  1431. }
  1432. slapi_log_error(*finished ? SLAPI_LOG_FATAL : slapi_log_urp, repl_plugin_name,
  1433. "%s: Consumer failed to replay change (uniqueid %s, CSN %s): %s (%d). %s.\n",
  1434. agmt_get_long_name(prp->agmt),
  1435. uniqueid, csn_str,
  1436. ldap_err2string(connection_error), connection_error,
  1437. *finished ? "Will retry later" : "Skipping");
  1438. }
  1439. else if (CONN_NOT_CONNECTED == replay_crc)
  1440. {
  1441. /* We lost the connection - enter backoff state */
  1442. return_value = UPDATE_CONNECTION_LOST;
  1443. *finished = 1;
  1444. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1445. "%s: Consumer failed to replay change (uniqueid %s, CSN %s): "
  1446. "%s(%d). Will retry later.\n",
  1447. agmt_get_long_name(prp->agmt),
  1448. uniqueid, csn_str,
  1449. connection_error ? ldap_err2string(connection_error) : "Connection lost",
  1450. connection_error);
  1451. }
  1452. else if (CONN_TIMEOUT == replay_crc)
  1453. {
  1454. return_value = UPDATE_TIMEOUT;
  1455. *finished = 1;
  1456. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1457. "%s: Consumer timed out to replay change (uniqueid %s, CSN %s): "
  1458. "%s.\n",
  1459. agmt_get_long_name(prp->agmt),
  1460. uniqueid, csn_str,
  1461. connection_error ? ldap_err2string(connection_error) : "Timeout");
  1462. }
  1463. else if (CONN_LOCAL_ERROR == replay_crc)
  1464. {
  1465. /*
  1466. * Something bad happened on the local server - enter
  1467. * backoff state.
  1468. */
  1469. return_value = UPDATE_TRANSIENT_ERROR;
  1470. *finished = 1;
  1471. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1472. "%s: Failed to replay change (uniqueid %s, CSN %s): "
  1473. "Local error. Will retry later.\n",
  1474. agmt_get_long_name(prp->agmt),
  1475. uniqueid, csn_str);
  1476. }
  1477. }
  1478. else
  1479. {
  1480. /* Positive response received */
  1481. (*num_changes_sent)++;
  1482. agmt_inc_last_update_changecount (prp->agmt, replica_id, 0 /*replayed*/);
  1483. }
  1484. return return_value;
  1485. }
  1486. /*
  1487. * Send a set of updates to the replica. Assumes that (1) the replica
  1488. * has already been acquired, (2) that the consumer's update vector has
  1489. * been checked and (3) that it's ok to send incremental updates.
  1490. * Returns:
  1491. * UPDATE_NO_MORE_UPDATES - all updates were sent successfully
  1492. * UPDATE_TRANSIENT_ERROR - some non-permanent error occurred. Try again later.
  1493. * UPDATE_FATAL_ERROR - some bad, permanent error occurred.
  1494. * UPDATE_SCHEDULE_WINDOW_CLOSED - the schedule window closed on us.
  1495. */
  1496. static int
  1497. send_updates(Private_Repl_Protocol *prp, RUV *remote_update_vector, PRUint32 *num_changes_sent)
  1498. {
  1499. CL5Entry entry;
  1500. slapi_operation_parameters op;
  1501. int return_value;
  1502. int rc;
  1503. CL5ReplayIterator *changelog_iterator;
  1504. int message_id = 0;
  1505. result_data *rd = NULL;
  1506. *num_changes_sent = 0;
  1507. /*
  1508. * Iterate over the changelog. Retrieve each update,
  1509. * construct an appropriate LDAP operation,
  1510. * attaching the CSN, and send the change.
  1511. */
  1512. rc = cl5CreateReplayIterator(prp, remote_update_vector, &changelog_iterator);
  1513. if (CL5_SUCCESS != rc)
  1514. {
  1515. switch (rc)
  1516. {
  1517. case CL5_BAD_DATA: /* invalid parameter passed to the function */
  1518. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1519. "%s: Invalid parameter passed to cl5CreateReplayIterator\n",
  1520. agmt_get_long_name(prp->agmt));
  1521. return_value = UPDATE_FATAL_ERROR;
  1522. break;
  1523. case CL5_BAD_FORMAT: /* db data has unexpected format */
  1524. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1525. "%s: Unexpected format encountered in changelog database\n",
  1526. agmt_get_long_name(prp->agmt));
  1527. return_value = UPDATE_FATAL_ERROR;
  1528. break;
  1529. case CL5_BAD_STATE: /* changelog is in an incorrect state for attempted operation */
  1530. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1531. "%s: Changelog database was in an incorrect state\n",
  1532. agmt_get_long_name(prp->agmt));
  1533. return_value = UPDATE_FATAL_ERROR;
  1534. break;
  1535. case CL5_BAD_DBVERSION: /* changelog has invalid dbversion */
  1536. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1537. "%s: Incorrect dbversion found in changelog database\n",
  1538. agmt_get_long_name(prp->agmt));
  1539. return_value = UPDATE_FATAL_ERROR;
  1540. break;
  1541. case CL5_DB_ERROR: /* database error */
  1542. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1543. "%s: A changelog database error was encountered\n",
  1544. agmt_get_long_name(prp->agmt));
  1545. return_value = UPDATE_FATAL_ERROR;
  1546. break;
  1547. case CL5_NOTFOUND: /* we have no changes to send */
  1548. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1549. "%s: No changes to send\n",
  1550. agmt_get_long_name(prp->agmt));
  1551. return_value = UPDATE_NO_MORE_UPDATES;
  1552. break;
  1553. case CL5_MEMORY_ERROR: /* memory allocation failed */
  1554. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1555. "%s: Memory allocation error occurred\n",
  1556. agmt_get_long_name(prp->agmt));
  1557. return_value = UPDATE_FATAL_ERROR;
  1558. break;
  1559. case CL5_SYSTEM_ERROR: /* NSPR error occurred: use PR_GetError for further info */
  1560. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1561. "%s: An NSPR error (%d) occurred\n",
  1562. agmt_get_long_name(prp->agmt), PR_GetError());
  1563. return_value = UPDATE_TRANSIENT_ERROR;
  1564. break;
  1565. case CL5_CSN_ERROR: /* CSN API failed */
  1566. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1567. "%s: A CSN API failure was encountered\n",
  1568. agmt_get_long_name(prp->agmt));
  1569. return_value = UPDATE_TRANSIENT_ERROR;
  1570. break;
  1571. case CL5_RUV_ERROR: /* RUV API failed */
  1572. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1573. "%s: An RUV API failure occurred\n",
  1574. agmt_get_long_name(prp->agmt));
  1575. return_value = UPDATE_TRANSIENT_ERROR;
  1576. break;
  1577. case CL5_OBJSET_ERROR: /* namedobjset api failed */
  1578. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1579. "%s: A namedobject API failure occurred\n",
  1580. agmt_get_long_name(prp->agmt));
  1581. return_value = UPDATE_TRANSIENT_ERROR;
  1582. break;
  1583. case CL5_PURGED_DATA: /* requested data has been purged */
  1584. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1585. "%s: Data required to update replica has been purged. "
  1586. "The replica must be reinitialized.\n",
  1587. agmt_get_long_name(prp->agmt));
  1588. return_value = UPDATE_FATAL_ERROR;
  1589. break;
  1590. case CL5_MISSING_DATA: /* data should be in the changelog, but is missing */
  1591. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1592. "%s: Missing data encountered\n",
  1593. agmt_get_long_name(prp->agmt));
  1594. return_value = UPDATE_FATAL_ERROR;
  1595. break;
  1596. case CL5_UNKNOWN_ERROR: /* unclassified error */
  1597. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1598. "%s: An unknown error was encountered\n",
  1599. agmt_get_long_name(prp->agmt));
  1600. return_value = UPDATE_TRANSIENT_ERROR;
  1601. break;
  1602. default:
  1603. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1604. "%s: An unknown error (%d) occurred "
  1605. "(cl5CreateReplayIterator)\n",
  1606. agmt_get_long_name(prp->agmt), rc);
  1607. return_value = UPDATE_TRANSIENT_ERROR;
  1608. }
  1609. }
  1610. else
  1611. {
  1612. int finished = 0;
  1613. ConnResult replay_crc;
  1614. char csn_str[CSN_STRSIZE];
  1615. /* Start the results reading thread */
  1616. rd = repl5_inc_rd_new(prp);
  1617. if (!prp->repl50consumer)
  1618. {
  1619. rc = repl5_inc_create_async_result_thread(rd);
  1620. if (rc) {
  1621. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name, "%s: repl5_inc_run: "
  1622. "repl5_tot_create_async_result_thread failed; error - %d\n",
  1623. agmt_get_long_name(prp->agmt), rc);
  1624. return_value = UPDATE_FATAL_ERROR;
  1625. }
  1626. }
  1627. memset ( (void*)&op, 0, sizeof (op) );
  1628. entry.op = &op;
  1629. do {
  1630. cl5_operation_parameters_done ( entry.op );
  1631. memset ( (void*)entry.op, 0, sizeof (op) );
  1632. rc = cl5GetNextOperationToReplay(changelog_iterator, &entry);
  1633. switch (rc)
  1634. {
  1635. case CL5_SUCCESS:
  1636. /* check that we don't return dummy entries */
  1637. if (is_dummy_operation (entry.op))
  1638. {
  1639. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1640. "%s: changelog iteration code returned a dummy entry with csn %s, "
  1641. "skipping ...\n",
  1642. agmt_get_long_name(prp->agmt), csn_as_string(entry.op->csn, PR_FALSE, csn_str));
  1643. continue;
  1644. }
  1645. replay_crc = replay_update(prp, entry.op, &message_id);
  1646. if (message_id)
  1647. {
  1648. rd->last_message_id_sent = message_id;
  1649. }
  1650. /* If we're talking to an old non-async replica, we need to pick up the response here */
  1651. if (CONN_OPERATION_SUCCESS != replay_crc)
  1652. {
  1653. int operation, error;
  1654. conn_get_error(prp->conn, &operation, &error);
  1655. csn_as_string(entry.op->csn, PR_FALSE, csn_str);
  1656. /* Figure out what to do next */
  1657. if (CONN_OPERATION_FAILED == replay_crc)
  1658. {
  1659. /* Map ldap error code to return value */
  1660. if (!ignore_error_and_keep_going(error))
  1661. {
  1662. return_value = UPDATE_TRANSIENT_ERROR;
  1663. finished = 1;
  1664. }
  1665. else
  1666. {
  1667. agmt_inc_last_update_changecount (prp->agmt, csn_get_replicaid(entry.op->csn), 1 /*skipped*/);
  1668. }
  1669. slapi_log_error(finished ? SLAPI_LOG_FATAL : slapi_log_urp, repl_plugin_name,
  1670. "%s: Failed to send update operation to consumer (uniqueid %s, CSN %s): %s. %s.\n",
  1671. agmt_get_long_name(prp->agmt),
  1672. entry.op->target_address.uniqueid, csn_str,
  1673. ldap_err2string(error),
  1674. finished ? "Will retry later" : "Skipping");
  1675. }
  1676. else if (CONN_NOT_CONNECTED == replay_crc)
  1677. {
  1678. /* We lost the connection - enter backoff state */
  1679. return_value = UPDATE_CONNECTION_LOST;
  1680. finished = 1;
  1681. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1682. "%s: Failed to send update operation to consumer (uniqueid %s, CSN %s): "
  1683. "%s. Will retry later.\n",
  1684. agmt_get_long_name(prp->agmt),
  1685. entry.op->target_address.uniqueid, csn_str,
  1686. error ? ldap_err2string(error) : "Connection lost");
  1687. }
  1688. else if (CONN_TIMEOUT == replay_crc)
  1689. {
  1690. return_value = UPDATE_TIMEOUT;
  1691. finished = 1;
  1692. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1693. "%s: Timed out sending update operation to consumer (uniqueid %s, CSN %s): "
  1694. "%s.\n",
  1695. agmt_get_long_name(prp->agmt),
  1696. entry.op->target_address.uniqueid, csn_str,
  1697. error ? ldap_err2string(error) : "Timeout");
  1698. }
  1699. else if (CONN_LOCAL_ERROR == replay_crc)
  1700. {
  1701. /*
  1702. * Something bad happened on the local server - enter
  1703. * backoff state.
  1704. */
  1705. return_value = UPDATE_TRANSIENT_ERROR;
  1706. finished = 1;
  1707. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1708. "%s: Failed to send update operation to consumer (uniqueid %s, CSN %s): "
  1709. "Local error. Will retry later.\n",
  1710. agmt_get_long_name(prp->agmt),
  1711. entry.op->target_address.uniqueid, csn_str);
  1712. }
  1713. }
  1714. else
  1715. {
  1716. char *uniqueid = NULL;
  1717. ReplicaId replica_id = 0;
  1718. csn_as_string(entry.op->csn, PR_FALSE, csn_str);
  1719. replica_id = csn_get_replicaid(entry.op->csn);
  1720. uniqueid = entry.op->target_address.uniqueid;
  1721. if (prp->repl50consumer && message_id)
  1722. {
  1723. int operation, error = 0;
  1724. conn_get_error(prp->conn, &operation, &error);
  1725. /* Get the response here */
  1726. replay_crc = repl5_inc_get_next_result(rd);
  1727. conn_get_error(prp->conn, &operation, &error);
  1728. csn_as_string(entry.op->csn, PR_FALSE, csn_str);
  1729. return_value = repl5_inc_update_from_op_result(prp, replay_crc, error, csn_str, uniqueid, replica_id, &finished, num_changes_sent);
  1730. }
  1731. else if (message_id) {
  1732. /* Queue the details for pickup later in the response thread */
  1733. repl5_inc_operation *sop = NULL;
  1734. sop = repl5_inc_operation_new();
  1735. PL_strncpyz(sop->csn_str, csn_str, sizeof(sop->csn_str));
  1736. sop->ldap_message_id = message_id;
  1737. sop->operation_type = entry.op->operation_type;
  1738. sop->replica_id = replica_id;
  1739. PL_strncpyz(sop->uniqueid, uniqueid, sizeof(sop->uniqueid));
  1740. repl5_int_push_operation(rd,sop);
  1741. repl5_inc_flow_control_results(prp->agmt, rd);
  1742. } else {
  1743. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1744. "%s: Skipping update operation with no message_id (uniqueid %s, CSN %s):\n",
  1745. agmt_get_long_name(prp->agmt),
  1746. entry.op->target_address.uniqueid, csn_str);
  1747. agmt_inc_last_update_changecount (prp->agmt, csn_get_replicaid(entry.op->csn), 1 /*skipped*/);
  1748. }
  1749. }
  1750. break;
  1751. case CL5_BAD_DATA:
  1752. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1753. "%s: Invalid parameter passed to cl5GetNextOperationToReplay\n",
  1754. agmt_get_long_name(prp->agmt));
  1755. return_value = UPDATE_FATAL_ERROR;
  1756. finished = 1;
  1757. break;
  1758. case CL5_NOTFOUND:
  1759. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1760. "%s: No more updates to send (cl5GetNextOperationToReplay)\n",
  1761. agmt_get_long_name(prp->agmt));
  1762. return_value = UPDATE_NO_MORE_UPDATES;
  1763. finished = 1;
  1764. break;
  1765. case CL5_DB_ERROR:
  1766. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1767. "%s: A database error occurred (cl5GetNextOperationToReplay)\n",
  1768. agmt_get_long_name(prp->agmt));
  1769. return_value = UPDATE_FATAL_ERROR;
  1770. finished = 1;
  1771. break;
  1772. case CL5_BAD_FORMAT:
  1773. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1774. "%s: A malformed changelog entry was encountered (cl5GetNextOperationToReplay)\n",
  1775. agmt_get_long_name(prp->agmt));
  1776. break;
  1777. case CL5_MEMORY_ERROR:
  1778. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1779. "%s: A memory allocation error occurred (cl5GetNextOperationToRepla)\n",
  1780. agmt_get_long_name(prp->agmt));
  1781. return_value = UPDATE_FATAL_ERROR;
  1782. break;
  1783. case CL5_IGNORE_OP:
  1784. break;
  1785. default:
  1786. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1787. "%s: Unknown error code (%d) returned from cl5GetNextOperationToReplay\n",
  1788. agmt_get_long_name(prp->agmt), rc);
  1789. return_value = UPDATE_TRANSIENT_ERROR;
  1790. break;
  1791. }
  1792. /* Check for protocol shutdown */
  1793. if (prp->terminate)
  1794. {
  1795. return_value = UPDATE_NO_MORE_UPDATES;
  1796. finished = 1;
  1797. }
  1798. if (*num_changes_sent >= MAX_CHANGES_PER_SESSION)
  1799. {
  1800. return_value = UPDATE_YIELD;
  1801. finished = 1;
  1802. }
  1803. PR_Lock(rd->lock);
  1804. /* See if the result thread has hit a problem */
  1805. if (!finished && rd->abort)
  1806. {
  1807. return_value = rd->result;
  1808. finished = 1;
  1809. }
  1810. PR_Unlock(rd->lock);
  1811. } while (!finished);
  1812. /* Terminate the results reading thread */
  1813. if (!prp->repl50consumer)
  1814. {
  1815. /* We need to ensure that we wait until all the responses have been received from our operations */
  1816. if (return_value != UPDATE_CONNECTION_LOST) {
  1817. /* if connection was lost/closed, there will be nothing to read */
  1818. repl5_inc_waitfor_async_results(rd);
  1819. }
  1820. rc = repl5_inc_destroy_async_result_thread(rd);
  1821. if (rc) {
  1822. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name, "%s: repl5_inc_run: "
  1823. "repl5_tot_destroy_async_result_thread failed; error - %d\n",
  1824. agmt_get_long_name(prp->agmt), rc);
  1825. }
  1826. *num_changes_sent = rd->num_changes_sent;
  1827. }
  1828. PR_Lock(rd->lock);
  1829. if (rd->flowcontrol_detection) {
  1830. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1831. "%s: Incremental update flow control triggered %d times\n"
  1832. "You may increase %s and/or decrease %s in the replica agreement configuration\n",
  1833. agmt_get_long_name(prp->agmt),
  1834. rd->flowcontrol_detection,
  1835. type_nsds5ReplicaFlowControlPause,
  1836. type_nsds5ReplicaFlowControlWindow);
  1837. }
  1838. PR_Unlock(rd->lock);
  1839. repl5_inc_rd_destroy(&rd);
  1840. cl5_operation_parameters_done ( entry.op );
  1841. cl5DestroyReplayIterator(&changelog_iterator);
  1842. }
  1843. return return_value;
  1844. }
  1845. /*
  1846. * XXXggood this should probably be in the superclass, since the full update
  1847. * protocol is going to need it too.
  1848. */
  1849. static int
  1850. repl5_inc_stop(Private_Repl_Protocol *prp)
  1851. {
  1852. PRIntervalTime start, maxwait, now;
  1853. Replica *replica = NULL;
  1854. PRUint64 timeout;
  1855. int return_value;
  1856. if((timeout = agmt_get_protocol_timeout(prp->agmt)) == 0){
  1857. timeout = DEFAULT_PROTOCOL_TIMEOUT;
  1858. if(prp->replica_object){
  1859. object_acquire(prp->replica_object);
  1860. replica = object_get_data(prp->replica_object);
  1861. if((timeout = replica_get_protocol_timeout(replica)) == 0){
  1862. timeout = DEFAULT_PROTOCOL_TIMEOUT;
  1863. }
  1864. object_release(prp->replica_object);
  1865. }
  1866. }
  1867. maxwait = PR_SecondsToInterval(timeout);
  1868. prp->terminate = 1;
  1869. event_notify(prp, EVENT_PROTOCOL_SHUTDOWN);
  1870. start = PR_IntervalNow();
  1871. now = start;
  1872. while (!prp->stopped && ((now - start) < maxwait))
  1873. {
  1874. DS_Sleep(PR_MillisecondsToInterval(100));
  1875. now = PR_IntervalNow();
  1876. }
  1877. if (!prp->stopped)
  1878. {
  1879. /* Isn't listening. Do something drastic. */
  1880. return_value = -1;
  1881. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1882. "%s: repl5_inc_stop: protocol does not stop after %llu seconds\n",
  1883. agmt_get_long_name(prp->agmt), (long long unsigned int)timeout);
  1884. }
  1885. else
  1886. {
  1887. return_value = 0;
  1888. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1889. "%s: repl5_inc_stop: protocol stopped after %d seconds\n",
  1890. agmt_get_long_name(prp->agmt),
  1891. PR_IntervalToSeconds(now-start));
  1892. }
  1893. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1894. if (NULL == prp->replica_object) {
  1895. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1896. "%s: repl5_inc_stop: protocol replica_object is NULL\n",
  1897. agmt_get_long_name(prp->agmt));
  1898. } else {
  1899. Replica *replica;
  1900. object_acquire(prp->replica_object);
  1901. replica = object_get_data(prp->replica_object);
  1902. if (NULL == replica) {
  1903. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1904. "%s: repl5_inc_stop: replica is NULL\n",
  1905. agmt_get_long_name(prp->agmt));
  1906. } else {
  1907. Object *ruv_obj = replica_get_ruv(replica);
  1908. if (NULL == ruv_obj) {
  1909. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1910. "%s: repl5_inc_stop: ruv_obj is NULL\n",
  1911. agmt_get_long_name(prp->agmt));
  1912. } else {
  1913. RUV *ruv;
  1914. object_acquire(ruv_obj);
  1915. ruv = (RUV*)object_get_data (ruv_obj);
  1916. if (NULL == ruv) {
  1917. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1918. "%s: repl5_inc_stop: ruv is NULL\n",
  1919. agmt_get_long_name(prp->agmt));
  1920. } else {
  1921. ruv_dump(ruv, "Database RUV", NULL);
  1922. }
  1923. object_release(ruv_obj);
  1924. }
  1925. }
  1926. object_release(prp->replica_object);
  1927. }
  1928. }
  1929. return return_value;
  1930. }
  1931. static int
  1932. repl5_inc_status(Private_Repl_Protocol *prp)
  1933. {
  1934. int return_value = 0;
  1935. return return_value;
  1936. }
  1937. static void
  1938. repl5_inc_notify_update(Private_Repl_Protocol *prp)
  1939. {
  1940. event_notify(prp, EVENT_TRIGGERING_CRITERIA_MET);
  1941. }
  1942. static void
  1943. repl5_inc_update_now(Private_Repl_Protocol *prp)
  1944. {
  1945. event_notify(prp, EVENT_REPLICATE_NOW);
  1946. }
  1947. static void
  1948. repl5_inc_notify_agmt_changed(Private_Repl_Protocol *prp)
  1949. {
  1950. event_notify(prp, EVENT_AGMT_CHANGED);
  1951. }
  1952. static void
  1953. repl5_inc_notify_window_opened (Private_Repl_Protocol *prp)
  1954. {
  1955. event_notify(prp, EVENT_WINDOW_OPENED);
  1956. }
  1957. static void
  1958. repl5_inc_notify_window_closed (Private_Repl_Protocol *prp)
  1959. {
  1960. event_notify(prp, EVENT_WINDOW_CLOSED);
  1961. }
  1962. Private_Repl_Protocol *
  1963. Repl_5_Inc_Protocol_new(Repl_Protocol *rp)
  1964. {
  1965. repl5_inc_private *rip = NULL;
  1966. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)slapi_ch_malloc(sizeof(Private_Repl_Protocol));
  1967. prp->delete = repl5_inc_delete;
  1968. prp->run = repl5_inc_run;
  1969. prp->stop = repl5_inc_stop;
  1970. prp->status = repl5_inc_status;
  1971. prp->notify_update = repl5_inc_notify_update;
  1972. prp->notify_agmt_changed = repl5_inc_notify_agmt_changed;
  1973. prp->notify_window_opened = repl5_inc_notify_window_opened;
  1974. prp->notify_window_closed = repl5_inc_notify_window_closed;
  1975. prp->update_now = repl5_inc_update_now;
  1976. prp->replica_object = prot_get_replica_object(rp);
  1977. if ((prp->lock = PR_NewLock()) == NULL)
  1978. {
  1979. goto loser;
  1980. }
  1981. if ((prp->cvar = PR_NewCondVar(prp->lock)) == NULL)
  1982. {
  1983. goto loser;
  1984. }
  1985. prp->stopped = 0;
  1986. prp->terminate = 0;
  1987. prp->eventbits = 0;
  1988. prp->conn = prot_get_connection(rp);
  1989. prp->agmt = prot_get_agreement(rp);
  1990. prp->last_acquire_response_code = NSDS50_REPL_REPLICA_READY;
  1991. rip = (void *)slapi_ch_malloc(sizeof(repl5_inc_private));
  1992. rip->ruv = NULL;
  1993. rip->backoff = NULL;
  1994. rip->rp = rp;
  1995. prp->private = (void *)rip;
  1996. prp->replica_acquired = PR_FALSE;
  1997. prp->repl50consumer = 0;
  1998. prp->repl71consumer = 0;
  1999. prp->repl90consumer = 0;
  2000. return prp;
  2001. loser:
  2002. repl5_inc_delete(&prp);
  2003. return NULL;
  2004. }
  2005. static void
  2006. repl5_inc_backoff_expired(time_t timer_fire_time, void *arg)
  2007. {
  2008. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)arg;
  2009. PR_ASSERT(NULL != prp);
  2010. event_notify(prp, EVENT_BACKOFF_EXPIRED);
  2011. }
  2012. /*
  2013. * Examine the update vector and determine our course of action.
  2014. * There are 3 different possibilities, plus a catch-all error:
  2015. * 1 - no update vector (ruv is NULL). The consumer's replica is
  2016. * pristine, so it needs to be initialized. Return
  2017. * EXAMINE_RUV_PRISTINE_REPLICA.
  2018. * 2 - ruv is present, but its database generation ID doesn't
  2019. * match the local generation ID. This means that either
  2020. * the local replica must be reinitialized from the remote
  2021. * replica or vice-versa. Return
  2022. * EXAMINE_RUV_GENERATION_MISMATCH.
  2023. * 3 - ruv is present, and we have all updates needed to bring
  2024. * the replica up to date using the incremental protocol.
  2025. * return EXAMINE_RUV_OK.
  2026. * 4 - parameter error. Return EXAMINE_RUV_PARAM_ERROR
  2027. */
  2028. static int
  2029. examine_update_vector(Private_Repl_Protocol *prp, RUV *remote_ruv)
  2030. {
  2031. int return_value;
  2032. PR_ASSERT(NULL != prp);
  2033. if (NULL == prp)
  2034. {
  2035. return_value = EXAMINE_RUV_PARAM_ERROR;
  2036. }
  2037. else if (NULL == remote_ruv)
  2038. {
  2039. return_value = EXAMINE_RUV_PRISTINE_REPLICA;
  2040. }
  2041. else
  2042. {
  2043. char *local_gen = NULL;
  2044. char *remote_gen = ruv_get_replica_generation(remote_ruv);
  2045. Object *local_ruv_obj;
  2046. RUV *local_ruv;
  2047. Replica *replica;
  2048. PR_ASSERT(NULL != prp->replica_object);
  2049. replica = object_get_data(prp->replica_object);
  2050. PR_ASSERT(NULL != replica);
  2051. local_ruv_obj = replica_get_ruv (replica);
  2052. if (NULL != local_ruv_obj)
  2053. {
  2054. local_ruv = (RUV*)object_get_data (local_ruv_obj);
  2055. PR_ASSERT (local_ruv);
  2056. local_gen = ruv_get_replica_generation(local_ruv);
  2057. object_release (local_ruv_obj);
  2058. }
  2059. if (NULL == remote_gen || NULL == local_gen || strcmp(remote_gen, local_gen) != 0)
  2060. {
  2061. return_value = EXAMINE_RUV_GENERATION_MISMATCH;
  2062. }
  2063. else
  2064. {
  2065. return_value = EXAMINE_RUV_OK;
  2066. }
  2067. slapi_ch_free((void**)&remote_gen);
  2068. slapi_ch_free((void**)&local_gen);
  2069. }
  2070. return return_value;
  2071. }
  2072. /*
  2073. * When we get an error from an LDAP operation, we call this
  2074. * function to decide if we should just keep replaying
  2075. * updates, or if we should stop, back off, and try again
  2076. * later.
  2077. * Returns PR_TRUE if we shoould keep going, PR_FALSE if
  2078. * we should back off and try again later.
  2079. *
  2080. * In general, we keep going if the return code is consistent
  2081. * with some sort of bug in URP that causes the consumer to
  2082. * emit an error code that it shouldn't have, e.g. LDAP_ALREADY_EXISTS.
  2083. *
  2084. * We stop if there's some indication that the server just completely
  2085. * failed to process the operation, e.g. LDAP_OPERATIONS_ERROR.
  2086. */
  2087. static PRBool
  2088. ignore_error_and_keep_going(int error)
  2089. {
  2090. int return_value = PR_FALSE;
  2091. switch (error)
  2092. {
  2093. /* Cases where we keep going */
  2094. case LDAP_SUCCESS:
  2095. case LDAP_NO_SUCH_ATTRIBUTE:
  2096. case LDAP_UNDEFINED_TYPE:
  2097. case LDAP_CONSTRAINT_VIOLATION:
  2098. case LDAP_TYPE_OR_VALUE_EXISTS:
  2099. case LDAP_INVALID_SYNTAX:
  2100. case LDAP_NO_SUCH_OBJECT:
  2101. case LDAP_INVALID_DN_SYNTAX:
  2102. case LDAP_IS_LEAF:
  2103. case LDAP_INSUFFICIENT_ACCESS:
  2104. case LDAP_NAMING_VIOLATION:
  2105. case LDAP_OBJECT_CLASS_VIOLATION:
  2106. case LDAP_NOT_ALLOWED_ON_NONLEAF:
  2107. case LDAP_NOT_ALLOWED_ON_RDN:
  2108. case LDAP_ALREADY_EXISTS:
  2109. case LDAP_NO_OBJECT_CLASS_MODS:
  2110. return_value = PR_TRUE;
  2111. break;
  2112. /* Cases where we stop and retry */
  2113. case LDAP_OPERATIONS_ERROR:
  2114. case LDAP_PROTOCOL_ERROR:
  2115. case LDAP_TIMELIMIT_EXCEEDED:
  2116. case LDAP_SIZELIMIT_EXCEEDED:
  2117. case LDAP_STRONG_AUTH_NOT_SUPPORTED:
  2118. case LDAP_STRONG_AUTH_REQUIRED:
  2119. case LDAP_PARTIAL_RESULTS:
  2120. case LDAP_REFERRAL:
  2121. case LDAP_ADMINLIMIT_EXCEEDED:
  2122. case LDAP_UNAVAILABLE_CRITICAL_EXTENSION:
  2123. case LDAP_CONFIDENTIALITY_REQUIRED:
  2124. case LDAP_SASL_BIND_IN_PROGRESS:
  2125. case LDAP_INAPPROPRIATE_MATCHING:
  2126. case LDAP_ALIAS_PROBLEM:
  2127. case LDAP_ALIAS_DEREF_PROBLEM:
  2128. case LDAP_INAPPROPRIATE_AUTH:
  2129. case LDAP_INVALID_CREDENTIALS:
  2130. case LDAP_BUSY:
  2131. case LDAP_UNAVAILABLE:
  2132. case LDAP_UNWILLING_TO_PERFORM:
  2133. case LDAP_LOOP_DETECT:
  2134. case LDAP_SORT_CONTROL_MISSING:
  2135. case LDAP_INDEX_RANGE_ERROR:
  2136. case LDAP_RESULTS_TOO_LARGE:
  2137. case LDAP_AFFECTS_MULTIPLE_DSAS:
  2138. case LDAP_OTHER:
  2139. case LDAP_SERVER_DOWN:
  2140. case LDAP_LOCAL_ERROR:
  2141. case LDAP_ENCODING_ERROR:
  2142. case LDAP_DECODING_ERROR:
  2143. case LDAP_TIMEOUT:
  2144. case LDAP_AUTH_UNKNOWN:
  2145. case LDAP_FILTER_ERROR:
  2146. case LDAP_USER_CANCELLED:
  2147. case LDAP_PARAM_ERROR:
  2148. case LDAP_NO_MEMORY:
  2149. case LDAP_CONNECT_ERROR:
  2150. case LDAP_NOT_SUPPORTED:
  2151. case LDAP_CONTROL_NOT_FOUND:
  2152. case LDAP_NO_RESULTS_RETURNED:
  2153. case LDAP_MORE_RESULTS_TO_RETURN:
  2154. case LDAP_CLIENT_LOOP:
  2155. case LDAP_REFERRAL_LIMIT_EXCEEDED:
  2156. return_value = PR_FALSE;
  2157. break;
  2158. }
  2159. return return_value;
  2160. }
  2161. /* this function converts a state to its name - for debug output */
  2162. static const char*
  2163. state2name (int state)
  2164. {
  2165. switch (state)
  2166. {
  2167. case STATE_START: return "start";
  2168. case STATE_WAIT_WINDOW_OPEN: return "wait_for_window_to_open";
  2169. case STATE_WAIT_CHANGES: return "wait_for_changes";
  2170. case STATE_READY_TO_ACQUIRE: return "ready_to_acquire_replica";
  2171. case STATE_BACKOFF_START: return "start_backoff";
  2172. case STATE_BACKOFF: return "backoff";
  2173. case STATE_SENDING_UPDATES: return "sending_updates";
  2174. case STATE_STOP_FATAL_ERROR: return "stop_fatal_error";
  2175. case STATE_STOP_FATAL_ERROR_PART2: return "stop_fatal_error";
  2176. case STATE_STOP_NORMAL_TERMINATION: return "stop_normal_termination";
  2177. default: return "invalid_state";
  2178. }
  2179. }
  2180. /* this function convert s an event to its name - for debug output */
  2181. static const char*
  2182. event2name (int event)
  2183. {
  2184. switch (event)
  2185. {
  2186. case EVENT_WINDOW_OPENED: return "update_window_opened";
  2187. case EVENT_WINDOW_CLOSED: return "update_window_closed";
  2188. case EVENT_TRIGGERING_CRITERIA_MET: return "data_modified";
  2189. case EVENT_BACKOFF_EXPIRED: return "backoff_timer_expired";
  2190. case EVENT_REPLICATE_NOW: return "replicate_now";
  2191. case EVENT_PROTOCOL_SHUTDOWN: return "protocol_shutdown";
  2192. case EVENT_AGMT_CHANGED: return "agreement_changed";
  2193. default: return "invalid_event";
  2194. }
  2195. }
  2196. static const char*
  2197. op2string(int op)
  2198. {
  2199. switch (op) {
  2200. case SLAPI_OPERATION_ADD:
  2201. return "add";
  2202. case SLAPI_OPERATION_MODIFY:
  2203. return "modify";
  2204. case SLAPI_OPERATION_DELETE:
  2205. return "delete";
  2206. case SLAPI_OPERATION_MODRDN:
  2207. return "rename";
  2208. case SLAPI_OPERATION_EXTENDED:
  2209. return "extended";
  2210. }
  2211. return "unknown";
  2212. }
  2213. void
  2214. repl5_set_backoff_min(Private_Repl_Protocol *prp, int min)
  2215. {
  2216. Replica *replica;
  2217. replica = (Replica *)object_get_data(prp->replica_object);
  2218. if(replica){
  2219. replica_set_backoff_min(replica, min);
  2220. }
  2221. }
  2222. void
  2223. repl5_set_backoff_max(Private_Repl_Protocol *prp, int max)
  2224. {
  2225. Replica *replica;
  2226. replica = object_get_data(prp->replica_object);
  2227. if(replica){
  2228. replica_set_backoff_max(replica, max);
  2229. }
  2230. }
  2231. int
  2232. repl5_get_backoff_min(Private_Repl_Protocol *prp)
  2233. {
  2234. Replica *replica;
  2235. replica = object_get_data(prp->replica_object);
  2236. if(replica){
  2237. return (int)replica_get_backoff_min(replica);
  2238. }
  2239. return PROTOCOL_BACKOFF_MINIMUM;
  2240. }
  2241. int
  2242. repl5_get_backoff_max(Private_Repl_Protocol *prp)
  2243. {
  2244. Replica *replica;
  2245. replica = object_get_data(prp->replica_object);
  2246. if(replica){
  2247. return (int)replica_get_backoff_max(replica);
  2248. }
  2249. return PROTOCOL_BACKOFF_MAXIMUM;
  2250. }