21#ifndef A11_NODES_ASYNC_NODE_H_
22#define A11_NODES_ASYNC_NODE_H_
33#include <absl/base/thread_annotations.h>
34#include <absl/status/status.h>
35#include <absl/status/statusor.h>
36#include <absl/time/time.h>
45#include "thread/boost_primitives.h"
70class AsyncNode :
public std::enable_shared_from_this<AsyncNode> {
85 static absl::StatusOr<std::shared_ptr<AsyncNode>>
Create(
86 std::shared_ptr<stores::ChunkStore> store,
103 absl::StatusOr<std::string>
GetId()
const;
117 [[
nodiscard]] std::shared_ptr<data::SerializationRegistry>
126 std::shared_ptr<data::SerializationRegistry>
registry);
132 absl::StatusOr<std::shared_ptr<stores::ChunkStoreReader>>
reader();
138 absl::StatusOr<std::shared_ptr<stores::ChunkStoreWriter>>
writer();
160 std::optional<stores::ChunkStoreReaderOptions> options = std::nullopt);
215 data::Chunk chunk, std::optional<std::uint32_t> seq = std::nullopt,
241 template <
typename T>
243 const T& value, std::optional<std::uint32_t> seq = std::nullopt,
244 bool final =
false, std::string_view mimetype = {}) {
245 std::shared_ptr<data::SerializationRegistry>
registry;
247 thread::MutexLock
lock(&mu_);
250 absl::StatusOr<data::Chunk> chunk =
registry->ToChunk<
T>(value, mimetype);
254 return PutChunk(std::move(*chunk), seq,
final);
263 std::optional<std::uint32_t> seq = std::nullopt);
272 absl::Duration
timeout = absl::InfiniteDuration());
281 absl::Duration
timeout = absl::InfiniteDuration());
292 template <
typename T>
294 absl::Duration
timeout = absl::InfiniteDuration(),
301 absl::StatusOr<std::optional<data::NodeFragment>> fragment =
304 return fragment.status();
305 if (!fragment->has_value())
307 absl::StatusOr<const data::Chunk*> chunk = (*fragment)->GetChunk();
309 return chunk.status();
310 if ((*chunk)->IsNull()) {
311 if ((*fragment)->continued) {
312 return absl::FailedPreconditionError(
313 "A null stream marker must be final");
317 std::shared_ptr<data::SerializationRegistry>
registry;
322 absl::StatusOr<T> value =
325 return value.status();
326 return std::optional<T>(std::move(*value));
368 absl::Status
AttachStream(std::shared_ptr<net::WireStream> stream);
375 absl::Status
DetachStream(
const std::shared_ptr<net::WireStream>& stream);
394 AsyncNode(std::shared_ptr<stores::ChunkStore> store,
395 std::shared_ptr<data::SerializationRegistry>
registry,
398 : store_(std::
move(store)),
403 const std::shared_ptr<stores::ChunkStore> store_;
404 mutable thread::Mutex mu_;
406 ABSL_GUARDED_BY(mu_);
409 std::shared_ptr<stores::ChunkStoreReader>
reader_ ABSL_GUARDED_BY(mu_);
410 std::shared_ptr<stores::ChunkStoreWriter>
writer_ ABSL_GUARDED_BY(mu_);
A11's pluggable storage interface for streamed node data: an ordered, appendable log of fragments key...
A read cursor over a ChunkStore that pulls fragments out in order (or by arrival),...
A write cursor over a ChunkStore that admits chunks into the store in sequence, applying backpressure...
Shared handle to one asynchronous result.
Definition future.h:110
An asynchronous, ordered stream of chunks read from and written to A11.
Definition async_node.h:70
absl::Status SetSerializationRegistry(std::shared_ptr< data::SerializationRegistry > registry)
Replace the serialization registry.
Definition async_node.cc:77
std::shared_ptr< stores::ChunkStore > GetChunkStore() const
Return the underlying chunk store backing this node.
Definition async_node.cc:67
a11::Task WaitForBufferToDrain()
Wait for the write buffer to drain.
Definition async_node.cc:265
a11::Future< std::uint32_t > PutChunk(data::Chunk chunk, std::optional< std::uint32_t > seq=std::nullopt, bool final=false)
Enqueue a raw chunk into the stream.
Definition async_node.cc:210
stores::ChunkStoreWriterOptions GetWriterOptions() const
Return a copy of the writer's current options.
Definition async_node.cc:145
a11::Future< std::uint32_t > PutNullFinal(std::optional< std::uint32_t > seq=std::nullopt)
Close the stream with an explicit null terminator (no value).
Definition async_node.cc:229
stores::ChunkStoreReaderOptions GetReaderOptions() const
Return a copy of the reader's current options.
Definition async_node.cc:111
static absl::StatusOr< std::shared_ptr< AsyncNode > > Create(std::shared_ptr< stores::ChunkStore > store, std::shared_ptr< data::SerializationRegistry > serialization_registry=nullptr, stores::ChunkStoreReaderOptions reader_options={}, stores::ChunkStoreWriterOptions writer_options={})
Create a node over an existing chunk store.
Definition async_node.cc:36
a11::Task AbortWithStatus(absl::Status status)
Fail the stream with an error status.
Definition async_node.cc:280
absl::Status DetachStream(const std::shared_ptr< net::WireStream > &stream)
Stop mirroring chunks over a previously attached wire stream.
Definition async_node.cc:293
AsyncNode(const AsyncNode &)=delete
a11::Future< std::optional< data::Chunk > > NextChunk(absl::Duration timeout=absl::InfiniteDuration())
Read the next raw chunk.
Definition async_node.cc:246
absl::Status SetReaderOptions(stores::ChunkStoreReaderOptions options)
Replace the reader options.
Definition async_node.cc:116
absl::Status SetWriterOptions(stores::ChunkStoreWriterOptions options)
Replace the writer options.
Definition async_node.cc:150
a11::Future< std::uint32_t > Put(const T &value, std::optional< std::uint32_t > seq=std::nullopt, bool final=false, std::string_view mimetype={})
Serialize and write a typed value to the stream.
Definition async_node.h:242
void CancelWriter()
Cancel the writer, unblocking pending Put/drain awaits.
Definition async_node.cc:311
absl::Status GetWriterStatus() const
Return the current status of the node's writer.
Definition async_node.cc:171
a11::Future< std::optional< data::NodeFragment > > NextFragment(absl::Duration timeout=absl::InfiniteDuration())
Read the next raw fragment.
Definition async_node.cc:237
absl::Status GetReaderStatus() const
Return the current status of the node's reader.
Definition async_node.cc:162
absl::Status ResetReader(std::optional< stores::ChunkStoreReaderOptions > options=std::nullopt)
Rewind/reconfigure the reader (e.g.
Definition async_node.cc:128
AsyncNode & operator=(const AsyncNode &)=delete
std::optional< absl::Status > GetWriterAbortStatus() const
Return the status the writer was aborted with.
Definition async_node.cc:183
absl::StatusOr< std::shared_ptr< stores::ChunkStoreWriter > > writer()
Return the node's chunk store writer (the producing half).
Definition async_node.cc:99
void CancelReader()
Cancel the reader, unblocking pending Next* awaits.
Definition async_node.cc:301
void Cancel()
Cancel both reader and writer, tearing down all pending streaming operations on the node at once.
Definition async_node.cc:321
absl::Status AttachStream(std::shared_ptr< net::WireStream > stream)
Tee this node's stored chunks onto a wire stream.
Definition async_node.cc:286
a11::Task DrainAndClose()
Flush all buffered chunks and close the writer.
Definition async_node.cc:274
a11::Future< bool > IsWritable()
Report whether the node can currently accept writes.
Definition async_node.cc:192
std::shared_ptr< data::SerializationRegistry > serialization_registry() const
Return the registry used to (de)serialize typed values.
Definition async_node.cc:71
a11::Future< std::optional< T > > NextObject(absl::Duration timeout=absl::InfiniteDuration(), std::vector< std::string > mimetype_patterns={})
Read and deserialize the next value.
Definition async_node.h:293
absl::StatusOr< std::shared_ptr< stores::ChunkStoreReader > > reader()
Return the node's chunk store reader (the consuming half).
Definition async_node.cc:87
absl::StatusOr< std::string > GetId() const
Return the node's stable identifier.
Definition async_node.cc:63
a11::Future< std::uint32_t > PutFragment(data::NodeFragment fragment)
Enqueue a fragment carrying its own sequence and final flag.
Definition async_node.cc:220
Completion values used by every asynchronous A11 operation.
Future< T > SubmitWithCancellationHook(absl::AnyInvocable< absl::StatusOr< T >() && > work, std::function< void()> cancellation_hook, thread::TreeOptions tree_options)
Run work on A11's fiber pool with application-specific cancellation.
Definition executor.h:30
Type-and-mimetype indexed serialization of values to/from Chunks.
A unit of data: bytes plus optional descriptive metadata.
Definition types.h:95
One piece of a node's stream: an inline chunk or a node reference.
Definition types.h:167
Tunables controlling how a ChunkStoreReader delivers and buffers fragments.
Definition chunk_store_reader.h:37
Tunables controlling how a ChunkStoreWriter batches and buffers chunks.
Definition chunk_store_writer.h:40
A11's core wire value types: chunks, node fragments and messages.