Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / Agents / NetworkAgent.cs @ e0f69809

History | View | Annotate | Download (22.7 kB)

1
#region
2
/* -----------------------------------------------------------------------
3
 * <copyright file="NetworkAgent.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;
44
using System.Collections.Generic;
45
using System.ComponentModel.Composition;
46
using System.Diagnostics;
47
using System.Diagnostics.Contracts;
48
using System.IO;
49
using System.Linq;
50
using System.Net;
51
using System.Reflection;
52
using System.Threading;
53
using System.Threading.Tasks;
54
using Castle.ActiveRecord;
55
using Pithos.Interfaces;
56
using Pithos.Network;
57
using log4net;
58

    
59
namespace Pithos.Core.Agents
60
{
61
    [Export]
62
    public class NetworkAgent
63
    {
64
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
65

    
66
        private Agent<CloudAction> _agent;
67

    
68
        [System.ComponentModel.Composition.Import]
69
        private DeleteAgent DeleteAgent { get; set; }
70

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

    
74
        private IStatusNotification _statusNotification;
75
        public IStatusNotification StatusNotification
76
        {
77
            get { return _statusNotification; }
78
            set
79
            {
80
                _statusNotification = value;
81
                DeleteAgent.StatusNotification = value;
82
                Uploader.StatusNotification = value;
83
                Downloader.StatusNotification = value;
84
            }
85
        }
86

    
87

    
88
        [System.ComponentModel.Composition.Import]
89
        public IPithosSettings Settings { get; set; }
90

    
91
        private Uploader _uploader;
92

    
93
        [System.ComponentModel.Composition.Import]
94
        public Uploader Uploader
95
        {
96
            get { return _uploader; }
97
            set
98
            {
99
                _uploader = value;
100
                _uploader.UnpauseEvent = _unPauseEvent;                
101
            }
102
        }
103

    
104
        private Downloader _downloader;
105

    
106
        [System.ComponentModel.Composition.Import]
107
        public Downloader Downloader
108
        {
109
            get { return _downloader; }
110
            set
111
            {
112
                _downloader = value;
113
                _downloader.UnpauseEvent = _unPauseEvent;
114
            }
115
        }
116

    
117
        [System.ComponentModel.Composition.Import]
118
        public Selectives Selectives { get; set; }
119
        
120
        //The Proceed signals the poll agent that it can proceed with polling. 
121
        //Essentially it stops the poll agent to give priority to the network agent
122
        //Initially the event is signalled because we don't need to pause
123
        private readonly AsyncManualResetEvent _proceedEvent = new AsyncManualResetEvent(true);
124
        private Agents.Selectives _selectives;
125
        private bool _pause;
126

    
127
        public AsyncManualResetEvent ProceedEvent
128
        {
129
            get { return _proceedEvent; }
130
        }
131

    
132
        private readonly AsyncManualResetEvent _unPauseEvent = new AsyncManualResetEvent(true);
133

    
134
        private CancellationTokenSource _currentOperationCancellation=new CancellationTokenSource();
135

    
136
        public void CancelCurrentOperation()
137
        {
138
            //What does it mean to cancel the current upload/download?
139
            //Obviously, the current operation will be cancelled by throwing
140
            //a cancellation exception.
141
            //
142
            //The default behavior is to retry any operations that throw.
143
            //Obviously this is not what we want in this situation.
144
            //The cancelled operation should NOT bea retried. 
145
            //
146
            //This can be done by catching the cancellation exception
147
            //and avoiding the retry.
148
            //
149

    
150
            //Have to reset the cancellation source - it is not possible to reset the source
151
            //Have to prevent a case where an operation requests a token from the old source
152
            var oldSource = Interlocked.Exchange(ref _currentOperationCancellation, new CancellationTokenSource());
153
            oldSource.Cancel();
154
            
155
        }
156

    
157
        public void Start()
158
        {
159
            if (_agent != null)
160
                return;
161

    
162
            if (Log.IsDebugEnabled)
163
                Log.Debug("Starting Network Agent");
164

    
165
            _agent = Agent<CloudAction>.Start(inbox =>
166
            {
167
                Action loop = null;
168
                loop = () =>
169
                {
170
                    DeleteAgent.ProceedEvent.Wait();
171
                    _unPauseEvent.Wait();
172
                    var message = inbox.Receive();
173
                    var process=message.Then(Process,inbox.CancellationToken);
174
                    inbox.LoopAsync(process, loop);
175
                };
176
                loop();
177
            });
178

    
179
        }
180

    
181
        private async Task Process(CloudAction action)
182
        {
183
            if (action == null)
184
                throw new ArgumentNullException("action");
185
            if (action.AccountInfo==null)
186
                throw new ArgumentException("The action.AccountInfo is empty","action");
187
            Contract.EndContractBlock();
188

    
189

    
190

    
191

    
192
            using (ThreadContext.Stacks["Operation"].Push(action.ToString()))
193
            {                
194

    
195
                var cloudFile = action.CloudFile;
196
                var downloadPath = action.GetDownloadPath();
197

    
198
                try
199
                {
200
                    StatusNotification.SetPithosStatus(PithosStatus.LocalSyncing,"Processing");
201
                    _proceedEvent.Reset();
202
                    
203
                    var accountInfo = action.AccountInfo;
204

    
205
                    if (action.Action == CloudActionType.DeleteCloud)
206
                    {                        
207
                        //Redirect deletes to the delete agent 
208
                        DeleteAgent.Post((CloudDeleteAction)action);
209
                    }
210
                    if (DeleteAgent.IsDeletedFile(action))
211
                    {
212
                        //Clear the status of already deleted files to avoid reprocessing
213
                        if (action.LocalFile != null)
214
                            StatusKeeper.ClearFileStatus(action.LocalFile.FullName);
215
                    }
216
                    else
217
                    {
218
                        switch (action.Action)
219
                        {
220
                            case CloudActionType.UploadUnconditional:
221
                                //Abort if the file was deleted before we reached this point
222
                                await Uploader.UploadCloudFile(action,CurrentOperationCancelToken);
223
                                break;
224
                            case CloudActionType.DownloadUnconditional:
225
                                await Downloader.DownloadCloudFile(accountInfo, cloudFile, downloadPath, CurrentOperationCancelToken);
226
                                break;
227
                            case CloudActionType.RenameCloud:
228
                                var moveAction = (CloudMoveAction)action;
229
                                RenameCloudFile(accountInfo, moveAction);
230
                                break;
231
                            case CloudActionType.RenameLocal:
232
                                RenameLocalFile(accountInfo, action);
233
                                break;
234
                            case CloudActionType.MustSynch:
235
                                if (!File.Exists(downloadPath) && !Directory.Exists(downloadPath))
236
                                {
237
                                    await Downloader.DownloadCloudFile(accountInfo, cloudFile, downloadPath, CurrentOperationCancelToken);
238
                                }
239
                                else
240
                                {
241
                                    await SyncFiles(accountInfo, action);
242
                                }
243
                                break;
244
                        }
245
                    }
246
                    Log.InfoFormat("End Processing {0}:{1}->{2}", action.Action, action.LocalFile,
247
                                           action.CloudFile.Name);
248
                }
249
/*
250
                catch (WebException exc)
251
                {                    
252
                    Log.ErrorFormat("[WEB ERROR] {0} : {1} -> {2} due to exception\r\n{3}", action.Action, action.LocalFile, action.CloudFile, exc);
253
                    
254
                    
255
                    //Actions that resulted in server errors should be retried                    
256
                    var response = exc.Response as HttpWebResponse;
257
                    if (response != null && response.StatusCode >= HttpStatusCode.InternalServerError)
258
                    {
259
                        _agent.Post(action);
260
                        Log.WarnFormat("[REQUEUE] {0} : {1} -> {2}", action.Action, action.LocalFile, action.CloudFile);
261
                    }
262
                }
263
*/
264
                catch (OperationCanceledException ex)
