/// <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>(NearCacheEntry <TVal> entry) { // See comments on _affinityTopologyVersionFunc about boxed copy approach. var currentVerBoxed = _affinityTopologyVersionFunc(); var entryVerBoxed = entry.Version; Debug.Assert(currentVerBoxed != null); if (ReferenceEquals(currentVerBoxed, entryVerBoxed)) { // Happy path: true on stable topology. return(true); } if (entryVerBoxed == null) { return(false); } 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); }
/** <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 NearCacheEntry <TV>(val, GetBoxedAffinityTopologyVersion(ver), part); } else { NearCacheEntry <TV> unused; _map.TryRemove(key, out unused); } }