use of org.finra.herd.model.jpa.StorageUnitEntity in project herd by FINRAOS.
the class Hive13DdlGenerator method processStorageUnitsForGenerateDdl.
/**
* Adds the relative "alter table add partition" statements for each storage unit entity. Please note that each request partition value might result in
* multiple available storage unit entities (subpartitions).
*
* @param sb the string builder to be updated with the "alter table add partition" statements
* @param replacements the hash map of string values to be used to substitute the custom DDL tokens with their actual values
* @param businessObjectFormatEntity the business object format entity
* @param businessObjectFormat the business object format
* @param ifNotExistsOption specifies if generated DDL contains "if not exists" option
* @param storageUnitEntities the list of storage unit entities
*/
private void processStorageUnitsForGenerateDdl(GenerateDdlRequest generateDdlRequest, StringBuilder sb, HashMap<String, String> replacements, BusinessObjectFormatEntity businessObjectFormatEntity, BusinessObjectFormat businessObjectFormat, String ifNotExistsOption, List<StorageUnitEntity> storageUnitEntities) {
// If flag is not set to suppress scan for unregistered sub-partitions, retrieve all storage
// file paths for the relative storage units loaded in a multi-valued map for easy access.
MultiValuedMap<Integer, String> storageUnitIdToStorageFilePathsMap = BooleanUtils.isTrue(generateDdlRequest.suppressScanForUnregisteredSubPartitions) ? new ArrayListValuedHashMap<>() : storageFileDao.getStorageFilePathsByStorageUnitIds(storageUnitHelper.getStorageUnitIds(storageUnitEntities));
// Process all available business object data instances.
for (StorageUnitEntity storageUnitEntity : storageUnitEntities) {
// Get business object data key and S3 key prefix for this business object data.
BusinessObjectDataKey businessObjectDataKey = businessObjectDataHelper.getBusinessObjectDataKey(storageUnitEntity.getBusinessObjectData());
String s3KeyPrefix = s3KeyPrefixHelper.buildS3KeyPrefix(storageUnitEntity.getStorage(), storageUnitEntity.getBusinessObjectData().getBusinessObjectFormat(), businessObjectDataKey);
// If flag is set to suppress scan for unregistered sub-partitions, use the directory path or the S3 key prefix
// as the partition's location, otherwise, use storage files to discover all unregistered sub-partitions.
Collection<String> storageFilePaths = new ArrayList<>();
if (BooleanUtils.isTrue(generateDdlRequest.suppressScanForUnregisteredSubPartitions)) {
// Validate the directory path value if it is present.
if (storageUnitEntity.getDirectoryPath() != null) {
Assert.isTrue(storageUnitEntity.getDirectoryPath().equals(s3KeyPrefix), String.format("Storage directory path \"%s\" registered with business object data {%s} " + "in \"%s\" storage does not match the expected S3 key prefix \"%s\".", storageUnitEntity.getDirectoryPath(), businessObjectDataHelper.businessObjectDataEntityAltKeyToString(storageUnitEntity.getBusinessObjectData()), storageUnitEntity.getStorage().getName(), s3KeyPrefix));
}
// Add the S3 key prefix to the list of storage files.
// We add a trailing '/' character to the prefix, since it represents a directory.
storageFilePaths.add(StringUtils.appendIfMissing(s3KeyPrefix, "/"));
} else {
// Retrieve storage file paths registered with this business object data in the specified storage.
storageFilePaths = storageUnitIdToStorageFilePathsMap.containsKey(storageUnitEntity.getId()) ? storageUnitIdToStorageFilePathsMap.get(storageUnitEntity.getId()) : new ArrayList<>();
// Validate storage file paths registered with this business object data in the specified storage.
// The validation check below is required even if we have no storage files registered.
storageFileHelper.validateStorageFilePaths(storageFilePaths, s3KeyPrefix, storageUnitEntity.getBusinessObjectData(), storageUnitEntity.getStorage().getName());
// If there are no storage files registered for this storage unit, we should use the storage directory path value.
if (storageFilePaths.isEmpty()) {
// Validate that directory path value is present and it matches the S3 key prefix.
Assert.isTrue(storageUnitEntity.getDirectoryPath() != null && storageUnitEntity.getDirectoryPath().startsWith(s3KeyPrefix), String.format("Storage directory path \"%s\" registered with business object data {%s} " + "in \"%s\" storage does not match the expected S3 key prefix \"%s\".", storageUnitEntity.getDirectoryPath(), businessObjectDataHelper.businessObjectDataEntityAltKeyToString(storageUnitEntity.getBusinessObjectData()), storageUnitEntity.getStorage().getName(), s3KeyPrefix));
// Add storage directory path the empty storage files list.
// We add a trailing '/' character to the path, since it represents a directory.
storageFilePaths.add(storageUnitEntity.getDirectoryPath() + "/");
}
}
// Retrieve the s3 bucket name.
String s3BucketName = getS3BucketName(storageUnitEntity.getStorage(), generateDdlRequest.s3BucketNames);
// For partitioned table, add the relative partitions to the generated DDL.
if (generateDdlRequest.isPartitioned) {
// the business object data equals to the number of partition columns defined in schema for the format selected for DDL generation.
if (BooleanUtils.isTrue(generateDdlRequest.suppressScanForUnregisteredSubPartitions)) {
int businessObjectDataRegisteredPartitions = 1 + CollectionUtils.size(businessObjectDataKey.getSubPartitionValues());
Assert.isTrue(businessObjectFormat.getSchema().getPartitions().size() == businessObjectDataRegisteredPartitions, String.format("Number of primary and sub-partition values (%d) specified for the business object data is not equal to " + "the number of partition columns (%d) defined in the schema of the business object format selected for DDL generation. " + "Business object data: {%s}, business object format: {%s}", businessObjectDataRegisteredPartitions, businessObjectFormat.getSchema().getPartitions().size(), businessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey), businessObjectFormatHelper.businessObjectFormatEntityAltKeyToString(businessObjectFormatEntity)));
} else // Otherwise, since the format version selected for DDL generation might not match the relative business object format version that business
// object data is registered against, validate that the number of sub-partition values specified for the business object data is less than
// the number of partition columns defined in schema for the format selected for DDL generation.
{
Assert.isTrue(businessObjectFormat.getSchema().getPartitions().size() > CollectionUtils.size(businessObjectDataKey.getSubPartitionValues()), String.format("Number of subpartition values specified for the business object data is greater than or equal to " + "the number of partition columns defined in the schema of the business object format selected for DDL generation. " + "Business object data: {%s}, business object format: {%s}", businessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey), businessObjectFormatHelper.businessObjectFormatEntityAltKeyToString(businessObjectFormatEntity)));
}
// Get partition information. For multiple level partitioning, auto-discover subpartitions (subdirectories) not already included into the S3 key
// prefix. Each discovered partition requires a standalone "add partition" clause. Please note that due to the above validation check, there
// should be no auto discoverable sub-partition columns, when flag is set to suppress scan for unregistered sub-partitions.
List<SchemaColumn> autoDiscoverableSubPartitionColumns = businessObjectFormat.getSchema().getPartitions().subList(1 + CollectionUtils.size(businessObjectDataKey.getSubPartitionValues()), businessObjectFormat.getSchema().getPartitions().size());
for (HivePartitionDto hivePartition : getHivePartitions(businessObjectDataKey, autoDiscoverableSubPartitionColumns, s3KeyPrefix, storageFilePaths, storageUnitEntity.getBusinessObjectData(), storageUnitEntity.getStorage().getName())) {
sb.append(String.format("ALTER TABLE `%s` ADD %sPARTITION (", generateDdlRequest.tableName, ifNotExistsOption));
// Specify all partition column values.
List<String> partitionKeyValuePairs = new ArrayList<>();
for (int i = 0; i < businessObjectFormat.getSchema().getPartitions().size(); i++) {
String partitionColumnName = businessObjectFormat.getSchema().getPartitions().get(i).getName();
String partitionValue = hivePartition.getPartitionValues().get(i);
partitionKeyValuePairs.add(String.format("`%s`='%s'", partitionColumnName, partitionValue));
}
sb.append(StringUtils.join(partitionKeyValuePairs, ", "));
sb.append(String.format(") LOCATION 's3n://%s/%s%s';\n", s3BucketName, s3KeyPrefix, StringUtils.isNotBlank(hivePartition.getPath()) ? hivePartition.getPath() : ""));
}
} else // This is a non-partitioned table.
{
// Get location for this non-partitioned table.
String tableLocation = String.format("s3n://%s/%s", s3BucketName, s3KeyPrefix);
if (generateDdlRequest.customDdlEntity == null) {
// Since custom DDL was not specified and this table is not partitioned, add a LOCATION clause.
// This is the last line in the non-partitioned table DDL.
sb.append(String.format("LOCATION '%s';", tableLocation));
} else {
// Since custom DDL was used for a non-partitioned table, substitute the relative custom DDL token with the actual table location.
replacements.put(NON_PARTITIONED_TABLE_LOCATION_CUSTOM_DDL_TOKEN, tableLocation);
}
}
}
}
use of org.finra.herd.model.jpa.StorageUnitEntity in project herd by FINRAOS.
the class BusinessObjectDataInitiateRestoreHelperServiceImplTest method testGetStorageUnitStorageUnitAlreadyRestoring.
@Test
public void testGetStorageUnitStorageUnitAlreadyRestoring() {
// Create a business object data entity.
BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity();
// Create a storage unit status entity.
StorageUnitStatusEntity storageUnitStatusEntity = new StorageUnitStatusEntity();
storageUnitStatusEntity.setCode(StorageUnitStatusEntity.RESTORING);
// Create a storage entity.
StorageEntity storageEntity = new StorageEntity();
storageEntity.setName(STORAGE_NAME);
// Create a storage unit entity.
StorageUnitEntity storageUnitEntity = new StorageUnitEntity();
storageUnitEntity.setStorage(storageEntity);
storageUnitEntity.setBusinessObjectData(businessObjectDataEntity);
storageUnitEntity.setStatus(storageUnitStatusEntity);
// Mock the external calls.
when(storageUnitDao.getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity)).thenReturn(Arrays.asList(storageUnitEntity));
when(businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity)).thenReturn(BUSINESS_OBJECT_DATA_KEY_AS_STRING);
// Try to call the method under test.
try {
businessObjectDataInitiateRestoreHelperServiceImpl.getStorageUnit(businessObjectDataEntity);
} catch (IllegalArgumentException e) {
assertEquals(String.format("Business object data is already being restored in \"%s\" S3 storage. Business object data: {%s}", STORAGE_NAME, BUSINESS_OBJECT_DATA_KEY_AS_STRING), e.getMessage());
}
// Verify the external calls.
verify(storageUnitDao).getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity);
verify(businessObjectDataHelper).businessObjectDataEntityAltKeyToString(businessObjectDataEntity);
verifyNoMoreInteractionsHelper();
}
use of org.finra.herd.model.jpa.StorageUnitEntity in project herd by FINRAOS.
the class BusinessObjectDataStorageUnitServiceImplTest method testCreateBusinessObjectDataStorageUnit.
@Test
public void testCreateBusinessObjectDataStorageUnit() {
// Create a business object data key.
BusinessObjectDataKey businessObjectDataKey = new BusinessObjectDataKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, SUBPARTITION_VALUES, DATA_VERSION);
// Create a business object data storage unit key.
BusinessObjectDataStorageUnitKey businessObjectDataStorageUnitKey = new BusinessObjectDataStorageUnitKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, SUBPARTITION_VALUES, DATA_VERSION, STORAGE_NAME);
// Create a storage unit directory.
StorageDirectory storageDirectory = new StorageDirectory(STORAGE_DIRECTORY_PATH);
// Create a list of storage files.
List<StorageFile> storageFiles = Arrays.asList(new StorageFile(FILE_NAME, FILE_SIZE, ROW_COUNT), new StorageFile(FILE_NAME_2, FILE_SIZE_2, ROW_COUNT_2));
// Create a business object data storage unit request.
BusinessObjectDataStorageUnitCreateRequest request = new BusinessObjectDataStorageUnitCreateRequest(businessObjectDataStorageUnitKey, storageDirectory, storageFiles, NO_DISCOVER_STORAGE_FILES);
// Create a business object data entity.
BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity();
businessObjectDataEntity.setId(ID);
// Create a storage entity.
StorageEntity storageEntity = new StorageEntity();
storageEntity.setName(STORAGE_NAME);
// Create a list of storage file entities.
List<StorageFileEntity> storageFileEntities = Arrays.asList(new StorageFileEntity(), new StorageFileEntity());
// Create a storage unit entity.
StorageUnitEntity storageUnitEntity = new StorageUnitEntity();
storageUnitEntity.setBusinessObjectData(businessObjectDataEntity);
storageUnitEntity.setStorage(storageEntity);
storageUnitEntity.setDirectoryPath(STORAGE_DIRECTORY_PATH);
storageUnitEntity.setStorageFiles(storageFileEntities);
// Create an expected business object data storage unit response.
BusinessObjectDataStorageUnitCreateResponse expectedResponse = new BusinessObjectDataStorageUnitCreateResponse(businessObjectDataStorageUnitKey, storageDirectory, storageFiles);
// Mock the external calls.
when(storageUnitHelper.getBusinessObjectDataKey(businessObjectDataStorageUnitKey)).thenReturn(businessObjectDataKey);
when(businessObjectDataDaoHelper.getBusinessObjectDataEntity(businessObjectDataKey)).thenReturn(businessObjectDataEntity);
when(storageDaoHelper.getStorageEntity(STORAGE_NAME)).thenReturn(storageEntity);
when(businessObjectDataDaoHelper.createStorageUnitEntity(businessObjectDataEntity, storageEntity, storageDirectory, storageFiles, NO_DISCOVER_STORAGE_FILES)).thenReturn(storageUnitEntity);
when(businessObjectDataHelper.createBusinessObjectDataKeyFromEntity(businessObjectDataEntity)).thenReturn(businessObjectDataKey);
when(storageUnitHelper.createBusinessObjectDataStorageUnitKey(businessObjectDataKey, STORAGE_NAME)).thenReturn(businessObjectDataStorageUnitKey);
when(storageFileHelper.createStorageFilesFromEntities(storageFileEntities)).thenReturn(storageFiles);
// Call the method under test.
BusinessObjectDataStorageUnitCreateResponse result = businessObjectDataStorageUnitServiceImpl.createBusinessObjectDataStorageUnit(request);
// Verify the external calls.
verify(storageUnitHelper).validateBusinessObjectDataStorageUnitKey(businessObjectDataStorageUnitKey);
verify(storageFileHelper).validateCreateRequestStorageFiles(storageFiles);
verify(storageUnitHelper).getBusinessObjectDataKey(businessObjectDataStorageUnitKey);
verify(businessObjectDataDaoHelper).getBusinessObjectDataEntity(businessObjectDataKey);
verify(storageDaoHelper).getStorageEntity(STORAGE_NAME);
verify(businessObjectDataDaoHelper).createStorageUnitEntity(businessObjectDataEntity, storageEntity, storageDirectory, storageFiles, NO_DISCOVER_STORAGE_FILES);
verify(businessObjectDataHelper).createBusinessObjectDataKeyFromEntity(businessObjectDataEntity);
verify(storageUnitHelper).createBusinessObjectDataStorageUnitKey(businessObjectDataKey, STORAGE_NAME);
verify(storageFileHelper).createStorageFilesFromEntities(storageFileEntities);
verify(storageUnitDao).saveAndRefresh(storageUnitEntity);
verifyNoMoreInteractionsHelper();
// Validate the results.
assertEquals(expectedResponse, result);
}
use of org.finra.herd.model.jpa.StorageUnitEntity in project herd by FINRAOS.
the class CleanupDestroyedBusinessObjectDataServiceImplTest method testCleanupS3StorageUnitWithIllegalArgumentException.
@Test
public void testCleanupS3StorageUnitWithIllegalArgumentException() {
// Create a business object data key.
BusinessObjectDataKey businessObjectDataKey = new BusinessObjectDataKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, SUBPARTITION_VALUES, DATA_VERSION);
// Create a storage unit key.
BusinessObjectDataStorageUnitKey businessObjectDataStorageUnitKey = new BusinessObjectDataStorageUnitKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, SUBPARTITION_VALUES, DATA_VERSION, STORAGE_NAME);
// Create mocks
BusinessObjectDataEntity businessObjectDataEntity = mock(BusinessObjectDataEntity.class);
// Create storage units
StoragePlatformEntity storagePlatformEntity = new StoragePlatformEntity();
storagePlatformEntity.setName(StoragePlatformEntity.S3);
StorageEntity storageEntity = new StorageEntity();
storageEntity.setStoragePlatform(storagePlatformEntity);
StorageUnitEntity storageUnitEntity1 = new StorageUnitEntity();
storageUnitEntity1.setStorage(storageEntity);
StorageUnitEntity storageUnitEntity2 = new StorageUnitEntity();
storageUnitEntity2.setStorage(storageEntity);
Collection<StorageUnitEntity> storageUnitEntities = Lists.newArrayList();
storageUnitEntities.add(storageUnitEntity1);
storageUnitEntities.add(storageUnitEntity2);
// Mock the external calls.
when(mockBusinessObjectDataHelper.createBusinessObjectDataKeyFromStorageUnitKey(businessObjectDataStorageUnitKey)).thenReturn(businessObjectDataKey);
when(mockBusinessObjectDataDaoHelper.getBusinessObjectDataEntity(businessObjectDataKey)).thenReturn(businessObjectDataEntity);
when(businessObjectDataEntity.getStorageUnits()).thenReturn(storageUnitEntities);
when(mockBusinessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey)).thenReturn(businessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey));
// Call the method under test.
try {
cleanupDestroyedBusinessObjectDataService.cleanupS3StorageUnit(businessObjectDataStorageUnitKey);
} catch (IllegalArgumentException illegalArgumentException) {
assertThat(illegalArgumentException.getMessage(), is(equalTo("Business object data has multiple (2) S3 storage units. Business object data: {" + businessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey) + "}")));
}
// Verify the external calls.
verify(mockBusinessObjectDataHelper).createBusinessObjectDataKeyFromStorageUnitKey(businessObjectDataStorageUnitKey);
verify(mockBusinessObjectDataDaoHelper).getBusinessObjectDataEntity(businessObjectDataKey);
verify(businessObjectDataEntity).getStorageUnits();
verify(mockBusinessObjectDataHelper).businessObjectDataKeyToString(businessObjectDataKey);
verifyNoMoreInteractions(businessObjectDataEntity);
verifyNoMoreInteractionsHelper();
}
use of org.finra.herd.model.jpa.StorageUnitEntity in project herd by FINRAOS.
the class BusinessObjectDataStorageUnitStatusServiceImplTest method testUpdateBusinessObjectDataStorageUnitStatus.
@Test
public void testUpdateBusinessObjectDataStorageUnitStatus() {
// Create a business object data key.
BusinessObjectDataKey businessObjectDataKey = new BusinessObjectDataKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, SUBPARTITION_VALUES, DATA_VERSION);
// Create a business object data storage unit key.
BusinessObjectDataStorageUnitKey businessObjectDataStorageUnitKey = new BusinessObjectDataStorageUnitKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, SUBPARTITION_VALUES, DATA_VERSION, STORAGE_NAME);
// Create a storage unit entity.
StorageUnitEntity storageUnitEntity = storageUnitDaoTestHelper.createStorageUnitEntity(businessObjectDataStorageUnitKey, STORAGE_UNIT_STATUS);
// Create a storage unit status entity.
StorageUnitStatusEntity storageUnitStatusEntity = storageUnitStatusDaoTestHelper.createStorageUnitStatusEntity(STORAGE_UNIT_STATUS_2);
// Create a business object data storage unit status update request.
BusinessObjectDataStorageUnitStatusUpdateRequest request = new BusinessObjectDataStorageUnitStatusUpdateRequest(STORAGE_UNIT_STATUS_2);
// Create a business object data storage unit status update response.
BusinessObjectDataStorageUnitStatusUpdateResponse expectedResponse = new BusinessObjectDataStorageUnitStatusUpdateResponse(businessObjectDataStorageUnitKey, STORAGE_UNIT_STATUS_2, STORAGE_UNIT_STATUS);
// Mock the external calls.
when(storageUnitDaoHelper.getStorageUnitEntityByKey(businessObjectDataStorageUnitKey)).thenReturn(storageUnitEntity);
when(storageUnitStatusDaoHelper.getStorageUnitStatusEntity(STORAGE_UNIT_STATUS_2)).thenReturn(storageUnitStatusEntity);
doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) {
// Get the storage unit entity parameter.
StorageUnitEntity storageUnitEntity = (StorageUnitEntity) invocation.getArguments()[0];
StorageUnitStatusEntity storageUnitStatusEntity = (StorageUnitStatusEntity) invocation.getArguments()[1];
// Update storage unit status.
storageUnitEntity.setStatus(storageUnitStatusEntity);
return null;
}
}).when(storageUnitDaoHelper).updateStorageUnitStatus(storageUnitEntity, storageUnitStatusEntity, STORAGE_UNIT_STATUS_2);
when(businessObjectDataHelper.createBusinessObjectDataKeyFromEntity(storageUnitEntity.getBusinessObjectData())).thenReturn(businessObjectDataKey);
when(storageUnitHelper.createBusinessObjectDataStorageUnitKey(businessObjectDataKey, STORAGE_NAME)).thenReturn(businessObjectDataStorageUnitKey);
// Call the method under test.
BusinessObjectDataStorageUnitStatusUpdateResponse result = businessObjectDataStorageUnitStatusServiceImpl.updateBusinessObjectDataStorageUnitStatus(businessObjectDataStorageUnitKey, request);
// Verify the external calls.
verify(storageUnitHelper).validateBusinessObjectDataStorageUnitKey(businessObjectDataStorageUnitKey);
verify(storageUnitDaoHelper).getStorageUnitEntityByKey(businessObjectDataStorageUnitKey);
verify(storageUnitStatusDaoHelper).getStorageUnitStatusEntity(STORAGE_UNIT_STATUS_2);
verify(storageUnitDaoHelper).updateStorageUnitStatus(storageUnitEntity, storageUnitStatusEntity, STORAGE_UNIT_STATUS_2);
verify(businessObjectDataHelper).createBusinessObjectDataKeyFromEntity(storageUnitEntity.getBusinessObjectData());
verify(storageUnitHelper).createBusinessObjectDataStorageUnitKey(businessObjectDataKey, STORAGE_NAME);
verifyNoMoreInteractionsHelper();
// Validate the results.
assertEquals(expectedResponse, result);
}
Aggregations