public async Task AquireLockTest()
        {
            var duration = TimeSpan.FromSeconds(15);
            var name     = "constantinos"; // using a random name :)
            var @lock    = await _LockManager.AcquireLock(name, duration);

            using (@lock) {
                await Task.Delay(TimeSpan.FromSeconds(0.5));
            }
            var @lock2 = await _LockManager.AcquireLock(name, duration);

            using (@lock2) {
                await Task.Delay(TimeSpan.FromSeconds(0.5));
            }
        }
Exemplo n.º 2
0
        public async Task <Releaser> AcquireLock(ulong pageId, LockTypeEnum lockType)
        {
            int lockId = lockManager.LockIdForPage(pageId);

            if (lockType != LockTypeEnum.Shared)
            {
                throw new ReadOnlyTranCantAcquireExLockException();
            }

            lock (lck)
            {
                if (locksHeld.ContainsKey(lockId))
                {
                    // Return dummy leaser. You don't really own this lock.
                    // This probably needs to change.
                    throw new TranAlreadyHoldingLock();
                }
            }

            var releaser = await lockManager.AcquireLock(lockType, pageId, this.transactionId);

            lock (lck)
            {
                locksHeld.Add(lockId, lockType);
            }

            releaser.SetReleaseCallback(() => this.ReleaseLock(lockId));

            return(releaser);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Try Aquire the lock. If success it will return a successful <see cref="LockLeaseResult"/>.
        /// If the lockmanger throws a <seealso cref="LockManagerLockException"/> it will catch that and return a failed <see cref="LockLeaseResult"/>
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static async Task <LockLeaseResult> TryAquireLock(this ILockManager manager, string name)
        {
            try {
                var @lock = await manager.AcquireLock(name);

                return(LockLeaseResult.Success(@lock));
            } catch (LockManagerLockException) {
                return(LockLeaseResult.Fail());
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Try aquire the lock. If success it will return a successful <see cref="LockLeaseResult"/>.
        /// If the <see cref="ILockManager"/> throws a <seealso cref="LockManagerException"/> it will catch that and return a failed <see cref="LockLeaseResult"/>.
        /// </summary>
        /// <param name="manager">The instance of <see cref="ILockManager"/>.</param>
        /// <param name="name">Topic or name.</param>
        /// <param name="duration">The duration the lease will be active. Defaults 30 seconds.</param>
        /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
        /// <returns>The task the represent the asynchronous operation result for acquiring the lock.</returns>
        public static async Task <LockLeaseResult> TryAcquireLock(this ILockManager manager, string name, TimeSpan?duration = null, CancellationToken cancellationToken = default)
        {
            try {
                var @lock = await manager.AcquireLock(name, duration, cancellationToken);

                return(LockLeaseResult.Success(@lock));
            } catch (LockManagerException) {
                return(LockLeaseResult.Fail());
            }
        }
Exemplo n.º 5
0
        private async Task <Releaser> AcquireLockInternal(ulong pageId, LockTypeEnum lockType, bool forceCallerOwnership)
        {
            ILockManager lockManager = this.pageManager.GetLockManager();
            int          lockId      = lockManager.LockIdForPage(pageId);

            lock (lck)
            {
                if (locksHeld.ContainsKey(lockId))
                {
                    return(new Releaser());
                }
            }

            var releaser = await lockManager.AcquireLock(lockType, pageId, this.transactionId).ConfigureAwait(continueOnCapturedContext: false);

            lock (lck)
            {
                locksHeld.Add(lockId, lockType);
            }

            releaser.SetReleaseCallback(() => this.ReleaseLockCallback(lockId));

            if (forceCallerOwnership)
            {
                return(releaser);
            }

            // TODO: Implement Isolation Level strategy.
            if (this.isolationLevel == IsolationLevelEnum.ReadCommitted)
            {
                // If this is a read lock return to caller.
                // If write transaction is the owner.
                if (lockType == LockTypeEnum.Shared)
                {
                    return(releaser);
                }
                else if (lockType == LockTypeEnum.Exclusive)
                {
                    this.myLocks.Add(releaser);
                    return(new Releaser());
                }
                else
                {
                    throw new InvalidProgramException();
                }
            }
            else
            {
                throw new InvalidProgramException();
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Obtains an exclusive lock according to the specified <see cref="LockToken" />,
        /// then executes the specified <see cref="Action" /> before releasing the lock.
        /// </summary>
        /// <param name="token">The token dictating the scope and behavior of execution.</param>
        /// <param name="action">The action to execute.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="token" /> is null.
        /// <para>or</para>
        /// <paramref name="action" /> is null.
        /// </exception>
        /// <exception cref="AcquireLockTimeoutException">The lock could not be obtained within the acquisition timeout specified by <paramref name="token" />.</exception>
        /// <exception cref="HoldLockTimeoutException"><paramref name="action" /> did not complete within the hold timeout specified by <paramref name="token" />.</exception>
        public void Run(LockToken token, Action action)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            LockTicket lockTicket = _lockManager.AcquireLock(token.Key, token.AcquireTimeout, 1, _requestName);

            try
            {
                RunAction(token, action);
            }
            finally
            {
                _lockManager.ReleaseLock(lockTicket);
            }
        }
Exemplo n.º 7
0
        public ActionResult <SampleDTO> Get(int id)
        {
            using (_lockManager.AcquireLock("temp", TimeSpan.FromSeconds(30)))
            {
                Console.WriteLine("111");
                //Thread.Sleep(15 * 1000);
            }

            var sample = new Sample()
            {
                Value = 11
            };

            //this._eventPublisher.Publish(sample);

            //_logger.LogWarning("{@sample}", sample);
            _cacheManager.SetAsync <Sample>("sample", sample, 60);
            return(_mapper.Map <Sample, SampleDTO>(_cacheManager.GetAsync("sample", () => Task.FromResult(new Sample())).Result));
        }