OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESCache3 Class Reference

Implementation of a caching mechanism for compressed data. More...

#include <BESCache3.h>

Inheritance diagram for BESCache3:
Inheritance graph
Collaboration diagram for BESCache3:
Collaboration graph

Public Member Functions

virtual bool cache_too_big (unsigned long long current_size) const
 look at the cache size; is it too large? Look at the cache size and see if it is too big. More...
 
virtual bool create_and_lock (const string &target, int &fd)
 Create a file in the cache and lock it for write access. More...
 
virtual void dump (ostream &strm) const
 dumps information about this object More...
 
virtual void exclusive_to_shared_lock (int fd)
 Transfer from an exclusive lock to a shared lock. More...
 
string get_cache_file_name (const string &src)
 Build the name of file that will holds the uncompressed data from 'src' in the cache. More...
 
virtual unsigned long long get_cache_size ()
 Get the cache size. More...
 
virtual bool get_read_lock (const string &target, int &fd)
 Get a read-only lock on the file if it exists. More...
 
virtual void lock_cache_read ()
 Get a shared lock on the 'cache info' file. More...
 
virtual void lock_cache_write ()
 Get an exclusive lock on the 'cache info' file. More...
 
virtual void unlock_and_close (const string &target)
 Unlock the named file. More...
 
virtual void unlock_and_close (int fd)
 Unlock the file. More...
 
virtual void unlock_cache ()
 Unlock the cache info file. More...
 
virtual void update_and_purge (const string &new_file)
 Purge files from the cache. More...
 
virtual unsigned long long update_cache_info (const string &target)
 Update the cache info file to include 'target'. More...
 
virtual ~BESCache3 ()
 

Static Public Member Functions

static BESCache3get_instance (BESKeys *keys, const string &cache_dir_key, const string &prefix_key, const string &size_key)
 Get an instance of the BESCache3 object. More...
 
static BESCache3get_instance (const string &cache_dir, const string &prefix, unsigned long size)
 Get an instance of the BESCache3 object. More...
 
static BESCache3get_instance ()
 Get an instance of the BESCache3 object. More...
 

Detailed Description

Implementation of a caching mechanism for compressed data.

This cache uses simple advisory locking found on most modern unix file systems. Compressed files are decompressed and stored in a cache where they can be used over and over until removed from the cache. Several processes can share the cache with each reading from files. At the same time, new files can be added and the cache can be purged, without disrupting the existing read operations.

How it works. When a file is added to the cache, the cache is locked - no other processes can add, read or remove files. Once a file has been added, the cache size is examined and, if needed, the cache is purged so that its size is 80% of the maximum size. Then the cache is unlocked. When a process looks to see if a file is already in the cache, the entire cache is locked. If the file is present, a shared read lock is obtained and the cache is unlocked.

Methods: create_and_lock() and get_read_lock() open and lock files; the former creates the file and locks it exclusively iff it does not exist, while the latter obtains a shared lock iff the file already exists. The unlock() methods unlock a file. The lock_cache_info() and unlock_cache_info() are used to control access to the whole cache - with the open + lock and close + unlock operations performed atomically. Other methods that operate on the cache info file must only be called when the lock has been obtained.

Definition at line 73 of file BESCache3.h.

Constructor & Destructor Documentation

virtual BESCache3::~BESCache3 ( )
inlinevirtual

Definition at line 115 of file BESCache3.h.

Member Function Documentation

bool BESCache3::cache_too_big ( unsigned long long  current_size) const
virtual

look at the cache size; is it too large? Look at the cache size and see if it is too big.

Returns
True if the size is too big, false otherwise.

Definition at line 698 of file BESCache3.cc.

Referenced by BESUncompressManager3::uncompress(), and update_and_purge().

bool BESCache3::create_and_lock ( const string &  target,
int &  fd 
)
virtual

Create a file in the cache and lock it for write access.

If the file does not exist, make it, open it for read-write access and get an exclusive lock on it. The locking operation blocks, although that should never happen.

