Esempio n. 1
0
  /** {@inheritDoc} */
  @Override
  public void saveCheckpoint(
      String key, Object state, GridTaskSessionScope scope, long timeout, boolean overwrite)
      throws GridException {
    A.notNull(key, "key");
    A.ensure(timeout >= 0, "timeout >= 0");

    synchronized (mux) {
      if (closed) throw new GridException("Failed to save checkpoint (session closed): " + this);
    }

    ctx.checkpoint().storeCheckpoint(this, key, state, scope, timeout, overwrite);
  }
Esempio n. 2
0
  /** {@inheritDoc} */
  @Override
  public Map<?, ?> waitForAttributes(Collection<?> keys, long timeout) throws InterruptedException {
    A.notNull(keys, "keys");

    if (keys.isEmpty()) return Collections.emptyMap();

    if (timeout == 0) timeout = Long.MAX_VALUE;

    long now = System.currentTimeMillis();

    // Prevent overflow.
    long end = now + timeout < 0 ? Long.MAX_VALUE : now + timeout;

    // Don't wait longer than session timeout.
    if (end > endTime) end = endTime;

    synchronized (mux) {
      while (!closed && !attrs.keySet().containsAll(keys) && now < end) {
        mux.wait(end - now);

        now = System.currentTimeMillis();
      }

      if (closed) throw new InterruptedException("Session was closed: " + this);

      Map<Object, Object> retVal = new HashMap<Object, Object>(keys.size());

      for (Object key : keys) retVal.put(key, attrs.get(key));

      return retVal;
    }
  }
Esempio n. 3
0
  /** {@inheritDoc} */
  @Override
  public boolean waitForAttribute(Object key, Object val, long timeout)
      throws InterruptedException {
    A.notNull(key, "key");

    if (timeout == 0) timeout = Long.MAX_VALUE;

    long now = System.currentTimeMillis();

    // Prevent overflow.
    long end = now + timeout < 0 ? Long.MAX_VALUE : now + timeout;

    // Don't wait longer than session timeout.
    if (end > endTime) end = endTime;

    synchronized (mux) {
      boolean isFound = false;

      while (!closed && !(isFound = isAttributeSet(key, val)) && now < end) {
        mux.wait(end - now);

        now = System.currentTimeMillis();
      }

      if (closed) throw new InterruptedException("Session was closed: " + this);

      return isFound;
    }
  }
Esempio n. 4
0
  /** {@inheritDoc} */
  @SuppressWarnings("unchecked")
  @Override
  public <K, V> V waitForAttribute(K key, long timeout) throws InterruptedException {
    A.notNull(key, "key");

    if (timeout == 0) timeout = Long.MAX_VALUE;

    long now = System.currentTimeMillis();

    // Prevent overflow.
    long end = now + timeout < 0 ? Long.MAX_VALUE : now + timeout;

    // Don't wait longer than session timeout.
    if (end > endTime) end = endTime;

    synchronized (mux) {
      while (!closed && !attrs.containsKey(key) && now < end) {
        mux.wait(end - now);

        now = System.currentTimeMillis();
      }

      if (closed) throw new InterruptedException("Session was closed: " + this);

      return (V) attrs.get(key);
    }
  }
  /**
   * Removes given value from the set and returns the instance stored in the set or {@code null} if
   * value was not found.
   *
   * @param val Value to remove.
   * @return The instance that was stored in the set or {@code null}.
   */
  @Nullable
  public V removex(V val) {
    A.notNull(val, "val");

    if (comp == null || !strict) {
      for (Iterator<V> it = vals.iterator(); it.hasNext(); ) {
        V v = it.next();

        if (v.equals(val)) {
          it.remove();

          return v;
        }
      }

      return null;
    }

    assert comp != null && strict;

    for (Iterator<V> it = vals.iterator(); it.hasNext(); ) {
      V v = it.next();

      // Prefer equals to comparator.
      if (v.equals(val)) {
        it.remove();

        return v;
      }

      if (comp.compare(v, val) > 0) break;
    }

    return null;
  }
