repl5_protocol.c 14 KB

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