forked from ravendb/ravendb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WhenUsingShardedServers.cs
210 lines (173 loc) · 5.94 KB
/
WhenUsingShardedServers.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="WhenUsingShardedServers.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Threading;
using Raven.Client;
using Raven.Client.Document;
using Raven.Database.Extensions;
using Raven.Database.Server;
using Raven.Server;
using Raven.Tests.Document;
using Rhino.Mocks;
using Xunit;
using System.Collections.Generic;
using Raven.Client.Shard;
using System.Linq;
namespace Raven.Tests.Shard
{
using Raven.Abstractions.Data;
public class WhenUsingShardedServers : RemoteClientTest, IDisposable
{
readonly string path1;
readonly string path2;
readonly RavenDbServer server1;
readonly RavenDbServer server2;
readonly Company company1;
readonly Company company2;
readonly IDictionary<string, IDocumentStore> shards;
readonly IShardResolutionStrategy shardResolution;
readonly ShardStrategy shardStrategy;
public WhenUsingShardedServers()
{
const string server = "localhost";
const int port1 = 8079;
const int port2 = 8081;
path1 = GetPath("TestShardedDb1");
path2 = GetPath("TestShardedDb2");
NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(port1);
NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(port2);
company1 = new Company { Name = "Company1" };
company2 = new Company { Name = "Company2" };
server1 = GetNewServer(port1, path1);
server2 = GetNewServer(port2, path2);
shards = new List<IDocumentStore> {
new DocumentStore { Identifier="Shard1", Url = "http://" + server +":"+port1},
new DocumentStore { Identifier="Shard2", Url = "http://" + server +":"+port2}
}.ToDictionary(x => x.Identifier, x => x);
shardResolution = MockRepository.GenerateStub<IShardResolutionStrategy>();
shardResolution.Stub(x => x.GenerateShardIdFor(Arg.Is(company1), Arg<ITransactionalDocumentSession>.Is.Anything)).Return("Shard1");
shardResolution.Stub(x => x.GenerateShardIdFor(Arg.Is(company2), Arg<ITransactionalDocumentSession>.Is.Anything)).Return("Shard2");
shardResolution.Stub(x => x.MetadataShardIdFor(company1)).Return("Shard1");
shardResolution.Stub(x => x.MetadataShardIdFor(company2)).Return("Shard1");
shardStrategy = new ShardStrategy(shards) { ShardResolutionStrategy = shardResolution };
}
[Fact]
public void CanOverrideTheShardIdGeneration()
{
using (var documentStore = new ShardedDocumentStore(shardStrategy))
{
documentStore.Initialize();
foreach (var shard in shards)
{
shard.Value.Conventions.DocumentKeyGenerator = (dbName, cmds, c) => ((Company)c).Name;
}
using (var session = documentStore.OpenSession())
{
session.Store(company1);
session.Store(company2);
Assert.Equal("Shard1/companies/1", company1.Id);
Assert.Equal("Shard2/companies/2", company2.Id);
}
}
}
[Fact]
public void CanQueryUsingInt()
{
shardStrategy.ShardAccessStrategy = new SequentialShardAccessStrategy();
using (var documentStore = new ShardedDocumentStore(shardStrategy))
{
documentStore.Initialize();
using (var session = documentStore.OpenSession())
{
session.Load<Company>(1);
}
}
}
[Fact]
public void CanInsertIntoTwoShardedServers()
{
using (var documentStore = new ShardedDocumentStore(shardStrategy))
{
documentStore.Initialize();
using (var session = documentStore.OpenSession())
{
session.Store(company1);
session.Store(company2);
session.SaveChanges();
}
}
}
[Fact]
public void CanGetSingleEntityFromCorrectShardedServer()
{
using (var documentStore = new ShardedDocumentStore(shardStrategy).Initialize())
using (var session = documentStore.OpenSession())
{
//store item that goes in 2nd shard
session.Store(company2);
session.SaveChanges();
//get it, should automagically retrieve from 2nd shard
shardResolution.Stub(x => x.PotentialShardsFor(null)).IgnoreArguments().Return(new[] { "Shard2" });
var loadedCompany = session.Load<Company>(company2.Id);
Assert.NotNull(loadedCompany);
Assert.Equal(company2.Name, loadedCompany.Name);
}
}
[Fact]
public void CanGetSingleEntityFromCorrectShardedServerWhenLocationIsUnknown()
{
shardStrategy.ShardAccessStrategy = new SequentialShardAccessStrategy();
using (var documentStore = new ShardedDocumentStore(shardStrategy).Initialize())
using (var session = documentStore.OpenSession())
{
//store item that goes in 2nd shard
session.Store(company2);
session.SaveChanges();
//get it, should try all shards and find it
shardResolution.Stub(x => x.PotentialShardsFor(null)).IgnoreArguments().Return(null);
var loadedCompany = session.Load<Company>(company2.Id);
Assert.NotNull(loadedCompany);
Assert.Equal(company2.Name, loadedCompany.Name);
}
}
[Fact]
public void CanGetAllShardedEntities()
{
//get them in simple single threaded sequence for this test
shardStrategy.ShardAccessStrategy = new SequentialShardAccessStrategy();
using (var documentStore = new ShardedDocumentStore(shardStrategy).Initialize())
using (var session = documentStore.OpenSession())
{
//store 2 items in 2 shards
session.Store(company1);
session.Store(company2);
session.SaveChanges();
//get all, should automagically retrieve from each shard
var allCompanies = session.Advanced.LuceneQuery<Company>()
.WaitForNonStaleResults()
.ToArray();
Assert.NotNull(allCompanies);
Assert.Equal(company1.Name, allCompanies[0].Name);
Assert.Equal(company2.Name, allCompanies[1].Name);
}
}
public override void Dispose()
{
server1.Dispose();
server2.Dispose();
Thread.Sleep(100);
foreach (var path in new[] { path1, path2 })
{
try
{
IOExtensions.DeleteDirectory(path);
}
catch (Exception) { }
}
base.Dispose();
}
}
}