repl5_protocol.c 16 KB

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