Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key/Value Store fixes #176

Merged
merged 7 commits into from
Nov 3, 2023
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
2 changes: 1 addition & 1 deletion sandbox/Example.KeyValueStore.Watcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

var store = await kv.CreateStoreAsync("e1");

await foreach (var entry in store.WatchAllAsync<int>())
await foreach (var entry in store.WatchAsync<int>())
{
Console.WriteLine($"[RCV] {entry}");
}
2 changes: 1 addition & 1 deletion src/NATS.Client.JetStream/Models/PubAckResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public record PubAckResponse
[System.Text.Json.Serialization.JsonPropertyName("seq")]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
[System.ComponentModel.DataAnnotations.Range(0D, 18446744073709552000D)]
public long Seq { get; set; } = default!;
public ulong Seq { get; set; } = default!;

/// <summary>
/// Indicates that the message was not stored due to the Nats-Msg-Id header and duplicate tracking
Expand Down
2 changes: 1 addition & 1 deletion src/NATS.Client.JetStream/Models/StoredMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public record StoredMessage
[System.Text.Json.Serialization.JsonPropertyName("seq")]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]
[System.ComponentModel.DataAnnotations.Range(0D, 18446744073709552000D)]
public long Seq { get; set; } = default!;
public ulong Seq { get; set; } = default!;

/// <summary>
/// The base64 encoded payload of the message body
Expand Down
2 changes: 1 addition & 1 deletion src/NATS.Client.JetStream/Models/StreamMsgGetRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public record StreamMsgGetRequest
/// </summary>
[System.Text.Json.Serialization.JsonPropertyName("seq")]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
public int Seq { get; set; } = default!;
public ulong Seq { get; set; } = default!;

/// <summary>
/// Retrieves the last message for a given subject, cannot be combined with seq
Expand Down
8 changes: 8 additions & 0 deletions src/NATS.Client.JetStream/NatsJSContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ public async ValueTask<PubAckResponse> PublishAsync<T>(
throw new NatsJSException("No response received");
}

public ValueTask<PubAckResponse> PublishAsync(
string subject,
string? msgId = default,
NatsHeaders? headers = default,
NatsPubOpts? opts = default,
CancellationToken cancellationToken = default) =>
PublishAsync<object?>(subject, default, msgId, headers, opts, cancellationToken);

internal string NewInbox() => NatsConnection.NewInbox(Connection.Opts.InboxPrefix);

internal async ValueTask<TResponse> JSRequestResponseAsync<TRequest, TResponse>(
Expand Down
4 changes: 2 additions & 2 deletions src/NATS.Client.JetStream/NatsJSException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ public class NatsJSDuplicateMessageException : NatsJSException
/// Create JetStream duplicate message exception.
/// </summary>
/// <param name="sequence">The duplicate sequence number.</param>
public NatsJSDuplicateMessageException(long sequence)
public NatsJSDuplicateMessageException(ulong sequence)
: base($"Duplicate of {sequence}") =>
Sequence = sequence;

/// <summary>
/// The duplicate sequence number.
/// </summary>
public long Sequence { get; }
public ulong Sequence { get; }
}

/// <summary>
Expand Down
13 changes: 8 additions & 5 deletions src/NATS.Client.JetStream/NatsJSStream.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NATS.Client.Core;
using NATS.Client.JetStream.Internal;
using NATS.Client.JetStream.Models;

namespace NATS.Client.JetStream;
Expand Down Expand Up @@ -168,7 +169,7 @@ public async ValueTask RefreshAsync(CancellationToken cancellationToken = defaul
request: null,
cancellationToken).ConfigureAwait(false);

public ValueTask<NatsMsg<T?>?> GetDirectAsync<T>(string subject, INatsSerializer? serializer = default, CancellationToken cancellationToken = default)
public ValueTask<NatsMsg<T?>?> GetDirectAsync<T>(StreamMsgGetRequest request, INatsSerializer? serializer = default, CancellationToken cancellationToken = default)
{
NatsSubOpts? subOpts;
if (serializer != null)
Expand All @@ -180,10 +181,12 @@ public async ValueTask RefreshAsync(CancellationToken cancellationToken = defaul
subOpts = default;
}

return _context.Connection.RequestAsync<object, T>(
subject: $"{_context.Opts.Prefix}.DIRECT.GET.{_name}.{subject}",
data: default,
requestOpts: default,
var requestSubject = $"{_context.Opts.Prefix}.DIRECT.GET.{_name}";

return _context.Connection.RequestAsync<StreamMsgGetRequest, T>(
subject: requestSubject,
data: request,
requestOpts: new NatsPubOpts { Serializer = NatsJSJsonSerializer.Default },
replyOpts: subOpts,
cancellationToken: cancellationToken);
}
Expand Down
15 changes: 0 additions & 15 deletions src/NATS.Client.KeyValueStore/INatsKVWatcher.cs

This file was deleted.

29 changes: 17 additions & 12 deletions src/NATS.Client.KeyValueStore/Internal/NatsKVWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public NatsKVWatchCommandMsg()
public NatsJSMsg<T?> Msg { get; init; } = default;
}

