]> git.sur5r.net Git - openldap/blob - servers/slurpd/rq.c
Kill lber_debug
[openldap] / servers / slurpd / rq.c
1 /*
2  * Copyright (c) 1996 Regents of the University of Michigan.
3  * All rights reserved.
4  *
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.
11  */
12
13 /*
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.
17  *
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.
22  *
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.
27  *
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.
31  *
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37
38 #include <ac/stdlib.h>
39 #include <ac/unistd.h>          /* get ftruncate() */
40
41 #include "slurp.h"
42 #include "globals.h"
43
44
45 /*
46  * Lock the replication queue.
47  */
48 static int
49 Rq_lock(
50     Rq  *rq
51 )
52 {
53     return( ldap_pvt_thread_mutex_lock( &rq->rq_mutex ));
54 }
55
56
57 /*
58  * Unlock the replication queue.
59  */
60 static int
61 Rq_unlock(
62     Rq  *rq
63 )
64 {
65     return( ldap_pvt_thread_mutex_unlock( &rq->rq_mutex ));
66 }
67
68
69
70 /*
71  * Return the head of the queue.  Callers should lock the queue before
72  * calling this routine.
73  */
74 static Re *
75 Rq_gethead(
76     Rq  *rq
77 )
78 {
79     return( rq == NULL ? NULL : rq->rq_head );
80 }
81
82
83 /*
84  * Return the next item in the queue.  Callers should lock the queue before
85  * calling this routine.
86  */
87 static Re *
88 Rq_getnext(
89     Re  *re
90 )
91 {
92     if ( re == NULL ) {
93         return NULL;
94     } else {
95         return( re->re_getnext( re ));
96     }
97 }
98
99
100 /*
101  * Delete the item at the head of the list.  The queue should be locked
102  * by the caller before calling this routine.
103  */
104 static int
105 Rq_delhead(
106     Rq  *rq
107 )
108 {
109     Re  *savedhead;
110     int rc;
111
112     if ( rq == NULL ) {
113         return( -1 );
114     }
115
116     savedhead = rq->rq_head;
117     if ( savedhead == NULL ) {
118         return( 0 );
119     }
120
121     if ( savedhead->re_getrefcnt( savedhead ) != 0 ) {
122         Debug( LDAP_DEBUG_ANY, "Warning: attempt to delete when refcnt != 0\n",
123                 0, 0, 0 );
124         return( -1 );
125     }
126
127     rq->rq_head = rq->rq_head->re_getnext( rq->rq_head );
128     rc = savedhead->re_free( savedhead );
129     rq->rq_nre--;       /* decrement count of Re's in queue */
130     return( rc );
131 }
132
133
134 /* 
135  * Add an entry to the tail of the replication queue.  Locking is handled
136  * internally.  When items are added to the queue, this routine wakes
137  * up any threads which are waiting for more work by signaling on the
138  * rq->rq_more condition variable.
139  */
140 static int
141 Rq_add(
142     Rq          *rq,
143     char        *buf
144 )
145 {
146     Re  *re;
147     int wasempty = 0;
148
149     /* Lock the queue */
150     rq->rq_lock( rq );
151
152     /* Create a new Re */
153     if ( Re_init( &re ) < 0 ) {
154         rq->rq_unlock( rq );
155         return -1;
156     }
157
158     /* parse buf and fill in the re struct */
159     if ( re->re_parse( re, buf ) < 0 ) {
160         re->re_free( re );
161         rq->rq_unlock( rq );
162         return -1;
163     }
164
165     /* Insert into queue */
166     if ( rq->rq_head == NULL ) {
167         rq->rq_head = re;
168         rq->rq_tail = re;
169         wasempty = 1;
170     } else {
171         rq->rq_tail->re_next = re;
172     }
173
174     /* set the sequence number */
175     re->re_seq = 0;
176     if ( !wasempty && !strcmp(rq->rq_tail->re_timestamp, re->re_timestamp )) {
177         /*
178          * Our new re has the same timestamp as the tail's timestamp.
179          * Increment the seq number in the tail and use it as our seq number.
180          */
181         re->re_seq = rq->rq_tail->re_seq + 1;
182     }
183     rq->rq_tail = re;
184
185     /* Increment count of items in queue */
186     rq->rq_nre++;
187     /* wake up any threads waiting for more work */
188     ldap_pvt_thread_cond_broadcast( &rq->rq_more );
189
190     /* ... and unlock the queue */
191     rq->rq_unlock( rq );
192
193     return 0;
194 }
195
196
197 /*
198  * Garbage-collect the replication queue.  Locking is handled internally.
199  */
200 static void
201 Rq_gc(
202     Rq  *rq
203 )
204 {
205     if ( rq == NULL ) {
206         Debug( LDAP_DEBUG_ANY, "Rq_gc: rq is NULL!\n", 0, 0, 0 );
207         return;
208     }
209     rq->rq_lock( rq ); 
210     while (( rq->rq_head != NULL ) &&
211             ( rq->rq_head->re_getrefcnt( rq->rq_head ) == 0 )) {
212         rq->rq_delhead( rq );
213         rq->rq_ndel++;  /* increment count of deleted entries */
214     }
215     rq->rq_unlock( rq ); 
216     return;
217 }
218
219
220 /*
221  * For debugging: dump the contents of the replication queue to a file.
222  * Locking is handled internally.
223  */
224 static void
225 Rq_dump(
226     Rq  *rq
227 )
228 {
229     Re          *re;
230     FILE        *fp;
231
232     if ( rq == NULL ) {
233         Debug( LDAP_DEBUG_ANY, "Rq_dump: rq is NULL!\n", 0, 0, 0 );
234         return;
235     }
236
237     if (( fp = fopen( SLURPD_DUMPFILE, "w" )) == NULL ) {
238         Debug( LDAP_DEBUG_ANY, "Rq_dump: cannot open \"%s\" for write\n",
239                 SLURPD_DUMPFILE, 0, 0 );
240         return;
241     }
242
243     rq->rq_lock( rq );
244     for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {
245         re->re_dump( re, fp );
246     }
247     rq->rq_unlock( rq );
248     fclose( fp );
249     return;
250 }
251
252
253 /*
254  * Write the contents of a replication queue to a file.  Returns zero if
255  * successful, -1 if not.  Handles queue locking internally.  Callers should
256  * provide an open file pointer, which should refer to a locked file.
257  */
258 static int
259 Rq_write(
260     Rq          *rq,
261     FILE        *fp
262 )
263 {
264     Re          *re;
265     time_t      now;
266
267     if ( rq == NULL ) {
268         return -1;
269     }
270
271     Debug( LDAP_DEBUG_ARGS, "re-write on-disk replication log\n",
272             0, 0, 0 );
273 #ifndef SEEK_SET
274 #define SEEK_SET 0
275 #endif
276     fseek( fp, 0L, SEEK_SET );  /* Go to beginning of file */
277     rq->rq_lock( rq );
278
279     for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {
280         if ( re->re_write( NULL, re, fp ) < 0 ) {
281             fflush( fp );
282             rq->rq_unlock( rq );
283             return -1;
284         }
285     }
286     fflush( fp );
287     sglob->srpos = ftell( fp ); /* update replog file position */
288     /* and truncate to correct len */
289     if ( ftruncate( fileno( fp ), sglob->srpos ) < 0 ) {
290         Debug( LDAP_DEBUG_ANY, "Error truncating replication log: %s\n",
291                 sys_errlist[ errno ], 0, 0 );
292     }
293     rq->rq_ndel = 0;    /* reset count of deleted re's */
294     time( &now );
295     rq->rq_lasttrim = now;      /* reset last trim time */
296     rq->rq_unlock( rq );
297     return 0;
298 }
299
300
301 /*
302  * Check to see if the private slurpd replication log needs trimming.
303  * The current criteria are:
304  *  - The last trim was more than 5 minutes ago, *and*
305  *  - We've finished with at least 50 replication log entries since the
306  *    last time we re-wrote the replication log.
307  *
308  * Return 1 if replogfile should be trimmed, 0 if not.
309  * Any different policy should be implemented by replacing this function.
310  */
311 static int
312 Rq_needtrim(
313     Rq  *rq
314 )
315 {
316     int         rc = 0;
317     Re          *re;
318     int         nzrc = 0;       /* nzrc is count of entries with refcnt == 0 */
319     time_t      now;
320
321     if ( rq == NULL ) {
322         return 0;
323     }
324
325     rq->rq_lock( rq );
326
327     time( &now );
328
329     if ( now > ( rq->rq_lasttrim + TRIMCHECK_INTERVAL )) {
330         rc = ( rq->rq_ndel >= 50 );
331     } else {
332         rc = 0;
333     }
334     rq->rq_unlock( rq );
335     return rc;
336 }
337
338
339 /*
340  * Return counts of Re structs in the queue.
341  */
342 static int
343 Rq_getcount(
344     Rq  *rq,
345     int type
346 )
347 {
348     int count = 0;
349     Re  *re;
350
351     if ( rq == NULL ) {
352         return 0;
353     }
354
355     rq->rq_lock( rq );
356     if ( type == RQ_COUNT_ALL ) {
357         count = rq->rq_nre;
358     } else {
359         for ( re = rq->rq_gethead( rq ); re != NULL;
360                 re = rq->rq_getnext( re )) {
361             if ( type == RQ_COUNT_NZRC ) {
362                 if ( re->re_getrefcnt( re ) > 1 ) {
363                     count++;
364                 }
365             }
366         }
367     }
368     rq->rq_unlock( rq );
369     return count;
370 }
371
372
373 /* 
374  * Allocate and initialize an Rq object.
375  */
376 int
377 Rq_init(
378     Rq  **rq
379 )
380 {
381     /* Instantiate the struct */
382     (*rq) = (Rq *) malloc( sizeof( Rq ));
383     if ( *rq == NULL ) {
384         return -1;
385     }
386
387     /* Fill in all the function pointers */
388     (*rq)->rq_gethead = Rq_gethead;
389     (*rq)->rq_getnext = Rq_getnext;
390     (*rq)->rq_delhead = Rq_delhead;
391     (*rq)->rq_add = Rq_add;
392     (*rq)->rq_gc = Rq_gc;
393     (*rq)->rq_lock = Rq_lock;
394     (*rq)->rq_unlock = Rq_unlock;
395     (*rq)->rq_dump = Rq_dump;
396     (*rq)->rq_needtrim = Rq_needtrim;
397     (*rq)->rq_write = Rq_write;
398     (*rq)->rq_getcount = Rq_getcount;
399
400     /* Initialize private data */
401     ldap_pvt_thread_mutex_init( &((*rq)->rq_mutex) );
402     ldap_pvt_thread_cond_init( &((*rq)->rq_more) );
403     (*rq)->rq_head = NULL;
404     (*rq)->rq_tail = NULL;
405     (*rq)->rq_nre = 0;
406     (*rq)->rq_ndel = 0;
407     (*rq)->rq_lasttrim = (time_t) 0L;
408
409     return 0;
410 }