2 /* -----------------------------------------------------------------------
\r
3 * <copyright file="PollAgent.cs" company="GRNet">
\r
5 * Copyright 2011-2012 GRNET S.A. All rights reserved.
\r
7 * Redistribution and use in source and binary forms, with or
\r
8 * without modification, are permitted provided that the following
\r
9 * conditions are met:
\r
11 * 1. Redistributions of source code must retain the above
\r
12 * copyright notice, this list of conditions and the following
\r
15 * 2. Redistributions in binary form must reproduce the above
\r
16 * copyright notice, this list of conditions and the following
\r
17 * disclaimer in the documentation and/or other materials
\r
18 * provided with the distribution.
\r
21 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
\r
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
\r
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\r
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
\r
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
\r
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
\r
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
\r
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
\r
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
\r
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
32 * POSSIBILITY OF SUCH DAMAGE.
\r
34 * The views and conclusions contained in the software and
\r
35 * documentation are those of the authors and should not be
\r
36 * interpreted as representing official policies, either expressed
\r
37 * or implied, of GRNET S.A.
\r
39 * -----------------------------------------------------------------------
\r
43 using System.Collections.Concurrent;
\r
44 using System.ComponentModel.Composition;
\r
45 using System.Diagnostics;
\r
46 using System.Diagnostics.Contracts;
\r
48 using System.Threading;
\r
49 using System.Threading.Tasks;
\r
50 using System.Threading.Tasks.Dataflow;
\r
51 using Castle.ActiveRecord;
\r
52 using Pithos.Interfaces;
\r
53 using Pithos.Network;
\r
56 namespace Pithos.Core.Agents
\r
59 using System.Collections.Generic;
\r
64 /// PollAgent periodically polls the server to detect object changes. The agent retrieves a listing of all
\r
65 /// objects and compares it with a previously cached version to detect differences.
\r
66 /// New files are downloaded, missing files are deleted from the local file system and common files are compared
\r
67 /// to determine the appropriate action
\r
70 public class PollAgent
\r
72 private static readonly ILog Log = LogManager.GetLogger("PollAgent");
\r
74 [System.ComponentModel.Composition.Import]
\r
75 public IStatusKeeper StatusKeeper { get; set; }
\r
77 [System.ComponentModel.Composition.Import]
\r
78 public IPithosSettings Settings { get; set; }
\r
80 [System.ComponentModel.Composition.Import]
\r
81 public NetworkAgent NetworkAgent { get; set; }
\r
83 public IStatusNotification StatusNotification { get; set; }
\r
85 private bool _firstPoll = true;
\r
87 //The Sync Event signals a manual synchronisation
\r
88 private readonly AsyncManualResetEvent _syncEvent = new AsyncManualResetEvent();
\r
90 private ConcurrentDictionary<string, DateTime> _lastSeen = new ConcurrentDictionary<string, DateTime>();
\r
91 private readonly ConcurrentBag<AccountInfo> _accounts = new ConcurrentBag<AccountInfo>();
\r
95 /// Start a manual synchronization
\r
97 public void SynchNow()
\r
103 /// Remote files are polled periodically. Any changes are processed
\r
105 /// <param name="since"></param>
\r
106 /// <returns></returns>
\r
107 public async Task PollRemoteFiles(DateTime? since = null)
\r
109 Debug.Assert(Thread.CurrentThread.IsBackground, "Polling Ended up in the main thread!");
\r
111 UpdateStatus(PithosStatus.Syncing);
\r
112 StatusNotification.Notify(new PollNotification());
\r
114 using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push("All accounts"))
\r
116 //If this poll fails, we will retry with the same since value
\r
117 var nextSince = since;
\r
120 //Next time we will check for all changes since the current check minus 1 second
\r
121 //This is done to ensure there are no discrepancies due to clock differences
\r
122 var current = DateTime.Now.AddSeconds(-1);
\r
124 var tasks = from accountInfo in _accounts
\r
125 select ProcessAccountFiles(accountInfo, since);
\r
127 await TaskEx.WhenAll(tasks.ToList());
\r
129 _firstPoll = false;
\r
130 //Reschedule the poll with the current timestamp as a "since" value
\r
131 nextSince = current;
\r
133 catch (Exception ex)
\r
135 Log.ErrorFormat("Error while processing accounts\r\n{0}", ex);
\r
136 //In case of failure retry with the same "since" value
\r
139 UpdateStatus(PithosStatus.InSynch);
\r
140 //The multiple try blocks are required because we can't have an await call
\r
141 //inside a finally block
\r
142 //TODO: Find a more elegant solution for reschedulling in the event of an exception
\r
145 //Wait for the polling interval to pass or the Sync event to be signalled
\r
146 nextSince = await WaitForScheduledOrManualPoll(nextSince);
\r
150 //Ensure polling is scheduled even in case of error
\r
151 TaskEx.Run(() => PollRemoteFiles(nextSince));
\r
157 /// Wait for the polling period to expire or a manual sync request
\r
159 /// <param name="since"></param>
\r
160 /// <returns></returns>
\r
161 private async Task<DateTime?> WaitForScheduledOrManualPoll(DateTime? since)
\r
163 var sync = _syncEvent.WaitAsync();
\r
164 var wait = TaskEx.Delay(TimeSpan.FromSeconds(Settings.PollingInterval), NetworkAgent.CancellationToken);
\r
165 var signaledTask = await TaskEx.WhenAny(sync, wait);
\r
167 //Wait for network processing to finish before polling
\r
168 var pauseTask=NetworkAgent.ProceedEvent.WaitAsync();
\r
169 await TaskEx.WhenAll(signaledTask, pauseTask);
\r
171 //If polling is signalled by SynchNow, ignore the since tag
\r
172 if (sync.IsCompleted)
\r
174 //TODO: Must convert to AutoReset
\r
175 _syncEvent.Reset();
\r
181 public async Task ProcessAccountFiles(AccountInfo accountInfo, DateTime? since = null)
\r
183 if (accountInfo == null)
\r
184 throw new ArgumentNullException("accountInfo");
\r
185 if (String.IsNullOrWhiteSpace(accountInfo.AccountPath))
\r
186 throw new ArgumentException("The AccountInfo.AccountPath is empty", "accountInfo");
\r
187 Contract.EndContractBlock();
\r
190 using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push(accountInfo.UserName))
\r
192 await NetworkAgent.GetDeleteAwaiter();
\r
194 Log.Info("Scheduled");
\r
195 var client = new CloudFilesClient(accountInfo);
\r
197 //We don't need to check the trash container
\r
198 var containers = client.ListContainers(accountInfo.UserName).Where(c=>c.Name!="trash");
\r
201 CreateContainerFolders(accountInfo, containers);
\r
205 //Wait for any deletions to finish
\r
206 await NetworkAgent.GetDeleteAwaiter();
\r
207 //Get the poll time now. We may miss some deletions but it's better to keep a file that was deleted
\r
208 //than delete a file that was created while we were executing the poll
\r
209 var pollTime = DateTime.Now;
\r
211 //Get the list of server objects changed since the last check
\r
212 //The name of the container is passed as state in order to create a dictionary of tasks in a subsequent step
\r
213 var listObjects = (from container in containers
\r
214 select Task<IList<ObjectInfo>>.Factory.StartNew(_ =>
\r
215 client.ListObjects(accountInfo.UserName, container.Name, since), container.Name)).ToList();
\r
216 //BUG: Can't detect difference between no changes or no objects
\r
217 //ListObjects returns nothing if there are no changes since the last check time (since value)
\r
218 //TODO: Must detect the difference between no server objects and no change
\r
220 //NOTE: One option is to "mark" all result lists with their container name, or
\r
221 //rather the url of the container
\r
224 var listShared = Task<IList<ObjectInfo>>.Factory.StartNew(_ =>
\r
225 client.ListSharedObjects(since), "shared");
\r
226 listObjects.Add(listShared);
\r
227 var listTasks = await Task.Factory.WhenAll(listObjects.ToArray());
\r
229 using (log4net.ThreadContext.Stacks["SCHEDULE"].Push("Process Results"))
\r
231 var dict = listTasks.ToDictionary(t => t.AsyncState);
\r
233 //Get all non-trash objects. Remember, the container name is stored in AsyncState
\r
234 var remoteObjects = from objectList in listTasks
\r
235 where (string)objectList.AsyncState != "trash"
\r
236 from obj in objectList.Result
\r
239 var sharedObjects = dict["shared"].Result;
\r
241 //DON'T process trashed files
\r
242 //If some files are deleted and added again to a folder, they will be deleted
\r
243 //even though they are new.
\r
244 //We would have to check file dates and hashes to ensure that a trashed file
\r
245 //can be deleted safely from the local hard drive.
\r
247 //Items with the same name, hash may be both in the container and the trash
\r
248 //Don't delete items that exist in the container
\r
249 var realTrash = from trash in trashObjects
\r
251 !remoteObjects.Any(
\r
252 info => info.Name == trash.Name && info.Hash == trash.Hash)
\r
254 ProcessTrashedFiles(accountInfo, realTrash);
\r
257 var cleanRemotes = (from info in remoteObjects.Union(sharedObjects)
\r
258 let name = info.Name??""
\r
259 where !name.EndsWith(".ignore", StringComparison.InvariantCultureIgnoreCase) &&
\r
260 !name.StartsWith(FolderConstants.CacheFolder + "/",
\r
261 StringComparison.InvariantCultureIgnoreCase)
\r
262 select info).ToList();
\r
264 var differencer = _differencer.PostSnapshot(accountInfo, cleanRemotes);
\r
266 ProcessDeletedFiles(accountInfo, differencer.Deleted.FilterDirectlyBelow(SelectiveUris), pollTime);
\r
268 // @@@ NEED To add previous state here as well, To compare with previous hash
\r
272 //Create a list of actions from the remote files
\r
273 var allActions = MovesToActions(accountInfo,differencer.Moved.FilterDirectlyBelow(SelectiveUris))
\r
275 ChangesToActions(accountInfo, differencer.Changed.FilterDirectlyBelow(SelectiveUris)))
\r
277 CreatesToActions(accountInfo, differencer.Created.FilterDirectlyBelow(SelectiveUris)));
\r
279 //And remove those that are already being processed by the agent
\r
280 var distinctActions = allActions
\r
281 .Except(NetworkAgent.GetEnumerable(), new PithosMonitor.LocalFileComparer())
\r
284 //Queue all the actions
\r
285 foreach (var message in distinctActions)
\r
287 NetworkAgent.Post(message);
\r
290 Log.Info("[LISTENER] End Processing");
\r
293 catch (Exception ex)
\r
295 Log.ErrorFormat("[FAIL] ListObjects for{0} in ProcessRemoteFiles with {1}", accountInfo.UserName, ex);
\r
299 Log.Info("[LISTENER] Finished");
\r
304 AccountsDifferencer _differencer = new AccountsDifferencer();
\r
305 private List<Uri> _selectiveUris=new List<Uri>();
\r
308 /// Deletes local files that are not found in the list of cloud files
\r
310 /// <param name="accountInfo"></param>
\r
311 /// <param name="cloudFiles"></param>
\r
312 /// <param name="pollTime"></param>
\r
313 private void ProcessDeletedFiles(AccountInfo accountInfo, IEnumerable<ObjectInfo> cloudFiles, DateTime pollTime)
\r
315 if (accountInfo == null)
\r
316 throw new ArgumentNullException("accountInfo");
\r
317 if (String.IsNullOrWhiteSpace(accountInfo.AccountPath))
\r
318 throw new ArgumentException("The AccountInfo.AccountPath is empty", "accountInfo");
\r
319 if (cloudFiles == null)
\r
320 throw new ArgumentNullException("cloudFiles");
\r
321 Contract.EndContractBlock();
\r
326 //Only consider files that are not being modified, ie they are in the Unchanged state
\r
327 var deleteCandidates = FileState.Queryable.Where(state =>
\r
328 state.FilePath.StartsWith(accountInfo.AccountPath)
\r
329 && state.FileStatus == FileStatus.Unchanged).ToList();
\r
332 //TODO: filesToDelete must take into account the Others container
\r
333 var filesToDelete = (from deleteCandidate in deleteCandidates
\r
334 let localFile = FileInfoExtensions.FromPath(deleteCandidate.FilePath)
\r
335 let relativeFilePath = localFile.AsRelativeTo(accountInfo.AccountPath)
\r
337 !cloudFiles.Any(r => r.RelativeUrlToFilePath(accountInfo.UserName) == relativeFilePath)
\r
338 select localFile).ToList();
\r
342 //Set the status of missing files to Conflict
\r
343 foreach (var item in filesToDelete)
\r
345 //Try to acquire a gate on the file, to take into account files that have been dequeued
\r
346 //and are being processed
\r
347 using (var gate = NetworkGate.Acquire(item.FullName, NetworkOperation.Deleting))
\r
351 StatusKeeper.SetFileState(item.FullName, FileStatus.Conflict, FileOverlayStatus.Deleted);
\r
354 UpdateStatus(PithosStatus.HasConflicts);
\r
355 StatusNotification.NotifyConflicts(filesToDelete, String.Format("{0} local files are missing from Pithos, possibly because they were deleted", filesToDelete.Count));
\r
356 StatusNotification.NotifyForFiles(filesToDelete, String.Format("{0} files were deleted", filesToDelete.Count), TraceLevel.Info);
\r
360 var deletedFiles = new List<FileSystemInfo>();
\r
361 foreach (var objectInfo in cloudFiles)
\r
363 var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
\r
364 var item = FileAgent.GetFileAgent(accountInfo).GetFileSystemInfo(relativePath);
\r
367 if ((item.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
\r
369 item.Attributes = item.Attributes & ~FileAttributes.ReadOnly;
\r
374 _lastSeen.TryRemove(item.FullName, out lastDate);
\r
375 deletedFiles.Add(item);
\r
377 StatusKeeper.SetFileState(item.FullName, FileStatus.Deleted, FileOverlayStatus.Deleted);
\r
379 StatusNotification.NotifyForFiles(deletedFiles, String.Format("{0} files were deleted", deletedFiles.Count), TraceLevel.Info);
\r
385 /// Creates a Sync action for each changed server file
\r
387 /// <param name="accountInfo"></param>
\r
388 /// <param name="changes"></param>
\r
389 /// <returns></returns>
\r
390 private IEnumerable<CloudAction> ChangesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> changes)
\r
392 if (changes == null)
\r
393 throw new ArgumentNullException();
\r
394 Contract.EndContractBlock();
\r
395 var fileAgent = FileAgent.GetFileAgent(accountInfo);
\r
397 //In order to avoid multiple iterations over the files, we iterate only once
\r
398 //over the remote files
\r
399 foreach (var objectInfo in changes)
\r
401 var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
\r
402 //If a directory object already exists, we may need to sync it
\r
403 if (fileAgent.Exists(relativePath))
\r
405 var localFile = fileAgent.GetFileSystemInfo(relativePath);
\r
406 //We don't need to sync directories
\r
407 if (objectInfo.Content_Type == @"application/directory" && localFile is DirectoryInfo)
\r
409 using (new SessionScope(FlushAction.Never))
\r
411 var state = StatusKeeper.GetStateByFilePath(localFile.FullName);
\r
412 _lastSeen[localFile.FullName] = DateTime.Now;
\r
413 //Common files should be checked on a per-case basis to detect differences, which is newer
\r
415 yield return new CloudAction(accountInfo, CloudActionType.MustSynch,
\r
416 localFile, objectInfo, state, accountInfo.BlockSize,
\r
417 accountInfo.BlockHash);
\r
422 //Remote files should be downloaded
\r
423 yield return new CloudDownloadAction(accountInfo, objectInfo);
\r
429 /// Creates a Local Move action for each moved server file
\r
431 /// <param name="accountInfo"></param>
\r
432 /// <param name="moves"></param>
\r
433 /// <returns></returns>
\r
434 private IEnumerable<CloudAction> MovesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> moves)
\r
437 throw new ArgumentNullException();
\r
438 Contract.EndContractBlock();
\r
439 var fileAgent = FileAgent.GetFileAgent(accountInfo);
\r
441 //In order to avoid multiple iterations over the files, we iterate only once
\r
442 //over the remote files
\r
443 foreach (var objectInfo in moves)
\r
445 var previousRelativepath = objectInfo.Previous.RelativeUrlToFilePath(accountInfo.UserName);
\r
446 //If the previous file already exists, we can execute a Move operation
\r
447 if (fileAgent.Exists(previousRelativepath))
\r
449 var previousFile = fileAgent.GetFileSystemInfo(previousRelativepath);
\r
450 using (new SessionScope(FlushAction.Never))
\r
452 var state = StatusKeeper.GetStateByFilePath(previousFile.FullName);
\r
453 _lastSeen[previousFile.FullName] = DateTime.Now;
\r
455 //For each moved object we need to move both the local file and update
\r
456 yield return new CloudAction(accountInfo, CloudActionType.RenameLocal,
\r
457 previousFile, objectInfo, state, accountInfo.BlockSize,
\r
458 accountInfo.BlockHash);
\r
459 //For modified files, we need to download the changes as well
\r
460 if (objectInfo.Hash!=objectInfo.PreviousHash)
\r
461 yield return new CloudDownloadAction(accountInfo,objectInfo);
\r
464 //If the previous file does not exist, we need to download it in the new location
\r
467 //Remote files should be downloaded
\r
468 yield return new CloudDownloadAction(accountInfo, objectInfo);
\r
475 /// Creates a download action for each new server file
\r
477 /// <param name="accountInfo"></param>
\r
478 /// <param name="creates"></param>
\r
479 /// <returns></returns>
\r
480 private IEnumerable<CloudAction> CreatesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> creates)
\r
482 if (creates == null)
\r
483 throw new ArgumentNullException();
\r
484 Contract.EndContractBlock();
\r
485 var fileAgent = FileAgent.GetFileAgent(accountInfo);
\r
487 //In order to avoid multiple iterations over the files, we iterate only once
\r
488 //over the remote files
\r
489 foreach (var objectInfo in creates)
\r
491 var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
\r
492 //If the object already exists, we probably have a conflict
\r
493 if (fileAgent.Exists(relativePath))
\r
495 //If a directory object already exists, we don't need to perform any other action
\r
496 var localFile = fileAgent.GetFileSystemInfo(relativePath);
\r
497 StatusKeeper.SetFileState(localFile.FullName, FileStatus.Conflict, FileOverlayStatus.Conflict);
\r
501 //Remote files should be downloaded
\r
502 yield return new CloudDownloadAction(accountInfo, objectInfo);
\r
507 private void ProcessTrashedFiles(AccountInfo accountInfo, IEnumerable<ObjectInfo> trashObjects)
\r
509 var fileAgent = FileAgent.GetFileAgent(accountInfo);
\r
510 foreach (var trashObject in trashObjects)
\r
512 var barePath = trashObject.RelativeUrlToFilePath(accountInfo.UserName);
\r
513 //HACK: Assume only the "pithos" container is used. Must find out what happens when
\r
514 //deleting a file from a different container
\r
515 var relativePath = Path.Combine("pithos", barePath);
\r
516 fileAgent.Delete(relativePath);
\r
521 /// Notify the UI to update the visual status
\r
523 /// <param name="status"></param>
\r
524 private void UpdateStatus(PithosStatus status)
\r
528 StatusKeeper.SetPithosStatus(status);
\r
529 StatusNotification.Notify(new Notification());
\r
531 catch (Exception exc)
\r
533 //Failure is not critical, just log it
\r
534 Log.Warn("Error while updating status", exc);
\r
538 private static void CreateContainerFolders(AccountInfo accountInfo, IEnumerable<ContainerInfo> containers)
\r
540 var containerPaths = from container in containers
\r
541 let containerPath = Path.Combine(accountInfo.AccountPath, container.Name)
\r
542 where container.Name != FolderConstants.TrashContainer && !Directory.Exists(containerPath)
\r
543 select containerPath;
\r
545 foreach (var path in containerPaths)
\r
547 Directory.CreateDirectory(path);
\r
551 public void SetSyncUris(Uri[] uris)
\r
553 SelectiveUris=uris.ToList();
\r
556 protected List<Uri> SelectiveUris
\r
558 get { return _selectiveUris;}
\r
559 set { _selectiveUris = value; }
\r
562 public void AddAccount(AccountInfo accountInfo)
\r
564 if (!_accounts.Contains(accountInfo))
\r
565 _accounts.Add(accountInfo);
\r