Skip to content

Commit

Permalink
fix volume resize in nbsd-lightweight
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmyagkov committed Sep 16, 2024
1 parent 2dab3a5 commit 6a03597
Showing 1 changed file with 64 additions and 12 deletions.
76 changes: 64 additions & 12 deletions cloud/blockstore/libs/service_local/service_local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cloud/blockstore/libs/service/storage.h>
#include <cloud/blockstore/libs/service/storage_provider.h>
#include <cloud/storage/core/libs/common/error.h>
#include <cloud/storage/core/libs/common/timer.h>

#include <library/cpp/protobuf/util/pb_io.h>

Expand Down Expand Up @@ -62,17 +63,23 @@ struct TMountSession
{
const TString SessionId;
const TString ClientId;
const IStoragePtr Storage;
const TStorageAdapter StorageAdapter;
const NProto::EVolumeAccessMode AccessMode;
IStoragePtr Storage;
std::unique_ptr<TStorageAdapter> StorageAdapter;

TMountSession(TString clientId, IStoragePtr storage, ui32 blockSize)
TMountSession(
TString clientId,
NProto::EVolumeAccessMode accessMode,
IStoragePtr storage,
ui32 blockSize)
: SessionId(CreateGuidAsString())
, ClientId(std::move(clientId))
, AccessMode(accessMode)
, Storage(storage)
, StorageAdapter(
std::move(storage),
blockSize,
true)
, StorageAdapter(std::make_unique<TStorageAdapter>(
std::move(storage),
blockSize,
true))
{}
};

Expand All @@ -83,7 +90,7 @@ using TMountSessionMap = THashMap<TString, TMountSessionPtr>;

struct TMountedVolume
{
const NProto::TVolume Volume;
NProto::TVolume Volume;

TMountSessionMap Sessions;
TMutex SessionLock;
Expand All @@ -92,11 +99,15 @@ struct TMountedVolume
: Volume(std::move(volume))
{}

TMountSessionPtr CreateSession(TString clientId, IStoragePtr storage)
TMountSessionPtr CreateSession(
TString clientId,
NProto::EVolumeAccessMode accessMode,
IStoragePtr storage)
{
with_lock (SessionLock) {
auto session = std::make_shared<TMountSession>(
std::move(clientId),
accessMode,
std::move(storage),
Volume.GetBlockSize());

Expand Down Expand Up @@ -201,6 +212,31 @@ class TVolumeManager

TFile fileData(MakeDataPath(diskId), EOpenModeFlag::OpenExisting);
fileData.Resize(volume.GetBlockSize() * blocksCount);

auto mountedVolume = FindMountedVolume(diskId);
if (!mountedVolume) {
return;
}
with_lock (mountedVolume->SessionLock) {
mountedVolume->Volume = volume;
for (auto& session: mountedVolume->Sessions) {
session.second->StorageAdapter.reset();
session.second->Storage.reset();
auto dataPath = MakeDataPath(volume.GetDiskId());
volume.SetDiskId(dataPath);
session.second->Storage = StorageProvider
->CreateStorage(
volume,
session.second->ClientId,
session.second->AccessMode)
.GetValueSync();
session.second->StorageAdapter =
std::make_unique<TStorageAdapter>(
session.second->Storage,
volume.GetBlockSize(),
true);
}
}
}

void DestroyVolume(const TString& diskId)
Expand Down Expand Up @@ -266,6 +302,7 @@ class TVolumeManager

auto session = mountedVolume->CreateSession(
clientId,
accessMode,
std::move(storage));

NProto::TMountVolumeResponse response;
Expand Down Expand Up @@ -666,7 +703,12 @@ TFuture<NProto::TReadBlocksResponse> TLocalService::ReadBlocks(
<< "Out of bounds read request";
}

return session->StorageAdapter.ReadBlocks(
if (!session->StorageAdapter) {
ythrow TServiceError(E_ARGUMENT)
<< "Invalid storage adapter";
}

return session->StorageAdapter->ReadBlocks(
Now(),
std::move(ctx),
std::move(request),
Expand Down Expand Up @@ -725,7 +767,12 @@ TFuture<NProto::TWriteBlocksResponse> TLocalService::WriteBlocks(
<< "Out of bounds write request";
}

return session->StorageAdapter.WriteBlocks(
if (!session->StorageAdapter) {
ythrow TServiceError(E_ARGUMENT)
<< "Invalid storage adapter";
}

return session->StorageAdapter->WriteBlocks(
Now(),
std::move(ctx),
std::move(request),
Expand Down Expand Up @@ -776,7 +823,12 @@ TFuture<NProto::TZeroBlocksResponse> TLocalService::ZeroBlocks(
<< "Out of bounds write request";
}

return session->StorageAdapter.ZeroBlocks(
if (!session->StorageAdapter) {
ythrow TServiceError(E_ARGUMENT)
<< "Invalid storage adapter";
}

return session->StorageAdapter->ZeroBlocks(
Now(),
std::move(ctx),
std::move(request),
Expand Down

0 comments on commit 6a03597

Please sign in to comment.