]> git.sur5r.net Git - openldap/blob - servers/slurpd/rq.c
import localtime -> gmtime change from -devel
[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 "slurp.h"
39 #include "globals.h"
40
41
42 /* externs */
43 extern void Re_dump LDAP_P(( Re *re ));
44
45 #ifdef DECL_SYS_ERRLIST
46 extern char *sys_errlist[];
47 #endif /* DECL_SYS_ERRLIST */
48
49 /*
50  * Lock the replication queue.
51  */
52 static int
53 Rq_lock(
54     Rq  *rq
55 )
56 {
57     return( pthread_mutex_lock( &rq->rq_mutex ));
58 }
59
60
61 /*
62  * Unlock the replication queue.
63  */
64 static int
65 Rq_unlock(
66     Rq  *rq
67 )
68 {
69     return( pthread_mutex_unlock( &rq->rq_mutex ));
70 }
71
72
73
74 /*
75  * Return the head of the queue.  Callers should lock the queue before
76  * calling this routine.
77  */
78 static Re *
79 Rq_gethead(
80     Rq  *rq
81 )
82 {
83     return( rq == NULL ? NULL : rq->rq_head );
84 }
85
86
87 /*
88  * Return the next item in the queue.  Callers should lock the queue before
89  * calling this routine.
90  */
91 static Re *
92 Rq_getnext(
93     Re  *re
94 )
95 {
96     if ( re == NULL ) {
97         return NULL;
98     } else {
99         return( re->re_getnext( re ));
100     }
101 }
102
103
104 /*
105  * Delete the item at the head of the list.  The queue should be locked
106  * by the caller before calling this routine.
107  */
108 static int
109 Rq_delhead(
110     Rq  *rq
111 )
112 {
113     Re  *savedhead;
114     int rc;
115
116     if ( rq == NULL ) {
117         return( -1 );
118     }
119
120     savedhead = rq->rq_head;
121     if ( savedhead == NULL ) {
122         return( 0 );
123     }
124
125     if ( savedhead->re_getrefcnt( savedhead ) != 0 ) {
126         Debug( LDAP_DEBUG_ANY, "Warning: attempt to delete when refcnt != 0\n",
127                 0, 0, 0 );
128         return( -1 );
129     }
130
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 */
134     return( rc );
135 }
136
137
138 /* 
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.
143  */
144 static int
145 Rq_add(
146     Rq          *rq,
147     char        *buf
148 )
149 {
150     Re  *re;
151     int wasempty = 0;
152
153     /* Lock the queue */
154     rq->rq_lock( rq );
155
156     /* Create a new Re */
157     if ( Re_init( &re ) < 0 ) {
158         rq->rq_unlock( rq );
159         return -1;
160     }
161
162     /* parse buf and fill in the re struct */
163     if ( re->re_parse( re, buf ) < 0 ) {
164         re->re_free( re );
165         rq->rq_unlock( rq );
166         return -1;
167     }
168
169     /* Insert into queue */
170     if ( rq->rq_head == NULL ) {
171         rq->rq_head = re;
172         rq->rq_tail = re;
173         wasempty = 1;
174     } else {
175         rq->rq_tail->re_next = re;
176     }
177
178     /* set the sequence number */
179     re->re_seq = 0;
180     if ( !wasempty && !strcmp(rq->rq_tail->re_timestamp, re->re_timestamp )) {
181         /*
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.
184          */
185         re->re_seq = rq->rq_tail->re_seq + 1;
186     }
187     rq->rq_tail = re;
188
189     /* Increment count of items in queue */
190     rq->rq_nre++;
191     /* wake up any threads waiting for more work */
192     pthread_cond_broadcast( &rq->rq_more );
193
194     /* ... and unlock the queue */
195     rq->rq_unlock( rq );
196
197     return 0;
198 }
199
200
201 /*
202  * Garbage-collect the replication queue.  Locking is handled internally.
203  */
204 static void
205 Rq_gc(
206     Rq  *rq
207 )
208 {
209     if ( rq == NULL ) {
210         Debug( LDAP_DEBUG_ANY, "Rq_gc: rq is NULL!\n", 0, 0, 0 );
211         return;
212     }
213     rq->rq_lock( rq ); 
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 */
218     }
219     rq->rq_unlock( rq ); 
220     return;
221 }
222
223
224 /*
225  * For debugging: dump the contents of the replication queue to a file.
226  * Locking is handled internally.
227  */
228 static void
229 Rq_dump(
230     Rq  *rq
231 )
232 {
233     Re          *re;
234     FILE        *fp;
235
236     if ( rq == NULL ) {
237         Debug( LDAP_DEBUG_ANY, "Rq_dump: rq is NULL!\n", 0, 0, 0 );
238         return;
239     }
240
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 );
244         return;
245     }
246
247     rq->rq_lock( rq );
248     for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {
249         re->re_dump( re, fp );
250     }
251     rq->rq_unlock( rq );
252     fclose( fp );
253     return;
254 }
255
256
257 /*
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.
261  */
262 static int
263 Rq_write(
264     Rq          *rq,
265     FILE        *fp
266 )
267 {
268     Re          *re;
269     time_t      now;
270
271     if ( rq == NULL ) {
272         return -1;
273     }
274
275     Debug( LDAP_DEBUG_ARGS, "re-write on-disk replication log\n",
276             0, 0, 0 );
277 #ifndef SEEK_SET
278 #define SEEK_SET 0
279 #endif
280     fseek( fp, 0L, SEEK_SET );  /* Go to beginning of file */
281     rq->rq_lock( rq );
282
283     for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {
284         if ( re->re_write( NULL, re, fp ) < 0 ) {
285             fflush( fp );
286             rq->rq_unlock( rq );
287             return -1;
288         }
289     }
290     fflush( fp );
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 );
296     }
297     rq->rq_ndel = 0;    /* reset count of deleted re's */
298     time( &now );
299     rq->rq_lasttrim = now;      /* reset last trim time */
300     rq->rq_unlock( rq );
301     return 0;
302 }
303
304
305 /*
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.
311  *
312  * Return 1 if replogfile should be trimmed, 0 if not.
313  * Any different policy should be implemented by replacing this function.
314  */
315 static int
316 Rq_needtrim(
317     Rq  *rq
318 )
319 {
320     int         rc = 0;
321     Re          *re;
322     int         nzrc = 0;       /* nzrc is count of entries with refcnt == 0 */
323     time_t      now;
324
325     if ( rq == NULL ) {
326         return 0;
327     }
328
329     rq->rq_lock( rq );
330
331     time( &now );
332
333     if ( now > ( rq->rq_lasttrim + TRIMCHECK_INTERVAL )) {
334         rc = ( rq->rq_ndel >= 50 );
335     } else {
336         rc = 0;
337     }
338     rq->rq_unlock( rq );
339     return rc;
340 }
341
342
343 /*
344  * Return counts of Re structs in the queue.
345  */
346 static int
347 Rq_getcount(
348     Rq  *rq,
349     int type
350 )
351 {
352     int count = 0;
353     Re  *re;
354
355     if ( rq == NULL ) {
356         return 0;
357     }
358
359     rq->rq_lock( rq );
360     if ( type == RQ_COUNT_ALL ) {
361         count = rq->rq_nre;
362     } else {
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 ) {
367                     count++;
368                 }
369             }
370         }
371     }
372     rq->rq_unlock( rq );
373     return count;
374 }
375
376
377 /* 
378  * Allocate and initialize an Rq object.
379  */
380 int
381 Rq_init(
382     Rq  **rq
383 )
384 {
385     /* Instantiate the struct */
386     (*rq) = (Rq *) malloc( sizeof( Rq ));
387     if ( *rq == NULL ) {
388         return -1;
389     }
390
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;
403
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;
409     (*rq)->rq_nre = 0;
410     (*rq)->rq_ndel = 0;
411     (*rq)->rq_lasttrim = (time_t) 0L;
412
413     return 0;
414 }
415