Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/contrib/cluster/Akka.DistributedData/DistributedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class DistributedData : IExtension
private readonly ActorSystem _system;

/// <summary>
/// Returns true if this member is not tagged with the role configured for the replicas.
/// Returns true if the cluster has terminated or this member does not have the configured replica role.
/// When either condition holds as the extension is constructed, its <see cref="Replicator"/> is set to dead letters.
/// </summary>
public bool IsTerminated => Cluster.Cluster.Get(_system).IsTerminated || (!string.IsNullOrEmpty(_settings.Role) && !Cluster.Cluster.Get(_system).SelfRoles.Contains(_settings.Role));

Expand Down Expand Up @@ -116,7 +117,7 @@ public static Config DefaultConfig() =>
/// <summary>
/// Asynchronously returns list of locally known keys.
/// </summary>
/// <returns></returns>
/// <returns>A task that completes with the key identifiers currently stored in the local replicator; deleted keys are omitted.</returns>
public async Task<IImmutableSet<string>> GetKeysAsync(CancellationToken cancellation = default (CancellationToken))
{
var response = await Replicator.Ask(Dsl.GetKeyIds, cancellation);
Expand Down
86 changes: 83 additions & 3 deletions src/contrib/cluster/Akka.DistributedData/ReadAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,32 @@ private Receive WaitRepairAck(DataEnvelope envelope) => msg =>
};
}

/// <summary>
/// Specifies the read consistency level used by a <see cref="Get"/> request.
/// See <see cref="Replicator"/> for an overview of the available read consistency levels.
/// </summary>
public interface IReadConsistency
{
/// <summary>
/// Gets the maximum time to wait for the requested read consistency level to be reached.
/// </summary>
TimeSpan Timeout { get; }
}

/// <summary>
/// Reads a value from the local replica only. This is the default consistency level for
/// <see cref="DistributedData.GetAsync{T}(IKey{T},IReadConsistency,System.Threading.CancellationToken)"/>.
/// </summary>
public sealed class ReadLocal : IReadConsistency
{
/// <summary>
/// Gets the singleton local read consistency level.
/// </summary>
public static readonly ReadLocal Instance = new();

/// <summary>
/// Gets the timeout for this consistency level, which is always zero because it reads only local state.
/// </summary>
public TimeSpan Timeout => TimeSpan.Zero;

private ReadLocal() { }
Expand All @@ -179,12 +196,27 @@ private ReadLocal() { }
public override int GetHashCode() => nameof(ReadLocal).GetHashCode();
}

