LCOV - code coverage report
Current view: top level - discof/restore/utils - fd_ssctrl.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 1 50 2.0 %
Date: 2026-08-14 04:54:57 Functions: 0 33 0.0 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_discof_restore_utils_fd_ssctrl_h
       2             : #define HEADER_fd_src_discof_restore_utils_fd_ssctrl_h
       3             : 
       4             : #include "../../../util/net/fd_net_headers.h"
       5             : #include "../../../flamenco/runtime/fd_runtime_const.h"
       6             : 
       7             : /* The snapshot tiles have a somewhat involved state machine, which is
       8             :    controlled by snapct.  Imagine first the following sequence:
       9             : 
      10             :     1. snapct is reading a full snapshot from the network and sends some
      11             :        data to snapdc to be decompressed.
      12             :     2. snapct hits a network error, and resets the connection to a new
      13             :        peer.
      14             :     3. The decompressor fails on data from the old peer, and sends a
      15             :        malformed message to snapct.
      16             :     4. snapct receives the malformed message, and abandons the new
      17             :        connection, even though it was not malformed.
      18             : 
      19             :    There are basically two ways to prevent this.  Option A is the tiles
      20             :    can pass not just control messages to one another, but also tag them
      21             :    with some xid indicating which "attempt" the control message is for.
      22             : 
      23             :    This is pretty hard to reason about, and the state machine can grow
      24             :    quite complicated.
      25             : 
      26             :    There's an easier way: the tiles just are fully synchronized with
      27             :    snapct.  Whatever "attempt" snapct is on, we ensure all other tiles
      28             :    are on it too.  This means when any tile fails a snapshot, all tiles
      29             :    must fail it and fully flush all frags in the pipeline before snapct
      30             :    can proceed with a new attempt.
      31             : 
      32             :    The control flow then is basically,
      33             : 
      34             :      1. All tiles start in the IDLE state.
      35             :      2. snapct initializes the pipeline by sending an INIT message.
      36             :         Each tile enters the PROCESSING state and then forwards the INIT
      37             :         message down the pipeline.  When snapct receives this INIT
      38             :         message, the entire pipeline is in PROCESSING state.
      39             :      3. Tiles continue to process data / frags as applicable.  If an
      40             :         error occurs, the tile enters the ERROR state and also sends an
      41             :         ERROR message downstream.  All downstream tiles also enter the
      42             :         ERROR state and forward the message.  Note that upstream tiles
      43             :         will not be in an ERROR state and will continue producing frags.
      44             :         When snapct receives the ERROR message, it will send a FAIL
      45             :         message.  snapct then waits for this FAIL message to be
      46             :         propagated through the pipeline and received back.  snapct
      47             :         also guarantees to flush any pending load data in the
      48             :         pipeline.  It then knows that all tiles are synchronized back
      49             :         in an IDLE state and it can try again with a new INIT.
      50             :      4. Once all snapshot data has entered the pipeline, snapct sends
      51             :         a FINI message through the pipeline and waits for it to be
      52             :         received back.  This synchronizes tiles to finish any
      53             :         remaining in-flight work.  It then sends either a NEXT or a
      54             :         DONE message through the pipeline, again waiting for it to be
      55             :         received back.  NEXT means another snapshot follows, whereas
      56             :         DONE means a shutdown will follow.
      57             : 
      58             :    That keeps the tiles in lockstep, and simplifies the state machine
      59             :    to a manageable level.
      60             : 
      61             :    In more detail, all tiles in the feedback loop of the pipeline must
      62             :    comply with the following rules; snapct enforces them; tiles outside
      63             :    of the feedback loop may support a subset of these:
      64             :      - Allowed state transitions on given control message:
      65             :         IDLE       to PROCESSING: on INIT_FULL / INIT_INCR ctrl msg.
      66             :         PROCESSING to FINISHING : on FINI ctrl msg (*).
      67             :         FINISHING  to IDLE      : on NEXT / DONE ctrl msg.
      68             :         IDLE       to SHUTDOWN  : on SHUTDOWN ctrl msg.
      69             :      - Control messages that apply to all states (except SHUTDOWN):
      70             :         On ERROR msg, always transition to ERROR.
      71             :         On FAIL msg, always transition to IDLE.
      72             :      - When in SHUTDOWN state, no data or control message is allowed.
      73             :      - Error handling:
      74             :         A tile that enters ERROR state on its own must forward an
      75             :         ERROR control message and discard any incoming message.  When
      76             :         in ERROR state, data is discarded, and only a FAIL control
      77             :         message is processed and forwarded (all others are discarded).
      78             :         As a result, only one ERROR message can propagate through the
      79             :         pipeline at any given time.
      80             :      - Holding onto a control message:
      81             :         A tile may hold onto a control message and forward it later
      82             :         after performing some asynchronous routine.  The tile's state
      83             :         transition may also be deferred until that control message is
      84             :         forwarded.
      85             :      - (*) A tile may self-transition from PROCESSING to FINISHING
      86             :         early, if it can detect end-of-stream.  The FINI message is
      87             :         used to synchronize the transition.
      88             : 
      89             :    Each non-ERROR control message is only generated once in snapct
      90             :    and will not be re-sent.  The pipeline will be locked on flushing
      91             :    that control message until all tiles forward it on, or an ERROR
      92             :    message is triggered by any of the tiles and forwarded. */
      93             : 
      94             : /* MTU for the snapshot data stream links (snapld_dc, snapdc_in).
      95             :    Producers adapt to the link mtu, so the value just needs to exceed
      96             :    the control structs.  64 KiB - 128 fills one dcache slot exactly
      97             :    and keeps a depth-16384 link under a gigantic page boundary. */
      98           0 : #define FD_SNAPSHOT_DATA_MTU                   (65408UL)
      99             : 
     100           0 : #define FD_SNAPSHOT_STATE_IDLE                 (0UL) /* Performing no work and should receive no data frags */
     101           0 : #define FD_SNAPSHOT_STATE_PROCESSING           (1UL) /* Performing usual work, no errors / EoF condition encountered */
     102           0 : #define FD_SNAPSHOT_STATE_FINISHING            (2UL) /* Tile has observed EoF, expects no additional data frags */
     103           0 : #define FD_SNAPSHOT_STATE_ERROR                (3UL) /* Some error occurred, will wait for a FAIL command to reset */
     104           0 : #define FD_SNAPSHOT_STATE_SHUTDOWN             (4UL) /* All work finished, tile can perform final cleanup and exit */
     105             : 
     106           0 : #define FD_SNAPSHOT_MSG_DATA                   (0UL) /* Fragment represents some snapshot data */
     107           0 : #define FD_SNAPSHOT_MSG_META                   (1UL) /* Fragment represents a fd_ssctrl_meta_t message */
     108             : 
     109           0 : #define FD_SNAPSHOT_MSG_CTRL_INIT_FULL         (2UL) /* Pipeline should start processing a full snapshot */
     110           0 : #define FD_SNAPSHOT_MSG_CTRL_INIT_INCR         (3UL) /* Pipeline should start processing an incremental snapshot */
     111           0 : #define FD_SNAPSHOT_MSG_CTRL_FAIL              (4UL) /* Current snapshot failed, undo work and reset to idle state */
     112           0 : #define FD_SNAPSHOT_MSG_CTRL_NEXT              (5UL) /* Current snapshot succeeded, commit work, go idle, and expect another snapshot */
     113           0 : #define FD_SNAPSHOT_MSG_CTRL_DONE              (6UL) /* Current snapshot succeeded, commit work, go idle, and expect shutdown */
     114           0 : #define FD_SNAPSHOT_MSG_CTRL_SHUTDOWN          (7UL) /* Snapshot load successful, no work left to do, perform final cleanup and shut down*/
     115           0 : #define FD_SNAPSHOT_MSG_CTRL_ERROR             (8UL) /* Some tile encountered an error with the current stream */
     116           0 : #define FD_SNAPSHOT_MSG_CTRL_FINI              (9UL) /* Current snapshot has been fully loaded, finish processing */
     117             : 
     118             : /* snapld -> snapct (via snapld_dc) */
     119          15 : #define FD_SNAPSHOT_MSG_LOAD_COMPLETE         (10UL) /* snapld finished reading/downloading all data */
     120             : 
     121             : /* Sent by snapct to tell snapld whether to load a local file or
     122             :    download from a particular external peer. */
     123             : typedef struct fd_ssctrl_init {
     124             :   int           file;
     125             :   int           zstd;
     126             :   ulong         slot; /* slot advertised by the snapshot peer */
     127             :   fd_ip4_port_t addr;
     128             :   uchar         snapshot_hash[ FD_HASH_FOOTPRINT ]; /* advertised snapshot hash from snapshot file name */
     129             :   char          hostname[ 256UL ];
     130             :   char          path[ PATH_MAX ];
     131             :   ulong         path_len;
     132             :   int           is_https;
     133             :   int           is_redirect; /* 1 if using well-known redirect path */
     134             :   ulong         file_sz;     /* file size in bytes (file loads only, 0 for HTTP) */
     135             : } fd_ssctrl_init_t;
     136             : 
     137             : /* Sent by snapld to tell snapct metadata about a downloaded snapshot. */
     138             : typedef struct fd_ssctrl_meta {
     139             :   ulong total_sz;
     140             :   ulong resolved_slot;
     141             :   uchar resolved_hash[ FD_HASH_FOOTPRINT ];
     142             :   char  resolved_name[ PATH_MAX ];
     143             : } fd_ssctrl_meta_t;
     144             : 
     145             : struct fd_snapshot_account_hdr {
     146             :   uchar   pubkey[ FD_PUBKEY_FOOTPRINT ];
     147             :   uchar   owner[ FD_PUBKEY_FOOTPRINT ];
     148             :   ulong   lamports;
     149             :   uchar   executable;
     150             :   ulong   data_len;
     151             : };
     152             : typedef struct fd_snapshot_account_hdr fd_snapshot_account_hdr_t;
     153             : 
     154             : /* fd_snapshot_account_hdr_init initializes a fd_snapshot_account_hdr_t struct
     155             :    with the appropriate account metadata fields. */
     156             : static inline void
     157             : fd_snapshot_account_hdr_init( fd_snapshot_account_hdr_t * account,
     158             :                            uchar const                    pubkey[ FD_PUBKEY_FOOTPRINT ],
     159             :                            uchar const                    owner[ FD_PUBKEY_FOOTPRINT ],
     160             :                            ulong                          lamports,
     161             :                            uchar                          executable,
     162           0 :                            ulong                          data_len ) {
     163           0 :   fd_memcpy( account->pubkey, pubkey, FD_PUBKEY_FOOTPRINT );
     164           0 :   fd_memcpy( account->owner,  owner,  FD_PUBKEY_FOOTPRINT );
     165           0 :   account->lamports   = lamports;
     166           0 :   account->executable = executable;
     167           0 :   account->data_len   = data_len;
     168           0 : }
     169             : 
     170             : static inline const char *
     171           0 : fd_ssctrl_state_str( ulong state ) {
     172           0 :   switch( state ) {
     173           0 :     case FD_SNAPSHOT_STATE_IDLE:        return "idle";
     174           0 :     case FD_SNAPSHOT_STATE_PROCESSING:  return "processing";
     175           0 :     case FD_SNAPSHOT_STATE_FINISHING:   return "finishing";
     176           0 :     case FD_SNAPSHOT_STATE_ERROR:       return "error";
     177           0 :     case FD_SNAPSHOT_STATE_SHUTDOWN:    return "shutdown";
     178           0 :     default:                            return "unknown";
     179           0 :   }
     180           0 : }
     181             : 
     182             : static inline const char *
     183           0 : fd_ssctrl_msg_ctrl_str( ulong sig ) {
     184           0 :   switch( sig ) {
     185           0 :     case FD_SNAPSHOT_MSG_DATA:                  return "data";
     186           0 :     case FD_SNAPSHOT_MSG_META:                  return "meta";
     187           0 :     case FD_SNAPSHOT_MSG_CTRL_INIT_FULL:        return "init_full";
     188           0 :     case FD_SNAPSHOT_MSG_CTRL_INIT_INCR:        return "init_incr";
     189           0 :     case FD_SNAPSHOT_MSG_CTRL_FAIL:             return "fail";
     190           0 :     case FD_SNAPSHOT_MSG_CTRL_NEXT:             return "next";
     191           0 :     case FD_SNAPSHOT_MSG_CTRL_DONE:             return "done";
     192           0 :     case FD_SNAPSHOT_MSG_CTRL_SHUTDOWN:         return "shutdown";
     193           0 :     case FD_SNAPSHOT_MSG_CTRL_ERROR:            return "error";
     194           0 :     case FD_SNAPSHOT_MSG_CTRL_FINI:             return "fini";
     195           0 :     case FD_SNAPSHOT_MSG_LOAD_COMPLETE:         return "load_complete";
     196           0 :     default:                                    return "unknown";
     197           0 :   }
     198           0 : }
     199             : 
     200             : #endif /* HEADER_fd_src_discof_restore_utils_fd_ssctrl_h */

Generated by: LCOV version 1.14