mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

opends
28.11.2006 eda79366f0bdacebb6fca64c8e472538c9b16798
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2006 Sun Microsystems, Inc.
 */
package org.opends.server.core;
 
 
 
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
 
import org.opends.server.types.DebugLogCategory;
import org.opends.server.types.DebugLogSeverity;
import org.opends.server.types.DN;
 
import static org.opends.server.loggers.Debug.*;
import static org.opends.server.util.StaticUtils.*;
 
 
 
/**
 * This class defines a Directory Server component that can keep track of all
 * locks needed throughout the Directory Server.  It is intended primarily for
 * entry locking but support for other types of objects might be added in the
 * future.
 */
public class LockManager
{
  /**
   * The fully-qualified name of this class for debugging purposes.
   */
  private static final String CLASS_NAME =
       "org.opends.server.core.LockManager";
 
 
 
  /**
   * The number of buckets into which the set of global DN locks will be
   * broken.
   */
  public static final int NUM_GLOBAL_DN_LOCKS =
       (10 * Runtime.getRuntime().availableProcessors());
 
 
 
  /**
   * The initial capacity to use for the DN lock hashtable.
   */
  public static final int DN_TABLE_INITIAL_SIZE = 50;
 
 
 
  /**
   * The load factor to use for the DN lock hashtable.
   */
  public static final float DN_TABLE_LOAD_FACTOR = 0.75F;
 
 
 
  /**
   * The default length of time in milliseconds to wait while attempting to
   * acquire a read or write lock.
   */
  public static final long DEFAULT_TIMEOUT = 3000;
 
 
 
  // The set of global DN locks that we need to ensure thread safety for all of
  // the other operations.
  private static ReentrantLock[] globalDNLocks;
 
  // The set of entry locks that the server knows about.
  private static ConcurrentHashMap<DN,ReentrantReadWriteLock> entryLocks;
 
 
 
  // Initialize all of the lock variables.
  static
  {
    // Create the set of global DN locks.
    globalDNLocks = new ReentrantLock[NUM_GLOBAL_DN_LOCKS];
    for (int i=0; i < NUM_GLOBAL_DN_LOCKS; i++)
    {
      globalDNLocks[i] = new ReentrantLock();
    }
 
 
    // Create an empty table for holding the entry locks.
    entryLocks = new ConcurrentHashMap<DN,ReentrantReadWriteLock>(
         DN_TABLE_INITIAL_SIZE, DN_TABLE_LOAD_FACTOR, NUM_GLOBAL_DN_LOCKS);
  }
 
 
 