internal class NatsKVWatcher<T> : INatsKVWatcher<T>
internal class NatsKVWatcher<T> : IAsyncDisposable
{
private readonly ILogger _logger;
private readonly bool _debug;
Expand All @@ -38,7 +38,7 @@ internal class NatsKVWatcher<T> : INatsKVWatcher<T>
private readonly string _filter;
private readonly NatsConnection _nats;
private readonly Channel<NatsKVWatchCommandMsg<T>> _commandChannel;
private readonly Channel<NatsKVEntry<T?>> _entryChannel;
private readonly Channel<NatsKVEntry<T>> _entryChannel;
private readonly Channel<string> _consumerCreateChannel;
private readonly Timer _timer;
private readonly int _hbTimeout;
Expand Down Expand Up @@ -96,11 +96,10 @@ public NatsKVWatcher(
Timeout.Infinite,
Timeout.Infinite);

// Channel size 1 is enough because we want backpressure to go all the way to the subscription
// so that we get most accurate view of the stream. We can keep them as 1 until we find a case
// where it's not enough due to performance for example.
_commandChannel = Channel.CreateBounded<NatsKVWatchCommandMsg<T>>(1);
_entryChannel = Channel.CreateBounded<NatsKVEntry<T?>>(1);
// Keep the channel size large enough to avoid blocking the connection
// TCP receiver thread in case other operations are in-flight.
_commandChannel = Channel.CreateBounded<NatsKVWatchCommandMsg<T>>(1000);
_entryChannel = Channel.CreateBounded<NatsKVEntry<T>>(1000);

// A single request to create the consumer is enough because we don't want to create a new consumer
// back to back in case the consumer is being recreated due to a timeout and a mismatch in consumer
Expand All @@ -115,7 +114,7 @@ public NatsKVWatcher(
_commandTask = Task.Run(CommandLoop);
}

public ChannelReader<NatsKVEntry<T?>> Entries => _entryChannel.Reader;
public ChannelReader<NatsKVEntry<T>> Entries => _entryChannel.Reader;

internal string Consumer
{
Expand All @@ -126,6 +125,12 @@ internal string Consumer
public async ValueTask DisposeAsync()
{
_nats.ConnectionDisconnected -= OnDisconnected;

if (_sub != null)
{
await _sub.DisposeAsync();
}

_consumerCreateChannel.Writer.TryComplete();
_commandChannel.Writer.TryComplete();
_entryChannel.Writer.TryComplete();
Expand Down Expand Up @@ -174,7 +179,7 @@ private async Task CommandLoop()

operation = operationValues[0] switch
{
"DEL" => NatsKVOperation.Delete,
"DEL" => NatsKVOperation.Del,
"PURGE" => NatsKVOperation.Purge,
_ => operation,
};
Expand Down Expand Up @@ -219,17 +224,17 @@ private async Task CommandLoop()
continue;
}

if (_opts.IgnoreDeletes && operation is NatsKVOperation.Delete or NatsKVOperation.Purge)
if (_opts.IgnoreDeletes && operation is NatsKVOperation.Del or NatsKVOperation.Purge)
{
continue;
}

var delta = (long)metadata.NumPending;

var entry = new NatsKVEntry<T?>(_bucket, key)
var entry = new NatsKVEntry<T>(_bucket, key)
{
Value = msg.Data,
Revision = (long)metadata.Sequence.Stream,
Revision = metadata.Sequence.Stream,
Operation = operation,
Created = metadata.Timestamp,
Delta = delta,
Expand Down
2 changes: 1 addition & 1 deletion src/NATS.Client.KeyValueStore/NatsKVEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public readonly record struct NatsKVEntry<T>(string Bucket, string Key)
/// <summary>
/// A unique sequence for this value.
/// </summary>
public long Revision { get; init; } = default;
public ulong Revision { get; init; } = default;

/// <summary>
/// Distance from the latest value.
Expand Down
33 changes: 33 additions & 0 deletions src/NATS.Client.KeyValueStore/NatsKVException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,36 @@ public NatsKVException(string message, Exception exception)
{
}
}

public class NatsKVKeyDeletedException : NatsKVException
{
public NatsKVKeyDeletedException(ulong revision)
: base("Key was deleted") =>
Revision = revision;

public ulong Revision { get; }
}

public class NatsKVWrongLastRevisionException : NatsKVException
{
public NatsKVWrongLastRevisionException()
: base("Wrong last revision")
{
}
}

public class NatsKVCreateException : NatsKVException
{
public NatsKVCreateException()
: base("Can't create entry")
{
}
}

public class NatsKVKeyNotFoundException : NatsKVException
{
public NatsKVKeyNotFoundException()
: base("Key not found")
{
}
}
14 changes: 14 additions & 0 deletions src/NATS.Client.KeyValueStore/NatsKVOpts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,17 @@ public record NatsKVWatchOpts
/// </summary>
public bool MetaOnly { get; init; } = false;
}

public record NatsKVDeleteOpts
{
public bool Purge { get; init; }

public ulong Revision { get; init; }
}

public record NatsKVPurgeOpts
{
public static readonly NatsKVPurgeOpts Default = new() { DeleteMarkersThreshold = TimeSpan.FromMinutes(30) };

public TimeSpan DeleteMarkersThreshold { get; init; }
}
Loading