/** {@inheritDoc} */
    @Nullable
    @Override
    public Map<String, Collection<?>> run(GridStreamerContext ctx, Collection<Quote> quotes)
        throws GridException {
      GridStreamerWindow win = ctx.window("stage1");

      // Add numbers to window.
      win.enqueueAll(quotes);

      Collection<Quote> polled = win.pollEvictedBatch();

      if (!polled.isEmpty()) {
        Map<String, Bar> map = new HashMap<>();

        for (Quote quote : polled) {
          String symbol = quote.symbol();

          Bar bar = map.get(symbol);

          if (bar == null) map.put(symbol, bar = new Bar(symbol));

          bar.update(quote.price());
        }

        return Collections.<String, Collection<?>>singletonMap(ctx.nextStageName(), map.values());
      }

      return null;
    }
  /** {@inheritDoc} */
  @Override
  public final Map<? extends GridJob, GridNode> map(List<GridNode> subgrid, Callable<T> arg)
      throws GridException {
    assert subgrid != null;
    assert !subgrid.isEmpty();

    GridJob job = createJob(arg);

    return Collections.singletonMap(job, balancer.getBalancedNode(job, null));
  }
示例#3
0
    /** {@inheritDoc} */
    @Override
    public Map<? extends GridComputeJob, GridNode> map(List<GridNode> subgrid, String arg)
        throws GridException {
      assert locNodeId != null;

      if (log.isInfoEnabled()) {
        log.info("Mapping jobs [subgrid=" + subgrid + ", arg=" + arg + ']');
      }

      GridNode remoteNode = null;

      for (GridNode node : subgrid) {
        if (!node.id().equals(locNodeId)) {
          remoteNode = node;
        }
      }

      return Collections.singletonMap(
          new GridComputeJobAdapter(locNodeId) {
            /** */
            @GridLocalNodeIdResource private UUID nodeId;

            /** {@inheritDoc} */
            @SuppressWarnings("NakedNotify")
            @Override
            public Serializable execute() throws GridException {
              assert nodeId != null;

              if (!nodeId.equals(argument(0))) {
                try {
                  synchronized (mux) {
                    mux.notifyAll();
                  }

                  Thread.sleep(Integer.MAX_VALUE);
                } catch (InterruptedException e) {
                  throw new GridComputeExecutionRejectedException(
                      "Expected interruption during execution.", e);
                }
              } else {
                return "success";
              }

              throw new GridComputeExecutionRejectedException(
                  "Expected exception during execution.");
            }
          },
          remoteNode);
    }
示例#4
0
 /** {@inheritDoc} */
 @SuppressWarnings({"unchecked"})
 @Override
 public Map<GridRichNode, Collection<K>> mapKeysToNodes(Collection<? extends K> keys) {
   return Collections.singletonMap(ctx.localNode(), (Collection<K>) keys);
 }
  /**
   * This method is called to map or split grid task into multiple grid jobs. This is the first
   * method that gets called when task execution starts.
   *
   * @param data Task execution argument. Can be {@code null}. This is the same argument as the one
   *     passed into {@code Grid#execute(...)} methods.
   * @param subgrid Nodes available for this task execution. Note that order of nodes is guaranteed
   *     to be randomized by container. This ensures that every time you simply iterate through grid
   *     nodes, the order of nodes will be random which over time should result into all nodes being
   *     used equally.
   * @return Map of grid jobs assigned to subgrid node. Unless {@link
   *     GridComputeTaskContinuousMapper} is injected into task, if {@code null} or empty map is
   *     returned, exception will be thrown.
   * @throws GridException If mapping could not complete successfully. This exception will be thrown
   *     out of {@link GridComputeTaskFuture#get()} method.
   */
  @Override
  public Map<? extends GridComputeJob, GridNode> map(
      List<GridNode> subgrid, @Nullable final Collection<Integer> data) throws GridException {
    assert !subgrid.isEmpty();

    // Give preference to wanted node. Otherwise, take the first one.
    GridNode targetNode =
        F.find(
            subgrid,
            subgrid.get(0),
            new GridPredicate<GridNode>() {
              @Override
              public boolean apply(GridNode e) {
                return preferredNode.equals(e.id());
              }
            });

    return Collections.singletonMap(
        new GridComputeJobAdapter() {
          @GridLoggerResource private GridLogger log;

          @GridInstanceResource private Grid grid;

          @Override
          public Object execute() throws GridException {
            log.info("Going to put data: " + data.size());

            GridCache<Object, Object> cache = grid.cache(cacheName);

            assert cache != null;

            Map<Integer, T2<Integer, Collection<Integer>>> putMap = groupData(data);

            for (Map.Entry<Integer, T2<Integer, Collection<Integer>>> entry : putMap.entrySet()) {
              T2<Integer, Collection<Integer>> pair = entry.getValue();

              Object affKey = pair.get1();

              // Group lock partition.
              try (GridCacheTx tx =
                  cache.txStartPartition(
                      cache.affinity().partition(affKey),
                      optimistic ? OPTIMISTIC : PESSIMISTIC,
                      REPEATABLE_READ,
                      0,
                      pair.get2().size())) {
                for (Integer val : pair.get2()) cache.put(val, val);

                tx.commit();
              }
            }

            log.info("Finished put data: " + data.size());

            return data;
          }

          /**
           * Groups values by partitions.
           *
           * @param data Data to put.
           * @return Grouped map.
           */
          private Map<Integer, T2<Integer, Collection<Integer>>> groupData(Iterable<Integer> data) {
            GridCache<Object, Object> cache = grid.cache(cacheName);

            Map<Integer, T2<Integer, Collection<Integer>>> res = new HashMap<>();

            for (Integer val : data) {
              int part = cache.affinity().partition(val);

              T2<Integer, Collection<Integer>> tup = res.get(part);

              if (tup == null) {
                tup = new T2<Integer, Collection<Integer>>(val, new LinkedList<Integer>());

                res.put(part, tup);
              }

              tup.get2().add(val);
            }

            return res;
          }
        },
        targetNode);
  }
示例#6
0
  /** {@inheritDoc} */
  @Override
  public void setAttribute(Object key, Object val) throws GridException {
    A.notNull(key, "key");

    setAttributes(Collections.singletonMap(key, val));
  }