Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / Agents / PollAgent.cs @ 759bd3c4

History | View | Annotate | Download (21.9 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
        //Remote files are polled periodically. Any changes are processed
103
        public async Task PollRemoteFiles(DateTime? since = null)
104
        {
105
            Debug.Assert(Thread.CurrentThread.IsBackground, "Polling Ended up in the main thread!");
106

    
107
            UpdateStatus(PithosStatus.Syncing);
108
            StatusNotification.Notify(new PollNotification());
109

    
110
            using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push("All accounts"))
111
            {
112
                //If this poll fails, we will retry with the same since value
113
                var nextSince = since;
114
                try
115
                {
116
                    //Next time we will check for all changes since the current check minus 1 second
117
                    //This is done to ensure there are no discrepancies due to clock differences
118
                    var current = DateTime.Now.AddSeconds(-1);
119

    
120
                    var tasks = from accountInfo in _accounts
121
                                select ProcessAccountFiles(accountInfo, since);
122

    
123
                    await TaskEx.WhenAll(tasks.ToList());
124

    
125
                    _firstPoll = false;
126
                    //Reschedule the poll with the current timestamp as a "since" value
127
                    nextSince = current;
128
                }
129
                catch (Exception ex)
130
                {
131
                    Log.ErrorFormat("Error while processing accounts\r\n{0}", ex);
132
                    //In case of failure retry with the same "since" value
133
                }
134

    
135
                UpdateStatus(PithosStatus.InSynch);
136
                //Wait for the polling interval to pass or the Sync event to be signalled
137
                nextSince = await WaitForScheduledOrManualPoll(nextSince);
138

    
139
                TaskEx.Run(()=>PollRemoteFiles(nextSince));
140

    
141
            }
142
        }
143

    
144
        /// <summary>
145
        /// Wait for the polling period to expire or a manual sync request
146
        /// </summary>
147
        /// <param name="since"></param>
148
        /// <returns></returns>
149
        private async Task<DateTime?> WaitForScheduledOrManualPoll(DateTime? since)
150
        {
151
            var sync = _syncEvent.WaitAsync();
152
            var wait = TaskEx.Delay(TimeSpan.FromSeconds(Settings.PollingInterval), NetworkAgent.CancellationToken);
153
            var signaledTask = await TaskEx.WhenAny(sync, wait);
154

    
155
            //Wait for network processing to finish before polling
156
            var pauseTask=NetworkAgent.ProceedEvent.WaitAsync();
157
            await TaskEx.WhenAll(signaledTask, pauseTask);
158

    
159
            //If polling is signalled by SynchNow, ignore the since tag
160
            if (sync.IsCompleted)
161
            {
162
                //TODO: Must convert to AutoReset
163
                _syncEvent.Reset();
164
                return null;
165
            }
166
            return since;
167
        }
168

    
169
        public async Task ProcessAccountFiles(AccountInfo accountInfo, DateTime? since = null)
170
        {
171
            if (accountInfo == null)
172
                throw new ArgumentNullException("accountInfo");
173
            if (String.IsNullOrWhiteSpace(accountInfo.AccountPath))
174
                throw new ArgumentException("The AccountInfo.AccountPath is empty", "accountInfo");
175
            Contract.EndContractBlock();
176

    
177

    
178
            using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push(accountInfo.UserName))
