Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / Agents / PollAgent.cs @ 174bbb6e

History | View | Annotate | Download (26 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.Reflection;
49
using System.Threading;
50
using System.Threading.Tasks;
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

    
62
    /// <summary>
63
    /// PollAgent periodically polls the server to detect object changes. The agent retrieves a listing of all
64
    /// objects and compares it with a previously cached version to detect differences. 
65
    /// New files are downloaded, missing files are deleted from the local file system and common files are compared
66
    /// to determine the appropriate action
67
    /// </summary>
68
    [Export]
69
    public class PollAgent
70
    {
71
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
72

    
73
        [System.ComponentModel.Composition.Import]
74
        public IStatusKeeper StatusKeeper { get; set; }
75

    
76
        [System.ComponentModel.Composition.Import]
77
        public IPithosSettings Settings { get; set; }
78

    
79
        [System.ComponentModel.Composition.Import]
80
        public NetworkAgent NetworkAgent { get; set; }
81

    
82
        public IStatusNotification StatusNotification { get; set; }
83

    
84
        private bool _firstPoll = true;
85

    
86
        //The Sync Event signals a manual synchronisation
87
        private readonly AsyncManualResetEvent _syncEvent = new AsyncManualResetEvent();
88

    
89
        private readonly ConcurrentDictionary<string, DateTime> _lastSeen = new ConcurrentDictionary<string, DateTime>();
90
        private readonly ConcurrentDictionary<string, AccountInfo> _accounts = new ConcurrentDictionary<string,AccountInfo>();
91

    
92

    
93
        /// <summary>
94
        /// Start a manual synchronization
95
        /// </summary>
96
        public void SynchNow()
97
        {            
98
            _syncEvent.Set();
99
        }
100

    
101
        /// <summary>
102
        /// Remote files are polled periodically. Any changes are processed
103
        /// </summary>
104
        /// <param name="since"></param>
105
        /// <returns></returns>
106
        public async Task PollRemoteFiles(DateTime? since = null)
107
        {
108
            if (Log.IsDebugEnabled)
109
                Log.DebugFormat("Polling changes after [{0}]",since);
110

    
111
            Debug.Assert(Thread.CurrentThread.IsBackground, "Polling Ended up in the main thread!");
112
            
113

    
114
            using (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
                    UpdateStatus(PithosStatus.PollSyncing);
121

    
122
                    //Next time we will check for all changes since the current check minus 1 second
123
                    //This is done to ensure there are no discrepancies due to clock differences
124
                    var current = DateTime.Now.AddSeconds(-1);
125

    
126
                    var tasks = from accountInfo in _accounts.Values
127
                                select ProcessAccountFiles(accountInfo, since);
128

    
129
                    await TaskEx.WhenAll(tasks.ToList());
130

    
131
                    _firstPoll = false;
132
                    //Reschedule the poll with the current timestamp as a "since" value
133
                    nextSince = current;
134
                }
135
                catch (Exception ex)
136
                {
137
                    Log.ErrorFormat("Error while processing accounts\r\n{0}", ex);
138
                    //In case of failure retry with the same "since" value
139
                }
140

    
141
                UpdateStatus(PithosStatus.PollComplete);
142
                //The multiple try blocks are required because we can't have an await call
143
                //inside a finally block
144
                //TODO: Find a more elegant solution for reschedulling in the event of an exception
145
                try
146
                {
147
                    //Wait for the polling interval to pass or the Sync event to be signalled
148
                    nextSince = await WaitForScheduledOrManualPoll(nextSince);
149
                }
150
                finally
151
                {
152
                    //Ensure polling is scheduled even in case of error
153
                    TaskEx.Run(() => PollRemoteFiles(nextSince));                        
154
                }
155
            }
156
        }
157

    
158
        /// <summary>
159
        /// Wait for the polling period to expire or a manual sync request
160
        /// </summary>
161
        /// <param name="since"></param>
162
        /// <returns></returns>
163
        private async Task<DateTime?> WaitForScheduledOrManualPoll(DateTime? since)
164
        {
165
            var sync = _syncEvent.WaitAsync();
166
            var wait = TaskEx.Delay(TimeSpan.FromSeconds(Settings.PollingInterval), NetworkAgent.CancellationToken);
167
            var signaledTask = await TaskEx.WhenAny(sync, wait);
168

    
169
            //Wait for network processing to finish before polling
170
            var pauseTask=NetworkAgent.ProceedEvent.WaitAsync();
171
            await TaskEx.WhenAll(signaledTask, pauseTask);
172

    
173
            //If polling is signalled by SynchNow, ignore the since tag
174
            if (sync.IsCompleted)
175
            {
176
                //TODO: Must convert to AutoReset
177
                _syncEvent.Reset();
178
                return null;
179
            }
180
            return since;
181
        }
182

    
183
        public async Task ProcessAccountFiles(AccountInfo accountInfo, DateTime? since = null)
184
        {
185
            if (accountInfo == null)
186
                throw new ArgumentNullException("accountInfo");
187
            if (String.IsNullOrWhiteSpace(accountInfo.AccountPath))
188
                throw new ArgumentException("The AccountInfo.AccountPath is empty", "accountInfo");
189
            Contract.EndContractBlock();
190

    
191

    
192
            using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push(accountInfo.UserName))
193
            {
194
                await NetworkAgent.GetDeleteAwaiter();
195

    
196
                Log.Info("Scheduled");
197
                var client = new CloudFilesClient(accountInfo);
198

    
199
                //We don't need to check the trash container
200
                var containers = client.ListContainers(accountInfo.UserName)
201
                    .Where(c=>c.Name!="trash")
202
                    .ToList();
203

    
204

    
205
                CreateContainerFolders(accountInfo, containers);
206

    
207
                try
208
                {
209
                    //Wait for any deletions to finish
210
                    await NetworkAgent.GetDeleteAwaiter();
211
                    //Get the poll time now. We may miss some deletions but it's better to keep a file that was deleted
212
                    //than delete a file that was created while we were executing the poll                    
213

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

    
220
                    var listShared = Task<IList<ObjectInfo>>.Factory.StartNew(_ => 
221
                        client.ListSharedObjects(since), "shared");
222
                    listObjects.Add(listShared);
223
                    var listTasks = await Task.Factory.WhenAll(listObjects.ToArray());
224

    
225
                    using (log4net.ThreadContext.Stacks["SCHEDULE"].Push("Process Results"))
226
                    {
227
                        var dict = listTasks.ToDictionary(t => t.AsyncState);
228

    
229
                        //Get all non-trash objects. Remember, the container name is stored in AsyncState
230
                        var remoteObjects = from objectList in listTasks
231
                                            where (string)objectList.AsyncState != "trash"
232
                                            from obj in objectList.Result
233
                                            select obj;
234

    
235
                        var sharedObjects = dict["shared"].Result;
236

    
237
                        //DON'T process trashed files
238
                        //If some files are deleted and added again to a folder, they will be deleted
239
                        //even though they are new.
240
                        //We would have to check file dates and hashes to ensure that a trashed file
241
                        //can be deleted safely from the local hard drive.
242
                        /*
243
                        //Items with the same name, hash may be both in the container and the trash
244
                        //Don't delete items that exist in the container
245
                        var realTrash = from trash in trashObjects
246
                                        where
247
                                            !remoteObjects.Any(
248
                                                info => info.Name == trash.Name && info.Hash == trash.Hash)
249
                                        select trash;
250
                        ProcessTrashedFiles(accountInfo, realTrash);
251
*/
252

    
253
                        var cleanRemotes = (from info in remoteObjects.Union(sharedObjects)
254
                                            let name = info.Name??""
255
                                            where !name.EndsWith(".ignore", StringComparison.InvariantCultureIgnoreCase) &&
256
                                                  !name.StartsWith(FolderConstants.CacheFolder + "/",
257
                                                                   StringComparison.InvariantCultureIgnoreCase)
258
                                            select info).ToList();
259

    
260
                        var differencer = _differencer.PostSnapshot(accountInfo, cleanRemotes);
261

    
262
                        ProcessDeletedFiles(accountInfo, differencer.Deleted.FilterDirectlyBelow(SelectiveUris));
263

    
264
                        // @@@ NEED To add previous state here as well, To compare with previous hash
265

    
266
                        
267

    
268
                        //Create a list of actions from the remote files
269
                        var allActions = MovesToActions(accountInfo,differencer.Moved.FilterDirectlyBelow(SelectiveUris))
270
                                        .Union(
271
                                        ChangesToActions(accountInfo, differencer.Changed.FilterDirectlyBelow(SelectiveUris)))
272
                                        .Union(
273
                                        CreatesToActions(accountInfo, differencer.Created.FilterDirectlyBelow(SelectiveUris)));
274

    
275
                        //And remove those that are already being processed by the agent
276
                        var distinctActions = allActions
277
                            .Except(NetworkAgent.GetEnumerable(), new PithosMonitor.LocalFileComparer())
278
                            .ToList();
279

    
280
                        //Queue all the actions
281
                        foreach (var message in distinctActions)
282
                        {
283
                            NetworkAgent.Post(message);
284
                        }
285

    
286
                        Log.Info("[LISTENER] End Processing");
287
                    }
288
                }
289
                catch (Exception ex)
290
                {
291
                    Log.ErrorFormat("[FAIL] ListObjects for{0} in ProcessRemoteFiles with {1}", accountInfo.UserName, ex);
292
                    return;
293
                }
294

    
295
                Log.Info("[LISTENER] Finished");
296

    
297
            }
298
        }
