if (versionInfo == null || versionInfo.getVersion() == null) { final String message = createErrorMessage("Could not find schema", schemaName, schemaBranchName, schemaVersion); throw new org.apache.nifi.schema.access.SchemaNotFoundException(message); final String schemaText = versionInfo.getSchemaText(); .name(schemaName.get()) .branch(schemaBranchName.orElse(null)) .version(versionInfo.getVersion()) .build();
@POST @Path("/subjects/{subject}") @ApiOperation(value = "Get schema information for the given schema subject and schema text", response = Schema.class, tags = OPERATION_GROUP_CONFLUENT_SR) @Timed @UnitOfWork public Response lookupSubjectVersion(@ApiParam(value = "Schema subject", required = true) @PathParam("subject") String subject, @ApiParam(value = "The schema ", required = true) String schema) { Response response; try { SchemaVersionInfo schemaVersionInfo = schemaRegistry.getSchemaVersionInfo(subject, schemaStringFromJson(schema).getSchema()); if (schemaVersionInfo != null) { response = WSUtils.respondEntity(new Schema(schemaVersionInfo.getName(), schemaVersionInfo.getVersion(), schemaVersionInfo.getId(), schemaVersionInfo.getSchemaText()), Response.Status.OK); } else { response = WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, subject); } } catch (InvalidSchemaException ex) { LOG.error("Given schema is invalid", ex); response = invalidSchemaError(); } catch (SchemaNotFoundException ex) { LOG.error("No schema found with subject [{}]", subject, ex); response = subjectNotFoundError(); } catch (Exception ex) { LOG.error("Encountered error while retrieving schema version with subject: [{}]", subject, ex); response = serverError(); } return response; }
int schemaVersion = schemaVersionInfo.getVersion(); String schemaText = schemaVersionInfo.getSchemaText(); List<SchemaVersionInfo> allEnabledSchemaVersions = schemaVersionService.getAllSchemaVersions(SchemaBranch.MASTER_BRANCH, schemaName) .stream() .filter(x -> SchemaVersionLifecycleStates.ENABLED.getId().equals(x.getStateId())) .collect(Collectors.toList()); if (validationLevel.equals(SchemaValidationLevel.ALL)) { for (SchemaVersionInfo curSchemaVersionInfo : allEnabledSchemaVersions) { int curVersion = curSchemaVersionInfo.getVersion(); if (curVersion < schemaVersion) { checkCompatibility(schemaVersionService, schemaMetadata, schemaText, curSchemaVersionInfo.getSchemaText()); } else { checkCompatibility(schemaVersionService, schemaMetadata, curSchemaVersionInfo.getSchemaText(), schemaText); int i = 0; int size = sortedSchemaVersionInfos.size(); for (; i < size && sortedSchemaVersionInfos.get(i).getVersion() < schemaVersion; i++) { String fromSchemaText = sortedSchemaVersionInfos.get(i).getSchemaText(); checkCompatibility(schemaVersionService, schemaMetadata, schemaText, fromSchemaText); for (; i < size && sortedSchemaVersionInfos.get(i).getVersion() > schemaVersion; i++) {
private void updateCacheInvalidationEntries(SchemaVersionInfo schemaVersionInfo) { // need to support this as SchemaIdVersion supports multiple ways to construct for backward compatible APIs // this would have been simple without that. SchemaVersionKey schemaVersionKey = new SchemaVersionKey(schemaVersionInfo.getName(), schemaVersionInfo.getVersion()); SchemaIdVersion key1 = new SchemaIdVersion(schemaVersionInfo.getId()); idWithNameVersion.putIfAbsent(key1, schemaVersionKey); Long schemaMetadataId = schemaVersionInfo.getSchemaMetadataId(); // schemaMetadataId can be null from earlier registry instances. if (schemaMetadataId != null) { SchemaIdVersion key2 = new SchemaIdVersion(schemaMetadataId, schemaVersionInfo.getVersion()); nameVersionWithIds.putIfAbsent(schemaVersionKey, Lists.newArrayList(key1, key2)); idWithNameVersion.putIfAbsent(key2, schemaVersionKey); } else { nameVersionWithIds.putIfAbsent(schemaVersionKey, Collections.singletonList(key1)); } }
final String schemaText = versionInfo.getSchemaText();
private Set<SchemaIdVersion> transformToSchemaIdVersions(Collection<SchemaVersionInfo> versionInfos) { return versionInfos.stream().map(versionInfo -> new SchemaIdVersion(versionInfo.getSchemaMetadataId(), versionInfo.getVersion(), versionInfo.getId())) .collect(Collectors.toSet()); }
@Override public Object deserialize(MessageAndMetadata context, Integer readerSchemaVersion) throws SerDesException { byte protocolId = retrieveProtocolId(context); SchemaIdVersion schemaIdVersion = retrieveSchemaIdVersion(protocolId, context); SchemaMetadata schemaMetadata; SchemaVersionInfo schemaVersionInfo; try { schemaVersionInfo = schemaRegistryClient.getSchemaVersionInfo(schemaIdVersion); schemaMetadata = schemaRegistryClient.getSchemaMetadataInfo(schemaVersionInfo.getName()).getSchemaMetadata(); } catch (Exception e) { throw new RegistryException(e); } return doDeserialize(context, protocolId, schemaMetadata, schemaVersionInfo.getVersion(), readerSchemaVersion); }
return new SchemaIdVersion(schemaMetadataId, schemaVersionInfo.getVersion(), schemaVersionInfo.getId());
private Response doGetAllSchemaVersionForBranch(String schemaName, String branchName) { try { LOG.info("Get all versions for schema : {}", schemaName); Collection<SchemaVersionInfo> schemaVersionInfos = schemaRegistryClient.getAllVersions(effectiveBranchName(branchName), schemaName); LOG.debug("Received schema versions [{}] from schema registry for schema: {}", schemaVersionInfos, schemaName); if (schemaVersionInfos != null && !schemaVersionInfos.isEmpty()) { List<String> schemaVersions = schemaVersionInfos.stream().map(x -> x.getVersion().toString()).collect(Collectors.toList()); return WSUtils.respondEntities(schemaVersions, OK); } else { return WSUtils.respondEntity(Collections.EMPTY_LIST, NOT_FOUND); } } catch (SchemaNotFoundException e) { LOG.error("Schema not found: [{}]", schemaName, e); throw EntityNotFoundException.byName(schemaName); } catch (Exception e) { throw new RuntimeException(e); } }
@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; }
@Override public Collection<SchemaBranch> getSchemaBranches(String schemaName) throws SchemaNotFoundException { Collection<SchemaVersionInfo> schemaVersionInfos = getAllVersions(schemaName); return schemaVersionInfos.stream().flatMap(schemaVersionInfo -> { try { return schemaVersionLifecycleManager.getSchemaBranches(schemaVersionInfo.getId()).stream(); } catch (SchemaBranchNotFoundException e) { throw new RuntimeException(String.format("Failed to obtain schema branch associated with schema name : %s", schemaName),e); } }).collect(Collectors.toSet()); }
Byte stateId = schemaVersionInfo.getStateId(); List<SchemaVersionLifecycleStateTransition> nextTransitions = nextTransitionsForStateIds.get(stateId); SCHEMA_REGISTRY_CLIENT.transitionState(schemaVersionInfo.getId(), targetStateId, null); Assert.assertEquals(targetStateId, SCHEMA_REGISTRY_CLIENT.getSchemaVersionInfo(schemaIdVersion_2).getStateId());
public Collection<SchemaVersionInfo> getAllVersions(final String schemaBranchName, final String schemaName, final List<Byte> stateIds) throws SchemaNotFoundException, SchemaBranchNotFoundException { Preconditions.checkNotNull(schemaBranchName, "Schema branch name can't be null"); Preconditions.checkNotNull(stateIds, "State Ids can't be null"); Set<Byte> stateIdSet = stateIds.stream().collect(Collectors.toSet()); return getAllVersions(schemaBranchName, schemaName).stream(). filter(schemaVersionInfo -> stateIdSet.contains(schemaVersionInfo.getStateId())). collect(Collectors.toList()); }
Collection<AggregatedSchemaBranch> aggregatedSchemaBranches = new ArrayList<>(); for (SchemaBranch schemaBranch : getSchemaBranches(schemaName)) { Long rootVersion = schemaBranch.getName().equals(SchemaBranch.MASTER_BRANCH) ? null: schemaVersionLifecycleManager.getRootVersion(schemaBranch).getId(); Collection<SchemaVersionInfo> schemaVersionInfos = getAllVersions(schemaBranch.getName(), schemaName); schemaVersionInfos.stream().forEach(schemaVersionInfo -> { SchemaVersionLifecycleContext context = null; try { context = schemaVersionLifecycleManager.createSchemaVersionLifeCycleContext(schemaVersionInfo.getId(), SchemaVersionLifecycleStates.INITIATED); MergeInfo mergeInfo = null; if (context.getDetails() == null) { schemaVersionInfo.setMergeInfo(mergeInfo); } catch (SchemaNotFoundException e) { schemaVersionInfo.setMergeInfo(null);
@Override public SchemaMetadataInfo getSchemaMetadata(long schemaVersionId) throws SchemaNotFoundException { SchemaVersionInfo schemaVersionInfo = getSchemaVersionInfo(schemaVersionId); return getSchemaMetadataInfo(schemaVersionInfo.getName()); }
public SchemaVersionInfo toSchemaVersionInfo() { return new SchemaVersionInfo(id, name, version, getSchemaMetadataId(), schemaText, timestamp, description, state); }
@POST @Path("/subjects/{subject}") @ApiOperation(value = "Get schema information for the given schema subject and schema text", response = Schema.class, tags = OPERATION_GROUP_CONFLUENT_SR) @Timed @UnitOfWork public Response lookupSubjectVersion(@ApiParam(value = "Schema subject", required = true) @PathParam("subject") String subject, @ApiParam(value = "The schema ", required = true) String schema) { Response response; try { SchemaVersionInfo schemaVersionInfo = schemaRegistry.getSchemaVersionInfo(subject, schemaStringFromJson(schema).getSchema()); if (schemaVersionInfo != null) { response = WSUtils.respondEntity(new Schema(schemaVersionInfo.getName(), schemaVersionInfo.getVersion(), schemaVersionInfo.getId(), schemaVersionInfo.getSchemaText()), Response.Status.OK); } else { response = WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, subject); } } catch (InvalidSchemaException ex) { LOG.error("Given schema is invalid", ex); response = invalidSchemaError(); } catch (SchemaNotFoundException ex) { LOG.error("No schema found with subject [{}]", subject, ex); response = subjectNotFoundError(); } catch (Exception ex) { LOG.error("Encountered error while retrieving schema version with subject: [{}]", subject, ex); response = serverError(); } return response; }
int schemaVersion = schemaVersionInfo.getVersion(); String schemaText = schemaVersionInfo.getSchemaText(); List<SchemaVersionInfo> allEnabledSchemaVersions = schemaVersionService.getAllSchemaVersions(SchemaBranch.MASTER_BRANCH, schemaName) .stream() .filter(x -> SchemaVersionLifecycleStates.ENABLED.getId().equals(x.getStateId())) .collect(Collectors.toList()); if (validationLevel.equals(SchemaValidationLevel.ALL)) { for (SchemaVersionInfo curSchemaVersionInfo : allEnabledSchemaVersions) { int curVersion = curSchemaVersionInfo.getVersion(); if (curVersion < schemaVersion) { checkCompatibility(schemaVersionService, schemaMetadata, schemaText, curSchemaVersionInfo.getSchemaText()); } else { checkCompatibility(schemaVersionService, schemaMetadata, curSchemaVersionInfo.getSchemaText(), schemaText); int i = 0; int size = sortedSchemaVersionInfos.size(); for (; i < size && sortedSchemaVersionInfos.get(i).getVersion() < schemaVersion; i++) { String fromSchemaText = sortedSchemaVersionInfos.get(i).getSchemaText(); checkCompatibility(schemaVersionService, schemaMetadata, schemaText, fromSchemaText); for (; i < size && sortedSchemaVersionInfos.get(i).getVersion() > schemaVersion; i++) {
private void updateCacheInvalidationEntries(SchemaVersionInfo schemaVersionInfo) { // need to support this as SchemaIdVersion supports multiple ways to construct for backward compatible APIs // this would have been simple without that. SchemaVersionKey schemaVersionKey = new SchemaVersionKey(schemaVersionInfo.getName(), schemaVersionInfo.getVersion()); SchemaIdVersion key1 = new SchemaIdVersion(schemaVersionInfo.getId()); idWithNameVersion.putIfAbsent(key1, schemaVersionKey); Long schemaMetadataId = schemaVersionInfo.getSchemaMetadataId(); // schemaMetadataId can be null from earlier registry instances. if (schemaMetadataId != null) { SchemaIdVersion key2 = new SchemaIdVersion(schemaMetadataId, schemaVersionInfo.getVersion()); nameVersionWithIds.putIfAbsent(schemaVersionKey, Lists.newArrayList(key1, key2)); idWithNameVersion.putIfAbsent(key2, schemaVersionKey); } else { nameVersionWithIds.putIfAbsent(schemaVersionKey, Collections.singletonList(key1)); } }
private String getResultantSchema(SchemaVersionKey schemaVersionKey, Map<String, SchemaParsingState> schemaParsingStates) throws InvalidSchemaException, SchemaNotFoundException { String schemaText = schemaVersionRetriever.retrieveSchemaVersion(schemaVersionKey).getSchemaText(); return getResultantSchema(schemaText, schemaParsingStates); }