use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException in project registry by hortonworks.
the class SchemaRegistryResource method checkCompatibilityWithSchema.
@POST
@Path("/schemas/{name}/compatibility")
@ApiOperation(value = "Checks if the given schema text is compatible with all the versions of the schema identified by the name", response = CompatibilityResult.class, tags = OPERATION_GROUP_SCHEMA)
@Timed
@UnitOfWork
public Response checkCompatibilityWithSchema(@QueryParam("branch") @DefaultValue(MASTER_BRANCH) String schemaBranchName, @ApiParam(value = "Schema name", required = true) @PathParam("name") String schemaName, @ApiParam(value = "schema text", required = true) String schemaText) {
Response response;
try {
CompatibilityResult compatibilityResult = schemaRegistry.checkCompatibility(schemaBranchName, schemaName, schemaText);
response = WSUtils.respondEntity(compatibilityResult, Response.Status.OK);
} catch (SchemaNotFoundException e) {
LOG.error("No schemas found with schemakey: [{}]", schemaName, e);
response = WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, schemaName);
} 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 checking compatibility with versions of schema with [{}] for given schema text [{}]", schemaName, schemaText, 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 SchemaRegistryResource method getLatestSchemaVersion.
@GET
@Path("/schemas/{name}/versions/latest")
@ApiOperation(value = "Get the latest version of the schema for the given schema name", response = SchemaVersionInfo.class, tags = OPERATION_GROUP_SCHEMA)
@Timed
@UnitOfWork
public Response getLatestSchemaVersion(@ApiParam(value = "Schema name", required = true) @PathParam("name") String schemaName, @QueryParam("branch") @DefaultValue(MASTER_BRANCH) String schemaBranchName) {
Response response;
try {
SchemaVersionInfo schemaVersionInfo = schemaRegistry.getLatestSchemaVersionInfo(schemaBranchName, schemaName);
if (schemaVersionInfo != null) {
response = WSUtils.respondEntity(schemaVersionInfo, Response.Status.OK);
} else {
LOG.info("No schemas found with schemakey: [{}]", schemaName);
response = WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, schemaName);
}
} 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 getting latest schema version for schemakey [{}]", schemaName, 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 SchemaBranchLifeCycleTest method getAllBranches.
@Test
public void getAllBranches() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException {
SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.NONE);
SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc");
SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion1.getSchemaVersionId());
SchemaIdVersion masterSchemaIdVersion2 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device-incompat.avsc");
SchemaBranch schemaBranch2 = addSchemaBranch("BRANCH2", schemaMetadata, masterSchemaIdVersion2.getSchemaVersionId());
Set<String> actualSchemaBranches = schemaRegistryClient.getSchemaBranches(schemaMetadata.getName()).stream().map(branch -> branch.getName()).collect(Collectors.toSet());
Set<String> expectedSchemaBranches = new HashSet<>(Lists.newArrayList("MASTER", schemaBranch1.getName(), schemaBranch2.getName()));
Assert.assertTrue(SetUtils.isEqualSet(actualSchemaBranches, expectedSchemaBranches));
}
use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException in project registry by hortonworks.
the class SchemaBranchLifeCycleTest method mergeSchemaWithDefaultMergeStrategy.
@Test
public void mergeSchemaWithDefaultMergeStrategy() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException {
SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.toString(), SchemaCompatibility.NONE);
SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc");
SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion1.getSchemaVersionId());
SchemaIdVersion schemaBranch1Version1 = addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc");
SchemaIdVersion schemaBranch1Version2 = addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-compat.avsc");
schemaRegistryClient.mergeSchemaVersion(schemaBranch1Version2.getSchemaVersionId());
Collection<SchemaVersionInfo> branchSchemaVersionInfos = schemaRegistryClient.getAllVersions(schemaBranch1.getName(), schemaMetadata.getName());
Collection<SchemaVersionInfo> masterSchemaVersionInfos = schemaRegistryClient.getAllVersions(schemaMetadata.getName());
Assert.assertTrue(masterSchemaVersionInfos.size() == 2);
Assert.assertTrue(branchSchemaVersionInfos.size() == 3);
Long branchVersionsInInitiatedState = branchSchemaVersionInfos.stream().filter(schemaVersionInfo -> schemaVersionInfo.getStateId().equals(SchemaVersionLifecycleStates.INITIATED.getId())).count();
Long masterVersionsInEnabledState = masterSchemaVersionInfos.stream().filter(schemaVersionInfo -> schemaVersionInfo.getStateId().equals(SchemaVersionLifecycleStates.ENABLED.getId())).count();
Assert.assertTrue(branchVersionsInInitiatedState == 2);
Assert.assertTrue(masterVersionsInEnabledState == 2);
}
use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException in project registry by hortonworks.
the class DefaultSchemaRegistry method deleteSchemaBranch.
@Override
public void deleteSchemaBranch(Long schemaBranchId) throws SchemaBranchNotFoundException, InvalidSchemaBranchDeletionException {
Preconditions.checkNotNull(schemaBranchId, "Schema branch name can't be null");
SchemaBranch schemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchId));
if (schemaBranch.getName().equals(SchemaBranch.MASTER_BRANCH))
throw new InvalidSchemaBranchDeletionException(String.format("Can't delete '%s' branch", SchemaBranch.MASTER_BRANCH));
SchemaBranchCache.Key keyOfSchemaBranchToDelete = SchemaBranchCache.Key.of(schemaBranchId);
schemaBranchCache.invalidateSchemaBranch(keyOfSchemaBranchToDelete);
List<QueryParam> schemaVersionMappingStorableQueryParams = new ArrayList<>();
schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_BRANCH_ID, schemaBranch.getId().toString()));
List<OrderByField> schemaVersionMappingOrderbyFields = new ArrayList<>();
schemaVersionMappingOrderbyFields.add(OrderByField.of(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, false));
Collection<SchemaBranchVersionMapping> schemaBranchVersionMappings = storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionMappingStorableQueryParams, schemaVersionMappingOrderbyFields);
if (schemaBranchVersionMappings == null)
throw new RuntimeException("Schema branch is invalid state, its not associated with any schema versions");
// Ignore the first version as it used in the 'MASTER' branch
Iterator<SchemaBranchVersionMapping> schemaBranchVersionMappingIterator = schemaBranchVersionMappings.iterator();
SchemaBranchVersionMapping rootVersionMapping = schemaBranchVersionMappingIterator.next();
storageManager.remove(rootVersionMapping.getStorableKey());
// Validate if the schema versions in the branch to be deleted are the root versions for other branches
Map<Integer, List<String>> schemaVersionTiedToOtherBranch = new HashMap<>();
List<Long> schemaVersionsToBeDeleted = new ArrayList<>();
while (schemaBranchVersionMappingIterator.hasNext()) {
SchemaBranchVersionMapping schemaBranchVersionMapping = schemaBranchVersionMappingIterator.next();
Long schemaVersionId = schemaBranchVersionMapping.getSchemaVersionInfoId();
try {
List<QueryParam> schemaVersionCountParam = new ArrayList<>();
schemaVersionCountParam.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, schemaBranchVersionMapping.getSchemaVersionInfoId().toString()));
Collection<SchemaBranchVersionMapping> mappingsForSchemaTiedToMutlipleBranch = storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionCountParam);
if (mappingsForSchemaTiedToMutlipleBranch.size() > 1) {
SchemaVersionInfo schemaVersionInfo = schemaVersionLifecycleManager.getSchemaVersionInfo(new SchemaIdVersion(schemaVersionId));
List<String> forkedBranchName = mappingsForSchemaTiedToMutlipleBranch.stream().filter(mapping -> !mapping.getSchemaBranchId().equals(schemaBranchId)).map(mappping -> schemaBranchCache.get(SchemaBranchCache.Key.of(mappping.getSchemaBranchId())).getName()).collect(Collectors.toList());
schemaVersionTiedToOtherBranch.put(schemaVersionInfo.getVersion(), forkedBranchName);
} else {
schemaVersionsToBeDeleted.add(schemaVersionId);
}
} catch (SchemaNotFoundException e) {
throw new RuntimeException(String.format("Failed to delete schema version : '%s' of schema branch : '%s'", schemaVersionId.toString(), schemaBranchId), e);
}
}
if (!schemaVersionTiedToOtherBranch.isEmpty()) {
StringBuilder message = new StringBuilder();
message.append("Failed to delete branch");
schemaVersionTiedToOtherBranch.entrySet().stream().forEach(versionWithBranch -> {
message.append(", schema version : '").append(versionWithBranch.getKey()).append("'");
message.append(" is tied to branch : '").append(Arrays.toString(versionWithBranch.getValue().toArray())).append("'");
});
throw new InvalidSchemaBranchDeletionException(message.toString());
} else {
for (Long schemaVersionId : schemaVersionsToBeDeleted) {
try {
schemaVersionLifecycleManager.deleteSchemaVersion(schemaVersionId);
} catch (SchemaLifecycleException e) {
throw new InvalidSchemaBranchDeletionException("Failed to delete schema branch, all schema versions in the branch should be in one of 'INITIATED', 'ChangesRequired' or 'Archived' state ", e);
} catch (SchemaNotFoundException e) {
throw new RuntimeException(String.format("Failed to delete schema version : '%s' of schema branch : '%s'", schemaVersionId.toString(), schemaBranchId), e);
}
}
}
storageManager.remove(new SchemaBranchStorable(schemaBranchId).getStorableKey());
invalidateSchemaBranchInAllHAServers(keyOfSchemaBranchToDelete);
}
Aggregations