cache.c revision 1a69a1a78cfaa86f3b68bbc965232b7876d4da2a
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 1999, 2000 Internet Software Consortium.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Permission to use, copy, modify, and distribute this software for any
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * purpose with or without fee is hereby granted, provided that the above
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * copyright notice and this permission notice appear in all copies.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* $Id: cache.c,v 1.21 2000/05/08 14:34:26 tale Exp $ */
229ce407c359b0b641759ba1fc4a5fa2054a44daBrian Wellington#define VALID_CACHE(cache) ISC_MAGIC_VALID(cache, CACHE_MAGIC)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * A cache_cleaner_t encapsulsates the state of the periodic
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * cache cleaning.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtontypedef struct cache_cleaner cache_cleaner_t;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtontypedef enum {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cleaner_s_idle, /* Waiting for cleaning-interval to expire. */
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington * Convenience macros for comprehensive assertion checking.
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington#define CLEANER_IDLE(c) ((c)->state == cleaner_s_idle && \
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define CLEANER_BUSY(c) ((c)->state == cleaner_s_busy && \
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int cleaning_interval; /* The cleaning-interval from
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington named.conf, in seconds. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_event_t *resched_event; /* Sent by cleaner task to
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington itself to reschedule */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington clean in one increment */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * The actual cache object.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Unlocked */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /* Locked by 'lock'. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Locked by 'filelock'. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Access to the on-disk cache file is also locked by 'filelock'. */
571688b02f955f6304649866e768b1f81739cbedBrian Wellington *** Functions
571688b02f955f6304649866e768b1f81739cbedBrian Wellington isc_taskmgr_t *taskmgr, isc_timermgr_t *timermgr,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoncleaning_timer_action(isc_task_t *task, isc_event_t *event);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinincremental_cleaning_action(isc_task_t *task, isc_event_t *event);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoncleaner_shutdown_action(isc_task_t *task, isc_event_t *event);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondns_cache_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timermgr_t *timermgr, dns_rdataclass_t rdclass,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington char *db_type, unsigned int db_argc, char **db_argv,
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington "isc_mutex_init() failed: %s",
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = dns_db_create(cache->mctx, db_type, dns_rootname, ISC_TRUE,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = cache_cleaner_init(cache, taskmgr, timermgr,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_event_free(&cache->cleaner.resched_event);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_dbiterator_destroy(&cache->cleaner.iterator);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_free(cache->mctx, cache->filename);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_put(cache->mctx, cache, sizeof *cache);
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellingtondns_cache_attach(dns_cache_t *cache, dns_cache_t **targetp) {
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington REQUIRE(targetp != NULL && *targetp == NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* XXXRTH This is not locked! */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondns_cache_attachdb(dns_cache_t *cache, dns_db_t **dbp) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington/* ARGSUSED */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtondns_cache_setfilename(dns_cache_t *cahce, char *filename) {
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellington isc_mem_free(cache->mctx, cache->filename);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* XXX handle TTLs in a way appropriate for the cache */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_db_load(cache->db, cache->filename);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* XXX to be written */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondns_cache_setcleaninginterval(dns_cache_t *cache, unsigned int t) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (t == 0) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timer_reset(cache->cleaner.cleaning_timer,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timertype_inactive, NULL, NULL, ISC_TRUE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_interval_set(&interval, cache->cleaner.cleaning_interval,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timer_reset(cache->cleaner.cleaning_timer,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Initialize the cache cleaner object at *cleaner.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Space for the object must be allocated by the caller.
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssoncache_cleaner_init(dns_cache_t *cache, isc_taskmgr_t *taskmgr,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_timermgr_t *timermgr, cache_cleaner_t *cleaner)
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (taskmgr != NULL && timermgr != NULL) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson result = isc_task_create(taskmgr, 1, &cleaner->task);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson "isc_task_create() failed: %s",
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_task_setname(cleaner->task, "cachecleaner", cleaner);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson result = isc_task_onshutdown(cleaner->task,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson "cache cleaner: "
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson "isc_task_onshutdown() failed: %s",
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson cleaner->cleaning_interval = 0; /* Initially turned off. */
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson result = isc_timer_create(timermgr, isc_timertype_inactive,
daa73eae708d568d453e6082e0890d35886a9e0fMark Andrews "isc_timer_create() failed: %s",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timer_detach(&cleaner->cleaning_timer);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssonbegin_cleaning(cache_cleaner_t *cleaner) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson * Create an iterator and position it at the beginning of the cache.
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson result = dns_db_createiterator(cleaner->cache->db, ISC_FALSE,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "cache cleaner could not create "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "iterator: %s", isc_result_totext(result));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_dbiterator_first(cleaner->iterator);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson * The database is empty. We are done.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "cache cleaner: dns_dbiterator_first() "
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "begin cache cleaning");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_task_send(cleaner->task, &cleaner->resched_event);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_dbiterator_destroy(&cleaner->iterator);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonend_cleaning(cache_cleaner_t *cleaner, isc_event_t *event) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "end cache cleaning");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_dbiterator_destroy(&cleaner->iterator);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * This is run once for every cache-cleaning-interval as defined in named.conf.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtoncleaning_timer_action(isc_task_t *task, isc_event_t *event) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington INSIST(event->ev_type == ISC_TIMEREVENT_TICK);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "cache cleaner did not finish "
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "in one cleaning-interval");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * Do incremental cleaning.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonincremental_cleaning_action(isc_task_t *task, isc_event_t *event) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson cache_cleaner_t *cleaner = event->ev_arg;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson INSIST(event->ev_type == DNS_EVENT_CACHECLEAN);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson REQUIRE(DNS_DBITERATOR_VALID(cleaner->iterator));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington while (n_names-- > 0) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = dns_dbiterator_current(cleaner->iterator, &node,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson "cache cleaner: dns_dbiterator_current() "
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson "failed: %s", dns_result_totext(result));
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson /* Check TTLs, mark expired rdatasets stale. */
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson result = dns_db_expirenode(cleaner->cache->db, node, now);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "cache cleaner: dns_db_expirenode() "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "failed: %s",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Continue anyway. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* This is where the actual freeing takes place. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_db_detachnode(cleaner->cache->db, &node);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Step to the next node */
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson result = dns_dbiterator_next(cleaner->iterator);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* We have successfully cleaned the whole cache. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "cache cleaner: dns_dbiterator_next() "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* We have successfully performed a cleaning increment. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_dbiterator_pause(cleaner->iterator);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS && result != ISC_R_NOMORE) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "cache cleaner: dns_dbiterator_pause() "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Try to continue. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Still busy, reschedule. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* No longer busy; save the event for later use. */
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington * Do immediate cleaning.
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellingtondns_cache_clean(dns_cache_t *cache, isc_stdtime_t now) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_db_createiterator(cache->db, ISC_FALSE, &iterator);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_dbiterator_current(iterator, &node,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Check TTLs, mark expired rdatasets stale. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_db_expirenode(cache->db, node, now);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "cache cleaner: dns_db_expirenode() "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "failed: %s",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Continue anyway. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* This is where the actual freeing takes place. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * The cleaner task is shutting down; do the necessary cleanup.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoncleaner_shutdown_action(isc_task_t *task, isc_event_t *event) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(event->ev_type == ISC_TASKEVENT_SHUTDOWN);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * By detaching the timer in the context of its task,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * we are guaranteed that there will be no further timer
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timer_detach(&cache->cleaner.cleaning_timer);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Make sure we don't reschedule anymore. */