We have a mobile app that uses both: Synchronization to Sync Gateway and P2P synchronization. For P2P we have a mechanism where the mobile device with the highest IP, its chosen as the “server”, the rest of the mobile devices in the same network, are “clients”, so they push their changes to the “server” and this one is in charge of pushing/pulling changes to the cloud server via SGW. If this server gets off for some reason, the next device with highest IP takes its place.
Synchronization works ok but recently we needed to introduce a filter in order avoid old data to be replicated between devices in such a way that we safely purge documents from a device and avoid other devices to push the recently purged documents back.
I used this guide to set the filter on the PUSH P2P replication:
Filter works ok if the “server” device is purged (has no old data), but if this one goes off and some other device takes its place becoming the “server”, and if this other device contains old data, this old data of course gets pulled from the rest of the devices.
I then tried setting also the filter on PULL replication, but it is not working either because now the rest of the documents (the ones that are NOT in the filter) are actually pushed to the “server” device, but changes from the server are NOT pulled from clients, causing the clients devices not to be in sync with the server device.
Any clue on how to solve this?
Thanks in advance,
if this other device contains old data, this old data of course gets pulled from the rest of the devices.
Are you using push or pull or both? Earlier you said “‘clients’ … push their changes to the ‘server’”.
I know filters work for push replications (where the filter runs locally) but I don’t remember whether they work for P2P pull replication (where the filter has to run on the ‘server’ that’s being pulled from.) I’d have to look at the source, and I don’t have that handy right now. (Also you haven’t said what platform this is for.)
In general if you want to prevent a specific document from being added to the database by a pull, you can add a db validation function that will reject it.
Also, why not have all the devices use the same criteria to purge old documents? That way they won’t end up with different sets of docs (or if they do, it’ll only be for a brief interval.)
Thank you Jens,
The guide only talks about push replication, so I’m not sure it works for pull replication, seems to me that is doesn’t as the filter is not working as it should on pull.
I’m using CB lite 1.3.1 for iOS. And yes, clients push and pull changes to/from server device.
How do I add a DB validation function?
Yes, the idea is that all devices have the same criteria to purge old documents, but I don’t want to generate traffic in the network while they re-replicate… perhaps I should purge the documents before I start P2P replication, when the app is started… hmmm, I’ll take a look and see if this might work.
There’s a CBLDatabase method to register a validation block. It gets to look at the properties of a potential doc revision and then approve or reject it before it’s added to the db. It operates on ALL new revisions, not just ones from replications.
(Sorry not to name the exact method; I’m on an iPad at Starbucks and don’t have the code handy!)
Great! I’ll check that option.
Thanks a lot!!
BR and merry Christmas