Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
NCBC-1382: Make KVErrorMap work with CouchbaseMock
MOTIVATION
----------
KV error map currently only works with CCP but should also work with
HTTP streaming config too. The integration test that was added to work
with cluster 5.0 and needs to also support CouchbaseMock.

MODIFICATIONS
-------------
- Move the GetKVErrorMap operation inside IIOService implementations of
  EnableServerFeatures
- Make the ErrorMap property internally available
- Remove SetErrorMap
- Update PooledIOService and MultiplexingIOService tests with ErrorMap
  changes

RESULT
------
The KV error map test now works with both HTTP Streaming and CCP
providers and can load the KV error map from either a 5.0 cluster or
Couchbase Mock.

Change-Id: I00557515fc80eb53862061670b52f614389c8b66
Reviewed-on: http://review.couchbase.org/77496
Tested-by: Build Bot <build@couchbase.com>
Reviewed-by: Mike Goldsmith <goldsmith.mike@gmail.com>
  • Loading branch information
MikeGoldsmith committed Jun 23, 2017
1 parent 9b85047 commit 44f6455
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 52 deletions.
3 changes: 1 addition & 2 deletions Src/Couchbase.IntegrationTests/ClusterTests.cs
Expand Up @@ -97,7 +97,7 @@ public void Test_Query_WhenAuthenticateNotCalled_ThrowsInvalidOperationException

[TestCase(true)]
[TestCase(false)]
[Ignore("Integration server does not support KV Error map yet")]
[Ignore("Only supported on cluster 5.0+ or CouchbaseMock")]
public void UseKvErrorMap_Retuns_True_When_KVErrorMap_Is_Enabled(bool enabled)
{
var config = TestConfiguration.GetConfiguration("basic");
Expand All @@ -112,7 +112,6 @@ public void UseKvErrorMap_Retuns_True_When_KVErrorMap_Is_Enabled(bool enabled)
};

var cluster = new Cluster(config);
cluster.Authenticate(new PasswordAuthenticator("session-webapp", "secure123"));
var bucket = cluster.OpenBucket("default");
Assert.AreEqual(enabled, bucket.SupportsKvErrorMap);
}
Expand Down
5 changes: 1 addition & 4 deletions Src/Couchbase.Tests/Fakes/FakeIOStrategy.cs
Expand Up @@ -250,10 +250,7 @@ public void Dispose()
{
}

public void SetErrorMap(ErrorMap errorMap)
{
throw new NotImplementedException();
}
public ErrorMap ErrorMap { get; protected set; }


public bool SupportsEnhancedDurability { get; protected set; }
Expand Down
5 changes: 2 additions & 3 deletions Src/Couchbase.Tests/IO/Services/FakeIOService.cs
Expand Up @@ -137,12 +137,11 @@ void IDisposable.Dispose()
throw new NotImplementedException();
}

public void SetErrorMap(ErrorMap errorMap)
public ErrorMap ErrorMap
{
throw new NotImplementedException();
get { throw new NotImplementedException(); }
}


