repl5_protocol.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. /* repl5_protocol.c */
  39. /*
  40. The replication protocol object manages the replication protocol for
  41. a given replica. It determines which protocol(s) are appropriate to
  42. use when updating a given replica. It also knows how to arbitrate
  43. incremental and total update protocols for a given replica.
  44. */
  45. #include "repl5.h"
  46. #include "repl5_prot_private.h"
  47. #define PROTOCOL_5_INCREMENTAL 1
  48. #define PROTOCOL_5_TOTAL 2
  49. #define PROTOCOL_4_INCREMENTAL 3
  50. #define PROTOCOL_4_TOTAL 4
  51. #define PROTOCOL_WINDOWS_INCREMENTAL 5
  52. #define PROTOCOL_WINDOWS_TOTAL 6
  53. typedef struct repl_protocol
  54. {
  55. Private_Repl_Protocol *prp_incremental; /* inc protocol to use */
  56. Private_Repl_Protocol *prp_total; /* total protocol to use */
  57. Private_Repl_Protocol *prp_active_protocol; /* Pointer to active protocol */
  58. Repl_Agmt *agmt; /* The replication agreement we're servicing */
  59. Repl_Connection *conn; /* Connection to remote server */
  60. Object *replica_object; /* Local replica. If non-NULL, replica object is acquired */
  61. int state;
  62. int next_state;
  63. PRLock *lock;
  64. } repl_protocol;
  65. /* States */
  66. #define STATE_FINISHED 503
  67. #define STATE_BAD_STATE_SHOULD_NEVER_HAPPEN 599
  68. /* Forward declarations */
  69. static Private_Repl_Protocol *private_protocol_factory(Repl_Protocol *rp, int type);
  70. /*
  71. * Create a new protocol instance.
  72. */
  73. Repl_Protocol *
  74. prot_new(Repl_Agmt *agmt, int protocol_state)
  75. {
  76. Slapi_DN *replarea_sdn = NULL;
  77. Repl_Protocol *rp = (Repl_Protocol *)slapi_ch_malloc(sizeof(Repl_Protocol));
  78. rp->prp_incremental = rp->prp_total = rp->prp_active_protocol = NULL;
  79. if (protocol_state == STATE_PERFORMING_TOTAL_UPDATE)
  80. {
  81. rp->state = STATE_PERFORMING_TOTAL_UPDATE;
  82. }
  83. else
  84. {
  85. rp->state = STATE_PERFORMING_INCREMENTAL_UPDATE;
  86. }
  87. rp->next_state = STATE_PERFORMING_INCREMENTAL_UPDATE;
  88. if ((rp->lock = PR_NewLock()) == NULL)
  89. {
  90. goto loser;
  91. }
  92. rp->agmt = agmt;
  93. /* now done in private_protocol_factory
  94. if ((rp->conn = conn_new(agmt)) == NULL)
  95. {
  96. goto loser;
  97. } */
  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. }
  115. else if (get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS)
  116. {
  117. rp->prp_incremental = private_protocol_factory(rp, PROTOCOL_WINDOWS_INCREMENTAL);
  118. rp->prp_total = private_protocol_factory(rp, PROTOCOL_WINDOWS_TOTAL);
  119. }
  120. /* XXXggood register callback handlers for entries updated, and
  121. schedule window enter/leave. */
  122. slapi_sdn_free(&replarea_sdn);
  123. return rp;
  124. loser:
  125. prot_delete(&rp);
  126. return NULL;
  127. }
  128. Object *
  129. prot_get_replica_object(Repl_Protocol *rp)
  130. {
  131. PR_ASSERT(NULL != rp);
  132. return rp->replica_object;
  133. }
  134. Repl_Agmt *
  135. prot_get_agreement(Repl_Protocol *rp)
  136. {
  137. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  138. if (NULL == rp) return NULL;
  139. return rp->agmt;
  140. }
  141. void
  142. prot_free(Repl_Protocol **rpp)
  143. {
  144. Repl_Protocol *rp = NULL;
  145. if (rpp == NULL || *rpp == NULL) return;
  146. rp = *rpp;
  147. PR_Lock(rp->lock);
  148. if (NULL != rp->prp_incremental)
  149. {
  150. rp->prp_incremental->delete(&rp->prp_incremental);
  151. }
  152. if (NULL != rp->prp_total)
  153. {
  154. rp->prp_total->delete(&rp->prp_total);
  155. }
  156. if (NULL != rp->replica_object)
  157. {
  158. object_release(rp->replica_object);
  159. }
  160. if (NULL != rp->conn)
  161. {
  162. conn_delete(rp->conn);
  163. }
  164. rp->prp_active_protocol = NULL;
  165. PR_Unlock(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. PR_ASSERT(NULL != rp);
  233. if (rp->agmt) {
  234. set_thread_private_agmtname (agmt_get_long_name(rp->agmt));
  235. }
  236. done = 0;
  237. while (!done)
  238. {
  239. switch (rp->state)
  240. {
  241. case STATE_PERFORMING_INCREMENTAL_UPDATE:
  242. /* Run the incremental update protocol */
  243. PR_Lock(rp->lock);
  244. dev_debug("prot_thread_main(STATE_PERFORMING_INCREMENTAL_UPDATE): begin");
  245. rp->prp_active_protocol = rp->prp_incremental;
  246. PR_Unlock(rp->lock);
  247. rp->prp_incremental->run(rp->prp_incremental);
  248. dev_debug("prot_thread_main(STATE_PERFORMING_INCREMENTAL_UPDATE): end");
  249. break;
  250. case STATE_PERFORMING_TOTAL_UPDATE:
  251. PR_Lock(rp->lock);
  252. /* stop incremental protocol if running */
  253. rp->prp_active_protocol = rp->prp_total;
  254. /* After total protocol finished, return to incremental */
  255. rp->next_state = STATE_PERFORMING_INCREMENTAL_UPDATE;
  256. PR_Unlock(rp->lock);
  257. /* Run the total update protocol */
  258. dev_debug("prot_thread_main(STATE_PERFORMING_TOTAL_UPDATE): begin");
  259. rp->prp_total->run(rp->prp_total);
  260. dev_debug("prot_thread_main(STATE_PERFORMING_TOTAL_UPDATE): end");
  261. /* update the agreement entry to notify clients that
  262. replica initialization is completed. */
  263. agmt_replica_init_done (rp->agmt);
  264. break;
  265. case STATE_FINISHED:
  266. dev_debug("prot_thread_main(STATE_FINISHED): exiting prot_thread_main");
  267. done = 1;
  268. break;
  269. }
  270. rp->state = rp->next_state;
  271. }
  272. }
  273. /*
  274. * Start a thread to handle the replication protocol.
  275. */
  276. void
  277. prot_start(Repl_Protocol *rp)
  278. {
  279. PR_ASSERT(NULL != rp);
  280. if (NULL != rp)
  281. {
  282. if (PR_CreateThread(PR_USER_THREAD, prot_thread_main, (void *)rp,
  283. #if defined(__hpux) && defined(__ia64)
  284. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 524288L ) == NULL)
  285. #else
  286. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE) == NULL)
  287. #endif
  288. {
  289. PRErrorCode prerr = PR_GetError();
  290. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  291. "%s: Unable to create protocol thread; NSPR error - %d, %s\n",
  292. agmt_get_long_name(rp->agmt),
  293. prerr, slapd_pr_strerror(prerr));
  294. }
  295. }
  296. else
  297. {
  298. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Unable to start "
  299. "protocol object - NULL protocol object passed to prot_start.\n");
  300. }
  301. }
  302. /*
  303. * Stop a protocol instance.
  304. */
  305. void
  306. prot_stop(Repl_Protocol *rp)
  307. {
  308. PR_ASSERT(NULL != rp);
  309. if (NULL != rp)
  310. {
  311. PR_Lock(rp->lock);
  312. rp->next_state = STATE_FINISHED;
  313. if (NULL != rp->prp_incremental)
  314. {
  315. if (rp->prp_incremental->stop(rp->prp_incremental) != 0)
  316. {
  317. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  318. "Warning: incremental protocol for replica \"%s\" "
  319. "did not shut down properly.\n",
  320. agmt_get_long_name(rp->agmt));
  321. }
  322. }
  323. if (NULL != rp->prp_total)
  324. {
  325. if (rp->prp_total->stop(rp->prp_total) != 0)
  326. {
  327. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  328. "Warning: total protocol for replica \"%s\" "
  329. "did not shut down properly.\n",
  330. agmt_get_long_name(rp->agmt));
  331. }
  332. }
  333. PR_Unlock(rp->lock);
  334. }
  335. else
  336. {
  337. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Error: prot_stop() "
  338. " called on NULL protocol instance.\n");
  339. }
  340. }
  341. /*
  342. * Call the notify_update method of the incremental or total update
  343. * protocol, is either is active.
  344. */
  345. void
  346. prot_notify_update(Repl_Protocol *rp)
  347. {
  348. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  349. if (NULL == rp) return;
  350. PR_Lock(rp->lock);
  351. if (NULL != rp->prp_active_protocol)
  352. {
  353. rp->prp_active_protocol->notify_update(rp->prp_active_protocol);
  354. }
  355. PR_Unlock(rp->lock);
  356. }
  357. /*
  358. * Call the notify_agmt_changed method of the incremental or total update
  359. * protocol, is either is active.
  360. */
  361. void
  362. prot_notify_agmt_changed(Repl_Protocol *rp, char * agmt_name)
  363. {
  364. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  365. if (NULL == rp) {
  366. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  367. "Replication agreement for %s could not be updated. "
  368. "For replication to take place, please enable the suffix "
  369. "and restart the server\n", agmt_name);
  370. return;
  371. }
  372. PR_Lock(rp->lock);
  373. if (NULL != rp->prp_active_protocol)
  374. {
  375. rp->prp_active_protocol->notify_agmt_changed(rp->prp_active_protocol);
  376. }
  377. PR_Unlock(rp->lock);
  378. }
  379. void
  380. prot_notify_window_opened (Repl_Protocol *rp)
  381. {
  382. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  383. if (NULL == rp) return;
  384. PR_Lock(rp->lock);
  385. if (NULL != rp->prp_active_protocol)
  386. {
  387. rp->prp_active_protocol->notify_window_opened(rp->prp_active_protocol);
  388. }
  389. PR_Unlock(rp->lock);
  390. }
  391. void
  392. prot_notify_window_closed (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_window_closed(rp->prp_active_protocol);
  400. }
  401. PR_Unlock(rp->lock);
  402. }
  403. int
  404. prot_status(Repl_Protocol *rp)
  405. {
  406. int return_status = PROTOCOL_STATUS_UNKNOWN;
  407. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  408. if (NULL != rp)
  409. {
  410. PR_Lock(rp->lock);
  411. if (NULL != rp->prp_active_protocol)
  412. {
  413. return_status = rp->prp_active_protocol->status(rp->prp_active_protocol);
  414. }
  415. PR_Unlock(rp->lock);
  416. }
  417. return return_status;
  418. }
  419. /*
  420. * Start an incremental protocol session, even if we're not
  421. * currently in a schedule window.
  422. * If the total protocol is active, do nothing.
  423. * Otherwise, notify the incremental protocol that it should
  424. * run once.
  425. */
  426. void
  427. prot_replicate_now(Repl_Protocol *rp)
  428. {
  429. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  430. if (NULL != rp)
  431. {
  432. PR_Lock(rp->lock);
  433. if (rp->prp_incremental == rp->prp_active_protocol)
  434. {
  435. rp->prp_active_protocol->update_now(rp->prp_active_protocol);
  436. }
  437. PR_Unlock(rp->lock);
  438. }
  439. }
  440. /*
  441. * A little factory function to create a protocol
  442. * instance of the correct type.
  443. */
  444. static Private_Repl_Protocol *
  445. private_protocol_factory(Repl_Protocol *rp, int type)
  446. {
  447. Private_Repl_Protocol *prp;
  448. switch (type)
  449. {
  450. case PROTOCOL_5_INCREMENTAL:
  451. if ((rp->conn = conn_new(rp->agmt)) != NULL)
  452. prp = Repl_5_Inc_Protocol_new(rp);
  453. break;
  454. case PROTOCOL_5_TOTAL:
  455. if ((rp->conn = conn_new(rp->agmt)) != NULL)
  456. prp = Repl_5_Tot_Protocol_new(rp);
  457. break;
  458. case PROTOCOL_WINDOWS_INCREMENTAL:
  459. if ((rp->conn = windows_conn_new(rp->agmt)) != NULL)
  460. prp = Windows_Inc_Protocol_new(rp);
  461. break;
  462. case PROTOCOL_WINDOWS_TOTAL:
  463. if ((rp->conn = windows_conn_new(rp->agmt)) != NULL)
  464. prp = Windows_Tot_Protocol_new(rp);
  465. break;
  466. }
  467. return prp;
  468. }