use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.
the class PulsarKafkaConsumer method seek.
@Override
public void seek(TopicPartition partition, long offset) {
MessageId msgId = MessageIdUtils.getMessageId(offset);
org.apache.pulsar.client.api.Consumer<byte[]> c = consumers.get(partition);
if (c == null) {
throw new IllegalArgumentException("Cannot seek on a partition where we are not subscribed");
}
try {
c.seek(msgId);
} catch (PulsarClientException e) {
throw new RuntimeException(e);
}
}
use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.
the class ServiceRequestManagerTest method testSubmitRequest.
@Test
public void testSubmitRequest() throws Exception {
ServiceRequest request = ServiceRequest.newBuilder().build();
MessageId msgId = mock(MessageId.class);
when(producer.sendAsync(any(byte[].class))).thenReturn(CompletableFuture.completedFuture(msgId));
CompletableFuture<MessageId> submitFuture = reqMgr.submitRequest(request);
assertSame(msgId, submitFuture.get());
verify(producer, times(1)).sendAsync(any(byte[].class));
}
use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.
the class FunctionMetaDataManager method submit.
@VisibleForTesting
CompletableFuture<RequestResult> submit(Request.ServiceRequest serviceRequest) {
ServiceRequestInfo serviceRequestInfo = ServiceRequestInfo.of(serviceRequest);
CompletableFuture<MessageId> messageIdCompletableFuture = this.serviceRequestManager.submitRequest(serviceRequest);
serviceRequestInfo.setCompletableFutureRequestMessageId(messageIdCompletableFuture);
CompletableFuture<RequestResult> requestResultCompletableFuture = new CompletableFuture<>();
serviceRequestInfo.setRequestResultCompletableFuture(requestResultCompletableFuture);
this.pendingServiceRequests.put(serviceRequestInfo.getServiceRequest().getRequestId(), serviceRequestInfo);
return requestResultCompletableFuture;
}
use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.
the class FunctionsImpl method triggerFunction.
@POST
@Path("/{tenant}/{namespace}/{functionName}/trigger")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response triggerFunction(@PathParam("tenant") final String tenant, @PathParam("namespace") final String namespace, @PathParam("name") final String functionName, @FormDataParam("data") final String input, @FormDataParam("dataStream") final InputStream uploadedInputStream) {
FunctionConfig functionConfig;
// validate parameters
try {
validateTriggerRequestParams(tenant, namespace, functionName, input, uploadedInputStream);
} catch (IllegalArgumentException e) {
log.error("Invalid trigger function request @ /{}/{}/{}", tenant, namespace, functionName, e);
return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getMessage())).build();
}
FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
if (!functionMetaDataManager.containsFunction(tenant, namespace, functionName)) {
log.error("Function in getFunction does not exist @ /{}/{}/{}", tenant, namespace, functionName);
return Response.status(Status.NOT_FOUND).type(MediaType.APPLICATION_JSON).entity(new ErrorData(String.format("Function %s doesn't exist", functionName))).build();
}
FunctionMetaData functionMetaData = functionMetaDataManager.getFunctionMetaData(tenant, namespace, functionName);
String inputTopicToWrite;
if (functionMetaData.getFunctionConfig().getInputsList().size() > 0) {
inputTopicToWrite = functionMetaData.getFunctionConfig().getInputsList().get(0);
} else {
inputTopicToWrite = functionMetaData.getFunctionConfig().getCustomSerdeInputs().entrySet().iterator().next().getKey();
}
String outputTopic = functionMetaData.getFunctionConfig().getOutput();
Reader reader = null;
Producer producer = null;
try {
if (outputTopic != null && !outputTopic.isEmpty()) {
reader = worker().getClient().newReader().topic(outputTopic).startMessageId(MessageId.latest).create();
}
producer = worker().getClient().newProducer().topic(inputTopicToWrite).create();
byte[] targetArray;
if (uploadedInputStream != null) {
targetArray = new byte[uploadedInputStream.available()];
uploadedInputStream.read(targetArray);
} else {
targetArray = input.getBytes();
}
MessageId msgId = producer.send(targetArray);
if (reader == null) {
return Response.status(Status.OK).build();
}
long curTime = System.currentTimeMillis();
long maxTime = curTime + 1000;
while (curTime < maxTime) {
Message msg = reader.readNext(10000, TimeUnit.MILLISECONDS);
if (msg == null)
break;
if (msg.getProperties().containsKey("__pfn_input_msg_id__") && msg.getProperties().containsKey("__pfn_input_topic__")) {
MessageId newMsgId = MessageId.fromByteArray(Base64.getDecoder().decode((String) msg.getProperties().get("__pfn_input_msg_id__")));
if (msgId.equals(newMsgId) && msg.getProperties().get("__pfn_input_topic__").equals(inputTopicToWrite)) {
return Response.status(Status.OK).entity(msg.getData()).build();
}
}
curTime = System.currentTimeMillis();
}
return Response.status(Status.REQUEST_TIMEOUT).build();
} catch (Exception e) {
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
} finally {
if (reader != null) {
reader.closeAsync();
}
if (producer != null) {
producer.closeAsync();
}
}
}
use of org.apache.pulsar.client.api.MessageId in project incubator-pulsar by apache.
the class ConsumerImpl method internalGetLastMessageIdAsync.
private void internalGetLastMessageIdAsync(final Backoff backoff, final AtomicLong remainingTime, CompletableFuture<MessageId> future) {
ClientCnx cnx = cnx();
if (isConnected() && cnx != null) {
if (!Commands.peerSupportsGetLastMessageId(cnx.getRemoteEndpointProtocolVersion())) {
future.completeExceptionally(new PulsarClientException.NotSupportedException("GetLastMessageId Not supported for ProtocolVersion: " + cnx.getRemoteEndpointProtocolVersion()));
}
long requestId = client.newRequestId();
ByteBuf getLastIdCmd = Commands.newGetLastMessageId(consumerId, requestId);
log.info("[{}][{}] Get topic last message Id", topic, subscription);
cnx.sendGetLastMessageId(getLastIdCmd, requestId).thenAccept((result) -> {
log.info("[{}][{}] Successfully getLastMessageId {}:{}", topic, subscription, result.getLedgerId(), result.getEntryId());
future.complete(new MessageIdImpl(result.getLedgerId(), result.getEntryId(), result.getPartition()));
}).exceptionally(e -> {
log.error("[{}][{}] Failed getLastMessageId command", topic, subscription);
future.completeExceptionally(e.getCause());
return null;
});
} else {
long nextDelay = Math.min(backoff.next(), remainingTime.get());
if (nextDelay <= 0) {
future.completeExceptionally(new PulsarClientException.TimeoutException("Could not getLastMessageId within configured timeout."));
return;
}
((ScheduledExecutorService) listenerExecutor).schedule(() -> {
log.warn("[{}] [{}] Could not get connection while getLastMessageId -- Will try again in {} ms", topic, getHandlerName(), nextDelay);
remainingTime.addAndGet(-nextDelay);
internalGetLastMessageIdAsync(backoff, remainingTime, future);
}, nextDelay, TimeUnit.MILLISECONDS);
}
}
Aggregations