Skip to content

Commit f799eb8

Browse files
committed
Add onboarding helper RandomlyForceCacheMissBitMask
1 parent c3a1efe commit f799eb8

File tree

5 files changed

+10
-9
lines changed

5 files changed

+10
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ These settings are common across all plugins, although different implementations
5959
| `$(MSBuildCacheMaxConcurrentCacheContentOperations)` | `int` | 64 | The maximum number of cache operations to perform concurrently |
6060
| `$(MSBuildCacheLocalCacheRootPath)` | `string` | "\MSBuildCache" (in repo's drive root) | Base directory to use for the local cache. |
6161
| `$(MSBuildCacheLocalCacheSizeInMegabytes)` | `int` | 102400 (100 GB) | The maximum size in megabytes of the local cache |
62-
| `$(MSBuildCacheRandomlyForceCacheMissPercentage)` | `int` | 0 | The percentage of cache hits to force to be treated as cache misses. Useful for testing. |
62+
| `$(MSBuildCacheRandomlyForceCacheMissBitMask)` | `int` | 0 | Force a cache miss if the all the specified bits in project hash are set. Because the project hash is stable given the same inputs, building the same commit with the same bitmask will cause the same projects to be forced to miss. For example, 1, 2, 4, 8,... will each cause ~50% cache misses they are represented by one bit. Since they are different bits, changing from 1 to 2 will cause a different set of ~50% to miss. 3, 5, 6, ... will each cause ~25%. 7, 15, .. will each cause ~12%. Useful for testing to simulate natural cache misses. |
6363
| `$(MSBuildCacheIgnoredInputPatterns)` | `Glob[]` | | Files which are part of the repo which should be ignored for cache invalidation |
6464
| `$(MSBuildCacheIgnoredOutputPatterns)` | `Glob[]` | `*.assets.cache; *assemblyreference.cache` | Files to ignore for output collection into the cache. Note that if output are ignored, the replayed cache entry will not have these files. This should be used for intermediate outputs which are not properly portable |
6565
| `$(MSBuildCacheIdenticalDuplicateOutputPatterns)` | `Glob[]` | | Files to allow duplicate outputs, with identical content, across projects. Projects which produce files at the same path with differing content will still produce an error. Note: this setting should be used sparingly as it impacts performance |

src/Common.Tests/PluginSettingsTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ public void LocalCacheSizeInMegabytesSetting()
9595
new[] { 123u, 456u, 789u });
9696

9797
[TestMethod]
98-
public void RandomlyForceCacheMissPercentageSetting()
98+
public void RandomlyForceCacheMissBitMaskSetting()
9999
=> TestBasicSetting(
100-
nameof(PluginSettings.RandomlyForceCacheMissPercentage),
101-
pluginSettings => pluginSettings.RandomlyForceCacheMissPercentage,
100+
nameof(PluginSettings.RandomlyForceCacheMissBitMask),
101+
pluginSettings => pluginSettings.RandomlyForceCacheMissBitMask,
102102
new[] { 0u, 10u, 100u });
103103

104104
private static IEnumerable<object[]> GlobTestData

src/Common/MSBuildCachePluginBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,15 @@ private async Task<CacheResult> GetCacheResultInnerAsync(BuildRequestData buildR
349349
return CacheResult.IndicateNonCacheHit(CacheResultType.CacheMiss);
350350
}
351351