Esempio n. 6
0
  /** {@inheritDoc} */
  @Override
  public boolean waitForAttributes(Map<?, ?> attrs, long timeout) throws InterruptedException {
    A.notNull(attrs, "attrs");

    if (attrs.isEmpty()) {
      return true;
    }

    if (timeout == 0) timeout = Long.MAX_VALUE;

    long now = System.currentTimeMillis();

    // Prevent overflow.
    long end = now + timeout < 0 ? Long.MAX_VALUE : now + timeout;

    // Don't wait longer than session timeout.
    if (end > endTime) end = endTime;

    synchronized (mux) {
      boolean isFound = false;

      while (!closed
          && !(isFound = this.attrs.entrySet().containsAll(attrs.entrySet()))
          && now < end) {
        mux.wait(end - now);

        now = System.currentTimeMillis();
      }

      if (closed) throw new InterruptedException("Session was closed: " + this);

      return isFound;
    }
  }
Esempio n. 7
0
  /**
   * Creates JEXL predicate with given parameters.
   *
   * @param exprStr JEXL boolean expression. Note that non-boolean return value will evaluate this
   *     predicate to {@code false}.
   * @param var1 Name of the 1st bound variable in JEXL expression.
   * @param var2 Name of the 2nd bound variable in JEXL expression.
   */
  public GridJexlPredicate2(String exprStr, String var1, String var2) {
    A.notNull(exprStr, "exprStr", var1, "var1", var2, "var2");

    this.exprStr = exprStr;
    this.var1 = var1;
    this.var2 = var2;
  }
  /**
   * Either adds a value to set or does nothing if value is already present.
   *
   * @param val Value to add.
   * @return The instance of value from this set or {@code null} if value was added.
   */
  @Nullable
  public V addx(V val) {
    A.notNull(val, "val");

    if (comp == null) {
      for (V v : vals) if (v.equals(val)) return v;

      vals.add(val);

      return null;
    }

    if (strict) {
      for (ListIterator<V> it = vals.listIterator(); it.hasNext(); ) {
        V v = it.next();

        // Prefer equals to comparator.
        if (v.equals(val)) return v;

        int c = comp.compare(v, val);

        if (c == 0) throw new IllegalStateException("Inconsistent equals and compare methods.");

        if (c > 0) {
          // Back up.
          it.previous();

          it.add(val);

          return null;
        }
      }

      vals.add(val);

      return null;
    }

    // Full scan first.
    for (V v : vals) if (v.equals(val)) return v;

    for (ListIterator<V> it = vals.listIterator(); it.hasNext(); ) {
      V v = it.next();

      if (comp.compare(v, val) > 0) {
        do {
          // Back up.
          v = it.previous();
        } while (comp.compare(v, val) == 0);

        it.add(val);

        return null;
      }
    }

    vals.add(val);

    return null;
  }
Esempio n. 9
0
  /**
   * Gets option value. If this option is "none" this method will throw {@link
   * IllegalArgumentException} exception. Note that this method may return {@code null}.
   *
   * @return An option value.
   * @throws IllegalArgumentException Thrown in case if this option is "none".
   */
  public T get() {
    A.ensure(this != NONE, "Value 'none' of GridOpt cannot be used.");

    assert val != null;

    return val;
  }
  /**
   * Constructs a new, empty set that orders its elements according to the specified comparator.
   *
   * @param max Upper bound of this map.
   * @param comparator The comparator that will be used to order this map. If <tt>null</tt>, the
   *     {@linkplain Comparable natural ordering} of the elements will be used.
   */
  public GridBoundedConcurrentOrderedMap(int max, Comparator<? super K> comparator) {
    super(comparator);

    A.ensure(max > 0, "max > 0");

    this.max = max;
  }
  /** {@inheritDoc} */
  @SuppressWarnings({"unchecked"})
  @Override
  public boolean remove(Object val) {
    A.notNull(val, "val");

    return removex((V) val) != null;
  }
