repl5_protocol.c 16 KB

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