-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMDBArea.Partition.cs
269 lines (215 loc) · 8.86 KB
/
MDBArea.Partition.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NFX;
using NFX.ApplicationModel;
using NFX.Environment;
using NFX.DataAccess.CRUD;
using NFX.DataAccess.Distributed;
namespace Agni.MDB
{
public abstract partial class MDBArea
{
/// <summary>
/// Represents partition within the area
/// </summary>
public sealed class Partition : MDBAppComponent, IComparable, IEquatable<Partition>, IComparable<Partition>
{
/// <summary>
/// Denotes connection types Primary/Secondary
/// </summary>
public enum ShardBackendConnection{Primary=0, Secondary}
/// <summary>
/// Represents a SHARD information for the DB particular host
/// </summary>
public sealed class Shard : MDBAppComponent, IComparable, IComparable<Shard>, IEquatable<Shard>
{
internal Shard(Partition part, IConfigSectionNode config) : base(part)
{
m_Partition = part;
m_Order = config.AttrByName(CONFIG_ORDER_ATTR).ValueAsInt(0);
PrimaryHostConnectString = ConfigStringBuilder.Build(config, CONFIG_PRIMARY_CONNECT_STRING_ATTR);
SecondaryHostConnectString = ConfigStringBuilder.Build(config, CONFIG_SECONDARY_CONNECT_STRING_ATTR);
if (PrimaryHostConnectString.IsNullOrWhiteSpace())
throw new MDBException(StringConsts.MDB_AREA_CONFIG_SHARD_CSTR_ERROR.Args(part.Area.Name, part.Order, CONFIG_PRIMARY_CONNECT_STRING_ATTR));
if (SecondaryHostConnectString.IsNullOrWhiteSpace())
throw new MDBException(StringConsts.MDB_AREA_CONFIG_SHARD_CSTR_ERROR.Args(part.Area.Name, part.Order, CONFIG_SECONDARY_CONNECT_STRING_ATTR));
}
private Partition m_Partition;
private int m_Order;
private ShardBackendConnection m_ConnectionType;
public MDBArea Area { get{ return m_Partition.Area;}}
public Partition Partition { get{ return m_Partition;}}
public int Order { get{ return m_Order;}}
public readonly string PrimaryHostConnectString;
public readonly string SecondaryHostConnectString;
/// <summary>
/// Returns Primary then secondary connect strings
/// </summary>
public IEnumerable<string> ConnectStrings
{
get
{
yield return PrimaryHostConnectString;
yield return SecondaryHostConnectString;
}
}
/// <summary>
/// Returns either primary or secondary connect string
/// depending on connection type
/// </summary>
[ExternalParameter(CoreConsts.EXT_PARAM_GROUP_DATA)]
public ShardBackendConnection ConnectionType
{
get
{
return m_ConnectionType;
}
set
{
if (m_ConnectionType!=value)
{
m_ConnectionType = value;
//todo Instrument
}
}
}
/// <summary>
/// Returns either primary or secondary connect string
/// depending on connection type
/// </summary>
public string EffectiveConnectionString
{
get
{
return m_ConnectionType==ShardBackendConnection.Primary ?
PrimaryHostConnectString : SecondaryHostConnectString;
}
}
/// <summary>
/// Returns new wrapper for operations on this shard
/// </summary>
public CRUDOperations CRUDOperations
{
get{ return new CRUDOperations(this);}
}
public override bool Equals(object obj)
{
var other = obj as Shard;
return this.Equals(other);
}
public bool Equals(Shard other)
{
if (other==null) return false;
return object.ReferenceEquals(this.m_Partition, other.m_Partition) &&
this.m_Order == other.m_Order;
}
public override int GetHashCode()
{
return m_Order.GetHashCode();
}
public override string ToString()
{
return "{0}->[{1}]".Args(m_Partition, m_Order);
}
public int CompareTo(object obj)
{
var other = obj as Shard;
return this.CompareTo(other);
}
public int CompareTo(Shard other)
{
if (other==null) return +1;
if (!object.ReferenceEquals(this.m_Partition, other.m_Partition)) return +1;
return this.m_Order.CompareTo(other.Order);
}
}
internal Partition(MDBArea area, IConfigSectionNode config) : base(area)
{
m_Area = area;
if (area is MDBCentralArea)
{
m_StartGDID = new GDID(0, 0, 0);
}
else
{
var sgdid = config.AttrByName(CONFIG_START_GDID_ATTR).ValueAsString();
if (!GDID.TryParse(sgdid, out m_StartGDID))
throw new MDBException(StringConsts.MDB_AREA_CONFIG_PARTITION_GDID_ERROR.Args(area.Name, sgdid, "unparsable, expected 'era:0:counter'"));
if (m_StartGDID.Authority!=0)
throw new MDBException(StringConsts.MDB_AREA_CONFIG_PARTITION_GDID_ERROR.Args(area.Name, sgdid, "authority segment must be 0"));
}
//Shards
var shards = new List<Shard>();
foreach(var snode in config.Children.Where( cn => cn.IsSameName(CONFIG_SHARD_SECTION)))
{
var shard = new Shard(this, snode);
shards.Add( shard );
}
if (shards.Count==0)
throw new MDBException(StringConsts.MDB_AREA_CONFIG_NO_PARTITION_SHARDS_ERROR.Args(area.Name, this.m_StartGDID));
if (shards.Count != shards.Distinct().Count())
throw new MDBException(StringConsts.MDB_AREA_CONFIG_DUPLICATE_PARTITION_SHARDS_ERROR.Args(area.Name, this.m_StartGDID));
shards.Sort();
m_Shards = shards.ToArray();
}
private MDBArea m_Area;
private int m_Order; internal void __setOrder(int num) { m_Order = num;}
private GDID m_StartGDID;
private Shard[] m_Shards;
public MDBArea Area { get{ return m_Area; }}
public int Order { get{ return m_Order; }}
public GDID StartGDID { get{ return m_StartGDID;}}
public Shard[] Shards { get{ return m_Shards; }}
/// <summary>
/// Returns CRUDOperations facade connected to the appropriate database server within this named area and partition
/// which services the shard computed from sharding id
/// </summary>
public CRUDOperations ShardedOperationsFor(object idSharding)
{
var shard = GetShardForID(idSharding);
return new CRUDOperations(shard);
}
/// <summary>
/// Finds appropriate shard for ID. See MDB.ShardingUtils
/// </summary>
public Shard GetShardForID(object idSharding)
{
ulong subid = MDB.ShardingUtils.ObjectToShardingID(idSharding);
return Shards[ subid % (ulong)Shards.Length ];
}
public override bool Equals(object obj)
{
var other = obj as Partition;
return this.Equals(other);
}
public bool Equals(Partition other)
{
if (other==null) return false;
return
object.ReferenceEquals(this.m_Area, other.m_Area) &&
GDIDRangeComparer.Instance.Compare(this.StartGDID, other.StartGDID)==0;
}
public int CompareTo(object obj)
{
var other = obj as Partition;
return this.CompareTo(other);
}
public int CompareTo(Partition other)
{
if (other==null) return +1;
if (!object.ReferenceEquals(this.m_Area, other.m_Area)) return +1;
return GDIDRangeComparer.Instance.Compare(this.StartGDID, other.StartGDID);
}
public override int GetHashCode()
{
return this.m_StartGDID.GetHashCode();
}
public override string ToString()
{
return "Area('{0}').Partition('{1}' starting '{2}' shards {3})".Args(m_Area.Name, m_Order, m_StartGDID, m_Shards.Length);
}
}//Partition
}//MDBArea
}