Skip to content

Commit

Permalink
Add back nebd common code and remove ceph code
Browse files Browse the repository at this point in the history
Change-Id: I0665cb4ed42c86786a486a9bc6051e0d7b159101
  • Loading branch information
wu-hanqing committed Jul 14, 2020
1 parent 700f935 commit 4dc0dd0
Show file tree
Hide file tree
Showing 54 changed files with 1,729 additions and 315 deletions.
2 changes: 1 addition & 1 deletion mk-deb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ then
exit
fi
fi
set -e

#step3 创建临时目录,拷贝二进制、lib库和配置模板
mkdir build
if [ $? -ne 0 ]
Expand Down
4 changes: 2 additions & 2 deletions nbd/src/NBDWatchContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <memory>
#include "nbd/src/ImageInstance.h"
#include "nbd/src/NBDController.h"
#include "src/common/interruptible_sleeper.h"
#include "nebd/src/common/interrupt_sleep.h"

namespace curve {
namespace nbd {
Expand Down Expand Up @@ -79,7 +79,7 @@ class NBDWatchContext {
// 任务线程
std::thread watchThread_;

curve::common::InterruptibleSleeper sleeper_;
nebd::common::InterruptibleSleeper sleeper_;
};

} // namespace nbd
Expand Down
2 changes: 1 addition & 1 deletion nebd/nebd-package/DEBIAN/control
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Architecture:amd64
Installed-Size:
Maintainer: nebd-dev
Provides:
Description: nebd is a proxy between qemu and cbd/rbd
Description: nebd is a proxy between qemu and cbd
5 changes: 2 additions & 3 deletions nebd/proto/client.proto
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ message ResizeResponse {
optional string retMsg = 2;
}

// 以下proto定义,仅ceph使用
message FlushRequest {
required int32 fd = 1;
}
Expand Down Expand Up @@ -115,14 +114,14 @@ message InvalidateCacheResponse {
}

service NebdFileService {
// for ceph & curve

rpc OpenFile(OpenFileRequest) returns (OpenFileResponse);
rpc CloseFile(CloseFileRequest) returns (CloseFileResponse);
rpc Read(ReadRequest) returns (ReadResponse);
rpc Write(WriteRequest) returns (WriteResponse);
rpc Discard(DiscardRequest) returns (DiscardResponse);
rpc ResizeFile(ResizeRequest) returns (ResizeResponse);
// for ceph only

rpc Flush(FlushRequest) returns (FlushResponse);
rpc GetInfo(GetInfoRequest) returns (GetInfoResponse);
rpc InvalidateCache(InvalidateCacheRequest) returns (InvalidateCacheResponse);
Expand Down
2 changes: 0 additions & 2 deletions nebd/src/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@ cc_library(
"//external:butil",
"//external:bthread",
"//external:bvar",
"//src/common:curve_uncopy",
"//src/common:curve_common",
],
)
242 changes: 242 additions & 0 deletions nebd/src/common/configuration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
/*
* Copyright (c) 2020 NetEase Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* Project: nebd
* File Created: 2019-08-07
* Author: hzchenwei7
*/

#include "nebd/src/common/configuration.h"

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>

namespace nebd {
namespace common {

bool Configuration::LoadConfig() {
std::ifstream cFile(confFile_);

if (cFile.is_open()) {
std::string line;
while (getline(cFile, line)) {
// FIXME: may not remove middle spaces
line.erase(std::remove_if(line.begin(), line.end(), isspace),
line.end());
if (line[0] == '#' || line.empty())
continue;

int delimiterPos = line.find("=");
std::string key = line.substr(0, delimiterPos);
std::string value = line.substr(delimiterPos + 1);
config_[key] = value;
}
} else {
return false;
}

return true;
}

bool Configuration::SaveConfig() {
// 当前先只保存配置,原文件的注释等内容先忽略
// TODO(yyk): 后续考虑改成原文件格式不变,只修改配置值
std::ofstream wStream(confFile_);
if (wStream.is_open()) {
for (auto& pair : config_) {
wStream << pair.first << "=" << pair.second << std::endl;
}
wStream.close();
} else {
return false;
}
return true;
}

std::string Configuration::DumpConfig() {
// TODO(wenyu): to implement
return "";
}


std::map<std::string, std::string> Configuration::ListConfig() const {
return config_;
}

void Configuration::SetConfigPath(const std::string &path) {
confFile_ = path;
}

std::string Configuration::GetConfigPath() {
return confFile_;
}

std::string Configuration::GetStringValue(const std::string &key) {
return GetValue(key);
}

bool Configuration::GetStringValue(const std::string &key, std::string *out) {
return GetValue(key, out);
}

void Configuration::SetStringValue(const std::string &key,
const std::string &value) {
SetValue(key, value);
}

int Configuration::GetIntValue(const std::string &key, uint64_t defaultvalue) {
std::string value = GetValue(key);
return (value == "") ? defaultvalue : std::stoi(value);
}

bool Configuration::GetIntValue(const std::string &key, int *out) {
std::string res;
if (GetValue(key, &res)) {
*out = std::stoi(res);
return true;
}
return false;
}

bool Configuration::GetUInt32Value(const std::string &key, uint32_t *out) {
std::string res;
if (GetValue(key, &res)) {
*out = std::stoul(res);
return true;
}
return false;
}

bool Configuration::GetUInt64Value(const std::string &key, uint64_t *out) {
std::string res;
if (GetValue(key, &res)) {
*out = std::stoull(res);
return true;
}
return false;
}

bool Configuration::GetInt64Value(const std::string& key, int64_t* out) {
std::string res;
if (GetValue(key, &res)) {
*out = std::stoll(res);
return true;
}

return false;
}

void Configuration::SetIntValue(const std::string &key, const int value) {
SetValue(key, std::to_string(value));
}

double Configuration::GetDoubleValue(
const std::string &key,
double defaultvalue) {
std::string value = GetValue(key);
return (value == "") ? defaultvalue : std::stod(value);
}

bool Configuration::GetDoubleValue(const std::string &key, double *out) {
std::string res;
if (GetValue(key, &res)) {
*out = std::stod(res);
return true;
}
return false;
}

void Configuration::SetDoubleValue(const std::string &key, const double value) {
SetValue(key, std::to_string(value));
}


double Configuration::GetFloatValue(
const std::string &key, float defaultvalue) {
std::string value = GetValue(key);
return (value == "") ? defaultvalue : std::stof(value);
}

bool Configuration::GetFloatValue(const std::string &key, float *out) {
std::string res;
if (GetValue(key, &res)) {
*out = std::stof(res);
return true;
}
return false;
}

void Configuration::SetFloatValue(const std::string &key, const float value) {
SetValue(key, std::to_string(value));
}

bool Configuration::GetBoolValue(const std::string &key, bool defaultvalue) {
std::string svalue = config_[key];
transform(svalue.begin(), svalue.end(), svalue.begin(), ::tolower);

bool istrue = (svalue == "true") || (svalue == "yes") || (svalue == "1");
bool isfalse = (svalue == "false") || (svalue == "no") || (svalue == "0");
bool ret = istrue ? true : isfalse ? false : defaultvalue;
return ret;
}

bool Configuration::GetBoolValue(const std::string &key, bool *out) {
std::string res;
if (GetValue(key, &res)) {
transform(res.begin(), res.end(), res.begin(), ::tolower);
bool istrue = (res == "true") || (res == "yes") || (res == "1");
bool isfalse = (res == "false") || (res == "no") || (res == "0");
if (istrue) {
*out = true;
return true;
}
if (isfalse) {
*out = false;
return true;
}
return false;
}

return false;
}


void Configuration::SetBoolValue(const std::string &key, const bool value) {
SetValue(key, std::to_string(value));
}

std::string Configuration::GetValue(const std::string &key) {
return config_[key];
}

bool Configuration::GetValue(const std::string &key, std::string *out) {
if (config_.find(key) != config_.end()) {
*out = config_[key];
return true;
}

return false;
}

void Configuration::SetValue(const std::string &key, const std::string &value) {
config_[key] = value;
}

} // namespace common
} // namespace nebd
Loading

0 comments on commit 4dc0dd0

Please sign in to comment.