1
0

repl_connext.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /* repl_connext.c - replication extension to the Connection object
  42. */
  43. #include "repl.h"
  44. #include "repl5.h"
  45. /* ***** Supplier side ***** */
  46. /* NOT NEEDED YET */
  47. /* ***** Consumer side ***** */
  48. /* consumer connection extension constructor */
  49. void* consumer_connection_extension_constructor (void *object, void *parent)
  50. {
  51. consumer_connection_extension *ext = (consumer_connection_extension*) slapi_ch_malloc (sizeof (consumer_connection_extension));
  52. if (ext == NULL)
  53. {
  54. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name, "unable to create replication consumer connection extension - out of memory\n" );
  55. }
  56. else
  57. {
  58. ext->is_legacy_replication_dn= 0;
  59. ext->repl_protocol_version = REPL_PROTOCOL_UNKNOWN;
  60. ext->replica_acquired = NULL;
  61. ext->isreplicationsession= 0;
  62. ext->supplier_ruv = NULL;
  63. ext->connection = NULL;
  64. ext->in_use_opid = -1;
  65. ext->lock = PR_NewLock();
  66. if (NULL == ext->lock)
  67. {
  68. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name, "unable to create replication consumer connection extension lock - out of memory\n" );
  69. /* no need to go through the full destructor, but still need to free up this memory */
  70. slapi_ch_free((void **)&ext);
  71. ext = NULL;
  72. }
  73. }
  74. return ext;
  75. }
  76. /* consumer connection extension destructor */
  77. void consumer_connection_extension_destructor (void *ext, void *object, void *parent)
  78. {
  79. PRUint64 connid = 0;
  80. if (ext)
  81. {
  82. /* Check to see if this replication session has acquired
  83. * a replica. If so, release it here.
  84. */
  85. consumer_connection_extension *connext = (consumer_connection_extension *)ext;
  86. if (NULL != connext->replica_acquired)
  87. {
  88. Replica *r = object_get_data ((Object*)connext->replica_acquired);
  89. /* If a total update was in progress, abort it */
  90. if (REPL_PROTOCOL_50_TOTALUPDATE == connext->repl_protocol_version)
  91. {
  92. Slapi_PBlock *pb = slapi_pblock_new();
  93. const Slapi_DN *repl_root_sdn = replica_get_root(r);
  94. PR_ASSERT(NULL != repl_root_sdn);
  95. if (NULL != repl_root_sdn)
  96. {
  97. slapi_pblock_set(pb, SLAPI_CONNECTION, connext->connection);
  98. slapi_pblock_set(pb, SLAPI_TARGET_SDN, (void*)repl_root_sdn);
  99. slapi_pblock_get(pb, SLAPI_CONN_ID, &connid);
  100. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  101. "Aborting total update in progress for replicated "
  102. "area %s connid=%" NSPRIu64 "\n", slapi_sdn_get_dn(repl_root_sdn),
  103. connid);
  104. slapi_stop_bulk_import(pb);
  105. }
  106. else
  107. {
  108. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  109. "consumer_connection_extension_destructor: can't determine root "
  110. "of replicated area.\n");
  111. }
  112. slapi_pblock_destroy(pb);
  113. /* allow reaping again */
  114. replica_set_tombstone_reap_stop(r, PR_FALSE);
  115. }
  116. replica_relinquish_exclusive_access(r, connid, -1);
  117. object_release ((Object*)connext->replica_acquired);
  118. connext->replica_acquired = NULL;
  119. }
  120. if (connext->supplier_ruv)
  121. {
  122. ruv_destroy ((RUV **)&connext->supplier_ruv);
  123. }
  124. if (connext->lock)
  125. {
  126. PR_DestroyLock(connext->lock);
  127. connext->lock = NULL;
  128. }
  129. connext->in_use_opid = -1;
  130. connext->connection = NULL;
  131. slapi_ch_free((void **)&ext);
  132. }
  133. }
  134. /* Obtain exclusive access to this connection extension.
  135. * Returns the consumer_connection_extension* if successful, else NULL.
  136. *
  137. * This is similar to obtaining exclusive access to the replica, but not identical.
  138. * For the connection extension, you only want to hold on to exclusive access as
  139. * long as it is being actively used to process an operation. Mainly that means
  140. * while processing either a 'start' or an 'end' extended operation. This makes
  141. * certain that if another 'start' or 'end' operation is received on the connection,
  142. * the ops will not trample on each other's state. As soon as you are done with
  143. * that single operation, it is time to relinquish the connection extension.
  144. * That differs from acquiring exclusive access to the replica, which is held over
  145. * after the 'start' operation and relinquished during the 'end' operation.
  146. */
  147. consumer_connection_extension*
  148. consumer_connection_extension_acquire_exclusive_access(void* conn, PRUint64 connid, int opid)
  149. {
  150. consumer_connection_extension* ret = NULL;
  151. /* step 1, grab the connext */
  152. consumer_connection_extension* connext = (consumer_connection_extension*)
  153. repl_con_get_ext(REPL_CON_EXT_CONN, conn);
  154. if (NULL != connext)
  155. {
  156. /* step 2, acquire its lock */
  157. PR_Lock(connext->lock);
  158. /* step 3, see if it is not in use, or in use by us */
  159. if (0 > connext->in_use_opid)
  160. {
  161. /* step 4, take it! */
  162. connext->in_use_opid = opid;
  163. ret = connext;
  164. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  165. "conn=%" NSPRIu64 " op=%d Acquired consumer connection extension\n",
  166. connid, opid);
  167. }
  168. else if (opid == connext->in_use_opid)
  169. {
  170. ret = connext;
  171. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  172. "conn=%" NSPRIu64 " op=%d Reacquired consumer connection extension\n",
  173. connid, opid);
  174. }
  175. else
  176. {
  177. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  178. "conn=%" NSPRIu64 " op=%d Could not acquire consumer connection extension; it is in use by op=%d\n",
  179. connid, opid, connext->in_use_opid);
  180. }
  181. /* step 5, drop the lock */
  182. PR_Unlock(connext->lock);
  183. }
  184. else
  185. {
  186. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  187. "conn=%" NSPRIu64 " op=%d Could not acquire consumer extension, it is NULL!\n",
  188. connid, opid);
  189. }
  190. return ret;
  191. }
  192. /* Relinquish exclusive access to this connection extension.
  193. * Returns 0 if exclusive access could NOT be relinquished, and non-zero if it was.
  194. * Specifically:
  195. * 1 if the extension was in use and was relinquished.
  196. * 2 if the extension was not in use to begin with.
  197. *
  198. * The extension will only be relinquished if it was first acquired by this op,
  199. * or if 'force' is TRUE. Do not use 'force' without a legitimate reason, such
  200. * as when destroying the parent connection.
  201. *
  202. * cf. consumer_connection_extension_acquire_exclusive_access() for details on how,
  203. * when, and why you would want to acquire and relinquish exclusive access.
  204. */
  205. int
  206. consumer_connection_extension_relinquish_exclusive_access(void* conn, PRUint64 connid, int opid, PRBool force)
  207. {
  208. int ret = 0;
  209. /* step 1, grab the connext */
  210. consumer_connection_extension* connext = (consumer_connection_extension*)
  211. repl_con_get_ext(REPL_CON_EXT_CONN, conn);
  212. if (NULL != connext)
  213. {
  214. /* step 2, acquire its lock */
  215. PR_Lock(connext->lock);
  216. /* step 3, see if it is in use */
  217. if (0 > connext->in_use_opid)
  218. {
  219. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  220. "conn=%" NSPRIu64 " op=%d Consumer connection extension is not in use\n",
  221. connid, opid);
  222. ret = 2;
  223. }
  224. else if (opid == connext->in_use_opid)
  225. {
  226. /* step 4, relinquish it (normal) */
  227. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  228. "conn=%" NSPRIu64 " op=%d Relinquishing consumer connection extension\n",
  229. connid, opid);
  230. connext->in_use_opid = -1;
  231. ret = 1;
  232. }
  233. else if (force)
  234. {
  235. /* step 4, relinquish it (forced) */
  236. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  237. "conn=%" NSPRIu64 " op=%d Forced to relinquish consumer connection extension held by op=%d\n",
  238. connid, opid, connext->in_use_opid);
  239. connext->in_use_opid = -1;
  240. ret = 1;
  241. }
  242. else
  243. {
  244. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  245. "conn=%" NSPRIu64 " op=%d Not relinquishing consumer connection extension, it is held by op=%d!\n",
  246. connid, opid, connext->in_use_opid);
  247. }
  248. /* step 5, drop the lock */
  249. PR_Unlock(connext->lock);
  250. }
  251. else
  252. {
  253. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  254. "conn=%" NSPRIu64 " op=%d Could not relinquish consumer extension, it is NULL!\n",
  255. connid, opid);
  256. }
  257. return ret;
  258. }