Permalink
Browse files

Fixing backup behaviors to always include the previous primary as well

  • Loading branch information...
1 parent 8c80a5b commit 877208bf1e9fa898e4df14a76f371f0589d80d4f ayenderahien committed Jun 5, 2009
@@ -7,21 +7,16 @@ namespace Rhino.DistributedHashTable.IntegrationTests
public class WhenNodeIsStarted : FullIntegrationTest, IDisposable
{
private readonly DistributedHashTableMasterHost masterHost;
- private readonly DistributedHashTableStorageHost storageHost;
private readonly Uri masterUri = new Uri("net.tcp://" + Environment.MachineName + ":2200/master");
public WhenNodeIsStarted()
{
masterHost = new DistributedHashTableMasterHost();
- storageHost = new DistributedHashTableStorageHost(
- masterUri);
-
masterHost.Start();
- storageHost.Start();
}
- [Fact]
+ [Fact(Skip = "not impl yet")]
public void WillJoinMaster()
{
Assert.True(false);
@@ -110,12 +110,12 @@ public OnMasterWithThreeNodes()
}
[Fact]
- public void AddingNewNodeResultInAllSegmentsHavingTwoBackupCopy()
+ public void AddingNewNodeResultInAllSegmentsHavingAtLeastTwoBackupCopy()
{
var yetAnotherEndPoint = NodeEndpoint.ForTest(7);
var ranges = master.Join(yetAnotherEndPoint);
master.CaughtUp(yetAnotherEndPoint, ranges.Select(x => x.Index).ToArray());
- Assert.True(master.Segments.All(x => x.Backups.Count == 2));
+ Assert.True(master.Segments.All(x => x.Backups.Count >= 2));
}
}
}
@@ -6,16 +6,16 @@ namespace Rhino.DistributedHashTable.Commands
{
public class RearrangeBackups
{
- private readonly IEnumerable<NodeEndpoint> endPoints;
+ private readonly HashSet<NodeEndpoint> endPoints;
private readonly int fairDistribution;
private readonly int numberOfBackCopiesToKeep;
private readonly IEnumerable<Segment> ranges;
private readonly List<BackupCount> currentDistribution;
public ICollection<BackUpAdded> Changed = new List<BackUpAdded>();
- public RearrangeBackups(IEnumerable<Internal.Segment> ranges,
- IEnumerable<NodeEndpoint> endPoints,
+ public RearrangeBackups(IEnumerable<Segment> ranges,
+ HashSet<NodeEndpoint> endPoints,
int numberOfBackCopiesToKeep)
{
this.ranges = ranges;
@@ -23,7 +23,7 @@ public class DistributedHashTableMaster : IDistributedHashTableMaster
private readonly HashSet<NodeEndpoint> endpoints = new HashSet<NodeEndpoint>();
- public IEnumerable<Segment> Segments
+ public Segment[] Segments
{
get { return Topology.Segments; }
}
@@ -90,30 +90,32 @@ public void CaughtUp(NodeEndpoint endPoint, params int[] caughtUpSegments)
Segment[] matchingSegments = GetMatchingSegments(caughtUpSegments, endPoint);
var modifiedSegments = from range in Segments
- join caughtUpSegment in matchingSegments on range.Version equals caughtUpSegment.Version into maybeMatchingSegment
- select
- new { range, maybeMatchingSegment };
+ join caughtUpSegment in matchingSegments on range.Index equals caughtUpSegment.Index into maybeMatchingSegment
+ select
+ new { range, maybeMatchingSegment };
Topology = new Topology((
- from modifiedSegment in modifiedSegments
- let x = modifiedSegment.maybeMatchingSegment.FirstOrDefault()
- select x == null
- ? modifiedSegment.range
- : new Segment
- {
- Index = x.Index,
- InProcessOfMovingToEndpoint = null,
- AssignedEndpoint = endPoint,
- //this is moving the current owner to the backup role
- Backups = x.Backups.Append(x.AssignedEndpoint).ToSet()
- }).ToArray()
+ from modifiedSegment in modifiedSegments
+ let x = modifiedSegment.maybeMatchingSegment.FirstOrDefault()
+ select x == null
+ ? modifiedSegment.range
+ : new Segment
+ {
+ Index = x.Index,
+ InProcessOfMovingToEndpoint = null,
+ AssignedEndpoint = endPoint,
+ Backups = x.Backups
+ .Append(x.AssignedEndpoint)
+ .Where(e => e != endPoint)
+ .ToSet()
+ }).ToArray()
);
RearrangeBackups();
TopologyChanged();
}
public void GaveUp(NodeEndpoint endpoint,
- params int[] rangesGivingUpOn)
+ params int[] rangesGivingUpOn)
{
var matchingSegments = GetMatchingSegments(rangesGivingUpOn, endpoint);
foreach (var range in matchingSegments)
@@ -123,12 +125,10 @@ public void CaughtUp(NodeEndpoint endPoint, params int[] caughtUpSegments)
}
private Segment[] GetMatchingSegments(IEnumerable<int> ranges,
- NodeEndpoint endPoint)
+ NodeEndpoint endPoint)
{
- var matchingSegments = Segments
- .Where(x => ranges.Contains(x.Index))
- .ToArray();
-
+ var matchingSegments = ranges.Select(i => Segments[i]).ToArray();
+
var rangesNotBeloningToThespecifiedEndpoint = matchingSegments
.Where(x => x.InProcessOfMovingToEndpoint != null)
.Where(x => endPoint.Equals(x.InProcessOfMovingToEndpoint) == false);
@@ -142,7 +142,7 @@ private void RearrangeBackups()
{
var rearranger = new RearrangeBackups(Segments, endpoints, NumberOfBackCopiesToKeep);
var rearranged = rearranger.Rearranging();
- if(rearranged == false)
+ if (rearranged == false)
return;
foreach (var backUpAdded in rearranger.Changed)
{
@@ -24,33 +24,40 @@ public static NodeEndpoint ForTest(int port)
public bool Equals(NodeEndpoint other)
{
- if (ReferenceEquals(null, other))
+ if(other == null)
return false;
- if (ReferenceEquals(this, other))
- return true;
- return Equals(other.Sync, Sync) && Equals(other.Async, Async);
+ return Sync == other.Sync && Async == other.Async;
}
public override string ToString()
{
return string.Format("Sync: {0}, Async: {1}", Sync, Async);
}
+ public static bool operator ==(NodeEndpoint left,
+ NodeEndpoint right)
+ {
+ return Equals(left, right);
+ }
+
+ public static bool operator !=(NodeEndpoint left,
+ NodeEndpoint right)
+ {
+ return !Equals(left, right);
+ }
+
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
if (ReferenceEquals(this, obj))
return true;
- return Equals((NodeEndpoint)obj);
+ return Equals((NodeEndpoint) obj);
}
public override int GetHashCode()
{
- unchecked
- {
- return ((Sync != null ? Sync.GetHashCode() : 0) * 397) ^ (Async != null ? Async.GetHashCode() : 0);
- }
+ return 0;
}
public byte[] ToBytes()
@@ -60,13 +60,14 @@ public static IEnumerable<T[]> Page<T>(this IEnumerable<T> self, int pageSize)
}
- public static IEnumerable<T> Append<T>(this IEnumerable<T> self, T item)
+ public static IEnumerable<T> Append<T>(this IEnumerable<T> self, T item) where T : class
{
foreach (var i in self)
{
yield return i;
}
- yield return item;
+ if (item != null)
+ yield return item;
}
public static bool Empty(this IEnumerable self)

0 comments on commit 877208b

Please sign in to comment.