|
| 1 | +// Copyright Subatomix Research Inc. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +namespace PSql.Deploy; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// A governor to limit parallelism within a database deployment operation. |
| 8 | +/// </summary> |
| 9 | +public class Parallelism : IDisposable |
| 10 | +{ |
| 11 | + private readonly SemaphoreSlim _targetLimiter; |
| 12 | + private readonly SemaphoreSlim _commandLimiter; |
| 13 | + |
| 14 | + private bool _isDisposed; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Initializes a new <see cref="Parallelism"/> instance with the |
| 18 | + /// specified limits. |
| 19 | + /// </summary> |
| 20 | + /// <param name="maxParallelTargets"> |
| 21 | + /// The maximum number of target databases against which a deployment |
| 22 | + /// operation should run in parallel. Must be positive. |
| 23 | + /// </param> |
| 24 | + /// <param name="maxParallelCommands"> |
| 25 | + /// The maximum number of commands that a deployment operation should |
| 26 | + /// execute in parallel across all target databases. Must be positive. |
| 27 | + /// </param> |
| 28 | + /// <param name="maxCommandsPerTarget"> |
| 29 | + /// The maximum number of commands that a deployment operation should |
| 30 | + /// execute in parallel against any one target database. Must be |
| 31 | + /// positive. |
| 32 | + /// </param> |
| 33 | + /// <exception cref="ArgumentOutOfRangeException"> |
| 34 | + /// <paramref name="maxParallelTargets"/>, |
| 35 | + /// <paramref name="maxParallelCommands"/>, and/or |
| 36 | + /// <paramref name="maxCommandsPerTarget"/> is zero or negative. |
| 37 | + /// </exception> |
| 38 | + public Parallelism(int maxParallelTargets, int maxParallelCommands, int maxCommandsPerTarget) |
| 39 | + { |
| 40 | + if (maxParallelTargets <= 0) |
| 41 | + throw new ArgumentOutOfRangeException(nameof(maxParallelTargets)); |
| 42 | + if (maxParallelCommands <= 0) |
| 43 | + throw new ArgumentOutOfRangeException(nameof(maxParallelCommands)); |
| 44 | + if (maxCommandsPerTarget <= 0) |
| 45 | + throw new ArgumentOutOfRangeException(nameof(maxCommandsPerTarget)); |
| 46 | + |
| 47 | + MaxParallelTargets = maxParallelTargets; |
| 48 | + MaxParallelCommands = maxParallelCommands; |
| 49 | + MaxCommandsPerTarget = maxCommandsPerTarget; |
| 50 | + |
| 51 | + _targetLimiter = CreateSemaphore(maxParallelTargets); |
| 52 | + _commandLimiter = CreateSemaphore(maxParallelCommands); |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Gets the maximum number of target databases against which a |
| 57 | + /// deployment operation should run in parallel. |
| 58 | + /// </summary> |
| 59 | + public int MaxParallelTargets { get; } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Gets the maximum number of commands that a deployment operation |
| 63 | + /// should execute in parallel across all target databases. |
| 64 | + /// </summary> |
| 65 | + public int MaxParallelCommands { get; } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Gets the maximum number of commands that a deployment operation |
| 69 | + /// should execute in parallel against any one target database. |
| 70 | + /// </summary> |
| 71 | + public int MaxCommandsPerTarget { get; } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Establishes a scope to perform a database deployment operation |
| 75 | + /// against one target database, occupying one unit of target |
| 76 | + /// parallelism. |
| 77 | + /// </summary> |
| 78 | + /// <param name="cancellation"> |
| 79 | + /// The token to monitor for cancellation requests. |
| 80 | + /// </param> |
| 81 | + /// <returns> |
| 82 | + /// An object that releases the unit of target parallelism on disposal. |
| 83 | + /// </returns> |
| 84 | + /// <exception cref="ObjectDisposedException"> |
| 85 | + /// The object has been disposed. |
| 86 | + /// </exception> |
| 87 | + public async Task<IDisposable> UseTargetScopeAsync(CancellationToken cancellation = default) |
| 88 | + { |
| 89 | + await _targetLimiter.WaitAsync(cancellation).ConfigureAwait(false); |
| 90 | + return new Releaser(_targetLimiter); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Establishes a scope to perform one linear sequence of commands |
| 95 | + /// against a target database, occupying one unit of command parallelism. |
| 96 | + /// </summary> |
| 97 | + /// <param name="cancellation"> |
| 98 | + /// The token to monitor for cancellation requests. |
| 99 | + /// </param> |
| 100 | + /// <returns> |
| 101 | + /// An object that releases the unit of command parallelism on disposal. |
| 102 | + /// </returns> |
| 103 | + /// <exception cref="ObjectDisposedException"> |
| 104 | + /// The object has been disposed. |
| 105 | + /// </exception> |
| 106 | + public async Task<IDisposable> UseCommandScopeAsync(CancellationToken cancellation = default) |
| 107 | + { |
| 108 | + await _commandLimiter.WaitAsync(cancellation).ConfigureAwait(false); |
| 109 | + return new Releaser(_commandLimiter); |
| 110 | + } |
| 111 | + |
| 112 | + private static SemaphoreSlim CreateSemaphore(int limit) |
| 113 | + { |
| 114 | + return new SemaphoreSlim(initialCount: limit, maxCount: limit); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Releases the resources used by the object. |
| 119 | + /// </summary> |
| 120 | + public void Dispose() |
| 121 | + { |
| 122 | + Dispose(managed: true); |
| 123 | + GC.SuppressFinalize(this); |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Releases the resources used by the object. |
| 128 | + /// </summary> |
| 129 | + /// <param name="managed"> |
| 130 | + /// <see langword="true"/> to dispose managed and unmanaged resources; |
| 131 | + /// <see langword="false"/> to dispose unmanaged resources only. |
| 132 | + /// </param> |
| 133 | + protected virtual void Dispose(bool managed) |
| 134 | + { |
| 135 | + if (_isDisposed) |
| 136 | + return; |
| 137 | + |
| 138 | + if (managed) |
| 139 | + { |
| 140 | + _targetLimiter .Dispose(); |
| 141 | + _commandLimiter.Dispose(); |
| 142 | + } |
| 143 | + |
| 144 | + _isDisposed = true; |
| 145 | + } |
| 146 | + |
| 147 | + private sealed class Releaser : IDisposable |
| 148 | + { |
| 149 | + private SemaphoreSlim? _semaphore; |
| 150 | + |
| 151 | + public Releaser(SemaphoreSlim semaphore) |
| 152 | + { |
| 153 | + _semaphore = semaphore; |
| 154 | + } |
| 155 | + |
| 156 | + public void Dispose() |
| 157 | + { |
| 158 | + Interlocked.Exchange(ref _semaphore, null)?.Release(); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments