-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSessionLocalFile.h
More file actions
285 lines (227 loc) · 7.74 KB
/
Copy pathSessionLocalFile.h
File metadata and controls
285 lines (227 loc) · 7.74 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#pragma once
#include <common/Common.h>
#include <common/nodes/Node.h>
#include <common/storage/Path.h>
#include <common/storage/PathInfo.h>
#include <common/storage/quota/QuotaData.h>
#include <common/threading/Mutex.h>
#include <common/toolkit/FDHandle.h>
#include <atomic>
#define RW_HINT_INVALID 0xFF
/**
* Represents the client session information for an open chunk file.
*/
class SessionLocalFile
{
public:
class Handle
{
friend class SessionLocalFile;
public:
Handle() = default;
Handle(const std::string& id, FDHandle fd):
id(id), fd(std::move(fd)), claimed(0)
{
}
bool close();
const FDHandle& getFD() const { return fd; }
const std::string& getID() const { return id; }
private:
std::string id;
FDHandle fd;
// for use by SessionLocalFile::releaseLastReference. only one caller may receive the
// handle if multiple threads try to release the last reference concurrently. we could
// also do this under a lock in SessionLocalFileStore but don't since we don't expect
// contention on the release path.
std::atomic<bool> claimed;
};
public:
/**
* @param fileHandleID format: <ownerID>#<fileID>
* @param openFlags system flags for open()
* @param serverCrashed true if session was created after a server crash, mark session as
* dirty
*/
SessionLocalFile(const std::string& fileHandleID, uint16_t targetID, std::string fileID,
int openFlags, bool serverCrashed) :
handle(std::make_shared<Handle>(fileHandleID, FDHandle())), targetID(targetID),
fileID(fileID)
{
this->openFlags = openFlags;
this->offset = -1; // initialize as invalid offset (will be set on file open)
this->isMirrorSession = false;
this->writeCounter = 0;
this->readCounter = 0;
this->lastReadAheadTrigger = 0;
this->serverCrashed = serverCrashed;
}
/**
* For dezerialisation only
*/
SessionLocalFile():
handle(std::make_shared<Handle>())
{
this->offset = -1; // initialize as invalid offset (will be set on file open)
}
template<typename This, typename Ctx>
static void serialize(This obj, Ctx& ctx)
{
ctx
% obj->handle->id
% obj->targetID
% obj->fileID
% obj->openFlags
% obj->offset
% obj->isMirrorSession
% obj->serverCrashed;
serializeNodeID(obj, ctx);
ctx
% serdes::atomicAs<int64_t>(obj->writeCounter)
% serdes::atomicAs<int64_t>(obj->readCounter)
% serdes::atomicAs<int64_t>(obj->lastReadAheadTrigger);
}
FhgfsOpsErr openFile(int targetFD, const PathInfo* pathInfo, bool isWriteOpen,
const SessionQuotaInfo* quotaInfo, uint64_t writeHint = RW_HINT_INVALID);
NodeHandle setMirrorNodeExclusive(NodeHandle mirrorNode);
private:
static void serializeNodeID(const SessionLocalFile* obj, Serializer& ser)
{
if (!obj->mirrorNode)
ser % uint16_t(0);
else
ser % uint16_t(obj->mirrorNode->getNumID().val());
}
static void serializeNodeID(SessionLocalFile* obj, Deserializer& des);
private:
// holds information about the underlying filesystem state this session file refers to for
// clients. this handle may be referenced by outsiders to separate removal of session files
// from their respective stores and closing of underlying fs objects. the protocol for such
// a close operation should be as follows:
// 1. remove the session file from its store, retain a shared_ptr
// 2. copy this handle to a temporary
// 3. move the session file reference to a weak_ptr. if the weak_ptr is expired after the
// move, the handle may be closed
// 4. claim the handle, and close() the handle if claiming succeeded
std::shared_ptr<Handle> handle;
uint16_t targetID;
std::string fileID;
int32_t openFlags; // system flags for open()
int64_t offset; // negative value for unspecified/invalid offset
NodeHandle mirrorNode; // the node to which all writes should be mirrored
bool isMirrorSession; // true if this is the mirror session of a file
AtomicInt64 writeCounter; // how much sequential data we have written after open/sync_file_range
AtomicInt64 readCounter; // how much sequential data we have read since open / last seek
AtomicInt64 lastReadAheadTrigger; // last readCounter value at which read-ahead was triggered
Mutex sessionMutex;
bool serverCrashed; // true if session was created after a server crash, mark session as dirty
static std::shared_ptr<Handle> releaseLastReference(std::shared_ptr<SessionLocalFile> file)
{
auto handle = file->handle;
std::weak_ptr<SessionLocalFile> weak(file);
// see Handle::claimed for explanation
file.reset();
if (weak.expired() && !handle->claimed.exchange(true))
return handle;
else
return nullptr;
}
public:
bool close()
{
std::lock_guard<Mutex> lock(sessionMutex);
return handle->close();
}
friend std::shared_ptr<Handle> releaseLastReference(std::shared_ptr<SessionLocalFile>&& file)
{
return SessionLocalFile::releaseLastReference(std::move(file));
}
std::string getFileHandleID() const
{
return handle->id;
}
uint16_t getTargetID() const
{
return targetID;
}
std::string getFileID() const
{
return fileID;
}
const FDHandle& getFD()
{
if (handle->fd.valid()) // optimization: try without a lock first
return handle->fd;
std::lock_guard<Mutex> const lock(sessionMutex);
return handle->fd;
}
int getOpenFlags() const
{
return openFlags;
}
bool getIsDirectIO() const
{
return (this->openFlags & (O_DIRECT | O_SYNC) ) != 0;
}
int64_t getOffset()
{
std::lock_guard<Mutex> const lock(sessionMutex);
return offset;
}
void setOffset(int64_t offset)
{
std::lock_guard<Mutex> const lock(sessionMutex);
this->offset = offset;
}
NodeHandle getMirrorNode() const
{
return mirrorNode;
}
bool getIsMirrorSession() const
{
return isMirrorSession;
}
void setIsMirrorSession(bool isMirrorSession)
{
this->isMirrorSession = isMirrorSession;
}
void resetWriteCounter()
{
this->writeCounter = 0;
}
void incWriteCounter(int64_t size)
{
this->writeCounter.increase(size);
}
int64_t getWriteCounter()
{
return this->writeCounter.read();
}
int64_t getReadCounter()
{
return this->readCounter.read();
}
void resetReadCounter()
{
this->readCounter.setZero();
}
void incReadCounter(int64_t size)
{
this->readCounter.increase(size);
}
int64_t getLastReadAheadTrigger()
{
return this->lastReadAheadTrigger.read();
}
void resetLastReadAheadTrigger()
{
this->lastReadAheadTrigger.setZero();
}
void setLastReadAheadTrigger(int64_t lastReadAheadTrigger)
{
this->lastReadAheadTrigger.set(lastReadAheadTrigger);
}
bool isServerCrashed()
{
return this->serverCrashed;
}
};