  /**
   * Attempts to acquire a read lock on the specified entry.  It will succeed
   * only if the lock is not already held.  If any blocking is required, then
   * this call will fail rather than block.
   *
   * @param  entryDN  The DN of the entry for which to obtain the read lock.
   *
   * @return  The read lock that was acquired, or <CODE>null</CODE> if it was
   *          not possible to obtain a read lock for some reason.
   */
  public static Lock tryLockRead(DN entryDN)
  {
    assert debugEnter(CLASS_NAME, "tryLockRead", String.valueOf(entryDN));
 
 
    int hashCode = (entryDN.hashCode() & 0x7FFFFFFF);
 
 
    // Get the hash code for the provided entry DN and determine which global
    // lock to acquire.  This will ensure that no two threads will be allowed to
    // lock or unlock the same entry at any given time, but should allow other
    // entries with different hash codes to be processed.
    ReentrantLock globalLock;
    try
    {
      globalLock = globalDNLocks[hashCode % NUM_GLOBAL_DN_LOCKS];
      if (! globalLock.tryLock())
      {
        return null;
      }
    }
    catch (Exception e)
    {
      assert debugException(CLASS_NAME, "lockRead", e);
 
      // This is not fine.  Some unexpected error occurred.
      assert debugMessage(DebugLogCategory.CORE_SERVER,
                          DebugLogSeverity.ERROR, CLASS_NAME, "lockRead",
                          "Unexpected exception while trying to obtain the " +
                          "global lock for entry " + entryDN.toString() +
                          ":  " + stackTraceToSingleLineString(e));
      return null;
    }
 
 
 
    // At this point we have the global lock for this bucket.  We must use a
    // try/catch/finally block to ensure that the global lock is always released
    // no matter what.
    try
    {
      // Now check to see if the entry is already in the lock table.
      ReentrantReadWriteLock entryLock = entryLocks.get(entryDN);
      if (entryLock == null)
      {
        // No lock exists for the entry.  Create one and put it in the table.
        entryLock = new ReentrantReadWriteLock();
        if (entryLock.readLock().tryLock())
        {
          entryLocks.put(entryDN, entryLock);
          return entryLock.readLock();
        }
        else
        {
          // This should never happen since we just created the lock.
          assert debugMessage(DebugLogCategory.CORE_SERVER,
                              DebugLogSeverity.ERROR, CLASS_NAME, "lockRead",
                              "Unable to acquire read lock on newly-created " +
                              "lock for entry " + entryDN.toString());
          return null;
        }
      }
      else
      {
        // There is already a lock for the entry.  Try to get its read lock.
        if (entryLock.readLock().tryLock())
        {
          // We got the read lock.  We don't need to do anything else.
          return entryLock.readLock();
        }
        else
        {
          // We couldn't get the read lock.  Write a debug message.
          assert debugMessage(DebugLogCategory.CORE_SERVER,
                              DebugLogSeverity.WARNING, CLASS_NAME, "lockRead",
                              "Unable to acquire a read lock for entry " +
                              entryDN.toString() + " that was already " +
                              "present in the lock table.");
          return null;
        }
      }
    }
    catch (Exception e)
    {
      assert debugException(CLASS_NAME, "lockRead", e);
 
      // This is not fine.  Some unexpected error occurred.
      assert debugMessage(DebugLogCategory.CORE_SERVER,
                          DebugLogSeverity.ERROR, CLASS_NAME, "lockRead",
                          "Unexpected exception while trying to obtain a " +
                          "read lock for entry " + entryDN.toString() +
                          ":  " + stackTraceToSingleLineString(e));
      return null;
    }
    finally
    {
      // This will always be called even after a return.
      globalLock.unlock();
    }
  }
 
 
 
  /**
   * Attempts to acquire a read lock for the specified entry.  Multiple threads
   * can hold the read lock concurrently for an entry as long as the write lock
   * is held.  If the write lock is held, then no other read or write locks will
   * be allowed for that entry until the write lock is released.  A default
   * timeout will be used for the lock.
   *
   * @param  entryDN  The DN of the entry for which to obtain the read lock.
   *
   * @return  The read lock that was acquired, or <CODE>null</CODE> if it was
   *          not possible to obtain a read lock for some reason.
   */
  public static Lock lockRead(DN entryDN)
  {
    assert debugEnter(CLASS_NAME, "lockRead", String.valueOf(entryDN));
 
    return lockRead(entryDN, DEFAULT_TIMEOUT);
  }
 
 
 
