repl5_protocol.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. /* now done in private_protocol_factory
  97. if ((rp->conn = conn_new(agmt)) == NULL)
  98. {
  99. goto loser;
  100. } */
  101. /* Acquire the local replica object */
  102. replarea_sdn = agmt_get_replarea(agmt);
  103. rp->replica_object = replica_get_replica_from_dn(replarea_sdn);
  104. if (NULL == rp->replica_object)
  105. {
  106. /* Whoa, no local replica!?!? */
  107. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  108. "%s: Unable to locate replica object for local replica %s\n",
  109. agmt_get_long_name(agmt),
  110. slapi_sdn_get_dn(replarea_sdn));
  111. goto loser;
  112. }
  113. if (get_agmt_agreement_type(agmt) == REPLICA_TYPE_MULTIMASTER)
  114. {
  115. rp->prp_incremental = private_protocol_factory(rp, PROTOCOL_5_INCREMENTAL);
  116. rp->prp_total = private_protocol_factory(rp, PROTOCOL_5_TOTAL);
  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. }
  123. /* XXXggood register callback handlers for entries updated, and
  124. schedule window enter/leave. */
  125. slapi_sdn_free(&replarea_sdn);
  126. return rp;
  127. loser:
  128. prot_delete(&rp);
  129. return NULL;
  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)
  164. {
  165. conn_delete(rp->conn);
  166. }
  167. rp->prp_active_protocol = NULL;
  168. PR_Unlock(rp->lock);
  169. slapi_ch_free((void **)rpp);
  170. }
  171. /*
  172. * Destroy a protocol instance XXXggood not complete
  173. */
  174. void
  175. prot_delete(Repl_Protocol **rpp)
  176. {
  177. Repl_Protocol *rp;
  178. PR_ASSERT(NULL != rpp);
  179. rp = *rpp;
  180. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  181. if (NULL != rp)
  182. {
  183. prot_stop(rp);
  184. prot_free(rpp);
  185. }
  186. }
  187. /*
  188. * Get the connection object.
  189. */
  190. Repl_Connection *
  191. prot_get_connection(Repl_Protocol *rp)
  192. {
  193. Repl_Connection *return_value;
  194. PR_ASSERT(NULL != rp);
  195. PR_Lock(rp->lock);
  196. return_value = rp->conn;
  197. PR_Unlock(rp->lock);
  198. return return_value;
  199. }
  200. /*
  201. * This function causes the total protocol to start.
  202. * This is accomplished by registering a state transition
  203. * to a new state, and then signaling the incremental
  204. * protocol to stop.
  205. */
  206. void
  207. prot_initialize_replica(Repl_Protocol *rp)
  208. {
  209. PR_ASSERT(NULL != rp);
  210. PR_Lock(rp->lock);
  211. /* check that total protocol is not running */
  212. rp->next_state = STATE_PERFORMING_TOTAL_UPDATE;
  213. /* Stop the incremental protocol, if running */
  214. rp->prp_incremental->stop(rp->prp_incremental);
  215. if (rp->prp_total) agmt_set_last_init_status(rp->prp_total->agmt, 0, 0, NULL);
  216. PR_Unlock(rp->lock);
  217. }
  218. /*
  219. * Main thread for protocol manager.
  220. This is a simple state machine. State transition table:
  221. Initial state: incremental update
  222. STATE EVENT NEXT STATE
  223. ----- ----- ----------
  224. incremental update shutdown finished
  225. incremental update total update requested total update
  226. total update shutdown finished
  227. total update update complete incremental update
  228. finished (any) finished
  229. */
  230. static void
  231. prot_thread_main(void *arg)
  232. {
  233. Repl_Protocol *rp = (Repl_Protocol *)arg;
  234. int done;
  235. PR_ASSERT(NULL != rp);
  236. if (rp->agmt) {
  237. set_thread_private_agmtname (agmt_get_long_name(rp->agmt));
  238. }
  239. done = 0;
  240. while (!done)
  241. {
  242. switch (rp->state)
  243. {
  244. case STATE_PERFORMING_INCREMENTAL_UPDATE:
  245. /* Run the incremental update protocol */
  246. PR_Lock(rp->lock);
  247. dev_debug("prot_thread_main(STATE_PERFORMING_INCREMENTAL_UPDATE): begin");
  248. rp->prp_active_protocol = rp->prp_incremental;
  249. PR_Unlock(rp->lock);
  250. rp->prp_incremental->run(rp->prp_incremental);
  251. dev_debug("prot_thread_main(STATE_PERFORMING_INCREMENTAL_UPDATE): end");
  252. break;
  253. case STATE_PERFORMING_TOTAL_UPDATE:
  254. PR_Lock(rp->lock);
  255. /* stop incremental protocol if running */
  256. rp->prp_active_protocol = rp->prp_total;
  257. /* After total protocol finished, return to incremental */
  258. rp->next_state = STATE_PERFORMING_INCREMENTAL_UPDATE;
  259. PR_Unlock(rp->lock);
  260. /* Run the total update protocol */
  261. dev_debug("prot_thread_main(STATE_PERFORMING_TOTAL_UPDATE): begin");
  262. rp->prp_total->run(rp->prp_total);
  263. dev_debug("prot_thread_main(STATE_PERFORMING_TOTAL_UPDATE): end");
  264. /* update the agreement entry to notify clients that
  265. replica initialization is completed. */
  266. agmt_replica_init_done (rp->agmt);
  267. break;
  268. case STATE_FINISHED:
  269. dev_debug("prot_thread_main(STATE_FINISHED): exiting prot_thread_main");
  270. done = 1;
  271. break;
  272. }
  273. rp->state = rp->next_state;
  274. }
  275. }
  276. /*
  277. * Start a thread to handle the replication protocol.
  278. */
  279. void
  280. prot_start(Repl_Protocol *rp)
  281. {
  282. PR_ASSERT(NULL != rp);
  283. if (NULL != rp)
  284. {
  285. if (PR_CreateThread(PR_USER_THREAD, prot_thread_main, (void *)rp,
  286. #if defined(__hpux) && defined(__ia64)
  287. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 524288L ) == NULL)
  288. #else
  289. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE) == NULL)
  290. #endif
  291. {
  292. PRErrorCode prerr = PR_GetError();
  293. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  294. "%s: Unable to create protocol thread; NSPR error - %d, %s\n",
  295. agmt_get_long_name(rp->agmt),
  296. prerr, slapd_pr_strerror(prerr));
  297. }
  298. }
  299. else
  300. {
  301. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Unable to start "
  302. "protocol object - NULL protocol object passed to prot_start.\n");
  303. }
  304. }
  305. /*
  306. * Stop a protocol instance.
  307. */
  308. void
  309. prot_stop(Repl_Protocol *rp)
  310. {
  311. PR_ASSERT(NULL != rp);
  312. if (NULL != rp)
  313. {
  314. PR_Lock(rp->lock);
  315. rp->next_state = STATE_FINISHED;
  316. if (NULL != rp->prp_incremental)
  317. {
  318. if (rp->prp_incremental->stop(rp->prp_incremental) != 0)
  319. {
  320. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  321. "Warning: incremental protocol for replica \"%s\" "
  322. "did not shut down properly.\n",
  323. agmt_get_long_name(rp->agmt));
  324. }
  325. }
  326. if (NULL != rp->prp_total)
  327. {
  328. if (rp->prp_total->stop(rp->prp_total) != 0)
  329. {
  330. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  331. "Warning: total protocol for replica \"%s\" "
  332. "did not shut down properly.\n",
  333. agmt_get_long_name(rp->agmt));
  334. }
  335. }
  336. PR_Unlock(rp->lock);
  337. }
  338. else
  339. {
  340. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Error: prot_stop() "
  341. " called on NULL protocol instance.\n");
  342. }
  343. }
  344. /*
  345. * Call the notify_update method of the incremental or total update
  346. * protocol, is either is active.
  347. */
  348. void
  349. prot_notify_update(Repl_Protocol *rp)
  350. {
  351. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  352. if (NULL == rp) return;
  353. PR_Lock(rp->lock);
  354. if (NULL != rp->prp_active_protocol)
  355. {
  356. rp->prp_active_protocol->notify_update(rp->prp_active_protocol);
  357. }
  358. PR_Unlock(rp->lock);
  359. }
  360. /*
  361. * Call the notify_agmt_changed method of the incremental or total update
  362. * protocol, is either is active.
  363. */
  364. void
  365. prot_notify_agmt_changed(Repl_Protocol *rp, char * agmt_name)
  366. {
  367. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  368. if (NULL == rp) {
  369. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  370. "Replication agreement for %s could not be updated. "
  371. "For replication to take place, please enable the suffix "
  372. "and restart the server\n", agmt_name);
  373. return;
  374. }
  375. PR_Lock(rp->lock);
  376. if (NULL != rp->prp_active_protocol)
  377. {
  378. rp->prp_active_protocol->notify_agmt_changed(rp->prp_active_protocol);
  379. }
  380. PR_Unlock(rp->lock);
  381. }
  382. void
  383. prot_notify_window_opened (Repl_Protocol *rp)
  384. {
  385. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  386. if (NULL == rp) return;
  387. PR_Lock(rp->lock);
  388. if (NULL != rp->prp_active_protocol)
  389. {
  390. rp->prp_active_protocol->notify_window_opened(rp->prp_active_protocol);
  391. }
  392. PR_Unlock(rp->lock);
  393. }
  394. void
  395. prot_notify_window_closed (Repl_Protocol *rp)
  396. {
  397. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  398. if (NULL == rp) return;
  399. PR_Lock(rp->lock);
  400. if (NULL != rp->prp_active_protocol)
  401. {
  402. rp->prp_active_protocol->notify_window_closed(rp->prp_active_protocol);
  403. }
  404. PR_Unlock(rp->lock);
  405. }
  406. int
  407. prot_status(Repl_Protocol *rp)
  408. {
  409. int return_status = PROTOCOL_STATUS_UNKNOWN;
  410. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  411. if (NULL != rp)
  412. {
  413. PR_Lock(rp->lock);
  414. if (NULL != rp->prp_active_protocol)
  415. {
  416. return_status = rp->prp_active_protocol->status(rp->prp_active_protocol);
  417. }
  418. PR_Unlock(rp->lock);
  419. }
  420. return return_status;
  421. }
  422. /*
  423. * Start an incremental protocol session, even if we're not
  424. * currently in a schedule window.
  425. * If the total protocol is active, do nothing.
  426. * Otherwise, notify the incremental protocol that it should
  427. * run once.
  428. */
  429. void
  430. prot_replicate_now(Repl_Protocol *rp)
  431. {
  432. /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
  433. if (NULL != rp)
  434. {
  435. PR_Lock(rp->lock);
  436. if (rp->prp_incremental == rp->prp_active_protocol)
  437. {
  438. rp->prp_active_protocol->update_now(rp->prp_active_protocol);
  439. }
  440. PR_Unlock(rp->lock);
  441. }
  442. }
  443. /*
  444. * A little factory function to create a protocol
  445. * instance of the correct type.
  446. */
  447. static Private_Repl_Protocol *
  448. private_protocol_factory(Repl_Protocol *rp, int type)
  449. {
  450. Private_Repl_Protocol *prp;
  451. switch (type)
  452. {
  453. case PROTOCOL_5_INCREMENTAL:
  454. if ((rp->conn = conn_new(rp->agmt)) != NULL)
  455. prp = Repl_5_Inc_Protocol_new(rp);
  456. break;
  457. case PROTOCOL_5_TOTAL:
  458. if ((rp->conn = conn_new(rp->agmt)) != NULL)
  459. prp = Repl_5_Tot_Protocol_new(rp);
  460. break;
  461. case PROTOCOL_WINDOWS_INCREMENTAL:
  462. if ((rp->conn = windows_conn_new(rp->agmt)) != NULL)
  463. prp = Windows_Inc_Protocol_new(rp);
  464. break;
  465. case PROTOCOL_WINDOWS_TOTAL:
  466. if ((rp->conn = windows_conn_new(rp->agmt)) != NULL)
  467. prp = Windows_Tot_Protocol_new(rp);
  468. break;
  469. }
  470. return prp;
  471. }