352-
if (Settings.RandomlyForceCacheMissPercentage > 0)
352+
if (Settings.RandomlyForceCacheMissBitMask != 0)
353353
{
354354
// use a hash of the project id so that it is repeatable
355355
#pragma warning disable CA1850 // ComputeHash is not in net472
356356
using SHA256 hasher = SHA256.Create();
357357
byte[] projectHash = hasher.ComputeHash(Encoding.UTF8.GetBytes(nodeContext.Id));
358+
uint hashInt = BitConverter.ToUInt32(projectHash, 0);
358359
#pragma warning restore CA1850
359-
if (BitConverter.ToUInt32(projectHash, 0) % 100 < Settings.RandomlyForceCacheMissPercentage)
360+
if ((hashInt & Settings.RandomlyForceCacheMissBitMask) == Settings.RandomlyForceCacheMissBitMask)
360361
{
361362
Interlocked.Increment(ref _cacheMissCount);
362363
return CacheResult.IndicateNonCacheHit(CacheResultType.CacheMiss);

src/Common/PluginSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public string LocalCacheRootPath
8181

8282
public uint LocalCacheSizeInMegabytes { get; init; } = 102400; // 100GB
8383

84-
public uint RandomlyForceCacheMissPercentage { get; init; }
84+
public uint RandomlyForceCacheMissBitMask { get; init; }
8585

8686
public IReadOnlyCollection<Glob> IgnoredInputPatterns { get; init; } = Array.Empty<Glob>();
8787

src/Common/build/Microsoft.MSBuildCache.Common.targets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<MSBuildCacheGlobalPropertiesToIgnore>$(MSBuildCacheGlobalPropertiesToIgnore);MSBuildCacheMaxConcurrentCacheContentOperations</MSBuildCacheGlobalPropertiesToIgnore>
1111
<MSBuildCacheGlobalPropertiesToIgnore>$(MSBuildCacheGlobalPropertiesToIgnore);MSBuildCacheLocalCacheRootPath</MSBuildCacheGlobalPropertiesToIgnore>
1212
<MSBuildCacheGlobalPropertiesToIgnore>$(MSBuildCacheGlobalPropertiesToIgnore);MSBuildCacheLocalCacheSizeInMegabytes</MSBuildCacheGlobalPropertiesToIgnore>
13-
<MSBuildCacheGlobalPropertiesToIgnore>$(MSBuildCacheGlobalPropertiesToIgnore);MSBuildCacheRandomlyForceCacheMissPercentage</MSBuildCacheGlobalPropertiesToIgnore>
13+
<MSBuildCacheGlobalPropertiesToIgnore>$(MSBuildCacheGlobalPropertiesToIgnore);MSBuildCacheRandomlyForceCacheMissBitMask</MSBuildCacheGlobalPropertiesToIgnore>
1414
<MSBuildCacheGlobalPropertiesToIgnore>$(MSBuildCacheGlobalPropertiesToIgnore);MSBuildCacheIgnoredInputPatterns</MSBuildCacheGlobalPropertiesToIgnore>
1515
<MSBuildCacheGlobalPropertiesToIgnore>$(MSBuildCacheGlobalPropertiesToIgnore);MSBuildCacheIgnoredOutputPatterns</MSBuildCacheGlobalPropertiesToIgnore>
1616
<MSBuildCacheGlobalPropertiesToIgnore>$(MSBuildCacheGlobalPropertiesToIgnore);MSBuildCacheIdenticalDuplicateOutputPatterns</MSBuildCacheGlobalPropertiesToIgnore>
@@ -30,7 +30,7 @@
3030
<MaxConcurrentCacheContentOperations>$(MSBuildCacheMaxConcurrentCacheContentOperations)</MaxConcurrentCacheContentOperations>
3131
<LocalCacheRootPath>$(MSBuildCacheLocalCacheRootPath)</LocalCacheRootPath>
3232
<LocalCacheSizeInMegabytes>$(MSBuildCacheLocalCacheSizeInMegabytes)</LocalCacheSizeInMegabytes>
33-
<RandomlyForceCacheMissPercentage>$(MSBuildCacheRandomlyForceCacheMissPercentage)</RandomlyForceCacheMissPercentage>
33+
<RandomlyForceCacheMissBitMask>$(MSBuildCacheRandomlyForceCacheMissBitMask)</RandomlyForceCacheMissBitMask>
3434
<IgnoredInputPatterns>$(MSBuildCacheIgnoredInputPatterns)</IgnoredInputPatterns>
3535
<IgnoredOutputPatterns>$(MSBuildCacheIgnoredOutputPatterns)</IgnoredOutputPatterns>
3636
<IdenticalDuplicateOutputPatterns>$(MSBuildCacheIdenticalDuplicateOutputPatterns)</IdenticalDuplicateOutputPatterns>

0 commit comments

Comments
 (0)