Cache the results from server functions.
Serve-side functions build new datasets and can be quite large. This code caches those results so that when clients ask for a suite of responses from the function calls, the computations are run only once (in the best case) and subsequent requests for data or metadata are satisfied using information in this case.
- Note
- Cache entry collisions: This cache must hold objects that are identified by the combination of a dataset and a constraint expression. The CE can be quite large and contain a number of 'special' characters like '()' and so on. Instead of building cache IDs using a simple concatenation of the dataset and CE, we use the C++ std::hash class to generate a hash code. However, it's possible that two different dataset/CE combinations will have the same hash values. We use a simple collision resolution system where a suffix is appended to the hash value. After a number of collisions, we give up and simply do not cache the response (providing no worse performance than if the cache did not exist - but currently we throw an exception - see load_from_cache and the constant 'max_collisions').
-
Cache entry format: The cache uses a specially formated 'response object' that is more efficient to read and write than a typical DAP2 or DAP4 response object. DAP2 serializes data using network byte order while the cache uses native machine order. DAP4 computes checksums; the cache does not. In addition, each cache entry contains the resource id as its first line so that the correct entry can be identified.
- Author
- ndp, jhrg
Definition at line 72 of file BESDapFunctionResponseCache.h.
BESDapFunctionResponseCache::BESDapFunctionResponseCache |
( |
const std::string & | cache_dir, |
|
|
const std::string & | prefix, |
|
|
unsigned long long | size ) |
|
inlineprotected |
Protected constructor that takes as arguments keys to the cache directory, file prefix, and size of the cache to be looked up a configuration file.
The keys specified are looked up in the specified keys object. If not found or not set correctly then an exception is thrown. I.E., if the cache directory is empty, the size is zero, or the prefix is empty.
- Parameters
-
cache_dir_key | key to look up in the keys file to find cache dir |
prefix_key | key to look up in the keys file to find the cache prefix |
size_key | key to look up in the keys file to find the cache size (in MBytes) |
- Exceptions
-
BESSyntaxUserError | if keys not set, cache dir or prefix empty, size is 0, or if cache dir does not exist. |
Definition at line 123 of file BESDapFunctionResponseCache.h.
bool BESFileLockingCache::create_and_lock |
( |
const std::string & | target, |
|
|
int & | fd ) |
|
virtualinherited |
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
-
target | The name of the file to make/open/lock |
fd | Value-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
-
BESBESInternalError | if any error except EEXIST is returned by open(2) or if fcntl(2) returns an error. |
Definition at line 625 of file BESFileLockingCache.cc.
void BESFileLockingCache::exclusive_to_shared_lock |
( |
int | fd | ) |
|
|
virtualinherited |
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
-
fd | The file descriptor that is exclusively locked and which, on exit, will have a shared lock. |
Definition at line 660 of file BESFileLockingCache.cc.
DDS * BESDapFunctionResponseCache::get_or_cache_dataset |
( |
libdap::DDS * | dds, |
|
|
const std::string & | constraint ) |
|
virtual |
Return a DDS loaded with data that can be serialized back to a client.
Given a DDS and a DAP2 constraint expression that contains only projection function calls, either pull a cached DDS* that is the result of evaluating those functions, or evaluate, cache and return the result. This is the main API cacll for this class.
- Note
- This method controls the cache lock, ensuring that the cache is unlocked when it returns.
-
The code that evaluates the function expression (when needed) could be sped up by using a thread to handle the process of writing the DDS to the cache, but this will be complicated until we have shared pointers (because the DDS* could be deleted while the cache code is still writing it).
- Parameters
-
- Returns
Definition at line 320 of file BESDapFunctionResponseCache.cc.
void BESFileLockingCache::initialize |
( |
const std::string & | cache_dir, |
|
|
const std::string & | prefix, |
|
|
unsigned long long | size ) |
|
inherited |
Initialize an instance of FileLockingCache.
Initialize and instance of FileLockingCache using the passed values for the cache directory, item prefix and max cache size. This will ignore the value of enable_cache() (but will correctly (re)set it based on the directory, ..., values). This provides a way for clients to re-initialize caches on the fly.
- Parameters
-
cache_dir | The directory into which the cache files will be written. |
prefix | The prefix that will be added to each cache file. |
size | The size of the cache in MBytes |
- Exceptions
-
BESInternalError | If the cache_dir does not exist or is not writable. size is 0, or if cache dir does not exist. |
BESError | If the parameters (directory, ...) are invalid. |
Definition at line 259 of file BESFileLockingCache.cc.
void BESFileLockingCache::purge_file |
( |
const std::string & | file | ) |
|
|
virtualinherited |
Purge a single file from the cache.
Purge a single file from the cache. The file might be old, etc., and need to be removed. Don't use this to shrink the cache when it gets too big, use update_and_purge() instead since that file optimizes accesses to the cache control file for several changes in a row.
- Parameters
-
file | The name of the file to purge. |
Definition at line 1125 of file BESFileLockingCache.cc.
void BESFileLockingCache::unlock_and_close |
( |
const std::string & | target | ) |
|
|
virtualinherited |
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 but might be useful in some settings. Unlock the named file.
This does not do any name mangling; it just closes and unlocks whatever is named (or throws BESBESInternalError if the file cannot be closed). If the file was opened more than once, all descriptors are closed. If you need to close a specific descriptor, use the other version of unlock_and_close().
- Note
- This method assumes that the file was opened/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_name | The name of the file to unlock. |
- Exceptions
-
Definition at line 745 of file BESFileLockingCache.cc.