ipfs / ipfs/kubo

Storing bitswap ledgers on disk

Open
#215 10 comments 0 reactions 0 assignees View on GitHub
kind/enhancement need/analysis topic/bitswap
Dominant language
Go
Stars
17.1k
Forks
3.2k
Avg merge
3d 18h
Merged PRs (30d)
11

Description

Presently, ledgers are only held in memory. Let this issue track implementation of persisted ledgers.

Ledgers could be stored to datastore using a unique key:

``` go
func LedgerKey(partner peer.ID) ds.Key {
k := b58.Encode(partner)
return ds.Key(fmt.Sprintf("/bitswap/ledger/%s", k)
}
```

and data could be stored as a []byte using the proto described here:

``` proto
message Ledger {
enum Strategy {
NICE = 0;
STANDARD = 1; // as described in IPFS Draft 3 (name DRAFT_3?)
}
optional uint64 bytes_sent = 1 [default = 0];
optional uint64 bytes_received = 2 [default = 0];
optional uint64 first_exchange_unix_time = 3; // default?
optional uint64 last_exchange_unix_time = 4; // default?
optional uint64 exchange_count = 5 [default = 0];
optional Strategy strategy = 6 [default = STANDARD];
}

```

**Two options**

**a) keep ledgers in memory (introduces book-keeping/consistency)**

Can be implemented using an async actor that selects on a timer and iterates over map of in-memory ledgers, writing entries to the datastore.

On the upside, bitswap transactions are faster. Downside: data loss in the event of a crash. Slightly more complexity in the implementation.

**b) fetch ledgers from disk**

On the upside, very simple Get and Put operations.

Downside: more costly.

With leveldb sync disabled, pay the cost of getting the data to the operating system buffer cache. I don't have figures off the top of my head, do we pay the price of a syscall?

With sync option enabled, mean latency could be in 5-20ms range. This prices feels a bit high for a potentially-hot code path.

NB: cost may be small compared to long-distance RTTs, but may be noticeable for communication within a single datacenter.

NB2: Many bitswap operations will already require multiple datastore `Get` operations. In those cases, this constitutes a mere incremental increase.

Thoughts? @whyrusleeping @jbenet

References:

leveldb sync
https://github.com/google/leveldb/blob/master/include/leveldb/options.h#L170

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.