repl5_protocol.c 14 KB

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