299

    
300
        readonly AccountsDifferencer _differencer = new AccountsDifferencer();
301
        private List<Uri> _selectiveUris=new List<Uri>();
302

    
303
        /// <summary>
304
        /// Deletes local files that are not found in the list of cloud files
305
        /// </summary>
306
        /// <param name="accountInfo"></param>
307
        /// <param name="cloudFiles"></param>
308
        private void ProcessDeletedFiles(AccountInfo accountInfo, IEnumerable<ObjectInfo> cloudFiles)
309
        {
310
            if (accountInfo == null)
311
                throw new ArgumentNullException("accountInfo");
312
            if (String.IsNullOrWhiteSpace(accountInfo.AccountPath))
313
                throw new ArgumentException("The AccountInfo.AccountPath is empty", "accountInfo");
314
            if (cloudFiles == null)
315
                throw new ArgumentNullException("cloudFiles");
316
            Contract.EndContractBlock();
317

    
318
            //On the first run
319
            if (_firstPoll)
320
            {
321
                //Only consider files that are not being modified, ie they are in the Unchanged state            
322
                var deleteCandidates = FileState.Queryable.Where(state =>
323
                    state.FilePath.StartsWith(accountInfo.AccountPath)
324
                    && state.FileStatus == FileStatus.Unchanged).ToList();
325

    
326

    
327
                //TODO: filesToDelete must take into account the Others container            
328
                var filesToDelete = (from deleteCandidate in deleteCandidates
329
                                     let localFile = FileInfoExtensions.FromPath(deleteCandidate.FilePath)
330
                                     let relativeFilePath = localFile.AsRelativeTo(accountInfo.AccountPath)
331
                                     where
332
                                         !cloudFiles.Any(r => r.RelativeUrlToFilePath(accountInfo.UserName) == relativeFilePath)
333
                                     select localFile).ToList();
334

    
335

    
336

    
337
                //Set the status of missing files to Conflict
338
                foreach (var item in filesToDelete)
339
                {
340
                    //Try to acquire a gate on the file, to take into account files that have been dequeued
341
                    //and are being processed
342
                    using (var gate = NetworkGate.Acquire(item.FullName, NetworkOperation.Deleting))
343
                    {
344
                        if (gate.Failed)
345
                            continue;
346
                        StatusKeeper.SetFileState(item.FullName, FileStatus.Conflict, FileOverlayStatus.Deleted);
347
                    }
348
                }
349
                UpdateStatus(PithosStatus.HasConflicts);
350
                StatusNotification.NotifyConflicts(filesToDelete, String.Format("{0} local files are missing from Pithos, possibly because they were deleted", filesToDelete.Count));
351
                StatusNotification.NotifyForFiles(filesToDelete, String.Format("{0} files were deleted", filesToDelete.Count), TraceLevel.Info);
352
            }
353
            else
354
            {
355
                var deletedFiles = new List<FileSystemInfo>();
356
                foreach (var objectInfo in cloudFiles)
357
                {
358
                    Log.DebugFormat("Handle deleted [{0}]",objectInfo.Uri);
359
                    var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
360
                    var item = FileAgent.GetFileAgent(accountInfo).GetFileSystemInfo(relativePath);
361
                    Log.DebugFormat("Will delete [{0}] for [{1}]", item.FullName,objectInfo.Uri);
362
                    if (item.Exists)
363
                    {
364
                        if ((item.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
365
                        {
366
                            item.Attributes = item.Attributes & ~FileAttributes.ReadOnly;
367

    
368
                        }
369
                        var directory = item as DirectoryInfo;
370
                        if (directory!=null)
371
                            directory.Delete(true);
372
                        else
373
                            item.Delete();
374
                        Log.DebugFormat("Deleted [{0}] for [{1}]", item.FullName, objectInfo.Uri);
375
                        DateTime lastDate;
376
                        _lastSeen.TryRemove(item.FullName, out lastDate);
377
                        deletedFiles.Add(item);
378
                    }
379
                    StatusKeeper.SetFileState(item.FullName, FileStatus.Deleted, FileOverlayStatus.Deleted);
380
                }
381
                Log.InfoFormat("[{0}] files were deleted",deletedFiles.Count);
382
                StatusNotification.NotifyForFiles(deletedFiles, String.Format("{0} files were deleted", deletedFiles.Count), TraceLevel.Info);
383
            }
384

    
385
        }
386

    
387
        /// <summary>
388
        /// Creates a Sync action for each changed server file
389
        /// </summary>
390
        /// <param name="accountInfo"></param>
391
        /// <param name="changes"></param>
392
        /// <returns></returns>
393
        private IEnumerable<CloudAction> ChangesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> changes)
394
        {
395
            if (changes == null)
396
                throw new ArgumentNullException();
397
            Contract.EndContractBlock();
398
            var fileAgent = FileAgent.GetFileAgent(accountInfo);
399

    
400
            //In order to avoid multiple iterations over the files, we iterate only once
401
            //over the remote files
402
            foreach (var objectInfo in changes)
403
            {
404
                var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
405
                //If a directory object already exists, we may need to sync it
406
                if (fileAgent.Exists(relativePath))
407
                {
408
                    var localFile = fileAgent.GetFileSystemInfo(relativePath);
409
                    //We don't need to sync directories
410
                    if (objectInfo.Content_Type == @"application/directory" && localFile is DirectoryInfo)
411
                        continue;
412
                    using (new SessionScope(FlushAction.Never))
413
                    {
414
                        var state = StatusKeeper.GetStateByFilePath(localFile.FullName);
415
                        _lastSeen[localFile.FullName] = DateTime.Now;
416
                        //Common files should be checked on a per-case basis to detect differences, which is newer
417

    
418
                        yield return new CloudAction(accountInfo, CloudActionType.MustSynch,
419
                                                     localFile, objectInfo, state, accountInfo.BlockSize,
420
                                                     accountInfo.BlockHash);
421
                    }
422
                }
423
                else
424
                {
425
                    //Remote files should be downloaded
426
                    yield return new CloudDownloadAction(accountInfo, objectInfo);
427
                }
428
            }
429
        }
430

    
431
        /// <summary>
432
        /// Creates a Local Move action for each moved server file
433
        /// </summary>
434
        /// <param name="accountInfo"></param>
435
        /// <param name="moves"></param>
436
        /// <returns></returns>
437
        private IEnumerable<CloudAction> MovesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> moves)
