-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathFhgfsOpsCommKit.c
More file actions
1721 lines (1419 loc) · 56.2 KB
/
Copy pathFhgfsOpsCommKit.c
File metadata and controls
1721 lines (1419 loc) · 56.2 KB
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 <app/App.h>
#include <common/net/message/session/FSyncLocalFileMsg.h>
#include <common/net/message/session/FSyncLocalFileRespMsg.h>
#include <common/net/message/control/GenericResponseMsg.h>
#include <common/net/message/session/rw/ReadLocalFileV2Msg.h>
#include <common/net/message/storage/StatStoragePathMsg.h>
#include <common/net/message/storage/StatStoragePathRespMsg.h>
#include <common/net/message/session/rw/WriteLocalFileMsg.h>
#include <common/net/message/session/rw/WriteLocalFileRespMsg.h>
#ifdef BEEGFS_NVFS
#include <common/net/message/session/rw/ReadLocalFileRDMAMsg.h>
#include <common/net/message/session/rw/WriteLocalFileRDMAMsg.h>
#include <common/net/message/session/rw/WriteLocalFileRDMARespMsg.h>
#endif
#include <common/net/sock/RDMASocket.h>
#include <net/filesystem/FhgfsOpsRemoting.h>
#include <common/nodes/MirrorBuddyGroupMapper.h>
#include <common/nodes/TargetStateStore.h>
#include <common/storage/StorageErrors.h>
#include <common/toolkit/MessagingTk.h>
#include <common/threading/Thread.h>
#include <fault-inject/fault-inject.h>
#include <linux/mempool.h>
#include "FhgfsOpsCommKit.h"
#define COMMKIT_RESET_SLEEP_MS 5000 /* how long to sleep if target state not good/offline */
static struct kmem_cache* headerBufferCache;
static mempool_t* headerBufferPool;
bool FhgfsOpsCommKit_initEmergencyPools()
{
const char* cacheName = BEEGFS_MODULE_NAME_STR "-msgheaders";
if(BEEGFS_COMMKIT_MSGBUF_SIZE > PAGE_SIZE)
{
WARN_ON(1);
return false;
}
#ifdef KERNEL_HAS_KMEMCACHE_DTOR
#if defined(KERNEL_HAS_SLAB_MEM_SPREAD)
headerBufferCache = kmem_cache_create(cacheName, BEEGFS_COMMKIT_MSGBUF_SIZE, 0,
SLAB_MEM_SPREAD, NULL, NULL);
#else
headerBufferCache = kmem_cache_create(cacheName, BEEGFS_COMMKIT_MSGBUF_SIZE, 0,
0, NULL, NULL);
#endif
#else
#if defined(KERNEL_HAS_SLAB_MEM_SPREAD)
headerBufferCache = kmem_cache_create(cacheName, BEEGFS_COMMKIT_MSGBUF_SIZE, 0,
SLAB_MEM_SPREAD, NULL);
#else
headerBufferCache = kmem_cache_create(cacheName, BEEGFS_COMMKIT_MSGBUF_SIZE, 0,
0, NULL);
#endif
#endif
if(!headerBufferCache)
return false;
headerBufferPool = mempool_create_slab_pool(4, headerBufferCache);
if(!headerBufferPool)
{
kmem_cache_destroy(headerBufferCache);
return false;
}
return true;
}
void FhgfsOpsCommKit_releaseEmergencyPools()
{
mempool_destroy(headerBufferPool);
kmem_cache_destroy(headerBufferCache);
}
static void* allocHeaderBuffer(gfp_t gfp)
{
return mempool_alloc(headerBufferPool, gfp);
}
static void freeHeaderBuffer(void* header)
{
mempool_free(header, headerBufferPool);
}
static const struct CommKitContextOps readfileOps;
static const struct CommKitContextOps writefileOps;
struct ReadfileIterOps
{
void (*nextIter)(CommKitContext*, FileOpState*);
void (*prepare)(CommKitContext*, FileOpState*);
};
struct WritefileIterOps
{
void (*nextIter)(CommKitContext*, FileOpState*);
void (*prepare)(CommKitContext*, FileOpState*);
};
static void commkit_initTargetInfo(struct CommKitTargetInfo* info, uint16_t targetID)
{
struct CommKitTargetInfo value = {
.state = CommKitState_PREPARE,
.targetID = targetID,
.useBuddyMirrorSecond = false,
.nodeResult = -FhgfsOpsErr_INTERNAL,
};
*info = value;
}
/**
* Note: Initializes the expectedNodeResult attribute from the size argument
* Note: defaults to server-side mirroring enabled.
*/
void FhgfsOpsCommKit_initFileOpState(FileOpState* state, loff_t offset, size_t size,
uint16_t targetID)
{
*state = (FileOpState) {
.offset = offset,
.transmitted = 0,
// For read: RECVHEADER will bump this in each HEADER-DATA loop iteration
// For write: PREPARE sets this to totalSize
.toBeTransmitted = 0,
.totalSize = size,
.firstWriteDoneForTarget = false,
.receiveFileData = false,
.expectedNodeResult = size,
#ifdef BEEGFS_NVFS
.rdmap = NULL,
#endif
};
commkit_initTargetInfo(&state->base, targetID);
}
void FhgfsOpsCommKit_initFsyncState(struct FsyncContext* context, struct FsyncState* state,
uint16_t targetID)
{
commkit_initTargetInfo(&state->base, targetID);
state->firstWriteDoneForTarget = false;
INIT_LIST_HEAD(&state->base.targetInfoList);
list_add_tail(&state->base.targetInfoList, &context->states);
}
void FhgfsOpsCommKit_initStatStorageState(struct list_head* states,
struct StatStorageState* state, uint16_t targetID)
{
commkit_initTargetInfo(&state->base, targetID);
INIT_LIST_HEAD(&state->base.targetInfoList);
list_add_tail(&state->base.targetInfoList, states);
}
static void __commkit_add_socket_pollstate(CommKitContext* context,
struct CommKitTargetInfo* info, short pollEvents)
{
PollState_addSocket(&context->pollState, info->socket, pollEvents);
context->numPollSocks++;
}
static bool __commkit_prepare_io(CommKitContext* context, struct CommKitTargetInfo* info,
int events)
{
if (fatal_signal_pending(current) || !Node_getIsActive(info->node))
{
info->state = CommKitState_SOCKETINVALIDATE;
return false;
}
// check for a poll() timeout or error (all states have to be cancelled in that case)
if(unlikely(context->pollTimedOut) || BEEGFS_SHOULD_FAIL(commkit_polltimeout, 1) )
{
info->nodeResult = -FhgfsOpsErr_COMMUNICATION;
info->state = CommKitState_SOCKETINVALIDATE;
return false;
}
if(!(info->socket->poll.revents & events) )
{
__commkit_add_socket_pollstate(context, info, events);
return false;
}
return true;
}
static bool __commkit_prepare_generic(CommKitContext* context, struct CommKitTargetInfo* info)
{
TargetMapper* targetMapper = App_getTargetMapper(context->app);
NodeStoreEx* storageNodes = App_getStorageNodes(context->app);
FhgfsOpsErr resolveErr;
NodeConnPool* connPool;
DevicePriorityContext devPrioCtx =
{
.maxConns = 0,
#ifdef BEEGFS_NVFS
.gpuIndex = -1,
#endif
};
bool allowWaitForConn = !context->numAcquiredConns; // don't wait if we got at least
// one conn already (this is important to avoid a deadlock between racing commkit processes)
#ifdef BEEGFS_NVFS
struct iov_iter *data = NULL;
// only set data if this is a storage call, NVFS ops are available and the
// iov is an ITER_IOVEC.
// Extended:
// - For read/write ops: payload is present, so extract FileOpVecState->data.
// - For fsync/statstorage ops: no payload expected, so skip and log (no send data).
// - This ensures we only pass a valid iov_iter to NVFS/GPU checks (RdmaInfo_detectNVFSRequest),
// avoiding false positives.
// - The cnt > 0 guard ensures that only non-empty IOVECs are considered valid.
//
if (context->ioInfo && context->ioInfo->nvfs)
{
if (context->ops == &readfileOps || context->ops == &writefileOps)
{
FileOpState* currentState;
struct FileOpVecState* vs;
currentState = container_of(info, struct FileOpState, base);
vs = container_of(currentState, struct FileOpVecState, base);
if (beegfs_iov_iter_is_iovec(&vs->data))
{
size_t cnt = iov_iter_count(&vs->data);
Logger_logFormatted(context->log, Log_DEBUG, context->ops->logContext,
"%s: state=%d targetID=%hu IOVEC count=%zu", __func__,
info->state, info->selectedTargetID, cnt);
// only set data if there’s actually something to transfer
if (cnt > 0)
data = &vs->data;
}
} else {
// fsync/statstorage ops and no payload
Logger_logFormatted(context->log, Log_DEBUG, context->ops->logContext,
"%s: state=%d targetID=%hu (no send data)", __func__, info->state, info->selectedTargetID);
}
}
#endif
info->socket = NULL;
info->nodeResult = -FhgfsOpsErr_COMMUNICATION;
info->selectedTargetID = info->targetID;
info->headerBuffer = allocHeaderBuffer(allowWaitForConn ? GFP_NOFS : GFP_NOWAIT);
if(!info->headerBuffer)
{
context->numBufferless += 1;
return false;
}
// select the right targetID and get target state
if(!context->ioInfo)
info->selectedTargetID = info->targetID;
else
if(StripePattern_getPatternType(context->ioInfo->pattern) == STRIPEPATTERN_BuddyMirror)
{ // given targetID refers to a buddy mirror group
MirrorBuddyGroupMapper* mirrorBuddies = App_getStorageBuddyGroupMapper(context->app);
info->selectedTargetID = info->useBuddyMirrorSecond ?
MirrorBuddyGroupMapper_getSecondaryTargetID(mirrorBuddies, info->targetID) :
MirrorBuddyGroupMapper_getPrimaryTargetID(mirrorBuddies, info->targetID);
if(unlikely(!info->selectedTargetID) )
{ // invalid mirror group ID
Logger_logErrFormatted(context->log, context->ops->logContext,
"Invalid mirror buddy group ID: %hu", info->targetID);
info->nodeResult = -FhgfsOpsErr_UNKNOWNTARGET;
goto cleanup;
}
}
// check target state
{
TargetStateStore* stateStore = App_getTargetStateStore(context->app);
CombinedTargetState targetState;
bool getStateRes = TargetStateStore_getState(stateStore, info->selectedTargetID,
&targetState);
if(unlikely( !getStateRes ||
(targetState.reachabilityState == TargetReachabilityState_OFFLINE) ||
( context->ioInfo &&
(StripePattern_getPatternType(context->ioInfo->pattern) ==
STRIPEPATTERN_BuddyMirror) &&
(targetState.consistencyState != TargetConsistencyState_GOOD) ) ) )
{ // unusable target state, retry details will be handled in retry handler
int targetAction = CK_SKIP_TARGET;
info->state = CommKitState_CLEANUP;
if(context->ops->selectedTargetBad)
targetAction = context->ops->selectedTargetBad(context, info, &targetState);
if(targetAction == CK_SKIP_TARGET)
goto error;
}
}
// get the target-node reference
info->node = NodeStoreEx_referenceNodeByTargetID(storageNodes,
info->selectedTargetID, targetMapper, &resolveErr);
if(unlikely(!info->node) )
{ // unable to resolve targetID
info->nodeResult = -resolveErr;
goto cleanup;
}
connPool = Node_getConnPool(info->node);
#ifdef BEEGFS_NVFS
// perform first test for GPUD
context->gpudRc = 0;
if (data)
context->gpudRc = RdmaInfo_detectNVFSRequest(&devPrioCtx, data);
#endif
// connect
info->socket = NodeConnPool_acquireStreamSocketEx(connPool, allowWaitForConn, &devPrioCtx);
if(!info->socket)
{ // no conn available => error or didn't want to wait
if(likely(!allowWaitForConn) )
{ // just didn't want to wait => keep stage and try again later
Node_put(info->node);
info->node = NULL;
context->numUnconnectable++;
goto error;
}
else
{ // connection error
if(!context->connFailedLogged)
{ // no conn error logged yet
NodeString nodeAndType;
Node_copyAliasWithTypeStr(info->node, &nodeAndType);
if (fatal_signal_pending(current)){
Logger_logFormatted(context->log, Log_DEBUG, context->ops->logContext,
"Connect to server canceled by pending signal: %s",
nodeAndType.buf );
}
else {
Logger_logFormatted(context->log, Log_WARNING, context->ops->logContext,
"Unable to connect to server: %s",
nodeAndType.buf );
}
}
context->connFailedLogged = true;
goto cleanup;
}
}
info->headerSize = context->ops->prepareHeader(context, info);
if(info->headerSize == 0)
goto cleanup;
context->numAcquiredConns++;
info->state = CommKitState_SENDHEADER;
return true;
cleanup:
info->state = CommKitState_CLEANUP;
return false;
error:
freeHeaderBuffer(info->headerBuffer);
info->headerBuffer = NULL;
return false;
}
static void __commkit_sendheader_generic(CommKitContext* context,
struct CommKitTargetInfo* info)
{
ssize_t sendRes;
if(BEEGFS_SHOULD_FAIL(commkit_sendheader_timeout, 1) )
sendRes = -ETIMEDOUT;
else
sendRes = Socket_send_kernel(info->socket, info->headerBuffer, info->headerSize, 0);
if(unlikely(sendRes != info->headerSize) )
{
NodeString nodeAndType;
Node_copyAliasWithTypeStr(info->node, &nodeAndType);
Logger_logFormatted(context->log, Log_WARNING, context->ops->logContext,
"Failed to send message to %s: %s", nodeAndType.buf,
info->socket->peername);
info->state = CommKitState_SOCKETINVALIDATE;
return;
}
info->state = CommKitState_SENDDATA;
info->headerSize = 0;
}
static void __commkit_senddata_generic(CommKitContext* context, struct CommKitTargetInfo* info)
{
int sendRes;
if(!__commkit_prepare_io(context, info, POLLOUT) )
return;
sendRes = context->ops->sendData(context, info);
if(unlikely(sendRes < 0) )
{
NodeString nodeAndType;
if(sendRes == -EFAULT)
{ // bad buffer address given
Logger_logFormatted(context->log, Log_DEBUG, context->ops->logContext,
"Bad buffer address");
info->nodeResult = -FhgfsOpsErr_ADDRESSFAULT;
info->state = CommKitState_SOCKETINVALIDATE;
return;
}
Node_copyAliasWithTypeStr(info->node, &nodeAndType);
Logger_logErrFormatted(context->log, context->ops->logContext,
"Communication error in SENDDATA stage. Node: %s", nodeAndType.buf );
if(context->ops->printSendDataDetails)
context->ops->printSendDataDetails(context, info);
info->state = CommKitState_SOCKETINVALIDATE;
return;
}
if(sendRes == 0)
{ // all of the data has been sent => proceed to the next stage
info->state = CommKitState_RECVHEADER;
__commkit_add_socket_pollstate(context, info, POLLIN);
return;
}
// there is still data to be sent => prepare pollout for the next round
__commkit_add_socket_pollstate(context, info, POLLOUT);
}
static void __commkit_recvheader_generic(CommKitContext* context, struct CommKitTargetInfo* info)
{
ssize_t recvRes = -EREMOTEIO;
// check for incoming data
if(!__commkit_prepare_io(context, info, POLLIN) )
return;
if(BEEGFS_SHOULD_FAIL(commkit_recvheader_timeout, 1) )
recvRes = -ETIMEDOUT;
else
{
size_t msgLength;
if(info->headerSize < NETMSG_MIN_LENGTH)
{
void *buffer = info->headerBuffer + info->headerSize;
ssize_t size = NETMSG_MIN_LENGTH - info->headerSize;
recvRes = Socket_recvT_kernel(info->socket, buffer, size, MSG_DONTWAIT, 0);
if(recvRes <= 0)
{
Logger_logFormatted(context->log, Log_DEBUG, context->ops->logContext,
"Failed to receive message header from: %s", info->socket->peername);
goto recv_err;
}
info->headerSize += recvRes;
}
msgLength = NetMessage_extractMsgLengthFromBuf(info->headerBuffer);
if(msgLength > BEEGFS_COMMKIT_MSGBUF_SIZE)
{ // message too big to be accepted
Logger_logFormatted(context->log, Log_WARNING, context->ops->logContext,
"Received a message that is too large from: %s (bufLen: %u, msgLen: %zdd)",
info->socket->peername, BEEGFS_COMMKIT_MSGBUF_SIZE, msgLength);
info->state = -CommKitState_SOCKETINVALIDATE;
info->nodeResult = -FhgfsOpsErr_COMMUNICATION;
return;
}
if(info->headerSize < msgLength)
{
void *buffer = info->headerBuffer + info->headerSize;
size_t size = msgLength - info->headerSize;
recvRes = Socket_recvT_kernel(info->socket, buffer, size, MSG_DONTWAIT, 0);
if(recvRes <= 0)
{
Logger_logFormatted(context->log, Log_DEBUG, context->ops->logContext,
"Failed to receive message body from: %s", info->socket->peername);
goto recv_err;
}
info->headerSize += recvRes;
}
if(info->headerSize < msgLength)
return;
}
recv_err:
if(unlikely(recvRes <= 0) )
{ // receive failed
// note: signal pending log msg will be printed in stage SOCKETEXCEPTION, so no need here
if (!fatal_signal_pending(current)) {
NodeString nodeAndType;
Node_copyAliasWithTypeStr(info->node, &nodeAndType);
Logger_logFormatted(context->log, Log_WARNING, context->ops->logContext,
"Receive failed from: %s @ %s", nodeAndType.buf,
info->socket->peername);
}
info->state = CommKitState_SOCKETINVALIDATE;
return;
}
recvRes = context->ops->recvHeader(context, info);
if(unlikely(recvRes < 0) )
info->state = CommKitState_SOCKETINVALIDATE;
else
info->state = CommKitState_RECVDATA;
}
static void __commkit_recvdata_generic(CommKitContext* context, struct CommKitTargetInfo* info)
{
int recvRes;
if(!__commkit_prepare_io(context, info, POLLIN) )
return;
recvRes = context->ops->recvData(context, info);
if(unlikely(recvRes < 0) )
{
NodeString nodeAndType;
Node_copyAliasWithTypeStr(info->node, &nodeAndType);
if(recvRes == -EFAULT)
{ // bad buffer address given
Logger_logFormatted(context->log, Log_DEBUG, context->ops->logContext,
"Bad buffer address");
info->nodeResult = -FhgfsOpsErr_ADDRESSFAULT;
info->state = CommKitState_SOCKETINVALIDATE;
return;
}
else if(recvRes == -ETIMEDOUT)
{ // timeout
Logger_logErrFormatted(context->log, context->ops->logContext,
"Communication timeout in RECVDATA stage. Node: %s",
nodeAndType.buf );
}
else
{ // error
Logger_logErrFormatted(context->log, context->ops->logContext,
"Communication error in RECVDATA stage. Node: %s (recv result: %lld)",
nodeAndType.buf, (long long)recvRes);
}
info->state = CommKitState_SOCKETINVALIDATE;
return;
}
if(recvRes == 0)
info->state = CommKitState_CLEANUP;
else
__commkit_add_socket_pollstate(context, info, POLLIN);
}
static void __commkit_socketinvalidate_generic(CommKitContext* context,
struct CommKitTargetInfo* info)
{
NodeString nodeAndType;
Node_copyAliasWithTypeStr(info->node, &nodeAndType);
if (fatal_signal_pending(current))
{ // interrupted by signal
info->nodeResult = -FhgfsOpsErr_INTERRUPTED;
Logger_logFormatted(context->log, Log_NOTICE, context->ops->logContext,
"Communication interrupted by signal. Node: %s", nodeAndType.buf );
}
else
if(!Node_getIsActive(info->node) )
{
info->nodeResult = -FhgfsOpsErr_UNKNOWNNODE;
Logger_logErrFormatted(context->log, context->ops->logContext,
"Communication with inactive node. Node: %s", nodeAndType.buf );
}
else if (info->nodeResult == -FhgfsOpsErr_ADDRESSFAULT)
{
// not a commkit error. release all resources and treat this CTI as done during cleanup.
}
else
{ // "normal" connection error
info->nodeResult = -FhgfsOpsErr_COMMUNICATION;
Logger_logErrFormatted(context->log, context->ops->logContext,
"Communication error. Node: %s", nodeAndType.buf );
if(context->ops->printSocketDetails)
context->ops->printSocketDetails(context, info);
}
NodeConnPool_invalidateStreamSocket(Node_getConnPool(info->node), info->socket);
context->numAcquiredConns--;
info->socket = NULL;
info->state = CommKitState_CLEANUP;
}
static void __commkit_cleanup_generic(CommKitContext* context, struct CommKitTargetInfo* info)
{
#ifdef BEEGFS_NVFS
//
// Clean up the RDMA mapping.
//
if (context->ops == &readfileOps)
{
FileOpState* currentState = container_of(info, FileOpState, base);
if (currentState->rdmap)
{
RdmaInfo_unmapRead(currentState->rdmap);
currentState->rdmap = NULL;
}
}
else if (context->ops == &writefileOps)
{
FileOpState* currentState = container_of(info, FileOpState, base);
if (currentState->rdmap)
{
RdmaInfo_unmapWrite(currentState->rdmap);
currentState->rdmap = NULL;
}
}
#endif // BEEGFS_NVFS
if(likely(info->socket) )
{
NodeConnPool_releaseStreamSocket(Node_getConnPool(info->node), info->socket);
context->numAcquiredConns--;
}
freeHeaderBuffer(info->headerBuffer);
info->headerBuffer = NULL;
if(likely(info->node) )
{
Node_put(info->node);
info->node = NULL;
}
// prepare next stage
if(unlikely(
(info->nodeResult == -FhgfsOpsErr_COMMUNICATION) ||
(info->nodeResult == -FhgfsOpsErr_AGAIN &&
(context->ops->retryFlags & CK_RETRY_LOOP_EAGAIN) ) ) )
{ // comm error occurred => check whether we can do a retry
if (fatal_signal_pending(current))
info->nodeResult = -FhgfsOpsErr_INTERRUPTED;
else if (App_getConnRetriesEnabled(context->app) &&
(!context->maxNumRetries || context->currentRetryNum < context->maxNumRetries))
{ // we have retries left
context->numRetryWaiters++;
info->state = CommKitState_RETRYWAIT;
return;
}
}
// success or no retries left => done
context->numDone++;
info->state = CommKitState_DONE;
}
static void __commkit_start_retry(CommKitContext* context, int flags)
{
struct CommKitTargetInfo* info;
TargetStateStore* stateStore = App_getTargetStateStore(context->app);
MirrorBuddyGroupMapper* mirrorBuddies = App_getStorageBuddyGroupMapper(context->app);
unsigned patternType = context->ioInfo
? StripePattern_getPatternType(context->ioInfo->pattern)
: STRIPEPATTERN_Invalid;
bool cancelRetries = false; // true if there are offline targets
bool resetRetries = false; /* true to not deplete retries if there are unusable target states
("!good && !offline") */
bool sleepOnResetRetries = true; // true to retry immediately without sleeping
// reset context values for retry round
context->numRetryWaiters = 0;
context->pollTimedOut = false;
// check for offline targets
list_for_each_entry(info, context->targetInfoList, targetInfoList)
{
if(info->state == CommKitState_RETRYWAIT)
{
CombinedTargetState targetState;
CombinedTargetState buddyTargetState;
uint16_t targetID = info->targetID;
uint16_t buddyTargetID = info->targetID;
bool getTargetStateRes;
bool getBuddyTargetStateRes = true;
// resolve the actual targetID
if(patternType == STRIPEPATTERN_BuddyMirror)
{
targetID = info->useBuddyMirrorSecond ?
MirrorBuddyGroupMapper_getSecondaryTargetID(mirrorBuddies, info->targetID) :
MirrorBuddyGroupMapper_getPrimaryTargetID(mirrorBuddies, info->targetID);
buddyTargetID = info->useBuddyMirrorSecond ?
MirrorBuddyGroupMapper_getPrimaryTargetID(mirrorBuddies, info->targetID) :
MirrorBuddyGroupMapper_getSecondaryTargetID(mirrorBuddies, info->targetID);
}
// check current target state
getTargetStateRes = TargetStateStore_getState(stateStore, targetID,
&targetState);
if (targetID == buddyTargetID)
buddyTargetState = targetState;
else
getBuddyTargetStateRes = TargetStateStore_getState(stateStore, buddyTargetID,
&buddyTargetState);
if( (!getTargetStateRes && ( (targetID != buddyTargetID) && !getBuddyTargetStateRes) ) ||
( (targetState.reachabilityState == TargetReachabilityState_OFFLINE) &&
(buddyTargetState.reachabilityState == TargetReachabilityState_OFFLINE) ) )
{ // no more retries when both buddies are offline
LOG_DEBUG_FORMATTED(context->log, Log_SPAM, context->ops->logContext,
"Skipping communication with offline targetID: %hu",
targetID);
cancelRetries = true;
break;
}
if(flags & CK_RETRY_BUDDY_FALLBACK)
{
if( ( !getTargetStateRes ||
(targetState.consistencyState != TargetConsistencyState_GOOD) ||
(targetState.reachabilityState != TargetReachabilityState_ONLINE) )
&& ( getBuddyTargetStateRes &&
(buddyTargetState.consistencyState == TargetConsistencyState_GOOD) &&
(buddyTargetState.reachabilityState == TargetReachabilityState_ONLINE) ) )
{ // current target not good but buddy is good => switch to buddy
LOG_DEBUG_FORMATTED(context->log, Log_SPAM, context->ops->logContext,
"Switching to buddy with good target state. "
"targetID: %hu; target state: %s / %s",
targetID, TargetStateStore_reachabilityStateToStr(targetState.reachabilityState),
TargetStateStore_consistencyStateToStr(targetState.consistencyState) );
info->useBuddyMirrorSecond = !info->useBuddyMirrorSecond;
info->state = CommKitState_PREPARE;
resetRetries = true;
sleepOnResetRetries = false;
continue;
}
}
if( (patternType == STRIPEPATTERN_BuddyMirror) &&
( (targetState.reachabilityState != TargetReachabilityState_ONLINE) ||
(targetState.consistencyState != TargetConsistencyState_GOOD) ) )
{ // both buddies not good, but at least one of them not offline => wait for clarification
LOG_DEBUG_FORMATTED(context->log, Log_DEBUG, context->ops->logContext,
"Waiting because of target state. "
"targetID: %hu; target state: %s / %s",
targetID, TargetStateStore_reachabilityStateToStr(targetState.reachabilityState),
TargetStateStore_consistencyStateToStr(targetState.consistencyState) );
info->state = CommKitState_PREPARE;
resetRetries = true;
continue;
}
if(info->nodeResult == -FhgfsOpsErr_AGAIN && (flags & CK_RETRY_LOOP_EAGAIN))
{
Logger_logFormatted(context->log, Log_DEBUG, context->ops->logContext,
"Waiting because target asked for infinite retries. targetID: %hu", targetID);
info->state = CommKitState_PREPARE;
resetRetries = true;
continue;
}
// normal retry
info->state = CommKitState_PREPARE;
}
}
// if we have offline targets, cancel all further retry waiters
if(cancelRetries)
{
list_for_each_entry(info, context->targetInfoList, targetInfoList)
{
if( (info->state != CommKitState_RETRYWAIT) && (info->state != CommKitState_PREPARE ) )
continue;
context->numDone++;
info->state = CommKitState_DONE;
}
return;
}
// wait before we actually start the retry
if(resetRetries)
{ // reset retries to not deplete them in case of non-good and non-offline targets
if(sleepOnResetRetries)
Thread_sleep(COMMKIT_RESET_SLEEP_MS);
context->currentRetryNum = 0;
}
else
{ // normal retry
MessagingTk_waitBeforeRetry(context->currentRetryNum);
context->currentRetryNum++;
}
}
static FhgfsOpsErr __commkit_message_genericResponse(CommKitContext* context,
struct CommKitTargetInfo* info, unsigned requestMsgType)
{
const char* logContext = "Messaging (RPC)";
bool parseRes;
GenericResponseMsg msg;
NodeString nodeAndType;
Node_copyAliasWithTypeStr(info->node, &nodeAndType);
GenericResponseMsg_init(&msg);
parseRes = NetMessageFactory_deserializeFromBuf(context->app, info->headerBuffer,
info->headerSize, &msg.simpleIntStringMsg.netMessage, NETMSGTYPE_GenericResponse);
if(!parseRes)
{
Logger_logFormatted(context->log, Log_ERR, "received bad message type from %s: %i",
nodeAndType.buf,
NetMessage_getMsgType(&msg.simpleIntStringMsg.netMessage) );
return -FhgfsOpsErr_INTERNAL;
}
switch(GenericResponseMsg_getControlCode(&msg) )
{
case GenericRespMsgCode_TRYAGAIN:
if(!info->logged.peerTryAgain)
{
info->logged.peerTryAgain = true;
Logger_logFormatted(context->log, Log_NOTICE, logContext,
"Peer is asking for a retry: %s; Reason: %s",
nodeAndType.buf,
GenericResponseMsg_getLogStr(&msg) );
Logger_logFormatted(context->log, Log_DEBUG, logContext,
"Message type: %u", requestMsgType);
}
return FhgfsOpsErr_AGAIN;
case GenericRespMsgCode_INDIRECTCOMMERR:
if(!info->logged.indirectCommError)
{
info->logged.indirectCommError = true;
Logger_logFormatted(context->log, Log_NOTICE, logContext,
"Peer reported indirect communication error: %s; Reason: %s",
nodeAndType.buf,
GenericResponseMsg_getLogStr(&msg) );
Logger_logFormatted(context->log, Log_DEBUG, logContext,
"Message type: %u", requestMsgType);
}
return FhgfsOpsErr_COMMUNICATION;
default:
Logger_logFormatted(context->log, Log_NOTICE, logContext,
"Peer replied with unknown control code: %s; Code: %u; Reason: %s",
nodeAndType.buf,
(unsigned)GenericResponseMsg_getControlCode(&msg),
GenericResponseMsg_getLogStr(&msg) );
Logger_logFormatted(context->log, Log_DEBUG, logContext,
"Message type: %u", requestMsgType);
return FhgfsOpsErr_INTERNAL;
}
}
void FhgfsOpsCommkit_communicate(App* app, RemotingIOInfo* ioInfo, struct list_head* targetInfos,
const struct CommKitContextOps* ops, void* private)
{
Config* cfg = App_getConfig(app);
int numStates = 0;
CommKitContext context =
{
.ops = ops,
.app = app,
.log = App_getLogger(app),
.private = private,
.ioInfo = ioInfo,
.targetInfoList = targetInfos,
.numRetryWaiters = 0, // counter for states that encountered a comm error
.numDone = 0, // counter for finished states
.numAcquiredConns = 0, // counter for currently acquired conns (to wait only for first conn)
.pollTimedOut = false,
.pollTimeoutLogged = false,
.connFailedLogged = false,
.currentRetryNum = 0,
.maxNumRetries = Config_getConnNumCommRetries(cfg),
#ifdef BEEGFS_NVFS
.gpudRc = -1,
#endif
};
do
{
struct CommKitTargetInfo* info;
context.numUnconnectable = 0; // will be increased by states the didn't get a conn
context.numPollSocks = 0; // will be increased by the states that need to poll
context.numBufferless = 0;
numStates = 0;
PollState_init(&context.pollState);
// let each state do something (before we call poll)
list_for_each_entry(info, targetInfos, targetInfoList)
{
numStates++;
switch(info->state)
{
case CommKitState_PREPARE:
if(!__commkit_prepare_generic(&context, info) )
break;
BEEGFS_FALLTHROUGH;
case CommKitState_SENDHEADER:
__commkit_sendheader_generic(&context, info);
__commkit_add_socket_pollstate(&context, info,
context.ops->sendData ? POLLOUT : POLLIN);
break;
case CommKitState_SENDDATA:
if(context.ops->sendData)
{
__commkit_senddata_generic(&context, info);
break;
}
BEEGFS_FALLTHROUGH;
case CommKitState_RECVHEADER:
if(context.ops->recvHeader)
{
__commkit_recvheader_generic(&context, info);
break;
}
BEEGFS_FALLTHROUGH;
case CommKitState_RECVDATA:
if(context.ops->recvData)
{
__commkit_recvdata_generic(&context, info);
break;
}
BEEGFS_FALLTHROUGH;
case CommKitState_CLEANUP:
__commkit_cleanup_generic(&context, info);
break;
case CommKitState_SOCKETINVALIDATE: