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