179
            {
180
                await NetworkAgent.GetDeleteAwaiter();
181

    
182
                Log.Info("Scheduled");
183
                var client = new CloudFilesClient(accountInfo);
184

    
185
                var containers = client.ListContainers(accountInfo.UserName);
186

    
187

    
188
                CreateContainerFolders(accountInfo, containers);
189

    
190
                try
191
                {
192
                    //Wait for any deletions to finish
193
                    await NetworkAgent.GetDeleteAwaiter();
194
                    //Get the poll time now. We may miss some deletions but it's better to keep a file that was deleted
195
                    //than delete a file that was created while we were executing the poll                    
196
                    var pollTime = DateTime.Now;
197

    
198
                    //Get the list of server objects changed since the last check
199
                    //The name of the container is passed as state in order to create a dictionary of tasks in a subsequent step
200
                    var listObjects = (from container in containers
201
                                       select Task<IList<ObjectInfo>>.Factory.StartNew(_ =>
202
                                             client.ListObjects(accountInfo.UserName, container.Name, since), container.Name)).ToList();
203

    
204
                    var listShared = Task<IList<ObjectInfo>>.Factory.StartNew(_ => client.ListSharedObjects(since), "shared");
205
                    listObjects.Add(listShared);
206
                    var listTasks = await Task.Factory.WhenAll(listObjects.ToArray());
207

    
208
                    using (log4net.ThreadContext.Stacks["SCHEDULE"].Push("Process Results"))
209
                    {
210
                        var dict = listTasks.ToDictionary(t => t.AsyncState);
211

    
212
                        //Get all non-trash objects. Remember, the container name is stored in AsyncState
213
                        var remoteObjects = from objectList in listTasks
214
                                            where (string)objectList.AsyncState != "trash"
215
                                            from obj in objectList.Result
216
                                            select obj;
217

    
218
                        var trashObjects = dict["trash"].Result;
219
                        var sharedObjects = dict["shared"].Result;
220

    
221
                        //DON'T process trashed files
222
                        //If some files are deleted and added again to a folder, they will be deleted
223
                        //even though they are new.
224
                        //We would have to check file dates and hashes to ensure that a trashed file
225
                        //can be deleted safely from the local hard drive.
226
                        /*
227
                        //Items with the same name, hash may be both in the container and the trash
228
                        //Don't delete items that exist in the container
229
                        var realTrash = from trash in trashObjects
230
                                        where
231
                                            !remoteObjects.Any(
232
                                                info => info.Name == trash.Name && info.Hash == trash.Hash)
233
                                        select trash;
234
                        ProcessTrashedFiles(accountInfo, realTrash);
235
*/
236

    
237
                        var cleanRemotes = (from info in remoteObjects.Union(sharedObjects)
238
                                            let name = info.Name
239
                                            where !name.EndsWith(".ignore", StringComparison.InvariantCultureIgnoreCase) &&
240
                                                  !name.StartsWith(FolderConstants.CacheFolder + "/",
241
                                                                   StringComparison.InvariantCultureIgnoreCase)
242
                                            select info).ToList();
243

    
244
                        var differencer = _differencer.PostSnapshot(accountInfo, cleanRemotes);
245

    
246
                        ProcessDeletedFiles(accountInfo, differencer.Deleted.FilterBelow(SelectiveUris), pollTime);
247

    
248
                        // @@@ NEED To add previous state here as well, To compare with previous hash
249

    
250
                        
251

    
252
                        //Create a list of actions from the remote files
253
                        var allActions = ChangesToActions(accountInfo, differencer.Changed.FilterBelow(SelectiveUris))
254
                                        .Union(
255
                                        CreatesToActions(accountInfo, differencer.Created.FilterBelow(SelectiveUris)));
256

    
257
                        //And remove those that are already being processed by the agent
258
                        var distinctActions = allActions
259
                            .Except(NetworkAgent.GetEnumerable(), new PithosMonitor.LocalFileComparer())
260
                            .ToList();
261

    
262
                        //Queue all the actions
263
                        foreach (var message in distinctActions)
264
                        {
265
                            NetworkAgent.Post(message);
266
                        }
267

    
268
                        Log.Info("[LISTENER] End Processing");
269
                    }
270
                }
271
                catch (Exception ex)
272
                {
273
                    Log.ErrorFormat("[FAIL] ListObjects for{0} in ProcessRemoteFiles with {1}", accountInfo.UserName, ex);
274
                    return;
275
                }
276

    
277
                Log.Info("[LISTENER] Finished");
278

    
279
            }
280
        }
281

    
282
        AccountsDifferencer _differencer = new AccountsDifferencer();
283
        private List<Uri> _selectiveUris=new List<Uri>();
284

    
285
        /// <summary>
286
        /// Deletes local files that are not found in the list of cloud files
287
        /// </summary>
