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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* 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.
*/
use crate::core::util::close::{Closeable, CloseableRef};
use crate::core::util::error::lucene_error::{LuceneError, Result};
use parking_lot::{ReentrantMutex, ReentrantMutexGuard, RwLock};
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::sync::Arc;
const REFERENCE_MANAGER_IS_CLOSED_MSG: &str = "this ReferenceManager is closed";
pub type RefreshListenerArc = Arc<dyn RefreshListener>;
/// Utility struct for safely sharing instances of a certain type across multiple threads, while
/// periodically refreshing them. This struct ensures each reference is closed only once all
/// threads have finished using it. Consult the documentation of [`ReferenceManager`]
/// implementations for their [`maybe_refresh`](Self::maybe_refresh) semantics.
///
/// `G` is the concrete type that will be [`acquire`](Self::acquire)d and
/// [`release`](Self::release)d.
///
/// @lucene.experimental
pub struct ReferenceManager<G, B> {
current: RwLock<Option<Arc<G>>>,
reference_lock: ReentrantMutex<()>,
refresh_lock: ReentrantMutex<()>,
refresh_listeners: RwLock<Vec<RefreshListenerArc>>,
base: B,
}
impl<G, B> ReferenceManager<G, B> {
pub(crate) fn new(current: G, base: B) -> Self {
Self {
current: RwLock::new(Some(Arc::new(current))),
reference_lock: ReentrantMutex::new(()),
refresh_lock: ReentrantMutex::new(()),
refresh_listeners: RwLock::new(Vec::new()),
base,
}
}
}
impl<G, B> ReferenceManager<G, B>
where
B: ReferenceManagerBase<G>,
{
fn ensure_open(&self) -> Result<()> {
if self.current.read().is_none() {
return Err(LuceneError::already_closed(
REFERENCE_MANAGER_IS_CLOSED_MSG.to_string(),
));
}
Ok(())
}
fn swap_reference(&self, new_reference: Option<Arc<G>>) -> Result<()> {
let _reference_lock = self.reference_lock.lock();
let old_reference = {
let mut current = self.current.write();
let old_reference = current
.take()
.ok_or_else(|| LuceneError::already_closed(REFERENCE_MANAGER_IS_CLOSED_MSG.to_string()))?;
*current = new_reference;
old_reference
};
self.release(old_reference)
}
/// Obtains the current reference. Every call to `acquire` must be matched with one call to
/// [`release`](Self::release). It is best to release in a finally-equivalent path and stop using
/// the reference after it has been released.
///
/// # Errors
///
/// Returns an already-closed error if the reference manager has been closed.
pub fn acquire(&self) -> Result<Arc<G>> {
loop {
let reference =
self.current.read().as_ref().cloned().ok_or_else(|| {
LuceneError::already_closed(REFERENCE_MANAGER_IS_CLOSED_MSG.to_string())
})?;
if self.base.try_inc_ref(reference.as_ref())? {
return Ok(reference);
}
if self.base.get_ref_count(reference.as_ref()) == 0
&& self
.current
.read()
.as_ref()
.is_some_and(|current| Arc::ptr_eq(current, &reference))
{
/* if we can't increment the reader but we are
still the current reference the RM is in a
illegal state since we can't make any progress
anymore. The reference is closed but the RM still
holds on to it as the actual instance.
This can only happen if somebody outside of the RM
decrements the refcount without a corresponding increment
since the RM assigns the new reference before counting down
the reference. */
return Err(LuceneError::illegal_state(
"The managed reference has already closed - this is likely a bug when the reference count is modified outside of the ReferenceManager",
));
}
}
}
/// Closes this [`ReferenceManager`] to prevent future acquiring. The managed resource might not
/// be released immediately if a user is holding a previously acquired reference. The resource
/// will be released once the last reference is released. Those references can still be used as
/// if the manager were active.
///
/// Calling this method more than once has no effect.
///
/// # Errors
///
/// Returns an error if the current reference could not be closed.
pub fn close(&self) -> Result<()> {
let _reference_lock = self.reference_lock.lock();
if self.current.read().is_some() {
// Make sure we can call this more than once. Closeable's contract says that invoking close
// on an already-closed resource has no effect.
self.swap_reference(None)?;
self.base.after_close()?;
}
Ok(())
}
fn do_maybe_refresh(&self, _refresh_guard: &ReentrantMutexGuard<'_, ()>) -> Result<()> {
// Both callers retain the refresh lock through this operation. Borrow their guard rather
// than taking the same reentrant lock again.
let reference = self.acquire()?;
let mut refreshed = false;
let refresh_result = catch_unwind(AssertUnwindSafe(|| -> Result<()> {
self.notify_refresh_listeners_before()?;
if let Some(new_reference) = self.base.refresh_if_needed(reference.as_ref())? {
let new_reference = Arc::new(new_reference);
debug_assert!(
!Arc::ptr_eq(&new_reference, &reference),
"refresh_if_needed should return None if refresh wasn't needed"
);
let swap_result = catch_unwind(AssertUnwindSafe(|| {
self.swap_reference(Some(new_reference.clone()))
}));
if !matches!(&swap_result, Ok(Ok(()))) {
self.release(new_reference)?;
}
unwrap_caught_result!(swap_result)?;
refreshed = true;
}
Ok(())
}));
let finally_result = (|| -> Result<()> {
self.release(reference)?;
self.notify_refresh_listeners_refreshed(refreshed)
})();
finally_result?;
unwrap_caught_result!(refresh_result)?;
self.base.after_maybe_refresh()
}
/// Call this, or [`maybe_refresh_blocking`](Self::maybe_refresh_blocking), periodically if
/// [`acquire`](Self::acquire) should return refreshed instances.
///
/// It is safe for more than one thread to call this at once. Only the first thread attempts the
/// refresh; subsequent threads return immediately. A return value of `true` means the calling
/// thread either refreshed or there were no changes to refresh. A return value of `false` means
/// another thread is currently refreshing.
///
/// # Errors
///
/// Returns an error if refreshing the resource fails or the reference manager has been closed.
pub fn maybe_refresh(&self) -> Result<bool> {
self.ensure_open()?;
// Ensure only one thread refreshes at once; other threads return immediately.
let Some(refresh_lock) = self.refresh_lock.try_lock() else {
return Ok(false);
};
self.do_maybe_refresh(&refresh_lock)?;
Ok(true)
}
/// Call this, or [`maybe_refresh`](Self::maybe_refresh), periodically if
/// [`acquire`](Self::acquire) should return refreshed instances.
///
/// Unlike [`maybe_refresh`](Self::maybe_refresh), this method blocks until another thread's
/// refresh completes. It is useful when the next call to [`acquire`](Self::acquire) must return a
/// refreshed instance.
///
/// # Errors
///
/// Returns an error if refreshing the resource fails or the reference manager has been closed.
pub fn maybe_refresh_blocking(&self) -> Result<()> {
self.ensure_open()?;
// Ensure only one thread refreshes at once.
let refresh_lock = self.refresh_lock.lock();
self.do_maybe_refresh(&refresh_lock)
}
/// Releases a reference previously obtained via [`acquire`](Self::acquire).
///
/// It is safe to call this after [`close`](Self::close).
///
/// # Errors
///
/// Returns an error if releasing the resource fails.
pub fn release(&self, reference: Arc<G>) -> Result<()> {
self.base.dec_ref(reference.as_ref())
}
fn notify_refresh_listeners_before(&self) -> Result<()> {
let refresh_listeners = self.refresh_listeners.read().clone();
for refresh_listener in refresh_listeners {
refresh_listener.before_refresh()?;
}
Ok(())
}
fn notify_refresh_listeners_refreshed(&self, did_refresh: bool) -> Result<()> {
let refresh_listeners = self.refresh_listeners.read().clone();
for refresh_listener in refresh_listeners {
refresh_listener.after_refresh(did_refresh)?;
}
Ok(())
}
/// Adds a listener to be notified when a reference is refreshed or swapped.
pub fn add_listener(&self, listener: RefreshListenerArc) {
self.refresh_listeners.write().push(listener);
}
/// Removes a listener added with [`add_listener`](Self::add_listener).
pub fn remove_listener(&self, listener: &RefreshListenerArc) {
let mut refresh_listeners = self.refresh_listeners.write();
if let Some(index) = refresh_listeners
.iter()
.position(|current| Arc::ptr_eq(current, listener))
{
refresh_listeners.remove(index);
}
}
}
impl<G, B> Closeable for ReferenceManager<G, B>
where
B: ReferenceManagerBase<G>,
{
fn close(&mut self) -> Result<()> {
ReferenceManager::close(self)
}
}
impl<G, B> CloseableRef for ReferenceManager<G, B>
where
B: ReferenceManagerBase<G>,
{
fn close(&self) -> Result<()> {
ReferenceManager::close(self)
}
}
/// Operations supplied by a concrete [`ReferenceManager`] implementation for its managed
/// reference type.
pub trait ReferenceManagerBase<G> {
/// Decrements reference counting on the given reference.
///
/// # Errors
///
/// Returns an error if reference decrement on the given resource failed.
fn dec_ref(&self, reference: &G) -> Result<()>;
/// Refreshes the given reference if needed. Returns `None` if no refresh was needed, otherwise
/// a new refreshed reference.
///
/// # Errors
///
/// Returns an already-closed error if the reference manager has been closed, or an I/O error if
/// the refresh operation failed.
fn refresh_if_needed(&self, reference_to_refresh: &G) -> Result<Option<G>>;
/// Tries to increment reference counting on the given reference. Returns `true` if the operation
/// was successful.
///
/// # Errors
///
/// Returns an already-closed error if the reference manager has been closed.
fn try_inc_ref(&self, reference: &G) -> Result<bool>;
/// Returns the current reference count of the given reference.
fn get_ref_count(&self, reference: &G) -> i32;
/// Called after `close()`, so an implementation can free any resources.
///
/// # Errors
///
/// Returns an error if the after-close operation in an implementation fails.
fn after_close(&self) -> Result<()> {
Ok(())
}
/// Called after a refresh was attempted, regardless of whether a new reference was in fact
/// created.
///
/// # Errors
///
/// Returns an error if a low-level I/O error occurs.
fn after_maybe_refresh(&self) -> Result<()> {
Ok(())
}
}
/// Use to receive notification when a refresh has finished. See
/// [`ReferenceManager::add_listener`].
pub trait RefreshListener: Send + Sync {
/// Called right before a refresh attempt starts.
fn before_refresh(&self) -> Result<()>;
/// Called after the attempted refresh. If the refresh opened a new reference,
/// `did_refresh` is `true` and [`ReferenceManager::acquire`] is guaranteed to return the new
/// reference.
fn after_refresh(&self, did_refresh: bool) -> Result<()>;
}