/// <summary>
/// Reads and merges values from the requested number of replicas in total, including the local replica.
/// See <see cref="Replicator"/> for the read consistency overview.
/// </summary>
public sealed class ReadFrom : IReadConsistency, IEquatable<ReadFrom>
{
/// <summary>
/// Gets the number of replicas whose values are included in the read, counting the local replica.
/// </summary>
public int N { get; }

/// <summary>
/// Gets the maximum time to wait for the requested replica responses.
/// </summary>
public TimeSpan Timeout { get; }

/// <summary>
/// Initializes a read that waits for values from <paramref name="n"/> replicas, including the local replica.
/// </summary>
/// <param name="n">The number of replicas whose values are included in the read, counting the local replica.</param>
/// <param name="timeout">The maximum time to wait for the requested replica responses.</param>
public ReadFrom(int n, TimeSpan timeout)
{
N = n;
Expand Down Expand Up @@ -215,11 +247,29 @@ public override int GetHashCode()
public override string ToString() => $"ReadFrom({N}, timeout={Timeout})";
}

/// <summary>
/// Reads and merges values from a majority of replicas. The required response count is the larger of the
/// majority count and <see cref="MinCapacity"/>, capped at the number of replicas selected for the request.
/// See <see cref="Replicator"/> for the read consistency overview.
/// </summary>
public sealed class ReadMajority : IReadConsistency, IEquatable<ReadMajority>
{
/// <summary>
/// Gets the maximum time to wait for the required replica responses.
/// </summary>
public TimeSpan Timeout { get; }

/// <summary>
/// Gets the minimum response count used in the calculation. The final response count is capped at the
/// number of replicas selected for the request.
/// </summary>
public int MinCapacity { get; }

/// <summary>
/// Initializes a majority read.
/// </summary>
/// <param name="timeout">The maximum time to wait for the required replica responses.</param>
/// <param name="minCapacity">The minimum response count used in the calculation; the final response count is capped at the number of replicas selected for the request.</param>
public ReadMajority(TimeSpan timeout, int minCapacity = 0)
{
Timeout = timeout;
Expand Down Expand Up @@ -254,16 +304,35 @@ public override int GetHashCode()
}

/// <summary>
/// <see cref="ReadMajority"/> but with the given number of <see cref="Additional"/> nodes added to the majority count. At most
/// all nodes. Exiting nodes are excluded using `ReadMajorityPlus` because those are typically
/// about to be removed and will not be able to respond.
/// Adjusts the majority response count by <see cref="Additional"/>. The greater of that adjusted count and
/// <see cref="MinCapacity"/> is capped at the number of replicas selected for the request. Exiting nodes
/// are excluded from this read, since they may not be able to respond.
/// </summary>
public sealed class ReadMajorityPlus : IReadConsistency, IEquatable<ReadMajorityPlus>
{
/// <summary>
/// Gets the maximum time to wait for the required replica responses.
/// </summary>
public TimeSpan Timeout { get; }

/// <summary>
/// Gets the adjustment applied to the majority response count before <see cref="MinCapacity"/> is applied
/// and the result is capped at the number of replicas selected for the request.
/// </summary>
public int Additional { get; }

/// <summary>
/// Gets the minimum response count used in the calculation. The final response count is capped at the
/// number of replicas selected for the request.
/// </summary>
public int MinCapacity { get; }

/// <summary>
/// Initializes a majority read with an adjusted response count.
/// </summary>
/// <param name="timeout">The maximum time to wait for the required replica responses.</param>
/// <param name="additional">The adjustment to the majority response count.</param>
/// <param name="minCapacity">The minimum response count used in the calculation; the final response count is capped at the number of replicas selected for the request.</param>
public ReadMajorityPlus(TimeSpan timeout, int additional, int minCapacity = 0)
{
Timeout = timeout;
Expand Down Expand Up @@ -302,10 +371,21 @@ public override int GetHashCode()
}
}

/// <summary>
/// Reads and merges values from every replica selected for the request. Exiting nodes are excluded.
/// See <see cref="Replicator"/> for the read consistency overview.
/// </summary>
public sealed class ReadAll : IReadConsistency, IEquatable<ReadAll>
{
/// <summary>
/// Gets the maximum time to wait for responses from all replicas selected for the request.
/// </summary>
public TimeSpan Timeout { get; }

/// <summary>
/// Initializes a read that waits for responses from all replicas selected for the request.
/// </summary>
/// <param name="timeout">The maximum time to wait for responses from all replicas selected for the request.</param>
public ReadAll(TimeSpan timeout)
{
Timeout = timeout;
Expand Down
22 changes: 15 additions & 7 deletions src/core/Akka.Streams/Dsl/ChannelSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

namespace Akka.Streams.Dsl
{
/// <summary>
/// Provides sinks that write stream elements to <see cref="ChannelWriter{T}"/> instances
/// or expose a bounded channel through a <see cref="ChannelReader{T}"/>.
/// </summary>
public static class ChannelSink
{
/// <summary>
Expand All @@ -38,14 +42,18 @@ public static Sink<T, NotUsed> FromWriter<T>(ChannelWriter<T> writer, bool isOwn

/// <summary>
/// Creates a sink that upon materialization, returns a <see cref="ChannelReader{T}"/> connected with
/// this materialized graph. It can then be used to consume events incoming from the graph. It will
/// also be completed once upstream completes.
/// this materialized graph. The reader can be used to consume events emitted by the graph. The channel
/// closes after normal upstream completion, once any pending write has finished. If upstream fails, the
/// channel closes with that failure. Buffered elements remain readable; the reader's <see cref="ChannelReader{T}.Completion"/>
/// task completes or faults after no more data is available to read.
/// When the channel is full, <see cref="BoundedChannelFullMode.Wait"/> waits for space and backpressures
/// upstream. The drop modes can discard an incoming or buffered element according to the selected mode.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="bufferSize"></param>
/// <param name="singleReader"></param>
/// <param name="fullMode"></param>
/// <returns></returns>
/// <typeparam name="T">The type of elements written to the channel.</typeparam>
/// <param name="bufferSize">The maximum number of elements the bounded channel can store.</param>
/// <param name="singleReader">Indicates that the caller guarantees at most one read operation at a time.</param>
/// <param name="fullMode">The behavior to use when the bounded channel is full.</param>
/// <returns>A sink that materializes to a reader for its bounded channel.</returns>
public static Sink<T, ChannelReader<T>> AsReader<T>(int bufferSize, bool singleReader = false, BoundedChannelFullMode fullMode = BoundedChannelFullMode.Wait) =>
Sink.FromGraph(new ChannelReaderSink<T>(bufferSize, singleReader, fullMode));

Expand Down
Loading