repl5_protocol.c 16 KB

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