提交 6c24a369 authored 作者: blu's avatar blu

refactor of delta config

上级 56bc22d2
......@@ -124,108 +124,111 @@ private:
else {
json &data = newConfig["data"];
// for clusters
for(auto &[k, v]: data.items()) {
// TODO: confirm overwrite, take snapshot
if(this->configMap.count(k) != 0) {
spdlog::warn("evcloudsvc TODO: key {} already exist, take snapshot for safety", k);
}else{
spdlog::info("evcloudsvc new config for cluster {}", k);
}
// this is one evmgr
if(v.count("sn") == 0||v["sn"] != k) {
ret["code"] = 2;
string msg = fmt::format("evcloudsvc invalid value for cluster mgr with sn {} but key: {}", k, string(v["sn"]));
ret["msg"] = msg;
spdlog::error(msg);
break;
}
else {
// find all modules
if(v.count("ipcs") == 0||v["ipcs"].size() == 0) {
spdlog::error("invalid ipcs in config body");
ret["code"] = 3;
break;
}
else {
json &ipcs = v["ipcs"];
for(auto &ipc : ipcs) {
if(ipc.count("modules") == 0||ipc["modules"].size() == 0) {
spdlog::error("invalid modules in ipcs config body");
ret["code"] = 4;
break;
}
else {
json &modules = ipc["modules"];
// else {
// // calc diff
for(auto &[mn, ma]: modules.items()) {
for(auto &m:ma) {
if(m.count("sn") != 0 && m["sn"].size() != 0) {
string modKey;
string sn = m["sn"];
//ml
if(mn == "evml" && m.count("type") != 0 && m["type"].size() != 0) {
modKey = sn +":evml:" + m["type"].get<string>();
}
else {
modKey = sn + ":" + mn;
}
// modules
if(this->configMap["sn2mods"].count(sn) == 0) {
this->configMap["sn2mods"][sn] = json();
}
// check exist
bool hasModKey =false;
for(auto &modKey_:this->configMap["sn2mods"][sn]) {
if(modKey_ == modKey) {
hasModKey = true;
break;
}
}
// // find all modules
// if(v.count("ipcs") == 0||v["ipcs"].size() == 0) {
// spdlog::error("invalid ipcs in config body");
// ret["code"] = 3;
// break;
// }
// else {
// json &ipcs = v["ipcs"];
// for(auto &ipc : ipcs) {
// if(ipc.count("modules") == 0||ipc["modules"].size() == 0) {
// spdlog::error("invalid modules in ipcs config body");
// ret["code"] = 4;
// break;
// }
// else {
// json &modules = ipc["modules"];
// for(auto &[mn, ma]: modules.items()) {
// for(auto &m:ma) {
// if(m.count("sn") != 0 && m["sn"].size() != 0) {
// string modKey;
// string sn = m["sn"];
// //ml
// if(mn == "evml" && m.count("type") != 0 && m["type"].size() != 0) {
// modKey = sn +":evml:" + m["type"].get<string>();
// }
// else {
// modKey = sn + ":" + mn;
// }
if(hasModKey) {
//nop
}
else {
this->configMap["sn2mods"][sn].push_back(modKey);
}
// // modules
// if(this->configMap["sn2mods"].count(sn) == 0) {
// this->configMap["sn2mods"][sn] = json();
// }
// // check exist
// bool hasModKey =false;
// for(auto &modKey_:this->configMap["sn2mods"][sn]) {
// if(modKey_ == modKey) {
// hasModKey = true;
// break;
// }
// }
// modkey -> sn_of_evmgr
this->configMap["mod2mgr"][modKey] = k;
}
else {
string msg = "evcloudsvc invalid config: " + data.dump();;
ret["code"] = -1;
ret["msg"] = msg;
spdlog::error(msg);
break;
}
}
} // for modules
// if(hasModKey) {
// //nop
// }
// else {
// this->configMap["sn2mods"][sn].push_back(modKey);
// }
if(ret["code"] != 0) {
break;
}
} // for ipc
}
}
// // modkey -> sn_of_evmgr
// this->configMap["mod2mgr"][modKey] = k;
// }
// else {
// string msg = "evcloudsvc invalid config: " + data.dump();;
// ret["code"] = -1;
// ret["msg"] = msg;
// spdlog::error(msg);
// break;
// }
// }
// } // for modules
if(ret["code"] != 0) {
break;
}
}
// update evmgr config
this->configMap[k] = k;
// if(ret["code"] != 0) {
// break;
// }
// } // for ipc
// }
// }
//save
iret = LVDB::setLocalConfig(v, k);
if(iret < 0) {
string msg = "failed to save config " + k + " -> " + v.dump();
spdlog::error(msg);
ret["code"] = iret;
ret["msg"] = msg;
// if(ret["code"] != 0) {
// break;
// }
// }
// // update evmgr config
// this->configMap[k] = k;
// //save
// iret = LVDB::setLocalConfig(v, k);
// if(iret < 0) {
// string msg = "failed to save config " + k + " -> " + v.dump();
// spdlog::error(msg);
// ret["code"] = iret;
// ret["msg"] = msg;
// }
// calc cluster cfg diff
if((this->peerData["config"].count(k) == 0 || this->peerData["config"][k].size() == 0) && v.size() == 0){
spdlog::warn("evcloudsvc ignore empty cluster config for {}", k);
continue;
}
// update in memory peerData
if(this->peerData["config"].count(k) != 0) {
json diff = json::diff(this->peerData["config"][k], v);
......@@ -417,6 +420,7 @@ private:
return ret;
}
/// refer to evcloudsvc.yaml
json getConfigForDevice(string sn)
{
json ret;
......
configmap:
- example: |-
```
{
"NMXH73Y2": "NMXH73Y2",
"code": 0,
"mod2mgr": {
"NMXH73Y2:evml:motion": "NMXH73Y2",
"NMXH73Y2:evpuller": "NMXH73Y2",
"NMXH73Y2:evpusher": "NMXH73Y2",
"NMXH73Y2:evslicer": "NMXH73Y2"
},
"sn2mods": {
"NMXH73Y2": [
"NMXH73Y2:evml:motion",
"NMXH73Y2:evpuller",
"NMXH73Y2:evpusher",
"NMXH73Y2:evslicer"
]
}
}
```
structure:
- mgrSn:
- description:
key: mgrSn
value: mgrSn
- sn2mods:
- description: device with sn that runs mods.
key: sn
value:
- key: sn:modName
value: 1
- mod2mgr:
- description: mod connects to mgr
key: modid
value: mgrSn
api:
- get:
- description: get all cluster configurations that has mod runs on this dev
steps:
- 1: get all models runs on sn
2: for each mod, get mgrSn into set S
3: merge cluster configs of S to data
4: return data for sn
add/update:
- description: add, patch
steps:
- 1: for each cluster config of sn in data
- 2: get diff, update sn2mods & md2mgr, mgrSn and send actions to edge
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论