cl5_api.h 20 KB

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