Search in sources :

Example 26 with PubSubMessage

use of com.yahoo.bullet.pubsub.PubSubMessage in project bullet-storm by yahoo.

the class DRPCQuerySubscriberTest method testReadingFromSpout.

@Test
public void testReadingFromSpout() throws Exception {
    // The MockDRPCSpout makes messages adds 2 zero based sequences with id foo and given string content.
    // It adds to metadata a string JSON with id: "fake" + foo, host: "testHost" and port: a total count of messages.
    injectedMockSpout.addMessageParts("foo", "{}", "{'duration': 2000}");
    injectedMockSpout.addMessageParts("bar", "{}");
    PubSubMessage actual = subscriber.receive();
    Assert.assertEquals(actual.getId(), "foo");
    Assert.assertEquals(actual.getSequence(), 0);
    Assert.assertEquals(actual.getContent(), "{}");
    Assert.assertFalse(actual.hasSignal());
    Assert.assertTrue(actual.hasMetadata());
    Metadata metadata = actual.getMetadata();
    Assert.assertEquals(metadata.getContent(), makeReturnInfo("fakefoo", "testHost", 0));
    actual = subscriber.receive();
    Assert.assertEquals(actual.getId(), "foo");
    Assert.assertEquals(actual.getSequence(), 1);
    Assert.assertEquals(actual.getContent(), "{'duration': 2000}");
    Assert.assertFalse(actual.hasSignal());
    Assert.assertTrue(actual.hasMetadata());
    metadata = actual.getMetadata();
    Assert.assertEquals(metadata.getContent(), makeReturnInfo("fakefoo", "testHost", 1));
    actual = subscriber.receive();
    Assert.assertEquals(actual.getId(), "bar");
    Assert.assertEquals(actual.getSequence(), 0);
    Assert.assertEquals(actual.getContent(), "{}");
    Assert.assertFalse(actual.hasSignal());
    Assert.assertTrue(actual.hasMetadata());
    metadata = actual.getMetadata();
    Assert.assertEquals(metadata.getContent(), makeReturnInfo("fakebar", "testHost", 2));
}
Also used : PubSubMessage(com.yahoo.bullet.pubsub.PubSubMessage) Metadata(com.yahoo.bullet.pubsub.Metadata) Test(org.testng.annotations.Test)

Example 27 with PubSubMessage

use of com.yahoo.bullet.pubsub.PubSubMessage in project bullet-storm by yahoo.

the class LoopBolt method execute.

@Override
public void execute(Tuple tuple) {
    String id = tuple.getString(TopologyConstants.ID_POSITION);
    Metadata metadata = (Metadata) tuple.getValue(TopologyConstants.METADATA_POSITION);
    log.info("Looping back metadata with signal {} for {}", metadata.getSignal(), id);
    publish(new PubSubMessage(id, null, metadata), tuple);
}
Also used : Metadata(com.yahoo.bullet.pubsub.Metadata) PubSubMessage(com.yahoo.bullet.pubsub.PubSubMessage)

Example 28 with PubSubMessage

use of com.yahoo.bullet.pubsub.PubSubMessage in project bullet-storm by yahoo.

the class ResultBolt method execute.

@Override
public void execute(Tuple tuple) {
    String id = tuple.getString(TopologyConstants.ID_POSITION);
    String result = tuple.getString(TopologyConstants.RESULT_POSITION);
    Metadata metadata = (Metadata) tuple.getValue(TopologyConstants.RESULT_METADATA_POSITION);
    publish(new PubSubMessage(id, result, metadata), tuple);
}
Also used : Metadata(com.yahoo.bullet.pubsub.Metadata) PubSubMessage(com.yahoo.bullet.pubsub.PubSubMessage)

Example 29 with PubSubMessage

use of com.yahoo.bullet.pubsub.PubSubMessage in project bullet-storm by yahoo.

the class DRPCQuerySubscriber method getMessages.

