Skip to content

Commit

Permalink
Add use-stale
Browse files Browse the repository at this point in the history
(cherry picked from commit ef63e5e)
  • Loading branch information
jiangwenyuan committed May 19, 2020
1 parent 125ec66 commit 0d103da
Show file tree
Hide file tree
Showing 15 changed files with 385 additions and 167 deletions.
12 changes: 11 additions & 1 deletion README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ dict.nosql.used: 0

**syntax:**

*nuster rule name [key KEY] [ttl TTL] [extend EXTEND] [wait on|off|TIME] [code CODE] [memory on|off] [disk on|off|sync] [etag on|off] [last-modified on|off] [if|unless condition]*
*nuster rule name [key KEY] [ttl TTL] [extend EXTEND] [wait on|off|TIME] [use-stale on|off|TIME] [code CODE] [memory on|off] [disk on|off|sync] [etag on|off] [last-modified on|off] [if|unless condition]*

**default:** *none*

Expand Down Expand Up @@ -482,6 +482,16 @@ percentage: |<- (100 - n1 - n2 - n3)% ->|<- n1% ->|<- n2% ->|<- n3% ->|<- n4% ->

> nosql模式下不会等待,相同的请求将被依次处理,最后一个请求的内容将被保存。
### use-stale on|off|TIME [cache only]

决定是否在更新缓存时是否使用过期的缓存,以及在后端宕机时是否使用过期缓存。

当use-stale on时,在更新缓存时使用过期缓存。

当use-stale off时,如果`wait off` 那么相同的请求将被传递到后端服务器否则等待。

`use-stale TIME`则允许在因后端服务器宕机而导致更新缓存失败后继续使用缓存TIME秒。

### code CODE1,CODE2...

默认只缓存200的响应,如果需要缓存其他的则可以添加,`all`会缓存任何状态码。
Expand Down
12 changes: 11 additions & 1 deletion README-JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ cache/nosqlの有効無効を決める。

**syntax:**

*nuster rule name [key KEY] [ttl TTL] [extend EXTEND] [wait on|off|TIME] [code CODE] [memory on|off] [disk on|off|sync] [etag on|off] [last-modified on|off] [if|unless condition]*
*nuster rule name [key KEY] [ttl TTL] [extend EXTEND] [wait on|off|TIME] [use-stale on|off|TIME] [code CODE] [memory on|off] [disk on|off|sync] [etag on|off] [last-modified on|off] [if|unless condition]*

**default:** *none*

Expand Down Expand Up @@ -484,6 +484,16 @@ percentage: |<- (100 - n1 - n2 - n3)% ->|<- n1% ->|<- n2% ->|<- n3% ->|<- n4% ->

> Nosqlモードではwaitしない。順番に処理して最後のリクエストの内容を保存する。
### use-stale on|off|TIME [cache only]

キャッシュが更新されているときや、バックエンドのサーバーダウンで更新失敗した時に、失効済みのキャッシュを使うかどうかを決める。

`use-stale on`: キャッシュが更新されている時、失効済みのキャッシュを使う。

`use-stale off`(ディフォルト): `wait off`の場合、同じなリクエストがバックエンドにフォーワードする, `wait on|TIME` の場合は待つ。

`use-stale TIME`: バックエンドのサーバーダウンで更新失敗した時に、失効済みのキャッシュをTIME秒間を使う。

### code CODE1,CODE2...

ディフォルトは200のリスポンスしかキャッシュしない、ほかのものをキャッシュしたい場合は
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ If there are filters on this proxy, put this directive after all other filters.

**syntax:**

*nuster rule name [key KEY] [ttl TTL] [extend EXTEND] [wait on|off|TIME] [code CODE] [memory on|off] [disk on|off|sync] [etag on|off] [last-modified on|off] [if|unless condition]*
*nuster rule name [key KEY] [ttl TTL] [extend EXTEND] [wait on|off|TIME] [use-stale on|off|TIME] [code CODE] [memory on|off] [disk on|off|sync] [etag on|off] [last-modified on|off] [if|unless condition]*

**default:** *none*

Expand Down Expand Up @@ -499,6 +499,16 @@ Note that other identical requests will not wait until the first request finishe

> In nosql mode, there is no wait mode. Multiple identical POST requests are served in the order it was received, and the body of the last request will be saved as the content.
### use-stale on|off|TIME [cache only]

Determines whether or not to serve stale cache to clients if it is being updated or the backend server is down.

When use-stale is on, the stale cache will be used to serve clients.

When use-stale is off, which is the default mode, same requests will be passed to the backend when the cache is being updated if `wait off` is set, otherwise wait if `wait on|TIME` is set.

`use-stale TIME` permits using the stale cache to serve clients for TIME seconds if the cache cannot be updated due to backend error.

### code CODE1,CODE2...

Cache only if the response status code is CODE.
Expand Down
8 changes: 8 additions & 0 deletions include/nuster/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ typedef struct nst_rule_config {
int last_modified; /* last_modified on|off */
int wait; /* -1: not wait, 0: wait forever, > 0, wait seconds */

/*
* -1: do not use stale
* 0: use stale while updating
* > 0: keep for N seconds if update failed and use stale
*/
int stale;

/*
* auto ttl extend
* ctime expire
Expand Down Expand Up @@ -216,6 +223,7 @@ typedef struct nst_rule_prop {
int last_modified; /* last_modified on|off */
uint8_t extend[4];
int wait;
int stale;
int status_code;
} nst_rule_prop_t;

