-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextent_client.cc
126 lines (107 loc) · 3.12 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
// 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(extent_protocol::extentid_t eid, std::string &buf)
{
extent_protocol::status ret = extent_protocol::OK;
if(cache.find(eid) != cache.end()){
if(cache[eid].removed)
return extent_protocol::NOENT;
else if(cache[eid].content_cached){
buf = cache[eid].content;
cache[eid].a.atime = time(0);
printf("buffered content eid=>%16llu, buf=>%s\n", eid, buf.c_str());
return ret;
}
}
ret = cl->call(extent_protocol::get, eid, buf);
printf("get conent from server buf=>%s\n", buf.c_str());
if(ret == extent_protocol::OK){
printf("cache content in the get eid=>%16llu\n", eid);
cache[eid].content = buf;
cache[eid].content_cached = true;
cache[eid].a.atime = time(0);
}
return ret;
}
extent_protocol::status
extent_client::getattr(extent_protocol::extentid_t eid,
extent_protocol::attr &attr)
{
extent_protocol::status ret = extent_protocol::OK;
if(cache.find(eid) != cache.end()){
if(cache[eid].removed)
return extent_protocol::NOENT;
attr = cache[eid].a;
return ret;
}
ret = cl->call(extent_protocol::getattr, eid, attr);
if(ret == extent_protocol::OK){
printf("cache attr in the getattr eid=>%16llu\n", eid);
cache[eid].a = attr;
}
return ret;
}
extent_protocol::status
extent_client::put(extent_protocol::extentid_t eid, std::string buf)
{
extent_protocol::status ret = extent_protocol::OK;
cache[eid].removed = false;
cache[eid].dirty = true;
cache[eid].content_cached = true;
cache[eid].content = buf;
cache[eid].a.size = buf.size();
cache[eid].a.ctime = cache[eid].a.mtime = time(0);
printf("put content into buffer eid=>%16llu, buf=>%s\n", eid, buf.c_str());
// TODO change attr?
return ret;
/*
int r;
ret = cl->call(extent_protocol::put, eid, buf, r);
return ret;
*/
}
extent_protocol::status
extent_client::remove(extent_protocol::extentid_t eid)
{
extent_protocol::status ret = extent_protocol::OK;
cache[eid].removed = true;
return ret;
/*
int r;
ret = cl->call(extent_protocol::remove, eid, r);
return ret;
*/
}
void
extent_client::flush(extent_protocol::extentid_t eid){
printf("extent client flush eid=>%16llu\n", eid);
if(cache.find(eid) == cache.end())
return;
if(cache[eid].removed){
int r;
printf("extent client flush removed eid=>%16llu\n", eid);
cl->call(extent_protocol::remove, eid, r);
}else if(cache[eid].dirty){
int r;
printf("extent client flush buf=>%s\n", cache[eid].content.c_str());
extent_protocol::status ret = cl->call(extent_protocol::put, eid, cache[eid].content, r);
printf("extent client flush return ret=>%d\n", ret);
}
cache.erase(eid);
}