/
RoundRobinSharding.cs
210 lines (180 loc) · 5.29 KB
/
RoundRobinSharding.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// -----------------------------------------------------------------------
// <copyright file="RoundRobinSharding.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using Raven.Client;
using Raven.Client.Connection;
using Raven.Client.Document;
using Raven.Client.Shard;
using Raven.Server;
using Xunit;
using Raven.Client.Linq;
using System.Linq;
namespace Raven.Tests.Shard
{
public class RoundRobinSharding : RavenTest
{
private readonly Dictionary<string, RavenDbServer> servers;
private readonly ShardedDocumentStore store;
private Dictionary<string, IDocumentStore> documentStores;
public RoundRobinSharding()
{
servers = new Dictionary<string, RavenDbServer>
{
{"one",GetNewServer(8078)},
{"two", GetNewServer(8077)},
{"tri", GetNewServer(8076)}
};
documentStores = new Dictionary<string, IDocumentStore>
{
{"one", new DocumentStore{Url = "http://localhost:8078"}},
{"two", new DocumentStore{Url = "http://localhost:8077"}},
{"tri", new DocumentStore{Url = "http://localhost:8076"}},
};
foreach (var documentStore in documentStores)
{
documentStore.Value.Conventions.FailoverBehavior = FailoverBehavior.FailImmediately;
}
var shardStrategy = new ShardStrategy(documentStores)
.ShardingOn<Post>()
.ShardingOn<PostComments>(x => x.PostId);
store = new ShardedDocumentStore(shardStrategy);
store.Initialize();
}
public override void Dispose()
{
foreach (var ravenDbServer in servers)
{
ravenDbServer.Value.Dispose();
}
store.Dispose();
base.Dispose();
}
[Fact]
public void SavingTwoPostsWillGoToTwoDifferentServers()
{
using(var session = store.OpenSession())
{
var p1 = new Post();
session.Store(p1);
var p2 = new Post();
session.Store(p2);
session.SaveChanges();
Assert.Equal("tri/posts/1", p1.Id);
Assert.Equal("two/posts/2", p2.Id);
}
}
[Fact]
public void WhenQueryingWillGoToTheRightServer()
{
using (var session = store.OpenSession())
{
var p1 = new Post();
session.Store(p1);
var pc1 = new PostComments {PostId = p1.Id};
session.Store(pc1);
session.SaveChanges();
}
foreach (var ravenDbServer in servers)
{
ravenDbServer.Value.Server.ResetNumberOfRequests();
}
using (var session = store.OpenSession())
{
var posts = session.Query<PostComments>().Where(x => x.PostId == "tri/posts/1").ToList();
Assert.NotEmpty(posts);
}
Assert.Equal(0, servers["one"].Server.NumberOfRequests);
Assert.Equal(0, servers["two"].Server.NumberOfRequests);
Assert.Equal(1, servers["tri"].Server.NumberOfRequests);
}
[Fact]
public void WhenQueryingWillGoToTheRightServer_UsingQueryById()
{
using (var session = store.OpenSession())
{
var p1 = new Post();
session.Store(p1);
var pc1 = new PostComments { PostId = p1.Id };
session.Store(pc1);
session.SaveChanges();
}
foreach (var ravenDbServer in servers)
{
ravenDbServer.Value.Server.ResetNumberOfRequests();
}
using (var session = store.OpenSession())
{
var posts = session.Query<Post>().Where(x => x.Id == "tri/posts/1").ToList();
Assert.NotEmpty(posts);
}
Assert.Equal(0, servers["one"].Server.NumberOfRequests);
Assert.Equal(0, servers["two"].Server.NumberOfRequests);
Assert.Equal(1, servers["tri"].Server.NumberOfRequests);
}
[Fact]
public void WhenQueryingWillGoToTheRightServer_Loading()
{
using (var session = store.OpenSession())
{
var p1 = new Post();
session.Store(p1);
var pc1 = new PostComments { PostId = p1.Id };
session.Store(pc1);
session.SaveChanges();
}
foreach (var ravenDbServer in servers)
{
ravenDbServer.Value.Server.ResetNumberOfRequests();
}
using (var session = store.OpenSession())
{
Assert.NotNull(session.Load<Post>("tri/posts/1"));
Assert.NotNull(session.Load<PostComments>("tri/PostComments/1"));
}
Assert.Equal(0, servers["one"].Server.NumberOfRequests);
Assert.Equal(0, servers["two"].Server.NumberOfRequests);
Assert.Equal(2, servers["tri"].Server.NumberOfRequests);
}
[Fact]
public void WillGetGoodLocalityOfReference()
{
using (var session = store.OpenSession())
{
var p1 = new Post();
session.Store(p1);
var p2 = new Post();
session.Store(p2);
var pc1 = new PostComments
{
PostId = p1.Id
};
session.Store(pc1);
var pc2 = new PostComments
{
PostId = p2.Id
};
session.Store(pc2);
session.SaveChanges();
Assert.Equal("tri/posts/1", p1.Id);
Assert.Equal("two/posts/2", p2.Id);
Assert.Equal("tri/PostComments/1", pc1.Id);
Assert.Equal("two/PostComments/2", pc2.Id);
}
}
public class Post
{
public string Id { get; set; }
public DateTime PublishedAt { get; set; }
}
public class PostComments
{
public string Id { get; set; }
public string PostId { get; set; }
public string[] Comments { get; set; }
}
}
}