Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does orbitdb sync? #68

Open
subhasisbanik opened this issue Oct 9, 2018 · 5 comments
Open

How does orbitdb sync? #68

subhasisbanik opened this issue Oct 9, 2018 · 5 comments

Comments

@subhasisbanik
Copy link

I have prepared a cluster of 2 nodes in IPFS with IPFS Daemon and swarm key.
I am running orbitdb and connecting to the IPFS node with "ipfs-api" in HTTP protocol.
I am able to post data through orbitdb (feed, keyvalue) from one node and am able to fetch it back in another orbitdb instance connected to the other node.
The issue is that when I am calling a "put" in one instance and trying to "get" it from another instance, its not able to "get" it the first time. But if the same "get" is being called from the same node the second time, its showing the data.

I am not sure how orbitdb sync works but below is the code that I am running.

Node 1-----------
const OrbitDB = require('orbit-db');
const IpfsApi = require('ipfs-api');
const ipfs = IpfsApi('localhost', '5002', {protocol: 'http'})
const orbitdb = new OrbitDB(ipfs);
const writePermissions = [orbitdb.key.getPublic('hex'), ];
const kv = await orbitdb.kvstore('new-db',{write:writePermissions});
await kv.load();
await kv.put('test', 80);

Node 2------------
const OrbitDB = require('orbit-db');
const IpfsApi = require('ipfs-api');
const ipfs = IpfsApi('localhost', '5002', {protocol: 'http'})
const orbitdb = new OrbitDB(ipfs);
const db = await orbitdb.open('/orbitdb/QmPJ4QXJDfBjPKB6JKZcqmrDfDxbubC85BMQvxDcR2Gunb/new-db');
await db.load();
db.get('test');

Please help!!!

@tyleryasaka
Copy link

tyleryasaka commented Nov 20, 2018

@subhasisbanik I know what the cause of this issue is (I ran into the same thing myself). db.load() resolves only after the database has finished loading from storage into memory. It does not wait for the database to be replicated from peers. The replication happens in the background, and is likely completing a split second after db.get('test') is run. That's why when you run the code a second time, you get the expected value.

If you want to wait for the replication to finish, you can listen for the replicated event. E.g.

await Promise.resolve(resolve => {
  db.events.on('replicated', resolve)
})

Beware, though; if no replication occurs, the above code will hang indefinitely. (For example, if the peers are already fully in sync.) This is a major problem; I don't know of a good solution off the top of my head, though I am looking into options. I'll report back if I find anything.

@subhasisbanik
Copy link
Author

subhasisbanik commented Nov 20, 2018 via email

@tyleryasaka
Copy link

Hi @subhasisbanik I did find a solution, which requires a couple modifications to orbit-db.

First, I made this change to orbit-db: projectaspen/orbit-db@f91220d

Then I made this minor adjustment to orbit-db-store: projectaspen/orbit-db-store@a31e1f7

This allows me to wait for the new synced event which is fired once for each peer + database, after all of the data has been copied over. If you wait for this event (once after opening each database) for each node you want to sync with, then when you retrieve data for orbit-db, it will be up-to-date.

To make this process smoother, I created 2 packages: 1 for the orbit-db server node, one for the client. They're experimental right now, but feel free to check them out! They use the orbit-db repo changes I linked to above.

@subhasisbanik
Copy link
Author

subhasisbanik commented Nov 21, 2018 via email

@aphelionz
Copy link
Member

Moving this to the Field Manual repo so we can detail this in the book

@aphelionz aphelionz transferred this issue from orbitdb/orbitdb Sep 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants