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

Add system disk use flag #371

Merged
merged 3 commits into from
May 20, 2021
Merged
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
11 changes: 8 additions & 3 deletions src/app/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

bool offline_chain_mode = false;
bool g_upgrade_flag = false;
bool g_use_sys_disk = false;
extern std::string config_file_path;
crust::Log *p_log = crust::Log::get_instance();

Expand Down Expand Up @@ -42,6 +43,10 @@ int SGX_CDECL main(int argc, char *argv[])
\nSWorker version: %s\n", VERSION, SWORKER_VERSION);
return 0;
}
else if (strcmp(argv[i], "--use-sysdisk") == 0)
{
g_use_sys_disk = true;
}
else if (strcmp(argv[i], "--offline") == 0)
{
offline_chain_mode = true;
Expand All @@ -60,8 +65,7 @@ int SGX_CDECL main(int argc, char *argv[])
// Check if configure path has been indicated
if (!is_set_config)
{
p_log->err("Please indicate configure file path!\n");
goto show_help;
p_log->info("-c argument is not provided, default config path: %s.json will be used.\n", config_file_path.c_str());
}

// Main branch
Expand All @@ -75,8 +79,9 @@ int SGX_CDECL main(int argc, char *argv[])
printf(" option: \n");
printf(" -h, --help: help information. \n");
printf(" -c, --config: required, indicate configure file path, followed by configure file path. Like: '--config Config.json'\n");
printf(" If no file provided, default path is etc/Config.json. \n");
printf(" If no file provided, default path is %s. \n", config_file_path.c_str());
printf(" -v, --version: show whole version and sworker version. \n");
printf(" --use-sysdisk: use system disk as data disk(be careful to using this argument leading to unexpected error). \n");
printf(" --offline: add this flag, program will not interact with the chain. \n");
printf(" --debug: add this flag, program will output debug logs. \n");
printf(" --upgrade: used to upgrade.\n");
Expand Down
68 changes: 38 additions & 30 deletions src/app/config/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ using namespace std;

Config *Config::config = NULL;
crust::Log *p_log = crust::Log::get_instance();
std::string config_file_path;
std::string config_file_path = CRUST_INST_DIR "/etc/Config.json";

extern bool offline_chain_mode;
extern bool g_use_sys_disk;

/**
* @desination: Single instance class function to get instance
Expand Down Expand Up @@ -69,7 +70,7 @@ bool Config::init(std::string path)
for (int i = 0; i < data_paths.size(); i++)
{
std::string d_path = data_paths[i].ToString();
this->data_paths.insert(d_path);
this->data_paths.push_back(d_path);
if (CRUST_SUCCESS != (crust_status = create_directory(d_path)))
{
p_log->err("Create path:'%s' failed! Error code:%lx\n", d_path.c_str(), crust_status);
Expand Down Expand Up @@ -155,17 +156,7 @@ void Config::show(void)
printf(" 'base path' : '%s',\n", this->base_path.c_str());
printf(" 'db path' : '%s',\n", this->db_path.c_str());
printf(" 'data path' : [\n");
std::vector<std::string> data_paths;
std::set<std::string> tmp_paths = this->get_data_paths();
data_paths.insert(data_paths.end(), tmp_paths.begin(), tmp_paths.end());
std::sort(data_paths.begin(), data_paths.end(), [](std::string s1, std::string s2) {
if (s1.length() != s2.length())
{
return s1.length() < s2.length();
}
return s1.compare(s2) < 0;
});
for (auto it = data_paths.begin(); it != data_paths.end(); )
for (auto it = this->data_paths.begin(); it != this->data_paths.end(); )
{
printf(" \"%s\"", (*it).c_str());
++it == data_paths.end() ? printf("\n") : printf(",\n");
Expand Down Expand Up @@ -211,7 +202,7 @@ std::string Config::get_config_path()
bool Config::unique_paths()
{
// Get system disk fsid
if (!offline_chain_mode)
if (!offline_chain_mode && !g_use_sys_disk)
{
struct statfs sys_st;
if (statfs(this->base_path.c_str(), &sys_st) != -1)
Expand All @@ -220,37 +211,47 @@ bool Config::unique_paths()
}
}

std::map<std::string, std::string> sid_m;
std::set<std::string> data_paths = this->data_paths;
std::set<std::string> sids_s;
std::set<std::string> data_paths(this->data_paths.begin(), this->data_paths.end());
this->data_paths.clear();
for (auto path : data_paths)
{
struct statfs st;
if (statfs(path.c_str(), &st) != -1)
{
std::string fsid = hexstring_safe(&st.f_fsid, sizeof(st.f_fsid));
// Compare to check if current disk is system disk
if (this->sys_fsid.compare(fsid) == 0)
{
this->data_paths.erase(path);
}
else
if (this->sys_fsid.compare(fsid) != 0)
{
// Remove duplicated disk
if (sid_m.find(fsid) == sid_m.end())
if (sids_s.find(fsid) == sids_s.end())
{
sid_m[fsid] = path;
}
else
{
this->data_paths.erase(path);
this->data_paths.push_back(path);
}
}
}
}
this->sort_data_paths();

return 0 != this->data_paths.size();
}

/**
* @description: Sort data paths
*/
void Config::sort_data_paths()
{
this->data_paths_mutex.lock();
std::sort(this->data_paths.begin(), this->data_paths.end(), [](std::string s1, std::string s2) {
if (s1.length() != s2.length())
{
return s1.size() < s2.size();
}
return s1.compare(s2) < 0;
});
this->data_paths_mutex.unlock();
}

/**
* @description: Check if given data path is valid
* @param path -> Reference to given data path
Expand Down Expand Up @@ -323,7 +324,7 @@ bool Config::is_valid_or_normal_disk(const std::string &path)
* @description: Get data paths
* @return: Data paths
*/
std::set<std::string> Config::get_data_paths()
std::vector<std::string> Config::get_data_paths()
{
SafeLock sl(this->data_paths_mutex);
sl.lock();
Expand Down Expand Up @@ -363,21 +364,28 @@ bool Config::config_file_add_data_paths(const json::JSON &paths)
}
SafeLock sl(this->data_paths_mutex);
sl.lock();
std::set<std::string> paths_s(this->data_paths.begin(), this->data_paths.end());
bool is_valid = false;
for (auto path : paths.ArrayRange())
{
std::string pstr = path.ToString();
if (this->is_valid_data_path(pstr, false))
{
config_json["data_path"].append(path);
this->data_paths.insert(pstr);
is_valid = true;
if (paths_s.find(pstr) == paths_s.end())
{
this->data_paths.push_back(pstr);
paths_s.insert(pstr);
is_valid = true;
}
}
}
sl.unlock();
if (! is_valid)
{
return false;
}
this->sort_data_paths();
std::string config_str = config_json.dump();
replace(config_str, "\\\\", "\\");
crust_status = save_file(config_file_path.c_str(), reinterpret_cast<const uint8_t *>(config_str.c_str()), config_str.size());
Expand Down
5 changes: 3 additions & 2 deletions src/app/config/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,17 @@ class Config
bool unique_paths();
bool is_valid_or_normal_disk(const std::string &path);
bool is_valid_data_path(const std::string &path, bool lock = true);
std::set<std::string> get_data_paths();
std::vector<std::string> get_data_paths();
bool config_file_add_data_paths(const json::JSON &paths);

private:
Config() {}
Config(const Config &);
bool init(std::string path);
void sort_data_paths();
Config& operator = (const Config &);
std::string sys_fsid;
std::set<std::string> data_paths; /* data path */
std::vector<std::string> data_paths; /* data path */
std::mutex data_paths_mutex;
};

