1
0

repl5_protocol.c 16 KB

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