Ottoman n1ql Index Building Issue

Would it be possible to open issues on github for ottoman? I’d prefer to report this there, but issues don’t appear available, so I’m reporting this here.

In ottoman’s code, right HERE: https://github.com/couchbaselabs/node-ottoman/blob/master/lib/cbstoreadapter.js#L376

GSI indexes are created for index type “n1ql”. The trouble is that it does not use the defer_build:true option in Couchbase. One behavior I’m seeing is that when you have a complex set of models with many n1ql indexes, only one gets built per software run. This seems to actually make sense via the docs; by default it appears couchbase does not defer the index build. Strangely, this means only one index is built at a time; the software might queue 20 to be created, but only one ends up being created.

When I hack cbstoreadapter to add WITH {“defer_build”:true} to the end of that CREATE INDEX statement, they all queue up; true their build is deferred, but it starts the build of all of them instead of one per query run.

Is this a bug in ottoman (that it should defer index builds, but doesn’t) – or is this a bug in couchbase (that you queue many CREATE INDEX statements, but only one runs)?

Hey @moxious,

Ottoman does not currently use deferred index building. If only a single index is being built at one time, this seems to be a bug in Couchbase Server. I’ll take a look as soon as possible and get back to you on this. As an aside, using deferred index building seems like a reasonable change as we likely should be doing this anyways. Don’t forget to add a ‘BUILD INDEX’ query to trigger the build to start though!

Cheers, Brett

Doing some more investigation, it seems it doesn’t support building many n1ql indexes at once. I haven’t been successful getting the BUILD INDEX approach to work yet with deferred indexes, but I do know why they’re only building one at a time.

[{“code”:5000,“msg”:“GSI CreateIndex() - cause: Cannot create Index default.DateValueMatrix__active while another index is being built.”}],“status”:“errors”,“metrics”:{“elapsedTime”:“4.480264759s”,“executionTime”:“2.467068ms”,“resultCount”:0,“resultSize”:0,“errorCount”:1}}

In cbstoreadapter.js, the handler function is silently swallowing the error - it only checks if the Couchbase query failed. But when you try to create an index while the DB is still busy, the query “succeeds” (err=null) but you get “meta” information back that looks like the above, and tells you exactly what’s wrong.

Arguably this is also a bug in the Node SDK for couchbase, which is causing Ottoman to do the wrong thing. Note errorCount: 1 in the “meta” results…why then would the err argument to the query callback be null?

Nice catch @moxious,

We originally were ignoring the return codes as we wanted to have the behaviour of ‘ignore if the index already exists’. Obviously, it looks like our index management needs two major improvements. The first is the use of deferred indexes to allow us to build multiples at once. Secondly, we need to monitor our return codes so we can be aware of when index creation failed vs already exists. Lastly it seems like it would be useful if we actually monitored index building and waiting until it was completed before ensureIndex completed.

Cheers, Brett