@@ -201,6 +201,11 @@ def upload_data(db, dataFile, platform, design, variant, args, rules):
201201
202202
203203# --- PUBSUB ---
204+ # Pub/Sub hard cap is 10 MB. Stay under with safety margin to leave room for
205+ # attribute overhead and future payload growth.
206+ MAX_PUBSUB_BYTES = 8 * 1024 * 1024
207+
208+
204209def build_design_record (dataFile , platform , design , variant , rules ):
205210 """Return a dict for one design to be included in the pipeline-level payload."""
206211 with open (dataFile ) as f :
@@ -215,9 +220,9 @@ def build_design_record(dataFile, platform, design, variant, rules):
215220 }
216221
217222
218- def publish_pipeline_report ( publisher , topic_path , design_records , args ):
219- """Publish one message for the entire pipeline run ."""
220- payload = {
223+ def build_pipeline_payload ( design_records , args ):
224+ """Return the v2 pipeline-level payload dict ."""
225+ return {
221226 "payload_schema_version" : 2 ,
222227 "jenkins_env" : args .jenkinsEnv ,
223228 "build_id" : args .buildID ,
@@ -228,10 +233,13 @@ def publish_pipeline_report(publisher, topic_path, design_records, args):
228233 "jenkins_url" : args .jenkinsURL ,
229234 "designs" : design_records ,
230235 }
231- message_data = json .dumps (payload , default = str ).encode ("utf-8" )
236+
237+
238+ def publish_pipeline_report (publisher , topic_path , message_data , design_count , args ):
239+ """Publish a pre-encoded v2 pipeline message."""
232240 size_kb = len (message_data ) / 1024
233241 print (
234- f"[INFO] Publishing pipeline report ({ len ( design_records ) } designs, { size_kb :.1f} KB) to Pub/Sub."
242+ f"[INFO] Publishing pipeline report ({ design_count } designs, { size_kb :.1f} KB) to Pub/Sub."
235243 )
236244 future = publisher .publish (
237245 topic_path ,
@@ -243,6 +251,46 @@ def publish_pipeline_report(publisher, topic_path, design_records, args):
243251 print (f"[INFO] Published pipeline report to Pub/Sub (message ID: { message_id } )." )
244252
245253
254+ def publish_v1_per_design (publisher , topic_path , design_records , args ):
255+ """Fallback path used when the v2 pipeline payload exceeds MAX_PUBSUB_BYTES.
256+
257+ Emits one v1-format message per design (no payload_schema_version, metrics
258+ flattened at the root), matching the legacy schema the ingestion service
259+ still supports.
260+ """
261+ for d in design_records :
262+ payload = {
263+ "build_id" : args .buildID ,
264+ "branch_name" : args .branchName ,
265+ "pipeline_id" : args .pipelineID ,
266+ "change_branch" : args .changeBranch ,
267+ "commit_sha" : args .commitSHA ,
268+ "jenkins_url" : args .jenkinsURL ,
269+ "jenkins_env" : args .jenkinsEnv ,
270+ "rules" : d ["rules" ],
271+ }
272+ for k , v in d ["metrics" ].items ():
273+ payload [k ] = v
274+
275+ message_data = json .dumps (payload , default = str ).encode ("utf-8" )
276+ try :
277+ future = publisher .publish (
278+ topic_path ,
279+ data = message_data ,
280+ jenkins_env = args .jenkinsEnv ,
281+ )
282+ message_id = future .result ()
283+ print (
284+ f"[INFO] Published v1 fallback message (ID: { message_id } ) for "
285+ f"{ d ['platform' ]} { d ['design' ]} { d ['variant' ]} ."
286+ )
287+ except Exception as e :
288+ print (
289+ f"[WARN] Pub/Sub v1 fallback publish failed for "
290+ f"{ d ['platform' ]} { d ['design' ]} { d ['variant' ]} : { e } "
291+ )
292+
293+
246294# --- END PUBSUB ---
247295
248296
@@ -323,10 +371,23 @@ def get_rules(dataFile):
323371
324372# --- PUBSUB ---
325373if publisher and design_records :
326- try :
327- publish_pipeline_report (publisher , topic_path , design_records , args )
328- except Exception as e :
329- print (f"[WARN] Pub/Sub publish failed for pipeline report: { e } " )
374+ payload = build_pipeline_payload (design_records , args )
375+ message_data = json .dumps (payload , default = str ).encode ("utf-8" )
376+
377+ if len (message_data ) > MAX_PUBSUB_BYTES :
378+ print (
379+ f"[WARN] v2 payload size { len (message_data ) / 1024 :.1f} KB exceeds "
380+ f"{ MAX_PUBSUB_BYTES // 1024 } KB cap. Falling back to v1 per-design publish "
381+ f"({ len (design_records )} messages)."
382+ )
383+ publish_v1_per_design (publisher , topic_path , design_records , args )
384+ else :
385+ try :
386+ publish_pipeline_report (
387+ publisher , topic_path , message_data , len (design_records ), args
388+ )
389+ except Exception as e :
390+ print (f"[WARN] Pub/Sub publish failed for pipeline report: { e } " )
330391elif publisher and not design_records :
331392 print ("[WARN] Pub/Sub publisher initialized but no design records were collected." )
332393# --- END PUBSUB ---
0 commit comments