use of com.hortonworks.registries.common.QueryParam in project registry by hortonworks.
the class SchemaVersionLifecycleManager method deleteSchemaVersionBranchMapping.
private void deleteSchemaVersionBranchMapping(Long schemaVersionId) throws SchemaNotFoundException, SchemaLifecycleException {
List<QueryParam> schemaVersionMappingStorableQueryParams = Lists.newArrayList();
schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, schemaVersionId.toString()));
List<OrderByField> orderByFields = new ArrayList<>();
orderByFields.add(OrderByField.of(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, false));
Collection<SchemaBranchVersionMapping> storables = storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionMappingStorableQueryParams, orderByFields);
if (storables == null || storables.isEmpty()) {
LOG.debug("No need to delete schema version mapping as the database did a cascade delete");
return;
}
if (storables.size() > 1) {
List<String> branchNamesTiedToSchema = storables.stream().map(storable -> schemaBranchCache.get(SchemaBranchCache.Key.of(storable.getSchemaBranchId())).getName()).collect(Collectors.toList());
throw new SchemaLifecycleException(String.format("Schema version with id : '%s' is tied with more than one branch : '%s' ", schemaVersionId.toString(), Arrays.toString(branchNamesTiedToSchema.toArray())));
}
storageManager.remove(new StorableKey(SchemaBranchVersionMapping.NAMESPACE, storables.iterator().next().getPrimaryKey()));
}
use of com.hortonworks.registries.common.QueryParam in project registry by hortonworks.
the class SchemaVersionLifecycleManager method getAllVersions.
public Collection<SchemaVersionInfo> getAllVersions(final String schemaName) throws SchemaNotFoundException {
List<QueryParam> queryParams = Collections.singletonList(new QueryParam(SchemaVersionStorable.NAME, schemaName));
SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName);
if (schemaMetadataInfo == null) {
throw new SchemaNotFoundException("Schema not found with name " + schemaName);
}
Collection<SchemaVersionStorable> storables = storageManager.find(SchemaVersionStorable.NAME_SPACE, queryParams, Collections.singletonList(OrderByField.of(SchemaVersionStorable.VERSION, true)));
List<SchemaVersionInfo> schemaVersionInfos;
if (storables != null && !storables.isEmpty()) {
schemaVersionInfos = storables.stream().map(SchemaVersionStorable::toSchemaVersionInfo).collect(Collectors.toList());
} else {
schemaVersionInfos = Collections.emptyList();
}
return schemaVersionInfos;
}
use of com.hortonworks.registries.common.QueryParam in project registry by hortonworks.
the class SchemaVersionLifecycleManager method getSchemaBranches.
public Set<SchemaBranch> getSchemaBranches(Long schemaVersionId) throws SchemaBranchNotFoundException {
List<QueryParam> schemaVersionMappingStorableQueryParams = new ArrayList<>();
Set<SchemaBranch> schemaBranches = new HashSet<>();
schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, schemaVersionId.toString()));
for (Storable storable : storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionMappingStorableQueryParams)) {
schemaBranches.add(schemaBranchCache.get(SchemaBranchCache.Key.of(((SchemaBranchVersionMapping) storable).getSchemaBranchId())));
}
return schemaBranches;
}
use of com.hortonworks.registries.common.QueryParam in project registry by hortonworks.
the class SchemaVersionLifecycleManager method mergeSchemaVersion.
public SchemaVersionMergeResult mergeSchemaVersion(Long schemaVersionId, SchemaVersionMergeStrategy schemaVersionMergeStrategy) throws SchemaNotFoundException, IncompatibleSchemaException {
try {
SchemaVersionInfo schemaVersionInfo = getSchemaVersionInfo(new SchemaIdVersion(schemaVersionId));
SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaVersionInfo.getName());
Set<SchemaBranch> schemaBranches = getSchemaBranches(schemaVersionId).stream().filter(schemaBranch -> {
try {
return !getRootVersion(schemaBranch).getId().equals(schemaVersionId);
} catch (SchemaNotFoundException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toSet());
if (schemaBranches.size() > 1) {
throw new SchemaVersionMergeException(String.format("Can't determine a unique schema branch for schema version id : '%s'", schemaVersionId));
} else if (schemaBranches.size() == 0) {
throw new SchemaVersionMergeException(String.format("Schema version id : '%s' is not associated with any branch", schemaVersionId));
}
Long schemaBranchId = schemaBranches.iterator().next().getId();
SchemaBranch schemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchId));
if (schemaVersionMergeStrategy.equals(SchemaVersionMergeStrategy.PESSIMISTIC)) {
SchemaVersionInfo latestSchemaVersion = getLatestEnabledSchemaVersionInfo(SchemaBranch.MASTER_BRANCH, schemaMetadataInfo.getSchemaMetadata().getName());
SchemaVersionInfo rootSchemaVersion = getRootVersion(schemaBranch);
if (!latestSchemaVersion.getId().equals(rootSchemaVersion.getId())) {
throw new SchemaVersionMergeException(String.format("The latest version of '%s' is different from the root version of the branch : '%s'", SchemaBranch.MASTER_BRANCH, schemaMetadataInfo.getSchemaMetadata().getName()));
}
}
byte[] initializedStateDetails;
try {
initializedStateDetails = ObjectMapperUtils.serialize(new InitializedStateDetails(schemaBranch.getName(), schemaVersionInfo.getId()));
} catch (JsonProcessingException e) {
throw new RuntimeException(String.format("Failed to serialize initializedState for %s and %s", schemaBranch.getName(), schemaVersionInfo.getId()));
}
SchemaVersionInfo createdSchemaVersionInfo;
try {
SchemaVersionInfo existingSchemaVersionInfo = findSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadataInfo.getSchemaMetadata().getType(), schemaVersionInfo.getSchemaText(), schemaMetadataInfo.getSchemaMetadata().getName());
if (existingSchemaVersionInfo != null) {
String mergeMessage = String.format("Given version %d is already merged to master with version %d", schemaVersionId, existingSchemaVersionInfo.getVersion());
LOG.info(mergeMessage);
return new SchemaVersionMergeResult(new SchemaIdVersion(schemaMetadataInfo.getId(), existingSchemaVersionInfo.getVersion(), existingSchemaVersionInfo.getId()), mergeMessage);
}
createdSchemaVersionInfo = createSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadataInfo.getSchemaMetadata(), schemaMetadataInfo.getId(), new SchemaVersion(schemaVersionInfo.getSchemaText(), schemaVersionInfo.getDescription(), SchemaVersionLifecycleStates.INITIATED.getId(), initializedStateDetails));
} catch (InvalidSchemaException e) {
throw new SchemaVersionMergeException(String.format("Failed to merge schema version : '%s'", schemaVersionId.toString()), e);
}
Collection<SchemaVersionStateStorable> schemaVersionStates = storageManager.find(SchemaVersionStateStorable.NAME_SPACE, Collections.singletonList(new QueryParam(SchemaVersionStateStorable.SCHEMA_VERSION_ID, schemaVersionId.toString())), Collections.singletonList(OrderByField.of(SchemaVersionStateStorable.SEQUENCE, true)));
if (schemaVersionStates == null || schemaVersionStates.isEmpty()) {
throw new RuntimeException(String.format("The database doesn't have any state transition recorded for the schema version id : '%s'", schemaVersionId));
}
updateSchemaVersionState(createdSchemaVersionInfo.getId(), schemaVersionStates.iterator().next().getSequence(), SchemaVersionLifecycleStates.ENABLED.getId(), null);
String mergeMessage = String.format("Given version %d is merged successfully to master with version %d", schemaVersionId, createdSchemaVersionInfo.getVersion());
LOG.info(mergeMessage);
return new SchemaVersionMergeResult(new SchemaIdVersion(schemaMetadataInfo.getId(), createdSchemaVersionInfo.getVersion(), createdSchemaVersionInfo.getId()), mergeMessage);
} catch (SchemaBranchNotFoundException e) {
throw new SchemaVersionMergeException(String.format("Failed to merge schema version : '%s'", schemaVersionId.toString()), e);
}
}
use of com.hortonworks.registries.common.QueryParam in project registry by hortonworks.
the class DefaultSchemaRegistry method getSchemaMetadataInfo.
@Override
public SchemaMetadataInfo getSchemaMetadataInfo(Long schemaMetadataId) {
SchemaMetadataStorable givenSchemaMetadataStorable = new SchemaMetadataStorable();
givenSchemaMetadataStorable.setId(schemaMetadataId);
List<QueryParam> params = Collections.singletonList(new QueryParam(SchemaMetadataStorable.ID, schemaMetadataId.toString()));
Collection<SchemaMetadataStorable> schemaMetadataStorables = storageManager.find(SchemaMetadataStorable.NAME_SPACE, params);
SchemaMetadataInfo schemaMetadataInfo = null;
if (schemaMetadataStorables != null && !schemaMetadataStorables.isEmpty()) {
schemaMetadataInfo = schemaMetadataStorables.iterator().next().toSchemaMetadataInfo();
if (schemaMetadataStorables.size() > 1) {
LOG.warn("No unique entry with schemaMetatadataId: [{}]", schemaMetadataId);
}
LOG.info("SchemaMetadata entries with id [{}] is [{}]", schemaMetadataStorables);
}
return schemaMetadataInfo;
}
Aggregations