forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCPStoreLibUvBackend.cpp
1269 lines (1086 loc) · 32.1 KB
/
TCPStoreLibUvBackend.cpp
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
#include <algorithm>
#include <deque>
#include <exception>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <fmt/format.h>
#include <torch/csrc/distributed/c10d/TCPStore.hpp>
#include <torch/csrc/distributed/c10d/TCPStoreBackend.hpp>
#include <torch/csrc/distributed/c10d/logging.h>
#ifdef TORCH_USE_LIBUV
#include <uv.h>
#endif
namespace c10d::detail {
#ifdef TORCH_USE_LIBUV
/*
Exception safety:
It's ok to use exceptions during client processing.
Other callbacks don't provide exception safety so avoid there.
*/
#define DEFAULT_BACKLOG 16384
#define MAX_KEY_COUNT (128 * 1024)
#define MAX_STRING_LEN (8 * 1024)
#define MAX_PAYLOAD_LEN (8 * 1024 * 1024)
// This controls the preferred size for buffers.
// Too small and we'll need multiple buffers for one request
// Too big and we might taxing malloc
#define ALLOC_BUFFER_SIZE ((size_t)4000)
class UvHandle : public c10::intrusive_ptr_target {
public:
~UvHandle() override = default;
c10::intrusive_ptr<UvHandle> iptr() {
return c10::intrusive_ptr<UvHandle>::reclaim_copy(this);
}
void close() {
if (uv_is_closing(unsafeGetHandle())) {
return;
}
uv_close(unsafeGetHandle(), on_close);
}
virtual uv_handle_t* unsafeGetHandle() = 0;
protected:
void handleReady() {
/*
This method must be called once the handle is ready and registered with the
loop.
Do not call this in the ctor, make_intrusive reset refcounts to one after
construction.
*/
uv_handle_set_data(unsafeGetHandle(), this);
at::raw::intrusive_ptr::incref(this);
}
virtual void onClose() = 0;
private:
static c10::intrusive_ptr<UvHandle> reclaim(uv_handle_t* handle) {
auto h = (UvHandle*)uv_handle_get_data(handle);
return c10::intrusive_ptr<UvHandle>::reclaim(h);
}
static void on_close(uv_handle_t* uv_handle) {
auto handle = reclaim(uv_handle);
handle->onClose();
}
};
class UvTcpSocket : public UvHandle {
uv_tcp_t client{};
c10::intrusive_ptr<UvTcpSocket> iptr() {
return c10::intrusive_ptr<UvTcpSocket>::reclaim_copy(this);
}
static c10::intrusive_ptr<UvTcpSocket> borrow(uv_stream_t* handle) {
auto h = (UvTcpSocket*)uv_handle_get_data((uv_handle_t*)handle);
return h->iptr();
}
static void alloc_buffer(
uv_handle_t* handle,
size_t suggested_size,
uv_buf_t* buf) {
suggested_size = std::min(suggested_size, (size_t)ALLOC_BUFFER_SIZE);
buf->base = (char*)malloc(suggested_size);
buf->len = suggested_size;
}
static void read_callback(
uv_stream_t* client,
ssize_t nread,
const uv_buf_t* buf) {
auto uv_socket = UvTcpSocket::borrow(client);
if (nread < 0) {
C10D_DEBUG(
"Read callback failed. code:{} name:{} desc:{}",
nread,
uv_err_name(nread),
uv_strerror(nread));
uv_socket->close();
return;
}
if (nread > 0) {
try {
uv_socket->processBuf(buf, nread);
} catch (std::exception& ex) {
C10D_INFO("Error processing client message: {}", ex.what());
uv_socket->close();
}
}
}
public:
explicit UvTcpSocket(uv_loop_t* loop) {
uv_tcp_init(loop, &client);
}
void startRead() {
int res = uv_read_start((uv_stream_t*)&client, alloc_buffer, read_callback);
if (res) {
C10D_INFO(
"Failed to setup read callback. client:{} code:{} name:{} desc:{}.",
(void*)this,
res,
uv_err_name(res),
uv_strerror(res));
close();
}
}
uv_handle_t* unsafeGetHandle() override {
return (uv_handle_t*)&client;
}
protected:
uv_stream_t* unsafeGetStream() {
return (uv_stream_t*)&client;
}
uv_tcp_t* unsafeGetSocket() {
return &client;
}
virtual void processBuf(const uv_buf_t* buf, size_t nread) {
TORCH_CHECK(
false, "Trying to read from a socket subclass that lacks processBuf");
}
void onClose() override {
// TODO use registerClient (and rename it to registerHandle) - this will
// significantly simplify things.
}
};
class UvTcpServer : public UvTcpSocket {
public:
typedef std::function<void(int)> OnConnectCallback;
explicit UvTcpServer(uv_loop_t* loop)
: UvTcpSocket(loop), onConnectCb(missingOnConnect) {}
static c10::intrusive_ptr<UvTcpServer> makeWithSocket(
uv_loop_t* loop,
int socket) {
auto res = c10::make_intrusive<UvTcpServer>(loop);
res->handleReady();
try {
int uv_res = uv_tcp_open((uv_tcp_t*)res->unsafeGetStream(), socket);
TORCH_CHECK(
uv_res == 0,
"Failed to open existing socket. socket:{} code:{} name:{} message:{}",
socket,
uv_res,
uv_err_name(uv_res),
uv_strerror(uv_res));
res->cacheSocketPort();
} catch (std::exception& ex) {
res->close();
throw;
}
return res;
}
void setOnConnectCallback(OnConnectCallback&& callback) {
onConnectCb = std::move(callback);
}
static c10::intrusive_ptr<UvTcpServer> makeWithPort(
uv_loop_t* loop,
uint16_t port,
bool useIpv6) {
auto res = c10::make_intrusive<UvTcpServer>(loop);
res->handleReady();
try {
struct sockaddr_storage addr {};
int uv_res = 0;
if (useIpv6) {
uv_res = uv_ip6_addr("::", port, (struct sockaddr_in6*)&addr);
} else {
uv_res = uv_ip4_addr("0.0.0.0", port, (struct sockaddr_in*)&addr);
}
TORCH_CHECK(
uv_res == 0,
"UV Store addr parsing failure. useIpv6:{} code:{} name:{} message:{}",
useIpv6,
uv_res,
uv_err_name(uv_res),
uv_strerror(uv_res));
uv_res =
uv_tcp_bind(res->unsafeGetSocket(), (const struct sockaddr*)&addr, 0);
TORCH_CHECK(
uv_res == 0,
"UV Store bind failed. useIpv6:{} code:{} name:{} message:{}",
useIpv6,
uv_res,
uv_err_name(uv_res),
uv_strerror(uv_res));
uv_res =
uv_listen(res->unsafeGetStream(), DEFAULT_BACKLOG, on_new_connection);
TORCH_CHECK(
uv_res == 0,
"UV Store listen failed. useIpv6:{} code:{} name:{} message:{}",
useIpv6,
uv_res,
uv_err_name(uv_res),
uv_strerror(uv_res));
res->cacheSocketPort();
} catch (std::exception& ex) {
res->close();
throw;
}
return res;
}
uint16_t port() const {
return portNum;
}
void accept(const c10::intrusive_ptr<UvTcpSocket>& socket) {
int res =
uv_accept(unsafeGetStream(), (uv_stream_t*)socket->unsafeGetHandle());
TORCH_CHECK(
res == 0,
"Failed to accept socket. code:{} name:{} desc:{}.",
res,
uv_err_name(res),
uv_strerror(res));
}
private:
OnConnectCallback onConnectCb;
uint16_t portNum{};
c10::intrusive_ptr<UvTcpServer> iptr() {
return c10::intrusive_ptr<UvTcpServer>::reclaim_copy(this);
}
static c10::intrusive_ptr<UvTcpServer> borrow(uv_stream_t* handle) {
auto h = (UvTcpServer*)uv_handle_get_data((uv_handle_t*)handle);
return h->iptr();
}
void cacheSocketPort() {
sockaddr_storage addr_s{};
int addr_len = sizeof(addr_s);
if (uv_tcp_getsockname(
(uv_tcp_t*)unsafeGetStream(),
reinterpret_cast<sockaddr*>(&addr_s),
&addr_len) != 0) {
throw std::runtime_error(
"The port number of the socket cannot be retrieved.");
}
if (addr_s.ss_family == AF_INET) {
portNum = ntohs(reinterpret_cast<sockaddr_in*>(&addr_s)->sin_port);
} else {
portNum = ntohs(reinterpret_cast<sockaddr_in6*>(&addr_s)->sin6_port);
}
}
static void missingOnConnect(int status) {
TORCH_CHECK(false, "Socket accepted byt onConnect callback missing");
}
static void on_new_connection(uv_stream_t* server, int status) {
borrow(server)->onConnectCb(status);
}
};
class WriterPayload : public c10::intrusive_ptr_target {
static c10::intrusive_ptr<WriterPayload> reclaim(uv_write_t* request) {
/* This method returns a intrusive_ptr that does not increase the refcount.
*/
auto h = (WriterPayload*)uv_req_get_data((uv_req_t*)request);
return c10::intrusive_ptr<WriterPayload>::reclaim(h);
}
void registeredInLoop() {
/*
This refcount increment must be matched by a reclaim call.
Call this method after sucessfully scheduling this handle with a loop.
*/
at::raw::intrusive_ptr::incref(this);
}
static void write_done(uv_write_t* req, int status) {
/* Since we're no longer actively used by the event loop, transfer ownership
* to this frame. */
auto wp = WriterPayload::reclaim(req);
auto handle = wp->handle;
if (status) {
C10D_INFO(
"Write to client failed. code:{} name:{} desc:{}.",
status,
uv_err_name(status),
uv_strerror(status));
handle->close();
}
}
std::vector<uint8_t> data;
uv_write_t req = {};
uv_buf_t buf = {};
c10::intrusive_ptr<UvHandle> handle;
public:
WriterPayload(
std::vector<uint8_t>&& in_data,
c10::intrusive_ptr<UvHandle> handle)
: data(std::move(in_data)), handle(std::move(handle)) {
uv_req_set_data((uv_req_t*)&req, this);
}
~WriterPayload() override = default;
void send() {
buf = uv_buf_init((char*)data.data(), data.size());
int res = uv_write(
&req, (uv_stream_t*)handle->unsafeGetHandle(), &buf, 1, write_done);
if (res) {
C10D_INFO(
"Write setup to client failed. code:{} name:{} desc:{}.",
res,
uv_err_name(res),
uv_strerror(res));
handle->close();
} else {
/* This object was successfully registered with the event loop, so keep it
* alive until it's unregistered. */
registeredInLoop();
}
}
};
class StreamWriter {
std::vector<uint8_t> data;
c10::intrusive_ptr<UvHandle> handle;
// must be stack allocated
void* operator new(size_t);
public:
StreamWriter(c10::intrusive_ptr<UvHandle> handle)
: handle(std::move(handle)) {}
void write1(uint8_t val) {
data.push_back(val);
}
template <typename T>
void write_value(T val) {
uint8_t* val_ptr = (uint8_t*)&val;
data.insert(data.end(), val_ptr, val_ptr + sizeof(T));
}
void write_vector(const std::vector<uint8_t>& val) {
write_value<uint64_t>(val.size());
data.insert(data.end(), val.begin(), val.end());
}
void write_string(const std::string& val) {
write_value<uint64_t>(val.size());
data.insert(data.end(), val.data(), val.data() + val.size());
}
void send() {
auto wd = c10::make_intrusive<WriterPayload>(std::move(data), handle);
wd->send();
}
};
class ChunkedStream {
std::deque<uv_buf_t> buffers;
size_t buff_idx{0};
size_t buff_offset{0};
size_t capacity{0};
size_t buff_offset_commit{0};
size_t read_offset{0};
public:
ChunkedStream() = default;
size_t buf_count() {
return buffers.size();
}
void append(uv_buf_t buf) {
if (buf.len == 0) {
free(buf.base);
} else {
capacity += buf.len;
buffers.push_back(buf);
}
}
bool read_many(char* dest, size_t size) {
if (available() < size) {
return false;
}
size_t remaining = size;
char* write_base = dest;
while (remaining > 0) {
auto to_read = std::min(buffers[buff_idx].len - buff_offset, remaining);
::memcpy(write_base, buffers[buff_idx].base + buff_offset, to_read);
buff_offset += to_read;
remaining -= to_read;
write_base += to_read;
if (buff_offset >= buffers[buff_idx].len) {
buff_offset = 0;
++buff_idx;
if (buff_idx >= buffers.size() && remaining > 0) {
TORCH_CHECK(
false,
"Trying to read past end of buffer buffer_idx:{} available:{} remaining:{}",
buff_idx,
buffers.size(),
remaining);
}
}
}
read_offset += size;
return true;
}
bool read1(uint8_t& byte) {
while (true) {
if (buff_idx >= buffers.size())
return false;
if (buff_offset >= buffers[buff_idx].len) {
buff_offset = 0;
++buff_idx;
continue;
}
break;
}
byte = buffers[buff_idx].base[buff_offset];
++buff_offset;
++read_offset;
return true;
}
template <typename T>
bool read_value(T& value) {
return read_many((char*)&value, sizeof(T));
}
bool read_key(std::string& str) {
uint64_t size = 0;
if (!read_value(size))
return false;
TORCH_CHECK(
size <= MAX_STRING_LEN,
"Invalid string size. size:{} max:{}",
size,
MAX_STRING_LEN);
if (available() < size)
return false;
str.resize(size);
return read_many((char*)str.data(), size);
}
bool read_payload(std::vector<uint8_t>& data) {
uint64_t size = 0;
if (!read_value(size))
return false;
auto size_in_bytes = size * sizeof(uint8_t);
TORCH_CHECK(
size_in_bytes <= MAX_PAYLOAD_LEN,
"Invalid payload size. size: {} max:{}",
size_in_bytes,
MAX_PAYLOAD_LEN);
if (available() < size_in_bytes)
return false;
data.resize(size);
return read_many((char*)data.data(), size_in_bytes);
}
size_t available() {
return capacity - read_offset;
}
void commit() {
if (buff_idx >= buffers.size() || buff_offset >= buffers[buff_idx].len) {
buff_offset = 0;
if (buff_idx < buffers.size())
++buff_idx;
}
for (size_t i = 0; i < buff_idx; ++i) {
free(buffers[0].base);
capacity -= buffers[0].len;
buffers.pop_front();
}
buff_idx = 0;
read_offset = buff_offset_commit = buff_offset;
}
void reset() {
buff_idx = 0;
read_offset = buff_offset = buff_offset_commit;
}
};
class LibUVStoreDaemon : public BackgroundThread {
public:
explicit LibUVStoreDaemon(int port);
~LibUVStoreDaemon() override;
uint16_t port() const override;
void set(const std::string& key, const std::vector<uint8_t>& value);
const std::vector<uint8_t>& compareAndSet(
const std::string& key,
const std::vector<uint8_t>& expectedValue,
const std::vector<uint8_t>& newValue);
const std::vector<uint8_t>& get(const std::string& key);
int64_t add(const std::string& key, int64_t addVal);
bool checkKeys(const std::vector<std::string>& keys);
bool waitKeys(
const std::vector<std::string>& keys,
const c10::intrusive_ptr<UvHandle>& client);
int64_t size();
int64_t deleteKey(const std::string& key);
void append(const std::string& key, const std::vector<uint8_t>& value);
void registerClient(const c10::intrusive_ptr<UvHandle>& client);
void unregisterClient(const c10::intrusive_ptr<UvHandle>& client);
void clearClientWaitState(const c10::intrusive_ptr<UvHandle>& client);
bool isMiscellaneousClient(const c10::intrusive_ptr<UvHandle>& client);
uint16_t get_socket_port(uv_tcp_t* handle);
void init(const TCPStoreOptions& opts);
protected:
void run() override;
void stop() override;
private:
uv_loop_t loop{};
c10::intrusive_ptr<UvTcpServer> tcpServer;
uv_async_t exit_handle{};
std::unordered_map<std::string, std::vector<uint8_t>> tcpStore_;
// From key -> the list of UvClient waiting on the key
std::unordered_map<std::string, std::vector<c10::intrusive_ptr<UvHandle>>>
waitingSockets_;
// From socket -> number of keys awaited
std::unordered_map<c10::intrusive_ptr<UvHandle>, size_t> keysAwaited_;
std::unordered_set<c10::intrusive_ptr<UvHandle>> clients_;
std::unordered_set<c10::intrusive_ptr<UvHandle>> miscellaneousClients_;
int port_;
static LibUVStoreDaemon& from_uv(uv_handle_t* stream) {
return *(LibUVStoreDaemon*)uv_handle_get_data(stream);
}
static void on_new_connection(uv_stream_t* server, int status) {
from_uv((uv_handle_t*)server).onConnect(status);
}
static void on_exit_request(uv_async_t* handle) {
from_uv((uv_handle_t*)handle).onExitRequest();
}
void onConnect(int status);
void onExitRequest();
void wakeupWaitingClients(const std::string& key);
// bool tryListen(bool use_ipv6);
static void print_active_handles(uv_handle_t* handle, void* arg);
};
class UvClient : public UvTcpSocket {
ChunkedStream stream;
LibUVStoreDaemon* store;
protected:
void processBuf(const uv_buf_t* buf, size_t nread) override {
auto tmp = *buf;
tmp.len = nread;
stream.append(tmp);
while (true) {
stream.reset();
uint8_t command = -1;
if (!stream.read1(command))
break;
if (store->isMiscellaneousClient(iptr())) {
if ((QueryType)command != QueryType::VALIDATE)
return;
if (!parse_validate_command())
return;
} else {
switch ((QueryType)command) {
case QueryType::SET:
if (!parse_set_command())
return;
break;
case QueryType::COMPARE_SET:
if (!parse_compare_set_command())
return;
break;
case QueryType::GET:
if (!parse_get_command())
return;
break;
case QueryType::ADD:
if (!parse_add_command())
return;
break;
case QueryType::CHECK:
if (!parse_check_command())
return;
break;
case QueryType::WAIT:
if (!parse_wait_command())
return;
break;
case QueryType::GETNUMKEYS:
if (!parse_getnumkeys_command())
return;
break;
case QueryType::DELETE_KEY:
if (!parse_delete_key_command())
return;
break;
case QueryType::APPEND:
if (!parse_append_command())
return;
break;
case QueryType::MULTI_GET:
if (!parse_multi_get_command())
return;
break;
case QueryType::MULTI_SET:
if (!parse_multi_set_command())
return;
break;
case QueryType::CANCEL_WAIT:
if (!parse_cancel_wait_command())
return;
break;
default:
C10D_DEBUG(
"Client sent invalid command. client:{} command:{}",
(void*)this,
(int)command);
close();
return;
}
}
stream.commit();
}
}
bool parse_validate_command() {
uint32_t validateNumber = 0;
if (!stream.read_value(validateNumber))
return false;
if (validateNumber != c10d::detail::validationMagicNumber)
return false;
return true;
}
bool parse_set_command() {
std::string key;
if (!stream.read_key(key))
return false;
std::vector<uint8_t> newData;
if (!stream.read_payload(newData))
return false;
store->set(key, newData);
return true;
}
bool parse_compare_set_command() {
std::string key;
if (!stream.read_key(key))
return false;
std::vector<uint8_t> currentValue;
if (!stream.read_payload(currentValue))
return false;
std::vector<uint8_t> newValue;
if (!stream.read_payload(newValue))
return false;
auto res = store->compareAndSet(key, currentValue, newValue);
StreamWriter sw(iptr());
sw.write_vector(res);
sw.send();
return true;
}
bool parse_get_command() {
std::string key;
if (!stream.read_key(key))
return false;
const auto& data = store->get(key);
StreamWriter sw(iptr());
sw.write_vector(data);
sw.send();
return true;
}
bool parse_add_command() {
std::string key;
if (!stream.read_key(key))
return false;
int64_t addVal = 0;
if (!stream.read_value(addVal))
return false;
addVal = store->add(key, addVal);
StreamWriter sw(iptr());
sw.write_value(addVal);
sw.send();
return true;
}
bool parse_check_command() {
uint64_t key_count = 0;
if (!stream.read_value(key_count))
return false;
TORCH_CHECK(
key_count <= MAX_KEY_COUNT,
"Too many keys being waited. keys:{} max:{}",
key_count,
MAX_KEY_COUNT);
std::vector<std::string> keys(key_count);
for (uint64_t i = 0; i < key_count; ++i) {
if (!stream.read_key(keys[i]))
return false;
}
// Now we have received all the keys
StreamWriter sw(iptr());
if (store->checkKeys(keys)) {
sw.write_value(CheckResponseType::READY);
} else {
sw.write_value(CheckResponseType::NOT_READY);
}
sw.send();
return true;
}
bool parse_wait_command() {
uint64_t key_count = 0;
if (!stream.read_value(key_count)) {
return false;
}
TORCH_CHECK(
key_count <= MAX_KEY_COUNT,
"Too many keys being waited. keys:{} max:{}",
key_count,
MAX_KEY_COUNT);
std::vector<std::string> keys(key_count);
for (uint64_t i = 0; i < key_count; ++i) {
if (!stream.read_key(keys[i]))
return false;
}
if (store->waitKeys(keys, iptr())) {
StreamWriter sw(iptr());
sw.write1((uint8_t)WaitResponseType::STOP_WAITING);
sw.send();
}
return true;
}
bool parse_getnumkeys_command() {
StreamWriter sw(iptr());
sw.write_value<int64_t>(store->size());
sw.send();
return true;
}
bool parse_delete_key_command() {
std::string key;
if (!stream.read_key(key))
return false;
auto numDeleted = store->deleteKey(key);
StreamWriter sw(iptr());
sw.write_value<int64_t>(numDeleted);
sw.send();
return true;
}
bool parse_append_command() {
std::string key;
if (!stream.read_key(key)) {
return false;
}
std::vector<uint8_t> data;
if (!stream.read_payload(data)) {
return false;
}
store->append(key, data);
return true;
}
bool parse_multi_get_command() {
uint64_t key_count = 0;
if (!stream.read_value(key_count)) {
return false;
}
TORCH_CHECK(
key_count <= MAX_KEY_COUNT,
"Too many keys with multi_get. keys:{} max:{}",
key_count,
MAX_KEY_COUNT);
StreamWriter sw(iptr());
for (const auto _ : c10::irange(key_count)) {
(void)_; // Suppress unused variable warning
std::string key;
if (!stream.read_key(key)) {
return false;
}
sw.write_vector(store->get(key));
}
sw.send();
return true;
}
bool parse_multi_set_command() {
uint64_t key_count = 0;
if (!stream.read_value(key_count)) {
return false;
}
TORCH_CHECK(
key_count <= MAX_KEY_COUNT,
"Too many keys with multi_get. keys:{} max:{}",
key_count,
MAX_KEY_COUNT);
for (const auto _ : c10::irange(key_count)) {
(void)_; // Suppress unused variable warning
std::string key;
if (!stream.read_key(key)) {
return false;
}
std::vector<uint8_t> newData;
if (!stream.read_payload(newData))
return false;
store->set(key, newData);
}
return true;
}
bool parse_cancel_wait_command() {
store->clearClientWaitState(iptr());
StreamWriter sw(iptr());
sw.write1((uint8_t)WaitResponseType::WAIT_CANCELED);
sw.send();
return true;
}
public:
explicit UvClient(uv_loop_t* loop, LibUVStoreDaemon* store)
: UvTcpSocket(loop), store(store) {}
static c10::intrusive_ptr<UvClient> make(
uv_loop_t* loop,
LibUVStoreDaemon* store) {
auto res = c10::make_intrusive<UvClient>(loop, store);
res->handleReady();
return res;
}
c10::intrusive_ptr<UvClient> iptr() {
return c10::intrusive_ptr<UvClient>::reclaim_copy(this);
}
protected:
void onClose() override {
store->unregisterClient(iptr());
}
};
void LibUVStoreDaemon::onConnect(int status) {
auto client = UvClient::make(&loop, this);
registerClient(client);
try {
tcpServer->accept(client);
client->startRead();
} catch (std::exception& e) {
C10D_INFO("Failed to accept client due to {}", e.what());
client->close();
}
}
void LibUVStoreDaemon::onExitRequest() {
C10D_DEBUG("Store exit requested\n");
uv_close((uv_handle_t*)&exit_handle, nullptr);
uv_stop(&loop);
}
void LibUVStoreDaemon::init(const TCPStoreOptions& opts) {
if (opts.masterListenFd.has_value()) {
tcpServer = UvTcpServer::makeWithSocket(&loop, *opts.masterListenFd);
} else {
try {
tcpServer = UvTcpServer::makeWithPort(&loop, opts.port, /*useIpv6=*/true);
} catch (std::exception& ex) {
C10D_INFO(
"Failed to bind to ipv6 address, trying ipv4. Error: {}", ex.what());
tcpServer =
UvTcpServer::makeWithPort(&loop, opts.port, /*useIpv6=*/false);
}
}
tcpServer->setOnConnectCallback(
[this](auto status) { this->onConnect(status); });
port_ = tcpServer->port();
TORCH_CHECK(
port_ == opts.port || opts.port == 0, // zero means use any port
"listen fd {} is bound to port {}, expected to be bound to port {}",
*opts.masterListenFd,
port_,
opts.port);
}
LibUVStoreDaemon::LibUVStoreDaemon(int port) : port_(port) {
TORCH_CHECK(uv_loop_init(&loop) == 0, "Failed to init uv loop");
TORCH_CHECK(
uv_async_init(&loop, &exit_handle, LibUVStoreDaemon::on_exit_request) ==