/** {@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); }
/** {@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; } }
/** * @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; }
/** {@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; } }
/** {@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; } }
/** {@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()); } } } }
/** {@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); } }
/** {@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); } }
/** {@inheritDoc} */ @Override public void setAttributes(Map<?, ?> attrs) { A.notNull(attrs, "attrs"); synchronized (this.attrs) { this.attrs.putAll(attrs); } }
/** {@inheritDoc} */ @Override public void setAttribute(Object key, @Nullable Object val) { A.notNull(key, "key"); synchronized (attrs) { attrs.put(key, val); } }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public <K, V> V getAttribute(K key) { A.notNull(key, "key"); synchronized (attrs) { return (V) attrs.get(key); } }
/** {@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; }
/** {@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); }
/** {@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} */ @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; } }
/** {@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); }
/** @param attrs Attributes to set. */ public void setInternal(Map<?, ?> attrs) { A.notNull(attrs, "attrs"); if (attrs.isEmpty()) return; List<GridTaskSessionAttributeListener> lsnrs; synchronized (mux) { this.attrs.putAll(attrs); lsnrs = this.lsnrs; mux.notifyAll(); } for (Map.Entry<?, ?> entry : attrs.entrySet()) for (GridTaskSessionAttributeListener lsnr : lsnrs) lsnr.onAttributeSet(entry.getKey(), entry.getValue()); }
/** {@inheritDoc} */ @Override public void setAttribute(Object key, Object val) throws GridException { A.notNull(key, "key"); setAttributes(Collections.singletonMap(key, val)); }