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 : metrics_write( ctx ); /* ensures that shutdown state is written to metrics workspace before the tile actually shuts down */
151 0 : break;
152 0 : default:
153 0 : FD_LOG_ERR(( "unexpected control sig %lu", sig ));
154 0 : return;
155 0 : }
156 :
157 : /* 3. Acknowledge the control message, so the sender knows we received
158 : it. We must acknowledge after handling the control frag, because
159 : if it causes us to generate a malformed transition, that must be
160 : sent back to the snaprd controller before the acknowledgement. */
161 0 : fd_stem_publish( stem, 1UL, FD_SNAPSHOT_MSG_CTRL_ACK, 0UL, 0UL, 0UL, 0UL, 0UL );
162 0 : }
163 :
164 : static inline int
165 : handle_data_frag( fd_snapdc_tile_t * ctx,
166 : fd_stem_context_t * stem,
167 : ulong chunk,
168 0 : ulong sz ) {
169 0 : FD_TEST( ctx->state!=FD_SNAPDC_STATE_DONE );
170 :
171 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPDC_STATE_MALFORMED ) ) return 0;
172 :
173 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPDC_STATE_FINISHING ) ) {
174 : /* We thought the snapshot was finished (we already read the full
175 : frame) and then we got another data fragment from the reader.
176 : This means the snapshot has extra padding or garbage on the end,
177 : which we don't trust so just abandon it completely. */
178 0 : transition_malformed( ctx, stem );
179 0 : return 0;
180 0 : }
181 :
182 0 : FD_TEST( ctx->state==FD_SNAPDC_STATE_DECOMPRESSING );
183 0 : FD_TEST( chunk>=ctx->in.chunk0 && chunk<=ctx->in.wmark && sz<=ctx->in.mtu && sz>=ctx->in.frag_pos );
184 :
185 0 : uchar const * data = fd_chunk_to_laddr_const( ctx->in.wksp, chunk );
186 :
187 0 : uchar const * in = data+ctx->in.frag_pos;
188 0 : uchar * out = fd_chunk_to_laddr( ctx->out.wksp, ctx->out.chunk );
189 0 : ulong in_consumed = 0UL, out_produced = 0UL;
190 0 : ulong error = ZSTD_decompressStream_simpleArgs( ctx->zstd,
191 0 : out,
192 0 : ctx->out.mtu,
193 0 : &out_produced,
194 0 : in,
195 0 : sz-ctx->in.frag_pos,
196 0 : &in_consumed );
197 0 : if( FD_UNLIKELY( ZSTD_isError( error ) ) ) {
198 0 : transition_malformed( ctx, stem );
199 0 : return 0;
200 0 : }
201 :
202 0 : if( FD_LIKELY( out_produced ) ) {
203 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_DATA, ctx->out.chunk, out_produced, 0UL, 0UL, 0UL );
204 0 : ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, out_produced, ctx->out.chunk0, ctx->out.wmark );
205 0 : }
206 :
207 0 : ctx->in.frag_pos += in_consumed;
208 0 : FD_TEST( ctx->in.frag_pos<=sz );
209 :
210 0 : if( FD_LIKELY( ctx->full ) ) {
211 0 : ctx->metrics.full.compressed_bytes_read += in_consumed;
212 0 : ctx->metrics.full.decompressed_bytes_read += out_produced;
213 0 : } else {
214 0 : ctx->metrics.incremental.compressed_bytes_read += in_consumed;
215 0 : ctx->metrics.incremental.decompressed_bytes_read += out_produced;
216 0 : }
217 :
218 0 : if( FD_UNLIKELY( !error ) ) {
219 0 : if( FD_UNLIKELY( ctx->in.frag_pos!=sz ) ) {
220 : /* Zstandard finished decoding the snapshot frame (the whole
221 : snapshot is a single frame), but, the fragment we got from
222 : the snapshot reader has not been fully consumed, so there is
223 : some trailing padding or garbage at the end of the snapshot.
224 :
225 : This is not valid under the snapshot format and indicates a
226 : problem so we abandon the snapshot. */
227 0 : transition_malformed( ctx, stem );
228 0 : return 0;
229 0 : }
230 :
231 0 : ctx->state = FD_SNAPDC_STATE_FINISHING;
232 0 : }
233 :
234 0 : int maybe_more_output = out_produced==ctx->out.mtu || ctx->in.frag_pos<sz;
235 0 : if( FD_LIKELY( !maybe_more_output ) ) ctx->in.frag_pos = 0UL;
236 0 : return maybe_more_output;
237 0 : }
238 :
239 : static inline int
240 : returnable_frag( fd_snapdc_tile_t * ctx,
241 : ulong in_idx,
242 : ulong seq,
243 : ulong sig,
244 : ulong chunk,
245 : ulong sz,
246 : ulong ctl,
247 : ulong tsorig,
248 : ulong tspub,
249 0 : fd_stem_context_t * stem ) {
250 0 : (void)in_idx;
251 0 : (void)seq;
252 0 : (void)ctl;
253 0 : (void)tsorig;
254 0 : (void)tspub;
255 :
256 0 : FD_TEST( ctx->state!=FD_SNAPDC_STATE_SHUTDOWN );
257 :
258 0 : if( FD_LIKELY( sig==FD_SNAPSHOT_MSG_DATA ) ) return handle_data_frag( ctx, stem, chunk, sz );
259 0 : else handle_control_frag( ctx,stem, sig );
260 :
261 0 : return 0;
262 0 : }
263 :
264 : static void
265 : unprivileged_init( fd_topo_t * topo,
266 0 : fd_topo_tile_t * tile ) {
267 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
268 :
269 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
270 0 : fd_snapdc_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapdc_tile_t), sizeof(fd_snapdc_tile_t) );
271 0 : void * _zstd = FD_SCRATCH_ALLOC_APPEND( l, 32UL, ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
272 :
273 0 : ctx->full = 1;
274 0 : ctx->state = FD_SNAPDC_STATE_DECOMPRESSING;
275 0 : ctx->zstd = ZSTD_initStaticDStream( _zstd, ZSTD_estimateDStreamSize( ZSTD_WINDOW_SZ ) );
276 0 : FD_TEST( ctx->zstd );
277 0 : FD_TEST( ctx->zstd==_zstd );
278 :
279 0 : ctx->in.frag_pos = 0UL;
280 0 : fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
281 :
282 0 : if( FD_UNLIKELY( tile->in_cnt !=1UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu ins, expected 1", tile->in_cnt ));
283 0 : if( FD_UNLIKELY( tile->out_cnt!=2UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu outs, expected 2", tile->out_cnt ));
284 :
285 0 : fd_topo_link_t * writer_link = &topo->links[ tile->out_link_id[ 0UL ] ];
286 0 : ctx->out.wksp = topo->workspaces[ topo->objs[ writer_link->dcache_obj_id ].wksp_id ].wksp;
287 0 : ctx->out.chunk0 = fd_dcache_compact_chunk0( ctx->out.wksp, writer_link->dcache );
288 0 : ctx->out.wmark = fd_dcache_compact_wmark ( ctx->out.wksp, writer_link->dcache, writer_link->mtu );
289 0 : ctx->out.chunk = ctx->out.chunk0;
290 0 : ctx->out.mtu = writer_link->mtu;
291 :
292 0 : fd_topo_link_t const * in_link = &topo->links[ tile->in_link_id[ 0UL ] ];
293 0 : fd_topo_wksp_t const * in_wksp = &topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ];
294 0 : ctx->in.wksp = in_wksp->wksp;;
295 0 : ctx->in.chunk0 = fd_dcache_compact_chunk0( ctx->in.wksp, in_link->dcache );
296 0 : ctx->in.wmark = fd_dcache_compact_wmark( ctx->in.wksp, in_link->dcache, in_link->mtu );
297 0 : ctx->in.mtu = in_link->mtu;
298 :
299 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
300 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
301 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu",
302 0 : scratch_top - (ulong)scratch - scratch_footprint( tile ),
303 0 : scratch_top,
304 0 : (ulong)scratch + scratch_footprint( tile ) ));
305 0 : }
306 :
307 0 : #define STEM_BURST 3UL /* For control fragments, one downstream clone, one acknowledgement, and one malformed message */
308 0 : #define STEM_LAZY 1000L
309 :
310 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_snapdc_tile_t
311 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_snapdc_tile_t)
312 :
313 : #define STEM_CALLBACK_SHOULD_SHUTDOWN should_shutdown
314 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
315 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
316 :
317 : #include "../../disco/stem/fd_stem.c"
318 :
319 : fd_topo_run_tile_t fd_tile_snapdc = {
320 : .name = NAME,
321 : .scratch_align = scratch_align,
322 : .scratch_footprint = scratch_footprint,
323 : .unprivileged_init = unprivileged_init,
324 : .run = stem_run,
325 : };
326 :
327 : #undef NAME
|