  /**
   * Attempts to acquire a read lock for the specified entry.  Multiple threads
   * can hold the read lock concurrently for an entry as long as the write lock
   * is held.  If the write lock is held, then no other read or write locks will
   * be allowed for that entry until the write lock is released.
   *
   * @param  entryDN  The DN of the entry for which to obtain the read lock.
   * @param  timeout  The maximum length of time in milliseconds to wait for the
   *                  lock before timing out.
   *
   * @return  The read lock that was acquired, or <CODE>null</CODE> if it was
   *          not possible to obtain a read lock for some reason.
   */
  public static Lock lockRead(DN entryDN, long timeout)
  {
    assert debugEnter(CLASS_NAME, "lockRead", String.valueOf(entryDN),
                      String.valueOf(timeout));
 
 
    int hashCode = (entryDN.hashCode() & 0x7FFFFFFF);
 
 
    // Get the hash code for the provided entry DN and determine which global
    // lock to acquire.  This will ensure that no two threads will be allowed to
    // lock or unlock the same entry at any given time, but should allow other
    // entries with different hash codes to be processed.
    ReentrantLock globalLock;
    try
    {
      globalLock = globalDNLocks[hashCode % NUM_GLOBAL_DN_LOCKS];
      if (! globalLock.tryLock(timeout, TimeUnit.MILLISECONDS))
      {
        return null;
      }
    }
    catch (InterruptedException ie)
    {
      assert debugException(CLASS_NAME, "lockRead", ie);
 
      // This is fine.  The thread trying to acquire the lock was interrupted.
      return null;
    }
    catch (Exception e)
    {
      assert debugException(CLASS_NAME, "lockRead", e);
 
      // This is not fine.  Some unexpected error occurred.
      assert debugMessage(DebugLogCategory.CORE_SERVER,
                          DebugLogSeverity.ERROR, CLASS_NAME, "lockRead",
                          "Unexpected exception while trying to obtain the " +
                          "global lock for entry " + entryDN.toString() +
                          ":  " + stackTraceToSingleLineString(e));
      return null;
    }
 
 
 
    // At this point we have the global lock for this bucket.  We must use a
    // try/catch/finally block to ensure that the global lock is always released
    // no matter what.
    try
    {
      // Now check to see if the entry is already in the lock table.
      ReentrantReadWriteLock entryLock = entryLocks.get(entryDN);
      if (entryLock == null)
      {
        // No lock exists for the entry.  Create one and put it in the table.
        entryLock = new ReentrantReadWriteLock();
        if (entryLock.readLock().tryLock(timeout, TimeUnit.MILLISECONDS))
        {
          entryLocks.put(entryDN, entryLock);
          return entryLock.readLock();
        }
        else
        {
          // This should never happen since we just created the lock.
          assert debugMessage(DebugLogCategory.CORE_SERVER,
                              DebugLogSeverity.ERROR, CLASS_NAME, "lockRead",
                              "Unable to acquire read lock on newly-created " +
                              "lock for entry " + entryDN.toString());
          return null;
        }
      }
      else
      {
        // There is already a lock for the entry.  Try to get its read lock.
        if (entryLock.readLock().tryLock(timeout, TimeUnit.MILLISECONDS))
        {
          // We got the read lock.  We don't need to do anything else.
          return entryLock.readLock();
        }
        else
        {
          // We couldn't get the read lock.  Write a debug message.
          assert debugMessage(DebugLogCategory.CORE_SERVER,
                              DebugLogSeverity.WARNING, CLASS_NAME, "lockRead",
                              "Unable to acquire a read lock for entry " +
                              entryDN.toString() + " that was already " +
                              "present in the lock table.");
          return null;
        }
      }
    }
    catch (InterruptedException ie)
    {
      assert debugException(CLASS_NAME, "lockRead", ie);
 
      // This is fine.  The thread trying to acquire the lock was interrupted.
      return null;
    }
    catch (Exception e)
    {
      assert debugException(CLASS_NAME, "lockRead", e);
 
      // This is not fine.  Some unexpected error occurred.
      assert debugMessage(DebugLogCategory.CORE_SERVER,
                          DebugLogSeverity.ERROR, CLASS_NAME, "lockRead",
                          "Unexpected exception while trying to obtain a " +
                          "read lock for entry " + entryDN.toString() +
                          ":  " + stackTraceToSingleLineString(e));
      return null;
    }
    finally
    {
      // This will always be called even after a return.
      globalLock.unlock();
    }
  }
 
 
 
  /**
   * Attempts to acquire a write lock on the specified entry.  It will succeed
   * only if the lock is not already held.  If any blocking is required, then
   * this call will fail rather than block.
   *
   * @param  entryDN  The DN of the entry for which to obtain the write lock.
   *
   * @return  The write lock that was acquired, or <CODE>null</CODE> if it was
   *          not possible to obtain a write lock for some reason.
   */
  public static Lock tryLockWrite(DN entryDN)
  {
    assert debugEnter(CLASS_NAME, "lockWrite", String.valueOf(entryDN));
 
 
    int hashCode = (entryDN.hashCode() & 0x7FFFFFFF);
 
 
    // Get the hash code for the provided entry DN and determine which global
    // lock to acquire.  This will ensure that no two threads will be allowed to
    // lock or unlock the same entry at any given time, but should allow other
    // entries with different hash codes to be processed.
    ReentrantLock globalLock;
    try
    {
      globalLock = globalDNLocks[hashCode % NUM_GLOBAL_DN_LOCKS];
      if (! globalLock.tryLock())
      {
        return null;
      }
    }
    catch (Exception e)
    {
      assert debugException(CLASS_NAME, "lockWrite", e);
 
      // This is not fine.  Some unexpected error occurred.
      assert debugMessage(DebugLogCategory.CORE_SERVER,
                          DebugLogSeverity.ERROR, CLASS_NAME, "lockWrite",
                          "Unexpected exception while trying to obtain the " +
                          "global lock for entry " + entryDN.toString() +
                          ":  " + stackTraceToSingleLineString(e));
      return null;
    }
 
 
 
    // At this point we have the global lock for this bucket.  We must use a
    // try/catch/finally block to ensure that the global lock is always released
    // no matter what.
    try
    {
      // Now check to see if the entry is already in the lock table.
      ReentrantReadWriteLock entryLock = entryLocks.get(entryDN);
      if (entryLock == null)
      {
        // No lock exists for the entry.  Create one and put it in the table.
        entryLock = new ReentrantReadWriteLock();
        if (entryLock.writeLock().tryLock())
        {
          entryLocks.put(entryDN, entryLock);
          return entryLock.writeLock();
        }
        else
        {
          // This should never happen since we just created the lock.
          assert debugMessage(DebugLogCategory.CORE_SERVER,
                              DebugLogSeverity.ERROR, CLASS_NAME, "lockWrite",
                              "Unable to acquire write lock on newly-created " +
                              "lock for entry " + entryDN.toString());
          return null;
        }
      }
      else
      {
        // There is already a lock for the entry.  Try to get its write lock.
        if (entryLock.writeLock().tryLock())
        {
          // We got the write lock.  We don't need to do anything else.
          return entryLock.writeLock();
        }
        else
        {
          // We couldn't get the write lock.  Write a debug message.
          assert debugMessage(DebugLogCategory.CORE_SERVER,
                              DebugLogSeverity.WARNING, CLASS_NAME, "lockWrite",
                              "Unable to acquire the write lock for entry " +
                              entryDN.toString() + " that was already " +
                              "present in the lock table.");
          return null;
        }
      }
    }
    catch (Exception e)
    {
      assert debugException(CLASS_NAME, "lockWrite", e);
 
      // This is not fine.  Some unexpected error occurred.
      assert debugMessage(DebugLogCategory.CORE_SERVER,
                          DebugLogSeverity.ERROR, CLASS_NAME, "lockWrite",
                          "Unexpected exception while trying to obtain the " +
                          "write lock for entry " + entryDN.toString() +
                          ":  " + stackTraceToSingleLineString(e));
      return null;
    }
    finally
    {
      // This will always be called even after a return.
      globalLock.unlock();
    }
  }
 
 
 
  /**
   * Attempts to acquire the write lock for the specified entry.  Only a single
   * thread may hold the write lock for an entry at any given time, and during
   * that time no read locks may be held for it.  A default timeout will be used
   * for the lock.
   *
   * @param  entryDN  The DN of the entry for which to obtain the write lock.
   *
   * @return  The write lock that was acquired, or <CODE>null</CODE> if it was
   *          not possible to obtain a read lock for some reason.
   */
  public static Lock lockWrite(DN entryDN)
  {
    assert debugEnter(CLASS_NAME, "lockRead", String.valueOf(entryDN));
 
    return lockWrite(entryDN, DEFAULT_TIMEOUT);
  }
 
 
 
  /**
   * Attempts to acquire the write lock for the specified entry.  Only a single
   * thread may hold the write lock for an entry at any given time, and during
   * that time no read locks may be held for it.
   *
   * @param  entryDN  The DN of the entry for which to obtain the write lock.
   * @param  timeout  The maximum length of time in milliseconds to wait for the
   *                  lock before timing out.
   *
   * @return  The write lock that was acquired, or <CODE>null</CODE> if it was
   *          not possible to obtain a read lock for some reason.
   */
  public static Lock lockWrite(DN entryDN, long timeout)
  {
    assert debugEnter(CLASS_NAME, "lockWrite", String.valueOf(entryDN),
                      String.valueOf(timeout));
 
 
    int hashCode = (entryDN.hashCode() & 0x7FFFFFFF);
 
 
    // Get the hash code for the provided entry DN and determine which global
    // lock to acquire.  This will ensure that no two threads will be allowed to
    // lock or unlock the same entry at any given time, but should allow other
    // entries with different hash codes to be processed.
    ReentrantLock globalLock;
    try
    {
      globalLock = globalDNLocks[hashCode % NUM_GLOBAL_DN_LOCKS];
      if (! globalLock.tryLock(timeout, TimeUnit.MILLISECONDS))
      {
        return null;
      }
    }
    catch (InterruptedException ie)
    {
      assert debugException(CLASS_NAME, "lockWrite", ie);
 
      // This is fine.  The thread trying to acquire the lock was interrupted.
      return null;
    }
    catch (Exception e)
    {
      assert debugException(CLASS_NAME, "lockWrite", e);
 
      // This is not fine.  Some unexpected error occurred.
      assert debugMessage(DebugLogCategory.CORE_SERVER,
                          DebugLogSeverity.ERROR, CLASS_NAME, "lockWrite",
                          "Unexpected exception while trying to obtain the " +
                          "global lock for entry " + entryDN.toString() +
                          ":  " + stackTraceToSingleLineString(e));
      return null;
    }
 
 
 
    // At this point we have the global lock for this bucket.  We must use a
    // try/catch/finally block to ensure that the global lock is always released
    // no matter what.
    try
    {
      // Now check to see if the entry is already in the lock table.
      ReentrantReadWriteLock entryLock = entryLocks.get(entryDN);
      if (entryLock == null)
      {
        // No lock exists for the entry.  Create one and put it in the table.
        entryLock = new ReentrantReadWriteLock();
        if (entryLock.writeLock().tryLock(timeout, TimeUnit.MILLISECONDS))
        {
          entryLocks.put(entryDN, entryLock);
          return entryLock.writeLock();
        }
        else
        {
          // This should never happen since we just created the lock.
          assert debugMessage(DebugLogCategory.CORE_SERVER,
                              DebugLogSeverity.ERROR, CLASS_NAME, "lockWrite",
                              "Unable to acquire write lock on newly-created " +
                              "lock for entry " + entryDN.toString());
          return null;
        }
      }
      else
      {
        // There is already a lock for the entry.  Try to get its write lock.
        if (entryLock.writeLock().tryLock(timeout, TimeUnit.MILLISECONDS))
        {
          // We got the write lock.  We don't need to do anything else.
          return entryLock.writeLock();
        }
        else
        {
          // We couldn't get the write lock.  Write a debug message.
          assert debugMessage(DebugLogCategory.CORE_SERVER,
                              DebugLogSeverity.WARNING, CLASS_NAME, "lockWrite",
                              "Unable to acquire the write lock for entry " +
                              entryDN.toString() + " that was already " +
                              "present in the lock table.");
          return null;
        }
      }
    }
    catch (InterruptedException ie)
    {
      assert debugException(CLASS_NAME, "lockWrite", ie);
 
      // This is fine.  The thread trying to acquire the lock was interrupted.
      return null;
    }
    catch (Exception e)
    {
      assert debugException(CLASS_NAME, "lockWrite", e);
 
      // This is not fine.  Some unexpected error occurred.
      assert debugMessage(DebugLogCategory.CORE_SERVER,
                          DebugLogSeverity.ERROR, CLASS_NAME, "lockWrite",
                          "Unexpected exception while trying to obtain the " +
                          "write lock for entry " + entryDN.toString() +
                          ":  " + stackTraceToSingleLineString(e));
      return null;
    }
    finally
    {
      // This will always be called even after a return.
      globalLock.unlock();
    }
  }
 
 
 
