A11 (C++ runtime)
Native C++ implementation of the A11 action and streaming runtime
Loading...
Searching...
No Matches
async_node.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors.
2
21#ifndef A11_NODES_ASYNC_NODE_H_
22#define A11_NODES_ASYNC_NODE_H_
23
24#include <cstddef>
25#include <cstdint>
26#include <memory>
27#include <optional>
28#include <string>
29#include <string_view>
30#include <utility>
31#include <vector>
32
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>
37
41#include "a11/data/types.h"
45#include "thread/boost_primitives.h"
46
47namespace a11::net {
48class WireStream;
49} // namespace a11::net
50
51namespace a11::nodes {
52
70class AsyncNode : public std::enable_shared_from_this<AsyncNode> {
71 public:
85 static absl::StatusOr<std::shared_ptr<AsyncNode>> Create(
86 std::shared_ptr<stores::ChunkStore> store,
87 std::shared_ptr<data::SerializationRegistry> serialization_registry =
88 nullptr,
91
92 ~AsyncNode() = default;
93
94 AsyncNode(const AsyncNode&) = delete;
95 AsyncNode& operator=(const AsyncNode&) = delete;
96
103 absl::StatusOr<std::string> GetId() const;
104
111 [[nodiscard]] std::shared_ptr<stores::ChunkStore> GetChunkStore() const;
112
117 [[nodiscard]] std::shared_ptr<data::SerializationRegistry>
119
125 absl::Status SetSerializationRegistry(
126 std::shared_ptr<data::SerializationRegistry> registry);
127
132 absl::StatusOr<std::shared_ptr<stores::ChunkStoreReader>> reader();
133
138 absl::StatusOr<std::shared_ptr<stores::ChunkStoreWriter>> writer();
139
145
152
159 absl::Status ResetReader(
160 std::optional<stores::ChunkStoreReaderOptions> options = std::nullopt);
161
167
174
180 [[nodiscard]] absl::Status GetReaderStatus() const;
181
187 [[nodiscard]] absl::Status GetWriterStatus() const;
188
194 [[nodiscard]] std::optional<absl::Status> GetWriterAbortStatus() const;
195
202
215 data::Chunk chunk, std::optional<std::uint32_t> seq = std::nullopt,
216 bool final = false);
217
224
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;
246 {
247 thread::MutexLock lock(&mu_);
249 }
250 absl::StatusOr<data::Chunk> chunk = registry->ToChunk<T>(value, mimetype);
251 if (!chunk.ok()) {
252 return a11::FailedFuture<std::uint32_t>(chunk.status());
253 }
254 return PutChunk(std::move(*chunk), seq, final);
255 }
256
263 std::optional<std::uint32_t> seq = std::nullopt);
264
272 absl::Duration timeout = absl::InfiniteDuration());
273
281 absl::Duration timeout = absl::InfiniteDuration());
282
292 template <typename T>
294 absl::Duration timeout = absl::InfiniteDuration(),
295 std::vector<std::string> mimetype_patterns = {}) {
296 std::shared_ptr<AsyncNode> self = shared_from_this();
298 [self = std::move(self), timeout,
299 mimetype_patterns = std::move(
300 mimetype_patterns)]() mutable -> absl::StatusOr<std::optional<T>> {
301 absl::StatusOr<std::optional<data::NodeFragment>> fragment =
302 self->NextFragment(timeout).Await();
303 if (!fragment.ok())
304 return fragment.status();
305 if (!fragment->has_value())
306 return std::nullopt;
307 absl::StatusOr<const data::Chunk*> chunk = (*fragment)->GetChunk();
308 if (!chunk.ok())
309 return chunk.status();
310 if ((*chunk)->IsNull()) {
311 if ((*fragment)->continued) {
312 return absl::FailedPreconditionError(
313 "A null stream marker must be final");
314 }
315 return std::nullopt;
316 }
317 std::shared_ptr<data::SerializationRegistry> registry;
318 {
319 thread::MutexLock lock(&self->mu_);
320 registry = self->serialization_registry_;
321 }
322 absl::StatusOr<T> value =
323 registry->FromChunk<T>(**chunk, mimetype_patterns);
324 if (!value.ok())
325 return value.status();
326 return std::optional<T>(std::move(*value));
327 });
328 }
329
336
349
356 a11::Task AbortWithStatus(absl::Status status);
357
368 absl::Status AttachStream(std::shared_ptr<net::WireStream> stream);
369
375 absl::Status DetachStream(const std::shared_ptr<net::WireStream>& stream);
376
380 void CancelReader();
381
385 void CancelWriter();
386
391 void Cancel();
392
393 private:
394 AsyncNode(std::shared_ptr<stores::ChunkStore> store,
395 std::shared_ptr<data::SerializationRegistry> registry,
396 stores::ChunkStoreReaderOptions reader_options,
397 stores::ChunkStoreWriterOptions writer_options)
398 : store_(std::move(store)),
402
403 const std::shared_ptr<stores::ChunkStore> store_;
404 mutable thread::Mutex mu_;
405 std::shared_ptr<data::SerializationRegistry> serialization_registry_
406 ABSL_GUARDED_BY(mu_);
407 stores::ChunkStoreReaderOptions reader_options_ ABSL_GUARDED_BY(mu_);
408 stores::ChunkStoreWriterOptions writer_options_ 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_);
411};
412
413} // namespace a11::nodes
414
415#endif // A11_NODES_ASYNC_NODE_H_
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.
Definition action.h:46
Definition action.h:50
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.