Esempio n. 12
0
  /**
   * Sets JEXL context variable value and returns {@code this}.
   *
   * @param var Name of the variable in JEXL context (new or existing).
   * @param val Value to be set or overridden in JEXL context.
   * @return This predicate so that this call can be chained.
   */
  public GridJexlPredicate2<T1, T2> with(String var, @Nullable Object val) {
    A.notNull(var, "var");

    map.put(var, val);

    return this;
  }
  /**
   * Constructs a new map containing the elements in the specified map, that orders its elements
   * according to their {@linkplain Comparable natural ordering}.
   *
   * @param max Upper bound of this map.
   * @param map The elements that will comprise the new map.
   * @throws ClassCastException if the elements in <tt>map</tt> are not {@link Comparable}, or are
   *     not mutually comparable.
   * @throws NullPointerException if the specified map or any of its elements are {@code null}.
   */
  public GridBoundedConcurrentOrderedMap(int max, Map<? extends K, ? extends V> map) {
    super(map);

    A.ensure(max > 0, "max > 0");

    this.max = max;
  }
  /**
   * Constructs a new map containing the same elements and using the same ordering as the specified
   * sorted map.
   *
   * @param max Upper bound of this map.
   * @param map Sorted map whose elements will comprise the new map.
   * @throws NullPointerException if the specified sorted map or any of its elements are {@code
   *     null}.
   */
  public GridBoundedConcurrentOrderedMap(int max, SortedMap<K, V> map) {
    super(map);

    A.ensure(max > 0, "max > 0");

    this.max = max;
  }
  /**
   * @param keys Keys.
   * @return If near entries for given keys are locked.
   */
  public boolean isAllLockedNearOnly(Iterable<? extends K> keys) {
    A.notNull(keys, "keys");

    for (K key : keys) if (!isLockedNearOnly(key)) return false;

    return true;
  }
Esempio n. 16
0
  /** {@inheritDoc} */
  @Override
  public void addAttributeListener(GridTaskSessionAttributeListener lsnr, boolean rewind) {
    A.notNull(lsnr, "lsnr");

    Map<Object, Object> attrs = null;

    List<GridTaskSessionAttributeListener> lsnrs;

    synchronized (mux) {
      lsnrs = new ArrayList<GridTaskSessionAttributeListener>(this.lsnrs.size());

      lsnrs.addAll(this.lsnrs);

      lsnrs.add(lsnr);

      lsnrs = Collections.unmodifiableList(lsnrs);

      this.lsnrs = lsnrs;

      if (rewind) attrs = new HashMap<Object, Object>(this.attrs);
    }

    if (rewind) {
      for (Map.Entry<Object, Object> entry : attrs.entrySet()) {
        for (GridTaskSessionAttributeListener l : lsnrs) {
          l.onAttributeSet(entry.getKey(), entry.getValue());
        }
      }
    }
  }
Esempio n. 17
0
  /** {@inheritDoc} */
  @Override
  public R get(long timeout, TimeUnit unit) throws GridException {
    A.ensure(timeout >= 0, "timeout cannot be negative: " + timeout);
    A.notNull(unit, "unit");

    checkValid();

    try {
      long now = System.currentTimeMillis();

      long end = timeout == 0 ? Long.MAX_VALUE : now + MILLISECONDS.convert(timeout, unit);

      // Account for overflow.
      if (end < 0) end = Long.MAX_VALUE;

      synchronized (mux) {
        while (!done && !cancelled && now < end) {
          mux.wait(end - now);

          if (!done) now = System.currentTimeMillis();
        }

        if (done) {
          if (err != null) throw U.cast(err);

          return res;
        }

        if (cancelled) throw new GridFutureCancelledException("Future was cancelled: " + this);

        throw new GridFutureTimeoutException(
            "Timeout was reached before computation completed [duration="
                + duration()
                + "ms, timeout="
                + unit.toMillis(timeout)
                + "ms]");
      }
    } catch (InterruptedException e) {
      throw new GridInterruptedException(
          "Got interrupted while waiting for future to complete [duration="
              + duration()
              + "ms, timeout="
              + unit.toMillis(timeout)
              + "ms]",
          e);
    }
  }
Esempio n. 18
0
  /** {@inheritDoc} */
  @Override
  public void setAttributes(Map<?, ?> attrs) {
    A.notNull(attrs, "attrs");

    synchronized (this.attrs) {
      this.attrs.putAll(attrs);
    }
  }
Esempio n. 19
0
  /** {@inheritDoc} */
  @Override
  public void setAttribute(Object key, @Nullable Object val) {
    A.notNull(key, "key");

    synchronized (attrs) {
      attrs.put(key, val);
    }
  }
Esempio n. 20
0
  /** {@inheritDoc} */
  @SuppressWarnings("unchecked")
  @Override
  public <K, V> V getAttribute(K key) {
    A.notNull(key, "key");

    synchronized (attrs) {
      return (V) attrs.get(key);
    }
  }
