aboutsummaryrefslogtreecommitdiff
path: root/src/zencompute/runners/remotehttprunner.cpp
blob: 55f78fdd69cd972fe35b2ad4a9162d9a50445972 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
// Copyright Epic Games, Inc. All Rights Reserved.

#include "remotehttprunner.h"

#if ZEN_WITH_COMPUTE_SERVICES

#	include <zencore/compactbinary.h>
#	include <zencore/compactbinarybuilder.h>
#	include <zencore/compactbinarypackage.h>
#	include <zencore/compress.h>
#	include <zencore/except.h>
#	include <zencore/filesystem.h>
#	include <zencore/fmtutils.h>
#	include <zencore/iobuffer.h>
#	include <zencore/iohash.h>
#	include <zencore/scopeguard.h>
#	include <zencore/system.h>
#	include <zencore/trace.h>
#	include <zenhttp/httpcommon.h>
#	include <zenstore/cidstore.h>

#	include <span>
#	include <unordered_set>

//////////////////////////////////////////////////////////////////////////

namespace zen::compute {

using namespace std::literals;

//////////////////////////////////////////////////////////////////////////

RemoteHttpRunner::RemoteHttpRunner(ChunkResolver&				InChunkResolver,
								   const std::filesystem::path& BaseDir,
								   std::string_view				HostName,
								   WorkerThreadPool&			InWorkerPool)
: FunctionRunner(BaseDir)
, m_Log(logging::Get("http_exec"))
, m_ChunkResolver{InChunkResolver}
, m_WorkerPool{InWorkerPool}
, m_HostName{HostName}
, m_DisplayName{HostName}
, m_BaseUrl{fmt::format("{}/compute", HostName)}
, m_Http(m_BaseUrl)
, m_InstanceId(Oid::NewOid())
{
	// Attempt to connect a WebSocket for push-based completion notifications.
	// If the remote doesn't support WS, OnWsClose fires and we fall back to polling.
	{
		std::string WsUrl = HttpToWsUrl(HostName, "/compute/ws");

		HttpWsClientSettings WsSettings;
		WsSettings.LogCategory	  = "http_exec_ws";
		WsSettings.ConnectTimeout = std::chrono::milliseconds{3000};

		IWsClientHandler& Handler = *this;
		m_WsClient				  = std::make_unique<HttpWsClient>(WsUrl, Handler, WsSettings);
		m_WsClient->Connect();
	}

	m_MonitorThread = std::thread{&RemoteHttpRunner::MonitorThreadFunction, this};
}

void
RemoteHttpRunner::SetRemoteHostname(std::string_view Hostname)
{
	if (!Hostname.empty())
	{
		m_DisplayName = fmt::format("{} ({})", m_HostName, Hostname);
	}
}

RemoteHttpRunner::~RemoteHttpRunner()
{
	Shutdown();
}

void
RemoteHttpRunner::Shutdown()
{
	m_AcceptNewActions = false;

	// Close the WebSocket client first, so no more wakeup signals arrive.
	if (m_WsClient)
	{
		m_WsClient->Close();
	}

	// Cancel all known remote queues so the remote side stops scheduling new
	// work and cancels in-flight actions belonging to those queues.

	{
		std::vector<std::pair<int, Oid>> Queues;

		m_QueueTokenLock.WithSharedLock([&] { Queues.assign(m_RemoteQueueTokens.begin(), m_RemoteQueueTokens.end()); });

		for (const auto& [QueueId, Token] : Queues)
		{
			CancelRemoteQueue(QueueId);
		}
	}

	// Stop the monitor thread so it no longer polls the remote.

	m_MonitorThreadEnabled = false;
	m_MonitorThreadEvent.Set();
	if (m_MonitorThread.joinable())
	{
		m_MonitorThread.join();
	}

	// Drain the running map and mark all remaining actions as Failed so the
	// scheduler can reschedule or finalize them.

	std::unordered_map<int, HttpRunningAction> Remaining;

	m_RunningLock.WithExclusiveLock([&] { Remaining.swap(m_RemoteRunningMap); });

	for (auto& [RemoteLsn, HttpAction] : Remaining)
	{
		ZEN_DEBUG("shutdown: marking remote action LSN {} (local LSN {}) as Failed", RemoteLsn, HttpAction.Action->ActionLsn);
		HttpAction.Action->FailureReason = "remote runner shutdown";
		HttpAction.Action->SetActionState(RunnerAction::State::Failed);
	}
}

bool
RemoteHttpRunner::RegisterWorker(const CbPackage& WorkerPackage)
{
	ZEN_TRACE_CPU("RemoteHttpRunner::RegisterWorker");
	const IoHash WorkerId	= WorkerPackage.GetObjectHash();
	CbPackage	 WorkerDesc = WorkerPackage;

	std::string WorkerUrl = fmt::format("/workers/{}", WorkerId);

	HttpClient::Response WorkerResponse = m_Http.Get(WorkerUrl);

	if (WorkerResponse.StatusCode == HttpResponseCode::NotFound)
	{
		HttpClient::Response DescResponse = m_Http.Post(WorkerUrl, WorkerDesc.GetObject());

		if (DescResponse.StatusCode == HttpResponseCode::NotFound)
		{
			CbPackage Pkg = WorkerDesc;

			// Build response package by sending only the attachments
			// the other end needs. We start with the full package and
			// remove the attachments which are not needed.

			{
				std::unordered_set<IoHash> Needed;

				CbObject Response = DescResponse.AsObject();

				for (auto& Item : Response["need"sv])
				{
					const IoHash NeedHash = Item.AsHash();

					Needed.insert(NeedHash);
				}

				std::unordered_set<IoHash> ToRemove;

				for (const CbAttachment& Attachment : Pkg.GetAttachments())
				{
					const IoHash& Hash = Attachment.GetHash();

					if (Needed.find(Hash) == Needed.end())
					{
						ToRemove.insert(Hash);
					}
				}

				for (const IoHash& Hash : ToRemove)
				{
					int RemovedCount = Pkg.RemoveAttachment(Hash);

					ZEN_ASSERT(RemovedCount == 1);
				}
			}

			// Post resulting package

			HttpClient::Response PayloadResponse = m_Http.Post(WorkerUrl, Pkg);

			if (!IsHttpSuccessCode(PayloadResponse.StatusCode))
			{
				ZEN_ERROR("ERROR: unable to register payloads for worker {} at {}{}", WorkerId, m_Http.GetBaseUri(), WorkerUrl);
				return false;
			}
		}
		else if (!IsHttpSuccessCode(DescResponse.StatusCode))
		{
			ZEN_ERROR("ERROR: unable to register worker {} at {}{}", WorkerId, m_Http.GetBaseUri(), WorkerUrl);
			return false;
		}
		else
		{
			ZEN_ASSERT(DescResponse.StatusCode == HttpResponseCode::NoContent);
		}
	}
	else if (WorkerResponse.StatusCode == HttpResponseCode::OK)
	{
		// Already known from a previous run
	}
	else if (!IsHttpSuccessCode(WorkerResponse.StatusCode))
	{
		ZEN_ERROR("ERROR: unable to look up worker {} at {}{} (error: {} {})",
				  WorkerId,
				  m_Http.GetBaseUri(),
				  WorkerUrl,
				  (int)WorkerResponse.StatusCode,
				  ToString(WorkerResponse.StatusCode));
		return false;
	}

	return true;
}

size_t
RemoteHttpRunner::QueryCapacity()
{
	if (!m_AcceptNewActions)
	{
		return 0;
	}

	// Estimate how much more work we're ready to accept.
	// Include actions currently being submitted over HTTP so we don't
	// keep queueing new submissions while previous ones are still in flight.

	RwLock::SharedLockScope _{m_RunningLock};

	size_t RunningCount = m_RemoteRunningMap.size() + m_InFlightSubmissions.load(std::memory_order_relaxed);

	if (RunningCount >= size_t(m_MaxRunningActions))
	{
		return 0;
	}

	return m_MaxRunningActions - RunningCount;
}

std::vector<SubmitResult>
RemoteHttpRunner::SubmitActions(const std::vector<Ref<RunnerAction>>& Actions)
{
	ZEN_TRACE_CPU("RemoteHttpRunner::SubmitActions");

	m_InFlightSubmissions.fetch_add(Actions.size(), std::memory_order_relaxed);
	auto InFlightGuard = MakeGuard([&] { m_InFlightSubmissions.fetch_sub(Actions.size(), std::memory_order_relaxed); });

	if (Actions.size() <= 1)
	{
		std::vector<SubmitResult> Results;

		for (const Ref<RunnerAction>& Action : Actions)
		{
			Results.push_back(SubmitAction(Action));
		}

		return Results;
	}

	// Collect distinct QueueIds and ensure remote queues exist once per queue

	std::unordered_map<int, Oid> QueueTokens;  // QueueId → remote token (0 stays as Zero)

	for (const Ref<RunnerAction>& Action : Actions)
	{
		const int QueueId = Action->QueueId;
		if (QueueId != 0 && QueueTokens.find(QueueId) == QueueTokens.end())
		{
			CbObject QueueMeta	 = Action->GetOwnerSession()->GetQueueMetadata(QueueId);
			CbObject QueueConfig = Action->GetOwnerSession()->GetQueueConfig(QueueId);
			QueueTokens[QueueId] = EnsureRemoteQueue(QueueId, QueueMeta, QueueConfig);
		}
	}

	// Group actions by QueueId

	struct QueueGroup
	{
		std::vector<Ref<RunnerAction>> Actions;
		std::vector<size_t>			   OriginalIndices;
	};

	std::unordered_map<int, QueueGroup> Groups;

	for (size_t i = 0; i < Actions.size(); ++i)
	{
		auto& Group = Groups[Actions[i]->QueueId];
		Group.Actions.push_back(Actions[i]);
		Group.OriginalIndices.push_back(i);
	}

	// Submit each group as a batch and map results back to original indices

	std::vector<SubmitResult> Results(Actions.size());

	for (auto& [QueueId, Group] : Groups)
	{
		std::string SubmitUrl = "/jobs";
		if (QueueId != 0)
		{
			if (Oid Token = QueueTokens[QueueId]; Token != Oid::Zero)
			{
				SubmitUrl = fmt::format("/queues/{}/jobs", Token);
			}
		}

		const size_t BatchLimit = size_t(m_MaxBatchSize);

		for (size_t Offset = 0; Offset < Group.Actions.size(); Offset += BatchLimit)
		{
			size_t End = zen::Min(Offset + BatchLimit, Group.Actions.size());

			std::vector<Ref<RunnerAction>> Chunk(Group.Actions.begin() + Offset, Group.Actions.begin() + End);

			std::vector<SubmitResult> ChunkResults = SubmitActionBatch(SubmitUrl, Chunk);

			for (size_t j = 0; j < ChunkResults.size(); ++j)
			{
				Results[Group.OriginalIndices[Offset + j]] = std::move(ChunkResults[j]);
			}
		}
	}

	return Results;
}

SubmitResult
RemoteHttpRunner::SubmitAction(Ref<RunnerAction> Action)
{
	ZEN_TRACE_CPU("RemoteHttpRunner::SubmitAction");

	// Verify whether we can accept more work

	if (!m_AcceptNewActions)
	{
		return SubmitResult{.IsAccepted = false, .Reason = "runner is shutting down"};
	}

	{
		RwLock::SharedLockScope _{m_RunningLock};
		if (m_RemoteRunningMap.size() >= size_t(m_MaxRunningActions))
		{
			return SubmitResult{.IsAccepted = false};
		}
	}

	using namespace std::literals;

	// Each enqueued action is assigned an integer index (logical sequence number),
	// which we use as a key for tracking data structures and as an opaque id which
	// may be used by clients to reference the scheduled action

	Action->ExecutionLocation = m_HostName;

	const int32_t	ActionLsn = Action->ActionLsn;
	const CbObject& ActionObj = Action->ActionObj;
	const IoHash	ActionId  = ActionObj.GetHash();

	MaybeDumpAction(ActionLsn, ActionObj);

	// Determine the submission URL. If the action belongs to a queue, ensure a
	// corresponding remote queue exists on the target node and submit via it.

	std::string SubmitUrl = "/jobs";
	if (const int QueueId = Action->QueueId; QueueId != 0)
	{
		CbObject QueueMeta	 = Action->GetOwnerSession()->GetQueueMetadata(QueueId);
		CbObject QueueConfig = Action->GetOwnerSession()->GetQueueConfig(QueueId);
		if (Oid Token = EnsureRemoteQueue(QueueId, QueueMeta, QueueConfig); Token != Oid::Zero)
		{
			SubmitUrl = fmt::format("/queues/{}/jobs", Token);
		}
	}

	// Submit the action to the remote. In eager-attach mode we build a
	// CbPackage with all referenced attachments upfront to avoid the 404
	// round-trip. In the default mode we POST the bare object first and
	// only upload missing attachments if the remote requests them.
	//
	// In both modes, FailedDependency (424) triggers a worker re-register
	// and a single retry.

	CbObject			 Result;
	HttpClient::Response WorkResponse;
	HttpResponseCode	 WorkResponseCode{};

	if (m_EagerAttach)
	{
		CbPackage Pkg;
		Pkg.SetObject(ActionObj);

		ActionObj.IterateAttachments([&](CbFieldView Field) {
			const IoHash AttachHash = Field.AsHash();

			if (IoBuffer Chunk = m_ChunkResolver.FindChunkByCid(AttachHash))
			{
				uint64_t		 DataRawSize = 0;
				IoHash			 DataRawHash;
				CompressedBuffer Compressed =
					CompressedBuffer::FromCompressed(SharedBuffer{Chunk}, /* out */ DataRawHash, /* out */ DataRawSize);

				Pkg.AddAttachment(CbAttachment(Compressed, AttachHash));
				m_LastSubmitStats.TotalAttachments.fetch_add(1, std::memory_order_relaxed);
				m_LastSubmitStats.TotalAttachmentBytes.fetch_add(Chunk.GetSize(), std::memory_order_relaxed);
			}
		});

		for (int Attempt = 0; Attempt < 2; ++Attempt)
		{
			WorkResponse	 = m_Http.Post(SubmitUrl, Pkg);
			WorkResponseCode = WorkResponse.StatusCode;

			if (WorkResponseCode == HttpResponseCode::FailedDependency && Attempt == 0)
			{
				ZEN_WARN("remote {} returned FailedDependency for action {} — re-registering worker and retrying",
						 m_Http.GetBaseUri(),
						 ActionId);

				(void)RegisterWorker(Action->Worker.Descriptor);
			}
			else
			{
				break;
			}
		}
	}
	else
	{
		for (int Attempt = 0; Attempt < 2; ++Attempt)
		{
			WorkResponse	 = m_Http.Post(SubmitUrl, ActionObj);
			WorkResponseCode = WorkResponse.StatusCode;

			if (WorkResponseCode == HttpResponseCode::FailedDependency && Attempt == 0)
			{
				ZEN_WARN("remote {} returned FailedDependency for action {} — re-registering worker and retrying",
						 m_Http.GetBaseUri(),
						 ActionId);

				(void)RegisterWorker(Action->Worker.Descriptor);
			}
			else
			{
				break;
			}
		}

		if (WorkResponseCode == HttpResponseCode::NotFound)
		{
			// Remote needs attachments — resolve them and retry with a CbPackage

			CbPackage Pkg;
			Pkg.SetObject(ActionObj);

			CbObject Response = WorkResponse.AsObject();

			for (auto& Item : Response["need"sv])
			{
				const IoHash NeedHash = Item.AsHash();

				if (IoBuffer Chunk = m_ChunkResolver.FindChunkByCid(NeedHash))
				{
					uint64_t		 DataRawSize = 0;
					IoHash			 DataRawHash;
					CompressedBuffer Compressed =
						CompressedBuffer::FromCompressed(SharedBuffer{Chunk}, /* out */ DataRawHash, /* out */ DataRawSize);

					ZEN_ASSERT(DataRawHash == NeedHash);

					Pkg.AddAttachment(CbAttachment(Compressed, NeedHash));
					m_LastSubmitStats.TotalAttachments.fetch_add(1, std::memory_order_relaxed);
					m_LastSubmitStats.TotalAttachmentBytes.fetch_add(Chunk.GetSize(), std::memory_order_relaxed);
				}
				else
				{
					return {.IsAccepted = false, .Reason = fmt::format("missing attachment {}", NeedHash)};
				}
			}

			HttpClient::Response PayloadResponse = m_Http.Post(SubmitUrl, Pkg);

			if (!PayloadResponse)
			{
				ZEN_WARN("unable to register payloads for action {} at {}{}", ActionId, m_Http.GetBaseUri(), SubmitUrl);
				return {.IsAccepted = false, .Reason = "HTTP request failed"};
			}

			WorkResponse	 = std::move(PayloadResponse);
			WorkResponseCode = WorkResponse.StatusCode;
		}
	}

	if (WorkResponseCode == HttpResponseCode::OK)
	{
		Result = WorkResponse.AsObject();
	}
	else if (!WorkResponse)
	{
		ZEN_WARN("submit of action {} to {}{} failed", ActionId, m_Http.GetBaseUri(), SubmitUrl);
		return {.IsAccepted = false, .Reason = "HTTP request failed"};
	}
	else if (!IsHttpSuccessCode(WorkResponseCode))
	{
		const int Code = static_cast<int>(WorkResponseCode);
		ZEN_WARN("submit of action {} to {}{} returned {} {}", ActionId, m_Http.GetBaseUri(), SubmitUrl, Code, ToString(Code));
		return {.IsAccepted = false,
				.Reason = fmt::format("unexpected response code {} {} from {}{}", Code, ToString(Code), m_Http.GetBaseUri(), SubmitUrl)};
	}

	if (Result)
	{
		if (const int32_t LsnField = Result["lsn"].AsInt32(0))
		{
			HttpRunningAction NewAction;
			NewAction.Action		  = Action;
			NewAction.RemoteActionLsn = LsnField;

			{
				RwLock::ExclusiveLockScope _(m_RunningLock);

				m_RemoteRunningMap[LsnField] = std::move(NewAction);
			}

			ZEN_DEBUG("scheduled action {} with remote LSN {} (local LSN {})", ActionId, LsnField, ActionLsn);

			Action->SetActionState(RunnerAction::State::Running);

			return SubmitResult{.IsAccepted = true};
		}
	}

	return {};
}

std::vector<SubmitResult>
RemoteHttpRunner::SubmitActionBatch(const std::string& SubmitUrl, const std::vector<Ref<RunnerAction>>& Actions)
{
	ZEN_TRACE_CPU("RemoteHttpRunner::SubmitActionBatch");

	if (!m_AcceptNewActions)
	{
		return std::vector<SubmitResult>(Actions.size(), SubmitResult{.IsAccepted = false, .Reason = "runner is shutting down"});
	}

	// Capacity check

	{
		RwLock::SharedLockScope _{m_RunningLock};
		if (m_RemoteRunningMap.size() >= size_t(m_MaxRunningActions))
		{
			std::vector<SubmitResult> Results(Actions.size(), SubmitResult{.IsAccepted = false});
			return Results;
		}
	}

	// Per-action setup and build batch body

	CbObjectWriter Body;
	Body.BeginArray("actions"sv);

	std::unordered_set<IoHash, IoHash::Hasher> AttachmentsSeen;

	for (const Ref<RunnerAction>& Action : Actions)
	{
		Action->ExecutionLocation = m_HostName;
		MaybeDumpAction(Action->ActionLsn, Action->ActionObj);
		Body.AddObject(Action->ActionObj);

		if (m_EagerAttach)
		{
			Action->ActionObj.IterateAttachments([&](CbFieldView Field) { AttachmentsSeen.insert(Field.AsHash()); });
		}
	}

	Body.EndArray();

	// In eager-attach mode, build a CbPackage with all referenced attachments
	// so the remote can accept in a single round-trip.  Otherwise POST a bare
	// CbObject and handle the 404 need-list flow.

	if (m_EagerAttach)
	{
		CbPackage Pkg;
		Pkg.SetObject(Body.Save());

		for (const IoHash& AttachHash : AttachmentsSeen)
		{
			if (IoBuffer Chunk = m_ChunkResolver.FindChunkByCid(AttachHash))
			{
				uint64_t		 DataRawSize = 0;
				IoHash			 DataRawHash;
				CompressedBuffer Compressed =
					CompressedBuffer::FromCompressed(SharedBuffer{Chunk}, /* out */ DataRawHash, /* out */ DataRawSize);

				Pkg.AddAttachment(CbAttachment(Compressed, AttachHash));
				m_LastSubmitStats.TotalAttachments.fetch_add(1, std::memory_order_relaxed);
				m_LastSubmitStats.TotalAttachmentBytes.fetch_add(Chunk.GetSize(), std::memory_order_relaxed);
			}
		}

		HttpClient::Response Response = m_Http.Post(SubmitUrl, Pkg);

		if (Response.StatusCode == HttpResponseCode::OK)
		{
			return ParseBatchResponse(Response, Actions);
		}
	}
	else
	{
		HttpClient::Response Response = m_Http.Post(SubmitUrl, Body.Save());

		if (Response.StatusCode == HttpResponseCode::OK)
		{
			return ParseBatchResponse(Response, Actions);
		}

		if (Response.StatusCode == HttpResponseCode::NotFound)
		{
			CbObject NeedObj = Response.AsObject();

			CbPackage Pkg;
			Pkg.SetObject(Body.Save());

			for (auto& Item : NeedObj["need"sv])
			{
				const IoHash NeedHash = Item.AsHash();

				if (IoBuffer Chunk = m_ChunkResolver.FindChunkByCid(NeedHash))
				{
					uint64_t		 DataRawSize = 0;
					IoHash			 DataRawHash;
					CompressedBuffer Compressed =
						CompressedBuffer::FromCompressed(SharedBuffer{Chunk}, /* out */ DataRawHash, /* out */ DataRawSize);

					ZEN_ASSERT(DataRawHash == NeedHash);

					Pkg.AddAttachment(CbAttachment(Compressed, NeedHash));
					m_LastSubmitStats.TotalAttachments.fetch_add(1, std::memory_order_relaxed);
					m_LastSubmitStats.TotalAttachmentBytes.fetch_add(Chunk.GetSize(), std::memory_order_relaxed);
				}
				else
				{
					ZEN_WARN("batch submit: missing attachment {} — falling back to individual submit", NeedHash);
					return FallbackToIndividualSubmit(Actions);
				}
			}

			HttpClient::Response RetryResponse = m_Http.Post(SubmitUrl, Pkg);

			if (RetryResponse.StatusCode == HttpResponseCode::OK)
			{
				return ParseBatchResponse(RetryResponse, Actions);
			}

			ZEN_WARN("batch submit retry failed with {} {} — falling back to individual submit",
					 (int)RetryResponse.StatusCode,
					 ToString(RetryResponse.StatusCode));
			return FallbackToIndividualSubmit(Actions);
		}
	}

	// Unexpected status or connection error — fall back to individual submission

	ZEN_WARN("batch submit to {}{} failed — falling back to individual submit", m_Http.GetBaseUri(), SubmitUrl);

	return FallbackToIndividualSubmit(Actions);
}

std::vector<SubmitResult>
RemoteHttpRunner::ParseBatchResponse(const HttpClient::Response& Response, const std::vector<Ref<RunnerAction>>& Actions)
{
	std::vector<SubmitResult> Results;
	Results.reserve(Actions.size());

	CbObject	ResponseObj = Response.AsObject();
	CbArrayView ResultArray = ResponseObj["results"sv].AsArrayView();

	size_t Index = 0;
	for (CbFieldView Field : ResultArray)
	{
		if (Index >= Actions.size())
		{
			break;
		}

		CbObjectView  Entry	   = Field.AsObjectView();
		const int32_t LsnField = Entry["lsn"sv].AsInt32(0);

		if (LsnField > 0)
		{
			HttpRunningAction NewAction;
			NewAction.Action		  = Actions[Index];
			NewAction.RemoteActionLsn = LsnField;

			{
				RwLock::ExclusiveLockScope _(m_RunningLock);
				m_RemoteRunningMap[LsnField] = std::move(NewAction);
			}

			ZEN_DEBUG("batch: scheduled action {} with remote LSN {} (local LSN {})",
					  Actions[Index]->ActionObj.GetHash(),
					  LsnField,
					  Actions[Index]->ActionLsn);

			Actions[Index]->SetActionState(RunnerAction::State::Running);

			Results.push_back(SubmitResult{.IsAccepted = true});
		}
		else
		{
			std::string_view ErrorMsg = Entry["error"sv].AsString();
			Results.push_back(SubmitResult{.IsAccepted = false, .Reason = std::string(ErrorMsg)});
		}

		++Index;
	}

	// If the server returned fewer results than actions, mark the rest as not accepted
	while (Results.size() < Actions.size())
	{
		Results.push_back(SubmitResult{.IsAccepted = false, .Reason = "no result from server"});
	}

	return Results;
}

std::vector<SubmitResult>
RemoteHttpRunner::FallbackToIndividualSubmit(const std::vector<Ref<RunnerAction>>& Actions)
{
	std::vector<std::future<SubmitResult>> Futures;
	Futures.reserve(Actions.size());

	for (const Ref<RunnerAction>& Action : Actions)
	{
		std::packaged_task<SubmitResult()> Task([this, Action]() { return SubmitAction(Action); });

		Futures.push_back(m_WorkerPool.EnqueueTask(std::move(Task), WorkerThreadPool::EMode::EnableBacklog));
	}

	std::vector<SubmitResult> Results;
	Results.reserve(Futures.size());

	for (auto& Future : Futures)
	{
		Results.push_back(Future.get());
	}

	return Results;
}

Oid
RemoteHttpRunner::EnsureRemoteQueue(int QueueId, const CbObject& Metadata, const CbObject& Config)
{
	{
		RwLock::SharedLockScope _(m_QueueTokenLock);
		if (auto It = m_RemoteQueueTokens.find(QueueId); It != m_RemoteQueueTokens.end())
		{
			return It->second;
		}
	}

	// Build a stable idempotency key that uniquely identifies this (runner instance, local queue)
	// pair. The server uses this to return the same remote queue token for concurrent or redundant
	// requests, preventing orphaned remote queues when multiple threads race through here.
	// Also send hostname so the server can associate the queue with its origin for diagnostics.
	CbObjectWriter Body;
	Body << "idempotency_key"sv << fmt::format("{}/{}", m_InstanceId, QueueId);
	Body << "hostname"sv << GetMachineName();
	if (Metadata)
	{
		Body << "metadata"sv << Metadata;
	}
	if (Config)
	{
		Body << "config"sv << Config;
	}

	HttpClient::Response Resp = m_Http.Post("/queues/remote", Body.Save());
	if (!Resp)
	{
		ZEN_WARN("failed to create remote queue for local queue {} on {}", QueueId, m_HostName);
		return Oid::Zero;
	}

	Oid Token = Oid::TryFromHexString(Resp.AsObject()["queue_token"sv].AsString());
	if (Token == Oid::Zero)
	{
		return Oid::Zero;
	}

	ZEN_DEBUG("created remote queue '{}' for local queue {} on {}", Token, QueueId, m_HostName);

	RwLock::ExclusiveLockScope _(m_QueueTokenLock);
	auto [It, Inserted] = m_RemoteQueueTokens.try_emplace(QueueId, Token);
	return It->second;
}

void
RemoteHttpRunner::CancelRemoteQueue(int QueueId)
{
	Oid Token;
	{
		RwLock::SharedLockScope _(m_QueueTokenLock);
		if (auto It = m_RemoteQueueTokens.find(QueueId); It != m_RemoteQueueTokens.end())
		{
			Token = It->second;
		}
	}

	if (Token == Oid::Zero)
	{
		return;
	}

	HttpClient::Response Resp = m_Http.Delete(fmt::format("/queues/{}", Token));

	if (Resp.StatusCode == HttpResponseCode::NoContent)
	{
		ZEN_DEBUG("cancelled remote queue '{}' (local queue {}) on {}", Token, QueueId, m_HostName);
	}
	else
	{
		ZEN_WARN("failed to cancel remote queue '{}' on {}: {}", Token, m_HostName, int(Resp.StatusCode));
	}
}

bool
RemoteHttpRunner::IsHealthy()
{
	if (HttpClient::Response Ready = m_Http.Get("/ready"))
	{
		return true;
	}
	else
	{
		// TODO: use response to propagate context
		return false;
	}
}

size_t
RemoteHttpRunner::GetSubmittedActionCount()
{
	RwLock::SharedLockScope _(m_RunningLock);
	return m_RemoteRunningMap.size();
}

//////////////////////////////////////////////////////////////////////////
//
// IWsClientHandler
//

void
RemoteHttpRunner::OnWsOpen()
{
	ZEN_INFO("WebSocket connected to {}", m_HostName);
	m_WsConnected.store(true, std::memory_order_release);
}

void
RemoteHttpRunner::OnWsMessage([[maybe_unused]] const WebSocketMessage& Msg)
{
	// The message content is a wakeup signal; no parsing needed.
	// Signal the monitor thread to sweep completed actions immediately.
	m_MonitorThreadEvent.Set();
}

void
RemoteHttpRunner::OnWsClose([[maybe_unused]] uint16_t Code, [[maybe_unused]] std::string_view Reason)
{
	ZEN_WARN("WebSocket disconnected from {} (code {})", m_HostName, Code);
	m_WsConnected.store(false, std::memory_order_release);
}

//////////////////////////////////////////////////////////////////////////

void
RemoteHttpRunner::MonitorThreadFunction()
{
	SetCurrentThreadName("RemoteHttpRunner_Monitor");

	do
	{
		const int NormalWaitingTime = 200;
		const int WsWaitingTime		= 2000;	 // Safety-net interval when WS is connected

		int	 WaitTimeMs = m_WsConnected.load(std::memory_order_relaxed) ? WsWaitingTime : NormalWaitingTime;
		auto WaitOnce	= [&] { return m_MonitorThreadEvent.Wait(WaitTimeMs); };
		auto SweepOnce	= [&] {
			 const size_t RetiredCount = SweepRunningActions();

			 if (m_WsConnected.load(std::memory_order_relaxed))
			 {
				 // WS connected: use long safety-net interval; the WS message
				 // will wake us immediately for the real work.
				 WaitTimeMs = WsWaitingTime;
			 }
			 else
			 {
				 // No WS: adaptive polling as before
				 m_RunningLock.WithSharedLock([&] {
					 if (m_RemoteRunningMap.size() > 16)
					 {
						 WaitTimeMs = NormalWaitingTime / 4;
					 }
					 else
					 {
						 if (RetiredCount)
						 {
							 WaitTimeMs = NormalWaitingTime / 2;
						 }
						 else
						 {
							 WaitTimeMs = NormalWaitingTime;
						 }
					 }
				 });
			 }
		};

		while (!WaitOnce())
		{
			SweepOnce();
		}

		// Signal received — may be a WS wakeup or a quit signal

		SweepOnce();
	} while (m_MonitorThreadEnabled);
}

size_t
RemoteHttpRunner::SweepRunningActions()
{
	ZEN_TRACE_CPU("RemoteHttpRunner::SweepRunningActions");
	std::vector<HttpRunningAction> CompletedActions;

	// Poll remote for list of completed actions

	HttpClient::Response ResponseCompleted = m_Http.Get("/jobs/completed"sv);

	if (CbObject Completed = ResponseCompleted.AsObject())
	{
		for (auto& FieldIt : Completed["completed"sv])
		{
			CbObjectView	 EntryObj	   = FieldIt.AsObjectView();
			const int32_t	 CompleteLsn   = EntryObj["lsn"sv].AsInt32();
			std::string_view StateName	   = EntryObj["state"sv].AsString();
			std::string_view FailureReason = EntryObj["reason"sv].AsString();

			RunnerAction::State RemoteState = RunnerAction::FromString(StateName);

			// Always fetch to drain the result from the remote's results map,
			// but only keep the result package for successfully completed actions.
			HttpClient::Response ResponseJob = m_Http.Get(fmt::format("/jobs/{}"sv, CompleteLsn));

			m_RunningLock.WithExclusiveLock([&] {
				if (auto CompleteIt = m_RemoteRunningMap.find(CompleteLsn); CompleteIt != m_RemoteRunningMap.end())
				{
					HttpRunningAction CompletedAction = std::move(CompleteIt->second);
					CompletedAction.RemoteState		  = RemoteState;
					CompletedAction.FailureReason	  = std::string(FailureReason);

					if (RemoteState == RunnerAction::State::Completed && ResponseJob)
					{
						CompletedAction.ActionResults = ResponseJob.AsPackage();
					}

					CompletedActions.push_back(std::move(CompletedAction));
					m_RemoteRunningMap.erase(CompleteIt);
				}
				else
				{
					// we received a completion notice for an action we don't know about,
					// this can happen if the runner is used by multiple upstream schedulers,
					// or if this compute node was recently restarted and lost track of
					// previously scheduled actions
				}
			});
		}

		if (CbObjectView Metrics = Completed["metrics"sv].AsObjectView())
		{
			//			if (const size_t CpuCount = Metrics["core_count"].AsInt32(0))
			if (const int32_t CpuCount = Metrics["lp_count"].AsInt32(0))
			{
				const int32_t NewCap = zen::Max(4, CpuCount);

				if (m_MaxRunningActions > NewCap)
				{
					ZEN_DEBUG("capping {} to {} actions (was {})", m_BaseUrl, NewCap, m_MaxRunningActions);

					m_MaxRunningActions = NewCap;
				}
			}
		}
	}

	// Notify outer. Note that this has to be done without holding any local locks
	// otherwise we may end up with deadlocks.

	for (HttpRunningAction& HttpAction : CompletedActions)
	{
		const int ActionLsn = HttpAction.Action->ActionLsn;

		if (HttpAction.RemoteState == RunnerAction::State::Completed)
		{
			ZEN_DEBUG("action {} LSN {} (remote LSN {}) completed on {}",
					  HttpAction.Action->ActionId,
					  ActionLsn,
					  HttpAction.RemoteActionLsn,
					  m_HostName);
			HttpAction.Action->SetResult(std::move(HttpAction.ActionResults));
		}
		else if (HttpAction.RemoteState == RunnerAction::State::Failed || HttpAction.RemoteState == RunnerAction::State::Abandoned)
		{
			HttpAction.Action->FailureReason = HttpAction.FailureReason;
			if (HttpAction.FailureReason.empty())
			{
				ZEN_WARN("action {} ({}) {} on remote {}",
						 HttpAction.Action->ActionId,
						 ActionLsn,
						 RunnerAction::ToString(HttpAction.RemoteState),
						 m_HostName);
			}
			else
			{
				ZEN_WARN("action {} ({}) {} on remote {}: {}",
						 HttpAction.Action->ActionId,
						 ActionLsn,
						 RunnerAction::ToString(HttpAction.RemoteState),
						 m_HostName,
						 HttpAction.FailureReason);
			}
		}
		else
		{
			ZEN_DEBUG("action {} LSN {} (remote LSN {}) -> {}",
					  HttpAction.Action->ActionId,
					  ActionLsn,
					  HttpAction.RemoteActionLsn,
					  RunnerAction::ToString(HttpAction.RemoteState));
		}

		HttpAction.Action->SetActionState(HttpAction.RemoteState);
	}

	return CompletedActions.size();
}

}  // namespace zen::compute

#endif