Esempio n. 1
0
        public async Task UpdateSessionAsync(Session session, DateTime leaseEnd, CancellationToken cancellation)
        {
            if (session == default)
            {
                throw new ArgumentDefaultException(nameof(session));
            }

            IStoredSession current = await _storage.GetSessionAsync(session, cancellation),
                           start,
                           desired;

            do
            {
                start = current;

                if (start == null || _storedSessionManager.IsEnded(start))
                {
                    throw new SessionTerminatedException(session);
                }

                desired = _storedSessionManager.UpdateLease(start, leaseEnd);

                current = await _storage.UpdateSessionAsync(desired, start, cancellation);
            }while (start != current);
        }
Esempio n. 2
0
        public async Task AddSessionEntryAsync(Session session, CoordinationEntryPath entryPath, CancellationToken cancellation)
        {
            if (session == default)
            {
                throw new ArgumentDefaultException(nameof(session));
            }

            IStoredSession current = await _storage.GetSessionAsync(session, cancellation),
                           start,
                           desired;

            do
            {
                start = current;

                if (start == null || _storedSessionManager.IsEnded(start))
                {
                    throw new SessionTerminatedException();
                }

                desired = _storedSessionManager.AddEntry(start, entryPath);

                current = await _storage.UpdateSessionAsync(desired, start, cancellation);
            }while (start != current);
        }
Esempio n. 3
0
        public async Task RemoveSessionEntryAsync(Session session, CoordinationEntryPath entryPath, CancellationToken cancellation)
        {
            if (session == default)
            {
                throw new ArgumentDefaultException(nameof(session));
            }

            IStoredSession current = await _storage.GetSessionAsync(session, cancellation),
                           start,
                           desired;

            do
            {
                start = current;

                if (start == null)
                {
                    return;
                }

                desired = _storedSessionManager.RemoveEntry(start, entryPath);

                if (_storedSessionManager.IsEnded(desired) && !desired.EntryPaths.Any())
                {
                    desired = null;
                }

                current = await _storage.UpdateSessionAsync(desired, start, cancellation);
            }while (start != current);
        }
 public StoredSession(IStoredSession session)
 {
     Id             = session.Session.ToString();
     IsEnded        = session.IsEnded;
     LeaseEnd       = session.LeaseEnd;
     EntryPaths     = session.EntryPaths.Select(p => p.EscapedPath.ConvertToString()).ToArray();
     StorageVersion = session.StorageVersion;
 }
 public StoredSession(IStoredSession storedSession)
 {
     Session        = storedSession.Session;
     IsEnded        = storedSession.IsEnded;
     LeaseEnd       = storedSession.LeaseEnd;
     EntryPaths     = storedSession.EntryPaths;
     StorageVersion = storedSession.StorageVersion;
 }
        public bool IsEnded(IStoredSession storedSession)
        {
            if (storedSession == null)
            {
                throw new ArgumentNullException(nameof(storedSession));
            }

            return(storedSession.IsEnded || storedSession.LeaseEnd <= _dateTimeProvider.GetCurrentTime());
        }
        public IStoredSession Copy(IStoredSession storedSession)
        {
            if (storedSession == null)
            {
                return(null);
            }

            return(storedSession as StoredSession ?? new StoredSession(storedSession));
        }
        public async Task <IStoredSession> UpdateSessionAsync(IStoredSession value, IStoredSession comparand, CancellationToken cancellation)
        {
            var convertedValue     = ConvertValue(value);
            var convertedComparand = ConvertValue(comparand);

            if (await _database.CompareExchangeAsync(convertedValue, convertedComparand, (left, right) => left.StorageVersion == right.StorageVersion, cancellation))
            {
                return(comparand);
            }

            return(await GetSessionAsync((comparand ?? value).Session, cancellation));
        }
        private StoredSession ConvertValue(IStoredSession value)
        {
            StoredSession convertedValue = null;

            if (value != null)
            {
                convertedValue = value as StoredSession ?? new StoredSession(value);

                Assert(convertedValue != null);
                Assert((convertedValue as IStoredSession).Session == value.Session);
            }

            return(convertedValue);
        }
        public IStoredSession End(IStoredSession storedSession)
        {
            if (storedSession == null)
            {
                throw new ArgumentNullException(nameof(storedSession));
            }

            if (IsEnded(storedSession))
            {
                return(storedSession);
            }

            return(new StoredSession(storedSession.Session, isEnded: true, storedSession.LeaseEnd, storedSession.EntryPaths, storedSession.StorageVersion + 1));
        }
        public IStoredSession RemoveEntry(IStoredSession storedSession, CoordinationEntryPath entryPath)
        {
            if (storedSession == null)
            {
                throw new ArgumentNullException(nameof(storedSession));
            }

            if (!storedSession.EntryPaths.Contains(entryPath))
            {
                return(storedSession);
            }

            return(new StoredSession(storedSession.Session, storedSession.IsEnded, storedSession.LeaseEnd, storedSession.EntryPaths.Remove(entryPath), storedSession.StorageVersion + 1));
        }
        public IStoredSession AddEntry(IStoredSession storedSession, CoordinationEntryPath entryPath)
        {
            if (storedSession == null)
            {
                throw new ArgumentNullException(nameof(storedSession));
            }

            if (IsEnded(storedSession))
            {
                throw new InvalidOperationException();
            }

            if (storedSession.EntryPaths.Contains(entryPath))
            {
                return(storedSession);
            }

            return(new StoredSession(storedSession.Session, storedSession.IsEnded, storedSession.LeaseEnd, storedSession.EntryPaths.Add(entryPath), storedSession.StorageVersion + 1));
        }
        public IStoredSession UpdateLease(IStoredSession storedSession, DateTime leaseEnd)
        {
            if (storedSession == null)
            {
                throw new ArgumentNullException(nameof(storedSession));
            }

            if (IsEnded(storedSession))
            {
                return(storedSession);
            }

            //if (LeaseEnd <= _dateTimeProvider.GetCurrentTime())
            //    return new StoredSession(_dateTimeProvider, Key, isEnded: true, LeaseEnd, Entries, StorageVersion + 1);

            if (leaseEnd <= storedSession.LeaseEnd)
            {
                return(storedSession);
            }

            return(new StoredSession(storedSession.Session, isEnded: false, leaseEnd, storedSession.EntryPaths, storedSession.StorageVersion + 1));
        }
Esempio n. 14
0
        public async Task EndSessionAsync(Session session, CancellationToken cancellation)
        {
            if (session == default)
            {
                throw new ArgumentDefaultException(nameof(session));
            }

            IStoredSession current = await _storage.GetSessionAsync(session, cancellation),
                           start,
                           desired;

            do
            {
                start = current;

                if (start == null)
                {
                    return;
                }

                desired = start.EntryPaths.Any() ? _storedSessionManager.End(start) : null;
                current = await _storage.UpdateSessionAsync(desired, start, cancellation);
            }while (start != current);
        }