-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathextent_client.cc
190 lines (148 loc) · 4.06 KB
/
extent_client.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// RPC stubs for clients to talk to extent_server
#include "extent_client.h"
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
// The calls assume that the caller holds a lock on the extent.
extent_client::extent_client(std::string dst)
{
sockaddr_in dstsock;
make_sockaddr(dst.c_str(), &dstsock);
cl = new rpcc(dstsock);
if (cl->bind() != 0) {
printf("extent_client: bind failed\n");
}
}
extent_protocol::status
extent_client::get_impl(extent_protocol::extentid_t eid)
{
extent_protocol::status ret;
std::string buf;
extent_protocol::attr attr;
ret = cl->call(extent_protocol::get, eid, buf);
if (ret != extent_protocol::OK) {
return ret;
}
ret = cl->call(extent_protocol::getattr, eid, attr);
if (ret != extent_protocol::OK) {
return ret;
}
extent_client::extent_t &ext = exts_cache[eid];
ext.ext = buf;
ext.attr = attr;
ext.dirty = false;
ext.removed = false;
return extent_protocol::OK;
}
extent_protocol::status
extent_client::put_impl(extent_protocol::extentid_t eid)
{
extent_protocol::status ret;
std::map<extent_protocol::extentid_t, extent_t>::iterator it;
int r;
it = exts_cache.find(eid);
VERIFY(it != exts_cache.end() && !it->second.removed && it->second.dirty);
ret = cl->call(extent_protocol::put, eid, std::move(it->second.ext), r);
if (ret != extent_protocol::OK) {
return ret;
}
exts_cache.erase(it);
return extent_protocol::OK;
}
extent_protocol::status
extent_client::remove_impl(extent_protocol::extentid_t eid)
{
extent_protocol::status ret;
std::map<extent_protocol::extentid_t, extent_t>::iterator it;
int r;
it = exts_cache.find(eid);
VERIFY(it != exts_cache.end() && it->second.removed);
ret = cl->call(extent_protocol::remove, eid, r);
if (ret != extent_protocol::OK) {
return ret;
}
exts_cache.erase(it);
return extent_protocol::OK;
}
extent_protocol::status
extent_client::get(extent_protocol::extentid_t eid, std::string &buf)
{
std::map<extent_protocol::extentid_t, extent_t>::iterator it;
it = exts_cache.find(eid);
if (it != exts_cache.end()) { // Cache hit
if (it->second.removed) {
return extent_protocol::IOERR;
}
it->second.attr.atime = time_since_epoch();
buf = it->second.ext;
return extent_protocol::OK;
}
extent_protocol::status ret = get_impl(eid);
if (ret != extent_protocol::OK) {
return ret;
}
exts_cache[eid].attr.atime = time_since_epoch();
buf = exts_cache[eid].ext;
return extent_protocol::OK;
}
extent_protocol::status
extent_client::getattr(extent_protocol::extentid_t eid,
extent_protocol::attr &attr)
{
std::map<extent_protocol::extentid_t, extent_t>::iterator it;
it = exts_cache.find(eid);
if (it != exts_cache.end()) { // Cache hit
if (it->second.removed) {
return extent_protocol::IOERR;
}
attr = it->second.attr;
return extent_protocol::OK;
}
extent_protocol::status ret = get_impl(eid);
if (ret != extent_protocol::OK) {
return ret;
}
attr = exts_cache[eid].attr;
return extent_protocol::OK;
}
extent_protocol::status
extent_client::put(extent_protocol::extentid_t eid, std::string buf)
{
extent_t ext;
unsigned int t = time_since_epoch();
ext.attr.size = buf.size();
ext.attr.atime = t;
ext.attr.mtime = t;
ext.attr.ctime = t;
ext.ext = std::move(buf);
ext.dirty = true;
ext.removed = false;
exts_cache[eid] = std::move(ext);
return extent_protocol::OK;
}
extent_protocol::status
extent_client::remove(extent_protocol::extentid_t eid)
{
exts_cache[eid].removed = true;
return extent_protocol::OK;
}
extent_protocol::status
extent_client::flush(extent_protocol::extentid_t eid)
{
printf("flushing extent %lld.\n", eid);
std::map<extent_protocol::extentid_t, extent_t>::iterator it;
it = exts_cache.find(eid);
if (it == exts_cache.end()) {
return extent_protocol::OK;
}
if (it->second.removed) {
return remove_impl(eid);
} else if (it->second.dirty) {
return put_impl(eid);
} else {
exts_cache.erase(it);
}
return extent_protocol::OK;
}