@Override public SchemaIdVersion handleSchemaVersionDeserialization(InputStream inputStream) { ByteBuffer byteBuffer = ByteBuffer.allocate(4); try { inputStream.read(byteBuffer.array()); } catch (IOException e) { throw new AvroRetryableException(e); } int schemaVersionId = byteBuffer.getInt(); return new SchemaIdVersion((long) schemaVersionId); }
@Override public SchemaIdVersion handleSchemaVersionDeserialization(InputStream inputStream) throws SerDesException { ByteBuffer byteBuffer = ByteBuffer.allocate(8); try { inputStream.read(byteBuffer.array()); } catch (IOException e) { throw new AvroRetryableException(e); } return new SchemaIdVersion(byteBuffer.getLong()); }
@Override public SchemaVersionInfo getSchemaVersionInfo(long schemaVersionId) throws SchemaNotFoundException { return SchemaVersionLifecycleManager.this.getSchemaVersionInfo(new SchemaIdVersion(schemaVersionId)); }
@Override public SchemaIdVersion handleSchemaVersionDeserialization(InputStream inputStream) { ByteBuffer byteBuffer = ByteBuffer.allocate(4); try { inputStream.read(byteBuffer.array()); } catch (IOException e) { throw new AvroRetryableException(e); } int schemaVersionId = byteBuffer.getInt(); return new SchemaIdVersion((long) schemaVersionId); }
@Override public SchemaIdVersion handleSchemaVersionDeserialization(InputStream inputStream) { // 8 bytes : schema metadata Id // 4 bytes : schema version ByteBuffer byteBuffer = ByteBuffer.allocate(12); try { inputStream.read(byteBuffer.array()); } catch (IOException e) { throw new AvroRetryableException(e); } long schemaMetadataId = byteBuffer.getLong(); int schemaVersion = byteBuffer.getInt(); return new SchemaIdVersion(schemaMetadataId, schemaVersion); }
@Override protected SchemaIdVersion retrieveSchemaIdVersion(byte protocolId, MessageContext input) throws SerDesException { Long id = (Long) input.headers.get("schema.metadata.id"); Integer version = (Integer) input.headers.get("schema.version"); return new SchemaIdVersion(id, version); } }
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 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)); } }
@GET @Path("/schemas/versionsById/{id}") @ApiOperation(value = "Get a version of the schema identified by the given versionid", response = SchemaVersionInfo.class, tags = OPERATION_GROUP_SCHEMA) @Timed @UnitOfWork public Response getSchemaVersionById(@ApiParam(value = "version identifier of the schema", required = true) @PathParam("id") Long versionId) { SchemaIdVersion schemaIdVersion = new SchemaIdVersion(versionId); Response response; try { SchemaVersionInfo schemaVersionInfo = schemaRegistry.getSchemaVersionInfo(schemaIdVersion); response = WSUtils.respondEntity(schemaVersionInfo, Response.Status.OK); } catch (SchemaNotFoundException e) { LOG.info("No schema version is found with schema version id : [{}]", versionId); response = WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, versionId.toString()); } catch (Exception ex) { LOG.error("Encountered error while getting schema version with id [{}]", versionId, ex); response = WSUtils.respond(Response.Status.INTERNAL_SERVER_ERROR, CatalogResponse.ResponseMessage.EXCEPTION, ex.getMessage()); } return response; }
@GET @Path("/schemas/versionsById/{id}") @ApiOperation(value = "Get a version of the schema identified by the given versionid", response = SchemaVersionInfo.class, tags = OPERATION_GROUP_SCHEMA) @Timed @UnitOfWork public Response getSchemaVersionById(@ApiParam(value = "version identifier of the schema", required = true) @PathParam("id") Long versionId) { SchemaIdVersion schemaIdVersion = new SchemaIdVersion(versionId); Response response; try { SchemaVersionInfo schemaVersionInfo = schemaRegistry.getSchemaVersionInfo(schemaIdVersion); response = WSUtils.respondEntity(schemaVersionInfo, Response.Status.OK); } catch (SchemaNotFoundException e) { LOG.info("No schema version is found with schema version id : [{}]", versionId); response = WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, versionId.toString()); } catch (Exception ex) { LOG.error("Encountered error while getting schema version with id [{}]", versionId, ex); response = WSUtils.respond(Response.Status.INTERNAL_SERVER_ERROR, CatalogResponse.ResponseMessage.EXCEPTION, ex.getMessage()); } return response; }
private boolean transitionSchemaVersionState(Long schemaVersionId, String operationOrTargetState, byte[] transitionDetails) throws SchemaNotFoundException, SchemaLifecycleException { WebTarget webTarget = currentSchemaRegistryTargets().schemaVersionsTarget.path(schemaVersionId + "/state/" + operationOrTargetState); Response response = Subject.doAs(subject, new PrivilegedAction<Response>() { @Override public Response run() { return webTarget.request().post(Entity.text(transitionDetails)); } }); boolean result = handleSchemaLifeCycleResponse(response); // invalidate this entry from cache. schemaVersionInfoCache.invalidateSchema(SchemaVersionInfoCache.Key.of(new SchemaIdVersion(schemaVersionId))); return result; }
private List<SchemaVersionInfo> getSortedSchemaVersions(Long schemaBranchId) throws SchemaNotFoundException, SchemaBranchNotFoundException { List<QueryParam> schemaVersionMappingStorableQueryParams = Lists.newArrayList(); schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_BRANCH_ID, schemaBranchId .toString())); List<OrderByField> orderByFields = new ArrayList<>(); orderByFields.add(OrderByField.of(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, false)); List<SchemaVersionInfo> schemaVersionInfos = new ArrayList<>(); Collection<SchemaBranchVersionMapping> storables = storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionMappingStorableQueryParams, orderByFields); if (storables == null || storables.size() == 0) { if (schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchId)) .getName() .equals(SchemaBranch.MASTER_BRANCH)) return Collections.emptyList(); else throw new InvalidSchemaBranchVersionMapping(String.format("No schema versions are attached to the schema branch id : '%s'", schemaBranchId)); } for (SchemaBranchVersionMapping storable : storables) { SchemaIdVersion schemaIdVersion = new SchemaIdVersion(storable.getSchemaVersionInfoId()); schemaVersionInfos.add(schemaVersionInfoCache.getSchema(SchemaVersionInfoCache.Key.of(schemaIdVersion))); } return schemaVersionInfos; }
@GET @Path("/schemas/ids/{id}") @ApiOperation(value = "Get schema version by id", response = Schema.class, tags = OPERATION_GROUP_CONFLUENT_SR) @Timed @UnitOfWork public Response getSchemaById(@ApiParam(value = "schema version id", required = true) @PathParam("id") Long id) { Response response; try { SchemaVersionInfo schemaVersionInfo = schemaRegistry.getSchemaVersionInfo(new SchemaIdVersion(id)); SchemaString schema = new SchemaString(); schema.setSchema(schemaVersionInfo.getSchemaText()); response = WSUtils.respondEntity(schema, Response.Status.OK); } catch (SchemaNotFoundException ex) { LOG.error("No schema version found with id [{}]", id, ex); response = schemaNotFoundError(); } catch (Exception ex) { LOG.error("Encountered error while retrieving Schema with id: [{}]", id, ex); response = serverError(); } return response; }
@GET @Path("/schemas/ids/{id}") @ApiOperation(value = "Get schema version by id", response = Schema.class, tags = OPERATION_GROUP_CONFLUENT_SR) @Timed @UnitOfWork public Response getSchemaById(@ApiParam(value = "schema version id", required = true) @PathParam("id") Long id) { Response response; try { SchemaVersionInfo schemaVersionInfo = schemaRegistry.getSchemaVersionInfo(new SchemaIdVersion(id)); SchemaString schema = new SchemaString(); schema.setSchema(schemaVersionInfo.getSchemaText()); response = WSUtils.respondEntity(schema, Response.Status.OK); } catch (SchemaNotFoundException ex) { LOG.error("No schema version found with id [{}]", id, ex); response = schemaNotFoundError(); } catch (Exception ex) { LOG.error("Encountered error while retrieving Schema with id: [{}]", id, ex); response = serverError(); } return response; }
private Set<SchemaIdVersion> transformToSchemaIdVersions(Collection<SchemaVersionInfo> versionInfos) { return versionInfos.stream().map(versionInfo -> new SchemaIdVersion(versionInfo.getSchemaMetadataId(), versionInfo.getVersion(), versionInfo.getId())) .collect(Collectors.toSet()); }
private void doDeleteSchemaVersion(Long schemaVersionId) throws SchemaNotFoundException, SchemaLifecycleException { SchemaVersionInfoCache.Key schemaVersionCacheKey = SchemaVersionInfoCache.Key.of(new SchemaIdVersion(schemaVersionId)); invalidateSchemaInAllHAServer(schemaVersionCacheKey); storageManager.remove(createSchemaVersionStorableKey(schemaVersionId)); deleteSchemaVersionBranchMapping(schemaVersionId); }
private SchemaIdVersion handleSchemaIdVersionResponse(SchemaMetadataInfo schemaMetadataInfo, Response response) throws IncompatibleSchemaException, InvalidSchemaException { int status = response.getStatus(); String msg = response.readEntity(String.class); if (status == Response.Status.BAD_REQUEST.getStatusCode() || status == Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()) { CatalogResponse catalogResponse = readCatalogResponse(msg); if (CatalogResponse.ResponseMessage.INCOMPATIBLE_SCHEMA.getCode() == catalogResponse.getResponseCode()) { throw new IncompatibleSchemaException(catalogResponse.getResponseMessage()); } else if (CatalogResponse.ResponseMessage.INVALID_SCHEMA.getCode() == catalogResponse.getResponseCode()) { throw new InvalidSchemaException(catalogResponse.getResponseMessage()); } else { throw new RuntimeException(catalogResponse.getResponseMessage()); } } Integer version = readEntity(msg, Integer.class); SchemaVersionInfo schemaVersionInfo = doGetSchemaVersionInfo(new SchemaVersionKey(schemaMetadataInfo.getSchemaMetadata() .getName(), version)); return new SchemaIdVersion(schemaMetadataInfo.getId(), version, schemaVersionInfo.getId()); }
public SchemaIdVersion addSchemaVersion(String schemaBranchName, SchemaMetadataInfo schemaMetadataInfo, SchemaVersion schemaVersion) throws SchemaNotFoundException, IncompatibleSchemaException, InvalidSchemaException, SchemaBranchNotFoundException { Preconditions.checkNotNull(schemaBranchName, "Schema branch name can't be null"); checkSchemaText(schemaVersion.getSchemaText()); SchemaVersionInfo schemaVersionInfo; // check whether there exists schema-metadata for schema-metadata-key SchemaMetadata schemaMetadata = schemaMetadataInfo.getSchemaMetadata(); // check whether the same schema text exists schemaVersionInfo = findSchemaVersion(schemaBranchName, schemaMetadata.getType(), schemaVersion.getSchemaText(), schemaMetadataInfo .getSchemaMetadata().getName()); if (schemaVersionInfo == null) { schemaVersionInfo = createSchemaVersion(schemaBranchName, schemaMetadata, schemaMetadataInfo.getId(), schemaVersion); } return new SchemaIdVersion(schemaMetadataInfo.getId(), schemaVersionInfo.getVersion(), schemaVersionInfo.getId()); }
@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; }
private void storeSchemaVersionState(SchemaVersionLifecycleContext schemaVersionLifecycleContext) throws SchemaNotFoundException { // store versions state, sequence SchemaVersionStateStorable stateStorable = new SchemaVersionStateStorable(); Long schemaVersionId = schemaVersionLifecycleContext.getSchemaVersionId(); byte stateId = schemaVersionLifecycleContext.getState().getId(); stateStorable.setSchemaVersionId(schemaVersionId); stateStorable.setSequence(schemaVersionLifecycleContext.getSequence() + 1); stateStorable.setStateId(stateId); stateStorable.setTimestamp(System.currentTimeMillis()); stateStorable.setDetails(schemaVersionLifecycleContext.getDetails()); stateStorable.setId(storageManager.nextId(SchemaVersionStateStorable.NAME_SPACE)); storageManager.add(stateStorable); // store latest state in versions entity StorableKey storableKey = new StorableKey(SchemaVersionStorable.NAME_SPACE, SchemaVersionStorable.getPrimaryKey(schemaVersionId)); SchemaVersionStorable versionedSchema = storageManager.get(storableKey); if (versionedSchema == null) { throw new SchemaNotFoundException("No Schema version exists with id " + schemaVersionId); } versionedSchema.setState(stateId); storageManager.update(versionedSchema); // invalidate schema version from cache SchemaVersionInfoCache.Key schemaVersionCacheKey = SchemaVersionInfoCache.Key.of(new SchemaIdVersion(schemaVersionId)); invalidateSchemaInAllHAServer(schemaVersionCacheKey); }