-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Init MVP Writer implementation (#206)
- Loading branch information
1 parent
d0d22de
commit 5a964cd
Showing
13 changed files
with
462 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Ydb.Sdk.Services.Topic; | ||
|
||
public interface IWriter<TValue> | ||
{ | ||
public Task<WriteResult> WriteAsync(TValue data); | ||
|
||
public Task<WriteResult> WriteAsync(Message<TValue> message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Ydb.Sdk.Services.Topic; | ||
|
||
public class Message<TValue> | ||
{ | ||
public Message(TValue data) | ||
{ | ||
Data = data; | ||
} | ||
|
||
public DateTime Timestamp { get; set; } = DateTime.Now; | ||
|
||
public TValue Data { get; } | ||
|
||
public List<Metadata> Metadata { get; } = new(); | ||
} | ||
|
||
public record Metadata(string Key, byte[] Value); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Ydb.Sdk.Services.Topic; | ||
|
||
internal abstract class TopicSession<TFromClient, TFromServer> : IDisposable | ||
{ | ||
private readonly Func<Task> _initialize; | ||
|
||
protected readonly Driver.BidirectionalStream<TFromClient, TFromServer> Stream; | ||
protected readonly ILogger Logger; | ||
protected readonly string SessionId; | ||
|
||
private int _isActive = 1; | ||
private bool _disposed; | ||
|
||
protected TopicSession(Driver.BidirectionalStream<TFromClient, TFromServer> stream, ILogger logger, | ||
string sessionId, Func<Task> initialize) | ||
{ | ||
Stream = stream; | ||
Logger = logger; | ||
SessionId = sessionId; | ||
_initialize = initialize; | ||
} | ||
|
||
protected async void ReconnectSession() | ||
{ | ||
if (Interlocked.CompareExchange(ref _isActive, 0, 1) == 0) | ||
{ | ||
Logger.LogWarning("Skipping reconnect. A reconnect session has already been initiated"); | ||
|
||
return; | ||
} | ||
|
||
Logger.LogInformation("WriterSession[{SessionId}] has been deactivated, starting to reconnect", SessionId); | ||
|
||
while (!_disposed) | ||
{ | ||
try | ||
{ | ||
await _initialize(); | ||
break; | ||
} | ||
catch (Exception e) | ||
{ | ||
Logger.LogError(e, "Unable to reconnect the session due to the following error"); | ||
} | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
lock (this) | ||
{ | ||
_disposed = true; | ||
} | ||
|
||
Stream.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Ydb.Topic; | ||
|
||
namespace Ydb.Sdk.Services.Topic; | ||
|
||
public class WriteResult | ||
{ | ||
private readonly long _offset; | ||
|
||
internal WriteResult(StreamWriteMessage.Types.WriteResponse.Types.WriteAck ack) | ||
{ | ||
switch (ack.MessageWriteStatusCase) | ||
{ | ||
case StreamWriteMessage.Types.WriteResponse.Types.WriteAck.MessageWriteStatusOneofCase.Written: | ||
Status = PersistenceStatus.Written; | ||
_offset = ack.Written.Offset; | ||
break; | ||
case StreamWriteMessage.Types.WriteResponse.Types.WriteAck.MessageWriteStatusOneofCase.Skipped: | ||
Status = PersistenceStatus.AlreadyWritten; | ||
break; | ||
case StreamWriteMessage.Types.WriteResponse.Types.WriteAck.MessageWriteStatusOneofCase.None: | ||
default: | ||
throw new YdbWriterException($"Unexpected WriteAck status: {ack.MessageWriteStatusCase}"); | ||
} | ||
} | ||
|
||
public PersistenceStatus Status { get; } | ||
|
||
public bool TryGetOffset(out long offset) | ||
{ | ||
offset = _offset; | ||
|
||
return Status == PersistenceStatus.Written; | ||
} | ||
} | ||
|
||
public enum PersistenceStatus | ||
{ | ||
Written, | ||
AlreadyWritten | ||
} |
Oops, something went wrong.