  /**
   * Releases a read or write lock held on the specified entry.
   *
   * @param  entryDN  The DN of the entry for which to release the lock.
   * @param  lock     The read or write lock held for the entry.
   */
  public static void unlock(DN entryDN, Lock lock)
  {
    assert debugEnter(CLASS_NAME, "unlock", String.valueOf(entryDN));
 
 
    // Unlock the entry without grabbing any additional locks.
    try
    {
      lock.unlock();
    }
    catch (Exception e)
    {
      // This should never happen.  However, if it does, then just capture the
      // exception and continue because it may still be necessary to remove the
      // lock for the entry from the table.
      assert debugException(CLASS_NAME, "unlock", e);
    }
 
 
    int hashCode = (entryDN.hashCode() & 0x7FFFFFFF);
 
 
    // Now grab the global lock for the entry and check to see if we can remove
    // it from the table.
    ReentrantLock globalLock;
    try
    {
      globalLock = globalDNLocks[hashCode % NUM_GLOBAL_DN_LOCKS];
 
      // This will block until it acquires the lock or until it is interrupted.
      globalLock.lockInterruptibly();
    }
    catch (InterruptedException ie)
    {
      assert debugException(CLASS_NAME, "unlock", ie);
 
      // The lock trying to acquire the lock was interrupted.  In this case,
      // we'll just return.  The worst that could happen here is that a lock
      // that isn't held by anything is still in the table which will just
      // consume a little memory.
      return;
    }
    catch (Exception e)
    {
      assert debugException(CLASS_NAME, "unlock", e);
 
      // This is not fine.  Some unexpected error occurred.  But again, the
      // worst that could happen is that we may not clean up an unheld lock,
      // which isn't really that big a deal unless it happens too often.
      assert debugMessage(DebugLogCategory.CORE_SERVER,
                          DebugLogSeverity.ERROR, CLASS_NAME, "unlock",
                          "Unexpected exception while trying to obtain the " +
                          "global lock for entry " + entryDN.toString() +
                          ":  " + stackTraceToSingleLineString(e));
      return;
    }
 
 
    // At this point we have the global lock for this bucket.  We must use a
    // try/catch/finally block to ensure that the global lock is always released
    // no matter what.
    try
    {
      ReentrantReadWriteLock entryLock = entryLocks.get(entryDN);
      if ((entryLock != null) && (entryLock.getReadLockCount() == 0) &&
          (! entryLock.isWriteLocked()))
      {
        // This lock isn't held so we can remove it from the table.
        entryLocks.remove(entryDN);
      }
 
      return;
    }
    catch (Exception e)
    {
      assert debugException(CLASS_NAME, "unlock", e);
 
      // This should never happen.
      assert debugMessage(DebugLogCategory.CORE_SERVER,
                          DebugLogSeverity.ERROR, CLASS_NAME, "unlock",
                          "Unexpected exception while trying to determine " +
                          "whether the lock for entry " + entryDN.toString() +
                          " can be removed:  " +
                          stackTraceToSingleLineString(e));
      return;
    }
    finally
    {
      globalLock.unlock();
    }
  }
 
 
 
  /**
   * Removes any reference to the specified entry from the lock table.  This may
   * be helpful if there is a case where a lock has been orphaned somehow and
   * must be removed before other threads may acquire it.
   *
   * @param  entryDN  The DN of the entry for which to remove the lock from the
   *                  table.
   *
   * @return  The read write lock that was removed from the table, or
   *          <CODE>null</CODE> if nothing was in the table for the specified
   *          entry.  If a lock object is returned, it may be possible to get
   *          information about who was holding it.
   */
  public static ReentrantReadWriteLock destroyLock(DN entryDN)
  {
    assert debugEnter(CLASS_NAME, "destroyLock", String.valueOf(entryDN));
 
    return entryLocks.remove(entryDN);
  }
 
 
 
  /**
   * Retrieves the number of entries currently held in the lock table.  Note
   * that this may be an expensive operation.
   *
   * @return  The number of entries currently held in the lock table.
   */
  public static int lockTableSize()
  {
    assert debugEnter(CLASS_NAME, "lockTableSize");
 
    return entryLocks.size();
  }
}