repl5_protocol.c 16 KB

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