repl5_inc_protocol.c 86 KB

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