final SchemaVersionKey schemaVersionKey = new SchemaVersionKey(schemaName.get(), schemaVersion.getAsInt()); versionInfo = getSchemaVersionInfo(client, schemaVersionKey); } else {
private SchemaVersionInfo getSchemaVersionInfo(final SchemaRegistryClient client, final SchemaVersionKey key) throws org.apache.nifi.schema.access.SchemaNotFoundException { try { // Try to fetch the SchemaVersionInfo from the cache. final Tuple<SchemaVersionInfo, Long> timestampedVersionInfo = schemaVersionByKeyCache.get(key); // Determine if the timestampedVersionInfo is expired boolean fetch = false; if (timestampedVersionInfo == null) { fetch = true; } else { final long minTimestamp = System.nanoTime() - versionInfoCacheNanos; fetch = timestampedVersionInfo.getValue() < minTimestamp; } // If not expired, use what we got from the cache if (!fetch) { return timestampedVersionInfo.getKey(); } // schema version info was expired or not found in cache. Fetch from schema registry final SchemaVersionInfo versionInfo = client.getSchemaVersionInfo(key); if (versionInfo == null) { throw new org.apache.nifi.schema.access.SchemaNotFoundException("Could not find schema with name '" + key.getSchemaName() + "' and version " + key.getVersion()); } // Store new version in cache. final Tuple<SchemaVersionInfo, Long> tuple = new Tuple<>(versionInfo, System.nanoTime()); schemaVersionByKeyCache.put(key, tuple); return versionInfo; } catch (final SchemaNotFoundException e) { throw new org.apache.nifi.schema.access.SchemaNotFoundException(e); } }
@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; }
@Override public String resolveSchema(SchemaVersionKey schemaVersionKey) throws InvalidSchemaException, SchemaNotFoundException { Map<String, SchemaParsingState> schemaParsingStates = new HashMap<>(); schemaParsingStates.put(schemaVersionKey.getSchemaName(), SchemaParsingState.PARSING); return getResultantSchema(schemaVersionKey, schemaParsingStates); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Key key = (Key) o; if (schemaVersionKey != null ? !schemaVersionKey.equals(key.schemaVersionKey) : key.schemaVersionKey != null) return false; return schemaIdVersion != null ? schemaIdVersion.equals(key.schemaIdVersion) : key.schemaIdVersion == null; }
@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; }
@Override public String resolveSchema(SchemaVersionKey schemaVersionKey) throws InvalidSchemaException, SchemaNotFoundException { Map<String, SchemaParsingState> schemaParsingStates = new HashMap<>(); schemaParsingStates.put(schemaVersionKey.getSchemaName(), SchemaParsingState.PARSING); return getResultantSchema(schemaVersionKey, schemaParsingStates); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Key key = (Key) o; if (schemaVersionKey != null ? !schemaVersionKey.equals(key.schemaVersionKey) : key.schemaVersionKey != null) return false; return schemaIdVersion != null ? schemaIdVersion.equals(key.schemaIdVersion) : key.schemaIdVersion == null; }
schemaName = metadata.getName(); final SchemaVersionKey schemaVersionKey = new SchemaVersionKey(schemaName, version.getAsInt()); versionInfo = getSchemaVersionInfo(client, schemaVersionKey); if (versionInfo == null) {
@DELETE @Path("/schemas/{name}/versions/{version}") @ApiOperation(value = "Delete a schema version given its schema name and version id", tags = OPERATION_GROUP_SCHEMA) @UnitOfWork public Response deleteSchemaVersion(@ApiParam(value = "Schema name", required = true) @PathParam("name") String schemaName, @ApiParam(value = "version of the schema", required = true) @PathParam("version") Integer versionNumber, @Context UriInfo uriInfo) { SchemaVersionKey schemaVersionKey = null; try { schemaVersionKey = new SchemaVersionKey(schemaName, versionNumber); schemaRegistry.deleteSchemaVersion(schemaVersionKey); return WSUtils.respond(Response.Status.OK); } catch (SchemaNotFoundException e) { LOG.error("No schemaVersion found with name: [{}], version : [{}]", schemaName, versionNumber); return WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, schemaVersionKey.toString()); } catch (SchemaLifecycleException e) { LOG.error("Failed to delete schema name: [{}], version : [{}]", schemaName, versionNumber, e); return WSUtils.respond(Response.Status.BAD_REQUEST, CatalogResponse.ResponseMessage.BAD_REQUEST_WITH_MESSAGE, e.getMessage()); } catch (Exception ex) { LOG.error("Encountered error while deleting schemaVersion with name: [{}], version : [{}]", schemaName, versionNumber, ex); return WSUtils.respond(Response.Status.INTERNAL_SERVER_ERROR, CatalogResponse.ResponseMessage.EXCEPTION, ex.getMessage()); } }
private SchemaVersionInfo retrieveSchemaVersionInfo(SchemaVersionKey schemaVersionKey) throws SchemaNotFoundException { String schemaName = schemaVersionKey.getSchemaName(); Integer version = schemaVersionKey.getVersion(); SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName); if (schemaMetadataInfo == null) { throw new SchemaNotFoundException("No SchemaMetadata exists with key: " + schemaName); } return fetchSchemaVersionInfo(schemaVersionKey.getSchemaName(), version); }
private Map<String, Schema> collectSchemaTypes(SchemaVersionKey schemaVersionKey, Map<String, SchemaParsingState> schemaParsingStates) throws SchemaNotFoundException, InvalidSchemaException { String schemaName = schemaVersionKey.getSchemaName(); SchemaParsingState schemaParsingState = schemaParsingStates.putIfAbsent(schemaName, SchemaParsingState.PARSING); // if it is already parsed then the respective schema types would have been already collected. if (SchemaParsingState.PARSED == schemaParsingState) { return null; } // if it is in parsing state earlier and it is visted again then ther eis circular dependency!! if (SchemaParsingState.PARSING == schemaParsingState) { throw new CyclicSchemaDependencyException("Cyclic dependency of schema imports with schema [" + schemaName + "]"); } // this schema is not yet parsed till now if (schemaParsingState == null) { Schema.Parser parser = new Schema.Parser(); Schema schema = parser.parse(getResultantSchema(schemaVersionKey, schemaParsingStates)); Map<String, Schema> complexTypes = new HashMap<>(); collectComplexTypes(schema, complexTypes); schemaParsingStates.put(schemaName, SchemaParsingState.PARSED); return complexTypes; } throw new IllegalStateException("Schema parsing with schema version " + schemaVersionKey + " is in invalid state!!"); }
private List<SchemaVersionKey> getIncludedSchemaVersions(String schemaText) throws InvalidSchemaException { JsonNode jsonNode = null; try { jsonNode = new ObjectMapper().readTree(schemaText); } catch (IOException e) { throw new InvalidSchemaException(e); } JsonNode includeSchemaNodes = jsonNode.get("includeSchemas"); List<SchemaVersionKey> includedSchemaVersions = new ArrayList<>(); if (includeSchemaNodes != null) { if (!includeSchemaNodes.isArray()) { throw new InvalidSchemaException("includeSchemas should be an array of strings"); } for (JsonNode includeSchema : includeSchemaNodes) { String name = includeSchema.get("name").asText(); JsonNode versionNode = includeSchema.get("version"); int version = versionNode != null ? versionNode.asInt() : SchemaVersionKey.LATEST_VERSION; includedSchemaVersions.add(new SchemaVersionKey(name, version)); } } return includedSchemaVersions; } }
@DELETE @Path("/schemas/{name}/versions/{version}") @ApiOperation(value = "Delete a schema version given its schema name and version id", tags = OPERATION_GROUP_SCHEMA) @UnitOfWork public Response deleteSchemaVersion(@ApiParam(value = "Schema name", required = true) @PathParam("name") String schemaName, @ApiParam(value = "version of the schema", required = true) @PathParam("version") Integer versionNumber, @Context UriInfo uriInfo) { SchemaVersionKey schemaVersionKey = null; try { schemaVersionKey = new SchemaVersionKey(schemaName, versionNumber); schemaRegistry.deleteSchemaVersion(schemaVersionKey); return WSUtils.respond(Response.Status.OK); } catch (SchemaNotFoundException e) { LOG.error("No schemaVersion found with name: [{}], version : [{}]", schemaName, versionNumber); return WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, schemaVersionKey.toString()); } catch (SchemaLifecycleException e) { LOG.error("Failed to delete schema name: [{}], version : [{}]", schemaName, versionNumber, e); return WSUtils.respond(Response.Status.BAD_REQUEST, CatalogResponse.ResponseMessage.BAD_REQUEST_WITH_MESSAGE, e.getMessage()); } catch (Exception ex) { LOG.error("Encountered error while deleting schemaVersion with name: [{}], version : [{}]", schemaName, versionNumber, ex); return WSUtils.respond(Response.Status.INTERNAL_SERVER_ERROR, CatalogResponse.ResponseMessage.EXCEPTION, ex.getMessage()); } }
private SchemaVersionInfo doGetSchemaVersionInfo(SchemaVersionKey schemaVersionKey) { LOG.info("Getting schema version from target registry for [{}]", schemaVersionKey); String schemaName = schemaVersionKey.getSchemaName(); WebTarget webTarget = currentSchemaRegistryTargets().schemasTarget.path(String.format("%s/versions/%d", schemaName, schemaVersionKey .getVersion())); return getEntity(webTarget, SchemaVersionInfo.class); }
private Map<String, Schema> collectSchemaTypes(SchemaVersionKey schemaVersionKey, Map<String, SchemaParsingState> schemaParsingStates) throws SchemaNotFoundException, InvalidSchemaException { String schemaName = schemaVersionKey.getSchemaName(); SchemaParsingState schemaParsingState = schemaParsingStates.putIfAbsent(schemaName, SchemaParsingState.PARSING); // if it is already parsed then the respective schema types would have been already collected. if (SchemaParsingState.PARSED == schemaParsingState) { return null; } // if it is in parsing state earlier and it is visted again then ther eis circular dependency!! if (SchemaParsingState.PARSING == schemaParsingState) { throw new CyclicSchemaDependencyException("Cyclic dependency of schema imports with schema [" + schemaName + "]"); } // this schema is not yet parsed till now if (schemaParsingState == null) { Schema.Parser parser = new Schema.Parser(); Schema schema = parser.parse(getResultantSchema(schemaVersionKey, schemaParsingStates)); Map<String, Schema> complexTypes = new HashMap<>(); collectComplexTypes(schema, complexTypes); schemaParsingStates.put(schemaName, SchemaParsingState.PARSED); return complexTypes; } throw new IllegalStateException("Schema parsing with schema version " + schemaVersionKey + " is in invalid state!!"); }
private SchemaVersionKey getSchemaKey(Long schemaId) { SchemaVersionKey schemaVersionKey = null; List<QueryParam> queryParams = Collections.singletonList(new QueryParam(SchemaVersionStorable.ID, schemaId.toString())); Collection<SchemaVersionStorable> versionedSchemas = storageManager.find(SchemaVersionStorable.NAME_SPACE, queryParams); if (versionedSchemas != null && !versionedSchemas.isEmpty()) { SchemaVersionStorable storable = versionedSchemas.iterator().next(); schemaVersionKey = new SchemaVersionKey(storable.getName(), storable.getVersion()); } return schemaVersionKey; }
@Override public void deleteSchemaVersion(SchemaVersionKey schemaVersionKey) throws SchemaNotFoundException, SchemaLifecycleException { schemaVersionInfoCache.invalidateSchema(new SchemaVersionInfoCache.Key(schemaVersionKey)); WebTarget target = currentSchemaRegistryTargets().schemasTarget.path(String.format("%s/versions/%s", schemaVersionKey .getSchemaName(), schemaVersionKey.getVersion())); Response response = Subject.doAs(subject, new PrivilegedAction<Response>() { @Override public Response run() { return target.request(MediaType.APPLICATION_JSON_TYPE).delete(Response.class); } }); handleDeleteSchemaResponse(response); }
@Test public void testSchemaVersionDeletion() throws Exception { SchemaVersionKey schemaVersionKey = addAndDeleteSchemaVersion(TEST_NAME_RULE.getMethodName()); Assert.assertTrue(SCHEMA_REGISTRY_CLIENT.getAllVersions(schemaVersionKey.getSchemaName()).isEmpty()); }
private List<SchemaVersionKey> getIncludedSchemaVersions(String schemaText) throws InvalidSchemaException { JsonNode jsonNode = null; try { jsonNode = new ObjectMapper().readTree(schemaText); } catch (IOException e) { throw new InvalidSchemaException(e); } JsonNode includeSchemaNodes = jsonNode.get("includeSchemas"); List<SchemaVersionKey> includedSchemaVersions = new ArrayList<>(); if (includeSchemaNodes != null) { if (!includeSchemaNodes.isArray()) { throw new InvalidSchemaException("includeSchemas should be an array of strings"); } for (JsonNode includeSchema : includeSchemaNodes) { String name = includeSchema.get("name").asText(); JsonNode versionNode = includeSchema.get("version"); int version = versionNode != null ? versionNode.asInt() : SchemaVersionKey.LATEST_VERSION; includedSchemaVersions.add(new SchemaVersionKey(name, version)); } } return includedSchemaVersions; } }