debugging <.< and splitting commands to reduce size

This commit is contained in:
Green Sky 2022-12-20 00:21:15 +01:00
parent e961b8aec3
commit a0c87e5fc5
No known key found for this signature in database
2 changed files with 27 additions and 10 deletions

View File

@ -71,7 +71,7 @@ struct List {
std::map<AgentType, uint64_t> last_seen_seq; std::map<AgentType, uint64_t> last_seen_seq;
std::optional<size_t> findIdx(const ListID& list_id) const { std::optional<size_t> findIdx(const ListID& list_id) const {
verify(); //verify(); // too expensive
for (size_t i = 0; i < list.size(); i++) { for (size_t i = 0; i < list.size(); i++) {
if (list[i].id == list_id) { if (list[i].id == list_id) {
return i; return i;

View File

@ -80,7 +80,7 @@ namespace std {
template<typename T> template<typename T>
static void from_json(const nlohmann::json& nlohmann_json_j, std::optional<T>& nlohmann_json_t) { static void from_json(const nlohmann::json& nlohmann_json_j, std::optional<T>& nlohmann_json_t) {
if (nlohmann_json_j != nullptr) { if (!nlohmann_json_j.is_null()) {
nlohmann_json_t = static_cast<T>(nlohmann_json_j); nlohmann_json_t = static_cast<T>(nlohmann_json_j);
} else { } else {
nlohmann_json_t = std::nullopt; nlohmann_json_t = std::nullopt;
@ -117,14 +117,23 @@ namespace std {
} else if (std::holds_alternative<Doc::ListType::OpDel>(nlohmann_json_t)) { } else if (std::holds_alternative<Doc::ListType::OpDel>(nlohmann_json_t)) {
nlohmann_json_j["t"] = "del"; nlohmann_json_j["t"] = "del";
nlohmann_json_j["d"] = std::get<Doc::ListType::OpDel>(nlohmann_json_t); nlohmann_json_j["d"] = std::get<Doc::ListType::OpDel>(nlohmann_json_t);
} else {
assert(false && "missing op type");
} }
} }
static void from_json(const nlohmann::json& nlohmann_json_j, Doc::Op& nlohmann_json_t) { static void from_json(const nlohmann::json& nlohmann_json_j, Doc::Op& nlohmann_json_t) {
if (nlohmann_json_j.is_null()) {
std::cerr << "got null j\n";
return;
}
if (nlohmann_json_j.at("t") == "add") { if (nlohmann_json_j.at("t") == "add") {
nlohmann_json_j.at("d").get_to(std::get<Doc::ListType::OpAdd>(nlohmann_json_t)); nlohmann_json_t = static_cast<Doc::ListType::OpAdd>(nlohmann_json_j.at("d"));
} else if (nlohmann_json_j.at("t") == "del") { } else if (nlohmann_json_j.at("t") == "del") {
nlohmann_json_j.at("d").get_to(std::get<Doc::ListType::OpDel>(nlohmann_json_t)); nlohmann_json_t = static_cast<Doc::ListType::OpDel>(nlohmann_json_j.at("d"));
} else {
assert(false && "missing op type");
} }
} }
} // namespace std } // namespace std
@ -506,6 +515,7 @@ void toxThread(SharedContext* ctx) {
f_pkg.seq = ctx->command_frontier.at(agent_local); f_pkg.seq = ctx->command_frontier.at(agent_local);
c_pkg = ctx->command_lists[agent_local][f_pkg.seq]; c_pkg = ctx->command_lists[agent_local][f_pkg.seq];
assert(!c_pkg.ops.empty());
} }
{ // gossip { // gossip
@ -857,16 +867,23 @@ int main(void) {
assert(ctx.command_lists.size() == ctx.command_frontier.size()); assert(ctx.command_lists.size() == ctx.command_frontier.size());
auto& local_command_list = ctx.command_lists[ctx.agent]; auto& local_command_list = ctx.command_lists[ctx.agent];
uint64_t seq {0}; uint64_t seq {0};
if (ctx.command_frontier.count(ctx.agent)) { // get last own seq if (ctx.command_frontier.count(ctx.agent)) { // get last own seq
seq = ctx.command_frontier[ctx.agent] + 1; seq = ctx.command_frontier[ctx.agent] + 1;
} }
local_command_list.emplace(seq, Command{ const size_t max_ops {5}; // limit ops per command so we can fit them into packets
ctx.agent, for (size_t i = 0; i < ops.size(); i+=max_ops, seq++) {
seq, std::vector<Doc::Op> tmp_ops {ops.cbegin()+i, ops.cbegin()+i+1};
ops assert(!tmp_ops.empty());
});
ctx.command_frontier[ctx.agent] = seq; local_command_list.emplace(seq, Command{
ctx.agent,
seq,
tmp_ops
});
ctx.command_frontier[ctx.agent] = seq;
}
} }
ctx.should_gossip_local.store(true); ctx.should_gossip_local.store(true);
} else { } else {