265
                {                    
266
                    Log.WarnFormat("Cancelling [{0}]",ex);
267
                }
268
                catch (DirectoryNotFoundException)
269
                {
270
                    Log.ErrorFormat("{0} : {1} -> {2}  failed because the directory was not found.\n Rescheduling a delete",
271
                        action.Action, action.LocalFile, action.CloudFile);
272
                    //Post a delete action for the missing file
273
                    Post(new CloudDeleteAction(action));
274
                }
275
                catch (FileNotFoundException)
276
                {
277
                    Log.ErrorFormat("{0} : {1} -> {2}  failed because the file was not found.\n Rescheduling a delete",
278
                        action.Action, action.LocalFile, action.CloudFile);
279
                    //Post a delete action for the missing file
280
                    Post(new CloudDeleteAction(action));
281
                }
282
                catch (Exception exc)
283
                {
284
                    Log.ErrorFormat("[REQUEUE] {0} : {1} -> {2} due to exception\r\n{3}",
285
                                     action.Action, action.LocalFile, action.CloudFile, exc);
286

    
287
                    _agent.Post(action);
288
                }
289
                finally
290
                {
291
                    if (_agent.IsEmpty)
292
                        _proceedEvent.Set();
293
                    UpdateStatus(PithosStatus.LocalComplete);                                        
294
                }
