SPY-148: Do not rename the worker thread for listeners.

It's more explicit if the naming of the threads is left to the
executor service (through custom thread factory).

Change-Id: I7457d036b70ba6dd30d6d99dd9d8cbcbaa0cf3dd
Reviewed-on: http://review.couchbase.org/31715
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Changes for the New feature test for credential encryption CRAM-MD5 or PLAIN

Change-Id: Ic777b04e87670f0a9d967b4a439fea205b129e97
Reviewed-on: http://review.couchbase.org/32588
Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Tested-by: Deepti Dawar <deepti.dawar@globallogic.com>
SPY-155: Synchronize when future listeners are notified.

This changeset prohibits a possible race condition when listeners
are called from the callback. Proper sync has been in place for
both adding and removing, but not for notifying.

Change-Id: I4cfa0036c2b7b89985b0026c5da5dca26cc9fe52
Reviewed-on: http://review.couchbase.org/33800
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
Add support for replica get with CAS (ReplicaGets).

This changeset adds the underlying operations and clone-logic for
replica get operations including the underlying CAS value.
This can be utilized by the upper layers to provide responses that
not only include the raw value from a replica, but also their
corresponding CAS value.
Change-Id: Ib218669b6bbc84f3111e107e14761a03ed2f9728
Reviewed-on: http://review.couchbase.org/33799
Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
SPY-153: Adding a typesafe StatusCode for OperationStatus instances.

This makes it possible to add easier checks on futures, while not
having to rely on string matching in the status message. This is
an addition, so old behavior will not be changed, its just more
convenient and can be better documented (it does not directly rely
on a server response).

Change-Id: Ie416e317d5fd21af2c3efb177b08b68dd8c45639
Reviewed-on: http://review.couchbase.org/33405
Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
SPY-127: Optimize StringUtils performance.

This changeset optimizes the individual StringUtils methods, especially
the isJsonObject method. Before, if no proper JSON was detected, a final
check against an Integer was performed. If no integer was found, a very
expensive exception was thrown, killing performance.

This check has been replaced with a faster regex. Also, other parts
of the methods have been refactored to be more performant.

Change-Id: I7616d2b042896aa76d73d536a3dde1c998990d9b
Reviewed-on: http://review.couchbase.org/34184
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
SPY-154: Also RETRY on binary NMVB response with bulk gets.

This changeset makes sure that even when one of the individual get
operations in the bulk request fails with a "Not My VBucket", the
single get operation gets retried.

To make this work, the underlying bulk operation is changed slightly
to be notified that it has to wait for N more operations before
notifying the listeners and counting down the latch.

Change-Id: I6413233a3d0289ab728312f8f4edab5df96a0081
Reviewed-on: http://review.couchbase.org/33700
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
SPY-158: Use Milliseconds instead of Seconds for "max reconnect" delay.

Motivation
----------
Currently, when a reconnection needs to happen against a node, a incremental
delay for subsequent reconnect tries is selected. Because of the wrong time
format used (seconds instead of milliseconds), the delay is always capped
at 30ms, leading to immediate reconnects with no backoff effect.

Modifications
-------------
For backwards compatibility, the "second" time unit is kept in the factory,
and instead in the MemcachedConnection the time is converted to milliseconds.

Result
------
A proper reconnect delay going from 2^0 seconds up to 2^4 seconds, peaking at
30 seconds by default (as always changeable through the factory)

This changeset makes sure that

Change-Id: I4a348f50e41451a6542b4b0263b915990730c3f8
Reviewed-on: http://review.couchbase.org/34728
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
SPY-159: Make the auth latch timeout configurable.

Motivation
----------
Currently, the auth latch timeout is fixed to one second and not changeable.
In certain scenarios (for example with CRAM-MD5 where more roundtrips need
to be issued), this is too short. Making it configurable allows to adjust the
setting to latencies in target environments.

Modification
------------
Expose a "authWaitTime" setting in the factory and the builder to allow set
the value in milliseconds. It still defaults to one second for backwards
compatibility.

Result
------
The auth timeout can be modified as needed in production deployments.

Change-Id: Id06f4ede00402688c00a283bcff82cde25a7a205
Reviewed-on: http://review.couchbase.org/34846
Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
SPY-157: Add Auth logging.

