Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / Agents / PollAgent.cs @ ec1a1baf

History | View | Annotate | Download (25.3 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 Castle.ActiveRecord;
51
using Pithos.Interfaces;
52
using Pithos.Network;
53
using log4net;
54

    
55
namespace Pithos.Core.Agents
56
{
57
    using System;
58
    using System.Collections.Generic;
59
    using System.Linq;
60

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

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

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

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

    
81
        public IStatusNotification StatusNotification { get; set; }
82

    
83
        private bool _firstPoll = true;
84

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

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

    
91

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

    
100
        /// <summary>
101
        /// Remote files are polled periodically. Any changes are processed
102
        /// </summary>
103
        /// <param name="since"></param>
104
        /// <returns></returns>
105
        public async Task PollRemoteFiles(DateTime? since = null)
106
        {
107
            Debug.Assert(Thread.CurrentThread.IsBackground, "Polling Ended up in the main thread!");
108

    
109
            UpdateStatus(PithosStatus.Syncing);
110
            StatusNotification.Notify(new PollNotification());
111

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

    
122
                    var tasks = from accountInfo in _accounts.Values
123
                                select ProcessAccountFiles(accountInfo, since);
124

    
125
                    await TaskEx.WhenAll(tasks.ToList());
126

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

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

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

    
165
            //Wait for network processing to finish before polling
166
            var pauseTask=NetworkAgent.ProceedEvent.WaitAsync();
167
            await TaskEx.WhenAll(signaledTask, pauseTask);
168

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

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

    
187

    
188
            using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push(accountInfo.UserName))
189
            {
190
                await NetworkAgent.GetDeleteAwaiter();
191

    
192
                Log.Info("Scheduled");
193
                var client = new CloudFilesClient(accountInfo);
194

    
195
                //We don't need to check the trash container
196
                var containers = client.ListContainers(accountInfo.UserName)
197
                    .Where(c=>c.Name!="trash")
198
                    .ToList();
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

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

    
216
                    var listShared = Task<IList<ObjectInfo>>.Factory.StartNew(_ => 
217
                        client.ListSharedObjects(since), "shared");
218
                    listObjects.Add(listShared);
219
                    var listTasks = await Task.Factory.WhenAll(listObjects.ToArray());
220

    
221
                    using (log4net.ThreadContext.Stacks["SCHEDULE"].Push("Process Results"))
222
                    {
223
                        var dict = listTasks.ToDictionary(t => t.AsyncState);
224

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

    
231
                        var sharedObjects = dict["shared"].Result;
232

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

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

    
256
                        var differencer = _differencer.PostSnapshot(accountInfo, cleanRemotes);
257

    
258
                        ProcessDeletedFiles(accountInfo, differencer.Deleted.FilterDirectlyBelow(SelectiveUris));
259

    
260
                        // @@@ NEED To add previous state here as well, To compare with previous hash
261

    
262
                        
263

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

    
271
                        //And remove those that are already being processed by the agent
272
                        var distinctActions = allActions
273
                            .Except(NetworkAgent.GetEnumerable(), new PithosMonitor.LocalFileComparer())
274
                            .ToList();
275

    
276
                        //Queue all the actions
277
                        foreach (var message in distinctActions)
278
                        {
279
                            NetworkAgent.Post(message);
280
                        }
281

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

    
291
                Log.Info("[LISTENER] Finished");
292

    
293
            }
294
        }
295

    
296
        readonly AccountsDifferencer _differencer = new AccountsDifferencer();
297
        private List<Uri> _selectiveUris=new List<Uri>();
298

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

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

    
322

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

    
331

    
332

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

    
362
                        }
363
                        item.Delete();
364
                        DateTime lastDate;
365
                        _lastSeen.TryRemove(item.FullName, out lastDate);
366
                        deletedFiles.Add(item);
367
                    }
368
                    StatusKeeper.SetFileState(item.FullName, FileStatus.Deleted, FileOverlayStatus.Deleted);
369
                }
370
                StatusNotification.NotifyForFiles(deletedFiles, String.Format("{0} files were deleted", deletedFiles.Count), TraceLevel.Info);
371
            }
372

    
373
        }
374

    
375
        /// <summary>
376
        /// Creates a Sync action for each changed server file
377
        /// </summary>
378
        /// <param name="accountInfo"></param>
379
        /// <param name="changes"></param>
380
        /// <returns></returns>
381
        private IEnumerable<CloudAction> ChangesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> changes)
382
        {
383
            if (changes == null)
384
                throw new ArgumentNullException();
385
            Contract.EndContractBlock();
386
            var fileAgent = FileAgent.GetFileAgent(accountInfo);
387

    
388
            //In order to avoid multiple iterations over the files, we iterate only once
389
            //over the remote files
390
            foreach (var objectInfo in changes)
391
            {
392
                var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
393
                //If a directory object already exists, we may need to sync it
394
                if (fileAgent.Exists(relativePath))
395
                {
396
                    var localFile = fileAgent.GetFileSystemInfo(relativePath);
397
                    //We don't need to sync directories
398
                    if (objectInfo.Content_Type == @"application/directory" && localFile is DirectoryInfo)
399
                        continue;
400
                    using (new SessionScope(FlushAction.Never))
401
                    {
402
                        var state = StatusKeeper.GetStateByFilePath(localFile.FullName);
403
                        _lastSeen[localFile.FullName] = DateTime.Now;
404
                        //Common files should be checked on a per-case basis to detect differences, which is newer
405

    
406
                        yield return new CloudAction(accountInfo, CloudActionType.MustSynch,
407
                                                     localFile, objectInfo, state, accountInfo.BlockSize,
408
                                                     accountInfo.BlockHash);
409
                    }
410
                }
411
                else
412
                {
413
                    //Remote files should be downloaded
414
                    yield return new CloudDownloadAction(accountInfo, objectInfo);
415
                }
416
            }
417
        }
