/** constructor initializes to given reference and stamp */
 public void testConstructor() {
   AtomicStampedReference ai = new AtomicStampedReference(one, 0);
   assertSame(one, ai.getReference());
   assertEquals(0, ai.getStamp());
   AtomicStampedReference a2 = new AtomicStampedReference(null, 1);
   assertNull(a2.getReference());
   assertEquals(1, a2.getStamp());
 }
  /**
   * @param updateSeq Update sequence.
   * @return {@code True} if entry has been transitioned to state EVICTED.
   */
  boolean tryEvict(boolean updateSeq) {
    if (state.getReference() != RENTING || state.getStamp() != 0 || groupReserved()) return false;

    // Attempt to evict partition entries from cache.
    clearAll();

    if (map.isEmpty() && state.compareAndSet(RENTING, EVICTED, 0, 0)) {
      if (log.isDebugEnabled()) log.debug("Evicted partition: " + this);

      if (!GridQueryProcessor.isEnabled(cctx.config())) clearSwap();

      if (cctx.isDrEnabled()) cctx.dr().partitionEvicted(id);

      cctx.dataStructures().onPartitionEvicted(id);

      rent.onDone();

      ((GridDhtPreloader) cctx.preloader()).onPartitionEvicted(this, updateSeq);

      clearDeferredDeletes();

      return true;
    }

    return false;
  }
  /**
   * Adds group reservation to this partition.
   *
   * @param r Reservation.
   * @return {@code false} If such reservation already added.
   */
  public boolean addReservation(GridDhtPartitionsReservation r) {
    assert state.getReference() != EVICTED : "we can reserve only active partitions";
    assert state.getStamp() != 0
        : "partition must be already reserved before adding group reservation";

    return reservations.addIfAbsent(r);
  }
 /** get returns the last values of reference and stamp set */
 public void testGetSet() {
   int[] mark = new int[1];
   AtomicStampedReference ai = new AtomicStampedReference(one, 0);
   assertSame(one, ai.getReference());
   assertEquals(0, ai.getStamp());
   assertSame(one, ai.get(mark));
   assertEquals(0, mark[0]);
   ai.set(two, 0);
   assertSame(two, ai.getReference());
   assertEquals(0, ai.getStamp());
   assertSame(two, ai.get(mark));
   assertEquals(0, mark[0]);
   ai.set(one, 1);
   assertSame(one, ai.getReference());
   assertEquals(1, ai.getStamp());
   assertSame(one, ai.get(mark));
   assertEquals(1, mark[0]);
 }
  /**
   * Reserves a partition so it won't be cleared.
   *
   * @return {@code True} if reserved.
   */
  @Override
  public boolean reserve() {
    while (true) {
      int reservations = state.getStamp();

      GridDhtPartitionState s = state.getReference();

      if (s == EVICTED) return false;

      if (state.compareAndSet(s, s, reservations, reservations + 1)) return true;
    }
  }
  /** compareAndSet in one thread enables another waiting for reference value to succeed */
  public void testCompareAndSetInMultipleThreads() throws Exception {
    final AtomicStampedReference ai = new AtomicStampedReference(one, 0);
    Thread t =
        new Thread(
            new CheckedRunnable() {
              public void realRun() {
                while (!ai.compareAndSet(two, three, 0, 0)) Thread.yield();
              }
            });

    t.start();
    assertTrue(ai.compareAndSet(one, two, 0, 0));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(three, ai.getReference());
    assertEquals(0, ai.getStamp());
  }
  /** Releases previously reserved partition. */
  @Override
  public void release() {
    while (true) {
      int reservations = state.getStamp();

      if (reservations == 0) return;

      GridDhtPartitionState s = state.getReference();

      assert s != EVICTED;

      // Decrement reservations.
      if (state.compareAndSet(s, s, reservations, --reservations)) {
        tryEvict(true);

        break;
      }
    }
  }
  /**
   * @param updateSeq Update sequence.
   * @return Future to signal that this node is no longer an owner or backup.
   */
  IgniteInternalFuture<?> rent(boolean updateSeq) {
    while (true) {
      int reservations = state.getStamp();

      GridDhtPartitionState s = state.getReference();

      if (s == RENTING || s == EVICTED) return rent;

      if (state.compareAndSet(s, RENTING, reservations, reservations)) {
        if (log.isDebugEnabled()) log.debug("Moved partition to RENTING state: " + this);

        // Evict asynchronously, as the 'rent' method may be called
        // from within write locks on local partition.
        tryEvictAsync(updateSeq);

        break;
      }
    }

    return rent;
  }
  /** @return {@code True} if transitioned to OWNING state. */
  boolean own() {
    while (true) {
      int reservations = state.getStamp();

      GridDhtPartitionState s = state.getReference();

      if (s == RENTING || s == EVICTED) return false;

      if (s == OWNING) return true;

      assert s == MOVING;

      if (state.compareAndSet(MOVING, OWNING, reservations, reservations)) {
        if (log.isDebugEnabled()) log.debug("Owned partition: " + this);

        // No need to keep history any more.
        evictHist = null;

        return true;
      }
    }
  }
Beispiel #10
0
 public boolean attemptModifyASR() {
   boolean modified = false;
   IntendedModification currentlyOngoingMod = ongoingMod.getReference();
   int stamp = ongoingMod.getStamp();
   if (currentlyOngoingMod == null) {
     // copy data structure state - for use
     // in intended modification
     // prepare intended modification
     IntendedModification newMod = new IntendedModification();
     // compareAndSet(V expectedReference, V newReference, int expectedStamp, int newStamp) {}
     boolean modSubmitted = ongoingMod.compareAndSet(null, newMod, stamp, stamp + 1);
     if (modSubmitted) {
       // complete modification via a series of compare-and-swap operations.
       // note: other threads may assist in completing the compare-and-swap
       // operations, so some CAS may fail
       modified = true;
     }
   } else {
     // attempt to complete ongoing modification, so the data structure is fired up
     // to allow access from this thread.
     modified = false;
   }
   return modified;
 }
 /** @return Partition state. */
 public GridDhtPartitionState state() {
   return state.getReference();
 }
 public State getState() {
   return state.getReference();
 }