288
        /// <param name="accountInfo"></param>
289
        /// <param name="cloudFiles"></param>
290
        /// <param name="pollTime"></param>
291
        private void ProcessDeletedFiles(AccountInfo accountInfo, IEnumerable<ObjectInfo> cloudFiles, DateTime pollTime)
292
        {
293
            if (accountInfo == null)
294
                throw new ArgumentNullException("accountInfo");
295
            if (String.IsNullOrWhiteSpace(accountInfo.AccountPath))
296
                throw new ArgumentException("The AccountInfo.AccountPath is empty", "accountInfo");
297
            if (cloudFiles == null)
298
                throw new ArgumentNullException("cloudFiles");
299
            Contract.EndContractBlock();
300

    
301
            //On the first run
302
            if (_firstPoll)
303
            {
304
                //Only consider files that are not being modified, ie they are in the Unchanged state            
305
                var deleteCandidates = FileState.Queryable.Where(state =>
306
                    state.FilePath.StartsWith(accountInfo.AccountPath)
307
                    && state.FileStatus == FileStatus.Unchanged).ToList();
308

    
309

    
310
                //TODO: filesToDelete must take into account the Others container            
311
                var filesToDelete = (from deleteCandidate in deleteCandidates
312
                                     let localFile = FileInfoExtensions.FromPath(deleteCandidate.FilePath)
313
                                     let relativeFilePath = localFile.AsRelativeTo(accountInfo.AccountPath)
314
                                     where
315
                                         !cloudFiles.Any(r => r.RelativeUrlToFilePath(accountInfo.UserName) == relativeFilePath)
316
                                     select localFile).ToList();
317

    
318

    
319

    
320
                //Set the status of missing files to Conflict
321
                foreach (var item in filesToDelete)
322
                {
323
                    //Try to acquire a gate on the file, to take into account files that have been dequeued
324
                    //and are being processed
325
                    using (var gate = NetworkGate.Acquire(item.FullName, NetworkOperation.Deleting))
326
                    {
327
                        if (gate.Failed)
328
                            continue;
329
                        StatusKeeper.SetFileState(item.FullName, FileStatus.Conflict, FileOverlayStatus.Deleted);
330
                    }
331
                }
332
                UpdateStatus(PithosStatus.HasConflicts);
333
                StatusNotification.NotifyConflicts(filesToDelete, String.Format("{0} local files are missing from Pithos, possibly because they were deleted", filesToDelete.Count));
334
                StatusNotification.NotifyForFiles(filesToDelete, String.Format("{0} files were deleted", filesToDelete.Count), TraceLevel.Info);
335
            }
336
            else
337
            {
338
                var deletedFiles = new List<FileSystemInfo>();
339
                foreach (var objectInfo in cloudFiles)
340
                {
341
                    var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
342
                    var item = FileAgent.GetFileAgent(accountInfo).GetFileSystemInfo(relativePath);
343
                    if (item.Exists)
344
                    {
345
                        if ((item.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
346
                        {
347
                            item.Attributes = item.Attributes & ~FileAttributes.ReadOnly;
348

    
349
                        }
350
                        item.Delete();
351
                        DateTime lastDate;
352
                        _lastSeen.TryRemove(item.FullName, out lastDate);
353
                        deletedFiles.Add(item);
354
                    }
355
                    StatusKeeper.SetFileState(item.FullName, FileStatus.Deleted, FileOverlayStatus.Deleted);
356
                }
357
                StatusNotification.NotifyForFiles(deletedFiles, String.Format("{0} files were deleted", deletedFiles.Count), TraceLevel.Info);
358
            }
359

    
360
        }
361

    
362
        //Creates an appropriate action for each server file
363
        private IEnumerable<CloudAction> ChangesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> changes)
364
        {
365
            if (changes == null)
366
                throw new ArgumentNullException();
367
            Contract.EndContractBlock();
368
            var fileAgent = FileAgent.GetFileAgent(accountInfo);
369

    
370
            //In order to avoid multiple iterations over the files, we iterate only once
371
            //over the remote files
372
            foreach (var objectInfo in changes)
373
            {
374
                var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
375
                //and remove any matching objects from the list, adding them to the commonObjects list
376
                if (fileAgent.Exists(relativePath))
377
                {
378
                    //If a directory object already exists, we don't need to perform any other action                    
379
                    var localFile = fileAgent.GetFileSystemInfo(relativePath);
380
                    if (objectInfo.Content_Type == @"application/directory" && localFile is DirectoryInfo)
381
                        continue;
382
                    using (new SessionScope(FlushAction.Never))
383
                    {
384
                        var state = StatusKeeper.GetStateByFilePath(localFile.FullName);
385
                        _lastSeen[localFile.FullName] = DateTime.Now;
386
                        //Common files should be checked on a per-case basis to detect differences, which is newer
387

    
388
                        yield return new CloudAction(accountInfo, CloudActionType.MustSynch,
389
                                                     localFile, objectInfo, state, accountInfo.BlockSize,
390
                                                     accountInfo.BlockHash);
391
                    }
392
                }
393
                else
394
                {
395
                    //Remote files should be downloaded
396
                    yield return new CloudDownloadAction(accountInfo, objectInfo);
397
                }
398
            }
399
        }
400

    
401
        private IEnumerable<CloudAction> CreatesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> creates)
