Search in sources :

Example 6 with SchemaBranchNotFoundException

use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException in project registry by hortonworks.

the class DefaultSchemaRegistry method createSchemaBranch.

@Override
public SchemaBranch createSchemaBranch(Long schemaVersionId, SchemaBranch schemaBranch) throws SchemaBranchAlreadyExistsException, SchemaNotFoundException {
    Preconditions.checkNotNull(schemaBranch.getName(), "Schema branch name can't be null");
    SchemaVersionInfo schemaVersionInfo = schemaVersionLifecycleManager.getSchemaVersionInfo(new SchemaIdVersion(schemaVersionId));
    SchemaBranchKey schemaBranchKey = new SchemaBranchKey(schemaBranch.getName(), schemaVersionInfo.getName());
    SchemaBranch existingSchemaBranch = null;
    try {
        existingSchemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchKey));
    } catch (SchemaBranchNotFoundException e) {
    // Ignore this error
    }
    if (existingSchemaBranch != null)
        throw new SchemaBranchAlreadyExistsException(String.format("A schema branch with name : '%s' already exists", schemaBranch.getName()));
    SchemaBranchStorable schemaBranchStorable = SchemaBranchStorable.from(schemaBranch);
    schemaBranchStorable.setSchemaMetadataName(schemaVersionInfo.getName());
    schemaBranchStorable.setId(storageManager.nextId(SchemaBranchStorable.NAME_SPACE));
    storageManager.add(schemaBranchStorable);
    SchemaBranch persistedSchemaBranch;
    try {
        persistedSchemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchKey));
    } catch (SchemaBranchNotFoundException e) {
        throw new RuntimeException(String.format("Failed to fetch persisted schema branch : '%s' from the database", schemaBranch.getName()));
    }
    SchemaBranchVersionMapping schemaBranchVersionMapping = new SchemaBranchVersionMapping(persistedSchemaBranch.getId(), schemaVersionInfo.getId());
    storageManager.add(schemaBranchVersionMapping);
    return persistedSchemaBranch;
}
Also used : SchemaBranchAlreadyExistsException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException)

Example 7 with SchemaBranchNotFoundException

use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException in project registry by hortonworks.

the class DefaultSchemaRegistry method getSchemaBranch.

private SchemaBranch getSchemaBranch(SchemaBranchKey schemaBranchKey) throws SchemaBranchNotFoundException {
    List<QueryParam> queryParams = new ArrayList<>();
    queryParams.add(new QueryParam(SchemaBranchStorable.NAME, schemaBranchKey.getSchemaBranchName()));
    queryParams.add(new QueryParam(SchemaBranchStorable.SCHEMA_METADATA_NAME, schemaBranchKey.getSchemaMetadataName()));
    Collection<SchemaBranchStorable> schemaBranchStorables = storageManager.find(SchemaBranchStorable.NAME_SPACE, queryParams);
    if (schemaBranchStorables == null || schemaBranchStorables.isEmpty())
        throw new SchemaBranchNotFoundException(String.format("Schema branch with key : %s not found", schemaBranchKey));
    else if (schemaBranchStorables.size() > 1)
        throw new SchemaBranchNotFoundException(String.format("Failed to unique determine a schema branch with key : %s", schemaBranchKey));
    return schemaBranchStorables.iterator().next().toSchemaBranch();
}
Also used : QueryParam(com.hortonworks.registries.common.QueryParam) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) ArrayList(java.util.ArrayList)

Example 8 with SchemaBranchNotFoundException

use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException in project registry by hortonworks.

the class SchemaBranchLifeCycleTest method addSchemaVersionToBranch.

