2 * Copyright (c) 1996 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
14 * rq.c - routines used to manage the queue of replication entries.
15 * An Rq (Replication queue) struct contains a linked list of Re
16 * (Replication entry) structures.
18 * Routines wishing to access the replication queue should do so through
19 * the Rq struct's member functions, e.g. rq->rq_gethead() and friends.
20 * For example, Re structs should be added to the queue by calling
21 * the rq_add() member function.
23 * Access to the queue is serialized by a mutex. Member functions which do
24 * not do their own locking should only be called after locking the queue
25 * using the rq_lock() member function. The queue should be unlocked with
26 * the rq_unlock() member function.
28 * Note that some member functions handle their own locking internally.
29 * Callers should not lock the queue before calling these functions.
30 * See the comment block for each function below.
43 extern void Re_dump LDAP_P(( Re *re ));
45 #ifdef DECL_SYS_ERRLIST
46 extern char *sys_errlist[];
47 #endif /* DECL_SYS_ERRLIST */
50 * Lock the replication queue.
57 return( pthread_mutex_lock( &rq->rq_mutex ));
62 * Unlock the replication queue.
69 return( pthread_mutex_unlock( &rq->rq_mutex ));
75 * Return the head of the queue. Callers should lock the queue before
76 * calling this routine.
83 return( rq == NULL ? NULL : rq->rq_head );
88 * Return the next item in the queue. Callers should lock the queue before
89 * calling this routine.
99 return( re->re_getnext( re ));
105 * Delete the item at the head of the list. The queue should be locked
106 * by the caller before calling this routine.
120 savedhead = rq->rq_head;
121 if ( savedhead == NULL ) {
125 if ( savedhead->re_getrefcnt( savedhead ) != 0 ) {
126 Debug( LDAP_DEBUG_ANY, "Warning: attempt to delete when refcnt != 0\n",
131 rq->rq_head = rq->rq_head->re_getnext( rq->rq_head );
132 rc = savedhead->re_free( savedhead );
133 rq->rq_nre--; /* decrement count of Re's in queue */
139 * Add an entry to the tail of the replication queue. Locking is handled
140 * internally. When items are added to the queue, this routine wakes
141 * up any threads which are waiting for more work by signaling on the
142 * rq->rq_more condition variable.
156 /* Create a new Re */
157 if ( Re_init( &re ) < 0 ) {
162 /* parse buf and fill in the re struct */
163 if ( re->re_parse( re, buf ) < 0 ) {
169 /* Insert into queue */
170 if ( rq->rq_head == NULL ) {
175 rq->rq_tail->re_next = re;
178 /* set the sequence number */
180 if ( !wasempty && !strcmp(rq->rq_tail->re_timestamp, re->re_timestamp )) {
182 * Our new re has the same timestamp as the tail's timestamp.
183 * Increment the seq number in the tail and use it as our seq number.
185 re->re_seq = rq->rq_tail->re_seq + 1;
189 /* Increment count of items in queue */
191 /* wake up any threads waiting for more work */
192 pthread_cond_broadcast( &rq->rq_more );
194 /* ... and unlock the queue */
202 * Garbage-collect the replication queue. Locking is handled internally.
210 Debug( LDAP_DEBUG_ANY, "Rq_gc: rq is NULL!\n", 0, 0, 0 );
214 while (( rq->rq_head != NULL ) &&
215 ( rq->rq_head->re_getrefcnt( rq->rq_head ) == 0 )) {
216 rq->rq_delhead( rq );
217 rq->rq_ndel++; /* increment count of deleted entries */
225 * For debugging: dump the contents of the replication queue to a file.
226 * Locking is handled internally.
237 Debug( LDAP_DEBUG_ANY, "Rq_dump: rq is NULL!\n", 0, 0, 0 );
241 if (( fp = fopen( SLURPD_DUMPFILE, "w" )) == NULL ) {
242 Debug( LDAP_DEBUG_ANY, "Rq_dump: cannot open \"%s\" for write\n",
243 SLURPD_DUMPFILE, 0, 0 );
248 for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {
249 re->re_dump( re, fp );
258 * Write the contents of a replication queue to a file. Returns zero if
259 * successful, -1 if not. Handles queue locking internally. Callers should
260 * provide an open file pointer, which should refer to a locked file.
275 Debug( LDAP_DEBUG_ARGS, "re-write on-disk replication log\n",
280 fseek( fp, 0L, SEEK_SET ); /* Go to beginning of file */
283 for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {
284 if ( re->re_write( NULL, re, fp ) < 0 ) {
291 sglob->srpos = ftell( fp ); /* update replog file position */
292 /* and truncate to correct len */
293 if ( ftruncate( fileno( fp ), sglob->srpos ) < 0 ) {
294 Debug( LDAP_DEBUG_ANY, "Error truncating replication log: %s\n",
295 sys_errlist[ errno ], 0, 0 );
297 rq->rq_ndel = 0; /* reset count of deleted re's */
299 rq->rq_lasttrim = now; /* reset last trim time */
306 * Check to see if the private slurpd replication log needs trimming.
307 * The current criteria are:
308 * - The last trim was more than 5 minutes ago, *and*
309 * - We've finished with at least 50 replication log entries since the
310 * last time we re-wrote the replication log.
312 * Return 1 if replogfile should be trimmed, 0 if not.
313 * Any different policy should be implemented by replacing this function.
322 int nzrc = 0; /* nzrc is count of entries with refcnt == 0 */
333 if ( now > ( rq->rq_lasttrim + TRIMCHECK_INTERVAL )) {
334 rc = ( rq->rq_ndel >= 50 );
344 * Return counts of Re structs in the queue.
360 if ( type == RQ_COUNT_ALL ) {
363 for ( re = rq->rq_gethead( rq ); re != NULL;
364 re = rq->rq_getnext( re )) {
365 if ( type == RQ_COUNT_NZRC ) {
366 if ( re->re_getrefcnt( re ) > 1 ) {
378 * Allocate and initialize an Rq object.
385 /* Instantiate the struct */
386 (*rq) = (Rq *) malloc( sizeof( Rq ));
391 /* Fill in all the function pointers */
392 (*rq)->rq_gethead = Rq_gethead;
393 (*rq)->rq_getnext = Rq_getnext;
394 (*rq)->rq_delhead = Rq_delhead;
395 (*rq)->rq_add = Rq_add;
396 (*rq)->rq_gc = Rq_gc;
397 (*rq)->rq_lock = Rq_lock;
398 (*rq)->rq_unlock = Rq_unlock;
399 (*rq)->rq_dump = Rq_dump;
400 (*rq)->rq_needtrim = Rq_needtrim;
401 (*rq)->rq_write = Rq_write;
402 (*rq)->rq_getcount = Rq_getcount;
404 /* Initialize private data */
405 pthread_mutex_init( &((*rq)->rq_mutex), pthread_mutexattr_default );
406 pthread_cond_init( &((*rq)->rq_more), pthread_condattr_default );
407 (*rq)->rq_head = NULL;
408 (*rq)->rq_tail = NULL;
411 (*rq)->rq_lasttrim = (time_t) 0L;