402
        {
403
            if (creates == null)
404
                throw new ArgumentNullException();
405
            Contract.EndContractBlock();
406
            var fileAgent = FileAgent.GetFileAgent(accountInfo);
407

    
408
            //In order to avoid multiple iterations over the files, we iterate only once
409
            //over the remote files
410
            foreach (var objectInfo in creates)
411
            {
412
                var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
413
                //and remove any matching objects from the list, adding them to the commonObjects list
414
                if (fileAgent.Exists(relativePath))
415
                {
416
                    //If the object already exists, we probably have a conflict
417
                    //If a directory object already exists, we don't need to perform any other action                    
418
                    var localFile = fileAgent.GetFileSystemInfo(relativePath);
419
                    StatusKeeper.SetFileState(localFile.FullName, FileStatus.Conflict, FileOverlayStatus.Conflict);
420
                }
421
                else
422
                {
423
                    //Remote files should be downloaded
424
                    yield return new CloudDownloadAction(accountInfo, objectInfo);
425
                }
426
            }
427
        }
428

    
429
        private void ProcessTrashedFiles(AccountInfo accountInfo, IEnumerable<ObjectInfo> trashObjects)
430
        {
431
            var fileAgent = FileAgent.GetFileAgent(accountInfo);
432
            foreach (var trashObject in trashObjects)
433
            {
434
                var barePath = trashObject.RelativeUrlToFilePath(accountInfo.UserName);
435
                //HACK: Assume only the "pithos" container is used. Must find out what happens when
436
                //deleting a file from a different container
437
                var relativePath = Path.Combine("pithos", barePath);
438
                fileAgent.Delete(relativePath);
439
            }
440
        }
441

    
442
        private void UpdateStatus(PithosStatus status)
443
        {
444
            StatusKeeper.SetPithosStatus(status);
445
            StatusNotification.Notify(new Notification());
446
        }
447

    
448
        private static void CreateContainerFolders(AccountInfo accountInfo, IEnumerable<ContainerInfo> containers)
449
        {
450
            var containerPaths = from container in containers
451
                                 let containerPath = Path.Combine(accountInfo.AccountPath, container.Name)
452
                                 where container.Name != FolderConstants.TrashContainer && !Directory.Exists(containerPath)
453
                                 select containerPath;
454

    
455
            foreach (var path in containerPaths)
456
            {
457
                Directory.CreateDirectory(path);
458
            }
459
        }
460

    
461
        public void SetSyncUris(string[] uris)
462
        {
463
            var selectiveUris = uris.Select(uri => new Uri(uri));
464
            SelectiveUris=selectiveUris.ToList();
465
        }
466

    
467
        protected List<Uri> SelectiveUris
468
        {
469
            get { return _selectiveUris;}
470
            set { _selectiveUris = value; }
471
        }
472
    }
473
}