cl5_api.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. /* cl5_api.h - interface to 5.0 changelog */
  42. #ifndef CL5_API_H
  43. #define CL5_API_H
  44. #include "repl5.h"
  45. #include "repl5_prot_private.h"
  46. #include <errno.h>
  47. #define BDB_IMPL "bdb" /* changelog type */
  48. #define BDB_REPLPLUGIN "libreplication-plugin" /* This backend plugin */
  49. #define CL5_TYPE "Changelog5" /* changelog type */
  50. #define VERSION_SIZE 127 /* size of the buffer to hold changelog version */
  51. #define CL5_DEFAULT_CONFIG -1 /* value that indicates to changelog to use default */
  52. #define CL5_STR_IGNORE "-1" /* tells function to ignore this parameter */
  53. #define CL5_NUM_IGNORE -1 /* tells function to ignore this parameter */
  54. #define CL5_STR_UNLIMITED "0" /* represent unlimited value (trimming ) */
  55. #define CL5_NUM_UNLIMITED 0 /* represent unlimited value (trimming ) */
  56. #define CL5_OS_ERR_IS_DISKFULL(err) ((err)==ENOSPC || (err)==EFBIG)
  57. /***** Data Structures *****/
  58. /* changelog configuration structure */
  59. typedef struct cl5dbconfig
  60. {
  61. size_t pageSize; /* page size in bytes */
  62. PRInt32 fileMode; /* file mode */
  63. PRUint32 maxConcurrentWrites; /* max number of concurrent cl writes */
  64. char *encryptionAlgorithm; /* nsslapd-encryptionalgorithm */
  65. char *symmetricKey;
  66. } CL5DBConfig;
  67. /* changelog entry format */
  68. typedef struct cl5entry
  69. {
  70. slapi_operation_parameters *op; /* operation applied to the server */
  71. time_t time; /* time added to the cl; used for trimming */
  72. } CL5Entry;
  73. /* default values for the changelog configuration structure above */
  74. /*
  75. * For historical reasons, dbcachesize refers to number of bytes at the DB level,
  76. * whereas cachesize refers to number of entries at the changelog cache level (cachememsize is the
  77. * one referring to number of bytes at the changelog cache level)
  78. */
  79. #define CL5_DEFAULT_CONFIG_DB_DBCACHESIZE 10485760 /* 10M bytes */
  80. #define CL5_DEFAULT_CONFIG_DB_DURABLE_TRANSACTIONS 1
  81. #define CL5_DEFAULT_CONFIG_DB_CHECKPOINT_INTERVAL 60
  82. #define CL5_DEFAULT_CONFIG_DB_CIRCULAR_LOGGING 1
  83. #define CL5_DEFAULT_CONFIG_DB_PAGE_SIZE 8*1024
  84. #define CL5_DEFAULT_CONFIG_DB_LOGFILE_SIZE 0
  85. #define CL5_DEFAULT_CONFIG_DB_VERBOSE 0
  86. #define CL5_DEFAULT_CONFIG_DB_DEBUG 0
  87. #define CL5_DEFAULT_CONFIG_DB_TRICKLE_PERCENTAGE 40
  88. #define CL5_DEFAULT_CONFIG_DB_SPINCOUNT 0
  89. #define CL5_DEFAULT_CONFIG_DB_TXN_MAX 200
  90. #define CL5_DEFAULT_CONFIG_CACHESIZE 3000 /* number of entries */
  91. #define CL5_DEFAULT_CONFIG_CACHEMEMSIZE 1048576 /* 1 M bytes */
  92. #define CL5_DEFAULT_CONFIG_NB_LOCK 1000 /* Number of locks in the lock table of the DB */
  93. /*
  94. * Small number of concurrent writes degradate the throughput.
  95. * Large one increases deadlock.
  96. */
  97. #ifdef SOLARIS
  98. #define CL5_DEFAULT_CONFIG_MAX_CONCURRENT_WRITES 10
  99. #else
  100. #define CL5_DEFAULT_CONFIG_MAX_CONCURRENT_WRITES 2
  101. #endif
  102. #define CL5_MIN_DB_DBCACHESIZE 524288 /* min 500K bytes */
  103. #define CL5_MIN_CACHESIZE 500 /* min number of entries */
  104. #define CL5_MIN_CACHEMEMSIZE 262144 /* min 250K bytes */
  105. #define CL5_MIN_NB_LOCK 1000 /* The minimal number of locks in the DB (Same as default) */
  106. /* data structure that allows iteration through changelog */
  107. typedef struct cl5replayiterator CL5ReplayIterator;
  108. /* changelog state */
  109. typedef enum
  110. {
  111. CL5_STATE_NONE, /* changelog has not been initialized */
  112. CL5_STATE_CLOSING, /* changelog is about to close; all threads must exit */
  113. CL5_STATE_CLOSED, /* changelog has been initialized, but not opened, or open and then closed */
  114. CL5_STATE_OPEN /* changelog is opened */
  115. } CL5State;
  116. /* error codes */
  117. enum
  118. {
  119. CL5_SUCCESS, /* successful operation */
  120. CL5_BAD_DATA, /* invalid parameter passed to the function */
  121. CL5_BAD_FORMAT, /* db data has unexpected format */
  122. CL5_BAD_STATE, /* changelog is in an incorrect state for attempted operation */
  123. CL5_BAD_DBVERSION, /* changelog has invalid dbversion */
  124. CL5_DB_ERROR, /* database error */
  125. CL5_NOTFOUND, /* requested entry or value was not found */
  126. CL5_MEMORY_ERROR, /* memory allocation failed */
  127. CL5_SYSTEM_ERROR, /* NSPR error occured, use PR_Error for furhter info */
  128. CL5_CSN_ERROR, /* CSN API failed */
  129. CL5_RUV_ERROR, /* RUV API failed */
  130. CL5_OBJSET_ERROR, /* namedobjset api failed */
  131. CL5_PURGED_DATA, /* requested data has been purged */
  132. CL5_MISSING_DATA, /* data should be in the changelog, but is missing */
  133. CL5_UNKNOWN_ERROR, /* unclassified error */
  134. CL5_IGNORE_OP /* ignore this updated - used by CLEANALLRUV task */
  135. };
  136. /***** Module APIs *****/
  137. /* Name: cl5Init
  138. Description: initializes changelog module; must be called by a single thread
  139. before any function of the module.
  140. Parameters: none
  141. Return: CL5_SUCCESS if function is successful;
  142. CL5_BAD_DATA if invalid directory is passed;
  143. CL5_SYSTEM error if NSPR call fails.
  144. */
  145. int cl5Init ();
  146. /* Name: cl5Cleanup
  147. Description: performs cleanup of the changelog module. Must be called by a single
  148. thread. It will closed db if it is still open.
  149. Parameters: none
  150. Return: none
  151. */
  152. void cl5Cleanup ();
  153. /* Name: cl5Open
  154. Description: opens changelog ; must be called after changelog is
  155. initialized using cl5Init. It is thread safe and the second
  156. call is ignored.
  157. Parameters: dir - changelog dir
  158. config - db configuration parameters; currently not used
  159. openMode - open mode
  160. Return: CL5_SUCCESS if successful;
  161. CL5_BAD_DATA if invalid directory is passed;
  162. CL5_BAD_DBVERSION if dbversion file is missing or has unexpected data
  163. CL5_SYSTEM_ERROR if NSPR error occurred (during db directory creation);
  164. CL5_MEMORY_ERROR if memory allocation fails;
  165. CL5_DB_ERROR if db initialization or open fails.
  166. */
  167. int cl5Open (const char *dir, const CL5DBConfig *config);
  168. /* Name: cl5Close
  169. Description: closes changelog and cleanups changelog module; waits until
  170. all threads are done using changelog
  171. Parameters: none
  172. Return: CL5_SUCCESS if successful;
  173. CL5_BAD_STATE if db is not in the open state;
  174. CL5_SYSTEM_ERROR if NSPR call fails
  175. */
  176. int cl5Close ();
  177. /* Name: cl5Delete
  178. Description: removes changelog
  179. Parameters: dir - changelog directory
  180. Return: CL5_SUCCESS if successful;
  181. CL5_BAD_STATE if the changelog is not in closed state;
  182. CL5_BAD_DATA if invalid directory supplied
  183. CL5_SYSTEM_ERROR if NSPR call fails
  184. */
  185. int cl5Delete (const char *dir);
  186. /* Name: cl5DeleteDBSync
  187. Description: The same as cl5DeleteDB except the function does not return
  188. until the file is removed.
  189. */
  190. int cl5DeleteDBSync (Object *replica);
  191. /* Name: cl5GetUpperBoundRUV
  192. Description: retrieves vector that represent the upper bound of changes
  193. stored in the changelog for the replica.
  194. Parameters: r - replica for which the vector is requested
  195. ruv - contains a copy of the upper bound ruv if function is successful;
  196. unchanged otherwise. It is responsibility of the caller to free
  197. the ruv when it is no longer is in use
  198. Return: CL5_SUCCESS if function is successful
  199. CL5_BAD_STATE if the changelog is not initialized;
  200. CL5_BAD_DATA - if NULL id is supplied
  201. CL5_NOTFOUND, if changelog file for replica is not found
  202. */
  203. int cl5GetUpperBoundRUV (Replica *r, RUV **ruv);
  204. /* Name: cl5ExportLDIF
  205. Description: dumps changelog to an LDIF file; changelog can be open or closed.
  206. Parameters: clDir - changelog dir
  207. ldifFile - full path to ldif file to write
  208. replicas - optional list of replicas whose changes should be exported;
  209. if the list is NULL, entire changelog is exported.
  210. Return: CL5_SUCCESS if function is successful;
  211. CL5_BAD_DATA if invalid parameter is passed;
  212. CL5_BAD_STATE if changelog is not initialized;
  213. CL5_DB_ERROR if db api fails;
  214. CL5_SYSTEM_ERROR if NSPR call fails;
  215. CL5_MEMORY_ERROR if memory allocation fails.
  216. */
  217. int cl5ExportLDIF (const char *ldifFile, Object **replicas);
  218. /* Name: cl5ImportLDIF
  219. Description: imports ldif file into changelog; changelog must be in the closed state
  220. Parameters: clDir - changelog dir
  221. ldifFile - absolute path to the ldif file to import
  222. replicas - optional list of replicas whose data should be imported;
  223. if the list is NULL, all data in the file is imported.
  224. Return: CL5_SUCCESS if function is successful;
  225. CL5_BAD_DATA if invalid parameter is passed;
  226. CL5_BAD_STATE if changelog is open or not initialized;
  227. CL5_DB_ERROR if db api fails;
  228. CL5_SYSTEM_ERROR if NSPR call fails;
  229. CL5_MEMORY_ERROR if memory allocation fails.
  230. */
  231. int cl5ImportLDIF (const char *clDir, const char *ldifFile, Object **replicas);
  232. /* Name: cl5GetState
  233. Description: returns database state
  234. Parameters: none
  235. Return: changelog state
  236. */
  237. int cl5GetState ();
  238. /* Name: cl5ConfigTrimming
  239. Description: sets changelog trimming parameters
  240. Parameters: maxEntries - maximum number of entries in the log;
  241. maxAge - maximum entry age;
  242. compactInterval - interval to compact changelog db;
  243. trimInterval - interval for changelog trimming.
  244. Return: CL5_SUCCESS if successful;
  245. CL5_BAD_STATE if changelog has not been open
  246. */
  247. int cl5ConfigTrimming (int maxEntries, const char *maxAge, int compactInterval, int trimInterval);
  248. /* Name: cl5GetOperation
  249. Description: retrieves operation specified by its csn and databaseid
  250. Parameters: op - must contain csn and databaseid; the rest of data is
  251. filled if function is successful
  252. Return: CL5_SUCCESS if function is successful;
  253. CL5_BAD_DATA if invalid op is passed;
  254. CL5_BAD_STATE if db has not been initialized;
  255. CL5_NOTFOUND if entry was not found;
  256. CL5_DB_ERROR if any other db error occurred;
  257. CL5_BADFORMAT if db data format does not match entry format.
  258. */
  259. int cl5GetOperation (Object *replica, slapi_operation_parameters *op);
  260. /* Name: cl5GetFirstOperation
  261. Description: retrieves first operation for a particular database
  262. replica - replica for which the operation should be retrieved.
  263. Parameters: op - buffer to store the operation;
  264. iterator - to be passed to the call to cl5GetNextOperation
  265. Return: CL5_SUCCESS, if successful
  266. CL5_BADDATA, if operation is NULL
  267. CL5_BAD_STATE, if changelog is not open
  268. CL5_DB_ERROR, if db call fails
  269. */
  270. int cl5GetFirstOperation (Object *replica, slapi_operation_parameters *op, void **iterator);
  271. /* Name: cl5GetNextOperation
  272. Description: retrieves the next op from the changelog as defined by the iterator
  273. Parameters: replica - replica for which the operation should be retrieved.
  274. op - returned operation, if function is successful
  275. iterator - in: identifies op to retrieve; out: identifies next op
  276. Return: CL5_SUCCESS, if successful
  277. CL5_BADDATA, if invalid parameter is supplied
  278. CL5_BAD_STATE, if changelog is not open
  279. CL5_NOTFOUND, empty changelog
  280. CL5_DB_ERROR, if db call fails
  281. */
  282. int cl5GetNextOperation (slapi_operation_parameters *op, void *iterator);
  283. /* Name: cl5DestroyIterator
  284. Description: destroys iterator once iteration through changelog is done
  285. Parameters: iterator - iterator to destroy
  286. Return: CL5_SUCCESS, if successful
  287. CL5_BADDATA, if invalid parameters is supplied
  288. CL5_BAD_STATE, if changelog is not open
  289. CL5_DB_ERROR, if db call fails
  290. */
  291. void cl5DestroyIterator (void *iterator);
  292. /* Name: cl5WriteOperationTxn
  293. Description: writes operation to changelog as part of a containing transaction
  294. Parameters: repl_name - name of the replica to which operation applies
  295. repl_gen - replica generation for the operation
  296. !!!Note that we pass name and generation rather than
  297. replica object since generation can change while operation
  298. is in progress (if the data is reloaded). !!!
  299. op - operation to write
  300. local - this is a non-replicated operation
  301. txn - the containing transaction
  302. Return: CL5_SUCCESS if function is successful;
  303. CL5_BAD_DATA if invalid op is passed;
  304. CL5_BAD_STATE if db has not been initialized;
  305. CL5_MEMORY_ERROR if memory allocation failed;
  306. CL5_DB_ERROR if any other db error occurred;
  307. */
  308. int cl5WriteOperationTxn(const char *repl_name, const char *repl_gen,
  309. const slapi_operation_parameters *op, PRBool local, void *txn);
  310. /* Name: cl5WriteOperation
  311. Description: writes operation to changelog
  312. Parameters: repl_name - name of the replica to which operation applies
  313. repl_gen - replica generation for the operation
  314. !!!Note that we pass name and generation rather than
  315. replica object since generation can change while operation
  316. is in progress (if the data is reloaded). !!!
  317. op - operation to write
  318. local - this is a non-replicated operation
  319. Return: CL5_SUCCESS if function is successful;
  320. CL5_BAD_DATA if invalid op is passed;
  321. CL5_BAD_STATE if db has not been initialized;
  322. CL5_MEMORY_ERROR if memory allocation failed;
  323. CL5_DB_ERROR if any other db error occurred;
  324. */
  325. int cl5WriteOperation(const char *repl_name, const char *repl_gen,
  326. const slapi_operation_parameters *op, PRBool local);
  327. /* Name: cl5CreateReplayIterator
  328. Description: creates an iterator that allows to retrieve changes that should
  329. to be sent to the consumer identified by ruv The iteration is performed by
  330. repeated calls to cl5GetNextOperationToReplay.
  331. Parameters: replica - replica whose data we wish to iterate;
  332. ruv - consumer ruv;
  333. iterator - iterator to be passed to cl5GetNextOperationToReplay call
  334. Return: CL5_SUCCESS, if function is successful;
  335. CL5_MISSING_DATA, if data that should be in the changelog is missing
  336. CL5_PURGED_DATA, if some data that consumer needs has been purged.
  337. Note that the iterator can be non null if the supplier contains
  338. some data that needs to be sent to the consumer
  339. CL5_NOTFOUND if the consumer is up to data with respect to the supplier
  340. CL5_BAD_DATA if invalid parameter is passed;
  341. CL5_BAD_STATE if db has not been open;
  342. CL5_DB_ERROR if any other db error occurred;
  343. CL5_MEMORY_ERROR if memory allocation fails.
  344. */
  345. int cl5CreateReplayIterator (Private_Repl_Protocol *prp, const RUV *ruv,
  346. CL5ReplayIterator **iterator);
  347. int cl5CreateReplayIteratorEx (Private_Repl_Protocol *prp, const RUV *consumerRuv,
  348. CL5ReplayIterator **iterator, ReplicaId consumerRID );
  349. /* Name: cl5GetNextOperationToReplay
  350. Description: retrieves next operation to be sent to the consumer and
  351. that was created on a particular master. Consumer and master info
  352. is encoded in the iterator parameter that must be created by calling
  353. to cl5CreateIterator.
  354. Parameters: iterator - iterator that identifies next entry to retrieve;
  355. op - operation retrieved if function is successful
  356. Return: CL5_SUCCESS if function is successful;
  357. CL5_BAD_DATA if invalid parameter is passed;
  358. CL5_NOTFOUND if end of iteration list is reached
  359. CL5_DB_ERROR if any other db error occurred;
  360. CL5_BADFORMAT if data in db is of unrecognized format;
  361. CL5_MEMORY_ERROR if memory allocation fails.
  362. */
  363. int cl5GetNextOperationToReplay (CL5ReplayIterator *iterator,
  364. CL5Entry *entry);
  365. /* Name: cl5DestroyReplayIterator
  366. Description: destroys iterator
  367. Parameters: iterator - iterator to destroy
  368. Return: none
  369. */
  370. void cl5DestroyReplayIterator (CL5ReplayIterator **iterator);
  371. /* Name: cl5DeleteOnClose
  372. Description: marks changelog for deletion when it is closed
  373. Parameters: flag; if flag = 1 then delete else don't
  374. Return: none
  375. */
  376. void cl5DeleteOnClose (PRBool rm);
  377. /* Name: cl5GetDir
  378. Description: returns changelog directory; must be freed by the caller;
  379. Parameters: none
  380. Return: copy of the directory; caller needs to free the string
  381. */
  382. char *cl5GetDir ();
  383. /* Name: cl5Exist
  384. Description: checks if a changelog exists in the specified directory
  385. Parameters: clDir - directory to check;
  386. Return: 1 - if changelog exists; 0 - otherwise
  387. */
  388. PRBool cl5Exist (const char *clDir);
  389. /* Name: cl5GetOperationCount
  390. Description: returns number of entries in the changelog. The changelog must be
  391. open for the value to be meaningful.
  392. Parameters: replica - optional parameter that specifies the replica whose operations
  393. we wish to count; if NULL all changelog entries are counted
  394. Return: number of entries in the changelog
  395. */
  396. int cl5GetOperationCount (Object *replica);
  397. /* Name: cl5_operation_parameters_done
  398. Description: frees all parameters that are not freed by operation_parameters_done
  399. function in the server.
  400. */
  401. void cl5_operation_parameters_done (struct slapi_operation_parameters *sop);
  402. /* Name: cl5CreateDirIfNeeded
  403. Description: Create the directory if it doesn't exist yet
  404. Parameters: dir - Contains the name of the directory to create. Must not be NULL
  405. Return: CL5_SUCCESS if succeeded or existed,
  406. CL5_SYSTEM_ERROR if failed.
  407. */
  408. int cl5CreateDirIfNeeded (const char *dir);
  409. int cl5DBData2Entry (const char *data, PRUint32 len, CL5Entry *entry);
  410. PRBool cl5HelperEntry (const char *csnstr, CSN *csn);
  411. CSN** cl5BuildCSNList (const RUV *consRuv, const RUV *supRuv);
  412. void cl5DestroyCSNList (CSN*** csns);
  413. int cl5_is_diskfull();
  414. int cl5_diskspace_is_available();
  415. /* Name: cl5DbDirIsEmpty
  416. Description: See if the given cldb directory is empty or doesn't yet exist.
  417. Parameters: dir - Contains the name of the directory.
  418. Return: TRUE - directory does not exist or is empty, is NULL, or is
  419. an empty string
  420. FALSE - otherwise
  421. */
  422. int cl5DbDirIsEmpty(const char *dir);
  423. /* Name: cl5WriteRUV
  424. Description: Write RUVs into changelog db's. Called before backup.
  425. Parameters: none
  426. Return: TRUE
  427. */
  428. int cl5WriteRUV();
  429. /* Name: cl5DeleteRUV
  430. Description: Read and delete RUVs from changelog db's. Called after backup.
  431. Parameters: none
  432. Return: TRUE
  433. */
  434. int cl5DeleteRUV();
  435. void cl5CleanRUV(ReplicaId rid);
  436. void cl5NotifyCleanup(int rid);
  437. void trigger_cl_trimming(ReplicaId rid);
  438. #endif