public bool SupportsEnhancedDurability
{
get { throw new NotImplementedException(); }
Expand Down
Expand Up @@ -193,8 +193,10 @@ public void Result_Has_Failure_Status_If_ErrorMap_Available()
mockConnectionPool.Setup(x => x.Acquire()).Returns(mockConnection.Object);
mockConnectionPool.SetupGet(x => x.Configuration).Returns(new PoolConfiguration());

var service = new MultiplexingIOService(mockConnectionPool.Object);
service.SetErrorMap(errorMap);
var service = new MultiplexingIOService(mockConnectionPool.Object)
{
ErrorMap = errorMap
};

var result = service.Execute(new FakeOperationWithRequiredKey("key", null, new DefaultTranscoder(), 0, 0));

Expand Down Expand Up @@ -227,8 +229,10 @@ public void Result_Has_UnknownError_Status_If_ErrorMap_Not_Available()
mockConnectionPool.Setup(x => x.Acquire()).Returns(mockConnection.Object);
mockConnectionPool.SetupGet(x => x.Configuration).Returns(new PoolConfiguration());

var service = new MultiplexingIOService(mockConnectionPool.Object);
service.SetErrorMap(errorMap);
var service = new MultiplexingIOService(mockConnectionPool.Object)
{
ErrorMap = errorMap
};

var result = service.Execute(new FakeOperationWithRequiredKey("key", null, new DefaultTranscoder(), 0, 0));

Expand Down
12 changes: 8 additions & 4 deletions Src/Couchbase.UnitTests/IO/Services/PooledIOServiceTests.cs
Expand Up @@ -83,8 +83,10 @@ public void Result_Has_Failure_Status_If_ErrorMap_Available()
mockConnectionPool.Setup(x => x.Acquire()).Returns(mockConnection.Object);
mockConnectionPool.SetupGet(x => x.Configuration).Returns(new PoolConfiguration());

var service = new PooledIOService(mockConnectionPool.Object);
service.SetErrorMap(errorMap);
var service = new PooledIOService(mockConnectionPool.Object)
{
ErrorMap = errorMap
};

var result = service.Execute(new FakeOperationWithRequiredKey("key", null, new DefaultTranscoder(), 0, 0));

Expand Down Expand Up @@ -117,8 +119,10 @@ public void Result_Has_UnknownError_Status_If_ErrorMap_Not_Available()
mockConnectionPool.Setup(x => x.Acquire()).Returns(mockConnection.Object);
mockConnectionPool.SetupGet(x => x.Configuration).Returns(new PoolConfiguration());

var service = new PooledIOService(mockConnectionPool.Object);
service.SetErrorMap(errorMap);
var service = new PooledIOService(mockConnectionPool.Object)
{
ErrorMap = errorMap
};

var result = service.Execute(new FakeOperationWithRequiredKey("key", null, new DefaultTranscoder(), 0, 0));

Expand Down
Expand Up @@ -63,17 +63,6 @@ public override IConfigInfo GetConfig(string bucketName, string username, string

ioService = IOServiceFactory(connectionPool);

if (ioService.SupportsKvErrorMap)
{
var errorMapResult = ioService.Execute(new GetErrorMap(Transcoder, ClientConfig.DefaultOperationLifespan));
if (!errorMapResult.Success)
{
throw new Exception("Error retrieving error map. Cluster indicated it was available.");
}

ioService.SetErrorMap(errorMapResult.Value);
}

if (ioService.SupportsEnhancedAuthentication) // only execute this if RBAC is enabled on the cluster
{
var selectBucketResult = ioService.Execute(new SelectBucket(bucketName, Transcoder, ClientConfig.DefaultOperationLifespan));
Expand Down
5 changes: 2 additions & 3 deletions Src/Couchbase/IO/IIOService.cs
Expand Up @@ -138,10 +138,9 @@ public interface IIOService : IDisposable
bool SupportsKvErrorMap { get; }

/// <summary>
/// Stores the error map.
/// The Error Map that is used to map error codes from the server to error messages.
/// </summary>
/// <param name="errorMap">The error map.</param>
void SetErrorMap(ErrorMap errorMap);
ErrorMap ErrorMap { get; }
}
}

Expand Down
32 changes: 22 additions & 10 deletions Src/Couchbase/IO/Services/MultiplexingIOService.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -79,6 +79,11 @@ public MultiplexingIOService(IConnectionPool connectionPool, ISaslMechanism sasl
/// </value>
public bool SupportsKvErrorMap { get; private set; }

/// <summary>
/// The Error Map that is used to map error codes from the server to error messages.
/// </summary>
public ErrorMap ErrorMap { get; internal set; }

public IPEndPoint EndPoint
{
get { return _connectionPool.EndPoint; }
Expand All @@ -100,7 +105,7 @@ public IOperationResult<T> Execute<T>(IOperation<T> operation, IConnection conne
{
var request = operation.Write();
var response = connection.Send(request);
operation.Read(response, _errorMap);
operation.Read(response, ErrorMap);
return operation.GetResultWithValue();
}

Expand Down Expand Up @@ -133,7 +138,7 @@ public IOperationResult<T> Execute<T>(IOperation<T> operation)
}
}

operation.Read(response, _errorMap);
operation.Read(response, ErrorMap);
return operation.GetResultWithValue();//might have to handle a special null case her
}

Expand Down Expand Up @@ -166,7 +171,7 @@ public IOperationResult Execute(IOperation operation)
}
}

operation.Read(response, _errorMap);
operation.Read(response, ErrorMap);
return operation.GetResult();
}

