public void TestEquality()
        {
            // Default.
            var ver = new AffinityTopologyVersion();

            Assert.AreEqual(0, ver.Version);
            Assert.AreEqual(0, ver.MinorVersion);
            Assert.AreEqual(0, ver.GetHashCode());
            Assert.AreEqual(new AffinityTopologyVersion(), ver);

            // Custom.
            var ver1 = new AffinityTopologyVersion(long.MaxValue, int.MaxValue);
            var ver2 = new AffinityTopologyVersion(long.MaxValue, int.MaxValue);

            Assert.AreEqual(ver1, ver2);
            Assert.IsTrue(ver1 == ver2);
            Assert.IsFalse(ver1 != ver2);

            Assert.AreNotEqual(ver, ver1);
            Assert.IsTrue(ver != ver1);
            Assert.IsFalse(ver == ver1);

            // ToString.
            Assert.AreEqual("AffinityTopologyVersion [Version=1, MinorVersion=2]", 
                new AffinityTopologyVersion(1, 2).ToString());
        }
Exemple #2
0
        /** <inheritdoc /> */
        public void UpdateFromThreadLocal(int partition, AffinityTopologyVersion affinityTopologyVersion)
        {
            if (_stopped)
            {
                return;
            }

            var pair = (KeyValuePair <TK, TV>)PlatformCacheManager.ThreadLocalPair.Value;

            _map[pair.Key] = new PlatformCacheEntry <TV>(
                pair.Value,
                GetBoxedAffinityTopologyVersion(affinityTopologyVersion),
                partition);
        }