@Override
public List<PubSubMessage> getMessages() throws PubSubException {
    // Try and read from DRPC. The DRPCSpout does a sleep for 1 ms if there are no tuples, so we don't have to do it.
    spout.nextTuple();
    if (!collector.haveOutput()) {
        return null;
    }
    // The DRPCSpout only should have emitted one tuple
    List<List<Object>> tuples = collector.reset();
    log.debug("Have a message through DRPC {}", tuples);
    List<Object> tupleAndID = tuples.get(0);
    // The first object is the actual DRPCSpout tuple and the second is the DRPC messageID.
    List<Object> tuple = (List<Object>) tupleAndID.get(0);
    Object drpcID = tupleAndID.get(1);
    // The first object in the tuple is our PubSubMessage as JSON
    String pubSubMessageJSON = (String) tuple.get(0);
    // The second object in the tuple is the serialized returnInfo added by the DRPCSpout
    String returnInfo = (String) tuple.get(1);
    log.debug("Read message\n{}\nfrom DRPC with return information {}", pubSubMessageJSON, returnInfo);
    PubSubMessage pubSubMessage = PubSubMessage.fromJSON(pubSubMessageJSON);
    // Add returnInfo as metadata. Cannot add it to pubSubMessage
    String id = pubSubMessage.getId();
    String content = pubSubMessage.getContent();
    int sequence = pubSubMessage.getSequence();
    PubSubMessage message = new PubSubMessage(id, content, new Metadata(null, returnInfo), sequence);
    emittedIDs.put(ImmutablePair.of(id, sequence), drpcID);
    return Collections.singletonList(message);
}
Also used : PubSubMessage(com.yahoo.bullet.pubsub.PubSubMessage) Metadata(com.yahoo.bullet.pubsub.Metadata) List(java.util.List)

Example 30 with PubSubMessage

use of com.yahoo.bullet.pubsub.PubSubMessage in project bullet-storm by yahoo.

the class DRPCQueryResultPubscriber method handleResponse.

private void handleResponse(String id, Response response) {
    if (response == null || response.getStatusCode() != OK_200) {
        log.error("Handling error for id {} with response {}", id, response);
        responses.offer(new PubSubMessage(id, DRPCError.CANNOT_REACH_DRPC.asJSONClip()));
        return;
    }
    log.info("Received for id {}: {} {}", response.getStatusCode(), id, response.getStatusText());
    String body = response.getResponseBody();
    PubSubMessage message = PubSubMessage.fromJSON(body);
    log.debug("Received for id {}:\n{}", message.getId(), message.getContent());
    responses.offer(message);
}
Also used : PubSubMessage(com.yahoo.bullet.pubsub.PubSubMessage)

Aggregations

PubSubMessage (com.yahoo.bullet.pubsub.PubSubMessage)30 Test (org.testng.annotations.Test)22 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)14 Response (org.asynchttpclient.Response)14 Metadata (com.yahoo.bullet.pubsub.Metadata)11 RESTPubSubTest.getOkResponse (com.yahoo.bullet.pubsub.rest.RESTPubSubTest.getOkResponse)10 BoundRequestBuilder (org.asynchttpclient.BoundRequestBuilder)10 RESTPubSubTest.getNotOkResponse (com.yahoo.bullet.pubsub.rest.RESTPubSubTest.getNotOkResponse)8 Tuple (org.apache.storm.tuple.Tuple)4 DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)4 PubSubException (com.yahoo.bullet.pubsub.PubSubException)3 ArrayList (java.util.ArrayList)3 TupleUtils.makeTuple (com.yahoo.bullet.storm.testing.TupleUtils.makeTuple)2 IOException (java.io.IOException)2 List (java.util.List)1 Values (org.apache.storm.tuple.Values)1 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)1 DefaultAsyncHttpClientConfig (org.asynchttpclient.DefaultAsyncHttpClientConfig)1