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