xdasd_mcache.c File Reference

OpenXDAS daemon file-based persistent message cache sub-system. More...

#include "xdasd_mcache.h"
#include "xdasd_parse.h"
#include "xdasd_log.h"
#include "xdas_base.h"
#include "xdas_wire.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

Go to the source code of this file.

Data Structures

struct  mcfile_
 A message cache file abstraction. More...
struct  mcache_
 A message cache abstraction. More...

Defines

#define MC_FILE_ATTRS   (0644)
#define MC_DIR_ATTRS   (0744)
#define MC_OPEN_FLAGS   (O_RDWR)
#define MC_CREATE_FLAGS   (O_RDWR | O_CREAT)
#define MCACHE_MAX_FILESZ   (1024 * 1024)
#define MCACHE_MAX_RECSZ   (64 * 1024)
#define MCACHE_BASE_FNAME   "xdasd.cache"
 The base file name of an xdas mcache file.
#define MCH_BOF   0
#define MCH_EOF   1
#define MCH_LIM   2

Typedefs

typedef struct mcfile_ mcfile_t
 A message cache file abstraction.
typedef struct mcache_ mcache_t
 A message cache abstraction.

Functions

static void mcache_calc_file_range (const char *mcdir, unsigned *lowp, unsigned *highp)
 Calculate the range of message cache files currently on disk.
static void mcache_close (mcfile_t *fp, int ferase)
 Close an mcache file.
static mcfile_tmcache_dup (mcfile_t *fp)
 Duplicate an existing mcfile object.
static int mcache_read_file_header (mcfile_t *fp)
 Open an existing mcache file numbered as specified.
static int mcache_write_file_header (mcfile_t *fp)
 Write a new file header to the specified file.
static int mcache_open (const char *mcdir, unsigned fileno, mcfile_t **fpp)
 Open an existing mcache file numbered as specified.
static int mcache_create (const char *mcdir, unsigned fileno, mcfile_t **fpp)
 Create a new mcache file numbered as specified.
static int mcache_write_record (mcfile_t *fp, Parsed *parsed, size_t msgsz)
 Write a message to the current input cache file and update all pointers.
static Parsedmcache_read_record (mcfile_t *fp)
 Read a parsed message from the current position of the specified file.
static int mcache_update_bof (mcfile_t *fp)
 Update the specified mcfile's BOF pointer.
static int mcache_put_msg (mcache_t *mcp, Parsed *parsed)
 Saves a parsed message to the file cache as a cache record.
static Parsedmcache_get_msg (mcache_t *mcp)
 Retrieve the next output message from a message cache.
int xdasd_mcache_put_msg (MsgCache mc, Parsed *parsed)
 Saves an audit message to the global file-based persistent message cache.
Parsedxdasd_mcache_get_msg (MsgCache mc)
 Retrieve the next output message from the global message cache.
MsgCache xdasd_mcache_create (const char *msgfdir)
 Create a new message cache object.
void xdasd_mcache_destroy (MsgCache mc)
 Cleanup the message cache sub-system.


Detailed Description

OpenXDAS daemon file-based persistent message cache sub-system.

The file cache is a set of cache files that provide persistence to the log cache data. This is done by using fixed size cache files to contain all data that is not yet logged to registered loggers. The system looks like this:

xdasd.cache.1 xdasd.cache.2 xdasd.cache.3 ... xdasd.cache.n

where the smallest numbered file contains the next entry to be committed to registered loggers, and the largest numbered file contains the last entry logged. Each cache file is created exactly 1Mb in size, and records are written to it in the following format:

   offset      type     contents
   ------      ----     --------
   0           uint32   bof (beginning of file)
   4           uint32   eof (end of file)
   8           uint32   limit (max file size)
   12          uint32   recsz_1
   16          char     rec_1[recsz_1]
   20+recsz_1  uint32   recsz_2
   24+recsz_1  char     rec_2[recsz_2]
   ...

Records are formatted internally like this:

   offset      type     contents
   ------      ----     --------
   0           uint32   recsz (count of remaining bytes in the record)
   4           uint32   flags
   8           uint16   offsets[34]
   76          char     msg[recsz-76]

Cache files are written until a record is too large to fit into the last logged (input) file. Records are never split between cache files. As records are added to the last logged cache file (largest file number) and removed from the last committed cache file (smallest file number), the pointer information at the top of the files on either end of the set is updated to reflect new bof (beginning of file) and eof (end of file) offsets (respectively).

When all of the information in the last committed file is committed to all registered loggers, it is removed, and the next file (numerically) becomes the last committed file. Ultimately, if the last committed file catches up with the last logged file, all cache files are removed through this process. The next time a record is logged, the caching system starts over again at file 0.

In case of a system crash, on restart the system continues the process of comitting and logging though the cache file set where it left off before the crash, thereby maintaining audit information integrity.

Some information is maintained in memory for the sake of performance. The file numbers of the end files are maintained in memory, and recalculated at system startup. The end files are also opened, and handles to these files are maintained in memory. These file handles are recalculated and opened again on startup after a crash.

The cache system always tries to flush all messages to all registered loggers before allowing the system to shut down cleanly, thus a clean shutdown and startup should result in no existing cache files.

This implementation depends on serializing calls to xdasd_mcache_get_msg and xdasd_mcache_put_msg functions. The data and file structures these routines manipulate will become corrupt if calls are not serialized.

This implementation makes no guarantees about the safety of messages not fully committed to this interface. In other words, there is a point during the call to xdasd_mcache_put_msg where the safety of the message becomes the responsibility of the cache. Before this point, the message is lost.

Author:
John Calcote (jcalcote@novell.com)
Attention:
Please submit patches to http://openxdas.sourceforge.net

Definition in file xdasd_mcache.c.


Define Documentation

#define MC_CREATE_FLAGS   (O_RDWR | O_CREAT)

Definition at line 164 of file xdasd_mcache.c.

Referenced by mcache_create().

#define MC_DIR_ATTRS   (0744)

Definition at line 162 of file xdasd_mcache.c.

Referenced by mcache_create().

#define MC_FILE_ATTRS   (0644)

Definition at line 161 of file xdasd_mcache.c.

Referenced by mcache_create().

#define MC_OPEN_FLAGS   (O_RDWR)

Definition at line 163 of file xdasd_mcache.c.

Referenced by mcache_open().


Generated on Thu Aug 20 22:33:07 2009 for OpenXDAS by  doxygen 1.5.6