|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Garnet.common; |
| 12 | +using Garnet.server; |
| 13 | +using NUnit.Framework; |
| 14 | +using NUnit.Framework.Legacy; |
| 15 | +using StackExchange.Redis; |
| 16 | +using Tsavorite.core; |
| 17 | + |
| 18 | +namespace Garnet.test |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Factory for the race-detecting custom dictionary. |
| 22 | + /// </summary> |
| 23 | + class RaceDetectDictFactory : CustomObjectFactory |
| 24 | + { |
| 25 | + public override CustomObjectBase Create(byte type) => new RaceDetectDict(type); |
| 26 | + public override CustomObjectBase Deserialize(byte type, BinaryReader reader) => new RaceDetectDict(type, reader); |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Custom dictionary object that detects concurrent modification during serialization. |
| 31 | + /// Uses a shallow copy in CloneObject (same pattern as all built-in GarnetObjects), |
| 32 | + /// which means the clone and original share the same mutable dictionary. |
| 33 | + /// SerializeObject catches InvalidOperationException to prove the race. |
| 34 | + /// </summary> |
| 35 | + class RaceDetectDict : CustomObjectBase |
| 36 | + { |
| 37 | + internal readonly Dictionary<byte[], byte[]> dict; |
| 38 | + |
| 39 | + /// <summary>Number of times serialization caught a concurrent modification exception.</summary> |
| 40 | + internal static volatile int SerializationRaceCount; |
| 41 | + |
| 42 | + public RaceDetectDict(byte type) |
| 43 | + : base(type, MemoryUtils.DictionaryOverhead) |
| 44 | + { |
| 45 | + dict = new(ByteArrayComparer.Instance); |
| 46 | + } |
| 47 | + |
| 48 | + public RaceDetectDict(byte type, BinaryReader reader) |
| 49 | + : base(type, reader, MemoryUtils.DictionaryOverhead) |
| 50 | + { |
| 51 | + dict = new(ByteArrayComparer.Instance); |
| 52 | + var count = reader.ReadInt32(); |
| 53 | + for (var i = 0; i < count; i++) |
| 54 | + { |
| 55 | + var key = reader.ReadBytes(reader.ReadInt32()); |
| 56 | + var value = reader.ReadBytes(reader.ReadInt32()); |
| 57 | + dict.Add(key, value); |
| 58 | + UpdateSize(key, value); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary>Shallow copy — shares the same dict (the bug we're exposing).</summary> |
| 63 | + public RaceDetectDict(RaceDetectDict obj) : base(obj) |
| 64 | + { |
| 65 | + dict = obj.dict; |
| 66 | + } |
| 67 | + |
| 68 | + public override CustomObjectBase CloneObject() => new RaceDetectDict(this); |
| 69 | + |
| 70 | + public override void SerializeObject(BinaryWriter writer) |
| 71 | + { |
| 72 | + try |
| 73 | + { |
| 74 | + writer.Write(dict.Count); |
| 75 | + foreach (var kvp in dict) |
| 76 | + { |
| 77 | + writer.Write(kvp.Key.Length); |
| 78 | + writer.Write(kvp.Key); |
| 79 | + writer.Write(kvp.Value.Length); |
| 80 | + writer.Write(kvp.Value); |
| 81 | + } |
| 82 | + } |
| 83 | + catch (InvalidOperationException) |
| 84 | + { |
| 85 | + // Race detected: collection modified during serialization |
| 86 | + Interlocked.Increment(ref SerializationRaceCount); |
| 87 | + |
| 88 | + // Write a valid empty object so the stream isn't corrupted |
| 89 | + writer.BaseStream.SetLength(0); |
| 90 | + writer.Write(0); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public override void Dispose() { } |
| 95 | + |
| 96 | + public override unsafe void Scan(long start, out List<byte[]> items, out long cursor, |
| 97 | + int count = 10, byte* pattern = null, int patternLength = 0, bool isNoValue = false) |
| 98 | + { |
| 99 | + cursor = 0; |
| 100 | + items = []; |
| 101 | + } |
| 102 | + |
| 103 | + public bool Set(byte[] key, byte[] value) |
| 104 | + { |
| 105 | + dict[key] = value; |
| 106 | + UpdateSize(key, value); |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + private void UpdateSize(byte[] key, byte[] value, bool add = true) |
| 111 | + { |
| 112 | + var memorySize = Utility.RoundUp(key.Length, IntPtr.Size) + Utility.RoundUp(value.Length, IntPtr.Size) |
| 113 | + + (2 * MemoryUtils.ByteArrayOverhead) + MemoryUtils.DictionaryEntryOverhead; |
| 114 | + if (add) |
| 115 | + HeapMemorySize += memorySize; |
| 116 | + else |
| 117 | + HeapMemorySize -= memorySize; |
| 118 | + } |
| 119 | + |
| 120 | + public bool TryGetValue(byte[] key, out byte[] value) => dict.TryGetValue(key, out value); |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary>Custom SET command for RaceDetectDict.</summary> |
| 124 | + class RaceDetectDictSet : CustomObjectFunctions |
| 125 | + { |
| 126 | + public override bool NeedInitialUpdate(scoped ReadOnlySpan<byte> key, ref ObjectInput input, ref RespMemoryWriter writer) => true; |
| 127 | + |
| 128 | + public override bool Updater(ReadOnlySpan<byte> key, ref ObjectInput input, IGarnetObject value, ref RespMemoryWriter writer, ref RMWInfo rmwInfo) |
| 129 | + { |
| 130 | + Debug.Assert(value is RaceDetectDict); |
| 131 | + var offset = 0; |
| 132 | + var keyArg = GetNextArg(ref input, ref offset).ToArray(); |
| 133 | + var valueArg = GetNextArg(ref input, ref offset).ToArray(); |
| 134 | + ((RaceDetectDict)value).Set(keyArg, valueArg); |
| 135 | + writer.WriteSimpleString("OK"u8); |
| 136 | + return true; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// End-to-end test that attempts to trigger the shallow Clone() race |
| 142 | + /// condition through a running Garnet server. Uses lowMemory mode and |
| 143 | + /// concurrent writes to create memory pressure and force page eviction. |
| 144 | + /// |
| 145 | + /// NOTE: Tsavorite's record-level locking prevents this race from |
| 146 | + /// manifesting in practice — CopyUpdate and subsequent mutations are |
| 147 | + /// serialized per-record. The unit-level test (ShallowCloneRaceConditionTests) |
| 148 | + /// proves the bug exists at the object level, but the server's concurrency |
| 149 | + /// control layer provides protection. This test is kept as a stress test |
| 150 | + /// to verify that the server remains stable under heavy object store churn. |
| 151 | + /// </summary> |
| 152 | + [TestFixture] |
| 153 | + public class ShallowCloneServerRaceTests |
| 154 | + { |
| 155 | + GarnetServer server; |
| 156 | + |
| 157 | + [SetUp] |
| 158 | + public void Setup() |
| 159 | + { |
| 160 | + TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true); |
| 161 | + server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, lowMemory: true); |
| 162 | + |
| 163 | + // Register our race-detecting custom object |
| 164 | + server.Register.NewCommand("RACESET", CommandType.ReadModifyWrite, |
| 165 | + new RaceDetectDictFactory(), new RaceDetectDictSet(), |
| 166 | + new RespCommandsInfo { Arity = 4 }); |
| 167 | + |
| 168 | + server.Start(); |
| 169 | + RaceDetectDict.SerializationRaceCount = 0; |
| 170 | + } |
| 171 | + |
| 172 | + [TearDown] |
| 173 | + public void TearDown() |
| 174 | + { |
| 175 | + server?.Dispose(); |
| 176 | + TestUtils.DeleteDirectory(TestUtils.MethodTestDir); |
| 177 | + } |
| 178 | + |
| 179 | + [Test] |
| 180 | + public async Task ConcurrentWritesDuringEvictionTriggersSerializationRace() |
| 181 | + { |
| 182 | + var cts = new CancellationTokenSource(TimeSpan.FromSeconds(20)); |
| 183 | + var writeCount = 0; |
| 184 | + |
| 185 | + // Use multiple parallel connections for maximum concurrency |
| 186 | + var tasks = new List<Task>(); |
| 187 | + for (int t = 0; t < 8; t++) |
| 188 | + { |
| 189 | + var threadId = t; |
| 190 | + tasks.Add(Task.Run(async () => |
| 191 | + { |
| 192 | + using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig()); |
| 193 | + var db = redis.GetDatabase(0); |
| 194 | + while (!cts.Token.IsCancellationRequested) |
| 195 | + { |
| 196 | + try |
| 197 | + { |
| 198 | + var keyNum = Interlocked.Increment(ref writeCount); |
| 199 | + // Many keys with large values to force eviction |
| 200 | + var objKey = $"raceobj-{keyNum % 200}"; |
| 201 | + var field = $"f-{keyNum}"; |
| 202 | + var value = new string('x', 512); |
| 203 | + await db.ExecuteAsync("RACESET", objKey, field, value); |
| 204 | + } |
| 205 | + catch { } |
| 206 | + } |
| 207 | + })); |
| 208 | + } |
| 209 | + |
| 210 | + // Parallel checkpoint task |
| 211 | + tasks.Add(Task.Run(async () => |
| 212 | + { |
| 213 | + using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig()); |
| 214 | + var db = redis.GetDatabase(0); |
| 215 | + while (!cts.Token.IsCancellationRequested) |
| 216 | + { |
| 217 | + try { await db.ExecuteAsync("BGSAVE"); } catch { } |
| 218 | + await Task.Delay(50); |
| 219 | + } |
| 220 | + })); |
| 221 | + |
| 222 | + await Task.WhenAll(tasks.Select(t => t.ContinueWith(_ => { }))); |
| 223 | + |
| 224 | + Console.WriteLine($"Total writes: {writeCount}"); |
| 225 | + Console.WriteLine($"Serialization races detected: {RaceDetectDict.SerializationRaceCount}"); |
| 226 | + |
| 227 | + if (RaceDetectDict.SerializationRaceCount > 0) |
| 228 | + { |
| 229 | + Console.WriteLine("BUG CONFIRMED: Shallow Clone() caused concurrent modification " + |
| 230 | + "during serialization in a running Garnet server."); |
| 231 | + ClassicAssert.Greater(RaceDetectDict.SerializationRaceCount, 0, |
| 232 | + "Serialization race was detected — shallow Clone() shares mutable collections"); |
| 233 | + } |
| 234 | + else |
| 235 | + { |
| 236 | + Assert.Warn($"Race was not triggered after {writeCount} writes. " + |
| 237 | + "The bug exists but requires specific timing. Try running with more memory pressure."); |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | +} |
0 commit comments