295
            }
296
        }
297

    
298
        private CancellationToken CurrentOperationCancelToken
299
        {
300
            get { return _currentOperationCancellation.Token; }
301
        }
302

    
303

    
304
        private void UpdateStatus(PithosStatus status)
305
        {
306
            StatusNotification.SetPithosStatus(status);
307
            //StatusNotification.Notify(new Notification());
308
        }
309

    
310
        private void RenameLocalFile(AccountInfo accountInfo, CloudAction action)
311
        {
312
            if (accountInfo == null)
313
                throw new ArgumentNullException("accountInfo");
314
            if (action == null)
315
                throw new ArgumentNullException("action");
316
            if (action.LocalFile == null)
317
                throw new ArgumentException("The action's local file is not specified", "action");
318
            if (!Path.IsPathRooted(action.LocalFile.FullName))
319
                throw new ArgumentException("The action's local file path must be absolute", "action");
320
            if (action.CloudFile == null)
321
                throw new ArgumentException("The action's cloud file is not specified", "action");
322
            Contract.EndContractBlock();
323
            using (ThreadContext.Stacks["Operation"].Push("RenameLocalFile"))
324
            {
325

    
326
                //We assume that the local file already exists, otherwise the poll agent
327
                //would have issued a download request
328

    
329
                var currentInfo = action.CloudFile;
330
                var previousInfo = action.CloudFile.Previous;
331
                var fileAgent = FileAgent.GetFileAgent(accountInfo);
332

    
333
                var previousRelativepath = previousInfo.RelativeUrlToFilePath(accountInfo.UserName);
334
                var previousFile = fileAgent.GetFileSystemInfo(previousRelativepath);
335

    
336
                //In every case we need to move the local file first
337
                MoveLocalFile(accountInfo, previousFile, fileAgent, currentInfo);
338
            }
339
        }
340

    
341
        private void MoveLocalFile(AccountInfo accountInfo, FileSystemInfo previousFile, FileAgent fileAgent,
342
                                   ObjectInfo currentInfo)
343
        {
344
            var currentRelativepath = currentInfo.RelativeUrlToFilePath(accountInfo.UserName);
345
            var newPath = Path.Combine(fileAgent.RootPath, currentRelativepath);
346

    
347
            var isFile= (previousFile is FileInfo);
348
            var previousFullPath = isFile? 
349
                FileInfoExtensions.GetProperFilePathCapitalization(previousFile.FullName):
350
                FileInfoExtensions.GetProperDirectoryCapitalization(previousFile.FullName);                
351
            
352
            using (NetworkGate.Acquire(previousFullPath, NetworkOperation.Renaming))
353
            using (NetworkGate.Acquire(newPath,NetworkOperation.Renaming)) 
354
            using (new SessionScope(FlushAction.Auto))
355
            {
356
                if (isFile)
357
                    (previousFile as FileInfo).MoveTo(newPath);
358
                else
359
                {
360
                    (previousFile as DirectoryInfo).MoveTo(newPath);
361
                }
362
                var state = StatusKeeper.GetStateByFilePath(previousFullPath);
363
                state.FilePath = newPath;
364
                state.SaveCopy();
365
                StatusKeeper.SetFileState(previousFullPath,FileStatus.Deleted,FileOverlayStatus.Deleted, "Deleted");
366
            }            
367
        }