438
        {
439
            if (moves == null)
440
                throw new ArgumentNullException();
441
            Contract.EndContractBlock();
442
            var fileAgent = FileAgent.GetFileAgent(accountInfo);
443

    
444
            //In order to avoid multiple iterations over the files, we iterate only once
445
            //over the remote files
446
            foreach (var objectInfo in moves)
447
            {
448
                var previousRelativepath = objectInfo.Previous.RelativeUrlToFilePath(accountInfo.UserName);
449
                //If the previous file already exists, we can execute a Move operation
450
                if (fileAgent.Exists(previousRelativepath))
451
                {
452
                    var previousFile = fileAgent.GetFileSystemInfo(previousRelativepath);
453
                    using (new SessionScope(FlushAction.Never))
454
                    {
455
                        var state = StatusKeeper.GetStateByFilePath(previousFile.FullName);
456
                        _lastSeen[previousFile.FullName] = DateTime.Now;
457

    
458
                        //For each moved object we need to move both the local file and update                                                
459
                        yield return new CloudAction(accountInfo, CloudActionType.RenameLocal,
460
                                                     previousFile, objectInfo, state, accountInfo.BlockSize,
461
                                                     accountInfo.BlockHash);
462
                        //For modified files, we need to download the changes as well
463
                        if (objectInfo.Hash!=objectInfo.PreviousHash)
464
                            yield return new CloudDownloadAction(accountInfo,objectInfo);
465
                    }
466
                }
467
                //If the previous file does not exist, we need to download it in the new location
468
                else
469
                {
470
                    //Remote files should be downloaded
471
                    yield return new CloudDownloadAction(accountInfo, objectInfo);
472
                }
473
            }
474
        }
