Ejemplo n.º 1
0
  /** {@inheritDoc} */
  @Override
  public boolean tmLock(GridCacheTxEx<K, V> tx, long timeout)
      throws GridCacheEntryRemovedException, GridDistributedLockCancelledException {
    if (tx.local()) {
      GridDhtTxLocal<K, V> dhtTx = (GridDhtTxLocal<K, V>) tx;

      // Null is returned if timeout is negative and there is other lock owner.
      return addDhtLocal(
              dhtTx.nearNodeId(),
              dhtTx.nearXidVersion(),
              tx.threadId(),
              tx.xidVersion(),
              timeout,
              false,
              tx.ec(),
              true)
          != null;
    }

    try {
      addRemote(
          tx.nodeId(),
          tx.otherNodeId(),
          tx.threadId(),
          tx.xidVersion(),
          tx.timeout(),
          tx.ec(),
          true);

      return true;
    } catch (GridDistributedLockCancelledException ignored) {
      if (log.isDebugEnabled())
        log.debug("Attempted to enter tx lock for cancelled ID (will ignore): " + tx);

      return false;
    }
  }
Ejemplo n.º 2
0
  /**
   * @param nodeId Reader to add.
   * @param msgId Message ID.
   * @return Future for all relevant transactions that were active at the time of adding reader, or
   *     {@code null} if reader was added
   * @throws GridCacheEntryRemovedException If entry was removed.
   */
  @Nullable
  public GridFuture<Boolean> addReader(UUID nodeId, long msgId)
      throws GridCacheEntryRemovedException {
    // Don't add local node as reader.
    if (cctx.nodeId().equals(nodeId)) return null;

    GridNode node = cctx.discovery().node(nodeId);

    // If remote node has no near cache, don't add it.
    if (node == null || !U.hasNearCache(node, cctx.dht().near().name())) return null;

    // If remote node is (primary?) or back up, don't add it as a reader.
    if (U.nodeIds(cctx.affinity(partition(), CU.allNodes(cctx))).contains(nodeId)) return null;

    boolean ret = false;

    GridCacheMultiTxFuture<K, V> txFut;

    Collection<GridCacheMvccCandidate<K>> cands = null;

    synchronized (mux) {
      checkObsolete();

      txFut = this.txFut;

      ReaderId reader = readerId(nodeId);

      if (reader == null) {
        reader = new ReaderId(nodeId, msgId);

        readers = new LinkedList<ReaderId>(readers);

        readers.add(reader);

        // Seal.
        readers = Collections.unmodifiableList(readers);

        txFut = this.txFut = new GridCacheMultiTxFuture<K, V>(cctx);

        cands = localCandidates();

        ret = true;
      } else {
        long id = reader.messageId();

        if (id < msgId) reader.messageId(msgId);
      }
    }

    if (ret) {
      assert txFut != null;

      if (!F.isEmpty(cands)) {
        for (GridCacheMvccCandidate<K> c : cands) {
          GridCacheTxEx<K, V> tx = cctx.tm().<GridCacheTxEx<K, V>>tx(c.version());

          if (tx != null) {
            assert tx.local();

            txFut.addTx(tx);
          }
        }
      }

      txFut.init();

      if (!txFut.isDone()) {
        txFut.listenAsync(
            new CI1<GridFuture<?>>() {
              @Override
              public void apply(GridFuture<?> f) {
                synchronized (mux) {
                  // Release memory.
                  GridDhtCacheEntry.this.txFut = null;
                }
              }
            });
      } else
        // Release memory.
        txFut = this.txFut = null;
    }

    return txFut;
  }