repl5_protocol.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. rp->state = STATE_PERFORMING_TOTAL_UPDATE;
  57. } else {
  58. rp->state = STATE_PERFORMING_INCREMENTAL_UPDATE;
  59. }
  60. rp->next_state = STATE_PERFORMING_INCREMENTAL_UPDATE;
  61. if ((rp->lock = PR_NewLock()) == NULL) {
  62. goto loser;
  63. }
  64. rp->agmt = agmt;
  65. rp->conn = NULL;
  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. /* Whoa, no local replica!?!? */
  71. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  72. "prot_new - %s: Unable to locate replica object for local replica %s\n",
  73. agmt_get_long_name(agmt),
  74. slapi_sdn_get_dn(replarea_sdn));
  75. goto loser;
  76. }
  77. if (get_agmt_agreement_type(agmt) == REPLICA_TYPE_MULTIMASTER) {
  78. rp->prp_incremental = private_protocol_factory(rp, PROTOCOL_5_INCREMENTAL);
  79. rp->prp_total = private_protocol_factory(rp, PROTOCOL_5_TOTAL);
  80. rp->delete_conn = conn_delete;
  81. } else if (get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS) {
  82. rp->prp_incremental = private_protocol_factory(rp, PROTOCOL_WINDOWS_INCREMENTAL);
  83. rp->prp_total = private_protocol_factory(rp, PROTOCOL_WINDOWS_TOTAL);
  84. rp->delete_conn = windows_conn_delete;
  85. }
  86. /* XXXggood register callback handlers for entries updated, and
  87. schedule window enter/leave. */
  88. goto done;
  89. loser:
  90. prot_delete(&rp);
  91. done:
  92. slapi_sdn_free(&replarea_sdn);
  93. return rp;
  94. }
  95. Object *
  96. prot_get_replica_object(Repl_Protocol *rp)
  97. {
  98. PR_ASSERT(NULL != rp);
  99. return rp->replica_object;
  100. }
  101. Repl_Agmt *
  102. prot_get_agreement(Repl_Protocol *rp)
  103. {
  104. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  105. if (NULL == rp)
  106. 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)
  114. return;
  115. rp = *rpp;
  116. PR_Lock(rp->lock);
  117. if (NULL != rp->prp_incremental) {
  118. rp->prp_incremental->delete (&rp->prp_incremental);
  119. }
  120. if (NULL != rp->prp_total) {
  121. rp->prp_total->delete (&rp->prp_total);
  122. }
  123. if (NULL != rp->replica_object) {
  124. object_release(rp->replica_object);
  125. }
  126. if ((NULL != rp->conn) && (NULL != rp->delete_conn)) {
  127. rp->delete_conn(rp->conn);
  128. rp->conn = NULL;
  129. }
  130. rp->prp_active_protocol = NULL;
  131. PR_Unlock(rp->lock);
  132. PR_DestroyLock(rp->lock);
  133. slapi_ch_free((void **)rpp);
  134. }
  135. /*
  136. * Destroy a protocol instance XXXggood not complete
  137. */
  138. void
  139. prot_delete(Repl_Protocol **rpp)
  140. {
  141. Repl_Protocol *rp;
  142. PR_ASSERT(NULL != rpp);
  143. rp = *rpp;
  144. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  145. if (NULL != rp) {
  146. prot_stop(rp);
  147. prot_free(rpp);
  148. }
  149. }
  150. /*
  151. * Get the connection object.
  152. */
  153. Repl_Connection *
  154. prot_get_connection(Repl_Protocol *rp)
  155. {
  156. Repl_Connection *return_value;
  157. PR_ASSERT(NULL != rp);
  158. PR_Lock(rp->lock);
  159. return_value = rp->conn;
  160. PR_Unlock(rp->lock);
  161. return return_value;
  162. }
  163. /*
  164. * This function causes the total protocol to start.
  165. * This is accomplished by registering a state transition
  166. * to a new state, and then signaling the incremental
  167. * protocol to stop.
  168. */
  169. void
  170. prot_initialize_replica(Repl_Protocol *rp)
  171. {
  172. PR_ASSERT(NULL != rp);
  173. PR_Lock(rp->lock);
  174. /* check that total protocol is not running */
  175. rp->next_state = STATE_PERFORMING_TOTAL_UPDATE;
  176. /* Stop the incremental protocol, if running */
  177. rp->prp_incremental->stop(rp->prp_incremental);
  178. if (rp->prp_total)
  179. agmt_set_last_init_status(rp->prp_total->agmt, 0, 0, 0, NULL);
  180. PR_Unlock(rp->lock);
  181. }
  182. /*
  183. * Main thread for protocol manager.
  184. This is a simple state machine. State transition table:
  185. Initial state: incremental update
  186. STATE EVENT NEXT STATE
  187. ----- ----- ----------
  188. incremental update shutdown finished
  189. incremental update total update requested total update
  190. total update shutdown finished
  191. total update update complete incremental update
  192. finished (any) finished
  193. */
  194. static void
  195. prot_thread_main(void *arg)
  196. {
  197. Repl_Protocol *rp = (Repl_Protocol *)arg;
  198. int done;
  199. Repl_Agmt *agmt = NULL;
  200. PR_ASSERT(NULL != rp);
  201. agmt = rp->agmt;
  202. if (!agmt) {
  203. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "prot_thread_main - Missing replication agreement\n");
  204. return;
  205. }
  206. set_thread_private_agmtname(agmt_get_long_name(agmt));
  207. done = 0;
  208. while (!done) {
  209. switch (rp->state) {
  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. Slapi_DN *dn = agmt_get_replarea(agmt);
  221. Replica *replica = NULL;
  222. Object *replica_obj = replica_get_replica_from_dn(dn);
  223. slapi_sdn_free(&dn);
  224. if (replica_obj) {
  225. replica = (Replica *)object_get_data(replica_obj);
  226. /* If total update against this replica is in progress,
  227. * we should not initiate the total update to other replicas. */
  228. if (replica_is_state_flag_set(replica, REPLICA_TOTAL_EXCL_RECV)) {
  229. object_release(replica_obj);
  230. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  231. "prot_thread_main - %s: total update on the replica is in progress. "
  232. "Cannot initiate the total update.\n",
  233. agmt_get_long_name(rp->agmt));
  234. break;
  235. } else {
  236. replica_set_state_flag(replica, REPLICA_TOTAL_EXCL_SEND, 0);
  237. }
  238. }
  239. PR_Lock(rp->lock);
  240. /* stop incremental protocol if running */
  241. rp->prp_active_protocol = rp->prp_total;
  242. /* After total protocol finished, return to incremental */
  243. rp->next_state = STATE_PERFORMING_INCREMENTAL_UPDATE;
  244. PR_Unlock(rp->lock);
  245. /* Run the total update protocol */
  246. dev_debug("prot_thread_main(STATE_PERFORMING_TOTAL_UPDATE): begin");
  247. rp->prp_total->run(rp->prp_total);
  248. dev_debug("prot_thread_main(STATE_PERFORMING_TOTAL_UPDATE): end");
  249. /* update the agreement entry to notify clients that
  250. replica initialization is completed. */
  251. agmt_replica_init_done(agmt);
  252. if (replica_obj) {
  253. replica_set_state_flag(replica, REPLICA_TOTAL_EXCL_SEND, 1);
  254. object_release(replica_obj);
  255. }
  256. break;
  257. }
  258. case STATE_FINISHED:
  259. dev_debug("prot_thread_main(STATE_FINISHED): exiting prot_thread_main");
  260. done = 1;
  261. break;
  262. }
  263. if (agmt_has_protocol(agmt)) {
  264. rp->state = rp->next_state;
  265. } else {
  266. done = 1;
  267. }
  268. }
  269. }
  270. /*
  271. * Start a thread to handle the replication protocol.
  272. */
  273. void
  274. prot_start(Repl_Protocol *rp)
  275. {
  276. PR_ASSERT(NULL != rp);
  277. if (NULL != rp) {
  278. rp->agmt_thread = PR_CreateThread(PR_USER_THREAD, prot_thread_main, (void *)rp,
  279. #if defined(__hpux) && defined(__ia64)
  280. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 524288L);
  281. #else
  282. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  283. #endif
  284. if (rp->agmt_thread == NULL) {
  285. PRErrorCode prerr = PR_GetError();
  286. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  287. "prot_start - %s: Unable to create protocol thread; NSPR error - %d, %s\n",
  288. agmt_get_long_name(rp->agmt),
  289. prerr, slapd_pr_strerror(prerr));
  290. }
  291. } else {
  292. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "prot_start - Unable to start "
  293. "protocol object - NULL protocol object passed to prot_start.\n");
  294. }
  295. }
  296. /*
  297. * Stop a protocol instance.
  298. */
  299. void
  300. prot_stop(Repl_Protocol *rp)
  301. {
  302. PR_ASSERT(NULL != rp);
  303. if (NULL != rp) {
  304. PR_Lock(rp->lock);
  305. rp->next_state = STATE_FINISHED;
  306. if (NULL != rp->prp_incremental) {
  307. if (rp->prp_incremental->stop(rp->prp_incremental) != 0) {
  308. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name,
  309. "prot_stop - Incremental protocol for replica \"%s\" "
  310. "did not shut down properly.\n",
  311. agmt_get_long_name(rp->agmt));
  312. }
  313. }
  314. if (NULL != rp->prp_total) {
  315. if (rp->prp_total->stop(rp->prp_total) != 0) {
  316. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name,
  317. "prot_stop - Total protocol for replica \"%s\" "
  318. "did not shut down properly.\n",
  319. agmt_get_long_name(rp->agmt));
  320. }
  321. }
  322. PR_Unlock(rp->lock);
  323. if (rp->agmt_thread != NULL) {
  324. (void)PR_JoinThread(rp->agmt_thread);
  325. rp->agmt_thread = NULL;
  326. }
  327. } else {
  328. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "prot_stop - "
  329. "NULL protocol instance.\n");
  330. }
  331. }
  332. /*
  333. * Call the notify_update method of the incremental or total update
  334. * protocol, is either is active.
  335. */
  336. void
  337. prot_notify_update(Repl_Protocol *rp)
  338. {
  339. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  340. if (NULL == rp)
  341. return;
  342. PR_Lock(rp->lock);
  343. if (NULL != rp->prp_active_protocol) {
  344. rp->prp_active_protocol->notify_update(rp->prp_active_protocol);
  345. }
  346. PR_Unlock(rp->lock);
  347. }
  348. /*
  349. * Call the notify_agmt_changed method of the incremental or total update
  350. * protocol, is either is active.
  351. */
  352. void
  353. prot_notify_agmt_changed(Repl_Protocol *rp, char *agmt_name)
  354. {
  355. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  356. if (NULL == rp) {
  357. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  358. "prot_notify_agmt_changed - Replication agreement for %s could not be updated. "
  359. "For replication to take place, please enable the suffix "
  360. "and restart the server\n",
  361. agmt_name);
  362. return;
  363. }
  364. PR_Lock(rp->lock);
  365. if (NULL != rp->prp_active_protocol) {
  366. rp->prp_active_protocol->notify_agmt_changed(rp->prp_active_protocol);
  367. }
  368. PR_Unlock(rp->lock);
  369. }
  370. void
  371. prot_notify_window_opened(Repl_Protocol *rp)
  372. {
  373. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  374. if (NULL == rp)
  375. return;
  376. PR_Lock(rp->lock);
  377. if (NULL != rp->prp_active_protocol) {
  378. rp->prp_active_protocol->notify_window_opened(rp->prp_active_protocol);
  379. }
  380. PR_Unlock(rp->lock);
  381. }
  382. void
  383. prot_notify_window_closed(Repl_Protocol *rp)
  384. {
  385. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  386. if (NULL == rp)
  387. return;
  388. PR_Lock(rp->lock);
  389. if (NULL != rp->prp_active_protocol) {
  390. rp->prp_active_protocol->notify_window_closed(rp->prp_active_protocol);
  391. }
  392. PR_Unlock(rp->lock);
  393. }
  394. int
  395. prot_status(Repl_Protocol *rp)
  396. {
  397. int return_status = PROTOCOL_STATUS_UNKNOWN;
  398. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  399. if (NULL != rp) {
  400. PR_Lock(rp->lock);
  401. if (NULL != rp->prp_active_protocol) {
  402. return_status = rp->prp_active_protocol->status(rp->prp_active_protocol);
  403. }
  404. PR_Unlock(rp->lock);
  405. }
  406. return return_status;
  407. }
  408. /*
  409. * Start an incremental protocol session, even if we're not
  410. * currently in a schedule window.
  411. * If the total protocol is active, do nothing.
  412. * Otherwise, notify the incremental protocol that it should
  413. * run once.
  414. */
  415. void
  416. prot_replicate_now(Repl_Protocol *rp)
  417. {
  418. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  419. if (NULL != rp) {
  420. PR_Lock(rp->lock);
  421. if (rp->prp_incremental == rp->prp_active_protocol) {
  422. rp->prp_active_protocol->update_now(rp->prp_active_protocol);
  423. }
  424. PR_Unlock(rp->lock);
  425. }
  426. }
  427. /*
  428. * A little factory function to create a protocol
  429. * instance of the correct type.
  430. */
  431. static Private_Repl_Protocol *
  432. private_protocol_factory(Repl_Protocol *rp, int type)
  433. {
  434. Private_Repl_Protocol *prp = NULL;
  435. switch (type) {
  436. case PROTOCOL_5_INCREMENTAL:
  437. if (NULL == rp->conn) {
  438. rp->conn = conn_new(rp->agmt);
  439. }
  440. if (NULL != rp->conn) {
  441. prp = Repl_5_Inc_Protocol_new(rp);
  442. }
  443. break;
  444. case PROTOCOL_5_TOTAL:
  445. if (NULL == rp->conn) {
  446. rp->conn = conn_new(rp->agmt);
  447. }
  448. if (NULL != rp->conn) {
  449. prp = Repl_5_Tot_Protocol_new(rp);
  450. }
  451. break;
  452. case PROTOCOL_WINDOWS_INCREMENTAL:
  453. if (NULL == rp->conn) {
  454. rp->conn = windows_conn_new(rp->agmt);
  455. }
  456. if (NULL != rp->conn) {
  457. prp = Windows_Inc_Protocol_new(rp);
  458. }
  459. break;
  460. case PROTOCOL_WINDOWS_TOTAL:
  461. if (NULL == rp->conn) {
  462. rp->conn = windows_conn_new(rp->agmt);
  463. }
  464. if (NULL != rp->conn) {
  465. prp = Windows_Tot_Protocol_new(rp);
  466. }
  467. break;
  468. }
  469. return prp;
  470. }