-
Notifications
You must be signed in to change notification settings - Fork 1
/
extent_client_cache.cc
136 lines (125 loc) · 3.87 KB
/
extent_client_cache.cc
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
#include "extent_client_cache.h"
#include "extent_client.h"
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <slock.h>
#include <jsl_log.h>
extent_client_cache::extent_client_cache(const std::string &dst) : extent_client(dst) {
}
extent_protocol::status extent_client_cache::get(extent_protocol::extentid_t eid,
std::string &buf) {
extent_protocol::status res = extent_protocol::OK;
bool found = true;
{
ScopedLock ml(&mutex_);
jsl_log(JSL_DBG_ME, "extent_client_cache: get %lud %llu\n", pthread_self(), eid);
if (store_.find(eid) == store_.end()) {
found = false;
}
}
Extent copy;
if (!found) {
// TODO(phil) ajouter une opération sur le serveur qui renvoie tout ?
res = extent_client::get(eid, copy.buf);
if (res != extent_protocol::OK) {
return res;
}
res = extent_client::getattr(eid, copy.attr);
if (res != extent_protocol::OK) {
return res;
}
}
// TODO(phil) qu'est ce qui peut arriver quand on n'a pas le verrou ici ?
{
ScopedLock ml(&mutex_);
Extent &v = store_[eid];
if (!found) {
store_[eid] = copy;
}
buf = v.buf;
}
return res;
}
extent_protocol::status extent_client_cache::getattr(extent_protocol::extentid_t eid,
extent_protocol::attr &attr) {
// TODO(phil) factorize this
extent_protocol::status res = extent_protocol::OK;
bool found = true;
{
ScopedLock ml(&mutex_);
jsl_log(JSL_DBG_ME, "extent_client_cache: getattr %lud %llu\n", pthread_self(), eid);
if (store_.find(eid) == store_.end()) {
found = false;
}
}
Extent copy;
if (!found) { // not found locally
// TODO -> ajouter une opération sur le serveur qui renvoie tout ?
res = extent_client::get(eid, copy.buf);
if (res != extent_protocol::OK) {
return res;
}
res = extent_client::getattr(eid, copy.attr);
if (res != extent_protocol::OK) {
return res;
}
}
{
ScopedLock ml(&mutex_);
Extent &v = store_[eid];
if (!found) {
store_[eid] = copy;
}
attr = v.attr;
}
return res;
}
extent_protocol::status extent_client_cache::put(extent_protocol::extentid_t eid,
std::string buf) {
ScopedLock ml(&mutex_);
jsl_log(JSL_DBG_ME, "extent_client_cache: put %lud %llu\n", pthread_self(), eid);
Extent &v = store_[eid];
v.buf = buf;
v.attr.size = buf.size();
v.dirty = true;
time_t cur_time = time(NULL);
// TODO(phil) why change ctime at every access?
v.attr.ctime = cur_time;
v.attr.mtime = cur_time;
return extent_protocol::OK;
}
extent_protocol::status extent_client_cache::remove(
extent_protocol::extentid_t eid) {
ScopedLock ml(&mutex_);
jsl_log(JSL_DBG_ME, "extent_client_cache remove %lud %llu\n", pthread_self(), eid);
if (store_.find(eid) == store_.end()) {
return extent_protocol::NOENT;
}
store_.erase(eid);
return extent_protocol::OK;
}
void extent_client_cache::flush(extent_protocol::extentid_t eid) {
ScopedLock ml(&mutex_);
jsl_log(JSL_DBG_ME, "extent_client_cache: flush %lud %llu %p\n", pthread_self(), eid, this);
extent_protocol::status res = extent_protocol::OK;
if (store_.find(eid) == store_.end()) {
jsl_log(JSL_DBG_ME, "extent_client_cache: flush - remove %llu\n", eid);
res = extent_client::remove(eid);
// TODO pas toujours bon... test-lab-3-b
VERIFY(res == extent_protocol::OK);
return;
}
Extent &v = store_[eid];
if (v.dirty) {
jsl_log(JSL_DBG_ME, "extent_client_cache: flush - write back %llu buf = %s\n ", eid, v.buf.c_str());
// writeback
res = extent_client::put(eid, v.buf);
VERIFY(res == extent_protocol::OK);
}
jsl_log(JSL_DBG_ME, "extent_client_cache: flush - erase %llu\n", eid);
store_.erase(eid);
return;
}