Esempio n. 21
0
  /** {@inheritDoc} */
  @Override
  public boolean removeCheckpoint(String key) throws GridException {
    A.notNull(key, "key");

    synchronized (mux) {
      if (closed) throw new GridException("Failed to remove checkpoint (session closed): " + this);
    }

    return ctx.checkpoint().removeCheckpoint(this, key);
  }
Esempio n. 22
0
  /** {@inheritDoc} */
  @Override
  public GridJobSibling getJobSibling(GridUuid jobId) throws GridException {
    A.notNull(jobId, "jobId");

    Collection<GridJobSibling> tmp = getJobSiblings();

    for (GridJobSibling sibling : tmp) if (sibling.getJobId().equals(jobId)) return sibling;

    return null;
  }
Esempio n. 23
0
  /** {@inheritDoc} */
  @SuppressWarnings("unchecked")
  @Override
  public <T> T loadCheckpoint(String key) throws GridException {
    A.notNull(key, "key");

    synchronized (mux) {
      if (closed) throw new GridException("Failed to load checkpoint (session closed): " + this);
    }

    return (T) ctx.checkpoint().loadCheckpoint(this, key);
  }
  /** {@inheritDoc} */
  @Nullable
  @Override
  public V put(K k, V v) {
    A.notNull(k, "k", v, "v");

    V ret = super.put(k, v);

    onPut();

    return ret;
  }
  /**
   * Set local batch size for this sequences.
   *
   * @param size Sequence batch size. Must be more then 0.
   */
  @Override
  public void batchSize(int size) {
    A.ensure(size > 0, " Batch size can't be less then 0: " + size);

    lock.lock();

    try {
      batchSize = size;
    } finally {
      lock.unlock();
    }
  }
  /** {@inheritDoc} */
  @Nullable
  @Override
  public V putIfAbsent(K k, V v) {
    A.notNull(k, "k", v, "v");

    V ret = super.putIfAbsent(k, v);

    // Handle size only if put succeeded.
    if (ret == null) onPut();

    return ret;
  }
Esempio n. 27
0
  /** {@inheritDoc} */
  @Override
  public boolean removeAttributeListener(GridTaskSessionAttributeListener lsnr) {
    A.notNull(lsnr, "lsnr");

    synchronized (mux) {
      List<GridTaskSessionAttributeListener> lsnrs =
          new ArrayList<GridTaskSessionAttributeListener>(this.lsnrs);

      boolean rmv = lsnrs.remove(lsnr);

      this.lsnrs = Collections.unmodifiableList(lsnrs);

      return rmv;
    }
  }
Esempio n. 28
0
  /** {@inheritDoc} */
  @Override
  public void setAttributes(Map<?, ?> attrs) throws GridException {
    A.notNull(attrs, "attrs");

    if (attrs.isEmpty()) return;

    // Note that there is no mux notification in this block.
    // The reason is that we wait for ordered attributes to
    // come back from task prior to notification. The notification
    // will happen in 'setInternal(...)' method.
    synchronized (mux) {
      this.attrs.putAll(attrs);
    }

    if (isTaskNode()) ctx.task().setAttributes(this, attrs);
  }
  /** {@inheritDoc} */
  @SuppressWarnings({"unchecked"})
  @Override
  public boolean contains(Object val) {
    A.notNull(val, "val");

    if (comp != null && strict) {
      for (V v : vals) {
        // Prefer equals to comparator.
        if (v.equals(val)) return true;

        if (comp.compare(v, (V) val) > 0) break;
      }

      return false;
    }

    return vals.contains(val);
  }
Esempio n. 30
0
  /**
   * Copy constructor.
   *
   * @param orig Copy to create this instance from.
   * @param newParams Optional array of new parameters to override the ondes from {@code orig}.
   */
  public GridifyArgumentAdapter(GridifyArgument orig, Object... newParams) {
    A.notNull(orig, "orig");

    cls = orig.getMethodClass();
    mtdName = orig.getMethodName();
    target = orig.getTarget();

    types = new Class[orig.getMethodParameterTypes().length];
    params = new Object[orig.getMethodParameters().length];

    System.arraycopy(orig.getMethodParameters(), 0, params, 0, params.length);
    System.arraycopy(orig.getMethodParameterTypes(), 0, types, 0, types.length);

    // Override parameters, if any.
    if (newParams.length > 0) {
      setMethodParameters(newParams);
    }
  }