Parameters
targetThe name of the file to make/open/lock
fdValue-result param that holds the file descriptor of the opened file
Returns
True if the operation was successful, false otherwise. This method will return false if the file already existed (the file won't be locked and the descriptor reference is undefined - but likely -1).
Exceptions
BESInternalErrorif any error except EEXIST is returned by open(2) or if fcntl(2) returns an error.

Definition at line 528 of file BESCache3.cc.

References BESDEBUG, lock_cache_write(), and unlock_cache().

Referenced by BESUncompressManager3::uncompress().

Here is the call graph for this function:

void BESCache3::dump ( ostream &  strm) const
virtual

dumps information about this object

Displays the pointer value of this instance along with information about this cache.

Parameters
strmC++ i/o stream to dump the information to

Implements BESObj.

Definition at line 878 of file BESCache3.cc.

References BESIndent::Indent(), BESIndent::LMarg(), and BESIndent::UnIndent().

Here is the call graph for this function:

void BESCache3::exclusive_to_shared_lock ( int  fd)
virtual

Transfer from an exclusive lock to a shared lock.

If the file has an exclusive write lock on it, change that to a shared read lock. This is an atomic operation. If the call to fcntl(2) is protected by locking the cache, a dead lock will result given typical use of this class. This method exists to help with the situation where one process has the cache locked and is blocking on a shared read lock for a file that a second process has locked exclusively (for writing). By changing the exclusive lock to a shared lock, the first process can get its shared lock and then release the cache.

Parameters
fdThe file descriptor that is exclusively locked and which, on exit, will have a shared lock.

Definition at line 558 of file BESCache3.cc.

Referenced by BESUncompressManager3::uncompress().

string BESCache3::get_cache_file_name ( const string &  src)

Build the name of file that will holds the uncompressed data from 'src' in the cache.

Note
How names are mangled: 'src' is the full name of the file to be cached.Tthe file name passed has an extension on the end that will be stripped once the file is cached. For example, if the full path to the file name is /usr/lib/data/fnoc1.nc.gz then the resulting file name will be #<prefix>#usr#lib#data#fnoc1.nc.

Definition at line 465 of file BESCache3.cc.

Referenced by BESUncompressManager3::uncompress().

unsigned long long BESCache3::get_cache_size ( )
virtual

Get the cache size.

Read the size information from the cache info file and return it. This methods locks the cache.

Returns
The size of the cache.

Definition at line 710 of file BESCache3.cc.

References lock_cache_read(), and unlock_cache().

Here is the call graph for this function:

BESCache3 * BESCache3::get_instance ( BESKeys keys,
const string &  cache_dir_key,
const string &  prefix_key,
const string &  size_key 
)
static

Get an instance of the BESCache3 object.

This class is a singleton, so the first call to any of three 'get_instance()' methods makes an instance and subsequent call return a pointer to that instance.

Parameters
keysThe BESKeys object (hold various configuration parameters)
cache_dir_keyKey to use to get the value of the cache directory
prefix_keyKey for the item/file prefix. Each file added to the cache uses this as a prefix so cached items can be easily identified when /tmp is used for the cache.
size_keyHow big should the cache be, in megabytes
Returns
A pointer to a BESCache3 object

Definition at line 74 of file BESCache3.cc.

BESCache3 * BESCache3::get_instance ( const string &  cache_dir,
const string &  prefix,
unsigned long  size 
)
static

Get an instance of the BESCache3 object.

This version is used for testing; it does not use the BESKeys object but takes values for the parameters directly.

Definition at line 86 of file BESCache3.cc.

BESCache3 * BESCache3::get_instance ( )
static

Get an instance of the BESCache3 object.

This version is used when there's no question that the cache has been instantiated.

Definition at line 98 of file BESCache3.cc.

Referenced by BESFileContainer::access(), and BESFileContainer::release().

bool BESCache3::get_read_lock ( const string &  target,
int &  fd 
)
virtual

Get a read-only lock on the file if it exists.

Try to get a read-only lock on the file, blocking until we can get it. If the file does not exist, return false.

Note
If this code returns false, that means the file did not exist in the cache at the time of the test. by the time the caller gets the result, the file may have been added to the cache by another process.
Parameters
srcsrc file that will be cached eventually
targeta value-result parameter set to the resulting cached file
Returns
true if the file is in the cache and has been locked, false if the file is/was not in the cache.
Exceptions
Errorif the attempt to get the (shared) lock failed for any reason other than that the file does/did not exist.

Definition at line 500 of file BESCache3.cc.

References BESDEBUG, lock_cache_read(), and unlock_cache().

Referenced by BESUncompressManager3::uncompress().

Here is the call graph for this function:

void BESCache3::lock_cache_read ( )
virtual

Get a shared lock on the 'cache info' file.

Definition at line 592 of file BESCache3.cc.

References BESDEBUG.

Referenced by get_cache_size(), and get_read_lock().

void BESCache3::lock_cache_write ( )
virtual

Get an exclusive lock on the 'cache info' file.

The 'cache info' file is used to control certain cache actions, ensuring that they are atomic. These include making sure that the create_and_lock() and read_and_lock() operations are atomic as well as the purge and related operations.

Note
This is intended to be used internally only bt might be useful in some settings.

Definition at line 580 of file BESCache3.cc.

References BESDEBUG.

Referenced by create_and_lock(), update_and_purge(), and update_cache_info().

void BESCache3::unlock_and_close ( const string &  file_name)
virtual

Unlock the named file.

This does not do any name mangling; it just unlocks whatever is named (or throws BESInternalError if the file cannot be closed).

Note
This method assumes that the file was opend/locked using one of read_and_lock() or create_and_lock(). Those methods record the name/file- descriptor pairs so that the files can be properly closed and locks released.
Parameters
file_nameThe name of the file to unlock.
Exceptions
BESInternalError

Definition at line 626 of file BESCache3.cc.

References BESDEBUG.

Referenced by BESFileContainer::release().

void BESCache3::unlock_and_close ( int  fd)
virtual

Unlock the file.

This does not do any name mangling; it just unlocks whatever is named (or throws BESInternalError if the file cannot be closed).

Parameters
fdThe descriptor of the file to unlock.
Exceptions
BESInternalError

Definition at line 638 of file BESCache3.cc.

References BESDEBUG.

void BESCache3::unlock_cache ( )
virtual

Unlock the cache info file.

Note
This is intended to be used internally only bt might be useful in some settings.

Definition at line 606 of file BESCache3.cc.

References BESDEBUG.

Referenced by create_and_lock(), get_cache_size(), get_read_lock(), BESUncompressManager3::uncompress(), update_and_purge(), and update_cache_info().

void BESCache3::update_and_purge ( const string &  new_file)
virtual

Purge files from the cache.

Purge files, oldest to newest, if the current size of the cache exceeds the size of the cache specified in the constructor. This method uses an exclusive lock on the cache for the duration of the purge process.

Parameters
new_fileThe name of a file this process just added to the cache. Using fcntl(2) locking there is no way this process can detect its own lock, so the shared read lock on the new file won't keep this process from deleting it (but will keep other processes from deleting it).

Definition at line 792 of file BESCache3.cc.

References BESDEBUG, BESISDEBUG, cache_too_big(), lock_cache_write(), and unlock_cache().

Referenced by BESUncompressManager3::uncompress().

Here is the call graph for this function:

unsigned long long BESCache3::update_cache_info ( const string &  target)
virtual

Update the cache info file to include 'target'.

Add the size of the named file to the total cache size recorded in the cache info file. The cache info file is exclusively locked by this method for its duration. This updates the cache info file and returns the new size.

Parameters
targetThe name of the file
Returns
The new size of the cache

Definition at line 657 of file BESCache3.cc.

References BESDEBUG, lock_cache_write(), and unlock_cache().

Referenced by BESUncompressManager3::uncompress().

Here is the call graph for this function:


The documentation for this class was generated from the following files: