@Override public void doHandleSchemaVersionSerialization(OutputStream outputStream, SchemaIdVersion schemaIdVersion) throws SerDesException { try { Long versionId = schemaIdVersion.getSchemaVersionId(); outputStream.write(ByteBuffer.allocate(8) .putLong(versionId).array()); } catch (IOException e) { throw new AvroRetryableException(e); } }
@Override protected void doHandleSchemaVersionSerialization(OutputStream outputStream, SchemaIdVersion schemaIdVersion) { Long versionId = schemaIdVersion.getSchemaVersionId(); if (versionId > Integer.MAX_VALUE) { throw new AvroException("Unsupported versionId, max id=" + Integer.MAX_VALUE + " , but was id=" + versionId); } else { // 4 bytes try { outputStream.write(ByteBuffer.allocate(4) .putInt(versionId.intValue()).array()); } catch (IOException e) { throw new AvroRetryableException(e); } } }
@Override public void handleSchemaVersionSerialization(OutputStream outputStream, SchemaIdVersion schemaIdVersion) { Long versionId = schemaIdVersion.getSchemaVersionId(); if (versionId > Integer.MAX_VALUE) { // if it is more than int max, fallback to SchemaVersionIdAsLongProtocolHandler LOG.debug("Upgraded to " + delegate + " as versionId is more than max integer"); delegate.handleSchemaVersionSerialization(outputStream, schemaIdVersion); } else { // 4 bytes try { outputStream.write(new byte[]{protocolId}); outputStream.write(ByteBuffer.allocate(4) .putInt(versionId.intValue()).array()); } catch (IOException e) { throw new AvroException(e); } } }
private SchemaVersionInfo doGetSchemaVersionInfo(SchemaIdVersion schemaIdVersion) throws SchemaNotFoundException { if (schemaIdVersion.getSchemaVersionId() != null) { LOG.info("Getting schema version from target registry for [{}]", schemaIdVersion.getSchemaVersionId()); return getEntity(currentSchemaRegistryTargets() .schemaVersionsByIdTarget .path(schemaIdVersion.getSchemaVersionId().toString()), SchemaVersionInfo.class); } else if (schemaIdVersion.getSchemaMetadataId() != null) { SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaIdVersion.getSchemaMetadataId()); SchemaVersionKey schemaVersionKey = new SchemaVersionKey(schemaMetadataInfo.getSchemaMetadata() .getName(), schemaIdVersion.getVersion()); LOG.info("Getting schema version from target registry for key [{}]", schemaVersionKey); return doGetSchemaVersionInfo(schemaVersionKey); } throw new IllegalArgumentException("Given argument not valid: " + schemaIdVersion); }
private SchemaVersionInfo retrieveSchemaVersionInfo(SchemaIdVersion key) throws SchemaNotFoundException { SchemaVersionInfo schemaVersionInfo = null; if (key.getSchemaVersionId() != null) { schemaVersionInfo = fetchSchemaVersionInfo(key.getSchemaVersionId()); } else if (key.getSchemaMetadataId() != null) { SchemaMetadataInfo schemaMetadataInfo = schemaMetadataFetcher.getSchemaMetadataInfo(key.getSchemaMetadataId()); Integer version = key.getVersion(); schemaVersionInfo = fetchSchemaVersionInfo(schemaMetadataInfo.getSchemaMetadata().getName(), version); } else { throw new IllegalArgumentException("Invalid SchemaIdVersion: " + key); } return schemaVersionInfo; }
@Test (expected = SchemaLifecycleException.class) public void deleteRootSchemaOfBranch() throws InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, IOException, SchemaBranchAlreadyExistsException, SchemaLifecycleException { SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.NONE); SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc"); addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion1.getSchemaVersionId()); schemaRegistryClient.archiveSchemaVersion(masterSchemaIdVersion1.getSchemaVersionId()); schemaRegistryClient.deleteSchemaVersion(masterSchemaIdVersion1.getSchemaVersionId()); }
@Test (expected = InvalidSchemaBranchDeletionException.class) public void deleteBranchWithRootVersionOfAnotherBranch() throws InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, IOException, SchemaBranchAlreadyExistsException, SchemaLifecycleException, InvalidSchemaBranchDeletionException { SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), 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"); addSchemaBranch("CHILD-BRANCH", schemaMetadata,schemaBranch1Version1.getSchemaVersionId()); schemaRegistryClient.enableSchemaVersion(schemaBranch1Version1.getSchemaVersionId()); schemaRegistryClient.archiveSchemaVersion(schemaBranch1Version1.getSchemaVersionId()); schemaRegistryClient.deleteSchemaBranch(schemaBranch1.getId()); }
@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)); }
@Test public void deleteBranchWithArchivedSchema() throws InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, IOException, SchemaBranchAlreadyExistsException, SchemaLifecycleException, InvalidSchemaBranchDeletionException { SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), 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"); schemaRegistryClient.enableSchemaVersion(schemaBranch1Version1.getSchemaVersionId()); schemaRegistryClient.archiveSchemaVersion(schemaBranch1Version1.getSchemaVersionId()); schemaRegistryClient.deleteSchemaBranch(schemaBranch1.getId()); }
@Test (expected = SchemaNotFoundException.class) public void mergeSchemaWithInvalidSchemaVersion() 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 schemaBranch1Version1 = addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc"); schemaRegistryClient.mergeSchemaVersion(schemaBranch1Version1.getSchemaVersionId()+1); }
@Test (expected = SchemaBranchAlreadyExistsException.class) public void createAlreadyExistingBranch() 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"); addSchemaBranch(schemaBranch1.getName(), schemaMetadata, masterSchemaIdVersion2.getSchemaVersionId()); }
@Test public void mergeSchemaWhenRootVersionIsNotLatest() 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 schemaBranch1Version1 = addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc"); addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device-compat.avsc"); schemaRegistryClient.mergeSchemaVersion(schemaBranch1Version1.getSchemaVersionId()); }
@Test (expected = InvalidSchemaBranchDeletionException.class) public void deleteBranchWithEnabledSchema() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException, InvalidSchemaBranchDeletionException, SchemaLifecycleException { SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), 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"); schemaRegistryClient.enableSchemaVersion(schemaBranch1Version1.getSchemaVersionId()); schemaRegistryClient.deleteSchemaBranch(schemaBranch1.getId()); }
@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); }
@Test public void testGetSchemaVersionFromStates() throws IOException, SchemaNotFoundException, InvalidSchemaException, IncompatibleSchemaException, SchemaLifecycleException, SchemaBranchAlreadyExistsException { SchemaMetadata schemaMetadata = createSchemaMetadata(TEST_NAME_RULE.getMethodName(), SchemaCompatibility.NONE); String schemaName = schemaMetadata.getName(); Long id = SCHEMA_REGISTRY_CLIENT.registerSchemaMetadata(schemaMetadata); SchemaIdVersion v1 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-1.avsc"), "Initial version of the schema")); SchemaIdVersion v2 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-2.avsc"), "Second version of the schema")); SchemaIdVersion v3 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-3.avsc"), "Third version of the schema, removes name field")); SchemaBranch schemaBranch = SCHEMA_REGISTRY_CLIENT.createSchemaBranch(v3.getSchemaVersionId(), new SchemaBranch("Branch-1", schemaName)); SchemaIdVersion v4 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaBranch.getName(), schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-4.avsc"), "Forth version of the schema, adds back name field, but different type")); SCHEMA_REGISTRY_CLIENT.startSchemaVersionReview(v4.getSchemaVersionId()); SCHEMA_REGISTRY_CLIENT.transitionState(v4.getSchemaVersionId(), SchemaVersionLifecycleStates.REVIEWED.getId(), null); SCHEMA_REGISTRY_CLIENT.archiveSchemaVersion(v2.getSchemaVersionId()); SCHEMA_REGISTRY_CLIENT.archiveSchemaVersion(v3.getSchemaVersionId()); Assert.assertEquals( transformToSchemaIdVersions(SCHEMA_REGISTRY_CLIENT.getAllVersions(SchemaBranch.MASTER_BRANCH, schemaName, Collections.singletonList(SchemaVersionLifecycleStates.ENABLED.getId()))), new HashSet<>(Arrays.asList(v1))); Assert.assertEquals( transformToSchemaIdVersions(SCHEMA_REGISTRY_CLIENT.getAllVersions(SchemaBranch.MASTER_BRANCH, schemaName, Collections.singletonList(SchemaVersionLifecycleStates.ARCHIVED.getId()))), new HashSet<>(Arrays.asList(v2,v3))); Assert.assertEquals( transformToSchemaIdVersions(SCHEMA_REGISTRY_CLIENT.getAllVersions(schemaBranch.getName(), schemaName, Collections.singletonList(SchemaVersionLifecycleStates.REVIEWED.getId()))), new HashSet<>(Arrays.asList(v4))); SCHEMA_REGISTRY_CLIENT.disableSchemaVersion(v1.getSchemaVersionId()); Assert.assertEquals( transformToSchemaIdVersions(SCHEMA_REGISTRY_CLIENT.getAllVersions(SchemaBranch.MASTER_BRANCH, schemaName, Arrays.asList(SchemaVersionLifecycleStates.ARCHIVED.getId(), SchemaVersionLifecycleStates.DISABLED.getId()))), new HashSet<>(Arrays.asList(v1,v2,v3))); }
@Test (expected = IncompatibleSchemaException.class) public void addIncompatibleSchemaToBranch() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException { SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.BACKWARD); SchemaIdVersion masterSchemaIdVersion = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc"); SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion.getSchemaVersionId()); addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc"); }
@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); }
@Test (expected = SchemaBranchNotFoundException.class) public void deleteInvalidSchemaBranch() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException, InvalidSchemaBranchDeletionException { 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"); schemaRegistryClient.deleteSchemaBranch(schemaBranch1.getId() + 1); }
@Test (expected = InvalidSchemaException.class) public void addInvalidSchemaToBranch() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException { SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.BACKWARD); SchemaIdVersion masterSchemaIdVersion = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc"); SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion.getSchemaVersionId()); String schema2 = "--- invalid schema ---"; schemaRegistryClient.addSchemaVersion(schemaBranch1.getName(), schemaMetadata.getName(), new SchemaVersion(schema2, "second version")); }
@Test public void deleteSchemaBranch() throws SchemaNotFoundException, SchemaBranchAlreadyExistsException, IOException, InvalidSchemaException, IncompatibleSchemaException, SchemaBranchNotFoundException, InvalidSchemaBranchDeletionException { 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"); Assert.assertTrue(schemaRegistryClient.getSchemaBranches(schemaMetadata.getName()).size() == 2); schemaRegistryClient.deleteSchemaBranch(schemaBranch1.getId()); Collection<SchemaVersionInfo> masterSchemaVersionInfos = schemaRegistryClient.getAllVersions(schemaMetadata.getName()); Assert.assertTrue(masterSchemaVersionInfos.size() == 1); Assert.assertTrue(schemaRegistryClient.getSchemaBranches(schemaMetadata.getName()).size() == 1); }