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
102 //Remote files are polled periodically. Any changes are processed
\r
103 public async Task PollRemoteFiles(DateTime? since = null)
\r
105 Debug.Assert(Thread.CurrentThread.IsBackground, "Polling Ended up in the main thread!");
\r
107 UpdateStatus(PithosStatus.Syncing);
\r
108 StatusNotification.Notify(new PollNotification());
\r
110 using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push("All accounts"))
\r
112 //If this poll fails, we will retry with the same since value
\r
113 var nextSince = since;
\r
116 //Next time we will check for all changes since the current check minus 1 second
\r
117 //This is done to ensure there are no discrepancies due to clock differences
\r
118 var current = DateTime.Now.AddSeconds(-1);
\r
120 var tasks = from accountInfo in _accounts
\r
121 select ProcessAccountFiles(accountInfo, since);
\r
123 await TaskEx.WhenAll(tasks.ToList());
\r
125 _firstPoll = false;
\r
126 //Reschedule the poll with the current timestamp as a "since" value
\r
127 nextSince = current;
\r
129 catch (Exception ex)
\r
131 Log.ErrorFormat("Error while processing accounts\r\n{0}", ex);
\r
132 //In case of failure retry with the same "since" value
\r
135 UpdateStatus(PithosStatus.InSynch);
\r
136 //Wait for the polling interval to pass or the Sync event to be signalled
\r
137 nextSince = await WaitForScheduledOrManualPoll(nextSince);
\r
139 TaskEx.Run(()=>PollRemoteFiles(nextSince));
\r
145 /// Wait for the polling period to expire or a manual sync request
\r
147 /// <param name="since"></param>
\r
148 /// <returns></returns>
\r
149 private async Task<DateTime?> WaitForScheduledOrManualPoll(DateTime? since)
\r
151 var sync = _syncEvent.WaitAsync();
\r
152 var wait = TaskEx.Delay(TimeSpan.FromSeconds(Settings.PollingInterval), NetworkAgent.CancellationToken);
\r
153 var signaledTask = await TaskEx.WhenAny(sync, wait);
\r
155 //Wait for network processing to finish before polling
\r
156 var pauseTask=NetworkAgent.ProceedEvent.WaitAsync();
\r
157 await TaskEx.WhenAll(signaledTask, pauseTask);
\r
159 //If polling is signalled by SynchNow, ignore the since tag
\r
160 if (sync.IsCompleted)
\r
162 //TODO: Must convert to AutoReset
\r
163 _syncEvent.Reset();
\r
169 public async Task ProcessAccountFiles(AccountInfo accountInfo, DateTime? since = null)
\r
171 if (accountInfo == null)
\r
172 throw new ArgumentNullException("accountInfo");
\r
173 if (String.IsNullOrWhiteSpace(accountInfo.AccountPath))
\r
174 throw new ArgumentException("The AccountInfo.AccountPath is empty", "accountInfo");
\r
175 Contract.EndContractBlock();
\r
178 using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push(accountInfo.UserName))
\r
180 await NetworkAgent.GetDeleteAwaiter();
\r
182 Log.Info("Scheduled");
\r
183 var client = new CloudFilesClient(accountInfo);
\r
185 var containers = client.ListContainers(accountInfo.UserName);
\r
188 CreateContainerFolders(accountInfo, containers);
\r
192 await NetworkAgent.GetDeleteAwaiter();
\r
193 //Get the poll time now. We may miss some deletions but it's better to keep a file that was deleted
\r
194 //than delete a file that was created while we were executing the poll
\r
195 var pollTime = DateTime.Now;
\r
197 //Get the list of server objects changed since the last check
\r
198 //The name of the container is passed as state in order to create a dictionary of tasks in a subsequent step
\r
199 var listObjects = (from container in containers
\r
200 select Task<IList<ObjectInfo>>.Factory.StartNew(_ =>
\r
201 client.ListObjects(accountInfo.UserName, container.Name, since), container.Name)).ToList();
\r
203 var listShared = Task<IList<ObjectInfo>>.Factory.StartNew(_ => client.ListSharedObjects(since), "shared");
\r
204 listObjects.Add(listShared);
\r
205 var listTasks = await Task.Factory.WhenAll(listObjects.ToArray());
\r
207 using (log4net.ThreadContext.Stacks["SCHEDULE"].Push("Process Results"))
\r
209 var dict = listTasks.ToDictionary(t => t.AsyncState);
\r
211 //Get all non-trash objects. Remember, the container name is stored in AsyncState
\r
212 var remoteObjects = from objectList in listTasks
\r
213 where (string)objectList.AsyncState != "trash"
\r
214 from obj in objectList.Result
\r
217 var trashObjects = dict["trash"].Result;
\r
218 var sharedObjects = dict["shared"].Result;
\r
220 //DON'T process trashed files
\r
221 //If some files are deleted and added again to a folder, they will be deleted
\r
222 //even though they are new.
\r
223 //We would have to check file dates and hashes to ensure that a trashed file
\r
224 //can be deleted safely from the local hard drive.
\r
226 //Items with the same name, hash may be both in the container and the trash
\r
227 //Don't delete items that exist in the container
\r
228 var realTrash = from trash in trashObjects
\r
230 !remoteObjects.Any(
\r
231 info => info.Name == trash.Name && info.Hash == trash.Hash)
\r
233 ProcessTrashedFiles(accountInfo, realTrash);
\r
236 var cleanRemotes = (from info in remoteObjects.Union(sharedObjects)
\r
237 let name = info.Name
\r
238 where !name.EndsWith(".ignore", StringComparison.InvariantCultureIgnoreCase) &&
\r
239 !name.StartsWith(FolderConstants.CacheFolder + "/",
\r
240 StringComparison.InvariantCultureIgnoreCase)
\r
241 select info).ToList();
\r
243 var differencer = _differencer.PostSnapshot(accountInfo, cleanRemotes);
\r
245 ProcessDeletedFiles(accountInfo, differencer.Deleted, pollTime);
\r
247 //Create a list of actions from the remote files
\r
248 var allActions = ChangesToActions(accountInfo, differencer.Changed)
\r
250 CreatesToActions(accountInfo, differencer.Created));
\r
252 //And remove those that are already being processed by the agent
\r
253 var distinctActions = allActions
\r
254 .Except(NetworkAgent.GetEnumerable(), new PithosMonitor.LocalFileComparer())
\r
257 //Queue all the actions
\r
258 foreach (var message in distinctActions)
\r
260 NetworkAgent.Post(message);
\r
263 Log.Info("[LISTENER] End Processing");
\r
266 catch (Exception ex)
\r
268 Log.ErrorFormat("[FAIL] ListObjects for{0} in ProcessRemoteFiles with {1}", accountInfo.UserName, ex);
\r
272 Log.Info("[LISTENER] Finished");
\r
277 AccountsDifferencer _differencer = new AccountsDifferencer();
\r
280 /// Deletes local files that are not found in the list of cloud files
\r
282 /// <param name="accountInfo"></param>
\r
283 /// <param name="cloudFiles"></param>
\r
284 /// <param name="pollTime"></param>
\r
285 private void ProcessDeletedFiles(AccountInfo accountInfo, IEnumerable<ObjectInfo> cloudFiles, DateTime pollTime)
\r
287 if (accountInfo == null)
\r
288 throw new ArgumentNullException("accountInfo");
\r
289 if (String.IsNullOrWhiteSpace(accountInfo.AccountPath))
\r
290 throw new ArgumentException("The AccountInfo.AccountPath is empty", "accountInfo");
\r
291 if (cloudFiles == null)
\r
292 throw new ArgumentNullException("cloudFiles");
\r
293 Contract.EndContractBlock();
\r
298 //Only consider files that are not being modified, ie they are in the Unchanged state
\r
299 var deleteCandidates = FileState.Queryable.Where(state =>
\r
300 state.FilePath.StartsWith(accountInfo.AccountPath)
\r
301 && state.FileStatus == FileStatus.Unchanged).ToList();
\r
304 //TODO: filesToDelete must take into account the Others container
\r
305 var filesToDelete = (from deleteCandidate in deleteCandidates
\r
306 let localFile = FileInfoExtensions.FromPath(deleteCandidate.FilePath)
\r
307 let relativeFilePath = localFile.AsRelativeTo(accountInfo.AccountPath)
\r
309 !cloudFiles.Any(r => r.RelativeUrlToFilePath(accountInfo.UserName) == relativeFilePath)
\r
310 select localFile).ToList();
\r
314 //Set the status of missing files to Conflict
\r
315 foreach (var item in filesToDelete)
\r
317 //Try to acquire a gate on the file, to take into account files that have been dequeued
\r
318 //and are being processed
\r
319 using (var gate = NetworkGate.Acquire(item.FullName, NetworkOperation.Deleting))
\r
323 StatusKeeper.SetFileState(item.FullName, FileStatus.Conflict, FileOverlayStatus.Deleted);
\r
326 UpdateStatus(PithosStatus.HasConflicts);
\r
327 StatusNotification.NotifyConflicts(filesToDelete, String.Format("{0} local files are missing from Pithos, possibly because they were deleted", filesToDelete.Count));
\r
328 StatusNotification.NotifyForFiles(filesToDelete, String.Format("{0} files were deleted", filesToDelete.Count), TraceLevel.Info);
\r
332 var deletedFiles = new List<FileSystemInfo>();
\r
333 foreach (var objectInfo in cloudFiles)
\r
335 var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
\r
336 var item = GetFileAgent(accountInfo).GetFileSystemInfo(relativePath);
\r
339 if ((item.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
\r
341 item.Attributes = item.Attributes & ~FileAttributes.ReadOnly;
\r
346 _lastSeen.TryRemove(item.FullName, out lastDate);
\r
347 deletedFiles.Add(item);
\r
349 StatusKeeper.SetFileState(item.FullName, FileStatus.Deleted, FileOverlayStatus.Deleted);
\r
351 StatusNotification.NotifyForFiles(deletedFiles, String.Format("{0} files were deleted", deletedFiles.Count), TraceLevel.Info);
\r
356 //Creates an appropriate action for each server file
\r
357 private IEnumerable<CloudAction> ChangesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> changes)
\r
359 if (changes == null)
\r
360 throw new ArgumentNullException();
\r
361 Contract.EndContractBlock();
\r
362 var fileAgent = GetFileAgent(accountInfo);
\r
364 //In order to avoid multiple iterations over the files, we iterate only once
\r
365 //over the remote files
\r
366 foreach (var objectInfo in changes)
\r
368 var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
\r
369 //and remove any matching objects from the list, adding them to the commonObjects list
\r
370 if (fileAgent.Exists(relativePath))
\r
372 //If a directory object already exists, we don't need to perform any other action
\r
373 var localFile = fileAgent.GetFileSystemInfo(relativePath);
\r
374 if (objectInfo.Content_Type == @"application/directory" && localFile is DirectoryInfo)
\r
376 using (new SessionScope(FlushAction.Never))
\r
378 var state = StatusKeeper.GetStateByFilePath(localFile.FullName);
\r
379 _lastSeen[localFile.FullName] = DateTime.Now;
\r
380 //Common files should be checked on a per-case basis to detect differences, which is newer
\r
382 yield return new CloudAction(accountInfo, CloudActionType.MustSynch,
\r
383 localFile, objectInfo, state, accountInfo.BlockSize,
\r
384 accountInfo.BlockHash);
\r
389 //Remote files should be downloaded
\r
390 yield return new CloudDownloadAction(accountInfo, objectInfo);
\r
395 private IEnumerable<CloudAction> CreatesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> creates)
\r
397 if (creates == null)
\r
398 throw new ArgumentNullException();
\r
399 Contract.EndContractBlock();
\r
400 var fileAgent = GetFileAgent(accountInfo);
\r
402 //In order to avoid multiple iterations over the files, we iterate only once
\r
403 //over the remote files
\r
404 foreach (var objectInfo in creates)
\r
406 var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
\r
407 //and remove any matching objects from the list, adding them to the commonObjects list
\r
408 if (fileAgent.Exists(relativePath))
\r
410 //If the object already exists, we probably have a conflict
\r
411 //If a directory object already exists, we don't need to perform any other action
\r
412 var localFile = fileAgent.GetFileSystemInfo(relativePath);
\r
413 StatusKeeper.SetFileState(localFile.FullName, FileStatus.Conflict, FileOverlayStatus.Conflict);
\r
417 //Remote files should be downloaded
\r
418 yield return new CloudDownloadAction(accountInfo, objectInfo);
\r
423 private static FileAgent GetFileAgent(AccountInfo accountInfo)
\r
425 return AgentLocator<FileAgent>.Get(accountInfo.AccountPath);
\r
428 private void ProcessTrashedFiles(AccountInfo accountInfo, IEnumerable<ObjectInfo> trashObjects)
\r
430 var fileAgent = GetFileAgent(accountInfo);
\r
431 foreach (var trashObject in trashObjects)
\r
433 var barePath = trashObject.RelativeUrlToFilePath(accountInfo.UserName);
\r
434 //HACK: Assume only the "pithos" container is used. Must find out what happens when
\r
435 //deleting a file from a different container
\r
436 var relativePath = Path.Combine("pithos", barePath);
\r
437 fileAgent.Delete(relativePath);
\r
441 private void UpdateStatus(PithosStatus status)
\r
443 StatusKeeper.SetPithosStatus(status);
\r
444 StatusNotification.Notify(new Notification());
\r
447 private static void CreateContainerFolders(AccountInfo accountInfo, IEnumerable<ContainerInfo> containers)
\r
449 var containerPaths = from container in containers
\r
450 let containerPath = Path.Combine(accountInfo.AccountPath, container.Name)
\r
451 where container.Name != FolderConstants.TrashContainer && !Directory.Exists(containerPath)
\r
452 select containerPath;
\r
454 foreach (var path in containerPaths)
\r
456 Directory.CreateDirectory(path);
\r