Fix rollback unit test to wait for updater to finish

The helper method used for shutting down view group should
wait for updater to finish before terminating the view group.

Change-Id: If3a8bd8eac88ece7ddf513b901647035fe58adee
Reviewed-on: http://review.couchbase.org/39152
Reviewed-by: Sarath Lakshman <sarath@couchbase.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Tested-by: buildbot <build@couchbase.com>
Fix unit tests logic of trigger and wait for initial build

The initial build is performed in two phases. First phase
stream changes from on-disk snapshot and build the btree.
Updater is restarted to process in-memory snapshots (if any)
after the updater run for on-disk snapshot. Since the updater
is restarted during initial build, the unit test's mechanism of
waiting for initial updater's pid monitor message won't work.
The updater waiter will receive a DOWN message soon after first
phase is completed, but initial indexing may not have completed.

Hence, right way to wait for initial indexing to complete would
be to obtain the group with stale=false paramter.

Change-Id: I13f1ea081b0de57dc0d48fb0e178bb5711147332
Reviewed-on: http://review.couchbase.org/39300
Reviewed-by: Sarath Lakshman <sarath@couchbase.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Tested-by: buildbot <build@couchbase.com>
Fix rollback test to wait for updater exit only if pid is not nil

In certain cases, if the indexed seqnos is equal to current
partition seqnos, start_updater call from unit test will not
trigger the updater since there is no change remaining to be
applied. In that case, gen_server:call for start_updater will
return nil as pid. The updater waiter should not monitor and
wait in that case.

Change-Id: I9754ab93e135032dd483dafcc8b0d5d91dbd9416
Reviewed-on: http://review.couchbase.org/39403
Reviewed-by: Sarath Lakshman <sarath@couchbase.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Tested-by: buildbot <build@couchbase.com>
MB-11728 Fix upr fake server implementation of vbucket-seqno stats

When partition number is not specified in vbucket-seqno stats
request, ep-engine returns seqnos for all the existing partitions.
It does not return error response at all. Error response is only
returned when partition number is specified in the request.

Fixed fake server to not  return error response when vbucket-seqno
request is used.

Change-Id: Ifb72374f572cb615fbb50e58f473beef75a908e8
Reviewed-on: http://review.couchbase.org/39404
Reviewed-by: Sarath Lakshman <sarath@couchbase.com>
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>
MB-11728 Avoid blocking view group process for get_stats()

Whenever a stale=update_after query or updater daemon triggers an updater run,
it needs to obtain vbucket-seqno stats to decide if we need to start the
updater or not. It blocks for while until stats response arrive from ep-engine.
The view group gen_server process messages one by one. If one of the message
takes more time, other messages get queued and will be delayed for processing.
Hence if a query request is in the mailbox for long, queries will experience
a delay in getting the response. Blocking in the view group process can result
in higher latency for queries and affect the query throughput.

Refactored start_updater() method to make it asynchronous in nature. Every
start updater request will internally send a signal to a dedicated updater
scheduler process and return immediately. The dedicated updater scheduler
process will perform a get_stats and post partition seqnos to the view_group.
View group process can immediately compare seqnos with indexed seqnos and
start updater if required.

Change-Id: Iddf5e46b4665e57a544219343243baab95accec9
Reviewed-on: http://review.couchbase.org/39153
Reviewed-by: Sarath Lakshman <sarath@couchbase.com>
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>
upr client: Wait for 100ms before retry during tmpfail

During rebalance, stream request for certain partitions can
get tmpfail response. Retrying without any wait time results
in log files getting flooded with retry log messages.

Change-Id: Iee57f4f80b707826fd562be5e253070a559f47e3
Reviewed-on: http://review.couchbase.org/39405
Reviewed-by: Sarath Lakshman <sarath@couchbase.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Tested-by: buildbot <build@couchbase.com>
Uniform use of posix path separation in runtest module.

Erlang modules expect paths in posix format where path
separation is '/'. This is the case even on Windows.
Since posix path work fine on Windows platform we can
force posix path by replacing '\' with '/'.

Cmake module option -m uses combination of absolute paths
and relative path. Forcing posix path format by string
concatination caters only to relative paths at runtime
Absolute paths will not work as expected with string
concatination. OS module specifically os.path.join will
handle these cases transparently. For example string
concatenation of /A/B/ and /A/B/C would give /A/B/A/B/C
where as os.path.join will result in /A/B/C which is
what is expected.

Since paths emitted by cmake would be used by other
modules, we need to use os.path module to generate
correct paths at runtime in our script and force
posix path format by replacing Windows style path
separation symbol '\' by Unix style path separation
symbol '/'. Also ensure that space escaped by '\'
are preserved, since directory names might contain
spaces in them. Hence regular expression substituion
is used.

Change-Id: I63cf5bd4b58fe7a0c78abac1f35eb37063e8ee7b
Reviewed-on: http://review.couchbase.org/39302
Reviewed-by: Harsha H S <hhs.couchbase@gmail.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Tested-by: buildbot <build@couchbase.com>
Add per bucket auth to UPR client

The view engine uses a special case of authentication for UPR which
uses a single username/password combination for the whole cluster
and not per bucket. The desired bucket is then selected with the
select bucket command.

If the couch_upr_client is used outside of the view engine it is
desirable to be able to use the per bucket SASL authentication as
well. When used, the select bucket command returns "not implemented"
as the bucket is already selected through the username that was used
for authentication.

Change-Id: I0809de393b8ed11f9d54e74274ca077c05048d47
Reviewed-on: http://review.couchbase.org/39530
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Sarath Lakshman <sarath@couchbase.com>
introduced pre db open hook

