NAME
map_waitfile, remap_waitfile - file used to signal programs writing to a database
SYNOPSIS
int *map_waitfile (Dbptr db, char **fp, int quiet)
int *remap_waitfile (Dbptr db, int *lock, char **fp)
DESCRIPTION
map_waitfile creates (if necessary) and maps into memory a file named
db.MSGFILE.
This file is used by
orb2db_msg(1) and programs like
orb2db(1) and
orb2wf(1).
orb2db_msg is used by
rtdbclean(1) to signal that a database is about to be modified wholesale,
probably removing old records from the wfdisc table.
Actually, only the first 8 bytes of the file are used. The first 4 bytes are a flag
to signal that a cleanup is in progress; the second 4 bytes
are an integer count, so that a process can detect a cleanup
which happened while it was suspended and close and reopen
the database.
The interaction between orb2db_msg and the program writing to the database should be the following:
-
The program writing to the database (e.g. orb2db) opens the file at startup by calling map_waitfile;
map_waitfile returns a pointer to the first 4 bytes of the file.
-
The program then regularly checks this integer; when it's non-zero, the program must stop writing to
the database and close it, and then wait for integer to change back to zero. Once the integer has
changed back to zero, the database must be reopened, and any database pointers held by the program
must be updated, since the record number may have changed, or the record may have been deleted.
-
The program should also check to see if the counter in the second integer of the file has changed;
if it has, this is an indication that a database cleanup has occurred while the program was waiting
for an input packet. This also means the program must close and reopen the database, as database
records have probably changed.
If the
FILES
The file created by map_waitfile (if it doesn't already exist) is
database_name.MSGFILE.
EXAMPLE
int
check_for_pause (int pktid, Save_params *params)
{
char *s, *t ;
int lost_waitfile = 0 ;
static int last_count = 0 ;
static int *lock=0 ;
static char *waitfile ;
int retcode = 0 ;
if ( lock == 0 ) {
lock = map_waitfile ( Params.db, &waitfile, 0 ) ;
if ( lock == 0 ) {
elog_die (0, "failed to open waitfile" ) ;
}
last_count = lock[1] ;
}
if ( *lock || last_count != lock[1]) {
struct stat statbuf ;
dbclose ( params->db ) ;
restat ( waitfile, &statbuf ) ;
elog_notify ( 0, "paused at pktid #%ld after %s, waiting for continue\n",
pktid, s=strtdelta(now() - statbuf.st_mtime) ) ;
free(s) ;
while ( *lock ) {
if ( restat ( waitfile, &statbuf ) ) {
complain ( 1, "waitfile %s disappeared!\n", waitfile ) ;
lost_waitfile = 1 ;
break ;
}
if ( now() - statbuf.st_mtime > params->pause_timeout ) {
complain ( 1, "waitfile is %s old > %s -- clearing pause and continuing\n",
s=strtdelta(now() - statbuf.st_mtime),
t=strtdelta((double) params->pause_timeout) ) ;
free(s) ;
free(t) ;
*lock = 0 ;
break;
}
snooze(1.0) ;
}
last_count = lock[1] ;
elog_notify ( 0, "continuing after pause\n" ) ;
if (lost_waitfile) {
lock = remap_waitfile ( params->db, lock, &waitfile ) ;
if ( lock == 0 ) {
elog_die (0, "failed to re-open waitfile" ) ;
} else {
lost_waitfile = 0 ;
}
}
retcode = 1 ;
}
return retcode ;
}
RETURN VALUES
map_waitfile returns a pointer to the memory mapped file, and a pointer to the name of the file.
remap_waitfile is given the original pointer to the memory mapped file, and returns a pointer to the new memory mapped file.
If either map_waitfile or remap_waitfile returns zero, some error has occurred and a message is left on the
elog(3) error log.
LIBRARY
$(DBLIBS)
ATTRIBUTES
MT-Safe
SEE ALSO
orb2db_msg(1)
orb2db(1)
AUTHOR
Daniel Quinlan