/** {@inheritDoc} */ @SuppressWarnings({"unchecked"}) @Override public void listenAsync(@Nullable final GridInClosure<? super GridFuture<R>> lsnr) { if (lsnr != null) { checkValid(); boolean done; synchronized (mux) { done = this.done; if (!done) lsnrs.add(lsnr); } if (done) { try { if (syncNotify) notifyListener(lsnr); else ctx.closure() .runLocalSafe( new GPR() { @Override public void run() { notifyListener(lsnr); } }, true); } catch (IllegalStateException ignore) { U.warn( null, "Future notification will not proceed because grid is stopped: " + ctx.gridName()); } } } }
/** @param ctx Kernal context. */ public GridFutureAdapter(GridKernalContext ctx) { assert ctx != null; this.ctx = ctx; log = ctx.log(getClass()); }
/** {@inheritDoc} */ @Override public boolean heldcc() { if (ctx == null) return false; if (job == null) job = ctx.job().activeJob(jobId); return job != null && job.held(); }
/** {@inheritDoc} */ @Override public void callcc() { if (ctx != null) { if (job == null) job = ctx.job().activeJob(jobId); if (job != null) // Execute in the same thread. job.execute(); } }
/** {@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 <T> T holdcc(long timeout) { if (ctx != null) { if (job == null) job = ctx.job().activeJob(jobId); // Completed? if (job != null) { if (timeout > 0 && !job.isDone()) { final long endTime = System.currentTimeMillis() + timeout; // Overflow. if (endTime > 0) { ctx.timeout() .addTimeoutObject( new GridTimeoutObject() { private final GridUuid id = GridUuid.randomUuid(); @Override public GridUuid timeoutId() { return id; } @Override public long endTime() { return endTime; } @Override public void onTimeout() { callcc(); } }); } } job.hold(); } } return null; }
/** Notifies all registered listeners. */ private void notifyListeners() { final Collection<GridInClosure<? super GridFuture<R>>> tmp; synchronized (mux) { tmp = new ArrayList<GridInClosure<? super GridFuture<R>>>(lsnrs); } boolean concurNotify = this.concurNotify; boolean syncNotify = this.syncNotify; if (concurNotify) { for (final GridInClosure<? super GridFuture<R>> lsnr : tmp) ctx.closure() .runLocalSafe( new GPR() { @Override public void run() { notifyListener(lsnr); } }, true); } else { // Always notify in the thread different from start thread. if (Thread.currentThread() == thread && !syncNotify) { ctx.closure() .runLocalSafe( new GPR() { @Override public void run() { // Since concurrent notifications are off, we notify // all listeners in one thread. for (GridInClosure<? super GridFuture<R>> lsnr : tmp) notifyListener(lsnr); } }, true); } else { for (GridInClosure<? super GridFuture<R>> lsnr : tmp) notifyListener(lsnr); } } }
/** {@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 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); }
/** * Notifies single listener. * * @param lsnr Listener. */ private void notifyListener(GridInClosure<? super GridFuture<R>> lsnr) { assert lsnr != null; try { lsnr.apply(this); } catch (IllegalStateException ignore) { U.warn( null, "Failed to notify listener (grid is stopped) [grid=" + ctx.gridName() + ", lsnr=" + lsnr + ']'); } catch (RuntimeException e) { U.error(log, "Failed to notify listener: " + lsnr, e); throw e; } catch (Error e) { U.error(log, "Failed to notify listener: " + lsnr, e); throw e; } }
/** {@inheritDoc} */ @Override public Collection<UUID> getTopology() throws GridException { return F.nodeIds(ctx.topology().getTopology(this, ctx.discovery().allNodes())); }
/** {@inheritDoc} */ @Override public boolean isTaskNode() { return taskNodeId.equals(ctx.discovery().localNode().id()); }