Expand Down
2 changes: 1 addition & 1 deletion include/nuster/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ typedef struct nst_ctx {

int rule_cnt;
int key_cnt;
hpx_buffer_t *key;
nst_rule_t *rule;
nst_key_t *key;
nst_key_t keys[0];
} nst_ctx_t;

Expand Down
14 changes: 13 additions & 1 deletion include/nuster/dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@

enum {
NST_DICT_ENTRY_STATE_INIT = 0,
NST_DICT_ENTRY_STATE_UPDATE,
NST_DICT_ENTRY_STATE_VALID,
NST_DICT_ENTRY_STATE_REFRESH,
NST_DICT_ENTRY_STATE_UPDATE,
NST_DICT_ENTRY_STATE_STALE,
NST_DICT_ENTRY_STATE_INVALID,
};

Expand Down Expand Up @@ -109,6 +111,11 @@ nst_dict_entry_expired(nst_dict_entry_t *entry) {

}

static inline int
nst_dict_entry_stale_valid(nst_dict_entry_t *entry) {
return entry->expire + entry->prop.stale > get_current_timestamp() / 1000;
}

static inline int
nst_dict_entry_invalid(nst_dict_entry_t *entry) {

Expand All @@ -122,6 +129,11 @@ nst_dict_entry_invalid(nst_dict_entry_t *entry) {
return nst_dict_entry_expired(entry);
}

/* check stale */
if(entry->state == NST_DICT_ENTRY_STATE_STALE) {
return !nst_dict_entry_stale_valid(entry);
}

return 0;
}

Expand Down
64 changes: 47 additions & 17 deletions include/nuster/disk.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <nuster/key.h>


#define NST_DISK_VERSION 5
#define NST_DISK_VERSION 6

/*
Offset Length(bytes) Content
Expand All @@ -44,7 +44,8 @@
8 * 10 8 etag, on|off: 4, length: 4
8 * 11 8 last-modified: on|off: 4, length: 4
8 * 12 8 ttl: 4, extend: 4
8 * 13 24 reserved
8 * 13 4 stale: 4,
8 * 13 + 4 20 reserved
NST_DISK_META_SIZE key_len key
+ key_len proxy_len proxy
+ proxy_len rule_len rule
Expand Down Expand Up @@ -72,6 +73,7 @@
#define NST_DISK_META_POS_LAST_MODIFIED_PROP 8 * 11
#define NST_DISK_META_POS_LAST_MODIFIED_LEN 8 * 11 + 4
#define NST_DISK_META_POS_TTL_EXTEND 8 * 12
#define NST_DISK_META_POS_STALE 8 * 13

#define NST_DISK_META_SIZE 8 * 16
#define NST_DISK_POS_KEY NST_DISK_META_SIZE
Expand Down Expand Up @@ -178,21 +180,6 @@ nst_disk_meta_get_expire(char *p) {
return *(uint64_t *)(p + NST_DISK_META_POS_EXPIRE);
}

static inline int
nst_disk_meta_check_expire(char *p) {
uint64_t expire = *(uint64_t *)(p + NST_DISK_META_POS_EXPIRE);

if(expire == 0) {
return NST_OK;
}

if(expire * 1000 > get_current_timestamp()) {
return NST_OK;
} else {
return NST_ERR;
}
}

static inline void
nst_disk_meta_set_header_len(char *p, uint64_t v) {
*(uint64_t *)(p + NST_DISK_META_POS_HEADER_LEN) = v;
Expand Down Expand Up @@ -303,6 +290,49 @@ nst_disk_meta_get_ttl_extend(char *p) {
return *(uint64_t *)(p + NST_DISK_META_POS_TTL_EXTEND);
}

static inline void
nst_disk_meta_set_stale(char *p, int32_t v) {
*(int32_t *)(p + NST_DISK_META_POS_STALE) = v;
}

static inline int32_t
nst_disk_meta_get_stale(char *p) {
return *(int32_t *)(p + NST_DISK_META_POS_STALE);
}

static inline int
nst_disk_meta_check_expire(char *p) {
uint64_t expire = nst_disk_meta_get_expire(p);
uint64_t now = get_current_timestamp();

if(expire == 0) {
return NST_OK;
}

if(expire * 1000 > now) {
return NST_OK;
}

return NST_ERR;
}

static inline int
nst_disk_meta_check_stale(char *p) {
uint64_t expire = nst_disk_meta_get_expire(p);
uint64_t now = get_current_timestamp();
int32_t stale = nst_disk_meta_get_stale(p);

if(stale == 0) {
return NST_OK;
}

if(expire + stale > now / 1000) {
return NST_OK;
}

return NST_ERR;
}

static inline int
nst_disk_pos_proxy(nst_disk_data_t *data) {
return NST_DISK_POS_KEY + nst_disk_meta_get_key_len(data->meta);
Expand Down
Loading

0 comments on commit 0d103da

Please sign in to comment.