Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cloud/blockstore/apps/client/lib/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ NProto::TMountVolumeResponse TCommand::MountVolume(
ClientConfig,
sessionConfig);

auto response = SafeExecute<NProto::TMountVolumeResponse>([&] {
NProto::TMountVolumeResponse response = SafeExecute([&] {
return WaitFor(session->MountVolume());
});

Expand All @@ -366,7 +366,7 @@ NProto::TMountVolumeResponse TCommand::MountVolume(

bool TCommand::UnmountVolume(ISession& session)
{
auto response = SafeExecute<NProto::TUnmountVolumeResponse>([&] {
NProto::TUnmountVolumeResponse response = SafeExecute([&] {
return WaitFor(session.UnmountVolume());
});

Expand Down
4 changes: 1 addition & 3 deletions cloud/blockstore/apps/client/lib/read_blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ class TReadBlocksCommand final

bool ReadBatch(IReadRangeIterator& rangeIter, ui32 batchId)
{
auto error = SafeExecute<NProto::TError>([&] {
auto error = SafeExecute([&] {
auto readRange = rangeIter.Next();

TString buffer;
Expand Down Expand Up @@ -496,8 +496,6 @@ class TReadBlocksCommand final
});
Ready.Signal();
});

return NProto::TError();
});

if (HasError(error)) {
Expand Down
2 changes: 1 addition & 1 deletion cloud/blockstore/apps/client/lib/write_blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ class TWriteBlocksCommand final
return true;
}

auto error = SafeExecute<NProto::TError>([&] {
auto error = SafeExecute([&] {
auto request = std::make_shared<NProto::TWriteBlocksLocalRequest>();
request->SetStartIndex(writeRange->StartIndex);
request->BlocksCount = writeRange->BlocksCount;
Expand Down
8 changes: 4 additions & 4 deletions cloud/blockstore/libs/client_rdma/rdma_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,10 +859,10 @@ NThreading::TFuture<TResultOrError<IBlockStorePtr>> CreateRdmaEndpointClientAsyn

auto future = client->StartEndpoint(config.Address, config.Port);
return future.Apply([endpoint = std::move(endpoint)] (const auto& future) mutable {
auto result = SafeExecute<TResultOrError<IBlockStorePtr>>(
auto result = SafeExecute(
[&] {
endpoint->Init(future.GetValue());
return TResultOrError<IBlockStorePtr>(endpoint);
return endpoint;
});
return result;
});
Expand Down Expand Up @@ -902,10 +902,10 @@ NThreading::TFuture<TResultOrError<IBlockStorePtr>> CreateRdmaDataEndpointAsync(

auto future = client->StartEndpoint(config.Address, config.Port);
return future.Apply([endpoint = std::move(endpoint)] (const auto& future) mutable {
auto result = SafeExecute<TResultOrError<IBlockStorePtr>>(
auto result = SafeExecute(
[&] {
endpoint->Init(future.GetValue());
return TResultOrError<IBlockStorePtr>(endpoint);
return endpoint;
});
return result;
});
Expand Down
2 changes: 1 addition & 1 deletion cloud/blockstore/libs/encryption/encryption_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ class TVolumeEncryptionClient final

const auto& encodedDEK = desc.GetEncryptionKey().GetEncryptedDEK();

auto [dek, error] = SafeExecute<TResultOrError<TString>>([&] {
auto [dek, error] = SafeExecute([&] {
return Base64Decode(encodedDEK);
});

Expand Down
4 changes: 2 additions & 2 deletions cloud/blockstore/libs/encryption/encryption_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class TEncryptionKeyProvider final
private:
TResponse ReadKeyFromKeyring(ui32 keyringId, ui32 expectedLen)
{
return SafeExecute<TResponse>([&] () -> TResponse {
return SafeExecute([&] () -> TResponse {
auto keyring = TKeyring::Create(keyringId);

if (keyring.GetValueSize() != expectedLen) {
Expand All @@ -112,7 +112,7 @@ class TEncryptionKeyProvider final

TResponse ReadKeyFromFile(TString filePath, ui32 expectedLen)
{
return SafeExecute<TResponse>([&] () -> TResponse {
return SafeExecute([&] () -> TResponse {
TFile file(
filePath,
EOpenModeFlag::OpenExisting | EOpenModeFlag::RdOnly);
Expand Down
2 changes: 1 addition & 1 deletion cloud/blockstore/libs/endpoints_rdma/rdma_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void TRdmaEndpoint::HandleRequest(
in,
out]() mutable
{
auto error = SafeExecute<NProto::TError>(
auto error = SafeExecute(
[self,
context,
callContext = std::move(callContext),
Expand Down
14 changes: 5 additions & 9 deletions cloud/blockstore/libs/local_nvme/service_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,8 @@ auto TLocalNVMeService::FetchDevices() -> NProto::TError

const auto pciAddr = NormalizePCIAddr(src.GetPCIAddress());

auto [device, deviceError] =
SafeExecute<TResultOrError<NProto::TNVMeDevice>>(
[&] { return SysFs->GetNVMeDeviceFromPCIAddr(pciAddr); });
auto [device, deviceError] = SafeExecute(
[&] { return SysFs->GetNVMeDeviceFromPCIAddr(pciAddr); });

if (HasError(deviceError)) {
STORAGE_ERROR(
Expand Down Expand Up @@ -400,12 +399,9 @@ auto TLocalNVMeService::BindDeviceToDriver(
"Bind " << device.GetSerialNumber().Quote() << " to " << driverName
<< " driver");

return SafeExecute<NProto::TError>(
return SafeExecute(
[&]
{
SysFs->BindPCIDeviceToDriver(device.GetPCIAddress(), driverName);
return MakeError(S_OK);
});
{ SysFs->BindPCIDeviceToDriver(device.GetPCIAddress(), driverName); });

return {};
}
Expand Down Expand Up @@ -493,7 +489,7 @@ auto TLocalNVMeService::ResetToSingleNamespace(
"Reset NVMe " << device.GetSerialNumber().Quote()
<< " to single namespace");

return SafeExecute<NProto::TError>(
return SafeExecute(
[&]
{
const TFsPath ctrlPath = GetNVMeCtrlPath(device);
Expand Down
8 changes: 2 additions & 6 deletions cloud/blockstore/libs/nbd/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ class TEndpoint final
auto localHost = PrintHostAndPort(ListenAddress);
STORAGE_DEBUG("listen on " << localHost);

return SafeExecute<NProto::TError>([&] {
return SafeExecute([&] {
ValidateSocketPath(ListenAddress);
DeleteSocketIfExists(ListenAddress);

Expand All @@ -325,14 +325,12 @@ class TEndpoint final
ListenAddress,
SocketAccessMode);
}

return NProto::TError();
});
}

NProto::TError Stop(bool deleteSocket)
{
return SafeExecute<NProto::TError>([&] {
return SafeExecute([&] {
if (Connection) {
Connection->Stop();
};
Expand All @@ -344,8 +342,6 @@ class TEndpoint final
if (deleteSocket) {
DeleteSocketIfExists(ListenAddress);
}

return NProto::TError();
});
}

Expand Down
16 changes: 6 additions & 10 deletions cloud/blockstore/libs/nvme/nvme_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ class TNvmeManager final

TResultOrError<TString> GetSerialNumber(const TString& path) override
{
return SafeExecute<TResultOrError<TString>>([&] {
return SafeExecute([&] {
TFileHandle device(path, OpenExisting | RdOnly);

auto str = [] (auto& arr) {
Expand All @@ -419,7 +419,7 @@ class TNvmeManager final

TResultOrError<bool> IsSsd(const TString& path) override
{
return SafeExecute<TResultOrError<bool>>([&] {
return SafeExecute([&] {
TFileHandle device(path, OpenExisting | RdOnly);

auto [isRot, error] = IsRotational(device);
Expand All @@ -434,7 +434,7 @@ class TNvmeManager final

NProto::TError Sanitize(const TString& ctrlPath) override
{
return SafeExecute<NProto::TError>(
return SafeExecute(
[&]
{
TFileHandle device(ctrlPath, OpenExisting | RdWr);
Expand All @@ -454,16 +454,14 @@ class TNvmeManager final
STORAGE_THROW_SERVICE_ERROR(MAKE_SYSTEM_ERROR(err))
<< "Sanitize failed: " << strerror(err);
}

return MakeError(S_OK);
});
}

TResultOrError<TSanitizeStatus> GetSanitizeStatus(
const TString& ctrlPath) override
{
return SafeExecute<TResultOrError<TSanitizeStatus>>(
[&]() -> TResultOrError<TSanitizeStatus>
return SafeExecute(
[&]
{
TFileHandle device(ctrlPath, OpenExisting | RdWr);
if (!device.IsOpen()) {
Expand Down Expand Up @@ -525,7 +523,7 @@ class TNvmeManager final

NProto::TError ResetToSingleNamespace(const TString& ctrlPath) final
{
return SafeExecute<NProto::TError>(
return SafeExecute(
[&]
{
TFileHandle device(ctrlPath, OpenExisting | RdWr);
Expand Down Expand Up @@ -589,8 +587,6 @@ class TNvmeManager final
CreateNamespace(device, totalBlocks, lbaFormatIndex);

AttachNamespace(device, nsid, ctrl.cntlid);

return MakeError(S_OK);
});
}

Expand Down
2 changes: 1 addition & 1 deletion cloud/blockstore/libs/service_rdma/rdma_target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class TRequestHandler final
TStringBuf out) override
{
TaskQueue->ExecuteSimple([=, endpoint = Endpoint] {
auto error = SafeExecute<NProto::TError>([=] {
auto error = SafeExecute([=] {
return DoHandleRequest(context, callContext, in, out);
});

Expand Down
3 changes: 1 addition & 2 deletions cloud/blockstore/libs/storage/disk_agent/rdma_target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ class TRequestHandler final
context = context,
doHandleRequest = std::move(doHandleRequest)]() mutable
{
auto error =
SafeExecute<NProto::TError>(std::move(doHandleRequest));
auto error = SafeExecute(std::move(doHandleRequest));

if (error.GetCode()) {
if (auto ep = endpoint.lock()) {
Expand Down
2 changes: 1 addition & 1 deletion cloud/blockstore/libs/vhost/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ TFuture<NProto::TError> TServer::StartEndpoint(
options,
std::move(storage));

auto error = SafeExecute<NProto::TError>([&] {
auto error = SafeExecute([&] {
return endpoint->Start();
});
if (HasError(error)) {
Expand Down
2 changes: 1 addition & 1 deletion cloud/blockstore/tools/testing/rdma-test/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TRequestHandler final
TStringBuf out) override
{
TaskQueue->ExecuteSimple([=, this] {
auto error = SafeExecute<NProto::TError>([=, this] {
auto error = SafeExecute([=, this] {
return DoHandleRequest(context, callContext, in, out);
});

Expand Down
67 changes: 46 additions & 21 deletions cloud/storage/core/libs/common/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -456,51 +456,76 @@ class TErrorResponse

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

template <typename TResponse, typename T>
TResponse SafeExecute(T&& block)
template <typename T>
struct TSafeExecuteResultImpl
{
using Type = TResultOrError<T>;
};

template <typename T>
requires std::is_same_v<T, void> || std::is_convertible_v<T, NProto::TError>
struct TSafeExecuteResultImpl<T>
{
using Type = NProto::TError;
};

template <TAcceptsError T>
struct TSafeExecuteResultImpl<T>
{
using Type = T;
};

template <typename T>
struct TSafeExecuteResultImpl<TResultOrError<T>>
{
using Type = TResultOrError<T>;
};

template <typename T>
using TSafeExecuteResult =
typename TSafeExecuteResultImpl<std::invoke_result_t<T>>::Type;

template <typename T>
auto SafeExecute(T&& block) noexcept -> TSafeExecuteResult<T>
{
try {
return block();
if constexpr (std::is_same_v<std::invoke_result_t<T>, void>) {
std::forward<T>(block)();
return {};
} else {
return std::forward<T>(block)();
}
} catch (const TServiceError& e) {
return TErrorResponse(e);
return TErrorResponse(e.GetCode(), TString(e.GetMessage()));
} catch (const TIoSystemError& e) {
return TErrorResponse(MAKE_SYSTEM_ERROR(e.Status()), e.what());
} catch (...) {
return TErrorResponse(E_FAIL, CurrentExceptionMessage());
}
}

template <typename T>
template <TAcceptsError T>
T ExtractResponse(NThreading::TFuture<T>& future)
{
return SafeExecute<T>([&] {
return future.ExtractValue();
});
return SafeExecute([&] { return future.ExtractValue(); });
}

template <typename T>
TResultOrError<T> ResultOrError(const NThreading::TFuture<T>& future)
TResultOrError<T> ResultOrError(const NThreading::TFuture<T>& future) noexcept
{
return SafeExecute<TResultOrError<T>>([&] {
return future.GetValue();
});
return SafeExecute([&] { return future.GetValue(); });
}

template <typename T>
TResultOrError<T> ResultOrError(NThreading::TFuture<T>& future)
{
return SafeExecute<TResultOrError<T>>([&] {
return future.ExtractValue();
});
return SafeExecute([&] { return future.ExtractValue(); });
}

inline TResultOrError<void> ResultOrError(
const NThreading::TFuture<void>& future)
inline NProto::TError ResultOrError(
const NThreading::TFuture<void>& future) noexcept
{
return SafeExecute<TResultOrError<void>>([&] {
future.TryRethrow();
return NProto::TError();
});
return SafeExecute([&] { future.TryRethrow(); });
}

inline TResultOrError<void> ResultOrError(NThreading::TFuture<void>& future)
Expand Down
4 changes: 3 additions & 1 deletion cloud/storage/core/libs/common/error_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ Y_UNIT_TEST_SUITE(GetDiagnosticsErrorKindTest)

Y_UNIT_TEST(ShouldWrapExceptionInResponse)
{
auto response = SafeExecute<TTestResponse>(
TTestResponse response = SafeExecute(
[]
{
throw TServiceError(E_REJECTED) << "request cancelled";
return TTestResponse();
});

UNIT_ASSERT_VALUES_EQUAL(E_REJECTED, response.GetError().GetCode());
}

Y_UNIT_TEST(ShouldExtractResponse)
Expand Down
Loading
Loading