368

    
369
        private async Task SyncFiles(AccountInfo accountInfo,CloudAction action)
370
        {
371
            if (accountInfo == null)
372
                throw new ArgumentNullException("accountInfo");
373
            if (action==null)
374
                throw new ArgumentNullException("action");
375
            if (action.LocalFile==null)
376
                throw new ArgumentException("The action's local file is not specified","action");
377
            if (!Path.IsPathRooted(action.LocalFile.FullName))
378
                throw new ArgumentException("The action's local file path must be absolute","action");
379
            if (action.CloudFile== null)
380
                throw new ArgumentException("The action's cloud file is not specified", "action");
381
            Contract.EndContractBlock();
382
            using (ThreadContext.Stacks["Operation"].Push("SyncFiles"))
383
            {
384

    
385
                //var localFile = action.LocalFile;
386
                var cloudFile = action.CloudFile;
387
                var downloadPath = action.LocalFile.GetProperCapitalization();
388

    
389
                var cloudHash = cloudFile.Hash.ToLower();
390
                var previousCloudHash = cloudFile.PreviousHash == null?null: cloudFile.PreviousHash.ToLower();
391
                var localHash = action.TreeHash.Value.TopHash.ToHashString();// LocalHash.Value.ToLower();
392
                //var topHash = action.TopHash.Value.ToLower();
393

    
394
                if(cloudFile.IsDirectory && action.LocalFile is DirectoryInfo)
395
                {
396
                    Log.InfoFormat("Skipping folder {0} , exists in server", downloadPath);
397
                    return;
398
                }
399

    
400
                //At this point we know that an object has changed on the server and that a local
401
                //file already exists. We need to decide whether the file has only changed on 
402
                //the server or there is a conflicting change on the client.
403
                //
404

    
405
                //If the hashes match, we are done
406
                if (cloudFile != ObjectInfo.Empty && cloudHash == localHash)
407
                {
408
                    Log.InfoFormat("Skipping {0}, hashes match", downloadPath);
409
                    return;
410
                }
411

    
412
                //If the local and remote files have 0 length their hashes will not match
413
                if (!cloudFile.IsDirectory && cloudFile.Bytes==0 && action.LocalFile is FileInfo && (action.LocalFile as FileInfo).Length==0 )
414
                {
415
                    Log.InfoFormat("Skipping {0}, files are empty", downloadPath);
416
                    return;
417
                }
418

    
419
                //The hashes DON'T match. We need to sync
420

    
421
                // If the previous tophash matches the local tophash, the file was only changed on the server. 
422
                if (localHash == previousCloudHash)
423
                {
424
                    await Downloader.DownloadCloudFile(accountInfo, cloudFile, downloadPath, CurrentOperationCancelToken);
425
                }
426
                else
427
                {
428
                    //If the previous and local hash don't match, there was a local conflict
429
                    //that was not uploaded to the server. We have a conflict
430
                    ReportConflictForMismatch(downloadPath);
431
                }
432
            }
433
        }
434

    
435
        private void ReportConflictForMismatch(string downloadPath)
436
        {
437
            if (String.IsNullOrWhiteSpace(downloadPath))
438
                throw new ArgumentNullException("downloadPath");
439
            Contract.EndContractBlock();
440

    
441
            StatusKeeper.SetFileState(downloadPath,FileStatus.Conflict, FileOverlayStatus.Conflict,"File changed at the server");
442
            UpdateStatus(PithosStatus.HasConflicts);
443
            var message = String.Format("Conflict detected for file {0}", downloadPath);
444
            Log.Warn(message);
445
            StatusNotification.NotifyChange(message, TraceLevel.Warning);
446
        }
