use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException in project registry by hortonworks.
the class SchemaVersionLifecycleManager method createSchemaVersion.
private SchemaVersionInfo createSchemaVersion(String schemaBranchName, SchemaMetadata schemaMetadata, Long schemaMetadataId, SchemaVersion schemaVersion) throws IncompatibleSchemaException, InvalidSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
Preconditions.checkNotNull(schemaBranchName, "schemaBranchName must not be null");
Preconditions.checkNotNull(schemaMetadataId, "schemaMetadataId must not be null");
String type = schemaMetadata.getType();
if (getSchemaProvider(type) == null) {
throw new UnsupportedSchemaTypeException("Given schema type " + type + " not supported");
}
SchemaBranch schemaBranch = null;
try {
schemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(new SchemaBranchKey(schemaBranchName, schemaMetadata.getName())));
} catch (SchemaBranchNotFoundException e) {
// Ignore this error
}
if (schemaBranch == null) {
if (getAllVersions(schemaBranchName, schemaMetadata.getName()).size() != 0)
throw new RuntimeException(String.format("Schema name : '%s' and branch name : '%s' has schema version, yet failed to obtain schema branch instance", schemaMetadata.getName(), schemaBranchName));
}
// generate fingerprint, it parses the schema and checks for semantic validation.
// throws InvalidSchemaException for invalid schemas.
final String fingerprint = getFingerprint(type, schemaVersion.getSchemaText());
final String schemaName = schemaMetadata.getName();
SchemaVersionStorable schemaVersionStorable = new SchemaVersionStorable();
final Long schemaVersionStorableId = storageManager.nextId(schemaVersionStorable.getNameSpace());
schemaVersionStorable.setId(schemaVersionStorableId);
schemaVersionStorable.setSchemaMetadataId(schemaMetadataId);
schemaVersionStorable.setFingerprint(fingerprint);
schemaVersionStorable.setName(schemaName);
schemaVersionStorable.setSchemaText(schemaVersion.getSchemaText());
schemaVersionStorable.setDescription(schemaVersion.getDescription());
schemaVersionStorable.setTimestamp(System.currentTimeMillis());
schemaVersionStorable.setState(DEFAULT_VERSION_STATE.getId());
if (!schemaBranchName.equals(SchemaBranch.MASTER_BRANCH)) {
schemaVersion.setState(SchemaVersionLifecycleStates.INITIATED.getId());
schemaVersion.setStateDetails(null);
}
// take a lock for a schema with same name.
int retryCt = 0;
while (true) {
try {
Integer version = 0;
Byte initialState = schemaVersion.getInitialState();
if (schemaMetadata.isEvolve()) {
// if the given version is added with enabled or initiated state then only check for compatibility
if (SchemaVersionLifecycleStates.ENABLED.getId().equals(initialState) || SchemaVersionLifecycleStates.INITIATED.getId().equals(initialState)) {
CompatibilityResult compatibilityResult = checkCompatibility(schemaBranchName, schemaName, schemaVersion.getSchemaText());
if (!compatibilityResult.isCompatible()) {
String errMsg = String.format("Given schema is not compatible with latest schema versions. \n" + "Error location: [%s] \n" + "Error encountered is: [%s]", compatibilityResult.getErrorLocation(), compatibilityResult.getErrorMessage());
LOG.error(errMsg);
throw new IncompatibleSchemaException(errMsg);
}
}
SchemaVersionInfo latestSchemaVersionInfo = getLatestSchemaVersionInfo(schemaName);
if (latestSchemaVersionInfo != null) {
version = latestSchemaVersionInfo.getVersion();
}
}
schemaVersionStorable.setVersion(version + 1);
storageManager.add(schemaVersionStorable);
updateSchemaVersionState(schemaVersionStorable.getId(), 1, initialState, schemaVersion.getStateDetails());
break;
} catch (StorageException e) {
// optimistic to try the next try would be successful. When retry attempts are exhausted, throw error back to invoker.
if (++retryCt == DEFAULT_RETRY_CT) {
LOG.error("Giving up after retry attempts [{}] while trying to add new version of schema with metadata [{}]", retryCt, schemaMetadata, e);
throw e;
}
LOG.debug("Encountered storage exception while trying to add a new version, attempting again : [{}] with error: [{}]", retryCt, e);
}
}
// fetching this as the ID may have been set by storage manager.
Long schemaInstanceId = schemaVersionStorable.getId();
SchemaBranchVersionMapping schemaBranchVersionMapping = new SchemaBranchVersionMapping(schemaBranch.getId(), schemaInstanceId);
storageManager.add(schemaBranchVersionMapping);
String storableNamespace = new SchemaFieldInfoStorable().getNameSpace();
List<SchemaFieldInfo> schemaFieldInfos = getSchemaProvider(type).generateFields(schemaVersionStorable.getSchemaText());
for (SchemaFieldInfo schemaFieldInfo : schemaFieldInfos) {
final Long fieldInstanceId = storageManager.nextId(storableNamespace);
SchemaFieldInfoStorable schemaFieldInfoStorable = SchemaFieldInfoStorable.fromSchemaFieldInfo(schemaFieldInfo, fieldInstanceId);
schemaFieldInfoStorable.setSchemaInstanceId(schemaInstanceId);
schemaFieldInfoStorable.setTimestamp(System.currentTimeMillis());
storageManager.add(schemaFieldInfoStorable);
}
return schemaVersionStorable.toSchemaVersionInfo();
}
use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException 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.SchemaBranchNotFoundException in project registry by hortonworks.
the class SchemaRegistryResource method findAggregatedSchemas.
@GET
@Path("/search/schemas/aggregated")
@ApiOperation(value = "Search for schemas containing the given name and description", notes = "Search the schemas for given name and description, return a list of schemas that contain the field.", response = AggregatedSchemaMetadataInfo.class, responseContainer = "Collection", tags = OPERATION_GROUP_SCHEMA)
@Timed
@UnitOfWork
public Response findAggregatedSchemas(@Context UriInfo uriInfo) {
MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
try {
Collection<SchemaMetadataInfo> schemaMetadataInfos = findSchemaMetadataInfos(uriInfo.getQueryParameters());
List<AggregatedSchemaMetadataInfo> aggregatedSchemaMetadataInfos = new ArrayList<>();
for (SchemaMetadataInfo schemaMetadataInfo : schemaMetadataInfos) {
SchemaMetadata schemaMetadata = schemaMetadataInfo.getSchemaMetadata();
List<SerDesInfo> serDesInfos = new ArrayList<>(schemaRegistry.getSerDes(schemaMetadataInfo.getSchemaMetadata().getName()));
aggregatedSchemaMetadataInfos.add(new AggregatedSchemaMetadataInfo(schemaMetadata, schemaMetadataInfo.getId(), schemaMetadataInfo.getTimestamp(), schemaRegistry.getAggregatedSchemaBranch(schemaMetadata.getName()), serDesInfos));
}
return WSUtils.respondEntities(aggregatedSchemaMetadataInfos, Response.Status.OK);
} catch (SchemaBranchNotFoundException e) {
return WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, e.getMessage());
} catch (Exception ex) {
LOG.error("Encountered error while finding schemas for given fields [{}]", queryParameters, ex);
return WSUtils.respond(Response.Status.INTERNAL_SERVER_ERROR, CatalogResponse.ResponseMessage.EXCEPTION, ex.getMessage());
}
}
use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException in project registry by hortonworks.
the class SchemaRegistryResource method addSchemaVersion.
@POST
@Path("/schemas/{name}/versions")
@ApiOperation(value = "Register a new version of the schema", notes = "Registers the given schema version to schema with name if the given schemaText is not registered as a version for this schema, " + "and returns respective version number." + "In case of incompatible schema errors, it throws error message like 'Unable to read schema: <> using schema <>' ", response = Integer.class, tags = OPERATION_GROUP_SCHEMA)
@Timed
@UnitOfWork
public Response addSchemaVersion(@QueryParam("branch") @DefaultValue(MASTER_BRANCH) String schemaBranchName, @ApiParam(value = "Schema name", required = true) @PathParam("name") String schemaName, @ApiParam(value = "Details about the schema", required = true) SchemaVersion schemaVersion, @Context UriInfo uriInfo) {
return handleLeaderAction(uriInfo, () -> {
Response response;
try {
LOG.info("adding schema version for name [{}] with [{}]", schemaName, schemaVersion);
SchemaIdVersion version = schemaRegistry.addSchemaVersion(schemaBranchName, schemaName, schemaVersion);
response = WSUtils.respondEntity(version.getVersion(), Response.Status.CREATED);
} catch (InvalidSchemaException ex) {
LOG.error("Invalid schema error encountered while adding schema [{}] with key [{}]", schemaVersion, schemaName, ex);
response = WSUtils.respond(Response.Status.BAD_REQUEST, CatalogResponse.ResponseMessage.INVALID_SCHEMA, ex.getMessage());
} catch (IncompatibleSchemaException ex) {
LOG.error("Incompatible schema error encountered while adding schema [{}] with key [{}]", schemaVersion, schemaName, ex);
response = WSUtils.respond(Response.Status.BAD_REQUEST, CatalogResponse.ResponseMessage.INCOMPATIBLE_SCHEMA, ex.getMessage());
} catch (UnsupportedSchemaTypeException ex) {
LOG.error("Unsupported schema type encountered while adding schema [{}] with key [{}]", schemaVersion, schemaName, ex);
response = WSUtils.respond(Response.Status.BAD_REQUEST, CatalogResponse.ResponseMessage.UNSUPPORTED_SCHEMA_TYPE, ex.getMessage());
} catch (SchemaBranchNotFoundException e) {
return WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, e.getMessage());
} catch (Exception ex) {
LOG.error("Encountered error while adding schema [{}] with key [{}]", schemaVersion, schemaName, ex, ex);
response = WSUtils.respond(Response.Status.INTERNAL_SERVER_ERROR, CatalogResponse.ResponseMessage.EXCEPTION, ex.getMessage());
}
return response;
});
}
use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException in project registry by hortonworks.
the class DefaultSchemaRegistry method getSchemaBranch.
private SchemaBranch getSchemaBranch(Long id) throws SchemaBranchNotFoundException {
List<QueryParam> schemaBranchQueryParam = new ArrayList<>();
schemaBranchQueryParam.add(new QueryParam(SchemaBranchStorable.ID, id.toString()));
Collection<SchemaBranchStorable> schemaBranchStorables = storageManager.find(SchemaBranchStorable.NAME_SPACE, schemaBranchQueryParam);
if (schemaBranchStorables == null || schemaBranchStorables.isEmpty())
throw new SchemaBranchNotFoundException(String.format("Schema branch with id : '%s' not found", id.toString()));
// size of the collection will always be less than 2, as ID is a primary key, so no need handle the case where size > 1
return schemaBranchStorables.iterator().next().toSchemaBranch();
}
Aggregations