repl5_protocol.c 12 KB

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