root / trunk / Pithos.Core / Agents / PollAgent.cs @ 99e6329f
History | View | Annotate | Download (23.6 kB)
1 |
#region |
---|---|
2 |
/* ----------------------------------------------------------------------- |
3 |
* <copyright file="PollAgent.cs" company="GRNet"> |
4 |
* |
5 |
* Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 |
* |
7 |
* Redistribution and use in source and binary forms, with or |
8 |
* without modification, are permitted provided that the following |
9 |
* conditions are met: |
10 |
* |
11 |
* 1. Redistributions of source code must retain the above |
12 |
* copyright notice, this list of conditions and the following |
13 |
* disclaimer. |
14 |
* |
15 |
* 2. Redistributions in binary form must reproduce the above |
16 |
* copyright notice, this list of conditions and the following |
17 |
* disclaimer in the documentation and/or other materials |
18 |
* provided with the distribution. |
19 |
* |
20 |
* |
21 |
* THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
22 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
24 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
25 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
28 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
29 |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
31 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 |
* POSSIBILITY OF SUCH DAMAGE. |
33 |
* |
34 |
* The views and conclusions contained in the software and |
35 |
* documentation are those of the authors and should not be |
36 |
* interpreted as representing official policies, either expressed |
37 |
* or implied, of GRNET S.A. |
38 |
* </copyright> |
39 |
* ----------------------------------------------------------------------- |
40 |
*/ |
41 |
#endregion |
42 |
|
43 |
using System.Collections.Concurrent; |
44 |
using System.ComponentModel.Composition; |
45 |
using System.Diagnostics; |
46 |
using System.Diagnostics.Contracts; |
47 |
using System.IO; |
48 |
using System.Threading; |
49 |
using System.Threading.Tasks; |
50 |
using System.Threading.Tasks.Dataflow; |
51 |
using Castle.ActiveRecord; |
52 |
using Pithos.Interfaces; |
53 |
using Pithos.Network; |
54 |
using log4net; |
55 |
|
56 |
namespace Pithos.Core.Agents |
57 |
{ |
58 |
using System; |
59 |
using System.Collections.Generic; |
60 |
using System.Linq; |
61 |
using System.Text; |
62 |
|
63 |
/// <summary> |
64 |
/// PollAgent periodically polls the server to detect object changes. The agent retrieves a listing of all |
65 |
/// objects and compares it with a previously cached version to detect differences. |
66 |
/// New files are downloaded, missing files are deleted from the local file system and common files are compared |
67 |
/// to determine the appropriate action |
68 |
/// </summary> |
69 |
[Export] |
70 |
public class PollAgent |
71 |
{ |
72 |
private static readonly ILog Log = LogManager.GetLogger("PollAgent"); |
73 |
|
74 |
[System.ComponentModel.Composition.Import] |
75 |
public IStatusKeeper StatusKeeper { get; set; } |
76 |
|
77 |
[System.ComponentModel.Composition.Import] |
78 |
public IPithosSettings Settings { get; set; } |
79 |
|
80 |
[System.ComponentModel.Composition.Import] |
81 |
public NetworkAgent NetworkAgent { get; set; } |
82 |
|
83 |
public IStatusNotification StatusNotification { get; set; } |
84 |
|
85 |
private bool _firstPoll = true; |
86 |
|
87 |
//The Sync Event signals a manual synchronisation |
88 |
private readonly AsyncManualResetEvent _syncEvent = new AsyncManualResetEvent(); |
89 |
|
90 |
private ConcurrentDictionary<string, DateTime> _lastSeen = new ConcurrentDictionary<string, DateTime>(); |
91 |
private readonly ConcurrentBag<AccountInfo> _accounts = new ConcurrentBag<AccountInfo>(); |
92 |
|
93 |
|
94 |
/// <summary> |
95 |
/// Start a manual synchronization |
96 |
/// </summary> |
97 |
public void SynchNow() |
98 |
{ |
99 |
_syncEvent.Set(); |
100 |
} |
101 |
|
102 |
/// <summary> |
103 |
/// Remote files are polled periodically. Any changes are processed |
104 |
/// </summary> |
105 |
/// <param name="since"></param> |
106 |
/// <returns></returns> |
107 |
public async Task PollRemoteFiles(DateTime? since = null) |
108 |
{ |
109 |
Debug.Assert(Thread.CurrentThread.IsBackground, "Polling Ended up in the main thread!"); |
110 |
|
111 |
UpdateStatus(PithosStatus.Syncing); |
112 |
StatusNotification.Notify(new PollNotification()); |
113 |
|
114 |
using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push("All accounts")) |
115 |
{ |
116 |
//If this poll fails, we will retry with the same since value |
117 |
var nextSince = since; |
118 |
try |
119 |
{ |
120 |
//Next time we will check for all changes since the current check minus 1 second |
121 |
//This is done to ensure there are no discrepancies due to clock differences |
122 |
var current = DateTime.Now.AddSeconds(-1); |
123 |
|
124 |
var tasks = from accountInfo in _accounts |
125 |
select ProcessAccountFiles(accountInfo, since); |
126 |
|
127 |
await TaskEx.WhenAll(tasks.ToList()); |
128 |
|
129 |
_firstPoll = false; |
130 |
//Reschedule the poll with the current timestamp as a "since" value |
131 |
nextSince = current; |
132 |
} |
133 |
catch (Exception ex) |
134 |
{ |
135 |
Log.ErrorFormat("Error while processing accounts\r\n{0}", ex); |
136 |
//In case of failure retry with the same "since" value |
137 |
} |
138 |
|
139 |
UpdateStatus(PithosStatus.InSynch); |
140 |
//The multiple try blocks are required because we can't have an await call |
141 |
//inside a finally block |
142 |
//TODO: Find a more elegant solution for reschedulling in the event of an exception |
143 |
try |
144 |
{ |
145 |
//Wait for the polling interval to pass or the Sync event to be signalled |
146 |
nextSince = await WaitForScheduledOrManualPoll(nextSince); |
147 |
} |
148 |
finally |
149 |
{ |
150 |
//Ensure polling is scheduled even in case of error |
151 |
TaskEx.Run(() => PollRemoteFiles(nextSince)); |
152 |
} |
153 |
} |
154 |
} |
155 |
|
156 |
/// <summary> |
157 |
/// Wait for the polling period to expire or a manual sync request |
158 |
/// </summary> |
159 |
/// <param name="since"></param> |
160 |
/// <returns></returns> |
161 |
private async Task<DateTime?> WaitForScheduledOrManualPoll(DateTime? since) |
162 |
{ |
163 |
var sync = _syncEvent.WaitAsync(); |
164 |
var wait = TaskEx.Delay(TimeSpan.FromSeconds(Settings.PollingInterval), NetworkAgent.CancellationToken); |
165 |
var signaledTask = await TaskEx.WhenAny(sync, wait); |
166 |
|
167 |
//Wait for network processing to finish before polling |
168 |
var pauseTask=NetworkAgent.ProceedEvent.WaitAsync(); |
169 |
await TaskEx.WhenAll(signaledTask, pauseTask); |
170 |
|
171 |
//If polling is signalled by SynchNow, ignore the since tag |
172 |
if (sync.IsCompleted) |
173 |
{ |
174 |
//TODO: Must convert to AutoReset |
175 |
_syncEvent.Reset(); |
176 |
return null; |
177 |
} |
178 |
return since; |
179 |
} |
180 |
|
181 |
public async Task ProcessAccountFiles(AccountInfo accountInfo, DateTime? since = null) |
182 |
{ |
183 |
if (accountInfo == null) |
184 |
throw new ArgumentNullException("accountInfo"); |
185 |
if (String.IsNullOrWhiteSpace(accountInfo.AccountPath)) |
186 |
throw new ArgumentException("The AccountInfo.AccountPath is empty", "accountInfo"); |
187 |
Contract.EndContractBlock(); |
188 |
|
189 |
|
190 |
using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push(accountInfo.UserName)) |
191 |
{ |
192 |
await NetworkAgent.GetDeleteAwaiter(); |
193 |
|
194 |
Log.Info("Scheduled"); |
195 |
var client = new CloudFilesClient(accountInfo); |
196 |
|
197 |
//We don't need to check the trash container |
198 |
var containers = client.ListContainers(accountInfo.UserName).Where(c=>c.Name!="trash"); |
199 |
|
200 |
|
201 |
CreateContainerFolders(accountInfo, containers); |
202 |
|
203 |
try |
204 |
{ |
205 |
//Wait for any deletions to finish |
206 |
await NetworkAgent.GetDeleteAwaiter(); |
207 |
//Get the poll time now. We may miss some deletions but it's better to keep a file that was deleted |
208 |
//than delete a file that was created while we were executing the poll |
209 |
var pollTime = DateTime.Now; |
210 |
|
211 |
//Get the list of server objects changed since the last check |
212 |
//The name of the container is passed as state in order to create a dictionary of tasks in a subsequent step |
213 |
var listObjects = (from container in containers |
214 |
select Task<IList<ObjectInfo>>.Factory.StartNew(_ => |
215 |
client.ListObjects(accountInfo.UserName, container.Name, since), container.Name)).ToList(); |
216 |
//BUG: Can't detect difference between no changes or no objects |
217 |
//ListObjects returns nothing if there are no changes since the last check time (since value) |
218 |
//TODO: Must detect the difference between no server objects and no change |
219 |
|
220 |
//NOTE: One option is to "mark" all result lists with their container name, or |
221 |
//rather the url of the container |
222 |
//Another option |
223 |
|
224 |
var listShared = Task<IList<ObjectInfo>>.Factory.StartNew(_ => |
225 |
client.ListSharedObjects(since), "shared"); |
226 |
listObjects.Add(listShared); |
227 |
var listTasks = await Task.Factory.WhenAll(listObjects.ToArray()); |
228 |
|
229 |
using (log4net.ThreadContext.Stacks["SCHEDULE"].Push("Process Results")) |
230 |
{ |
231 |
var dict = listTasks.ToDictionary(t => t.AsyncState); |
232 |
|
233 |
//Get all non-trash objects. Remember, the container name is stored in AsyncState |
234 |
var remoteObjects = from objectList in listTasks |
235 |
where (string)objectList.AsyncState != "trash" |
236 |
from obj in objectList.Result |
237 |
select obj; |
238 |
|
239 |
var sharedObjects = dict["shared"].Result; |
240 |
|
241 |
//DON'T process trashed files |
242 |
//If some files are deleted and added again to a folder, they will be deleted |
243 |
//even though they are new. |
244 |
//We would have to check file dates and hashes to ensure that a trashed file |
245 |
//can be deleted safely from the local hard drive. |
246 |
/* |
247 |
//Items with the same name, hash may be both in the container and the trash |
248 |
//Don't delete items that exist in the container |
249 |
var realTrash = from trash in trashObjects |
250 |
where |
251 |
!remoteObjects.Any( |
252 |
info => info.Name == trash.Name && info.Hash == trash.Hash) |
253 |
select trash; |
254 |
ProcessTrashedFiles(accountInfo, realTrash); |
255 |
*/ |
256 |
|
257 |
var cleanRemotes = (from info in remoteObjects.Union(sharedObjects) |
258 |
let name = info.Name |
259 |
where !name.EndsWith(".ignore", StringComparison.InvariantCultureIgnoreCase) && |
260 |
!name.StartsWith(FolderConstants.CacheFolder + "/", |
261 |
StringComparison.InvariantCultureIgnoreCase) |
262 |
select info).ToList(); |
263 |
|
264 |
var differencer = _differencer.PostSnapshot(accountInfo, cleanRemotes); |
265 |
|
266 |
ProcessDeletedFiles(accountInfo, differencer.Deleted.FilterDirectlyBelow(SelectiveUris), pollTime); |
267 |
|
268 |
// @@@ NEED To add previous state here as well, To compare with previous hash |
269 |
|
270 |
|
271 |
|
272 |
//Create a list of actions from the remote files |
273 |
var allActions = ChangesToActions(accountInfo, differencer.Changed.FilterDirectlyBelow(SelectiveUris)) |
274 |
.Union( |
275 |
CreatesToActions(accountInfo, differencer.Created.FilterDirectlyBelow(SelectiveUris))); |
276 |
|
277 |
//And remove those that are already being processed by the agent |
278 |
var distinctActions = allActions |
279 |
.Except(NetworkAgent.GetEnumerable(), new PithosMonitor.LocalFileComparer()) |
280 |
.ToList(); |
281 |
|
282 |
//Queue all the actions |
283 |
foreach (var message in distinctActions) |
284 |
{ |
285 |
NetworkAgent.Post(message); |
286 |
} |
287 |
|
288 |
Log.Info("[LISTENER] End Processing"); |
289 |
} |
290 |
} |
291 |
catch (Exception ex) |
292 |
{ |
293 |
Log.ErrorFormat("[FAIL] ListObjects for{0} in ProcessRemoteFiles with {1}", accountInfo.UserName, ex); |
294 |
return; |
295 |
} |
296 |
|
297 |
Log.Info("[LISTENER] Finished"); |
298 |
|
299 |
} |
300 |
} |
301 |
|
302 |
AccountsDifferencer _differencer = new AccountsDifferencer(); |
303 |
private List<Uri> _selectiveUris=new List<Uri>(); |
304 |
|
305 |
/// <summary> |
306 |
/// Deletes local files that are not found in the list of cloud files |
307 |
/// </summary> |
308 |
/// <param name="accountInfo"></param> |
309 |
/// <param name="cloudFiles"></param> |
310 |
/// <param name="pollTime"></param> |
311 |
private void ProcessDeletedFiles(AccountInfo accountInfo, IEnumerable<ObjectInfo> cloudFiles, DateTime pollTime) |
312 |
{ |
313 |
if (accountInfo == null) |
314 |
throw new ArgumentNullException("accountInfo"); |
315 |
if (String.IsNullOrWhiteSpace(accountInfo.AccountPath)) |
316 |
throw new ArgumentException("The AccountInfo.AccountPath is empty", "accountInfo"); |
317 |
if (cloudFiles == null) |
318 |
throw new ArgumentNullException("cloudFiles"); |
319 |
Contract.EndContractBlock(); |
320 |
|
321 |
//On the first run |
322 |
if (_firstPoll) |
323 |
{ |
324 |
//Only consider files that are not being modified, ie they are in the Unchanged state |
325 |
var deleteCandidates = FileState.Queryable.Where(state => |
326 |
state.FilePath.StartsWith(accountInfo.AccountPath) |
327 |
&& state.FileStatus == FileStatus.Unchanged).ToList(); |
328 |
|
329 |
|
330 |
//TODO: filesToDelete must take into account the Others container |
331 |
var filesToDelete = (from deleteCandidate in deleteCandidates |
332 |
let localFile = FileInfoExtensions.FromPath(deleteCandidate.FilePath) |
333 |
let relativeFilePath = localFile.AsRelativeTo(accountInfo.AccountPath) |
334 |
where |
335 |
!cloudFiles.Any(r => r.RelativeUrlToFilePath(accountInfo.UserName) == relativeFilePath) |
336 |
select localFile).ToList(); |
337 |
|
338 |
|
339 |
|
340 |
//Set the status of missing files to Conflict |
341 |
foreach (var item in filesToDelete) |
342 |
{ |
343 |
//Try to acquire a gate on the file, to take into account files that have been dequeued |
344 |
//and are being processed |
345 |
using (var gate = NetworkGate.Acquire(item.FullName, NetworkOperation.Deleting)) |
346 |
{ |
347 |
if (gate.Failed) |
348 |
continue; |
349 |
StatusKeeper.SetFileState(item.FullName, FileStatus.Conflict, FileOverlayStatus.Deleted); |
350 |
} |
351 |
} |
352 |
UpdateStatus(PithosStatus.HasConflicts); |
353 |
StatusNotification.NotifyConflicts(filesToDelete, String.Format("{0} local files are missing from Pithos, possibly because they were deleted", filesToDelete.Count)); |
354 |
StatusNotification.NotifyForFiles(filesToDelete, String.Format("{0} files were deleted", filesToDelete.Count), TraceLevel.Info); |
355 |
} |
356 |
else |
357 |
{ |
358 |
var deletedFiles = new List<FileSystemInfo>(); |
359 |
foreach (var objectInfo in cloudFiles) |
360 |
{ |
361 |
var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName); |
362 |
var item = FileAgent.GetFileAgent(accountInfo).GetFileSystemInfo(relativePath); |
363 |
if (item.Exists) |
364 |
{ |
365 |
if ((item.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) |
366 |
{ |
367 |
item.Attributes = item.Attributes & ~FileAttributes.ReadOnly; |
368 |
|
369 |
} |
370 |
item.Delete(); |
371 |
DateTime lastDate; |
372 |
_lastSeen.TryRemove(item.FullName, out lastDate); |
373 |
deletedFiles.Add(item); |
374 |
} |
375 |
StatusKeeper.SetFileState(item.FullName, FileStatus.Deleted, FileOverlayStatus.Deleted); |
376 |
} |
377 |
StatusNotification.NotifyForFiles(deletedFiles, String.Format("{0} files were deleted", deletedFiles.Count), TraceLevel.Info); |
378 |
} |
379 |
|
380 |
} |
381 |
|
382 |
//Creates an appropriate action for each server file |
383 |
private IEnumerable<CloudAction> ChangesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> changes) |
384 |
{ |
385 |
if (changes == null) |
386 |
throw new ArgumentNullException(); |
387 |
Contract.EndContractBlock(); |
388 |
var fileAgent = FileAgent.GetFileAgent(accountInfo); |
389 |
|
390 |
//In order to avoid multiple iterations over the files, we iterate only once |
391 |
//over the remote files |
392 |
foreach (var objectInfo in changes) |
393 |
{ |
394 |
var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName); |
395 |
//and remove any matching objects from the list, adding them to the commonObjects list |
396 |
if (fileAgent.Exists(relativePath)) |
397 |
{ |
398 |
//If a directory object already exists, we don't need to perform any other action |
399 |
var localFile = fileAgent.GetFileSystemInfo(relativePath); |
400 |
if (objectInfo.Content_Type == @"application/directory" && localFile is DirectoryInfo) |
401 |
continue; |
402 |
using (new SessionScope(FlushAction.Never)) |
403 |
{ |
404 |
var state = StatusKeeper.GetStateByFilePath(localFile.FullName); |
405 |
_lastSeen[localFile.FullName] = DateTime.Now; |
406 |
//Common files should be checked on a per-case basis to detect differences, which is newer |
407 |
|
408 |
yield return new CloudAction(accountInfo, CloudActionType.MustSynch, |
409 |
localFile, objectInfo, state, accountInfo.BlockSize, |
410 |
accountInfo.BlockHash); |
411 |
} |
412 |
} |
413 |
else |
414 |
{ |
415 |
//Remote files should be downloaded |
416 |
yield return new CloudDownloadAction(accountInfo, objectInfo); |
417 |
} |
418 |
} |
419 |
} |
420 |
|
421 |
private IEnumerable<CloudAction> CreatesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> creates) |
422 |
{ |
423 |
if (creates == null) |
424 |
throw new ArgumentNullException(); |
425 |
Contract.EndContractBlock(); |
426 |
var fileAgent = FileAgent.GetFileAgent(accountInfo); |
427 |
|
428 |
//In order to avoid multiple iterations over the files, we iterate only once |
429 |
//over the remote files |
430 |
foreach (var objectInfo in creates) |
431 |
{ |
432 |
var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName); |
433 |
//and remove any matching objects from the list, adding them to the commonObjects list |
434 |
if (fileAgent.Exists(relativePath)) |
435 |
{ |
436 |
//If the object already exists, we probably have a conflict |
437 |
//If a directory object already exists, we don't need to perform any other action |
438 |
var localFile = fileAgent.GetFileSystemInfo(relativePath); |
439 |
StatusKeeper.SetFileState(localFile.FullName, FileStatus.Conflict, FileOverlayStatus.Conflict); |
440 |
} |
441 |
else |
442 |
{ |
443 |
//Remote files should be downloaded |
444 |
yield return new CloudDownloadAction(accountInfo, objectInfo); |
445 |
} |
446 |
} |
447 |
} |
448 |
|
449 |
private void ProcessTrashedFiles(AccountInfo accountInfo, IEnumerable<ObjectInfo> trashObjects) |
450 |
{ |
451 |
var fileAgent = FileAgent.GetFileAgent(accountInfo); |
452 |
foreach (var trashObject in trashObjects) |
453 |
{ |
454 |
var barePath = trashObject.RelativeUrlToFilePath(accountInfo.UserName); |
455 |
//HACK: Assume only the "pithos" container is used. Must find out what happens when |
456 |
//deleting a file from a different container |
457 |
var relativePath = Path.Combine("pithos", barePath); |
458 |
fileAgent.Delete(relativePath); |
459 |
} |
460 |
} |
461 |
|
462 |
/// <summary> |
463 |
/// Notify the UI to update the visual status |
464 |
/// </summary> |
465 |
/// <param name="status"></param> |
466 |
private void UpdateStatus(PithosStatus status) |
467 |
{ |
468 |
try |
469 |
{ |
470 |
StatusKeeper.SetPithosStatus(status); |
471 |
StatusNotification.Notify(new Notification()); |
472 |
} |
473 |
catch (Exception exc) |
474 |
{ |
475 |
//Failure is not critical, just log it |
476 |
Log.Warn("Error while updating status", exc); |
477 |
} |
478 |
} |
479 |
|
480 |
private static void CreateContainerFolders(AccountInfo accountInfo, IEnumerable<ContainerInfo> containers) |
481 |
{ |
482 |
var containerPaths = from container in containers |
483 |
let containerPath = Path.Combine(accountInfo.AccountPath, container.Name) |
484 |
where container.Name != FolderConstants.TrashContainer && !Directory.Exists(containerPath) |
485 |
select containerPath; |
486 |
|
487 |
foreach (var path in containerPaths) |
488 |
{ |
489 |
Directory.CreateDirectory(path); |
490 |
} |
491 |
} |
492 |
|
493 |
public void SetSyncUris(Uri[] uris) |
494 |
{ |
495 |
SelectiveUris=uris.ToList(); |
496 |
} |
497 |
|
498 |
protected List<Uri> SelectiveUris |
499 |
{ |
500 |
get { return _selectiveUris;} |
501 |
set { _selectiveUris = value; } |
502 |
} |
503 |
|
504 |
public void AddAccount(AccountInfo accountInfo) |
505 |
{ |
506 |
if (!_accounts.Contains(accountInfo)) |
507 |
_accounts.Add(accountInfo); |
508 |
} |
509 |
} |
510 |
} |