pht: fix insert always to root bug

This makes the pht node recover all indexed data on a DHT node (maximum
MAX_NODE_ENTRY_COUNT per node) so that the pht node can be aware if it has to
split its data in the leaf.
This commit is contained in:
Simon Désaulniers authored and Adrien Béraud committed 2016-09-01 18:02:23 -04:00
1 parent c4928553ba
commit 5a239cddf7
2 files changed
+16 -11

No files matched your search

+6 -2
View File
@@ -131,7 +131,11 @@ struct IndexEntry : public dht::Value::Serializable<IndexEntry> {
class Pht {
static constexpr const char* INDEX_PREFIX = "index.pht.";
static constexpr const size_t MAX_NODE_ENTRY_COUNT {128};
/* This is the maximum number of entries per node. This parameter is
* critical and influences the traffic alot during a lookup operation.
*/
static constexpr const size_t MAX_NODE_ENTRY_COUNT {16};
public:
using Key = std::map<std::string, Prefix>;
@@ -180,7 +184,7 @@ private:
void lookupStep(Prefix k, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
std::shared_ptr<std::vector<std::shared_ptr<Value>>> vals,
LookupCallback cb, Dht::DoneCallbackSimple done_cb,
std::shared_ptr<unsigned> max_common_prefix_len);
std::shared_ptr<unsigned> max_common_prefix_len, bool all_values = false);
/**
* Updates the canary token on the node responsible for the specified
+10 -9
View File
@@ -9,7 +9,7 @@ const ValueType IndexEntry::TYPE = ValueType::USER_DATA;
void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
std::shared_ptr<std::vector<std::shared_ptr<Value>>> vals,
LookupCallback cb, Dht::DoneCallbackSimple done_cb,
std::shared_ptr<unsigned> max_common_prefix_len)
std::shared_ptr<unsigned> max_common_prefix_len, bool all_values)
{
struct node_lookup_result {
bool done {false};
@@ -19,7 +19,7 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
auto mid = (*lo + *hi)/2;
auto first_res = std::make_shared<node_lookup_result>();
auto second_res = std::make_shared<node_lookup_result>();
auto on_done = [=](bool ok){
auto on_done = [=](bool ok) {
bool is_leaf = first_res->is_pht and not second_res->is_pht;
if (not ok) {
if (done_cb)
@@ -27,20 +27,21 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
}
else if (is_leaf or *lo > *hi) {
// leaf node
if (cb)
if (cb) {
if (vals->size() == 0 and max_common_prefix_len and mid > 0) {
auto p_ = (p.getPrefix(mid)).getSibling().getFullSize();
*lo = mid;
*hi = p_.size_;
lookupStep(p_, lo, hi, vals, cb, done_cb, max_common_prefix_len);
lookupStep(p_, lo, hi, vals, cb, done_cb, max_common_prefix_len, all_values);
}
cb(*vals, p.getPrefix(mid));
}
if (done_cb)
done_cb(true);
} else if (first_res->is_pht) {
// internal node
*lo = mid+1;
lookupStep(p, lo, hi, vals, cb, done_cb, max_common_prefix_len);
lookupStep(p, lo, hi, vals, cb, done_cb, max_common_prefix_len, all_values);
} else {
// first get failed before second.
if (done_cb)
@@ -77,7 +78,7 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
}
}
}
else if (entry.prefix == p.content_)
else if (all_values or entry.prefix == p.content_)
add_value(false);
}
return true;
@@ -95,7 +96,7 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
if (not first_res->is_pht) {
// Not a PHT node.
*hi = mid-1;
lookupStep(p, lo, hi, vals, cb, done_cb, max_common_prefix_len);
lookupStep(p, lo, hi, vals, cb, done_cb, max_common_prefix_len, all_values);
} else {
first_res->done = true;
if (second_res->done)
@@ -172,7 +173,7 @@ void Pht::insert(Key k, Value v, Dht::DoneCallbackSimple done_cb) {
if (done_cb)
done_cb(false);
} else {
if (vals->size() > MAX_NODE_ENTRY_COUNT)
if (vals->size() >= MAX_NODE_ENTRY_COUNT)
*final_prefix = kp.getPrefix(final_prefix->size_+1);
IndexEntry entry;
@@ -183,7 +184,7 @@ void Pht::insert(Key k, Value v, Dht::DoneCallbackSimple done_cb) {
updateCanary(*final_prefix);
dht_->put(final_prefix->hash(), std::move(entry), done_cb);
}
}, nullptr
}, nullptr, true
);
}