Exemplo n.º 1
0
        /// <summary>
        /// Checks whether specified cache entry is still valid, based on Affinity Topology Version.
        /// When primary node changes for a key, GridNearCacheEntry stops receiving updates for that key,
        /// because reader ("subscription") on new primary is not yet established.
        /// <para />
        /// This method is similar to GridNearCacheEntry.valid().
        /// </summary>
        /// <param name="entry">Entry to validate.</param>
        /// <typeparam name="TVal">Value type.</typeparam>
        /// <returns>True if entry is valid and can be returned to the user; false otherwise.</returns>
        private bool IsValid <TVal>(PlatformCacheEntry <TVal> entry)
        {
            // See comments on _affinityTopologyVersionFunc about boxed copy approach.
            var currentVerBoxed = _affinityTopologyVersionFunc();
            var entryVerBoxed   = entry.Version;

            if (entryVerBoxed == null || currentVerBoxed == null)
            {
                return(false);
            }

            if (ReferenceEquals(currentVerBoxed, entryVerBoxed))
            {
                // Happy path: true on stable topology.
                return(true);
            }

            var entryVer   = (AffinityTopologyVersion)entryVerBoxed;
            var currentVer = (AffinityTopologyVersion)currentVerBoxed;

            if (entryVer >= currentVer)
            {
                return(true);
            }

            var part  = entry.Partition;
            var valid = _affinity.IsAssignmentValid(entryVer, part);

            // Update version or mark as invalid (null).
            entry.CompareExchangeVersion(valid ? currentVerBoxed : null, entryVerBoxed);

            return(valid);
        }
Exemplo n.º 2
0
        /** <inheritdoc /> */
        public void Update(IBinaryStream stream, Marshaller marshaller)
        {
            Debug.Assert(stream != null);
            Debug.Assert(marshaller != null);

            if (_stopped)
            {
                return;
            }

            var mode   = _keepBinary ? BinaryMode.ForceBinary : BinaryMode.Deserialize;
            var reader = marshaller.StartUnmarshal(stream, mode);

            var key    = reader.ReadObject <TK>();
            var hasVal = stream.ReadBool();

            if (hasVal)
            {
                var val  = reader.ReadObject <TV>();
                var part = stream.ReadInt();
                var ver  = new AffinityTopologyVersion(stream.ReadLong(), stream.ReadInt());

                _map[key] = new PlatformCacheEntry <TV>(val, GetBoxedAffinityTopologyVersion(ver), part);
            }
            else
            {
                PlatformCacheEntry <TV> unused;
                _map.TryRemove(key, out unused);
            }
        }