Skip to content

Commit

Permalink
nbd: unmap wait process exit
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-hanqing committed Jan 19, 2021
1 parent 4747da1 commit f4bc7c2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
48 changes: 47 additions & 1 deletion nbd/src/NBDTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,26 @@ int NBDTool::Connect(NBDConfig *cfg) {
}

int NBDTool::Disconnect(const std::string& devpath) {
pid_t devpid = -1;
std::vector<DeviceInfo> devices;

int ret = List(&devices);
for (const auto& device : devices) {
if (device.config.devpath == devpath) {
devpid = device.pid;
break;
}
}

NBDControllerPtr nbdCtrl = GetController(false);
return nbdCtrl->DisconnectByPath(devpath);
ret = nbdCtrl->DisconnectByPath(devpath);
if (ret != 0) {
return ret;
}

ret = WaitForTerminate(devpath, devpid, 5000);

return 0;
}

int NBDTool::List(std::vector<DeviceInfo>* infos) {
Expand All @@ -137,6 +155,8 @@ void NBDTool::RunServerUntilQuit() {
} else {
ctrl->RunUntilQuit();
}

nbdWatchCtx_->StopWatch();
}

ImagePtr g_test_image = nullptr;
Expand All @@ -150,5 +170,31 @@ ImagePtr NBDTool::GenerateImage(const std::string& imageName) {
return result;
}

int NBDTool::WaitForTerminate(const std::string& devpath, pid_t pid,
uint64_t totalRetryMs) {
if (pid < 0) {
return 0;
}

// TODO(wuhanqing): make this to NBDConfig
int times = 20;
uint64_t sleepMs = totalRetryMs / times;

while (times-- > 0) {
if (kill(pid, 0) == -1) {
if (errno == ESRCH) {
return 0;
}
std::cerr << "curve-nbd test device failed, dev: " << devpath
<< ", err = " << cpp_strerror(-errno) << std::endl;
return -errno;
}

std::this_thread::sleep_for(std::chrono::milliseconds(sleepMs));
}

return -ETIMEDOUT;
}

} // namespace nbd
} // namespace curve
4 changes: 4 additions & 0 deletions nbd/src/NBDTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class NBDTool {
// 生成image instance
ImagePtr GenerateImage(const std::string& imageName);

// wait curve-nbd process to exit
int WaitForTerminate(const std::string& devpath, pid_t pid,
uint64_t totalRetryMs);

private:
class NBDSocketPair {
public:
Expand Down
1 change: 1 addition & 0 deletions robot/Resources/keywords/test_curve_stability_nbd.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ def test_snapshot_all(vol_uuid):
test_clone_iovol_consistency(lazy)
# test_clone_vol_same_uuid(lazy)
test_recover_snapshot(lazy)
config.snapshot_thrash.nbd_unmap()
config.snapshot_thrash.nbd_delete()
return "finally"

Expand Down

0 comments on commit f4bc7c2

Please sign in to comment.