FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/session/SessionSourceDatabaseSupervisor.cpp at master · usersbin/rstudio · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
usersbin
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
session
/
SessionSourceDatabaseSupervisor.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
343 lines (277 loc) · 9.05 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
session
/
SessionSourceDatabaseSupervisor.cpp
Copy path
File metadata and controls
343 lines (277 loc) · 9.05 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* SessionSourceDatabaseSupervisor.cpp
*
* Copyright (C) 2009-11 by RStudio, Inc.
*
* This program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
"
SessionSourceDatabaseSupervisor.hpp
"
#
include
<
vector
>
#
include
<
boost/foreach.hpp
>
#
include
<
boost/scope_exit.hpp
>
#
include
<
boost/algorithm/string/predicate.hpp
>
#
include
<
core/Error.hpp
>
#
include
<
core/FilePath.hpp
>
#
include
<
core/FileSerializer.hpp
>
#
include
<
core/FileLock.hpp
>
#
include
<
core/BoostErrors.hpp
>
#
include
<
core/system/System.hpp
>
#
include
<
session/SessionModuleContext.hpp
>
#
include
"
session/SessionSourceDatabase.hpp
"
using
namespace
core
;
namespace
session
{
namespace
source_database
{
namespace
supervisor
{
namespace
{
const
char
*
const
kSessionDirPrefix
=
"
session-
"
;
FilePath
oldSourceDatabaseRoot
()
{
return
module_context::scopedScratchPath
().
complete
(
"
source_database
"
);
}
FilePath
sourceDatabaseRoot
()
{
return
module_context::scopedScratchPath
().
complete
(
"
source_database_v2
"
);
}
FilePath
persistentTitledDir
()
{
return
sourceDatabaseRoot
().
complete
(
"
persistent/titled
"
);
}
FilePath
persistentUntitledDir
()
{
return
sourceDatabaseRoot
().
complete
(
"
persistent/untitled
"
);
}
FilePath
sessionLockFilePath
(
const
FilePath& sessionDir)
{
return
sessionDir.
complete
(
"
lock_file
"
);
}
//
session dir lock (initialized by attachToSourceDatabase)
FileLock s_sessionDirLock;
Error
removeSessionDir
(
const
FilePath& sessionDir)
{
//
first remove children
std::vector<FilePath> children;
Error error = sessionDir.
children
(&children);
if
(error)
LOG_ERROR
(error);
BOOST_FOREACH
(
const
FilePath& filePath, children)
{
error = filePath.
remove
();
if
(error)
LOG_ERROR
(error);
}
//
then remove dir
return
sessionDir.
remove
();
}
FilePath
generateSessionDirPath
()
{
return
sourceDatabaseRoot
().
complete
(
kSessionDirPrefix
+
core::system::generateUuid
());
}
bool
isNotSessionDir
(
const
FilePath& filePath)
{
return
!filePath.
isDirectory
() || !
boost::algorithm::starts_with
(
filePath.
filename
(),
kSessionDirPrefix
);
}
Error
enumerateSessionDirs
(std::vector<FilePath>* pSessionDirs)
{
//
get the directories
Error error =
sourceDatabaseRoot
().
children
(pSessionDirs);
if
(error)
return
error;
//
clean out non session dirs
pSessionDirs->
erase
(
std::remove_if
(pSessionDirs->
begin
(),
pSessionDirs->
end
(),
isNotSessionDir),
pSessionDirs->
end
());
//
return success
return
Success
();
}
void
attemptToMoveSourceDbFiles
(
const
FilePath& fromPath,
const
FilePath& toPath)
{
//
enumerate the from path
std::vector<FilePath> children;
Error error = fromPath.
children
(&children);
if
(error)
LOG_ERROR
(error);
//
move the files
BOOST_FOREACH
(
const
FilePath& filePath, children)
{
Error error = filePath.
move
(toPath.
complete
(filePath.
filename
()));
if
(error)
LOG_ERROR
(error);
}
}
//
NOTE: the supervisor needs to return a session dir in order for the process
//
to start. therefore, in the createSessionDir family of functions below
//
once we successfully create and lock the session dir other errors (such
//
as trying to move files into the session dir) are simply logged
Error
createSessionDir
(FilePath* pSessionDir)
{
*pSessionDir =
generateSessionDirPath
();
Error error = pSessionDir->
ensureDirectory
();
if
(error)
return
error;
return
s_sessionDirLock.
acquire
(
sessionLockFilePath
(*pSessionDir));
}
Error
createSessionDirFromOldSourceDatabase
(FilePath* pSessionDir)
{
//
move properties (if any) into new source database root
FilePath propsPath =
oldSourceDatabaseRoot
().
complete
(
"
properties
"
);
if
(propsPath.
exists
())
{
FilePath newPropsPath =
sourceDatabaseRoot
().
complete
(
"
properties
"
);
Error error = propsPath.
move
(newPropsPath);
if
(error)
LOG_ERROR
(error);
}
//
move the old source database into a new dir
*pSessionDir =
generateSessionDirPath
();
Error error =
oldSourceDatabaseRoot
().
move
(*pSessionDir);
if
(error)
LOG_ERROR
(error);
//
if that failed we might still need to call ensureDirectory
error = pSessionDir->
ensureDirectory
();
if
(error)
return
error;
//
acquire the lock
return
s_sessionDirLock.
acquire
(
sessionLockFilePath
(*pSessionDir));
}
Error
createSessionDirFromPersistent
(FilePath* pSessionDir)
{
//
create new session dir
Error error =
createSessionDir
(pSessionDir);
if
(error)
return
error;
//
move persistent titled files
if
(
persistentTitledDir
().
exists
())
attemptToMoveSourceDbFiles
(
persistentTitledDir
(), *pSessionDir);
//
move persistent untitled files
if
(
persistentUntitledDir
().
exists
())
attemptToMoveSourceDbFiles
(
persistentUntitledDir
(), *pSessionDir);
//
return success
return
Success
();
}
bool
reclaimOrphanedSession
(
const
std::vector<FilePath>& sessionDirs,
FilePath* pSessionDir)
{
BOOST_FOREACH
(
const
FilePath& sessionDir, sessionDirs)
{
FilePath lockFilePath =
sessionLockFilePath
(sessionDir);
if
(!
FileLock::isLocked
(lockFilePath))
{
Error error = s_sessionDirLock.
acquire
(lockFilePath);
if
(!error)
{
*pSessionDir = sessionDir;
return
true
;
}
else
{
LOG_ERROR
(error);
}
}
}
return
false
;
}
bool
noLockedSessionsExist
(
const
std::vector<FilePath>& sessionDirs)
{
BOOST_FOREACH
(
const
FilePath& sessionDir, sessionDirs)
{
FilePath lockFilePath =
sessionLockFilePath
(sessionDir);
if
(
FileLock::isLocked
(lockFilePath))
return
false
;
}
return
true
;
}
}
//
anonymous namespace
Error
attachToSourceDatabase
(FilePath* pSessionDir)
{
//
check whether we will need to migrate -- ensure we do this only
//
one time so that if for whatever reason we can't migrate the
//
old source database we don't get stuck trying to do it every
//
time we start up
bool
needToMigrate = !
sourceDatabaseRoot
().
exists
() &&
oldSourceDatabaseRoot
().
exists
();
//
ensure the root path exists
Error error =
sourceDatabaseRoot
().
ensureDirectory
();
if
(error)
return
error;
//
check for existing sessions (use this to decide how to startup below)
std::vector<FilePath> sessionDirs;
error =
enumerateSessionDirs
(&sessionDirs);
if
(error)
LOG_ERROR
(error);
//
attempt to migrate if necessary
if
(needToMigrate)
return
createSessionDirFromOldSourceDatabase
(pSessionDir);
//
if there is an orphan (crash) then reclaim it
else
if
(
reclaimOrphanedSession
(sessionDirs, pSessionDir))
return
Success
();
//
if there are no existing locked sessions then create from persistent
else
if
(
noLockedSessionsExist
(sessionDirs))
return
createSessionDirFromPersistent
(pSessionDir);
//
otherwise startup with a brand new session dir
else
return
createSessionDir
(pSessionDir);
}
Error
detachFromSourceDatabase
()
{
//
confirm that we are attached
if
(s_sessionDirLock.
lockFilePath
().
empty
())
return
core::pathNotFoundError
(
ERROR_LOCATION
);
//
list all current source docs
std::vector<boost::shared_ptr<SourceDocument> > sourceDocs;
Error error =
source_database::list
(&sourceDocs);
if
(error)
return
error;
//
get references to persistent subdirs
FilePath titledDir =
persistentTitledDir
();
FilePath untitledDir =
persistentUntitledDir
();
//
first blow away the existing persistent titled dir
error = titledDir.
removeIfExists
();
if
(error)
LOG_ERROR
(error);
//
ensure both directories exist -- if they don't it is a fatal error
error = titledDir.
ensureDirectory
();
if
(error)
return
error;
error = untitledDir.
ensureDirectory
();
if
(error)
return
error;
//
now write the source database entries to the appropriate places
BOOST_FOREACH
(boost::shared_ptr<SourceDocument> pDoc, sourceDocs)
{
if
(pDoc->
isUntitled
())
{
error = pDoc->
writeToFile
(untitledDir.
complete
(pDoc->
id
()));
if
(error)
LOG_ERROR
(error);
}
else
{
error = pDoc->
writeToFile
(titledDir.
complete
(pDoc->
id
()));
if
(error)
LOG_ERROR
(error);
}
}
//
record session dir (parent of lock file)
FilePath sessionDir = s_sessionDirLock.
lockFilePath
().
parent
();
//
give up our lock
error = s_sessionDirLock.
release
();
if
(error)
LOG_ERROR
(error);
//
remove the session directory
return
removeSessionDir
(sessionDir);
}
}
//
namespace supervisor
}
//
namespace source_database
}
//
namespace session
Back
|
FazBrowse Home
|
New Git URL