Пример #1
0
        /// <summary>
        /// Perform read-only operations in a consistent context.It guarantees that multiple operations on multiple collections, even on a multi-node clusters
        /// give a consistent result. No write operation (normal or transactional) will be executed while the context is open.
        ///
        /// </summary>
        /// <param name="action">Should contain a list of queries performed on a <see cref="ConsistentContext"/></param>
        /// <param name="collections"></param>
        public void ConsistentRead(Action <ConsistentContext> action, [NotNull] params string[] collections)
        {
            if (collections.Length == 0)
            {
                throw new ArgumentException("Value cannot be an empty collection.", nameof(collections));
            }


            // check that the collections have been declared
            lock (_collectionSchema)
            {
                foreach (var collection in collections)
                {
                    if (!_collectionSchema.ContainsKey(collection))
                    {
                        throw new NotSupportedException(
                                  $"Unknown collection {collection}. Use Connector.DeclareCollection");
                    }
                }
            }

            Guid sessionId = default;

            try
            {
                sessionId = Client.AcquireLock(false, collections);

                action(new ConsistentContext(sessionId, this, collections));
            }
            finally
            {
                Client.ReleaseLock(sessionId);
            }
        }
Пример #2
0
        public void Lock_Disposable()
        {
            var client = new Client();

            const string keyName = "test/lock/disposable";

            using (var l = client.AcquireLock(keyName))
            {
                Assert.IsTrue(l.IsHeld);
            }
        }
Пример #3
0
 public void Lock_EphemeralAcquireRelease()
 {
     var client = new Client();
     const string keyName = "test/lock/ephemerallock";
     var sessionId = client.Session.Create(new SessionEntry { Behavior = SessionBehavior.Delete });
     using (var l = client.AcquireLock(new LockOptions(keyName) { Session = sessionId.Response }, CancellationToken.None))
     {
         Assert.IsTrue(l.IsHeld);
         client.Session.Destroy(sessionId.Response);
     }
     Assert.IsNull(client.KV.Get(keyName).Response);
 }
Пример #4
0
        public void Lock_EphemeralAcquireRelease()
        {
            var          client    = new Client();
            const string keyName   = "test/lock/ephemerallock";
            var          sessionId = client.Session.Create(new SessionEntry {
                Behavior = SessionBehavior.Delete
            });

            using (var l = client.AcquireLock(new LockOptions(keyName)
            {
                Session = sessionId.Response
            }, CancellationToken.None))
            {
                Assert.IsTrue(l.IsHeld);
                client.Session.Destroy(sessionId.Response);
            }
            Assert.IsNull(client.KV.Get(keyName).Response);
        }
Пример #5
0
        public void Lock_Disposable()
        {
            var client = new Client();

            const string keyName = "test/lock/disposable";
            using (var l = client.AcquireLock(keyName))
            {
                Assert.IsTrue(l.IsHeld);
            }
        }