So that when entire thing is run we can catch and refuse attempts to
open vbuckets as couchdb databases.

Change-Id: I08547d29d94d425263370cb3d84dacd8c1270c49
Reviewed-on: http://review.couchbase.org/39473
Reviewed-by: Sarath Lakshman <sarath@couchbase.com>
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>
MB-11592 Fix upr snapshot marker flag values

EP-Engine has changed flag values for on-disk and in-memory
snapshot markers. Also a new flag "chk" has been added.

Change-Id: I8a262eb7856c97e11e95570fbdaef82f3f79054a
Reviewed-on: http://review.couchbase.org/39494
Reviewed-by: Michael Wiederhold <mike@couchbase.com>
Tested-by: Michael Wiederhold <mike@couchbase.com>
MB-11746 Fix race condition in ddoc updation

When ddoc_update happens, we restart the group process. ns_server calls
define_group when ddoc is updated. Restart of group process and ns_server
call to define group are independent to each other. In define_group, we
first get the pid of the group process and then call the gen_server call
for define_view.If the group process exits in between getting the group
pid and calling the gen_server, when we call the gen_server, we get the
error.I have added a retry logic if exit reason is normal.

Change-Id: I7f3b23e43162e0d7eb93794c6ba3fa11bfbfce1c
Reviewed-on: http://review.couchbase.org/39663
Reviewed-by: Sarath Lakshman <sarath@couchbase.com>
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Revert "MB-11728 Avoid blocking view group process for get_stats()"

This reverts commit 47b2944e56342180abd5d2bac21f84184d5b6e9a.
From perf tests, it is noticed that it doesn't solve the issue raised
in MB-11728 and bottleneck is moved to updater scheduler.

Change-Id: I4dda6cf6bdcc874ec02027465099e8da90efd729
Reviewed-on: http://review.couchbase.org/39598
Reviewed-by: Sarath Lakshman <sarath@couchbase.com>
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Nimish Gupta <nimish@couchbase.com>
Move code into the more generic module

This is a refactoring without a functional change. Code from the specific
mapreduce views merger is moved into the more general index merger.

Change-Id: I119109624e1a0c4bd5b5f55f37f3f74b7e8cc9f6
Reviewed-on: http://review.couchbase.org/39638
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Sarath Lakshman <sarath@couchbase.com>
Supply view type to couchstore

Couchstore deals with mapreduce as well as spatial views. In order to
know which view it currently processes, we need pass on the view type.

0 stands for mapreduce view
1 stands for spatial view

Change-Id: I7b19789d600d9497cf5314209fa37f12227ca469
Reviewed-on: http://review.couchbase.org/39639
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Tested-by: Volker Mische <volker.mische@gmail.com>
MB-11797 Correct the parameters during stop in upr connection retry.

Change-Id: I40c7e3bb364ba65236d4208ca012a052cd568b4a
Reviewed-on: http://review.couchbase.org/39735
Tested-by: Nimish Gupta <nimish@couchbase.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>
MB-11728 Use provided seqnos for starting updater

When stale=false query is processed, we are performing
get_seqs() call multiple times. We can get the sequence numbers
only once and reuse multiple times and avoid extra latency.

Change-Id: Ia5de3717dbc95460fc3591f499f86287b05617d8
Reviewed-on: http://review.couchbase.org/39724
Reviewed-by: Sarath Lakshman <sarath@couchbase.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Tested-by: buildbot <build@couchbase.com>
MB-11728 Implement partition seqnos lazy cache

For stale=update_after queries, we are not really interested in
latest partition seqnos. Hence, we could cache the seqnos and make
use of it without paying the latency cost for stale=update_after queries.
The cache can be updated lazily based on ttl value (300 ms). Everytime
when a query obtains the cached seqnos, it checks whether the seqnos are
older than ttl value. If cached value is older than ttl, it will update
the cache by obtaining latest seqnos.

Change-Id: Iaea385692c8b948604e779df0f4a74942b6a4414
Reviewed-on: http://review.couchbase.org/39725
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Tested-by: buildbot <build@couchbase.com>
MB-11728 Make seqnos cache updates asynchronous

When seqno cached value has expired, we could do asynchronous updates
instead of synchronously waiting for get_seqs() call.

An async API for obtaining stats has been added in couch_upr_client and
used async method to request and update when seqnos in cache is expired.

With this change, the throughput for stale=update_after improved from
110 queries/sec to 250 queries/sec.

Change-Id: I28343bd11c7ee52a5cbf1e6326c568fcf477acc0
Reviewed-on: http://review.couchbase.org/39726
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Reviewed-by: Sarath Lakshman <sarath@couchbase.com>
Tested-by: buildbot <build@couchbase.com>
MB-11828 upr client: Increase gen_server call timeout to 1 min

The default gen_server call timeout value of 5 seconds seems to be too low.
During rebalance, the upr client seems to be busy and results in timeout.
This impacts the rebalance and it fails during certain cases.

Change-Id: I7791060c26b01fd6f1b6d4afce370c37ad92bef6
Reviewed-on: http://review.couchbase.org/39984
Reviewed-by: Nimish Gupta <nimish@couchbase.com>
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>
MB-11828 upr client: Fix badmatch due to use of bound variable

Change-Id: I4433732fc812d9a94658d463581bb4f668e6ebbd
Reviewed-on: http://review.couchbase.org/39988
Reviewed-by: Sarath Lakshman <sarath@couchbase.com>
Reviewed-by: Nimish Gupta <nimish@couchbase.com>
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>