Expand Down
21 changes: 12 additions & 9 deletions src/app/ocalls/StoreOCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,20 @@ crust_status_t ocall_save_ipfs_block(const char *path, const uint8_t *data, size

// Choose disk
std::string cid(path, CID_LENGTH);
uint32_t index = 0;
read_rand(reinterpret_cast<uint8_t *>(&index), sizeof(index));
index = index % FILE_DISK_LIMIT;
uint32_t ci = index + 2; // Current index, jump the first "Qm"
uint32_t start_index = 0;
read_rand(reinterpret_cast<uint8_t *>(&start_index), sizeof(start_index));
start_index = start_index % FILE_DISK_LIMIT;
uint32_t end_index = (CID_LENGTH - 2) / 2;
uint32_t ci = start_index; // Current index
uint32_t oi = ci; // Origin index
uint32_t loop_num = FILE_DISK_LIMIT;
const char *p_index_path = path + 2;
do
{
uint32_t di = path[ci] % disk_json.size();
uint32_t ii = ci * 2;
uint32_t di = (p_index_path[ii] + p_index_path[ii+1]) % disk_json.size();
size_t reserved = disk_json[di][WL_DISK_AVAILABLE].ToInt() * 1024 * 1024 * 1024;
if (reserved > data_size)
if (reserved > data_size * 4)
{
std::string disk_path = disk_json[di][WL_DISK_PATH].ToString();
std::string uuid_str = EnclaveData::get_instance()->get_uuid(disk_path);
Expand All @@ -92,10 +95,10 @@ crust_status_t ocall_save_ipfs_block(const char *path, const uint8_t *data, size
ci = (ci + 1) % loop_num;
if (ci == oi)
{
ci = FILE_DISK_LIMIT + 2;
loop_num = CID_LENGTH + 1;
ci = FILE_DISK_LIMIT;
loop_num = end_index + 1;
}
} while (ci != CID_LENGTH);
} while (ci < end_index);

return CRUST_STORAGE_NO_ENOUGH_SPACE;
}
Expand Down