@@ -413,45 +413,67 @@ void TFileSystem::Write(
413413 request->SetBuffer (alignedBuffer.TakeBuffer ());
414414
415415 const auto size = buffer.size ();
416+ const auto wbcMode = GetServerWriteBackCacheMode (fi);
416417
417- if (ShouldUseServerWriteBackCache (fi)) {
418+ const auto handle = fi->fh ;
419+ const auto reqId = callContext->RequestId ;
420+
421+ auto callback = [=, ptr = weak_from_this ()](const auto & future)
422+ {
423+ auto self = ptr.lock ();
424+ if (!self) {
425+ return ;
426+ }
427+
428+ const auto & response = future.GetValue ();
429+ const auto & error = response.GetError ();
430+
431+ if (wbcMode != EServerWriteBackCacheMode::Enabled) {
432+ self->FSyncQueue
433+ ->Dequeue (reqId, error, TNodeId{ino}, THandle{handle});
434+ }
435+
436+ if (self->CheckResponse (self, *callContext, req, response)) {
437+ self->ReplyWrite (*callContext, error, req, size);
438+ }
439+ };
440+
441+ if (wbcMode == EServerWriteBackCacheMode::Enabled) {
418442 WriteBackCache.WriteData (callContext, std::move (request))
419- .Subscribe (
420- [=,
421- ptr = weak_from_this ()] (const auto & future)
422- {
423- auto self = ptr.lock ();
424- if (!self) {
425- return ;
426- }
427-
428- const auto & response = future.GetValue ();
429- const auto & error = response.GetError ();
430-
431- if (CheckResponse (self, *callContext, req, response)) {
432- self->ReplyWrite (*callContext, error, req, size);
433- }
434- });
443+ .Subscribe (std::move (callback));
435444 return ;
436445 }
437446
438- const auto handle = fi->fh ;
439- const auto reqId = callContext->RequestId ;
440447 FSyncQueue->Enqueue (reqId, TNodeId {ino}, THandle {handle});
441448
442- Session->WriteData (callContext, std::move (request))
443- .Subscribe ([=, ptr = weak_from_this ()] (const auto & future) {
444- auto self = ptr.lock ();
445- if (!self) {
446- return ;
447- }
449+ if (wbcMode == EServerWriteBackCacheMode::Disabled) {
450+ Session->WriteData (callContext, std::move (request))
451+ .Subscribe (std::move (callback));
452+ return ;
453+ }
448454
449- const auto & response = future.GetValue ();
450- const auto & error = response.GetError ();
451- self->FSyncQueue ->Dequeue (reqId, error, TNodeId {ino}, THandle {handle});
455+ Y_ABORT_UNLESS (
456+ wbcMode == EServerWriteBackCacheMode::Draining,
457+ " Invalid EServerWriteBackCacheMode value = %d" ,
458+ wbcMode);
452459
453- if (CheckResponse (self, *callContext, req, response)) {
454- self->ReplyWrite (*callContext, error, req, size);
460+ auto flushFuture = WriteBackCache.FlushNodeData (request->GetNodeId ());
461+ if (flushFuture.HasValue ()) {
462+ Session->WriteData (callContext, std::move (request))
463+ .Subscribe (std::move (callback));
464+ return ;
465+ }
466+
467+ flushFuture.Subscribe (
468+ [ptr = weak_from_this (),
469+ callback = std::move (callback),
470+ callContext = std::move (callContext),
471+ request = std::move (request)](const auto & f) mutable
472+ {
473+ f.GetValue ();
474+ if (auto self = ptr.lock ()) {
475+ self->Session ->WriteData (callContext, std::move (request))
476+ .Subscribe (std::move (callback));
455477 }
456478 });
457479}
@@ -577,43 +599,66 @@ void TFileSystem::WriteBuf(
577599 request->SetHandle (fi->fh );
578600 request->SetOffset (offset);
579601
580- if (ShouldUseServerWriteBackCache (fi)) {
602+ const auto wbcMode = GetServerWriteBackCacheMode (fi);
603+ const auto handle = fi->fh ;
604+ const auto reqId = callContext->RequestId ;
605+
606+ auto callback = [=, ptr = weak_from_this ()](const auto & future)
607+ {
608+ auto self = ptr.lock ();
609+ if (!self) {
610+ return ;
611+ }
612+
613+ const auto & response = future.GetValue ();
614+ const auto & error = response.GetError ();
615+
616+ if (wbcMode != EServerWriteBackCacheMode::Enabled) {
617+ self->FSyncQueue
618+ ->Dequeue (reqId, error, TNodeId{ino}, THandle{handle});
619+ }
620+
621+ if (self->CheckResponse (self, *callContext, req, response)) {
622+ self->ReplyWrite (*callContext, error, req, size);
623+ }
624+ };
625+
626+ if (wbcMode == EServerWriteBackCacheMode::Enabled) {
581627 WriteBackCache.WriteData (callContext, std::move (request))
582- .Subscribe (
583- [=, ptr = weak_from_this ()] (const auto & future)
584- {
585- auto self = ptr.lock ();
586- if (!self) {
587- return ;
588- }
589-
590- const auto & response = future.GetValue ();
591- const auto & error = response.GetError ();
592-
593- if (CheckResponse (self, *callContext, req, response)) {
594- self->ReplyWrite (*callContext, error, req, size);
595- }
596- });
628+ .Subscribe (std::move (callback));
597629 return ;
598630 }
599631
600- const auto handle = fi->fh ;
601- const auto reqId = callContext->RequestId ;
602- FSyncQueue->Enqueue (reqId, TNodeId {ino}, THandle {handle});
632+ FSyncQueue->Enqueue (reqId, TNodeId{ino}, THandle{handle});
603633
604- Session->WriteData (callContext, std::move (request))
605- .Subscribe ([=, ptr = weak_from_this ()] (const auto & future) {
606- auto self = ptr.lock ();
607- if (!self) {
608- return ;
609- }
634+ if (wbcMode == EServerWriteBackCacheMode::Disabled) {
635+ Session->WriteData (callContext, std::move (request))
636+ .Subscribe (std::move (callback));
637+ return ;
638+ }
610639
611- const auto & response = future.GetValue ();
612- const auto & error = response.GetError ();
613- self->FSyncQueue ->Dequeue (reqId, error, TNodeId {ino}, THandle {handle});
640+ Y_ABORT_UNLESS (
641+ wbcMode == EServerWriteBackCacheMode::Draining,
642+ " Invalid EServerWriteBackCacheMode value = %d" ,
643+ wbcMode);
614644
615- if (CheckResponse (self, *callContext, req, response)) {
616- self->ReplyWrite (*callContext, error, req, size);
645+ auto flushFuture = WriteBackCache.FlushNodeData (request->GetNodeId ());
646+ if (flushFuture.HasValue ()) {
647+ Session->WriteData (callContext, std::move (request))
648+ .Subscribe (std::move (callback));
649+ return ;
650+ }
651+
652+ flushFuture.Subscribe (
653+ [ptr = weak_from_this (),
654+ callback = std::move (callback),
655+ callContext = std::move (callContext),
656+ request = std::move (request)](const auto & f) mutable
657+ {
658+ f.GetValue ();
659+ if (auto self = ptr.lock ()) {
660+ self->Session ->WriteData (callContext, std::move (request))
661+ .Subscribe (std::move (callback));
617662 }
618663 });
619664}
@@ -960,17 +1005,23 @@ void TFileSystem::FSyncDir(
9601005
9611006// //////////////////////////////////////////////////////////////////////////////
9621007
963- bool TFileSystem::ShouldUseServerWriteBackCache (const fuse_file_info* fi) const
1008+ EServerWriteBackCacheMode TFileSystem::GetServerWriteBackCacheMode (
1009+ const fuse_file_info* fi) const
9641010{
965- if (!WriteBackCache || !Config-> GetServerWriteBackCacheEnabled () ) {
966- return false ;
1011+ if (!WriteBackCache) {
1012+ return EServerWriteBackCacheMode::Disabled ;
9671013 }
9681014
9691015 if (fi->flags & O_DIRECT) {
970- return false ;
1016+ return EServerWriteBackCacheMode::Disabled ;
9711017 }
9721018
973- return true ;
1019+ if (Config->GetServerWriteBackCacheEnabled ()) {
1020+ return EServerWriteBackCacheMode::Enabled;
1021+ }
1022+
1023+ return WriteBackCache.IsEmpty () ? EServerWriteBackCacheMode::Disabled
1024+ : EServerWriteBackCacheMode::Draining;
9741025}
9751026
9761027} // namespace NCloud::NFileStore::NFuse
0 commit comments