/** @param e Error. */
  void onError(Throwable e) {
    tx.commitError(e);

    if (err.compareAndSet(null, e)) {
      boolean marked = tx.setRollbackOnly();

      if (e instanceof GridCacheTxRollbackException) {
        if (marked) {
          try {
            tx.rollback();
          } catch (GridException ex) {
            U.error(log, "Failed to automatically rollback transaction: " + tx, ex);
          }
        }
      } else if (tx.implicit()
          && tx.isSystemInvalidate()) { // Finish implicit transaction on heuristic error.
        try {
          tx.close();
        } catch (GridException ex) {
          U.error(log, "Failed to invalidate transaction: " + tx, ex);
        }
      }

      onComplete();
    }
  }
Beispiel #2
0
 public void setUpper(int i) {
   while (true) {
     IntPair oldv = values.get();
     if (i < oldv.lower)
       throw new IllegalArgumentException("Can't set upper to " + i + " < lower");
     IntPair newv = new IntPair(oldv.lower, i);
     if (values.compareAndSet(oldv, newv)) return;
   }
 }
 /** @param lsnr Listener to external collision events. */
 public void setCollisionExternalListener(@Nullable GridCollisionExternalListener lsnr) {
   if (enabled()) {
     if (lsnr != null && !extLsnr.compareAndSet(null, lsnr))
       assert false
           : "Collision external listener has already been set "
               + "(perhaps need to add support for multiple listeners)";
     else if (log.isDebugEnabled())
       log.debug("Successfully set external collision listener: " + lsnr);
   }
 }
  /** {@inheritDoc} */
  @Override
  public GridDiscoveryTopologySnapshot topologySnapshot() throws IgniteCheckedException {
    get();

    if (topSnapshot.get() == null)
      topSnapshot.compareAndSet(
          null,
          new GridDiscoveryTopologySnapshot(discoEvt.topologyVersion(), discoEvt.topologyNodes()));

    return topSnapshot.get();
  }
  /** @param e Error. */
  void onError(Throwable e) {
    tx.commitError(e);

    if (err.compareAndSet(null, e)) {
      boolean marked = tx.setRollbackOnly();

      if (e instanceof GridCacheTxRollbackException)
        if (marked) {
          try {
            tx.rollback();
          } catch (GridException ex) {
            U.error(log, "Failed to automatically rollback transaction: " + tx, ex);
          }
        }

      onComplete();
    }
  }
Beispiel #6
0
  /**
   * Basically, future mapping consists from two parts. First, we must determine the topology
   * version this future will map on. Locking is performed within a user transaction, we must
   * continue to map keys on the same topology version as it started. If topology version is
   * undefined, we get current topology future and wait until it completes so the topology is ready
   * to use.
   *
   * <p>During the second part we map keys to primary nodes using topology snapshot we obtained
   * during the first part. Note that if primary node leaves grid, the future will fail and
   * transaction will be rolled back.
   */
  void map() {
    // Obtain the topology version to use.
    GridDiscoveryTopologySnapshot snapshot =
        tx != null
            ? tx.topologySnapshot()
            : cctx.mvcc().lastExplicitLockTopologySnapshot(Thread.currentThread().getId());

    if (snapshot != null) {
      // Continue mapping on the same topology version as it was before.
      topSnapshot.compareAndSet(null, snapshot);

      map(keys);

      markInitialized();

      return;
    }

    // Must get topology snapshot and map on that version.
    mapOnTopology();
  }
Beispiel #7
0
  /**
   * Acquires topology future and checks it completeness under the read lock. If it is not complete,
   * will asynchronously wait for it's completeness and then try again.
   */
  void mapOnTopology() {
    // We must acquire topology snapshot from the topology version future.
    try {
      cctx.topology().readLock();

      try {
        GridDhtTopologyFuture fut = cctx.topologyVersionFuture();

        if (fut.isDone()) {
          GridDiscoveryTopologySnapshot snapshot = fut.topologySnapshot();

          if (tx != null) {
            tx.topologyVersion(snapshot.topologyVersion());
            tx.topologySnapshot(snapshot);
          }

          topSnapshot.compareAndSet(null, snapshot);

          map(keys);

          markInitialized();
        } else {
          fut.listenAsync(
              new CI1<GridFuture<Long>>() {
                @Override
                public void apply(GridFuture<Long> t) {
                  mapOnTopology();
                }
              });
        }
      } finally {
        cctx.topology().readUnlock();
      }
    } catch (GridException e) {
      onDone(e);
    }
  }
  /**
   * @param nodeId Sender node id.
   * @param msg Single partition info.
   */
  public void onReceive(final UUID nodeId, final GridDhtPartitionsSingleMessage msg) {
    assert msg != null;

    assert msg.exchangeId().equals(exchId);

    // Update last seen version.
    while (true) {
      GridCacheVersion old = lastVer.get();

      if (old == null || old.compareTo(msg.lastVersion()) < 0) {
        if (lastVer.compareAndSet(old, msg.lastVersion())) break;
      } else break;
    }

    if (isDone()) {
      if (log.isDebugEnabled())
        log.debug(
            "Received message for finished future (will reply only to sender) [msg="
                + msg
                + ", fut="
                + this
                + ']');

      sendAllPartitions(nodeId, cctx.gridConfig().getNetworkSendRetryCount());
    } else {
      initFut.listen(
          new CI1<IgniteInternalFuture<Boolean>>() {
            @Override
            public void apply(IgniteInternalFuture<Boolean> t) {
              try {
                if (!t.get()) // Just to check if there was an error.
                return;

                ClusterNode loc = cctx.localNode();

                singleMsgs.put(nodeId, msg);

                boolean match = true;

                // Check if oldest node has changed.
                if (!oldestNode.get().equals(loc)) {
                  match = false;

                  synchronized (mux) {
                    // Double check.
                    if (oldestNode.get().equals(loc)) match = true;
                  }
                }

                if (match) {
                  boolean allReceived;

                  synchronized (rcvdIds) {
                    if (rcvdIds.add(nodeId)) updatePartitionSingleMap(msg);

                    allReceived = allReceived();
                  }

                  // If got all replies, and initialization finished, and reply has not been sent
                  // yet.
                  if (allReceived && ready.get() && replied.compareAndSet(false, true)) {
                    spreadPartitions();

                    onDone(exchId.topologyVersion());
                  } else if (log.isDebugEnabled())
                    log.debug(
                        "Exchange future full map is not sent [allReceived="
                            + allReceived()
                            + ", ready="
                            + ready
                            + ", replied="
                            + replied.get()
                            + ", init="
                            + init.get()
                            + ", fut="
                            + this
                            + ']');
                }
              } catch (IgniteCheckedException e) {
                U.error(log, "Failed to initialize exchange future: " + this, e);
              }
            }
          });
    }
  }
Beispiel #9
0
 /** @param t Error. */
 private void onError(Throwable t) {
   err.compareAndSet(null, t instanceof GridCacheLockTimeoutException ? null : t);
 }