Slide 1

Slide 1 text

BARREL BUILD A P2P DOCUMENT
 ORIENTED DATABASE https://barrel-db.org Erlang Factory San Francisco 2016

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

VISION AND CONCEPT

Slide 6

Slide 6 text

local database mobile sensor "cloud" database local database mobile DATA IS MOBILE

Slide 7

Slide 7 text

share discover replicate PEER TO PEER (P2P)

Slide 8

Slide 8 text

▸ Local first ▸ Put/Match the data next to you ▸ Query Locally ▸ Replicate a view of the data you need

Slide 9

Slide 9 text

WHAT IS BARREL

Slide 10

Slide 10 text

WHAT IS BARREL ▸ a document database ▸ document are JSON with attachments and links ▸ changes feed for document and indexes ▸ replication between any nodes in both way ▸ views (~ map) ▸ HTTP 1.1/2 API

Slide 11

Slide 11 text

▸ DATA: not just blobs ▸ Replicated APPs ▸ Couchapps but extended and revisited REPLICATED APPS

Slide 12

Slide 12 text

DECONSTRUCT

Slide 13

Slide 13 text

APPEND ONLY & MVCC Doc1 Doc2 Doc3 Doc4 Doc5 Doc6 Doc7 Btree Node Btree Node Document revision Block btree node invalid data version

Slide 14

Slide 14 text

▸ Create a new file to remove the fragmentation ▸ A race between copy and the addition of new data ▸ Require at least twice of the storage THE COMPACTION ISSUE

Slide 15

Slide 15 text

DOCUMENT STORAGE ID 1 METADATA 1 ID 2 METADATA 2 ID 3 METADATA 3 ID-Index SEQ 1 METADATA 1 SEQ 2 METADATA 2 SEQ 3 METADATA 3 Seq-Index Btree Node Btree Node Doc Indexed document DB file

Slide 16

Slide 16 text

▸ 2 indexes (btree): by sequence, by id ▸ 1 index for local documents without conflict handling ▸ A revision tree is stored in indexes pointed to the revision offset ▸ The revision is stored in the file separately HOW ARE STORED DOCUMENTS

Slide 17

Slide 17 text

▸ Reverse index (map) ▸ Index using a function ▸ Function in javascripts, erlang, .. ▸ Incremental index ▸ Retrieves changes (aka view changes) ▸ View are regrouped by groups (1 db file/group) VIEWS

Slide 18

Slide 18 text

VIEW STORAGE DOCID View 1 KEY 1 SEQ 1 ADD KEY 2 SEQ 2 DEL View 2 KEY 1 SEQ 1 ADD Log-Index [KEY 1, DOCID] [VALUE, DOCREV, SEQ] [KEY 2, DOCID] [del, DOCREV, SEQ] [KEY 3, DOCID 2] [VALUE, DOCREV, SEQ] Key-Index [SEQ 1, KEY] [VALUE, DOCREV, SEQ] [SEQ 2, KEY 2] [del, DOCREV, SEQ] [SEQ 3, KEY]3 [VALUE, DOCREV, SEQ] SEQ-Index view

Slide 19

Slide 19 text

REVISION TREE

Slide 20

Slide 20 text

BUILT IN ERLANG /> <

Slide 21

Slide 21 text

▸ Write is slow ▸ Read should not being blocked by writes ▸ No shared memory ▸ No atomic integer trick ▸ Only actors and message passing ▸ Operations on a doc are atomic CHALLENGES

Slide 22

Slide 22 text

READ/WRITE OPERATIONS DB STATE READER READER writer writer update share state

Slide 23

Slide 23 text

▸ LRU to cache blocks
 https://github.com/barrel-db/erlang-lru ▸ 1 File process, Operations are limited ▸ DB users are linked to the database process ▸ Optional Write buffer to reduce the latency ▸ Optional wal READ/WRITE OPERATIONS

Slide 24

Slide 24 text

▸ STORE SEGMENTS of data for compaction ▸ IO is "relatively" slow in erlang ▸ USE a “native KV store” as a nif. SPEEDUPS

Slide 25

Slide 25 text

INDEX OPERATIONS View Group READER READER change reader indexer update share state send /collect changes DB get changes

Slide 26

Slide 26 text

▸ Credit Flow Based ▸ The View group keep the state ▸ View group is created on demande ▸ kept open until it has readers ▸ Indexer ask for updates ▸ Read functions (Map functions) are processed in // INDEX OPERATIONS

Slide 27

Slide 27 text

▸ Added 2 features: ▸ MOVE: move doc(s) to another node or database (like copy but with delet ▸ User hooks functions (run in background) using hooks:
 https://github.com/barrel-db/hooks ▸ Partition on demand ▸ Decision depends on the application needs NEW FUNCTIONS

Slide 28

Slide 28 text

CHANGES HANDLER subscriber change dispatcher broadcast changes DB

Slide 29

Slide 29 text

▸ Use the sequence index ▸ changes load balancing ▸ consumer subscribe on patterns (delete, update, …) ▸ Create changes Load Balancer on demand ▸ Allows remote nodes to subscribe to a queue ▸ Based on primer (release on March 2016) CHANGES EVENTS

Slide 30

Slide 30 text

▸ Use the sequence index ▸ changes load balancing ▸ consumer subscribe on patterns (delete, update, …) ▸ Create changes Load Balancer on demand ▸ Allows remote nodes to subscribe to a queue ▸ Based on primer (release on March 2016) CHANGES EVENTS

Slide 31

Slide 31 text

▸ inherited the HTTP api in mochiweb ▸ small changes to makes the server more resilient ▸ chatterbox ▸ wip in cowboy. ▸ yaws ? HTTP API

Slide 32

Slide 32 text

P2P

Slide 33

Slide 33 text

▸ Over HTTP ▸ Replication is the core ▸ Each nodes can replicate each others ▸ PUSH/PULL ▸ Chained replication P2P

Slide 34

Slide 34 text

▸ Based ont the change feed ▸ fetch the revisions and their attachments
 not present on the node ▸ continuous or not ▸ try to collect multiple docs at once ▸ use hackney:
 http://github.com/benoitc/hackney ▸ Use a Flow-based pattern instead of a classic pool REPLICATION

Slide 35

Slide 35 text

REPLICATION OPERATIONS replication worker replication proxy fetch docs DB SOURCE get changes DB TARGET notify changes push docs

Slide 36

Slide 36 text

▸ Replication state is stored a least on one node ▸ checkpoints ▸ get the revisions not actually stored on the nodes (“_rev_diffs” ▸ the replication proxy maintains routes ▸ build replication chains, by replicating status REPLICATION

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

HTTPS://BARREL-DB.ORG Barrel HTTP://ENKIM.EU Enki Multimedia