repl5_protocol.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /* repl5_protocol.c */
  7. /*
  8. The replication protocol object manages the replication protocol for
  9. a given replica. It determines which protocol(s) are appropriate to
  10. use when updating a given replica. It also knows how to arbitrate
  11. incremental and total update protocols for a given replica.
  12. */
  13. #include "repl5.h"
  14. #include "repl5_prot_private.h"
  15. #define PROTOCOL_5_INCREMENTAL 1
  16. #define PROTOCOL_5_TOTAL 2
  17. #define PROTOCOL_4_INCREMENTAL 3
  18. #define PROTOCOL_4_TOTAL 4
  19. typedef struct repl_protocol
  20. {
  21. Private_Repl_Protocol *prp_incremental; /* inc protocol to use */
  22. Private_Repl_Protocol *prp_total; /* total protocol to use */
  23. Private_Repl_Protocol *prp_active_protocol; /* Pointer to active protocol */
  24. Repl_Agmt *agmt; /* The replication agreement we're servicing */
  25. Repl_Connection *conn; /* Connection to remote server */
  26. Object *replica_object; /* Local replica. If non-NULL, replica object is acquired */
  27. int state;
  28. int next_state;
  29. PRLock *lock;
  30. } repl_protocol;
  31. /* States */
  32. #define STATE_FINISHED 503
  33. #define STATE_BAD_STATE_SHOULD_NEVER_HAPPEN 599
  34. /* Forward declarations */
  35. static Private_Repl_Protocol *private_protocol_factory(Repl_Protocol *rp, int type);
  36. /*
  37. * Create a new protocol instance.
  38. */
  39. Repl_Protocol *
  40. prot_new(Repl_Agmt *agmt, int protocol_state)
  41. {
  42. Slapi_DN *replarea_sdn = NULL;
  43. Repl_Protocol *rp = (Repl_Protocol *)slapi_ch_malloc(sizeof(Repl_Protocol));
  44. rp->prp_incremental = rp->prp_total = rp->prp_active_protocol = NULL;
  45. if (protocol_state == STATE_PERFORMING_TOTAL_UPDATE)
  46. {
  47. rp->state = STATE_PERFORMING_TOTAL_UPDATE;
  48. }
  49. else
  50. {
  51. rp->state = STATE_PERFORMING_INCREMENTAL_UPDATE;
  52. }
  53. rp->next_state = STATE_PERFORMING_INCREMENTAL_UPDATE;
  54. if ((rp->lock = PR_NewLock()) == NULL)
  55. {
  56. goto loser;
  57. }
  58. rp->agmt = agmt;
  59. if ((rp->conn = conn_new(agmt)) == NULL)
  60. {
  61. goto loser;
  62. }
  63. /* Acquire the local replica object */
  64. replarea_sdn = agmt_get_replarea(agmt);
  65. rp->replica_object = replica_get_replica_from_dn(replarea_sdn);
  66. if (NULL == rp->replica_object)
  67. {
  68. /* Whoa, no local replica!?!? */
  69. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  70. "%s: Unable to locate replica object for local replica %s\n",
  71. agmt_get_long_name(agmt),
  72. slapi_sdn_get_dn(replarea_sdn));
  73. goto loser;
  74. }
  75. rp->prp_incremental = private_protocol_factory(rp, PROTOCOL_5_INCREMENTAL);
  76. rp->prp_total = private_protocol_factory(rp, PROTOCOL_5_TOTAL);
  77. /* XXXggood register callback handlers for entries updated, and
  78. schedule window enter/leave. */
  79. slapi_sdn_free(&replarea_sdn);
  80. return rp;
  81. loser:
  82. prot_delete(&rp);
  83. return NULL;
  84. }
  85. Object *
  86. prot_get_replica_object(Repl_Protocol *rp)
  87. {
  88. PR_ASSERT(NULL != rp);
  89. return rp->replica_object;
  90. }
  91. Repl_Agmt *
  92. prot_get_agreement(Repl_Protocol *rp)
  93. {
  94. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  95. if (NULL == rp) return NULL;
  96. return rp->agmt;
  97. }
  98. void
  99. prot_free(Repl_Protocol **rpp)
  100. {
  101. Repl_Protocol *rp = NULL;
  102. if (rpp == NULL || *rpp == NULL) return;
  103. rp = *rpp;
  104. PR_Lock(rp->lock);
  105. if (NULL != rp->prp_incremental)
  106. {
  107. rp->prp_incremental->delete(&rp->prp_incremental);
  108. }
  109. if (NULL != rp->prp_total)
  110. {
  111. rp->prp_total->delete(&rp->prp_total);
  112. }
  113. if (NULL != rp->replica_object)
  114. {
  115. object_release(rp->replica_object);
  116. }
  117. if (NULL != rp->conn)
  118. {
  119. conn_delete(rp->conn);
  120. }
  121. rp->prp_active_protocol = NULL;
  122. PR_Unlock(rp->lock);
  123. slapi_ch_free((void **)rpp);
  124. }
  125. /*
  126. * Destroy a protocol instance XXXggood not complete
  127. */
  128. void
  129. prot_delete(Repl_Protocol **rpp)
  130. {
  131. Repl_Protocol *rp;
  132. PR_ASSERT(NULL != rpp);
  133. rp = *rpp;
  134. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  135. if (NULL != rp)
  136. {
  137. prot_stop(rp);
  138. prot_free(rpp);
  139. }
  140. }
  141. /*
  142. * Get the connection object.
  143. */
  144. Repl_Connection *
  145. prot_get_connection(Repl_Protocol *rp)
  146. {
  147. Repl_Connection *return_value;
  148. PR_ASSERT(NULL != rp);
  149. PR_Lock(rp->lock);
  150. return_value = rp->conn;
  151. PR_Unlock(rp->lock);
  152. return return_value;
  153. }
  154. /*
  155. * This function causes the total protocol to start.
  156. * This is accomplished by registering a state transition
  157. * to a new state, and then signaling the incremental
  158. * protocol to stop.
  159. */
  160. void
  161. prot_initialize_replica(Repl_Protocol *rp)
  162. {
  163. PR_ASSERT(NULL != rp);
  164. PR_Lock(rp->lock);
  165. /* check that total protocol is not running */
  166. rp->next_state = STATE_PERFORMING_TOTAL_UPDATE;
  167. /* Stop the incremental protocol, if running */
  168. rp->prp_incremental->stop(rp->prp_incremental);
  169. if (rp->prp_total) agmt_set_last_init_status(rp->prp_total->agmt, 0, 0, NULL);
  170. PR_Unlock(rp->lock);
  171. }
  172. /*
  173. * Main thread for protocol manager.
  174. This is a simple state machine. State transition table:
  175. Initial state: incremental update
  176. STATE EVENT NEXT STATE
  177. ----- ----- ----------
  178. incremental update shutdown finished
  179. incremental update total update requested total update
  180. total update shutdown finished
  181. total update update complete incremental update
  182. finished (any) finished
  183. */
  184. static void
  185. prot_thread_main(void *arg)
  186. {
  187. Repl_Protocol *rp = (Repl_Protocol *)arg;
  188. int done;
  189. PR_ASSERT(NULL != rp);
  190. if (rp->agmt) {
  191. set_thread_private_agmtname (agmt_get_long_name(rp->agmt));
  192. }
  193. done = 0;
  194. while (!done)
  195. {
  196. switch (rp->state)
  197. {
  198. case STATE_PERFORMING_INCREMENTAL_UPDATE:
  199. /* Run the incremental update protocol */
  200. PR_Lock(rp->lock);
  201. dev_debug("prot_thread_main(STATE_PERFORMING_INCREMENTAL_UPDATE): begin");
  202. rp->prp_active_protocol = rp->prp_incremental;
  203. PR_Unlock(rp->lock);
  204. rp->prp_incremental->run(rp->prp_incremental);
  205. dev_debug("prot_thread_main(STATE_PERFORMING_INCREMENTAL_UPDATE): end");
  206. break;
  207. case STATE_PERFORMING_TOTAL_UPDATE:
  208. PR_Lock(rp->lock);
  209. /* stop incremental protocol if running */
  210. rp->prp_active_protocol = rp->prp_total;
  211. /* After total protocol finished, return to incremental */
  212. rp->next_state = STATE_PERFORMING_INCREMENTAL_UPDATE;
  213. PR_Unlock(rp->lock);
  214. /* Run the total update protocol */
  215. dev_debug("prot_thread_main(STATE_PERFORMING_TOTAL_UPDATE): begin");
  216. rp->prp_total->run(rp->prp_total);
  217. dev_debug("prot_thread_main(STATE_PERFORMING_TOTAL_UPDATE): end");
  218. /* update the agreement entry to notify clients that
  219. replica initialization is completed. */
  220. agmt_replica_init_done (rp->agmt);
  221. break;
  222. case STATE_FINISHED:
  223. dev_debug("prot_thread_main(STATE_FINISHED): exiting prot_thread_main");
  224. done = 1;
  225. break;
  226. }
  227. rp->state = rp->next_state;
  228. }
  229. }
  230. /*
  231. * Start a thread to handle the replication protocol.
  232. */
  233. void
  234. prot_start(Repl_Protocol *rp)
  235. {
  236. PR_ASSERT(NULL != rp);
  237. if (NULL != rp)
  238. {
  239. if (PR_CreateThread(PR_USER_THREAD, prot_thread_main, (void *)rp,
  240. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE) == NULL)
  241. {
  242. PRErrorCode prerr = PR_GetError();
  243. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  244. "%s: Unable to create protocol thread; NSPR error - %d, %s\n",
  245. agmt_get_long_name(rp->agmt),
  246. prerr, slapd_pr_strerror(prerr));
  247. }
  248. }
  249. else
  250. {
  251. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Unable to start "
  252. "protocol object - NULL protocol object passed to prot_start.\n");
  253. }
  254. }
  255. /*
  256. * Stop a protocol instance.
  257. */
  258. void
  259. prot_stop(Repl_Protocol *rp)
  260. {
  261. PR_ASSERT(NULL != rp);
  262. if (NULL != rp)
  263. {
  264. PR_Lock(rp->lock);
  265. rp->next_state = STATE_FINISHED;
  266. if (NULL != rp->prp_incremental)
  267. {
  268. if (rp->prp_incremental->stop(rp->prp_incremental) != 0)
  269. {
  270. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  271. "Warning: incremental protocol for replica \"%s\" "
  272. "did not shut down properly.\n",
  273. agmt_get_long_name(rp->agmt));
  274. }
  275. }
  276. if (NULL != rp->prp_total)
  277. {
  278. if (rp->prp_total->stop(rp->prp_total) != 0)
  279. {
  280. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  281. "Warning: total protocol for replica \"%s\" "
  282. "did not shut down properly.\n",
  283. agmt_get_long_name(rp->agmt));
  284. }
  285. }
  286. PR_Unlock(rp->lock);
  287. }
  288. else
  289. {
  290. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Error: prot_stop() "
  291. " called on NULL protocol instance.\n");
  292. }
  293. }
  294. /*
  295. * Call the notify_update method of the incremental or total update
  296. * protocol, is either is active.
  297. */
  298. void
  299. prot_notify_update(Repl_Protocol *rp)
  300. {
  301. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  302. if (NULL == rp) return;
  303. PR_Lock(rp->lock);
  304. if (NULL != rp->prp_active_protocol)
  305. {
  306. rp->prp_active_protocol->notify_update(rp->prp_active_protocol);
  307. }
  308. PR_Unlock(rp->lock);
  309. }
  310. /*
  311. * Call the notify_agmt_changed method of the incremental or total update
  312. * protocol, is either is active.
  313. */
  314. void
  315. prot_notify_agmt_changed(Repl_Protocol *rp, char * agmt_name)
  316. {
  317. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  318. if (NULL == rp) {
  319. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  320. "Replication agreement for %s could not be updated. "
  321. "For replication to take place, please enable the suffix "
  322. "and restart the server\n", agmt_name);
  323. return;
  324. }
  325. PR_Lock(rp->lock);
  326. if (NULL != rp->prp_active_protocol)
  327. {
  328. rp->prp_active_protocol->notify_agmt_changed(rp->prp_active_protocol);
  329. }
  330. PR_Unlock(rp->lock);
  331. }
  332. void
  333. prot_notify_window_opened (Repl_Protocol *rp)
  334. {
  335. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  336. if (NULL == rp) return;
  337. PR_Lock(rp->lock);
  338. if (NULL != rp->prp_active_protocol)
  339. {
  340. rp->prp_active_protocol->notify_window_opened(rp->prp_active_protocol);
  341. }
  342. PR_Unlock(rp->lock);
  343. }
  344. void
  345. prot_notify_window_closed (Repl_Protocol *rp)
  346. {
  347. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  348. if (NULL == rp) return;
  349. PR_Lock(rp->lock);
  350. if (NULL != rp->prp_active_protocol)
  351. {
  352. rp->prp_active_protocol->notify_window_closed(rp->prp_active_protocol);
  353. }
  354. PR_Unlock(rp->lock);
  355. }
  356. int
  357. prot_status(Repl_Protocol *rp)
  358. {
  359. int return_status = PROTOCOL_STATUS_UNKNOWN;
  360. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  361. if (NULL != rp)
  362. {
  363. PR_Lock(rp->lock);
  364. if (NULL != rp->prp_active_protocol)
  365. {
  366. return_status = rp->prp_active_protocol->status(rp->prp_active_protocol);
  367. }
  368. PR_Unlock(rp->lock);
  369. }
  370. return return_status;
  371. }
  372. /*
  373. * Start an incremental protocol session, even if we're not
  374. * currently in a schedule window.
  375. * If the total protocol is active, do nothing.
  376. * Otherwise, notify the incremental protocol that it should
  377. * run once.
  378. */
  379. void
  380. prot_replicate_now(Repl_Protocol *rp)
  381. {
  382. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  383. if (NULL != rp)
  384. {
  385. PR_Lock(rp->lock);
  386. if (rp->prp_incremental == rp->prp_active_protocol)
  387. {
  388. rp->prp_active_protocol->update_now(rp->prp_active_protocol);
  389. }
  390. PR_Unlock(rp->lock);
  391. }
  392. }
  393. /*
  394. * A little factory function to create a protocol
  395. * instance of the correct type.
  396. */
  397. static Private_Repl_Protocol *
  398. private_protocol_factory(Repl_Protocol *rp, int type)
  399. {
  400. Private_Repl_Protocol *prp;
  401. switch (type)
  402. {
  403. case PROTOCOL_5_INCREMENTAL:
  404. prp = Repl_5_Inc_Protocol_new(rp);
  405. break;
  406. case PROTOCOL_5_TOTAL:
  407. prp = Repl_5_Tot_Protocol_new(rp);
  408. break;
  409. }
  410. return prp;
  411. }