Search in sources :

Example 1 with TopicId

use of io.cdap.cdap.proto.id.TopicId in project cdap by caskdata.

the class TetheringServerHandler method connectControlChannel.

/**
 * Sends control commands to the client.
 */
@GET
@Path("/tethering/controlchannels/{peer}")
public void connectControlChannel(FullHttpRequest request, HttpResponder responder, @PathParam("peer") String peer, @QueryParam("messageId") String messageId) throws IOException, NotImplementedException, PeerNotFoundException, ForbiddenException, BadRequestException {
    checkTetheringServerEnabled();
    store.updatePeerTimestamp(peer);
    TetheringStatus tetheringStatus = store.getPeer(peer).getTetheringStatus();
    if (tetheringStatus == TetheringStatus.PENDING) {
        throw new PeerNotFoundException(String.format("Peer %s not found", peer));
    } else if (tetheringStatus == TetheringStatus.REJECTED) {
        responder.sendStatus(HttpResponseStatus.FORBIDDEN);
        throw new ForbiddenException(String.format("Peer %s is not authorized", peer));
    }
    List<TetheringControlResponse> controlResponses = new ArrayList<>();
    MessageFetcher fetcher = messagingContext.getMessageFetcher();
    TopicId topic = new TopicId(NamespaceId.SYSTEM.getNamespace(), topicPrefix + peer);
    String lastMessageId = messageId;
    try (CloseableIterator<Message> iterator = fetcher.fetch(topic.getNamespace(), topic.getTopic(), 1, messageId)) {
        while (iterator.hasNext()) {
            Message message = iterator.next();
            TetheringControlMessage controlMessage = GSON.fromJson(message.getPayloadAsString(StandardCharsets.UTF_8), TetheringControlMessage.class);
            lastMessageId = message.getId();
            controlResponses.add(new TetheringControlResponse(lastMessageId, controlMessage));
        }
    } catch (TopicNotFoundException e) {
        LOG.warn("Received control connection from peer {} that's not tethered", peer);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(String.format("Invalid message id %s", messageId));
    }
    if (controlResponses.isEmpty()) {
        controlResponses.add(new TetheringControlResponse(lastMessageId, new TetheringControlMessage(TetheringControlMessage.Type.KEEPALIVE)));
    }
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(controlResponses.toArray(new TetheringControlResponse[0]), TetheringControlResponse[].class));
}
Also used : ForbiddenException(io.cdap.cdap.common.ForbiddenException) MessageFetcher(io.cdap.cdap.api.messaging.MessageFetcher) Message(io.cdap.cdap.api.messaging.Message) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) ArrayList(java.util.ArrayList) BadRequestException(io.cdap.cdap.common.BadRequestException) TopicId(io.cdap.cdap.proto.id.TopicId) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with TopicId

use of io.cdap.cdap.proto.id.TopicId in project cdap by caskdata.

the class TetheringHandler method deleteTether.

/**
 * Deletes a tether.
 */
@DELETE
@Path("/tethering/connections/{peer}")
public void deleteTether(HttpRequest request, HttpResponder responder, @PathParam("peer") String peer) throws PeerNotFoundException, IOException {
    store.deletePeer(peer);
    // Remove per-peer tethering topic if we're running on the server
    if (cConf.getBoolean(Constants.Tethering.TETHERING_SERVER_ENABLED)) {
        TopicId topic = new TopicId(NamespaceId.SYSTEM.getNamespace(), topicPrefix + peer);
        try {
            messagingService.deleteTopic(topic);
        } catch (TopicNotFoundException e) {
            LOG.info("Topic {} was not found", topic.getTopic());
        }
    }
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) TopicId(io.cdap.cdap.proto.id.TopicId) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 3 with TopicId

use of io.cdap.cdap.proto.id.TopicId in project cdap by caskdata.

the class RuntimeClientServerTest method testMixedSendMessage.