Motivation
----------
Debugging how long authentication steps take is currently not
possible. Proper logging needs to be added in order to debug
authentication timeframes.

Modifications
-------------
Added logging infrastructure for the individual steps (list sasl
mechs, and challenge/response cycles), as well as a total amount
on a per-node basis.

If an individual step takes over 250ms it is WARNed instead of
DEBUGed, and if the total auth step takes over 1 second it it also
WARNed. Note that while these timings are fixed values, in general
the operation taking this long points to something wrong since it
is expected to take in the 1ms range.

Result
------
Auth times can now be observed both in development and production,
better pointing to possible issues.

Change-Id: I3deab53332251263c5a6dcf3e2bfd75538dfb24c
Reviewed-on: http://review.couchbase.org/34849
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
SPY-160: Redistribute ops when waiting for authentication to complete.

Motivation
----------
When an operation gets added and the node needs to (re)connect, it waits
for a given amount of time (authWaitTime). If the auth does not complete
in the period of time, the operation is cancelled. This is suboptimal,
especially if the operation is not timed out yet, it could wait longer
until it gets timed out/cancelled.

Modifications
-------------
This changeset makes sure that an operation gets redistributed (assuming
FailureMode is NOT cancel) if the authLatch is not counted down in the
wait period.

Redistributing will only happen (further down the code stack) if the
op is 1) not cancelled and 2) not timed out already.

Result
------
Still valid operations will be transparently rescheduled until they
either go through validly or will eventually time out or get
cancelled.

Change-Id: I6f849314e11435a6ebc69a4bfc1eab705df850e8
Reviewed-on: http://review.couchbase.org/34852
Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
SPY-156: Expose more async mutate methods.

Motivation
----------
More flexible incr/decr methods are exposed as sync variants than
with their async counterparts. This patch is an effort to bring them
on par (almost) completely.

Modifications
-------------
More async variants have been exposed, which were already supported
through the private asyncMutate method. Note that since the ascii
protocol doesn't support defaults and expiration for incr/decr, those
return an UnsupportedOperationException, because it is nontrivial to
fix. Please refer to the sync variants if you need that.

Result
------
When the binary protocol is used, much more asyncIncr/asyncDecr methods
are exposed, explicitly helping with default values and expiration.

Change-Id: I4403b14d6146afb325afa9c239da28b55e89e935
Reviewed-on: http://review.couchbase.org/34257
Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
SPY-157: Fix auth logging times.

Motivation
----------
In the previous commit which added logging, it measured nanoseconds and printed
milliseconds. This also screwed up the debug/warn metric.

Modifications
-------------
Correctly convert nanoseconds to milliseconds.

Result
------
Timings are now properly warned and formatted.

Change-Id: Id174a49e03e052f9e32c98cc32aa7ad0b82bbb4d
Reviewed-on: http://review.couchbase.org/34936
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
SPY-160: Queue retry op instead of redistributing it immediately.

Motivation
----------
After making the original SPY-160 changes, the ops were correctly redistributed,
but if the latch never gets counted down it gets recursively distributed and
added. Example:

	  at net.spy.memcached.protocol.TCPMemcachedNodeImpl.addOp(TCPMemcachedNodeImpl.java:344)
	  at net.spy.memcached.MemcachedConnection.addOperation(MemcachedConnection.java:1206)
	  at net.spy.memcached.MemcachedConnection.redistributeOperation(MemcachedConnection.java:994)
	  at net.spy.memcached.protocol.TCPMemcachedNodeImpl.addOp(TCPMemcachedNodeImpl.java:350)
	  at net.spy.memcached.MemcachedConnection.addOperation(MemcachedConnection.java:1206)
	  at net.spy.memcached.MemcachedConnection.redistributeOperation(MemcachedConnection.java:994)
	  at net.spy.memcached.protocol.TCPMemcachedNodeImpl.addOp(TCPMemcachedNodeImpl.java:350)
	  at net.spy.memcached.MemcachedConnection.addOperation(MemcachedConnection.java:1206)
	  at net.spy.memcached.MemcachedConnection.redistributeOperation(MemcachedConnection.java:994)