475

    
476

    
477
        /// <summary>
478
        /// Creates a download action for each new server file
479
        /// </summary>
480
        /// <param name="accountInfo"></param>
481
        /// <param name="creates"></param>
482
        /// <returns></returns>
483
        private IEnumerable<CloudAction> CreatesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> creates)
484
        {
485
            if (creates == null)
486
                throw new ArgumentNullException();
487
            Contract.EndContractBlock();
488
            var fileAgent = FileAgent.GetFileAgent(accountInfo);
489

    
490
            //In order to avoid multiple iterations over the files, we iterate only once
491
            //over the remote files
492
            foreach (var objectInfo in creates)
493
            {
494
                var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
495
                //If the object already exists, we probably have a conflict
496
                if (fileAgent.Exists(relativePath))
497
                {
498
                    //If a directory object already exists, we don't need to perform any other action                    
499
                    var localFile = fileAgent.GetFileSystemInfo(relativePath);
500
                    StatusKeeper.SetFileState(localFile.FullName, FileStatus.Conflict, FileOverlayStatus.Conflict);
501
                }
502
                else
503
                {
504
                    //Remote files should be downloaded
505
                    yield return new CloudDownloadAction(accountInfo, objectInfo);
506
                }
507
            }
508
        }
509

    
510
        /// <summary>
