ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_Provenance.H
Go to the documentation of this file.
1 #ifndef ERF_PROVENANCE_H_
2 #define ERF_PROVENANCE_H_
3 
4 #include <array>
5 #include <cstdint>
6 #include <ctime>
7 #include <string>
8 #include <string_view>
9 #include <vector>
10 
11 namespace erf_provenance
12 {
13 
14 constexpr int schema_version = 1;
15 constexpr const char* provenance_begin = "ERF_PROVENANCE_BEGIN";
16 constexpr const char* provenance_end = "ERF_PROVENANCE_END";
17 
18 /**
19  * The reason a lineage is incomplete is part of the persisted contract.
20  * Missing or invalid metadata never prevents ERF from reading physical state.
21  */
22 enum class LineageStatus
23 {
24  Complete,
31 };
32 
33 enum class ArtifactType
34 {
35  Checkpoint,
36  Plotfile2D,
38 };
39 
41 {
42  Valid,
48 };
49 
50 /**
51  * Identity and known ancestry for one ERF process invocation.
52  *
53  * Simulation identity names one rooted restart family, which may branch.
54  * Execution identity names one invocation. Artifact identity is kept separate
55  * because one invocation can write many checkpoints and plotfiles. The known
56  * ancestry path is stored in every artifact so old directories are not needed
57  * to reconstruct repeated restarts. The parent UUID gives the immediate edge;
58  * the source checkpoint UUID selects the exact checkpoint artifact. These UUIDs
59  * identify records; they do not authenticate contents or provide cryptographic
60  * proof.
61  */
63 {
65  std::string simulation_uuid;
66  std::string execution_uuid;
67  std::string parent_execution_uuid;
68  std::vector<std::string> execution_lineage;
70  bool lineage_complete = true;
74  std::string execution_start_utc;
75 };
76 
78 {
79  std::string artifact_uuid;
81  int artifact_step = -1;
82  double artifact_time_seconds = 0.0;
83  std::string artifact_created_utc;
84 };
85 
87 {
90 };
91 
93 {
96  std::string diagnostic;
97 
98  [[nodiscard]] bool valid () const noexcept
99  {
101  }
102 };
103 
105 
106 /** Format supplied bytes as a lowercase canonical RFC UUIDv4 string. */
107 std::string uuid_v4_from_bytes (const std::array<std::uint8_t, 16>& bytes);
108 
109 /** Validate a lowercase or uppercase canonical RFC UUIDv4 string. */
110 bool is_valid_uuid_v4 (std::string_view uuid);
111 
112 /** Generate a UUID without using AMReX's scientific random-number stream. */
113 std::string generate_uuid_v4 ();
114 
115 /** Format a supplied clock value as an ISO 8601 UTC timestamp. */
116 std::string format_utc (std::time_t time_value);
117 
118 /** Return the current time in the persisted UTC format. */
119 std::string current_utc ();
120 
121 /** Serialize exactly one schema-1 machine-readable block. */
122 std::string serialize_provenance_block (const ProvenanceRecord& record);
123 
124 /** Parse only delimited provenance markers from a human-readable job_info. */
125 ProvenanceParseResult parse_provenance_block (std::string_view job_info_text);
126 
127 /** Read and broadcast one job_info before parsing the same bytes on every rank. */
128 ProvenanceReadResult read_job_info_file (const std::string& job_info_path);
129 
130 /** Create the root record for one cold-start invocation. */
131 ExecutionProvenance make_cold_start_provenance (const std::string& execution_uuid,
132  const std::string& execution_start_utc);
133 
134 /** Create a child record from a valid checkpoint record, including its artifact UUID. */
136  const ProvenanceRecord& parent_checkpoint,
137  const std::string& checkpoint_path);
138 
139 /** Start a new known lineage segment after a nonfatal provenance read failure. */
141  ProvenanceReadStatus failure,
142  const std::string& checkpoint_path);
143 
144 /** Return the stable persisted token for a lineage status. */
145 const char* lineage_status_token (LineageStatus status) noexcept;
146 
147 /** Return the stable persisted token for an artifact type. */
148 const char* artifact_type_token (ArtifactType type) noexcept;
149 
150 /** Initialize one process-wide invocation record and share it across multiblock ERF objects. */
152 
153 } // namespace erf_provenance
154 
155 #endif
Definition: ERF_Provenance.cpp:247
std::string uuid_v4_from_bytes(const std::array< std::uint8_t, 16 > &bytes)
Definition: ERF_Provenance.cpp:249
ProvenanceReadResult read_job_info_file(const std::string &path)
Definition: ERF_Provenance.cpp:482
std::string current_utc()
Definition: ERF_Provenance.cpp:315
constexpr int schema_version
Definition: ERF_Provenance.H:14
std::string serialize_provenance_block(const ProvenanceRecord &record)
Definition: ERF_Provenance.cpp:344
ExecutionProvenance initialize_execution_provenance()
Definition: ERF_Provenance.cpp:559
ProvenanceParseResult parse_provenance_block(std::string_view text)
Definition: ERF_Provenance.cpp:374
ExecutionProvenance make_restart_provenance(const ExecutionProvenance &current_invocation, const ProvenanceRecord &parent_checkpoint, const std::string &checkpoint_path)
Definition: ERF_Provenance.cpp:519
const char * lineage_status_token(LineageStatus status) noexcept
Definition: ERF_Provenance.cpp:320
LineageStatus
Definition: ERF_Provenance.H:23
std::string format_utc(std::time_t time_value)
Definition: ERF_Provenance.cpp:302
constexpr const char * provenance_end
Definition: ERF_Provenance.H:16
ExecutionProvenance make_incomplete_restart_provenance(const ExecutionProvenance &current_invocation, ProvenanceReadStatus failure_status, const std::string &checkpoint_path)
Definition: ERF_Provenance.cpp:543
const char * artifact_type_token(ArtifactType type) noexcept
Definition: ERF_Provenance.cpp:334
bool is_valid_uuid_v4(std::string_view uuid)
Definition: ERF_Provenance.cpp:262
ProvenanceReadStatus
Definition: ERF_Provenance.H:41
std::string generate_uuid_v4()
Definition: ERF_Provenance.cpp:270
ExecutionProvenance make_cold_start_provenance(const std::string &execution_uuid, const std::string &execution_start_utc)
Definition: ERF_Provenance.cpp:508
constexpr const char * provenance_begin
Definition: ERF_Provenance.H:15
ArtifactType
Definition: ERF_Provenance.H:34
Definition: ERF_Provenance.H:78
int artifact_step
Definition: ERF_Provenance.H:81
std::string artifact_uuid
Definition: ERF_Provenance.H:79
double artifact_time_seconds
Definition: ERF_Provenance.H:82
std::string artifact_created_utc
Definition: ERF_Provenance.H:83
ArtifactType artifact_type
Definition: ERF_Provenance.H:80
Definition: ERF_Provenance.H:63
int schema_version
Definition: ERF_Provenance.H:64
std::string source_checkpoint_uuid
Definition: ERF_Provenance.H:72
std::string execution_uuid
Definition: ERF_Provenance.H:66
std::vector< std::string > execution_lineage
Definition: ERF_Provenance.H:68
LineageStatus lineage_status
Definition: ERF_Provenance.H:71
bool lineage_complete
Definition: ERF_Provenance.H:70
std::string parent_execution_uuid
Definition: ERF_Provenance.H:67
std::string execution_start_utc
Definition: ERF_Provenance.H:74
std::string source_checkpoint_path
Definition: ERF_Provenance.H:73
int restart_generation
Definition: ERF_Provenance.H:69
std::string simulation_uuid
Definition: ERF_Provenance.H:65
Definition: ERF_Provenance.H:93
ProvenanceRecord record
Definition: ERF_Provenance.H:95
bool valid() const noexcept
Definition: ERF_Provenance.H:98
std::string diagnostic
Definition: ERF_Provenance.H:96
ProvenanceReadStatus status
Definition: ERF_Provenance.H:94
Definition: ERF_Provenance.H:87
ArtifactProvenance artifact
Definition: ERF_Provenance.H:89
ExecutionProvenance execution
Definition: ERF_Provenance.H:88