repl5_protocol.c 12 KB

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