Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nbd: add auto map at boot #347

Merged
merged 1 commit into from
May 19, 2021
Merged

Conversation

SeanHai
Copy link
Contributor

@SeanHai SeanHai commented May 13, 2021

What problem does this PR solve?

Issue Number: close #xxx

Problem Summary:

What is changed and how it works?

What's Changed:

How it Works:

Side effects(Breaking backward compatibility? Performance regression?):

Check List

  • Relevant documentation/comments is changed or added
  • I acknowledge that all my contributions will be made under the project's license

@SeanHai SeanHai force-pushed the curve_nbd_automap branch 2 times, most recently from 5459eab to c119d4b Compare May 14, 2021 07:49
nbd/src/main.cpp Outdated
static void AddRecord() {
std::fstream fs;
// check if exist
fs.open(CURVETAB_PATH, std::fstream::in);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read-only open?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read-only open?

std::fstream::in is read-only

nbd/src/main.cpp Outdated
while (getline(ss, str, '\t')) {
lineArray.push_back(str);
}
if (lineArray.size() != 2 && lineArray.size() != 3) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under what circumstances lineArray.size() == 2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under what circumstances lineArray.size() == 2

if you only use curve-nbd map, will only image and device write in curvetab like: cbd:pool//test_test_ /dev/nbd0
then if you mkfs on the device, you need record the mount point in the curvetab manually like fstab.
reason for not using fstab is fstab run before rc or system.d at reboot.

nbd/src/main.cpp Outdated
fs.close();
return;
}
if (lineArray[0] == nbdConfig->imgname) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if the same mount point?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if the same mount point?

Not needed, only record image and device here. Mount point is ot necessary in curvetab.
The correspondence between image device and mount-point is record manually after you mount like fstab

nbd/src/main.cpp Outdated
}
fs.close();
// write new record
fs.open(CURVETAB_PATH, std::fstream::app);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a problem to mount multiple volumes at the same time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a problem to mount multiple volumes at the same time?

add flock

nbd/src/main.cpp Outdated
}

std::string lineString;
while (getline(fs, lineString)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remarks:// Check if the record exists

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remarks:// Check if the record exists

done

nbd/src/main.cpp Outdated
std::fstream fs;
fs.open(CURVETAB_PATH, std::fstream::in);
if (!fs.is_open()) {
std::cerr << "curve-nbd: open curvetab file failed.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return?

done

// check if exist
fs.open(CURVETAB_PATH, std::fstream::in);
if (!fs.is_open()) {
std::cerr << "curve-nbd: open curvetab file failed.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return

done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return with error code?

nbd/src/main.cpp Outdated

std::string lineString;
std::string newContent;
while (getline(fs, lineString)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remarks: // find record

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remarks: // find record

done.
// find record need deleted

nbd/src/main.cpp Outdated
while (getline(ss, str, '\t')) {
lineArray.push_back(str);
}
if (lineArray.size() != 2 && lineArray.size() != 3) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

done

nbd/src/main.cpp Outdated
}
fs.close();
// update record info
fs.open(CURVETAB_PATH, std::fstream::out);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

judgment : fs.is_open()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

judgment : fs.is_open()

done

@SeanHai SeanHai force-pushed the curve_nbd_automap branch 5 times, most recently from 5bfbddc to d719751 Compare May 18, 2021 06:27
do
image=$(echo $line | awk '{print $1}')
device=$(echo $line | awk '{print $2}')
mount=$(echo $line | awk '{print $3}')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

done

nbd/src/main.cpp Outdated
if (0 == flock(fd, LOCK_SH )) {
// check if the record exists
std::string lineString;
while (getline(fs, lineString)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for parse file in AddRecord and DelRecord is highly coincident, you can abstract the comon logic into one function.

nbd/src/main.cpp Outdated
}
if (lineArray.size() != 2 && lineArray.size() != 3) {
std::cerr << "curve-nbd: curvetab file content error.";
fs.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The close() is redunrant, because the file stream's destructor will close the file.

Copy link
Contributor Author

@SeanHai SeanHai May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The close() is redunrant, because the file stream's destructor will close the file.

the fs will close when goes out of scope. but this fs reused latter in this function. so I think close is needed

nbd/src/main.cpp Outdated
}
}
}
fs.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should invoke flock(fd, LOCK_UN) before fs.close().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should invoke flock(fd, LOCK_UN) before fs.close().

I think this is ok , because flock() is tied to the file descriptor, it gets released when the file is closed. so flock(fd, LOCK_UN) even not write

nbd/src/main.cpp Outdated
@@ -95,6 +97,112 @@ static void Usage() {
<< std::endl;
}

// use for record image and device info to auto map when boot
static void AddRecord() {
std::fstream fs;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use std::ifstream to replace std::fstream and open with std::fstream::in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use std::ifstream to replace std::fstream and open with std::fstream::in.

because fs reused latter in this function as std::fstream::app

nbd/src/main.cpp Outdated
}
fd = static_cast<__gnu_cxx::stdio_filebuf< char > * const >
(fs.rdbuf())->fd();
if (0 == flock(fd, LOCK_SH )) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want use the flock to sync the operation in file, i suggest use the FileLock class in nebd/src/common/file_lock.cpp.

nbd/src/main.cpp Outdated
fs.close();
flock(fd, LOCK_UN);
}
// update record info
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note there is time window in the read and write logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note there is time window in the read and write logic.

what is mean?

@SeanHai SeanHai force-pushed the curve_nbd_automap branch 3 times, most recently from 34e4652 to f89fa8d Compare May 18, 2021 13:50
@@ -61,6 +61,7 @@ namespace nbd {
#define PROCESS_NAME "curve-nbd"
#define NBD_PATH_PREFIX "/sys/block/nbd"
#define DEV_PATH_PREFIX "/dev/nbd"
#define CURVETAB_PATH "/etc/curve/curvetab"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better to named CURVETAB_LOG_PATH ?

unset recordmap[$device]
fi
done < ${confPath}
for key in ${!recordmap[@]};do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for key in ${!recordmap[@]};do -> for key in ${!recordmap[@]}; do

@Wine93
Copy link
Contributor

Wine93 commented May 19, 2021

LGTM

@SeanHai
Copy link
Contributor Author

SeanHai commented May 19, 2021

recheck

1 similar comment
@SeanHai
Copy link
Contributor Author

SeanHai commented May 19, 2021

recheck

@SeanHai
Copy link
Contributor Author

SeanHai commented May 19, 2021

recheck

@YunhuiChen YunhuiChen merged commit 0a11418 into opencurve:master May 19, 2021
ilixiaocui pushed a commit to ilixiaocui/curve that referenced this pull request Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants