use of org.apache.pulsar.common.api.proto.CommandGetSchema in project pulsar by apache.
the class ServerCnx method handleGetSchema.
@Override
protected void handleGetSchema(CommandGetSchema commandGetSchema) {
if (log.isDebugEnabled()) {
if (commandGetSchema.hasSchemaVersion()) {
log.debug("Received CommandGetSchema call from {}, schemaVersion: {}, topic: {}, requestId: {}", remoteAddress, new String(commandGetSchema.getSchemaVersion()), commandGetSchema.getTopic(), commandGetSchema.getRequestId());
} else {
log.debug("Received CommandGetSchema call from {}, schemaVersion: {}, topic: {}, requestId: {}", remoteAddress, null, commandGetSchema.getTopic(), commandGetSchema.getRequestId());
}
}
long requestId = commandGetSchema.getRequestId();
SchemaVersion schemaVersion = SchemaVersion.Latest;
if (commandGetSchema.hasSchemaVersion()) {
if (commandGetSchema.getSchemaVersion().length == 0) {
commandSender.sendGetSchemaErrorResponse(requestId, ServerError.IncompatibleSchema, "Empty schema version");
return;
}
schemaVersion = schemaService.versionFromBytes(commandGetSchema.getSchemaVersion());
}
String schemaName;
try {
schemaName = TopicName.get(commandGetSchema.getTopic()).getSchemaName();
} catch (Throwable t) {
commandSender.sendGetSchemaErrorResponse(requestId, ServerError.InvalidTopicName, t.getMessage());
return;
}
schemaService.getSchema(schemaName, schemaVersion).thenAccept(schemaAndMetadata -> {
if (schemaAndMetadata == null) {
commandSender.sendGetSchemaErrorResponse(requestId, ServerError.TopicNotFound, "Topic not found or no-schema");
} else {
commandSender.sendGetSchemaResponse(requestId, SchemaInfoUtil.newSchemaInfo(schemaName, schemaAndMetadata.schema), schemaAndMetadata.version);
}
}).exceptionally(ex -> {
commandSender.sendGetSchemaErrorResponse(requestId, ServerError.UnknownError, ex.getMessage());
return null;
});
}
use of org.apache.pulsar.common.api.proto.CommandGetSchema in project pulsar by apache.
the class LookupProxyHandler method handleGetSchema.
public void handleGetSchema(CommandGetSchema commandGetSchema) {
GET_SCHEMA_REQUESTS.inc();
if (log.isDebugEnabled()) {
log.debug("[{}] Received GetSchema {}", clientAddress, commandGetSchema);
}
final long clientRequestId = commandGetSchema.getRequestId();
String serviceUrl = getBrokerServiceUrl(clientRequestId);
String topic = commandGetSchema.getTopic();
Optional<SchemaVersion> schemaVersion;
if (commandGetSchema.hasSchemaVersion()) {
schemaVersion = Optional.of(commandGetSchema.getSchemaVersion()).map(BytesSchemaVersion::of);
} else {
schemaVersion = Optional.empty();
}
if (!StringUtils.isNotBlank(serviceUrl)) {
return;
}
InetSocketAddress addr = getAddr(serviceUrl, clientRequestId);
if (addr == null) {
return;
}
if (log.isDebugEnabled()) {
log.debug("Getting connections to '{}' for getting schema of topic '{}' with clientReq Id '{}'", addr, topic, clientRequestId);
}
proxyConnection.getConnectionPool().getConnection(addr).thenAccept(clientCnx -> {
// Connected to backend broker
long requestId = proxyConnection.newRequestId();
ByteBuf command;
command = Commands.newGetSchema(requestId, topic, schemaVersion);
clientCnx.sendGetRawSchema(command, requestId).whenComplete((r, t) -> {
if (t != null) {
log.warn("[{}] Failed to get schema {}: {}", clientAddress, topic, t);
proxyConnection.ctx().writeAndFlush(Commands.newError(clientRequestId, ServerError.ServiceNotReady, t.getMessage()));
} else {
proxyConnection.ctx().writeAndFlush(Commands.newGetSchemaResponse(clientRequestId, r));
}
proxyConnection.getConnectionPool().releaseConnection(clientCnx);
});
}).exceptionally(ex -> {
// Failed to connect to backend broker
proxyConnection.ctx().writeAndFlush(Commands.newError(clientRequestId, ServerError.ServiceNotReady, ex.getMessage()));
return null;
});
}
use of org.apache.pulsar.common.api.proto.CommandGetSchema in project pulsar by apache.
the class Commands method newGetSchema.
public static ByteBuf newGetSchema(long requestId, String topic, Optional<SchemaVersion> version) {
BaseCommand cmd = localCmd(Type.GET_SCHEMA);
CommandGetSchema schema = cmd.setGetSchema().setRequestId(requestId).setTopic(topic);
version.ifPresent(schemaVersion -> schema.setSchemaVersion(schemaVersion.bytes()));
return serializeWithSize(cmd);
}
Aggregations