511
        /// Notify the UI to update the visual status
512
        /// </summary>
513
        /// <param name="status"></param>
514
        private void UpdateStatus(PithosStatus status)
515
        {
516
            try
517
            {
518
                StatusNotification.SetPithosStatus(status);
519
                //StatusNotification.Notify(new Notification());
520
            }
521
            catch (Exception exc)
522
            {
523
                //Failure is not critical, just log it
524
                Log.Warn("Error while updating status", exc);
525
            }
526
        }
527

    
528
        private static void CreateContainerFolders(AccountInfo accountInfo, IEnumerable<ContainerInfo> containers)
529
        {
530
            var containerPaths = from container in containers
531
                                 let containerPath = Path.Combine(accountInfo.AccountPath, container.Name)
532
                                 where container.Name != FolderConstants.TrashContainer && !Directory.Exists(containerPath)
533
                                 select containerPath;
534

    
535
            foreach (var path in containerPaths)
536
            {
537
                Directory.CreateDirectory(path);
538
            }
539
        }
540

    
541
        public void SetSyncUris(Uri[] uris)
542
        {            
543
            SelectiveUris=uris.ToList();
544
        }
545

    
546
        protected List<Uri> SelectiveUris
547
        {
548
            get { return _selectiveUris;}
549
            set { _selectiveUris = value; }
550
        }
551

    
552
        public void AddAccount(AccountInfo accountInfo)
553
        {
554
            //Avoid adding a duplicate accountInfo
555
            _accounts.TryAdd(accountInfo.UserName, accountInfo);
556
        }
557

    
558
        public void RemoveAccount(AccountInfo accountInfo)
559
        {
560
            AccountInfo account;
561
            _accounts.TryRemove(accountInfo.UserName,out account);
562
        }
563
    }
564
}