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));
}
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);
}
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);
}
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);
}
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);
}
Aggregations