forked from alibaba/nginx-tfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_tfs_block_cache.c
152 lines (118 loc) · 4.6 KB
/
ngx_http_tfs_block_cache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (C) 2010-2012 Alibaba Group Holding Limited
*/
#include <ngx_http_tfs.h>
#include <ngx_http_tfs_block_cache.h>
#include <ngx_http_tfs_local_block_cache.h>
#include <ngx_http_tfs_remote_block_cache.h>
ngx_int_t
ngx_http_tfs_block_cache_lookup(ngx_http_tfs_block_cache_ctx_t *ctx,
ngx_pool_t *pool, ngx_log_t *log, ngx_http_tfs_block_cache_key_t *key,
ngx_http_tfs_block_cache_value_t *value)
{
ngx_int_t rc = NGX_DECLINED;
if (ctx->curr_lookup_cache == NGX_HTTP_TFS_LOCAL_BLOCK_CACHE) {
ctx->curr_lookup_cache = NGX_HTTP_TFS_REMOTE_BLOCK_CACHE;
if (ctx->use_cache & NGX_HTTP_TFS_LOCAL_BLOCK_CACHE) {
rc = ngx_http_tfs_local_block_cache_lookup(ctx->local_ctx, pool, log, key, value);
if (rc == NGX_OK) {
return rc;
}
}
}
if (ctx->curr_lookup_cache == NGX_HTTP_TFS_REMOTE_BLOCK_CACHE) {
ctx->curr_lookup_cache = NGX_HTTP_TFS_NO_BLOCK_CACHE;
if (ctx->use_cache & NGX_HTTP_TFS_REMOTE_BLOCK_CACHE) {
rc = ngx_http_tfs_remote_block_cache_lookup(&ctx->remote_ctx, pool, log, key);
}
}
return rc;
}
void
ngx_http_tfs_block_cache_insert(ngx_http_tfs_block_cache_ctx_t *ctx,
ngx_pool_t *pool, ngx_log_t *log, ngx_http_tfs_block_cache_key_t *key,
ngx_http_tfs_block_cache_value_t *value)
{
if (ctx->use_cache & NGX_HTTP_TFS_REMOTE_BLOCK_CACHE) {
ngx_http_tfs_remote_block_cache_insert(&ctx->remote_ctx, pool, log, key, value);
}
if (ctx->use_cache & NGX_HTTP_TFS_LOCAL_BLOCK_CACHE) {
ngx_http_tfs_local_block_cache_insert(ctx->local_ctx, log, key, value);
}
}
void
ngx_http_tfs_block_cache_remove(ngx_http_tfs_block_cache_ctx_t *ctx,
ngx_pool_t *pool, ngx_log_t *log, ngx_http_tfs_block_cache_key_t* key, uint8_t hit_status)
{
if (hit_status != NGX_HTTP_TFS_NO_BLOCK_CACHE
&& (ctx->use_cache & NGX_HTTP_TFS_LOCAL_BLOCK_CACHE))
{
ngx_http_tfs_local_block_cache_remove(ctx->local_ctx, log, key);
}
if (hit_status == NGX_HTTP_TFS_REMOTE_BLOCK_CACHE) {
ngx_http_tfs_remote_block_cache_remove(&ctx->remote_ctx, pool, log, key);
}
}
ngx_int_t
ngx_http_tfs_block_cache_batch_lookup(ngx_http_tfs_block_cache_ctx_t *ctx,
ngx_pool_t *pool, ngx_log_t *log, ngx_array_t *keys,
ngx_array_t *kvs)
{
uint32_t i;
ngx_int_t rc;
ngx_uint_t local_miss_count;
ngx_array_t local_miss_keys;
ngx_http_tfs_t *t;
ngx_http_tfs_segment_data_t *segment_data;
ngx_http_tfs_block_cache_key_t *key;
rc = NGX_DECLINED;
if (ctx->curr_lookup_cache == NGX_HTTP_TFS_LOCAL_BLOCK_CACHE) {
ctx->curr_lookup_cache = NGX_HTTP_TFS_REMOTE_BLOCK_CACHE;
if (ctx->use_cache & NGX_HTTP_TFS_LOCAL_BLOCK_CACHE) {
rc = ngx_http_tfs_local_block_cache_batch_lookup(ctx->local_ctx, pool, log, keys, kvs);
if (rc == NGX_OK || rc == NGX_ERROR) {
return rc;
}
}
}
/* rc == NGX_DECLIEND */
if (ctx->curr_lookup_cache == NGX_HTTP_TFS_REMOTE_BLOCK_CACHE) {
ctx->curr_lookup_cache = NGX_HTTP_TFS_NO_BLOCK_CACHE;
if (ctx->use_cache & NGX_HTTP_TFS_REMOTE_BLOCK_CACHE) {
t = ctx->remote_ctx.data;
local_miss_count = keys->nelts - kvs->nelts;
rc = ngx_array_init(&local_miss_keys, t->pool, local_miss_count, sizeof(ngx_http_tfs_block_cache_key_t));
if (rc == NGX_ERROR) {
return rc;
}
segment_data = &t->file.segment_data[t->file.segment_index];
for (i = 0; i < keys->nelts; i++, segment_data++) {
if (segment_data->cache_hit == NGX_HTTP_TFS_NO_BLOCK_CACHE) {
key = (ngx_http_tfs_block_cache_key_t *) ngx_array_push(&local_miss_keys);
key->ns_addr = *((uint64_t*)(&t->name_server_addr));
key->block_id = segment_data->segment_info.block_id;
}
}
rc = ngx_http_tfs_remote_block_cache_batch_lookup(&ctx->remote_ctx, pool, log, &local_miss_keys);
}
}
return rc;
}
ngx_int_t
ngx_http_tfs_block_cache_cmp(ngx_http_tfs_block_cache_key_t *left,
ngx_http_tfs_block_cache_key_t *right)
{
if (left->ns_addr == right->ns_addr) {
if (left->block_id == right->block_id) {
return 0;
}
if (left->block_id < right->block_id) {
return -1;
}
return 1;
}
if (left->ns_addr < right->ns_addr) {
return -1;
}
return 1;
}