Sync Function Blues

71 views
Skip to first unread message

vf

unread,
Jan 1, 2015, 5:03:05 PM1/1/15
to mobile-c...@googlegroups.com

I’m setting up Sync Gateway and running into two major issues with my sync function, which I’ll paste here for reference:

function (doc, oldDoc) {
    if (doc._deleted) {
        return; //temporary.
    }

    if (!doc.type)
        throw({forbidden: 'All documents must have a type'});

    if (doc.type === 'organization') {
        // requireAccess(doc._id);
        if (doc.members) access(doc.members, doc._id);      
        channel(doc._id);
    }

    if (doc.type === 'list' || doc.type === 'item') {
        // requireAccess(doc.organization);
        channel(doc.organization);
    }
}

The first and most pressing is that users don’t appear to be assigned channels correctly. I have an Organization document that has a members property that contains SG usernames. The sync function I wrote grants access to a channel define by the organization’s ID.

if (doc.type === 'organization') {
        if (doc.members) access(doc.members, doc._id);
        channel(doc._id);
}

When I view the organization document from within the SG Admin UI, I can see that it grants access to user X, but when I view that user I don’t see the organization’s channel listed.

What’s most frustrating is that this happens inconsistently. In my test database, I have 3 organizations that user X is a member of, but the user only has access to 1 channel.

Issue #2 is that I have a user who has access to channel “*”, which I assumed meant that they had access to every channel. But when I add requireAccess calls to my sync function, that user can no longer see any data.

vf

unread,
Jan 1, 2015, 5:38:48 PM1/1/15
to mobile-c...@googlegroups.com
Oh, I'm running SG 1.0.4. (I had the same issue with 1.0.3)

vf

unread,
Jan 1, 2015, 10:59:44 PM1/1/15
to mobile-c...@googlegroups.com
So I haven't retested Issue #2, but I was able to resolve Issue #1 by hitting POST /_resync on the admin port.

It's not very obvious that that is a required step after changing the sync function in a way that alters permissions. For example, it's not mentioned in the Sync Function API documentation at all (http://developer.couchbase.com/mobile/develop/guides/sync-gateway/sync-function-api-guide/index.html). I notice now that when I change the sync function and restart the server that there's a little blurb outputted into the error log. I'm assuming you don't automatically resync on launch because of the performance implications?

On a related note, when one changes the sync function using the Admin UI, is it correct to assume that the new sync function is used while the server is running and that the server reverts back to the sync function given in config.json upon restart? Is it necessary to hit POST /_resync after changing the sync function in the Admin UI?

J. Chris Anderson

unread,
Jan 12, 2015, 8:01:59 PM1/12/15
to mobile-c...@googlegroups.com
Thanks for these questions. I think there are a few issues we have to resolve.


On Thursday, January 1, 2015 at 7:59:44 PM UTC-8, vf wrote:
So I haven't retested Issue #2, but I was able to resolve Issue #1 by hitting POST /_resync on the admin port.

It's not very obvious that that is a required step after changing the sync function in a way that alters permissions. For example, it's not mentioned in the Sync Function API documentation at all (http://developer.couchbase.com/mobile/develop/guides/sync-gateway/sync-function-api-guide/index.html). I notice now that when I change the sync function and restart the server that there's a little blurb outputted into the error log. I'm assuming you don't automatically resync on launch because of the performance implications?


The admin sync function updates are not persisted, when you are done developing you need to update your config files.
 
On a related note, when one changes the sync function using the Admin UI, is it correct to assume that the new sync function is used while the server is running and that the server reverts back to the sync function given in config.json upon restart? Is it necessary to hit POST /_resync after changing the sync function in the Admin UI?

I'm not sure about _resync maybe Adam or Jens can answer. It might be that we just need to call resync after we save the sync function (in the admin console code).

Chris

Adam Fraser

unread,
Jan 13, 2015, 1:19:44 AM1/13/15
to mobile-c...@googlegroups.com
On the _resync question, you're correct: doing a POST to _resync will re-run the sync function against every current document in the database.  So after changing the sync function in the admin UI, the sync function should be active and used any subsequent document updates, but you'd need to call _resync to trigger the reprocessing of existing documents.

Jens Alfke

unread,
Jan 13, 2015, 1:39:25 AM1/13/15
to mobile-c...@googlegroups.com
On Jan 1, 2015, at 7:59 PM, vf <vladimir...@gmail.com> wrote:

It's not very obvious that that is a required step after changing the sync function in a way that alters permissions. For example, it's not mentioned in the Sync Function API documentation at all (http://developer.couchbase.com/mobile/develop/guides/sync-gateway/sync-function-api-guide/index.html).

The "Changing the sync function" section is out of date — it was written for an earlier version of SG, in which it would do the equivalent of _resync on launch when it detected a change in the sync function from the previous launch. (I've just filed an issue against the docs to get this fixed.)

I notice now that when I change the sync function and restart the server that there's a little blurb outputted into the error log. I'm assuming you don't automatically resync on launch because of the performance implications?

Yes; it can take a long time to resync and the gateway isn't available to serve any requests during that time. And some sync function changes won't affect existing documents, making that unnecessary. So we decided to put the developer in charge of whether to resync or not.

On a related note, when one changes the sync function using the Admin UI, is it correct to assume that the new sync function is used while the server is running and that the server reverts back to the sync function given in config.json upon restart?

The gateway never writes to the config file. It's strictly read-only. Therefore any changes to the sync function (or other database configuration) made at runtime will never be persistent across launches. You have to update the config file correspondingly.

—Jens

Reply all
Reply to author
Forward
0 new messages