Exemple #3
0
        public void InitClient()
        {
            var configs = GetConfigs();

            _grid1 = Ignition.Start(Configuration(configs.Item1));
            _grid2 = Ignition.Start(Configuration(configs.Item2));

            AffinityTopologyVersion waitingTop = new AffinityTopologyVersion(2, 1);

            Assert.True(_grid1.WaitTopology(waitingTop), "Failed to wait topology " + waitingTop);

            _grid3 = Ignition.Start(Configuration(configs.Item3));

            // Start thin client.
            _igniteClient = Ignition.StartClient(GetThinClientConfiguration());
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AffinityFunctionContext"/> class.
        /// </summary>
        /// <param name="reader">The reader.</param>
        internal AffinityFunctionContext(IBinaryRawReader reader)
        {
            var cnt = reader.ReadInt();

            if (cnt > 0)
            {
                _previousAssignment = new List<List<IClusterNode>>(cnt);

                for (var i = 0; i < cnt; i++)
                    _previousAssignment.Add(IgniteUtils.ReadNodes(reader));
            }

            _backups = reader.ReadInt();
            _currentTopologySnapshot = IgniteUtils.ReadNodes(reader);
            _currentTopologyVersion = new AffinityTopologyVersion(reader.ReadLong(), reader.ReadInt());
            _discoveryEvent = EventReader.Read<DiscoveryEvent>(reader);
        }
Exemple #5
0
        /// <summary>
        /// Decodes the response that we got from <see cref="HandleResponse"/>.
        /// </summary>
        private T DecodeResponse <T>(BinaryHeapStream stream, Func <ClientResponseContext, T> readFunc,
                                     Func <ClientStatusCode, string, T> errorFunc)
        {
            ClientStatusCode statusCode;

            if (ServerVersion >= Ver140)
            {
                var flags = (ClientFlags)stream.ReadShort();

                if ((flags & ClientFlags.AffinityTopologyChanged) == ClientFlags.AffinityTopologyChanged)
                {
                    var topVer = new AffinityTopologyVersion(stream.ReadLong(), stream.ReadInt());
                    if (_topVerCallback != null)
                    {
                        _topVerCallback(topVer);
                    }
                }

                statusCode = (flags & ClientFlags.Error) == ClientFlags.Error
                    ? (ClientStatusCode)stream.ReadInt()
                    : ClientStatusCode.Success;
            }
            else
            {
                statusCode = (ClientStatusCode)stream.ReadInt();
            }

            if (statusCode == ClientStatusCode.Success)
            {
                return(readFunc != null
                    ? readFunc(new ClientResponseContext(stream, this))
                    : default(T));
            }

            var msg = BinaryUtils.Marshaller.StartUnmarshal(stream).ReadString();

            if (errorFunc != null)
            {
                return(errorFunc(statusCode, msg));
            }

            throw new IgniteClientException(msg, null, statusCode);
        }
Exemple #6
0
        private object ReadDistributionMapResponse(IBinaryStream s)
        {
            var affinityTopologyVersion = new AffinityTopologyVersion(s.ReadLong(), s.ReadInt());
            var size    = s.ReadInt();
            var mapping = new Dictionary <int, ClientCachePartitionMap>();

            for (int i = 0; i < size; i++)
            {
                var grp = new ClientCachePartitionAwarenessGroup(s);

                // Count partitions to avoid reallocating array.
                int maxPartNum = 0;
                foreach (var partMap in grp.PartitionMap)
                {
                    foreach (var part in partMap.Value)
                    {
                        if (part > maxPartNum)
                        {
                            maxPartNum = part;
                        }
                    }
                }

                // Populate partition array.
                var partNodeIds = new Guid[maxPartNum + 1];
                foreach (var partMap in grp.PartitionMap)
                {
                    foreach (var part in partMap.Value)
                    {
                        partNodeIds[part] = partMap.Key;
                    }
                }

                foreach (var cache in grp.Caches)
                {
                    mapping[cache.Key] = new ClientCachePartitionMap(cache.Key, partNodeIds, cache.Value);
                }
            }

            _distributionMap = new ClientCacheTopologyPartitionMap(mapping, affinityTopologyVersion);

            return(null);
        }
        public void TestRebalanceEvents()
        {
            ICollection <int> cacheRebalanceStopStartEvts = new[]
            {
                EventType.CacheRebalanceStarted,
                EventType.CacheRebalanceStopped
            };

            var listener = new Listener <CacheRebalancingEvent>();

            using (IIgnite ignite0 = Ignition.Start(GetConfig(listener, cacheRebalanceStopStartEvts, "TestRebalanceEvents")))
            {
                var cache = ignite0.GetOrCreateCache <int, int>(CacheName);

                for (int i = 0; i < 2000; i++)
                {
                    cache[i] = i;
                }

                using (IIgnite ignite1 = Ignition.Start(GetConfig(listener, cacheRebalanceStopStartEvts)))
                {
                    AffinityTopologyVersion afterRebalanceTop = new AffinityTopologyVersion(2, 1);

                    Assert.True(ignite1.WaitTopology(afterRebalanceTop, CacheName), "Failed to wait topology " + afterRebalanceTop);

                    var events = listener.GetEvents();

                    Assert.AreEqual(2, events.Count);

                    var rebalanceStart = events.First();

                    Assert.AreEqual(CacheName, rebalanceStart.CacheName);
                    Assert.AreEqual(EventType.CacheRebalanceStarted, rebalanceStart.Type);

                    var rebalanceStop = events.Last();

                    Assert.AreEqual(CacheName, rebalanceStop.CacheName);
                    Assert.AreEqual(EventType.CacheRebalanceStopped, rebalanceStop.Type);
                }
            }
        }
Exemple #8
0
        public void CacheGet_NewNodeEnteredTopology_RequestIsRoutedToDefaultNode()
        {
            // Warm-up.
            Assert.AreEqual(1, _cache.Get(1));

            // Before topology change.
            Assert.AreEqual(12, _cache.Get(12));
            Assert.AreEqual(1, GetClientRequestGridIndex());

            Assert.AreEqual(14, _cache.Get(14));
            Assert.AreEqual(2, GetClientRequestGridIndex());

            // After topology change.
            var cfg = GetIgniteConfiguration();

            cfg.AutoGenerateIgniteInstanceName = true;

            using (var ignite = Ignition.Start(cfg))
            {
                var affinityChangedTop = new AffinityTopologyVersion(ignite.GetCluster().TopologyVersion, 1);

                Assert.True(ignite.WaitTopology(affinityChangedTop, _cache.Name),
                            "Failed to wait topology " + affinityChangedTop);

                // Warm-up.
                Assert.AreEqual(1, _cache.Get(1));

                // Get default node index by performing non-partition-aware operation.
                _cache.GetAll(Enumerable.Range(1, 10));
                var defaultNodeIdx = GetClientRequestGridIndex("GetAll");
                Assert.Greater(defaultNodeIdx, -1);

                // Assert: keys 12 and 14 belong to a new node now, but we don't have the new node in the server list.
                // Requests are routed to default node.
                Assert.AreEqual(12, _cache.Get(12));
                Assert.AreEqual(defaultNodeIdx, GetClientRequestGridIndex());

                Assert.AreEqual(14, _cache.Get(14));
                Assert.AreEqual(defaultNodeIdx, GetClientRequestGridIndex());
            }
        }
Exemple #9
0
        public void FixtureSetUp()
        {
            var cfg = new IgniteConfiguration(TestUtils.GetTestConfiguration())
            {
                CacheConfiguration = new[]
                {
                    new CacheConfiguration(CacheName)
                    {
                        AffinityFunction = new SimpleAffinityFunction(),
                        Backups          = 7
                    },
                    new CacheConfiguration(CacheNameRendezvous)
                    {
                        AffinityFunction = new RendezvousAffinityFunctionEx {
                            Bar = "test"
                        }
                    }
                },
                UserAttributes = new Dictionary <string, object> {
                    { BackupFilterAttrName, 1 }
                }
            };

            _ignite = Ignition.Start(cfg);

            var cfg2 = new IgniteConfiguration(TestUtils.GetTestConfiguration())
            {
                IgniteInstanceName = "grid2",
                UserAttributes     = new Dictionary <string, object> {
                    { BackupFilterAttrName, 2 }
                }
            };

            _ignite2 = Ignition.Start(cfg2);

            AffinityTopologyVersion waitingTop = new AffinityTopologyVersion(2, 1);

            Assert.True(_ignite.WaitTopology(waitingTop), "Failed to wait topology " + waitingTop);
            Assert.True(_ignite2.WaitTopology(waitingTop), "Failed to wait topology " + waitingTop);
        }
 /// <summary>
 /// Called when topology version changes.
 /// </summary>
 public void OnAffinityTopologyVersionChanged(AffinityTopologyVersion affinityTopologyVersion)
 {
     _affinityTopologyVersion = affinityTopologyVersion;
 }
Exemple #11
0
        /// <summary>
        /// Gets boxed affinity version. Reuses existing boxing copy to reduce allocations.
        /// </summary>
        private object GetBoxedAffinityTopologyVersion(AffinityTopologyVersion ver)
        {
            var currentVerBoxed = _affinityTopologyVersionFunc();

            return((AffinityTopologyVersion)currentVerBoxed == ver ? currentVerBoxed : ver);
        }
 /// <summary>
 /// Updates current Affinity Topology Version.
 /// </summary>
 private void OnAffinityTopologyVersionChange(AffinityTopologyVersion affinityTopologyVersion)
 {
     _affinityTopologyVersion = affinityTopologyVersion;
 }