예제 #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);
        }
예제 #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);
        }
예제 #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);
        }
예제 #4
0
 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;
 }
예제 #5
0
 public StoredSession(IStoredSession storedSession)
 {
     Session        = storedSession.Session;
     IsEnded        = storedSession.IsEnded;
     LeaseEnd       = storedSession.LeaseEnd;
     EntryPaths     = storedSession.EntryPaths;
     StorageVersion = storedSession.StorageVersion;
 }
예제 #6
0
        public bool IsEnded(IStoredSession storedSession)
        {
            if (storedSession == null)
            {
                throw new ArgumentNullException(nameof(storedSession));
            }

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

            return(storedSession as StoredSession ?? new StoredSession(storedSession));
        }
예제 #8
0
        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));
        }
예제 #9
0
        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);
        }
예제 #10
0
        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));
        }
예제 #11
0
        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));
        }
예제 #12
0
        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));
        }
예제 #13
0
        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));
        }
예제 #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);
        }