@Test
public void testMixedSendMessage() throws Exception {
    ProgramRunId programRunId = NamespaceId.DEFAULT.app("app").workflow("workflow").run(RunIds.generate());
    TopicId topicId = NamespaceId.SYSTEM.topic("topic");
    List<Message> messages = new ArrayList<>();
    // Generate a mix of large and small messages
    for (int i = 0; i < 10; i++) {
        messages.add(createMessage(i + 1));
    }
    for (int i = 0; i < 10; i++) {
        messages.add(createMessage(i + RuntimeClient.CHUNK_SIZE));
    }
    runtimeClient.sendMessages(programRunId, topicId, messages.iterator());
    assertMessages(topicId, messages);
}
Also used : Message(io.cdap.cdap.api.messaging.Message) ArrayList(java.util.ArrayList) TopicId(io.cdap.cdap.proto.id.TopicId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) Test(org.junit.Test)

Example 4 with TopicId

use of io.cdap.cdap.proto.id.TopicId in project cdap by caskdata.

the class RuntimeClientServerTest method testLargeMessage.

@Test
public void testLargeMessage() throws Exception {
    ProgramRunId programRunId = NamespaceId.DEFAULT.app("app").workflow("workflow").run(RunIds.generate());
    TopicId topicId = NamespaceId.SYSTEM.topic(TEST_TOPIC);
    List<Message> messages = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        messages.add(createMessage(RuntimeClient.CHUNK_SIZE * 2));
    }
    runtimeClient.sendMessages(programRunId, topicId, messages.iterator());
    assertMessages(topicId, messages);
}
Also used : Message(io.cdap.cdap.api.messaging.Message) ArrayList(java.util.ArrayList) TopicId(io.cdap.cdap.proto.id.TopicId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) Test(org.junit.Test)

Example 5 with TopicId

use of io.cdap.cdap.proto.id.TopicId in project cdap by caskdata.

the class RuntimeClientServerTest method testLogMessage.

@Test
public void testLogMessage() throws Exception {
    ProgramRunId programRunId = NamespaceId.DEFAULT.app("app").workflow("workflow").run(RunIds.generate());
    TopicId topicId = NamespaceId.SYSTEM.topic(cConf.get(Constants.Logging.TMS_TOPIC_PREFIX) + "0");
    List<Message> messages = IntStream.range(0, 100).mapToObj(this::createMessage).collect(Collectors.toList());
    runtimeClient.sendMessages(programRunId, topicId, messages.iterator());
    List<String> expected = messages.stream().map(Message::getPayloadAsString).collect(Collectors.toList());
    Assert.assertEquals(expected, logEntries);
}
Also used : Message(io.cdap.cdap.api.messaging.Message) TopicId(io.cdap.cdap.proto.id.TopicId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) Test(org.junit.Test)

Aggregations

TopicId (io.cdap.cdap.proto.id.TopicId)76 Test (org.junit.Test)42 TopicMetadata (io.cdap.cdap.messaging.TopicMetadata)40 ArrayList (java.util.ArrayList)30 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)22 RawMessage (io.cdap.cdap.messaging.data.RawMessage)16 TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)14 IOException (java.io.IOException)13 MessageId (io.cdap.cdap.messaging.data.MessageId)12 Path (javax.ws.rs.Path)12 HashMap (java.util.HashMap)8 TopicAlreadyExistsException (io.cdap.cdap.api.messaging.TopicAlreadyExistsException)7 BadRequestException (io.cdap.cdap.common.BadRequestException)6 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)6 Injector (com.google.inject.Injector)5 Message (io.cdap.cdap.api.messaging.Message)5 MessagingService (io.cdap.cdap.messaging.MessagingService)5 StoreRequest (io.cdap.cdap.messaging.StoreRequest)5 NoOpMetricsCollectionService (io.cdap.cdap.common.metrics.NoOpMetricsCollectionService)4 TimeProvider (io.cdap.cdap.common.utils.TimeProvider)4