-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathPartitionResolver.cs
189 lines (161 loc) · 6.33 KB
/
PartitionResolver.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
//------------------------------------------------------------------------------
// <copyright file="PartitionResolver.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web {
using System;
using System.Configuration;
using System.Collections;
using System.Threading;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Web.Util;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Security.Principal;
using System.Xml;
using System.Collections.Specialized;
using System.Configuration.Provider;
using System.Globalization;
using System.Web.Management;
using System.Web.Hosting;
using System.Web.Configuration;
using System.Security.Permissions;
public interface IPartitionResolver {
void Initialize();
string ResolvePartition(Object key);
}
internal interface IPartitionInfo {
string GetTracingPartitionString();
}
internal delegate IPartitionInfo CreatePartitionInfo(string connectionString);
class PartitionManager : IDisposable {
internal PartitionManager(CreatePartitionInfo createCallback) {
_createCallback = createCallback;
}
HybridDictionary _partitions = new HybridDictionary();
ReaderWriterLock _lock = new ReaderWriterLock();
CreatePartitionInfo _createCallback;
internal object GetPartition(IPartitionResolver partitionResolver, string id) {
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure))
EtwTrace.Trace(EtwTraceType.ETW_TYPE_SESSIONSTATE_PARTITION_START, HttpContext.Current.WorkerRequest, partitionResolver.GetType().FullName, id);
string partitionString = null;
string errorMessage = null;
IPartitionInfo partitionInfo = null;
try {
try {
partitionString = partitionResolver.ResolvePartition(id);
if (partitionString == null) {
throw new HttpException(
SR.GetString(SR.Bad_partition_resolver_connection_string, partitionResolver.GetType().FullName));
}
}
catch (Exception e) {
errorMessage = e.Message;
throw;
}
try {
_lock.AcquireReaderLock(-1);
partitionInfo = (IPartitionInfo)_partitions[partitionString];
if (partitionInfo != null) {
Debug.Trace("PartitionManager", "id=" + id + "; partitionString=" + partitionString);
return partitionInfo;
}
}
finally {
if (_lock.IsReaderLockHeld) {
_lock.ReleaseReaderLock();
}
}
// Not found. Has to add it.
try {
_lock.AcquireWriterLock(-1);
// One more time
partitionInfo = (IPartitionInfo)_partitions[partitionString];
if (partitionInfo == null) {
partitionInfo = _createCallback(partitionString);
Debug.Trace("PartitionManager", "Add a new partition; id=" + id + "; partitionString=" + partitionString);
_partitions.Add(partitionString, partitionInfo);
}
Debug.Trace("PartitionManager", "id=" + id + "; partitionString=" + partitionString);
return partitionInfo;
}
finally {
if (_lock.IsWriterLockHeld) {
_lock.ReleaseWriterLock();
}
}
}
finally {
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) {
string msg = errorMessage;
if (msg == null) {
if (partitionInfo != null) {
msg = partitionInfo.GetTracingPartitionString();
}
else {
msg = String.Empty;
}
}
EtwTrace.Trace(EtwTraceType.ETW_TYPE_SESSIONSTATE_PARTITION_END, HttpContext.Current.WorkerRequest, msg);
}
}
}
public void Dispose() {
if (_partitions == null) {
return;
}
try {
_lock.AcquireWriterLock(-1);
if (_partitions != null) {
foreach (PartitionInfo partitionInfo in _partitions.Values) {
partitionInfo.Dispose();
}
_partitions = null;
}
}
catch {
// ignore exceptions in dispose
}
finally {
if (_lock.IsWriterLockHeld) {
_lock.ReleaseWriterLock();
}
}
}
}
internal class PartitionInfo : IDisposable, IPartitionInfo {
ResourcePool _rpool;
internal PartitionInfo(ResourcePool rpool) {
_rpool = rpool;
}
internal object RetrieveResource() {
return _rpool.RetrieveResource();
}
internal void StoreResource(IDisposable o) {
_rpool.StoreResource(o);
}
protected virtual string TracingPartitionString {
get {
return String.Empty;
}
}
string IPartitionInfo.GetTracingPartitionString() {
return TracingPartitionString;
}
public void Dispose() {
if (_rpool == null) {
return;
}
lock (this) {
if (_rpool != null) {
_rpool.Dispose();
_rpool = null;
}
}
}
};
}