-
Notifications
You must be signed in to change notification settings - Fork 640
/
TwoPhaseCommitTool.cs
201 lines (190 loc) · 7.75 KB
/
TwoPhaseCommitTool.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
using System;
using System.IO;
#if FEATURE_SERIALIZABLE_EXCEPTIONS
using System.Runtime.Serialization;
#endif
namespace Lucene.Net.Index
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <summary>
/// A utility for executing 2-phase commit on several objects.
/// <para/>
/// @lucene.experimental
/// </summary>
/// <seealso cref="ITwoPhaseCommit"/>
public sealed class TwoPhaseCommitTool
{
/// <summary>
/// No instance </summary>
private TwoPhaseCommitTool()
{
}
/// <summary>
/// Thrown by <see cref="TwoPhaseCommitTool.Execute(ITwoPhaseCommit[])"/> when an
/// object fails to <see cref="ITwoPhaseCommit.PrepareCommit()"/>.
/// </summary>
// LUCENENET: It is no longer good practice to use binary serialization.
// See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568
#if FEATURE_SERIALIZABLE_EXCEPTIONS
[Serializable]
#endif
public class PrepareCommitFailException
: IOException
{
/// <summary>
/// Sole constructor. </summary>
public PrepareCommitFailException(Exception cause, ITwoPhaseCommit obj)
: base("prepareCommit() failed on " + obj, cause)
{
}
// LUCENENET: For testing purposes
internal PrepareCommitFailException(string message)
: base(message)
{
}
#if FEATURE_SERIALIZABLE_EXCEPTIONS
/// <summary>
/// Initializes a new instance of this class with serialized data.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
protected PrepareCommitFailException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif
}
/// <summary>
/// Thrown by <see cref="TwoPhaseCommitTool.Execute(ITwoPhaseCommit[])"/> when an
/// object fails to <see cref="ITwoPhaseCommit.Commit()"/>.
/// </summary>
// LUCENENET: It is no longer good practice to use binary serialization.
// See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568
#if FEATURE_SERIALIZABLE_EXCEPTIONS
[Serializable]
#endif
public class CommitFailException : IOException
{
/// <summary>
/// Sole constructor. </summary>
public CommitFailException(Exception cause, ITwoPhaseCommit obj)
: base("commit() failed on " + obj, cause)
{
}
// LUCENENET: For testing purposes
internal CommitFailException(string message)
: base(message)
{
}
#if FEATURE_SERIALIZABLE_EXCEPTIONS
/// <summary>
/// Initializes a new instance of this class with serialized data.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
protected CommitFailException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif
}
/// <summary>
/// Rollback all objects, discarding any exceptions that occur. </summary>
private static void Rollback(params ITwoPhaseCommit[] objects)
{
foreach (ITwoPhaseCommit tpc in objects)
{
// ignore any exception that occurs during rollback - we want to ensure
// all objects are rolled-back.
if (tpc != null)
{
try
{
tpc.Rollback();
}
catch (Exception t) when (t.IsThrowable())
{
// ignore
}
}
}
}
/// <summary>
/// Executes a 2-phase commit algorithm by first
/// <see cref="ITwoPhaseCommit.PrepareCommit()"/> all objects and only if all succeed,
/// it proceeds with <see cref="ITwoPhaseCommit.Commit()"/>. If any of the objects
/// fail on either the preparation or actual commit, it terminates and
/// <see cref="ITwoPhaseCommit.Rollback()"/> all of them.
/// <para/>
/// <b>NOTE:</b> It may happen that an object fails to commit, after few have
/// already successfully committed. This tool will still issue a rollback
/// instruction on them as well, but depending on the implementation, it may
/// not have any effect.
/// <para/>
/// <b>NOTE:</b> if any of the objects are <c>null</c>, this method simply
/// skips over them.
/// </summary>
/// <exception cref="PrepareCommitFailException">
/// if any of the objects fail to
/// <see cref="ITwoPhaseCommit.PrepareCommit()"/> </exception>
/// <exception cref="CommitFailException">
/// if any of the objects fail to <see cref="ITwoPhaseCommit.Commit()"/> </exception>
public static void Execute(params ITwoPhaseCommit[] objects)
{
ITwoPhaseCommit tpc = null;
try
{
// first, all should successfully prepareCommit()
for (int i = 0; i < objects.Length; i++)
{
tpc = objects[i];
if (tpc != null)
{
tpc.PrepareCommit();
}
}
}
catch (Exception t) when (t.IsThrowable())
{
// first object that fails results in rollback all of them and
// throwing an exception.
Rollback(objects);
throw new PrepareCommitFailException(t, tpc);
}
// If all successfully prepareCommit(), attempt the actual commit()
try
{
for (int i = 0; i < objects.Length; i++)
{
tpc = objects[i];
if (tpc != null)
{
tpc.Commit();
}
}
}
catch (Exception t) when (t.IsThrowable())
{
// first object that fails results in rollback all of them and
// throwing an exception.
Rollback(objects);
throw new CommitFailException(t, tpc);
}
}
}
}