Exemplo n.º 1
0
  /**
   * @param entries Entries.
   * @param resFut Result future.
   * @param activeKeys Active keys.
   * @param remaps Remaps count.
   */
  private void load0(
      Collection<? extends Map.Entry<K, V>> entries,
      final GridFutureAdapter<Object> resFut,
      final Collection<K> activeKeys,
      final int remaps) {
    assert entries != null;

    if (remaps >= MAX_REMAP_CNT) {
      resFut.onDone(new GridException("Failed to finish operation (too many remaps): " + remaps));

      return;
    }

    Map<GridNode, Collection<Map.Entry<K, V>>> mappings = new HashMap<>();

    boolean initPda = ctx.deploy().enabled() && jobPda == null;

    for (Map.Entry<K, V> entry : entries) {
      GridNode node;

      try {
        K key = entry.getKey();

        assert key != null;

        if (initPda) {
          jobPda = new DataLoaderPda(key, entry.getValue(), updater);

          initPda = false;
        }

        node = ctx.affinity().mapKeyToNode(cacheName, key);
      } catch (GridException e) {
        resFut.onDone(e);

        return;
      }

      if (node == null) {
        resFut.onDone(
            new GridTopologyException(
                "Failed to map key to node "
                    + "(no nodes with cache found in topology) [infos="
                    + entries.size()
                    + ", cacheName="
                    + cacheName
                    + ']'));

        return;
      }

      Collection<Map.Entry<K, V>> col = mappings.get(node);

      if (col == null) mappings.put(node, col = new ArrayList<>());

      col.add(entry);
    }

    for (final Map.Entry<GridNode, Collection<Map.Entry<K, V>>> e : mappings.entrySet()) {
      final UUID nodeId = e.getKey().id();

      Buffer buf = bufMappings.get(nodeId);

      if (buf == null) {
        Buffer old = bufMappings.putIfAbsent(nodeId, buf = new Buffer(e.getKey()));

        if (old != null) buf = old;
      }

      final Collection<Map.Entry<K, V>> entriesForNode = e.getValue();

      GridInClosure<GridFuture<?>> lsnr =
          new GridInClosure<GridFuture<?>>() {
            @Override
            public void apply(GridFuture<?> t) {
              try {
                t.get();

                for (Map.Entry<K, V> e : entriesForNode) activeKeys.remove(e.getKey());

                if (activeKeys.isEmpty()) resFut.onDone();
              } catch (GridException e1) {
                if (log.isDebugEnabled())
                  log.debug("Future finished with error [nodeId=" + nodeId + ", err=" + e1 + ']');

                if (cancelled) {
                  resFut.onDone(
                      new GridException(
                          "Data loader has been cancelled: " + GridDataLoaderImpl.this, e1));
                } else load0(entriesForNode, resFut, activeKeys, remaps + 1);
              }
            }
          };

      GridFutureAdapter<?> f;

      try {
        f = buf.update(entriesForNode, lsnr);
      } catch (GridInterruptedException e1) {
        resFut.onDone(e1);

        return;
      }

      if (ctx.discovery().node(nodeId) == null) {
        if (bufMappings.remove(nodeId, buf)) buf.onNodeLeft();

        if (f != null)
          f.onDone(
              new GridTopologyException(
                  "Failed to wait for request completion " + "(node has left): " + nodeId));
      }
    }
  }
  // For each Tweet that is read, check threshold of the stock and apply
  @Override
  public void execute(Tuple input) {
    String userId = input.getString(0);
    String displayname = input.getString(1);
    String hashtag_all = input.getString(2);
    String tweet = input.getString(3);
    String created = input.getString(4);
    String longitude = input.getString(5);
    String latitude = input.getString(6);
    String language = input.getString(7);
    String fullTweet = input.getString(8);

    if (hashtag_all.length() == 0) {
      System.out.println("Skipping tweet...unable to find hashtag from it:" + tweet);
      collector.ack(input);
      return;
    }

    String hashtags[] = hashtag_all.split(" ");

    for (String hashtag : hashtags) {

      System.out.println(
          "RuleBolt received event displayname: "
              + displayname
              + " hashtag: "
              + hashtag
              + " tweet: "
              + tweet);
      // double latitude = input.getDoubleByField("latitude");
      // long correlationId = input.getLongByField("correlationId");
      // int truckId = input.getIntegerByField("truckId");

      // save event to our Map of events and retrive how many times its been mentioned in tweets
      // tweeted
      twitterEvents.putIfAbsent(hashtag, new AtomicInteger(0));
      int numTimesStockTweeted = twitterEvents.get(hashtag).incrementAndGet();

      // query HBase table for threshold for the stock symbol that was tweeted about
      int threshold = findThresholdForStock(hashtag);
      // int threshold = DEFAULT_ALERT_THRESHOLD;

      System.out.println(
          "\n\n\n\n\n\n\nStock: "
              + hashtag
              + " now has count: "
              + numTimesStockTweeted
              + ", threshold = "
              + threshold
              + " structure: "
              + twitterEvents
              + "\n\n\n\n\n\n\n");

      // check if this event takes the tweet volume for this stock above threshold
      if (numTimesStockTweeted > threshold) {
        int unixTime = (int) (System.currentTimeMillis() / 1000L);
        String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        String upsert =
            String.format(
                "upsert into alerts values(%d, '%s', '%s', %d)",
                unixTime, hashtag, timeStamp, numTimesStockTweeted);
        System.out.println(
            "ALERT!!! Stock: "
                + hashtag
                + " exceeded limit: "
                + threshold
                + " as it has count: "
                + numTimesStockTweeted
                + " on: "
                + timeStamp);

        runHbaseUpsert(upsert);

        createSolrAlert(userId, created, hashtag);
      }
    }

    collector.ack(input);
  }