use of com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException in project registry by hortonworks.
the class SchemaVersionLifecycleManager method fetchSchemaVersionInfo.
private SchemaVersionInfo fetchSchemaVersionInfo(Long id) throws SchemaNotFoundException {
StorableKey storableKey = new StorableKey(SchemaVersionStorable.NAME_SPACE, SchemaVersionStorable.getPrimaryKey(id));
SchemaVersionStorable versionedSchema = storageManager.get(storableKey);
if (versionedSchema == null) {
throw new SchemaNotFoundException("No Schema version exists with id " + id);
}
return versionedSchema.toSchemaVersionInfo();
}
use of com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException in project registry by hortonworks.
the class SchemaVersionLifecycleManager method mergeSchemaVersion.
public SchemaVersionMergeResult mergeSchemaVersion(Long schemaVersionId, SchemaVersionMergeStrategy schemaVersionMergeStrategy) throws SchemaNotFoundException, IncompatibleSchemaException {
try {
SchemaVersionInfo schemaVersionInfo = getSchemaVersionInfo(new SchemaIdVersion(schemaVersionId));
SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaVersionInfo.getName());
Set<SchemaBranch> schemaBranches = getSchemaBranches(schemaVersionId).stream().filter(schemaBranch -> {
try {
return !getRootVersion(schemaBranch).getId().equals(schemaVersionId);
} catch (SchemaNotFoundException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toSet());
if (schemaBranches.size() > 1) {
throw new SchemaVersionMergeException(String.format("Can't determine a unique schema branch for schema version id : '%s'", schemaVersionId));
} else if (schemaBranches.size() == 0) {
throw new SchemaVersionMergeException(String.format("Schema version id : '%s' is not associated with any branch", schemaVersionId));
}
Long schemaBranchId = schemaBranches.iterator().next().getId();
SchemaBranch schemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchId));
if (schemaVersionMergeStrategy.equals(SchemaVersionMergeStrategy.PESSIMISTIC)) {
SchemaVersionInfo latestSchemaVersion = getLatestEnabledSchemaVersionInfo(SchemaBranch.MASTER_BRANCH, schemaMetadataInfo.getSchemaMetadata().getName());
SchemaVersionInfo rootSchemaVersion = getRootVersion(schemaBranch);
if (!latestSchemaVersion.getId().equals(rootSchemaVersion.getId())) {
throw new SchemaVersionMergeException(String.format("The latest version of '%s' is different from the root version of the branch : '%s'", SchemaBranch.MASTER_BRANCH, schemaMetadataInfo.getSchemaMetadata().getName()));
}
}
byte[] initializedStateDetails;
try {
initializedStateDetails = ObjectMapperUtils.serialize(new InitializedStateDetails(schemaBranch.getName(), schemaVersionInfo.getId()));
} catch (JsonProcessingException e) {
throw new RuntimeException(String.format("Failed to serialize initializedState for %s and %s", schemaBranch.getName(), schemaVersionInfo.getId()));
}
SchemaVersionInfo createdSchemaVersionInfo;
try {
SchemaVersionInfo existingSchemaVersionInfo = findSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadataInfo.getSchemaMetadata().getType(), schemaVersionInfo.getSchemaText(), schemaMetadataInfo.getSchemaMetadata().getName());
if (existingSchemaVersionInfo != null) {
String mergeMessage = String.format("Given version %d is already merged to master with version %d", schemaVersionId, existingSchemaVersionInfo.getVersion());
LOG.info(mergeMessage);
return new SchemaVersionMergeResult(new SchemaIdVersion(schemaMetadataInfo.getId(), existingSchemaVersionInfo.getVersion(), existingSchemaVersionInfo.getId()), mergeMessage);
}
createdSchemaVersionInfo = createSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadataInfo.getSchemaMetadata(), schemaMetadataInfo.getId(), new SchemaVersion(schemaVersionInfo.getSchemaText(), schemaVersionInfo.getDescription(), SchemaVersionLifecycleStates.INITIATED.getId(), initializedStateDetails));
} catch (InvalidSchemaException e) {
throw new SchemaVersionMergeException(String.format("Failed to merge schema version : '%s'", schemaVersionId.toString()), e);
}
Collection<SchemaVersionStateStorable> schemaVersionStates = storageManager.find(SchemaVersionStateStorable.NAME_SPACE, Collections.singletonList(new QueryParam(SchemaVersionStateStorable.SCHEMA_VERSION_ID, schemaVersionId.toString())), Collections.singletonList(OrderByField.of(SchemaVersionStateStorable.SEQUENCE, true)));
if (schemaVersionStates == null || schemaVersionStates.isEmpty()) {
throw new RuntimeException(String.format("The database doesn't have any state transition recorded for the schema version id : '%s'", schemaVersionId));
}
updateSchemaVersionState(createdSchemaVersionInfo.getId(), schemaVersionStates.iterator().next().getSequence(), SchemaVersionLifecycleStates.ENABLED.getId(), null);
String mergeMessage = String.format("Given version %d is merged successfully to master with version %d", schemaVersionId, createdSchemaVersionInfo.getVersion());
LOG.info(mergeMessage);
return new SchemaVersionMergeResult(new SchemaIdVersion(schemaMetadataInfo.getId(), createdSchemaVersionInfo.getVersion(), createdSchemaVersionInfo.getId()), mergeMessage);
} catch (SchemaBranchNotFoundException e) {
throw new SchemaVersionMergeException(String.format("Failed to merge schema version : '%s'", schemaVersionId.toString()), e);
}
}
use of com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException in project registry by hortonworks.
the class ConfluentSchemaRegistryCompatibleResource method getSchemaById.
@GET
@Path("/schemas/ids/{id}")
@ApiOperation(value = "Get schema version by id", response = Schema.class, tags = OPERATION_GROUP_CONFLUENT_SR)
@Timed
@UnitOfWork
public Response getSchemaById(@ApiParam(value = "schema version id", required = true) @PathParam("id") Long id) {
Response response;
try {
SchemaVersionInfo schemaVersionInfo = schemaRegistry.getSchemaVersionInfo(new SchemaIdVersion(id));
SchemaString schema = new SchemaString();
schema.setSchema(schemaVersionInfo.getSchemaText());
response = WSUtils.respondEntity(schema, Response.Status.OK);
} catch (SchemaNotFoundException ex) {
LOG.error("No schema version found with id [{}]", id, ex);
response = schemaNotFoundError();
} catch (Exception ex) {
LOG.error("Encountered error while retrieving Schema with id: [{}]", id, ex);
response = serverError();
}
return response;
}
use of com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException in project registry by hortonworks.
the class ConfluentSchemaRegistryCompatibleResource method getAllVersions.
@GET
@Path("/subjects/{subject}/versions")
@ApiOperation(value = "Get all schema versions of given subject", response = Integer.class, responseContainer = "Collection", tags = OPERATION_GROUP_CONFLUENT_SR)
@Timed
@UnitOfWork
public Response getAllVersions(@ApiParam(value = "subject", required = true) @PathParam("subject") String subject) {
Response response;
try {
List<Integer> registeredSubjects = schemaRegistry.getAllVersions(subject).stream().map(SchemaVersionInfo::getVersion).collect(Collectors.toList());
response = WSUtils.respondEntity(registeredSubjects, Response.Status.OK);
} catch (SchemaNotFoundException ex) {
LOG.error("No schema found with subject [{}]", subject, ex);
response = subjectNotFoundError();
} catch (Exception ex) {
LOG.error("Encountered error while retrieving all subjects", ex);
response = serverError();
}
return response;
}
use of com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException in project registry by hortonworks.
the class SchemaRegistryResource method getSchemaVersion.
@GET
@Path("/schemas/{name}/versions/{version}")
@ApiOperation(value = "Get a version of the schema identified by the schema name", response = SchemaVersionInfo.class, tags = OPERATION_GROUP_SCHEMA)
@Timed
@UnitOfWork
public Response getSchemaVersion(@ApiParam(value = "Schema name", required = true) @PathParam("name") String schemaMetadata, @ApiParam(value = "version of the schema", required = true) @PathParam("version") Integer versionNumber) {
SchemaVersionKey schemaVersionKey = new SchemaVersionKey(schemaMetadata, versionNumber);
Response response;
try {
SchemaVersionInfo schemaVersionInfo = schemaRegistry.getSchemaVersionInfo(schemaVersionKey);
response = WSUtils.respondEntity(schemaVersionInfo, Response.Status.OK);
} catch (SchemaNotFoundException e) {
LOG.info("No schemas found with schemaVersionKey: [{}]", schemaVersionKey);
response = WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, schemaVersionKey.toString());
} catch (Exception ex) {
LOG.error("Encountered error while getting all schema versions for schemakey [{}]", schemaMetadata, ex);
response = WSUtils.respond(Response.Status.INTERNAL_SERVER_ERROR, CatalogResponse.ResponseMessage.EXCEPTION, ex.getMessage());
}
return response;
}
Aggregations