Line data Source code
1 : #include "utils/fd_ssctrl.h"
2 :
3 : #include "../../disco/topo/fd_topo.h"
4 : #include "../../disco/metrics/fd_metrics.h"
5 :
6 : #define ZSTD_STATIC_LINKING_ONLY
7 : #include <zstd.h>
8 :
9 : #define NAME "snapdc"
10 :
11 0 : #define ZSTD_WINDOW_SZ (1UL<<25UL) /* 32MiB */
12 :
13 : /* The snapdc tile is a state machine that decompresses the full and
14 : optionally incremental snapshot byte stream that it receives from the
15 : snaprd tile.
16 :
17 : snaprd may send a reset notification, which causes snapdc to reset
18 : its decompressor state to waiting for either the full or incremental
19 : snapshot respectively. */
20 :
21 0 : #define FD_SNAPDC_STATE_DECOMPRESSING (0) /* We are in the process of decompressing a valid stream */
22 0 : #define FD_SNAPDC_STATE_FINISHING (1) /* The frame has been fully decompressed, we are waiting to make sure the snapshot has no more data */
23 0 : #define FD_SNAPDC_STATE_MALFORMED (2) /* The decompression stream is malformed, we are waiting for a reset notification */
24 0 : #define FD_SNAPDC_STATE_DONE (3) /* The decompression stream is done, the tile is waiting for a shutdown message */
25 0 : #define FD_SNAPDC_STATE_SHUTDOWN (4) /* The tile is done, been told to shut down, and has likely already exited */
26 :
27 : struct fd_snapdc_tile {
28 : int full;
29 : int state;
30 :
31 : ZSTD_DCtx * zstd;
32 :
33 : struct {
34 : fd_wksp_t * wksp;
35 : ulong chunk0;
36 : ulong wmark;
37 : ulong mtu;
38 : ulong frag_pos;
39 : } in;
40 :
41 : struct {
42 : fd_wksp_t * wksp;
43 : ulong chunk0;
44 : ulong wmark;
45 : ulong chunk;
46 : ulong mtu;
47 : } out;
48 :
49 : struct {
50 : struct {
51 : ulong compressed_bytes_read;
52 : ulong decompressed_bytes_read;
53 : } full;
54 :
55 : struct {
56 : ulong compressed_bytes_read;
57 : ulong decompressed_bytes_read;
58 : } incremental;
59 : } metrics;
60 : };
61 : typedef struct fd_snapdc_tile fd_snapdc_tile_t;
62 :
63 : FD_FN_PURE static ulong
64 0 : scratch_align( void ) {
65 0 : return alignof(fd_snapdc_tile_t);
66 0 : }
67 :
68 : FD_FN_PURE static ulong
69 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
70 0 : (void)tile;
71 0 : ulong l = FD_LAYOUT_INIT;
72 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_snapdc_tile_t), sizeof(fd_snapdc_tile_t) );
73 0 : l = FD_LAYOUT_APPEND( l, 32UL, ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
74 0 : return FD_LAYOUT_FINI( l, scratch_align() );
75 0 : }
76 :
77 : static inline int
78 0 : should_shutdown( fd_snapdc_tile_t * ctx ) {
79 0 : return ctx->state==FD_SNAPDC_STATE_SHUTDOWN;
80 0 : }
81 :
82 : static void
83 0 : metrics_write( fd_snapdc_tile_t * ctx ) {
84 0 : FD_MGAUGE_SET( SNAPDC, FULL_COMPRESSED_BYTES_READ, ctx->metrics.full.compressed_bytes_read );
85 0 : FD_MGAUGE_SET( SNAPDC, FULL_DECOMPRESSED_BYTES_READ, ctx->metrics.full.decompressed_bytes_read );
86 :
87 0 : FD_MGAUGE_SET( SNAPDC, INCREMENTAL_COMPRESSED_BYTES_READ, ctx->metrics.incremental.compressed_bytes_read );
88 0 : FD_MGAUGE_SET( SNAPDC, INCREMENTAL_DECOMPRESSED_BYTES_READ, ctx->metrics.incremental.decompressed_bytes_read );
89 :
90 0 : FD_MGAUGE_SET( SNAPDC, STATE, (ulong)(ctx->state) );
91 0 : }
92 :
93 : static inline void
94 : transition_malformed( fd_snapdc_tile_t * ctx,
95 0 : fd_stem_context_t * stem ) {
96 0 : ctx->state = FD_SNAPDC_STATE_MALFORMED;
97 0 : ctx->in.frag_pos = 0UL;
98 0 : fd_stem_publish( stem, 1UL, FD_SNAPSHOT_MSG_CTRL_MALFORMED, 0UL, 0UL, 0UL, 0UL, 0UL );
99 0 : }
100 :
101 : static inline void
102 : handle_control_frag( fd_snapdc_tile_t * ctx,
103 : fd_stem_context_t * stem,
104 0 : ulong sig ) {
105 : /* 1. Pass the control message downstream to the next consumer. */
106 0 : fd_stem_publish( stem, 0UL, sig, ctx->out.chunk, 0UL, 0UL, 0UL, 0UL );
107 0 : ulong error = ZSTD_DCtx_reset( ctx->zstd, ZSTD_reset_session_only );
108 0 : if( FD_UNLIKELY( ZSTD_isError( error ) ) ) FD_LOG_ERR(( "ZSTD_DCtx_reset failed (%lu-%s)", error, ZSTD_getErrorName( error ) ));
109 :
110 : /* 2. Check if the control message is actually valid given the state
111 : machine, and if not, return a malformed message to the sender. */
112 0 : switch( sig ) {
113 0 : case FD_SNAPSHOT_MSG_CTRL_RESET_FULL:
114 0 : ctx->state = FD_SNAPDC_STATE_DECOMPRESSING;
115 0 : ctx->full = 1;
116 0 : ctx->metrics.full.compressed_bytes_read = 0UL;
117 0 : ctx->metrics.full.decompressed_bytes_read = 0UL;
118 0 : ctx->metrics.incremental.compressed_bytes_read = 0UL;
119 0 : ctx->metrics.incremental.decompressed_bytes_read = 0UL;
120 0 : break;
121 0 : case FD_SNAPSHOT_MSG_CTRL_RESET_INCREMENTAL:
122 0 : ctx->state = FD_SNAPDC_STATE_DECOMPRESSING;
123 0 : ctx->full = 0;
124 0 : ctx->metrics.full.compressed_bytes_read = 0UL;
125 0 : ctx->metrics.full.decompressed_bytes_read = 0UL;
126 0 : ctx->metrics.incremental.compressed_bytes_read = 0UL;
127 0 : ctx->metrics.incremental.decompressed_bytes_read = 0UL;
128 0 : break;
129 0 : case FD_SNAPSHOT_MSG_CTRL_EOF_FULL:
130 0 : FD_TEST( ctx->full );
131 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPDC_STATE_MALFORMED ) ) break;
132 0 : else if( FD_UNLIKELY( ctx->state==FD_SNAPDC_STATE_DECOMPRESSING ) ) {
133 0 : transition_malformed( ctx, stem );
134 0 : break;
135 0 : }
136 0 : ctx->state = FD_SNAPDC_STATE_DECOMPRESSING;
137 0 : ctx->full = 0;
138 0 : break;
139 0 : case FD_SNAPSHOT_MSG_CTRL_DONE:
140 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPDC_STATE_MALFORMED ) ) break;
141 0 : else if( FD_UNLIKELY( ctx->state==FD_SNAPDC_STATE_DECOMPRESSING ) ) {
142 0 : transition_malformed( ctx, stem );
143 0 : break;
144 0 : }
145 0 : ctx->state = FD_SNAPDC_STATE_DONE;
146 0 : break;
147 0 : case FD_SNAPSHOT_MSG_CTRL_SHUTDOWN:
148 0 : FD_TEST( ctx->state==FD_SNAPDC_STATE_DONE );
149 0 : ctx->state = FD_SNAPDC_STATE_SHUTDOWN;
150 0 : break;
151 0 : default:
152 0 : FD_LOG_ERR(( "unexpected control sig %lu", sig ));
153 0 : return;
154 0 : }
155 :
156 : /* 3. Acknowledge the control message, so the sender knows we received
157 : it. We must acknowledge after handling the control frag, because
158 : if it causes us to generate a malformed transition, that must be
159 : sent back to the snaprd controller before the acknowledgement. */
160 0 : fd_stem_publish( stem, 1UL, FD_SNAPSHOT_MSG_CTRL_ACK, 0UL, 0UL, 0UL, 0UL, 0UL );
161 0 : }
162 :
163 : static inline int
164 : handle_data_frag( fd_snapdc_tile_t * ctx,
165 : fd_stem_context_t * stem,
166 : ulong chunk,
167 0 : ulong sz ) {
168 0 : FD_TEST( ctx->state!=FD_SNAPDC_STATE_DONE );
169 :
170 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPDC_STATE_MALFORMED ) ) return 0;
171 :
172 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPDC_STATE_FINISHING ) ) {
173 : /* We thought the snapshot was finished (we already read the full
174 : frame) and then we got another data fragment from the reader.
175 : This means the snapshot has extra padding or garbage on the end,
176 : which we don't trust so just abandon it completely. */
177 0 : transition_malformed( ctx, stem );
178 0 : return 0;
179 0 : }
180 :
181 0 : FD_TEST( ctx->state==FD_SNAPDC_STATE_DECOMPRESSING );
182 0 : FD_TEST( chunk>=ctx->in.chunk0 && chunk<=ctx->in.wmark && sz<=ctx->in.mtu && sz>=ctx->in.frag_pos );
183 :
184 0 : uchar const * data = fd_chunk_to_laddr_const( ctx->in.wksp, chunk );
185 :
186 0 : uchar const * in = data+ctx->in.frag_pos;
187 0 : uchar * out = fd_chunk_to_laddr( ctx->out.wksp, ctx->out.chunk );
188 0 : ulong in_consumed = 0UL, out_produced = 0UL;
189 0 : ulong error = ZSTD_decompressStream_simpleArgs( ctx->zstd,
190 0 : out,
191 0 : ctx->out.mtu,
192 0 : &out_produced,
193 0 : in,
194 0 : sz-ctx->in.frag_pos,
195 0 : &in_consumed );
196 0 : if( FD_UNLIKELY( ZSTD_isError( error ) ) ) {
197 0 : transition_malformed( ctx, stem );
198 0 : return 0;
199 0 : }
200 :
201 0 : if( FD_LIKELY( out_produced ) ) {
202 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_DATA, ctx->out.chunk, out_produced, 0UL, 0UL, 0UL );
203 0 : ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, out_produced, ctx->out.chunk0, ctx->out.wmark );
204 0 : }
205 :
206 0 : ctx->in.frag_pos += in_consumed;
207 0 : FD_TEST( ctx->in.frag_pos<=sz );
208 :
209 0 : if( FD_LIKELY( ctx->full ) ) {
210 0 : ctx->metrics.full.compressed_bytes_read += in_consumed;
211 0 : ctx->metrics.full.decompressed_bytes_read += out_produced;
212 0 : } else {
213 0 : ctx->metrics.incremental.compressed_bytes_read += in_consumed;
214 0 : ctx->metrics.incremental.decompressed_bytes_read += out_produced;
215 0 : }
216 :
217 0 : if( FD_UNLIKELY( !error ) ) {
218 0 : if( FD_UNLIKELY( ctx->in.frag_pos!=sz ) ) {
219 : /* Zstandard finished decoding the snapshot frame (the whole
220 : snapshot is a single frame), but, the fragment we got from
221 : the snapshot reader has not been fully consumed, so there is
222 : some trailing padding or garbage at the end of the snapshot.
223 :
224 : This is not valid under the snapshot format and indicates a
225 : problem so we abandon the snapshot. */
226 0 : transition_malformed( ctx, stem );
227 0 : return 0;
228 0 : }
229 :
230 0 : ctx->state = FD_SNAPDC_STATE_FINISHING;
231 0 : }
232 :
233 0 : int maybe_more_output = out_produced==ctx->out.mtu || ctx->in.frag_pos<sz;
234 0 : if( FD_LIKELY( !maybe_more_output ) ) ctx->in.frag_pos = 0UL;
235 0 : return maybe_more_output;
236 0 : }
237 :
238 : static inline int
239 : returnable_frag( fd_snapdc_tile_t * ctx,
240 : ulong in_idx,
241 : ulong seq,
242 : ulong sig,
243 : ulong chunk,
244 : ulong sz,
245 : ulong tsorig,
246 : ulong tspub,
247 0 : fd_stem_context_t * stem ) {
248 0 : (void)in_idx;
249 0 : (void)seq;
250 0 : (void)sig;
251 0 : (void)tsorig;
252 0 : (void)tspub;
253 :
254 0 : FD_TEST( ctx->state!=FD_SNAPDC_STATE_SHUTDOWN );
255 :
256 0 : if( FD_LIKELY( sig==FD_SNAPSHOT_MSG_DATA ) ) return handle_data_frag( ctx, stem, chunk, sz );
257 0 : else handle_control_frag( ctx,stem, sig );
258 :
259 0 : return 0;
260 0 : }
261 :
262 : static void
263 : unprivileged_init( fd_topo_t * topo,
264 0 : fd_topo_tile_t * tile ) {
265 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
266 :
267 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
268 0 : fd_snapdc_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapdc_tile_t), sizeof(fd_snapdc_tile_t) );
269 0 : void * _zstd = FD_SCRATCH_ALLOC_APPEND( l, 32UL, ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
270 :
271 0 : ctx->full = 1;
272 0 : ctx->state = FD_SNAPDC_STATE_DECOMPRESSING;
273 0 : ctx->zstd = ZSTD_initStaticDStream( _zstd, ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
274 0 : FD_TEST( ctx->zstd );
275 0 : FD_TEST( ctx->zstd==_zstd );
276 :
277 0 : ctx->in.frag_pos = 0UL;
278 0 : fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
279 :
280 0 : if( FD_UNLIKELY( tile->in_cnt !=1UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu ins, expected 1", tile->in_cnt ));
281 0 : if( FD_UNLIKELY( tile->out_cnt!=2UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu outs, expected 2", tile->out_cnt ));
282 :
283 0 : fd_topo_link_t * writer_link = &topo->links[ tile->out_link_id[ 0UL ] ];
284 0 : ctx->out.wksp = topo->workspaces[ topo->objs[ writer_link->dcache_obj_id ].wksp_id ].wksp;
285 0 : ctx->out.chunk0 = fd_dcache_compact_chunk0( ctx->out.wksp, writer_link->dcache );
286 0 : ctx->out.wmark = fd_dcache_compact_wmark ( ctx->out.wksp, writer_link->dcache, writer_link->mtu );
287 0 : ctx->out.chunk = ctx->out.chunk0;
288 0 : ctx->out.mtu = writer_link->mtu;
289 :
290 0 : fd_topo_link_t const * in_link = &topo->links[ tile->in_link_id[ 0UL ] ];
291 0 : fd_topo_wksp_t const * in_wksp = &topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ];
292 0 : ctx->in.wksp = in_wksp->wksp;;
293 0 : ctx->in.chunk0 = fd_dcache_compact_chunk0( ctx->in.wksp, in_link->dcache );
294 0 : ctx->in.wmark = fd_dcache_compact_wmark( ctx->in.wksp, in_link->dcache, in_link->mtu );
295 0 : ctx->in.mtu = in_link->mtu;
296 :
297 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
298 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
299 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu",
300 0 : scratch_top - (ulong)scratch - scratch_footprint( tile ),
301 0 : scratch_top,
302 0 : (ulong)scratch + scratch_footprint( tile ) ));
303 0 : }
304 :
305 0 : #define STEM_BURST 3UL /* For control fragments, one downstream clone, one acknowledgement, and one malformed message */
306 0 : #define STEM_LAZY 1000L
307 :
308 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_snapdc_tile_t
309 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_snapdc_tile_t)
310 :
311 : #define STEM_CALLBACK_SHOULD_SHUTDOWN should_shutdown
312 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
313 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
314 :
315 : #include "../../disco/stem/fd_stem.c"
316 :
317 : fd_topo_run_tile_t fd_tile_snapdc = {
318 : .name = NAME,
319 : .scratch_align = scratch_align,
320 : .scratch_footprint = scratch_footprint,
321 : .unprivileged_init = unprivileged_init,
322 : .run = stem_run,
323 : };
324 :
325 : #undef NAME
|