418

    
419
        /// <summary>
420
        /// Creates a Local Move action for each moved server file
421
        /// </summary>
422
        /// <param name="accountInfo"></param>
423
        /// <param name="moves"></param>
424
        /// <returns></returns>
425
        private IEnumerable<CloudAction> MovesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> moves)
426
        {
427
            if (moves == null)
428
                throw new ArgumentNullException();
429
            Contract.EndContractBlock();
430
            var fileAgent = FileAgent.GetFileAgent(accountInfo);
431

    
432
            //In order to avoid multiple iterations over the files, we iterate only once
433
            //over the remote files
434
            foreach (var objectInfo in moves)
435
            {
436
                var previousRelativepath = objectInfo.Previous.RelativeUrlToFilePath(accountInfo.UserName);
437
                //If the previous file already exists, we can execute a Move operation
438
                if (fileAgent.Exists(previousRelativepath))
439
                {
440
                    var previousFile = fileAgent.GetFileSystemInfo(previousRelativepath);
441
                    using (new SessionScope(FlushAction.Never))
442
                    {
443
                        var state = StatusKeeper.GetStateByFilePath(previousFile.FullName);
444
                        _lastSeen[previousFile.FullName] = DateTime.Now;
445

    
446
                        //For each moved object we need to move both the local file and update                                                
447
                        yield return new CloudAction(accountInfo, CloudActionType.RenameLocal,
448
                                                     previousFile, objectInfo, state, accountInfo.BlockSize,
449
                                                     accountInfo.BlockHash);
450
                        //For modified files, we need to download the changes as well
451
                        if (objectInfo.Hash!=objectInfo.PreviousHash)
452
                            yield return new CloudDownloadAction(accountInfo,objectInfo);
453
                    }
454
                }
455
                //If the previous file does not exist, we need to download it in the new location
456
                else
457
                {
458
                    //Remote files should be downloaded
459
                    yield return new CloudDownloadAction(accountInfo, objectInfo);
460
                }
461
            }
462
        }
463

    
464

    
465
        /// <summary>
466
        /// Creates a download action for each new server file
467
        /// </summary>
468
        /// <param name="accountInfo"></param>
469
        /// <param name="creates"></param>
470
        /// <returns></returns>
471
        private IEnumerable<CloudAction> CreatesToActions(AccountInfo accountInfo, IEnumerable<ObjectInfo> creates)
472
        {
473
            if (creates == null)
474
                throw new ArgumentNullException();
475
            Contract.EndContractBlock();
476
            var fileAgent = FileAgent.GetFileAgent(accountInfo);
477

    
478
            //In order to avoid multiple iterations over the files, we iterate only once
479
            //over the remote files
480
            foreach (var objectInfo in creates)
481
            {
482
                var relativePath = objectInfo.RelativeUrlToFilePath(accountInfo.UserName);
483
                //If the object already exists, we probably have a conflict
484
                if (fileAgent.Exists(relativePath))
485
                {
486
                    //If a directory object already exists, we don't need to perform any other action                    
487
                    var localFile = fileAgent.GetFileSystemInfo(relativePath);
488
                    StatusKeeper.SetFileState(localFile.FullName, FileStatus.Conflict, FileOverlayStatus.Conflict);
489
                }
490
                else
491
                {
492
                    //Remote files should be downloaded
493
                    yield return new CloudDownloadAction(accountInfo, objectInfo);
494
                }
495
            }
496
        }
497

    
498
        /// <summary>
499
        /// Notify the UI to update the visual status
500
        /// </summary>
501
        /// <param name="status"></param>
502
        private void UpdateStatus(PithosStatus status)
503
        {
504
            try
505
            {
506
                StatusKeeper.SetPithosStatus(status);
507
                StatusNotification.Notify(new Notification());
508
            }
509
            catch (Exception exc)
510
            {
511
                //Failure is not critical, just log it
512
                Log.Warn("Error while updating status", exc);
513
            }
514
        }
515

    
516
        private static void CreateContainerFolders(AccountInfo accountInfo, IEnumerable<ContainerInfo> containers)
517
        {
518
            var containerPaths = from container in containers
519
                                 let containerPath = Path.Combine(accountInfo.AccountPath, container.Name)
520
                                 where container.Name != FolderConstants.TrashContainer && !Directory.Exists(containerPath)
521
                                 select containerPath;
522

    
523
            foreach (var path in containerPaths)
524
            {
525
                Directory.CreateDirectory(path);
526
            }
527
        }
528

    
529
        public void SetSyncUris(Uri[] uris)
530
        {            
531
            SelectiveUris=uris.ToList();
532
        }
533

    
534
        protected List<Uri> SelectiveUris
535
        {
536
            get { return _selectiveUris;}
537
            set { _selectiveUris = value; }
538
        }
539

    
540
        public void AddAccount(AccountInfo accountInfo)
541
        {
542
            //Avoid adding a duplicate accountInfo
543
            _accounts.TryAdd(accountInfo.UserName, accountInfo);
544
        }
545

    
546
        public void RemoveAccount(AccountInfo accountInfo)
547
        {
548
            AccountInfo account;
549
            _accounts.TryRemove(accountInfo.UserName,out account);
550
        }
551
    }
552
}