Skip to content

Commit

Permalink
Merge branch 'release/v0.5.2.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiru Srikantha committed Jun 15, 2015
2 parents aae160a + d4f3431 commit eabc4fc
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Consul.Test/AgentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public void Agent_CheckStartPassing()
}

[TestMethod]
public void Agent_Checks_serviceBound()
public void Agent_Checks_ServiceBound()
{
var c = ClientTest.MakeClient();

Expand Down
2 changes: 1 addition & 1 deletion Consul.Test/KVTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public void KV_WatchGet()

Task.Run(() =>
{
Thread.Sleep(100);
Task.Delay(1100).Wait();
var p = new KVPair(key) { Flags = 42, Value = value };
var putRes = kv.Put(p);
Assert.IsTrue(putRes.Response);
Expand Down
37 changes: 36 additions & 1 deletion Consul.Test/LockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void Lock_AcquireWaitRelease()

Assert.IsTrue(l.IsHeld);

// Wait for multiple renewal cycles to ensure the semaphore session stays renewed.
// Wait for multiple renewal cycles to ensure the lock session stays renewed.
Task.Delay(TimeSpan.FromSeconds(60)).Wait();
Assert.IsTrue(l.IsHeld);

Expand Down Expand Up @@ -141,6 +141,7 @@ public void Lock_Contend()
{
Debug.WriteLine("Contender {0} acquired", v);
}
Task.Delay(1000).Wait();
lockKey.Release();
});
acquireTasks[i].Start();
Expand All @@ -154,6 +155,40 @@ public void Lock_Contend()
}
}

[TestMethod]
public void Lock_Contend_LockDelay()
{
var c = ClientTest.MakeClient();
const string key = "test/lock";

var acquired = new bool[3];

var acquireTasks = new Task[3];

for (var i = 0; i < 3; i++)
{
var v = i;
acquireTasks[i] = new Task(() =>
{
var lockKey = c.CreateLock(key);
lockKey.Acquire(CancellationToken.None);
acquired[v] = lockKey.IsHeld;
if (lockKey.IsHeld)
{
Debug.WriteLine("Contender {0} acquired", v);
}
c.Session.Destroy(lockKey.LockSession);
});
acquireTasks[i].Start();
}

Task.WaitAll(acquireTasks, (int)(4 * Lock.DefaultLockWaitTime.TotalMilliseconds));

foreach (var item in acquired)
{
Assert.IsTrue(item);
}
}
[TestMethod]
public void Lock_Destroy()
{
Expand Down
7 changes: 7 additions & 0 deletions Consul/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ public QueryResult<T> Execute()
var stopwatch = Stopwatch.StartNew();

var req = WebRequest.CreateHttp(BuildConsulUri(Url, Params));
req.Timeout = _requestTimeout;
req.ReadWriteTimeout = _requestTimeout;
req.Method = Method.Method;
req.Accept = "application/json";
req.KeepAlive = true;
Expand Down Expand Up @@ -478,6 +480,8 @@ public WriteResult<T2> Execute()
{
var stopwatch = Stopwatch.StartNew();
var req = WebRequest.CreateHttp(BuildConsulUri(Url, Params));
req.Timeout = _requestTimeout;
req.ReadWriteTimeout = _requestTimeout;
req.Method = Method.Method;
req.Accept = "application/json";
req.KeepAlive = true;
Expand Down Expand Up @@ -597,6 +601,9 @@ private static void WriteRawRequestBody(object body, Stream stream)
/// </summary>
public abstract class Request
{
// 15 minute HTTP timeout
protected const int _requestTimeout = 900000;

public Config Config { get; set; }
public HttpMethod Method { get; set; }
public Uri Url { get; set; }
Expand Down
26 changes: 17 additions & 9 deletions Consul/Lock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,21 @@ public CancellationToken Acquire(CancellationToken ct)
_cts.Cancel();
throw new TaskCanceledException();
}
try
{
Task.Delay(DefaultLockRetryTime, ct).Wait(ct);
}
catch (TaskCanceledException)

// Failed to get the lock, determine why by querying for the key again
qOpts.WaitIndex = 0;
pair = _client.KV.Get(Opts.Key, qOpts);

// If the session is not null, this means that a wait can safely happen using a long poll
if (pair.Response != null && pair.Response.Session != null)
{
qOpts.WaitIndex = pair.LastIndex;
continue;
}

// If the session is null and the lock failed to acquire, then it means a lock-delay is in effect and a timed wait must be used to avoid a hot loop.
try { Task.Delay(DefaultLockRetryTime, ct).Wait(ct); }
catch (TaskCanceledException) {/* Ignore TaskTaskCanceledException */}
}
throw new LockNotHeldException("Unable to acquire the lock with Consul");
}
Expand Down Expand Up @@ -711,7 +719,7 @@ public void ExecuteLocked(LockOptions opts, CancellationToken ct, Action action)
}

/// <summary>
/// Do not use except unless you need this. Executes an action in a new thread under a lock, ABORTING THE THREAD if the lock is lost and the action does not complete within the lock-delay.
/// Do not use unless you need this. Executes an action in a new thread under a lock, ABORTING THE THREAD if the lock is lost and the action does not complete within the lock-delay.
/// </summary>
/// <param name="key"></param>
/// <param name="action"></param>
Expand All @@ -729,7 +737,7 @@ public void ExecuteAbortableLocked(string key, Action action)
}

/// <summary>
/// Do not use except unless you need this. Executes an action in a new thread under a lock, ABORTING THE THREAD if the lock is lost and the action does not complete within the lock-delay.
/// Do not use unless you need this. Executes an action in a new thread under a lock, ABORTING THE THREAD if the lock is lost and the action does not complete within the lock-delay.
/// </summary>
/// <param name="opts"></param>
/// <param name="action"></param>
Expand All @@ -747,7 +755,7 @@ public void ExecuteAbortableLocked(LockOptions opts, Action action)
}

/// <summary>
/// Do not use except unless you need this. Executes an action in a new thread under a lock, ABORTING THE THREAD if the lock is lost and the action does not complete within the lock-delay.
/// Do not use unless you need this. Executes an action in a new thread under a lock, ABORTING THE THREAD if the lock is lost and the action does not complete within the lock-delay.
/// </summary>
/// <param name="key"></param>
/// <param name="ct"></param>
Expand All @@ -766,7 +774,7 @@ public void ExecuteAbortableLocked(string key, CancellationToken ct, Action acti
}

/// <summary>
/// Do not use except unless you need this. Executes an action in a new thread under a lock, ABORTING THE THREAD if the lock is lost and the action does not complete within the lock-delay.
/// Do not use unless you need this. Executes an action in a new thread under a lock, ABORTING THE THREAD if the lock is lost and the action does not complete within the lock-delay.
/// </summary>
/// <param name="opts"></param>
/// <param name="ct"></param>
Expand Down
4 changes: 2 additions & 2 deletions Consul/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.5.2.7")]
[assembly: AssemblyFileVersion("0.5.2.7")]
[assembly: AssemblyVersion("0.5.2.8")]
[assembly: AssemblyFileVersion("0.5.2.8")]
[assembly: InternalsVisibleTo("Consul.Test")]

0 comments on commit eabc4fc

Please sign in to comment.