@Test
public void addSchemaVersionToBranch() 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());
    addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc");
    addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-compat.avsc");
    Collection<SchemaVersionInfo> schemaBranch1VersionInfos = schemaRegistryClient.getAllVersions(schemaBranch1.getName(), schemaMetadata.getName());
    Collection<SchemaVersionInfo> masterSchemaVersionInfos = schemaRegistryClient.getAllVersions(schemaMetadata.getName());
    Assert.assertTrue(masterSchemaVersionInfos.size() == 1);
    Assert.assertTrue(schemaBranch1VersionInfos.size() == 3);
    Long versionsInInitiatedState = schemaBranch1VersionInfos.stream().filter(schemaVersionInfo -> schemaVersionInfo.getStateId().equals(SchemaVersionLifecycleStates.INITIATED.getId())).count();
    Long versionsInEnabledState = schemaBranch1VersionInfos.stream().filter(schemaVersionInfo -> schemaVersionInfo.getStateId().equals(SchemaVersionLifecycleStates.ENABLED.getId())).count();
    Assert.assertTrue(versionsInInitiatedState == 2);
    Assert.assertTrue(versionsInEnabledState == 1);
}
Also used : Arrays(java.util.Arrays) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) SchemaRegistryTestProfileType(com.hortonworks.registries.schemaregistry.avro.conf.SchemaRegistryTestProfileType) SchemaVersionLifecycleStates(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStates) SchemaRegistryTestServerClientWrapper(com.hortonworks.registries.schemaregistry.avro.helper.SchemaRegistryTestServerClientWrapper) RunWith(org.junit.runner.RunWith) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) AvroSchemaRegistryClientUtil(com.hortonworks.registries.schemaregistry.avro.util.AvroSchemaRegistryClientUtil) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) TestName(org.junit.rules.TestName) After(org.junit.After) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) Before(org.junit.Before) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) SchemaCompatibility(com.hortonworks.registries.schemaregistry.SchemaCompatibility) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) SetUtils(org.apache.commons.collections.SetUtils) Collection(java.util.Collection) InvalidSchemaBranchDeletionException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) Set(java.util.Set) SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) SchemaBranch(com.hortonworks.registries.schemaregistry.SchemaBranch) Rule(org.junit.Rule) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) CustomParameterizedRunner(com.hortonworks.registries.schemaregistry.avro.util.CustomParameterizedRunner) SchemaBranchAlreadyExistsException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException) Assert(org.junit.Assert) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaBranch(com.hortonworks.registries.schemaregistry.SchemaBranch) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) Test(org.junit.Test)

Example 9 with SchemaBranchNotFoundException

use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException in project registry by hortonworks.

the class SchemaRegistryClient method deleteSchemaBranch.

@Override
public void deleteSchemaBranch(Long schemaBranchId) throws SchemaBranchNotFoundException, InvalidSchemaBranchDeletionException {
    WebTarget target = currentSchemaRegistryTargets().schemasTarget.path("branch/" + schemaBranchId);
    Response response = Subject.doAs(subject, new PrivilegedAction<Response>() {

        @Override
        public Response run() {
            return target.request().delete();
        }
    });
    int status = response.getStatus();
    if (status == Response.Status.NOT_FOUND.getStatusCode()) {
        throw new SchemaBranchNotFoundException(response.readEntity(String.class));
    } else if (status == Response.Status.BAD_REQUEST.getStatusCode()) {
        throw new InvalidSchemaBranchDeletionException(response.readEntity(String.class));
    } else if (status != Response.Status.OK.getStatusCode()) {
        throw new RuntimeException(response.readEntity(String.class));
    }
}
Also used : Response(javax.ws.rs.core.Response) CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) InvalidSchemaBranchDeletionException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) WebTarget(javax.ws.rs.client.WebTarget)

Example 10 with SchemaBranchNotFoundException

use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException in project registry by hortonworks.

the class SchemaRegistryResource method getAllSchemaVersions.

@GET
@Path("/schemas/{name}/versions")
@ApiOperation(value = "Get all the versions of the schema for the given schema name)", response = SchemaVersionInfo.class, responseContainer = "List", tags = OPERATION_GROUP_SCHEMA)
@Timed
@UnitOfWork
public Response getAllSchemaVersions(@ApiParam(value = "Schema name", required = true) @PathParam("name") String schemaName, @QueryParam("branch") @DefaultValue(MASTER_BRANCH) String schemaBranchName, @QueryParam("states") List<Byte> stateIds) {
    Response response;
    try {
        Collection<SchemaVersionInfo> schemaVersionInfos = schemaRegistry.getAllVersions(schemaBranchName, schemaName, stateIds);
        if (schemaVersionInfos != null) {
            response = WSUtils.respondEntities(schemaVersionInfos, 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 all schema versions for schemakey [{}]", schemaName, ex);
        response = WSUtils.respond(Response.Status.INTERNAL_SERVER_ERROR, CatalogResponse.ResponseMessage.EXCEPTION, ex.getMessage());
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) InvalidSchemaBranchDeletionException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException) FileNotFoundException(java.io.FileNotFoundException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaBranchAlreadyExistsException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) IOException(java.io.IOException) Path(javax.ws.rs.Path) UnitOfWork(com.hortonworks.registries.common.transaction.UnitOfWork) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

SchemaBranchNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException)16 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)12 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)11 InvalidSchemaBranchDeletionException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException)10 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)10 SchemaBranchAlreadyExistsException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException)10 SchemaLifecycleException (com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException)10 IOException (java.io.IOException)9 UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)8 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)6 ArrayList (java.util.ArrayList)6 Collection (java.util.Collection)6 Collectors (java.util.stream.Collectors)6 Timed (com.codahale.metrics.annotation.Timed)5 Lists (com.google.common.collect.Lists)5 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)5 UnitOfWork (com.hortonworks.registries.common.transaction.UnitOfWork)5 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)5 SchemaVersionLifecycleStates (com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStates)5 ApiOperation (io.swagger.annotations.ApiOperation)5