Skip to content

Commit

Permalink
Fixing backup behaviors to always include the previous primary as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ayenderahien committed Jun 5, 2009
1 parent 8c80a5b commit 877208b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 45 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@ namespace Rhino.DistributedHashTable.IntegrationTests
public class WhenNodeIsStarted : FullIntegrationTest, IDisposable public class WhenNodeIsStarted : FullIntegrationTest, IDisposable
{ {
private readonly DistributedHashTableMasterHost masterHost; private readonly DistributedHashTableMasterHost masterHost;
private readonly DistributedHashTableStorageHost storageHost;
private readonly Uri masterUri = new Uri("net.tcp://" + Environment.MachineName + ":2200/master"); private readonly Uri masterUri = new Uri("net.tcp://" + Environment.MachineName + ":2200/master");


public WhenNodeIsStarted() public WhenNodeIsStarted()
{ {
masterHost = new DistributedHashTableMasterHost(); masterHost = new DistributedHashTableMasterHost();
storageHost = new DistributedHashTableStorageHost(
masterUri);

masterHost.Start(); masterHost.Start();
storageHost.Start();
} }




[Fact] [Fact(Skip = "not impl yet")]
public void WillJoinMaster() public void WillJoinMaster()
{ {
Assert.True(false); Assert.True(false);
Expand Down
4 changes: 2 additions & 2 deletions Rhino.DistributedHashTable.Tests/BackCopiesBehavior.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ public OnMasterWithThreeNodes()
} }


[Fact] [Fact]
public void AddingNewNodeResultInAllSegmentsHavingTwoBackupCopy() public void AddingNewNodeResultInAllSegmentsHavingAtLeastTwoBackupCopy()
{ {
var yetAnotherEndPoint = NodeEndpoint.ForTest(7); var yetAnotherEndPoint = NodeEndpoint.ForTest(7);
var ranges = master.Join(yetAnotherEndPoint); var ranges = master.Join(yetAnotherEndPoint);
master.CaughtUp(yetAnotherEndPoint, ranges.Select(x => x.Index).ToArray()); 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));
} }
} }
} }
Expand Down
6 changes: 3 additions & 3 deletions Rhino.DistributedHashTable/Commands/RearrangeBackups.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ namespace Rhino.DistributedHashTable.Commands
{ {
public class RearrangeBackups public class RearrangeBackups
{ {
private readonly IEnumerable<NodeEndpoint> endPoints; private readonly HashSet<NodeEndpoint> endPoints;
private readonly int fairDistribution; private readonly int fairDistribution;
private readonly int numberOfBackCopiesToKeep; private readonly int numberOfBackCopiesToKeep;
private readonly IEnumerable<Segment> ranges; private readonly IEnumerable<Segment> ranges;
private readonly List<BackupCount> currentDistribution; private readonly List<BackupCount> currentDistribution;


public ICollection<BackUpAdded> Changed = new List<BackUpAdded>(); public ICollection<BackUpAdded> Changed = new List<BackUpAdded>();


public RearrangeBackups(IEnumerable<Internal.Segment> ranges, public RearrangeBackups(IEnumerable<Segment> ranges,
IEnumerable<NodeEndpoint> endPoints, HashSet<NodeEndpoint> endPoints,
int numberOfBackCopiesToKeep) int numberOfBackCopiesToKeep)
{ {
this.ranges = ranges; this.ranges = ranges;
Expand Down
46 changes: 23 additions & 23 deletions Rhino.DistributedHashTable/Internal/DistributedHashTableMaster.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DistributedHashTableMaster : IDistributedHashTableMaster


private readonly HashSet<NodeEndpoint> endpoints = new HashSet<NodeEndpoint>(); private readonly HashSet<NodeEndpoint> endpoints = new HashSet<NodeEndpoint>();


public IEnumerable<Segment> Segments public Segment[] Segments
{ {
get { return Topology.Segments; } get { return Topology.Segments; }
} }
Expand Down Expand Up @@ -90,30 +90,32 @@ public void CaughtUp(NodeEndpoint endPoint, params int[] caughtUpSegments)
Segment[] matchingSegments = GetMatchingSegments(caughtUpSegments, endPoint); Segment[] matchingSegments = GetMatchingSegments(caughtUpSegments, endPoint);


var modifiedSegments = from range in Segments var modifiedSegments = from range in Segments
join caughtUpSegment in matchingSegments on range.Version equals caughtUpSegment.Version into maybeMatchingSegment join caughtUpSegment in matchingSegments on range.Index equals caughtUpSegment.Index into maybeMatchingSegment
select select
new { range, maybeMatchingSegment }; new { range, maybeMatchingSegment };


Topology = new Topology(( Topology = new Topology((
from modifiedSegment in modifiedSegments from modifiedSegment in modifiedSegments
let x = modifiedSegment.maybeMatchingSegment.FirstOrDefault() let x = modifiedSegment.maybeMatchingSegment.FirstOrDefault()
select x == null select x == null
? modifiedSegment.range ? modifiedSegment.range
: new Segment : new Segment
{ {
Index = x.Index, Index = x.Index,
InProcessOfMovingToEndpoint = null, InProcessOfMovingToEndpoint = null,
AssignedEndpoint = endPoint, AssignedEndpoint = endPoint,
//this is moving the current owner to the backup role Backups = x.Backups
Backups = x.Backups.Append(x.AssignedEndpoint).ToSet() .Append(x.AssignedEndpoint)
}).ToArray() .Where(e => e != endPoint)
.ToSet()
}).ToArray()
); );
RearrangeBackups(); RearrangeBackups();
TopologyChanged(); TopologyChanged();
} }


public void GaveUp(NodeEndpoint endpoint, public void GaveUp(NodeEndpoint endpoint,
params int[] rangesGivingUpOn) params int[] rangesGivingUpOn)
{ {
var matchingSegments = GetMatchingSegments(rangesGivingUpOn, endpoint); var matchingSegments = GetMatchingSegments(rangesGivingUpOn, endpoint);
foreach (var range in matchingSegments) foreach (var range in matchingSegments)
Expand All @@ -123,12 +125,10 @@ public void GaveUp(NodeEndpoint endpoint,
} }


private Segment[] GetMatchingSegments(IEnumerable<int> ranges, private Segment[] GetMatchingSegments(IEnumerable<int> ranges,
NodeEndpoint endPoint) NodeEndpoint endPoint)
{ {
var matchingSegments = Segments var matchingSegments = ranges.Select(i => Segments[i]).ToArray();
.Where(x => ranges.Contains(x.Index))
.ToArray();

var rangesNotBeloningToThespecifiedEndpoint = matchingSegments var rangesNotBeloningToThespecifiedEndpoint = matchingSegments
.Where(x => x.InProcessOfMovingToEndpoint != null) .Where(x => x.InProcessOfMovingToEndpoint != null)
.Where(x => endPoint.Equals(x.InProcessOfMovingToEndpoint) == false); .Where(x => endPoint.Equals(x.InProcessOfMovingToEndpoint) == false);
Expand All @@ -142,7 +142,7 @@ private void RearrangeBackups()
{ {
var rearranger = new RearrangeBackups(Segments, endpoints, NumberOfBackCopiesToKeep); var rearranger = new RearrangeBackups(Segments, endpoints, NumberOfBackCopiesToKeep);
var rearranged = rearranger.Rearranging(); var rearranged = rearranger.Rearranging();
if(rearranged == false) if (rearranged == false)
return; return;
foreach (var backUpAdded in rearranger.Changed) foreach (var backUpAdded in rearranger.Changed)
{ {
Expand Down
25 changes: 16 additions & 9 deletions Rhino.DistributedHashTable/Internal/NodeEndpoint.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,33 +24,40 @@ public static NodeEndpoint ForTest(int port)


public bool Equals(NodeEndpoint other) public bool Equals(NodeEndpoint other)
{ {
if (ReferenceEquals(null, other)) if(other == null)
return false; return false;
if (ReferenceEquals(this, other)) return Sync == other.Sync && Async == other.Async;
return true;
return Equals(other.Sync, Sync) && Equals(other.Async, Async);
} }


public override string ToString() public override string ToString()
{ {
return string.Format("Sync: {0}, Async: {1}", Sync, Async); 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) public override bool Equals(object obj)
{ {
if (ReferenceEquals(null, obj)) if (ReferenceEquals(null, obj))
return false; return false;
if (ReferenceEquals(this, obj)) if (ReferenceEquals(this, obj))
return true; return true;
return Equals((NodeEndpoint)obj); return Equals((NodeEndpoint) obj);
} }


public override int GetHashCode() public override int GetHashCode()
{ {
unchecked return 0;
{
return ((Sync != null ? Sync.GetHashCode() : 0) * 397) ^ (Async != null ? Async.GetHashCode() : 0);
}
} }


public byte[] ToBytes() public byte[] ToBytes()
Expand Down
5 changes: 3 additions & 2 deletions Rhino.DistributedHashTable/Util/EnumerableExtensions.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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) foreach (var i in self)
{ {
yield return i; yield return i;
} }
yield return item; if (item != null)
yield return item;
} }


public static bool Empty(this IEnumerable self) public static bool Empty(this IEnumerable self)
Expand Down

0 comments on commit 877208b

Please sign in to comment.