-
Notifications
You must be signed in to change notification settings - Fork 0
/
extent_client.cc
188 lines (156 loc) · 4.41 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
// 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_data::extent_data()
:dirty(false),removed(false),extent_cached(false),buf()
{
}
extent_client::extent_client(std::string dst)
:extent_map(),_extent_mutex()
{
sockaddr_in dstsock;
make_sockaddr(dst.c_str(), &dstsock);
cl = new rpcc(dstsock);
if (cl->bind() != 0) {
printf("extent_client: bind failed\n");
}
}
extent_client::~extent_client() {
delete cl;
}
extent_protocol::status
extent_client::get(extent_protocol::extentid_t eid, std::string &buf)
{
ScopedLock sl(&_extent_mutex);
extent_protocol::status ret = extent_protocol::OK;
time_t now = time(0);
Extent_Map::iterator iter = extent_map.find(eid);
if (iter == extent_map.end()) {
ret = cl->call(extent_protocol::get, eid, buf);
if (ret == extent_protocol::NOENT) {
return ret;
}
extent_data newextent;
ret = cl->call(extent_protocol::getattr, eid, newextent.attr);
newextent.attr.size = buf.size();
newextent.buf = buf;
newextent.extent_cached = true;
newextent.attr.mtime = now;
newextent.attr.ctime = now;
extent_map.insert(std::make_pair(eid,newextent));
return ret;
} else if (iter->second.removed) {
ret = extent_protocol::NOENT;
return ret;
} else if (!iter->second.extent_cached) {
ret = cl->call(extent_protocol::get, eid, buf);
iter->second.extent_cached = true;
iter->second.buf = buf;
if (ret == extent_protocol::NOENT) {
return ret;
}
} else {
buf = iter->second.buf;
}
iter->second.attr.mtime = now;
iter->second.attr.ctime = now;
iter->second.attr.size = buf.size();
return ret;
}
extent_protocol::status
extent_client::getattr(extent_protocol::extentid_t eid,
extent_protocol::attr &attr)
{
ScopedLock sl(&_extent_mutex);
extent_protocol::status ret = extent_protocol::OK;
Extent_Map::iterator iter = extent_map.find(eid);
if (iter == extent_map.end()) {
cl->call(extent_protocol::getattr, eid, attr);
if (ret == extent_protocol::NOENT) {
return ret;
}
extent_data newextent;
newextent.attr = attr;
extent_map.insert(std::make_pair(eid,newextent));
return ret;
}
if (iter->second.removed) {
ret = extent_protocol::NOENT;
return ret;
}
else {
attr = iter->second.attr;
return ret;
}
}
extent_protocol::status
extent_client::put(extent_protocol::extentid_t eid, std::string buf)
{
ScopedLock sl(&_extent_mutex);
extent_protocol::status ret = extent_protocol::OK;
time_t now = time(0);
Extent_Map::iterator iter = extent_map.find(eid);
if (iter == extent_map.end()) {
extent_data newextent;
newextent.buf = buf;
newextent.attr.atime = now;
newextent.attr.mtime = now;
newextent.attr.ctime = now;
newextent.attr.size = buf.size();
newextent.dirty = true;
newextent.extent_cached = true;
extent_map.insert(std::make_pair(eid,newextent));
} else if (iter->second.removed)
{
ret = extent_protocol::NOENT;
return ret;
} else {
iter->second.extent_cached = true;
iter->second.buf = buf;
iter->second.attr.mtime = now;
iter->second.attr.ctime = now;
iter->second.attr.size = buf.size();
iter->second.dirty = true;
}
return ret;
}
extent_protocol::status
extent_client::remove(extent_protocol::extentid_t eid)
{
ScopedLock sl(&_extent_mutex);
extent_protocol::status ret = extent_protocol::OK;
Extent_Map::iterator iter = extent_map.find(eid);
if (iter == extent_map.end()) {
extent_data newextent;
newextent.removed = true;
newextent.dirty = true;
iter->second.extent_cached=true;
extent_map.insert(std::make_pair(eid,newextent));
} else {
iter->second.removed = true;
iter->second.extent_cached=true;
iter->second.dirty = true;
}
return ret;
}
extent_protocol::status extent_client::flush(extent_protocol::extentid_t eid)
{
ScopedLock sl(&_extent_mutex);
extent_protocol::status ret = extent_protocol::OK;
int r;
Extent_Map::iterator iter = extent_map.find(eid);
if (iter->second.dirty) {
if (iter->second.removed) {
ret = cl->call(extent_protocol::remove, eid,r);
} else {
ret = cl->call(extent_protocol::put, eid, iter->second.buf, r);
}
}
extent_map.erase(iter);
return ret;
}