cache: support persist cache when restart smartdns

This commit is contained in:
Nick Peng
2020-09-05 12:55:30 +08:00
parent 2b81fffb7e
commit 0b45da29c7
13 changed files with 447 additions and 80 deletions

View File

@@ -19,7 +19,11 @@
#include "dns_cache.h"
#include "stringutil.h"
#include "tlog.h"
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#define DNS_CACHE_MAX_HITNUM 5000
#define DNS_CACHE_HITNUM_STEP 2
@@ -65,7 +69,7 @@ static __attribute__((unused)) struct dns_cache *_dns_cache_last(void)
return list_last_entry(&dns_cache_head.cache_list, struct dns_cache, list);
}
static struct dns_cache *_dns_cache_first(void)
static struct dns_cache *_dns_inactive_cache_first(void)
{
struct dns_cache *dns_cache = NULL;
@@ -162,7 +166,7 @@ struct dns_cache_data *dns_cache_new_data_addr(uint32_t cache_flag, char *cname,
cache_addr->head.cache_flag = cache_flag;
cache_addr->head.cache_type = CACHE_TYPE_ADDR;
cache_addr->head.size = sizeof(struct dns_cache_addr) - sizeof(struct dns_cache_head);
cache_addr->head.size = sizeof(struct dns_cache_addr) - sizeof(struct dns_cache_data_head);
return (struct dns_cache_data *)cache_addr;
@@ -219,12 +223,12 @@ int dns_cache_replace(char *domain, int ttl, dns_type_t qtype, int speed, struct
/* update cache data */
pthread_mutex_lock(&dns_cache_head.lock);
dns_cache->ttl = ttl;
dns_cache->qtype = qtype;
dns_cache->ttl = ttl;
dns_cache->del_pending = 0;
dns_cache->speed = speed;
time(&dns_cache->insert_time);
dns_cache->info.ttl = ttl;
dns_cache->info.qtype = qtype;
dns_cache->info.ttl = ttl;
dns_cache->info.speed = speed;
time(&dns_cache->info.insert_time);
old_cache_data = dns_cache->cache_data;
dns_cache->cache_data = cache_data;
list_del_init(&dns_cache->list);
@@ -236,21 +240,13 @@ int dns_cache_replace(char *domain, int ttl, dns_type_t qtype, int speed, struct
return 0;
}
int dns_cache_insert(char *domain, int ttl, dns_type_t qtype, int speed, struct dns_cache_data *cache_data)
int _dns_cache_insert(struct dns_cache_info *info, struct dns_cache_data *cache_data, struct list_head *head)
{
uint32_t key = 0;
struct dns_cache *dns_cache = NULL;
if (cache_data == NULL || domain == NULL) {
return -1;
}
if (dns_cache_head.size <= 0) {
return 0;
}
/* if cache already exists, free */
dns_cache = dns_cache_lookup(domain, qtype);
dns_cache = dns_cache_lookup(info->domain, info->qtype);
if (dns_cache) {
dns_cache_delete(dns_cache);
dns_cache_release(dns_cache);
@@ -262,32 +258,22 @@ int dns_cache_insert(char *domain, int ttl, dns_type_t qtype, int speed, struct
goto errout;
}
if (ttl < DNS_CACHE_TTL_MIN) {
ttl = DNS_CACHE_TTL_MIN;
}
memset(dns_cache, 0, sizeof(*dns_cache));
key = hash_string(domain);
key = jhash(&qtype, sizeof(qtype), key);
safe_strncpy(dns_cache->domain, domain, DNS_MAX_CNAME_LEN);
atomic_set(&dns_cache->hitnum, 3);
key = hash_string(info->domain);
key = jhash(&info->qtype, sizeof(info->qtype), key);
atomic_set(&dns_cache->ref, 1);
dns_cache->qtype = qtype;
dns_cache->ttl = ttl;
dns_cache->hitnum_update_add = DNS_CACHE_HITNUM_STEP;
memcpy(&dns_cache->info, info, sizeof(*info));
dns_cache->del_pending = 0;
dns_cache->speed = speed;
dns_cache->cache_data = cache_data;
time(&dns_cache->insert_time);
pthread_mutex_lock(&dns_cache_head.lock);
hash_add(dns_cache_head.cache_hash, &dns_cache->node, key);
list_add_tail(&dns_cache->list, &dns_cache_head.cache_list);
list_add_tail(&dns_cache->list, head);
INIT_LIST_HEAD(&dns_cache->check_list);
/* Release extra cache, remove oldest cache record */
if (atomic_inc_return(&dns_cache_head.num) > dns_cache_head.size) {
struct dns_cache *del_cache;
del_cache = _dns_cache_first();
del_cache = _dns_inactive_cache_first();
if (del_cache) {
_dns_cache_remove(del_cache);
}
@@ -303,6 +289,33 @@ errout:
return -1;
}
int dns_cache_insert(char *domain, int ttl, dns_type_t qtype, int speed, struct dns_cache_data *cache_data)
{
struct dns_cache_info info;
if (cache_data == NULL || domain == NULL) {
return -1;
}
if (dns_cache_head.size <= 0) {
return 0;
}
if (ttl < DNS_CACHE_TTL_MIN) {
ttl = DNS_CACHE_TTL_MIN;
}
info.hitnum = 3;
safe_strncpy(info.domain, domain, DNS_MAX_CNAME_LEN);
info.qtype = qtype;
info.ttl = ttl;
info.hitnum_update_add = DNS_CACHE_HITNUM_STEP;
info.speed = speed;
time(&info.insert_time);
return _dns_cache_insert(&info, cache_data, &dns_cache_head.cache_list);
}
struct dns_cache *dns_cache_lookup(char *domain, dns_type_t qtype)
{
uint32_t key = 0;
@@ -322,11 +335,11 @@ struct dns_cache *dns_cache_lookup(char *domain, dns_type_t qtype)
pthread_mutex_lock(&dns_cache_head.lock);
hash_for_each_possible(dns_cache_head.cache_hash, dns_cache, node, key)
{
if (dns_cache->qtype != qtype) {
if (dns_cache->info.qtype != qtype) {
continue;
}
if (strncmp(domain, dns_cache->domain, DNS_MAX_CNAME_LEN) != 0) {
if (strncmp(domain, dns_cache->info.domain, DNS_MAX_CNAME_LEN) != 0) {
continue;
}
@@ -336,7 +349,7 @@ struct dns_cache *dns_cache_lookup(char *domain, dns_type_t qtype)
if (dns_cache_ret) {
/* Return NULL if the cache times out */
if (dns_cache_head.enable_inactive == 0 && (now - dns_cache_ret->insert_time > dns_cache_ret->ttl)) {
if (dns_cache_head.enable_inactive == 0 && (now - dns_cache_ret->info.insert_time > dns_cache_ret->info.ttl)) {
_dns_cache_remove(dns_cache_ret);
dns_cache_ret = NULL;
} else {
@@ -355,7 +368,7 @@ int dns_cache_get_ttl(struct dns_cache *dns_cache)
int ttl = 0;
time(&now);
ttl = dns_cache->insert_time + dns_cache->ttl - now;
ttl = dns_cache->info.insert_time + dns_cache->info.ttl - now;
if (ttl < 0) {
return 0;
}
@@ -377,15 +390,14 @@ void dns_cache_delete(struct dns_cache *dns_cache)
int dns_cache_hitnum_dec_get(struct dns_cache *dns_cache)
{
int hitnum = 0;
pthread_mutex_lock(&dns_cache_head.lock);
hitnum = atomic_dec_return(&dns_cache->hitnum);
if (dns_cache->hitnum_update_add > DNS_CACHE_HITNUM_STEP) {
dns_cache->hitnum_update_add--;
dns_cache->info.hitnum--;
if (dns_cache->info.hitnum_update_add > DNS_CACHE_HITNUM_STEP) {
dns_cache->info.hitnum_update_add--;
}
pthread_mutex_unlock(&dns_cache_head.lock);
return hitnum;
return dns_cache->info.hitnum;
}
void dns_cache_update(struct dns_cache *dns_cache)
@@ -394,13 +406,13 @@ void dns_cache_update(struct dns_cache *dns_cache)
if (!list_empty(&dns_cache->list)) {
list_del_init(&dns_cache->list);
list_add_tail(&dns_cache->list, &dns_cache_head.cache_list);
atomic_add(dns_cache->hitnum_update_add, &dns_cache->hitnum);
if (atomic_read(&dns_cache->hitnum) > DNS_CACHE_MAX_HITNUM) {
atomic_set(&dns_cache->hitnum, DNS_CACHE_MAX_HITNUM);
dns_cache->info.hitnum += dns_cache->info.hitnum_update_add;
if (dns_cache->info.hitnum > DNS_CACHE_MAX_HITNUM) {
dns_cache->info.hitnum = DNS_CACHE_MAX_HITNUM;
}
if (dns_cache->hitnum_update_add < DNS_CACHE_HITNUM_STEP_MAX) {
dns_cache->hitnum_update_add++;
if (dns_cache->info.hitnum_update_add < DNS_CACHE_HITNUM_STEP_MAX) {
dns_cache->info.hitnum_update_add++;
}
}
pthread_mutex_unlock(&dns_cache_head.lock);
@@ -414,7 +426,7 @@ void _dns_cache_remove_expired_ttl(time_t *now)
list_for_each_entry_safe(dns_cache, tmp, &dns_cache_head.inactive_list, list)
{
ttl = dns_cache->insert_time + dns_cache->ttl - *now;
ttl = dns_cache->info.insert_time + dns_cache->info.ttl - *now;
if (ttl > 0) {
continue;
}
@@ -443,7 +455,7 @@ void dns_cache_invalidate(dns_cache_preinvalid_callback callback, int ttl_pre)
pthread_mutex_lock(&dns_cache_head.lock);
list_for_each_entry_safe(dns_cache, tmp, &dns_cache_head.cache_list, list)
{
ttl = dns_cache->insert_time + dns_cache->ttl - now;
ttl = dns_cache->info.insert_time + dns_cache->info.ttl - now;
if (ttl > 0 && ttl < ttl_pre) {
/* If the TTL time is in the pre-timeout range, call callback function */
if (callback && dns_cache->del_pending == 0) {
@@ -479,6 +491,216 @@ void dns_cache_invalidate(dns_cache_preinvalid_callback callback, int ttl_pre)
}
}
static int _dns_cache_read_record(int fd, uint32_t cache_number)
{
int i = 0;
int ret = 0;
struct dns_cache_record cache_record;
struct dns_cache_data_head data_head;
struct dns_cache_data *cache_data = NULL;
struct list_head *head = NULL;
for (i = 0; i < cache_number; i++) {
ret = read(fd, &cache_record, sizeof(cache_record));
if (ret != sizeof(cache_record)) {
tlog(TLOG_ERROR, "read cache failed, %s", strerror(errno));
goto errout;
}
if (cache_record.magic != MAGIC_CACHE_DATA) {
tlog(TLOG_ERROR, "magic is invalid.");
goto errout;
}
if (cache_record.type == CACHE_RECORD_TYPE_ACTIVE) {
head = &dns_cache_head.cache_list;
} else {
head = &dns_cache_head.inactive_list;
}
ret = read(fd, &data_head, sizeof(data_head));
if (ret != sizeof(data_head)) {
tlog(TLOG_ERROR, "read data head failed, %s", strerror(errno));
goto errout;
}
if (data_head.size > 1024 * 8) {
tlog(TLOG_ERROR, "data may invalid, skip load cache.");
goto errout;
}
cache_data = malloc(data_head.size + sizeof(data_head));
if (cache_data == NULL) {
tlog(TLOG_ERROR, "malloc cache data failed %s", strerror(errno));
goto errout;
}
memcpy(&cache_data->head, &data_head, sizeof(data_head));
ret = read(fd, cache_data->data, data_head.size);
if (ret != data_head.size) {
tlog(TLOG_ERROR, "read cache data failed, %s", strerror(errno));
goto errout;
}
if (_dns_cache_insert(&cache_record.info, cache_data, head) != 0) {
tlog(TLOG_ERROR, "insert cache data failed.");
cache_data = NULL;
goto errout;
}
cache_data = NULL;
}
return 0;
errout:
if (cache_data) {
free(cache_data);
}
return -1;
}
int dns_cache_load(const char *file)
{
int fd = -1;
int ret = 0;
fd = open(file, O_RDONLY);
if (fd < 0) {
return 0;
}
struct dns_cache_file cache_file;
ret = read(fd, &cache_file, sizeof(cache_file));
if (ret != sizeof(cache_file)) {
tlog(TLOG_ERROR, "read cache head failed.");
goto errout;
}
if (cache_file.magic != MAGIC_NUMBER) {
tlog(TLOG_ERROR, "cache file is invalid.");
goto errout;
}
if (strncmp(cache_file.version, __TIMESTAMP__, DNS_CACHE_VERSION_LEN) != 0) {
tlog(TLOG_WARN, "cache version is different, skip load cache.");
goto errout;
}
if (_dns_cache_read_record(fd, cache_file.cache_number) != 0) {
goto errout;
}
close(fd);
return 0;
errout:
if (fd > 0) {
close(fd);
}
return -1;
}
static int _dns_cache_write_record(int fd, uint32_t *cache_number, enum CACHE_RECORD_TYPE type, struct list_head *head)
{
struct dns_cache *dns_cache = NULL;
struct dns_cache *tmp = NULL;
struct dns_cache_record cache_record;
pthread_mutex_lock(&dns_cache_head.lock);
list_for_each_entry_safe_reverse(dns_cache, tmp, head, list)
{
cache_record.magic = MAGIC_CACHE_DATA;
cache_record.type = type;
memcpy(&cache_record.info, &dns_cache->info, sizeof(struct dns_cache_info));
int ret = write(fd, &cache_record, sizeof(cache_record));
if (ret != sizeof(cache_record)) {
tlog(TLOG_ERROR, "write cache failed, %s", strerror(errno));
goto errout;
}
struct dns_cache_data *cache_data = dns_cache->cache_data;
ret = write(fd, cache_data, sizeof(*cache_data) + cache_data->head.size);
if (ret != sizeof(*cache_data) + cache_data->head.size) {
tlog(TLOG_ERROR, "write cache data failed, %s", strerror(errno));
goto errout;
}
(*cache_number)++;
}
pthread_mutex_unlock(&dns_cache_head.lock);
return 0;
errout:
pthread_mutex_unlock(&dns_cache_head.lock);
return -1;
}
static int _dns_cache_write_records(int fd, uint32_t *cache_number)
{
if (_dns_cache_write_record(fd, cache_number, CACHE_RECORD_TYPE_ACTIVE, &dns_cache_head.cache_list) != 0) {
return -1;
}
if (_dns_cache_write_record(fd, cache_number, CACHE_RECORD_TYPE_INACTIVE, &dns_cache_head.inactive_list) != 0) {
return -1;
}
return 0;
}
int dns_cache_save(const char *file)
{
int fd = -1;
uint32_t cache_number = 0;
tlog(TLOG_DEBUG, "write cache file %s", file);
fd = open(file, O_TRUNC | O_CREAT | O_WRONLY | O_CLOEXEC, 0640);
if (fd < 0) {
tlog(TLOG_ERROR, "create file %s failed, %s", file, strerror(errno));
goto errout;
}
struct dns_cache_file cache_file;
memset(&cache_file, 0, sizeof(cache_file));
cache_file.magic = MAGIC_NUMBER;
safe_strncpy(cache_file.version, __TIMESTAMP__, DNS_CACHE_VERSION_LEN);
cache_file.cache_number = 0;
if (lseek(fd, sizeof(cache_file), SEEK_SET) < 0) {
tlog(TLOG_ERROR, "seek file %s failed, %s", file, strerror(errno));
goto errout;
}
if (_dns_cache_write_records(fd, &cache_number) != 0) {
tlog(TLOG_ERROR, "write record to file %s failed.", file);
goto errout;
}
if (lseek(fd, 0, SEEK_SET) < 0) {
tlog(TLOG_ERROR, "seek file %s failed, %s", file, strerror(errno));
goto errout;
}
cache_file.cache_number = cache_number;
if (write(fd, &cache_file, sizeof(cache_file)) != sizeof(cache_file)) {
tlog(TLOG_ERROR, "write file head %s failed, %s, %d", file, strerror(errno), fd);
goto errout;
}
tlog(TLOG_DEBUG, "wrote total %d records.", cache_number);
close(fd);
return 0;
errout:
if (fd > 0) {
close(fd);
}
return -1;
}
void dns_cache_destroy(void)
{
struct dns_cache *dns_cache = NULL;