Problem with replication using sync gateway + CBLite in Phonegap application


#1

Hello to everybody,

I have a problem regarding the replication of a Phonegap/Cordova application, but before a little bit of background:

  • I have an external web application that is used by some users to save data in a couchbase server bucket in JSON format (bucket name: ‘oliclinomel’).

  • I have a sync gateway which it is shadowing the previous bucket. The purpose is to have the data of the shadowed bucket available for the people who install the mobile application in order to use it offline. The mobile users will just consume the data, they won’t create new documents in the sync_gateway, the data just goes in one direction. This is the configuration:

    {
    “interface”: “:5984”,
    “adminInterface”: “:5985”,
    “log”: [ “CRUD”, “CRUD+”, “HTTP”, “HTTP+”, “Access”, “Cache”, “Shadow”, “Shadow+”, “Changes”, “Changes+”, “SG” ],
    “databases”: {
    “oliclinomel_sync”: {
    “server”: “http://localhost:8091”,
    “bucket”: “oliclinomel_sync”,
    “sync”: “function(doc) {channel(doc.channels);}”,
    “users”: {
    “GUEST”: {“disabled”: false, “admin_channels”: [""], “all_channels”: [""] },
    “oliclinomel”: {
    “disabled”: false,
    “admin_channels”: [""],
    “password”: “password”,
    “admin_roles”: [],
    “roles”: [],
    “all_channels”: ["
    "]
    }
    },
    “shadow”: {
    “server”: “http://localhost:8091”,
    “bucket”: “oliclinomel”
    }
    }
    }
    }

For the moment I am using guest access as all the information stored in the couchbase server buckets in public, maybe later I will route the information in several channels depending on the content of the JSON documents.

When I start the sync_gateway it seems that all the documents are properly synchronized (you can see it in the log). So far so good, so the next step is to create the local CBLite database which should be synchronized with the sync gateway, and here is where my problems start.

When my application is loaded I make post request in order to replicate the bucket from the gateway:

var remote = {
 url: 'http://' + GATEWAY_HOST + BUCKET
 };


var pullInfo = {
 create_target: true,
 source: remote,
 target: dbName,
 continuous: false,
 cancel: false
};
$log.debug('[COUCHBASE]: Pulling changes from ', pullInfo.source);
return $http.post(cbUrl + '_replicate', pullInfo).then(function (res) {
 $log.debug('[COUCHBASE_HOST]: Replication successful with session id ', res.data.session_id);
 return res.data;
}, function (res) {
 $log.debug('[COUCHBASE_HOST]: Replication failed: ', res);
});

As far as I know the previous request is a one shot replication, which is ok as I don’t need a frequent synchronization, and if you take a look to the attached logs (line 60), you can see that all the documents are identified and sent to the local DB, but the problem is that when I retrieve the _all_docs from the CBLite DB in the app I get the documents in the below screenshot (2 design docs created in the setup of the local database and just 2 of the retrieved documents from the replication)

Screenshot

And if I make a second the replication request, the missing documents in the DB are retrieved by the gateway (as you can see in the log file) but the documents are not saved into CBLite and the local document that it is create to track the replications never is updated:

_sync:local:01202cb351fa162c38927bbcbf7ee22354f91089
{
"_rev": “0-1”,
“lastSequence”: “4”
}

I have tried also with continuous replication instead of one shot but I have the same problem, do you have any clue why this is happening to me?

I use the Phonegap Couchbase Lite plugin and the problems I have them with Android (in both device and emulator), I haven’t tested it on iOS yet.

About versions, I am using Couchbase server 3.0.1, sync gateway 1.0.3 (I am about to install the 1.0.4 to see if it gets better) and 1.0.3 of the Phonegap plugin for CBLite.

And also sometimes I get this error when I start the gateway:

11:50:22.092581 WARNING: Error from Incr in _reserveSequences(1): MCResponse status=DELTA_BADVAL, opcode=INCREMENT, opaque=0, msg: Non-numeric server-side value for incr or decr -- db.(*sequenceAllocator)._reserveSequences() at sequence_allocator.go:59
11:50:22.093581 FATAL: Error opening database: Couldn't create user "GUEST": MCResponse status=DELTA_BADVAL, opcode=INCREMENT, opaque=0, msg: Non-numeric server-side value for incr or decr -- rest.RunServer() at config.go:415

But usually after the 2nd or third try it works, I don’t know if it is related.

Thanks and best regards.
Jaime


#2

The Sync Gateway folks say that’s apparently an issue with Couchbase Server, and it only happens on Windows (as far as they know.) They’ve filed an issue. Workaround would be to run the database server on Unix instead. If you’re not running it on Windows, please let us know.


#3

OK, that’s very important to know. I don’t work on the Android implementation myself.

The best thing to do is to file an issue on Github. That will bring it to the attention of the Android/Java engineers.


#4

I have finally fixed my issue, after install in my emulator a program to see the logs of the device, I noticed that this error was appearing every time the replication was done:

03-31 10:39:32.112 E/CBLite  (3968): Database: Invalid top-level key '_class' in document to be inserted
03-31 10:39:32.112 I/CBLite  (3968): CBLManagerWorkExecutor CANCEL transaction (level 2)
03-31 10:39:32.112 W/Sync    (3968): com.couchbase.lite.replicator.PullerInternal@5cbee10: failed to write {Document-t_c-in-en #1-054356dc8a11d1fec32cff12df3d1688}: status=400
03-31 10:39:32.112 E/Sync    (3968): com.couchbase.lite.replicator.PullerInternal@5cbee10: Progress: set error = org.apache.http.client.HttpResponseException

So it seems that the documents that have that attributes aren’t replicated to CBLite. This ‘_class’ attribute is added by the spring-data-couchbase library when saving some documents to the shadowed bucket in Couchbase Server, so I had to avoid saving the documents with that library and use the methods in the CocuhbaseClient instead. I don’t know if this behaviour is correct but I think that the replication shouldn’t fail if an attribute like that is saved to the document.

I think I will file an issue in github

Best regards.


#5

Properties starting with “_” are reserved (like _id, _rev, _attachments) so you can’t add custom ones to documents. It sounds like the bucket-shadowing code in Sync Gateway isn’t enforcing that and is copying them into the SG documents. That seems like a bug in Sync Gateway.