Modifications
-------------
The changeset, instead of redistributing right now, adds the operation to the retry queue which means
it will be handled eventually, but gives the IO thread a chance to run other tasks (including retreiving
auth response information).

Result
------
The operation is queued and is not blocking the IO thread.

Change-Id: I73a8e77255a54bceeb929febcadb0e555febdd67
Reviewed-on: http://review.couchbase.org/34938
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
Add development pom.xml

Motivation
----------
While ivy/ant works for years now, it is a little too hard to get
started when contributing simple patches with IDE setup. Since most
IDEs have good maven support, adding a pom.xml to get up and running
quickly makes it easier to contribute and develop.

Modification
------------
Add a pom.xml which is inteded to get up and running quickly. It is not
designed to do release management, which still happens through the
ant/ivy pipeline.

Note that the test case fix is by intention, because otherwise the
unit tests get messed up when ran through IntelliJ with the maven
file imported.

Result
------
Contributors and developers can import the project more easily and
contribute patches.

Change-Id: I837eb73cb07560aef15301490eaab56e484be3a8
Reviewed-on: http://review.couchbase.org/34845
Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
SPY-160: Avoid ConcurrentModificationException with the retry queue

Motivation
----------
ConcurrentModificationException will show up since the code can recursively
modify itself.

Modifications
-------------
Copy the original list into a temporary new one to avoid the exception.

Result
------
Redistribute on the retry list will work as expected, without an
exception.

Change-Id: Ieff1a87136bef38280b7f139192419073d6a878a
Reviewed-on: http://review.couchbase.org/34977
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
SPY-161: Cascade op cancellations.

Motivation
----------
When an operation is redistributed (be it because of reconnects
and/or "not my vbucket" responses, it gets cloned. The problem
is that the initial callback always points to the original
operation, not the cloned one. While the clone gets the callback,
the other way round (like cancelling from user-code) will never
trickle through to the cloned ops.

Modifications
-------------
In order to fix this issue, code has been added which connects
the original op with the cloned ones, by keeping a list of
cloned operations in the original op. To prevent overly long
stack in malicious scenarios, an overall clone-cap of 100
clones is introduced. If this cap is reached, the operation
will not be cloned further but cancelled.

Result
------
With this modifications, timeouts and cancellations from user code
will trickle through to the clone operations, prevent issues when
cloned operations are around "forever".

Change-Id: I304534a62202b705d78ca5f775f47afcd6be238a
Reviewed-on: http://review.couchbase.org/34883
Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Enhanced auth logging on sasl failure.

Motivation:
-----------
In case of long-taking auth roundtrips, it could be the case - for one 
reason or another - that a empty sasl list gets returned or the auth 
thread gets interrupted, leading to the same effect. This needs to 
be better debugable from the logs.

Modifications:
--------------
Better logging has been added for auth failure scenarios, and also the 
sasl mechs response has been refactored a bit so that the timing is 
also reported in case of an error.

Result:
-------
Auth errors are now easier to debug from the logs.

Change-Id: I9f0fff8b2c3a6b1492fb0fd82746ff78f7b006c5
Reviewed-on: http://review.couchbase.org/35672
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
SPY-162: Also respect nagle setting on reconnect.

Motivation
----------
If a server connection is reset (because it is lost or a reconnection
is triggered), the nagle setting is not re-set properly.

Modifications
-------------
During the reconnect phase, properly re-set the nagle setting so it is
respected on the new connection.

Result
------
The custom nagle setting is respected and used even across new connections.

Change-Id: Ic0b513b8538a39710aaf8fd0f0f699a5c561bc09
Reviewed-on: http://review.couchbase.org/35553
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
SPY-163: Count down bulk get latch even when empty key list is provided.

Motivation
----------
When a empty iterator (or empty key list) is passed in to a get bulk operation,
a result is never returned and the thread is blocked because it waits on a
latch that will never be counted down (because no response will ever arrive).

Modifications
-------------
If no chunks with keys are sent out to the servers, the latch is initialized
to 0 right away so that the code doesn't need to wait at all.

Result
------
The code now returns properly with an empty map instead of blocking the thread
forever.

Change-Id: I0711c399d9f15010bc808e0f651ce6ad605c06f0
Reviewed-on: http://review.couchbase.org/36208
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>