Expand Down Expand Up @@ -310,7 +315,8 @@ private void EnableServerFeatures(IConnection connection)
features.Add((short) ServerFeatures.XError);
}

var hello = new Hello(features.ToArray(), new DefaultTranscoder(), 0, 0);
var transcoder = new DefaultTranscoder();
var hello = new Hello(features.ToArray(), transcoder, 0, 0);

var result = Execute(hello, connection);
if (result.Success)
Expand All @@ -319,6 +325,17 @@ private void EnableServerFeatures(IConnection connection)
SupportsSubdocXAttributes = result.Value.Contains((short) ServerFeatures.SubdocXAttributes);
SupportsEnhancedAuthentication = result.Value.Contains((short) ServerFeatures.SelectBucket);
SupportsKvErrorMap = result.Value.Contains((short) ServerFeatures.XError);

if (SupportsKvErrorMap)
{
var errorMapResult = Execute(new GetErrorMap(transcoder, 0));
if (!errorMapResult.Success)
{
throw new Exception("Error retrieving error map. Cluster indicated it was available.");
}

ErrorMap = errorMapResult.Value;
}
}
else
{
Expand Down Expand Up @@ -408,11 +425,6 @@ void Dispose(bool disposing)
}
}

public void SetErrorMap(ErrorMap errorMap)
{
_errorMap = errorMap;
}

#if DEBUG
~MultiplexingIOService()
{
Expand Down
33 changes: 22 additions & 11 deletions Src/Couchbase/IO/Services/PooledIOService.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -70,7 +70,7 @@ public IOperationResult<T> Execute<T>(IOperation<T> operation, IConnection conne
var response = connection.Send(request);

//Read the response and return the completed operation
operation.Read(response, _errorMap);
operation.Read(response, ErrorMap);
return operation.GetResultWithValue();
}

Expand Down Expand Up @@ -129,7 +129,7 @@ public IOperationResult Execute(IOperation operation)
_connectionPool.Release(connection);
}

operation.Read(response, _errorMap);
operation.Read(response, ErrorMap);
return operation.GetResult();
}

Expand Down Expand Up @@ -190,7 +190,7 @@ public IOperationResult<T> Execute<T>(IOperation<T> operation)
_connectionPool.Release(connection);
}

operation.Read(response, _errorMap);
operation.Read(response, ErrorMap);
return operation.GetResultWithValue();
}

Expand Down Expand Up @@ -384,14 +384,25 @@ protected void EnableServerFeatures(IConnection connection)
features.Add((short) ServerFeatures.XError);
}

var hello = new Hello(features.ToArray(), new DefaultTranscoder(), 0, 0);
var result = Execute(hello, connection);
var transcoder = new DefaultTranscoder();
var result = Execute(new Hello(features.ToArray(), transcoder, 0, 0), connection);
if (result.Success)
{
SupportsEnhancedDurability = result.Value.Contains((short) ServerFeatures.MutationSeqno);
SupportsSubdocXAttributes = result.Value.Contains((short) ServerFeatures.SubdocXAttributes);
SupportsEnhancedAuthentication = result.Value.Contains((short) ServerFeatures.SelectBucket);
SupportsKvErrorMap = result.Value.Contains((short) ServerFeatures.XError);

if (SupportsKvErrorMap)
{
var errorMapResult = Execute(new GetErrorMap(transcoder, 0));
if (!errorMapResult.Success)
{
throw new Exception("Error retrieving error map. Cluster indicated it was available.");
}

ErrorMap = errorMapResult.Value;
}
}
else
{
Expand Down Expand Up @@ -455,6 +466,11 @@ private static void LogFailedHelloOperation(IResult result)
/// </value>
public bool SupportsKvErrorMap { get; private set; }

/// <summary>
/// The Error Map that is used to map error codes from the server to error messages.
/// </summary>
public ErrorMap ErrorMap { get; internal set; }

/// <summary>
/// Returns true if internal TCP connections are using SSL.
/// </summary>
Expand Down Expand Up @@ -494,11 +510,6 @@ void Dispose(bool disposing)
_disposed = true;
}

public void SetErrorMap(ErrorMap errorMap)
{
_errorMap = errorMap;
}

#if DEBUG
~PooledIOService()
{
Expand Down

0 comments on commit 44f6455

Please sign in to comment.