447

    
448
        public void Post(CloudAction cloudAction)
449
        {
450
            if (cloudAction == null)
451
                throw new ArgumentNullException("cloudAction");
452
            if (cloudAction.AccountInfo==null)
453
                throw new ArgumentException("The CloudAction.AccountInfo is empty","cloudAction");
454
            Contract.EndContractBlock();
455

    
456
            DeleteAgent.ProceedEvent.Wait();
457
            
458
            if (cloudAction is CloudDeleteAction)
459
                DeleteAgent.Post((CloudDeleteAction)cloudAction);
460
            else
461
                _agent.Post(cloudAction);
462
        }
463
       
464

    
465
        public IEnumerable<CloudAction> GetEnumerable()
466
        {
467
            return _agent.GetEnumerable();
468
        }
469

    
470
        public Task GetDeleteAwaiter()
471
        {
472
            return DeleteAgent.ProceedEvent.WaitAsync();
473
        }
474
        public CancellationToken CancellationToken
475
        {
476
            get { return _agent.CancellationToken; }
477
        }
478

    
479
        public bool Pause
480
        {
481
            get {
482
                return _pause;
483
            }
484
            set {
485
                _pause = value;
486
                if (_pause)
487
                    _unPauseEvent.Reset();
488
                else
489
                {
490
                    _unPauseEvent.Set();
491
                }
492
            }
493
        }
494

    
495

    
496
        private void RenameCloudFile(AccountInfo accountInfo,CloudMoveAction action)
497
        {
498
            if (accountInfo==null)
499
                throw new ArgumentNullException("accountInfo");
500
            if (action==null)
501
                throw new ArgumentNullException("action");
502
            if (action.CloudFile==null)
503
                throw new ArgumentException("CloudFile","action");
504
            if (action.LocalFile==null)
505
                throw new ArgumentException("LocalFile","action");
506
            if (action.OldLocalFile==null)
507
                throw new ArgumentException("OldLocalFile","action");
508
            if (action.OldCloudFile==null)
509
                throw new ArgumentException("OldCloudFile","action");
510
            Contract.EndContractBlock();
511

    
512
            using (ThreadContext.Stacks["Operation"].Push("RenameCloudFile"))
513
            {
514

    
515
                var newFilePath = action.LocalFile.FullName;
516

    
517
                //How do we handle concurrent renames and deletes/uploads/downloads?
518
                //* A conflicting upload means that a file was renamed before it had a chance to finish uploading
519
                //  This should never happen as the network agent executes only one action at a time
520
                //* A conflicting download means that the file was modified on the cloud. While we can go on and complete
521
                //  the rename, there may be a problem if the file is downloaded in blocks, as subsequent block requests for the 
522
                //  same name will fail.
523
                //  This should never happen as the network agent executes only one action at a time.
524
                //* A conflicting delete can happen if the rename was followed by a delete action that didn't have the chance
525
                //  to remove the rename from the queue.
526
                //  We can probably ignore this case. It will result in an error which should be ignored            
527

    
528

    
529
                //The local file is already renamed
530
                StatusKeeper.SetFileOverlayStatus(newFilePath, FileOverlayStatus.Modified);
531

    
532

    
533
                var account = action.CloudFile.Account ?? accountInfo.UserName;
534
                var container = action.CloudFile.Container;
535

    
536
                var client = new CloudFilesClient(accountInfo);
537
                //TODO: What code is returned when the source file doesn't exist?
538
                client.MoveObject(account, container, action.OldCloudFile.Name, container, action.CloudFile.Name);
539

    
540
                StatusKeeper.SetFileStatus(newFilePath, FileStatus.Unchanged);
541
                StatusKeeper.SetFileOverlayStatus(newFilePath, FileOverlayStatus.Normal);
542
                NativeMethods.RaiseChangeNotification(newFilePath);
543
            }
544
        }
545

    
546

    
547
        
548

    
549
    }
550

    
551
   
552

    
553

    
554
}