Skip to content

Commit

Permalink
fix: fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
YarikRevich committed Nov 28, 2024
1 parent ce17d97 commit 7be609a
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.objectstorage.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* Represents repository content unit.
*/
@Getter
@AllArgsConstructor(staticName = "of")
public class TemporateContentUnitDto {
/**
* Represents provider.
*/
private Integer provider;

/**
* Represents se.
*/
private Integer secret;

/**
* Represents file location.
*/
private String location;

/**
* Represents file hash.
*/
private String hash;

/**
* Represents created at timestamp.
*/
private Long createdAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public TemporateEntity findEarliest() throws RepositoryOperationFailureException
/**
* Retrieves all the persisted temporate entities with the given hash.
*
* @param hash given hash.
* @return retrieved temporate entities.
* @throws RepositoryOperationFailureException if repository operation fails.
*/
Expand Down Expand Up @@ -212,9 +213,66 @@ public List<TemporateEntity> findByHash(String hash) throws
return result;
}

/**
* Retrieves all the persisted temporate entities with the given location, provider and secret.
*
* @param location given location.
* @param provider given provider.
* @param secret given secret.
* @return retrieved temporate entity.
* @throws RepositoryOperationFailureException if repository operation fails.
*/
public TemporateEntity findEarliestByLocationProviderAndSecret(
String location, Integer provider, Integer secret) throws RepositoryOperationFailureException {
ResultSet resultSet;

try {
resultSet =
repositoryExecutor.performQueryWithResult(
String.format(
"SELECT t.id, t.hash, t.created_at FROM %s as t WHERE t.location = '%s' AND t.provider = %d AND t.secret = %d ORDER BY created_at DESC LIMIT 1",
properties.getDatabaseTemporateTableName(),
location,
provider,
secret));

} catch (QueryExecutionFailureException | QueryEmptyResultException e) {
throw new RepositoryOperationFailureException(e.getMessage());
}

Integer id;
String hash;
Long createdAt;

try {
id = resultSet.getInt("id");
hash = resultSet.getString("hash");
createdAt = resultSet.getLong("created_at");

} catch (SQLException e1) {
try {
resultSet.close();
} catch (SQLException e2) {
throw new RepositoryOperationFailureException(e2.getMessage());
}

throw new RepositoryOperationFailureException(e1.getMessage());
}

try {
resultSet.close();
} catch (SQLException e) {
throw new RepositoryOperationFailureException(e.getMessage());
}

return TemporateEntity.of(id, provider, secret, location, hash, createdAt);
}

/**
* Retrieves all the persisted temporate entities with the given provider and secret.
*
* @param provider given provider.
* @param secret given secret.
* @return retrieved temporate entities.
* @throws RepositoryOperationFailureException if repository operation fails.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.objectstorage.dto.ContentCompoundUnitDto;
import com.objectstorage.dto.RepositoryContentUnitDto;
import com.objectstorage.dto.EarliestTemporateContentDto;
import com.objectstorage.dto.TemporateContentUnitDto;
import com.objectstorage.entity.repository.ContentEntity;
import com.objectstorage.entity.repository.ProviderEntity;
import com.objectstorage.entity.repository.SecretEntity;
Expand Down Expand Up @@ -82,7 +83,8 @@ public List<ContentRetrievalProviderUnit> retrieveFilteredTemporateContent(
}

return temporateContent.stream().map(
element -> ContentRetrievalProviderUnit.of(element.getLocation())).toList();
element -> ContentRetrievalProviderUnit.of(
element.getLocation(), element.getCreatedAt())).toList();
}

/**
Expand Down Expand Up @@ -143,7 +145,7 @@ public EarliestTemporateContentDto retrieveEarliestTemporateContent() throws Tem
SecretEntity secret;

try {
secret = secretRepository.findById(temporate.getId());
secret = secretRepository.findById(temporate.getSecret());
} catch (RepositoryOperationFailureException e) {
throw new TemporateContentRetrievalFailureException(e.getMessage());
}
Expand Down Expand Up @@ -177,6 +179,64 @@ public EarliestTemporateContentDto retrieveEarliestTemporateContent() throws Tem
temporateEntity.getCreatedAt());
}

/**
* Retrieves temporate content from the temporate repository with the given location, provider and secret.
*
* @param location given temporate content location.
* @param validationSecretsUnit given validation secrets unit.
* @return retrieved temporate content.
*/
public TemporateContentUnitDto retrieveTemporateContentByLocationProviderAndSecret(String location, ValidationSecretsUnit validationSecretsUnit)
throws TemporateContentRemovalFailureException {
ProviderEntity provider;

try {
provider = providerRepository.findByName(validationSecretsUnit.getProvider().toString());
} catch (RepositoryOperationFailureException e) {
throw new TemporateContentRemovalFailureException(e.getMessage());
}

String signature = repositoryConfigurationHelper.getExternalCredentials(
validationSecretsUnit.getProvider(),
validationSecretsUnit.getCredentials().getExternal());

try {
if (!secretRepository.isPresentBySessionAndCredentials(
validationSecretsUnit.getCredentials().getInternal().getId(), signature)) {
throw new TemporateContentRemovalFailureException(
new RepositoryContentApplicationNotExistsException().getMessage());
}
} catch (RepositoryOperationFailureException e) {
throw new TemporateContentRemovalFailureException(e.getMessage());
}

SecretEntity secret;

try {
secret = secretRepository.findBySessionAndCredentials(
validationSecretsUnit.getCredentials().getInternal().getId(),
signature);
} catch (RepositoryOperationFailureException e) {
throw new TemporateContentRemovalFailureException(e.getMessage());
}

TemporateEntity temporate;

try {
temporate = temporateRepository
.findEarliestByLocationProviderAndSecret(location, provider.getId(), secret.getId());
} catch (RepositoryOperationFailureException e) {
throw new TemporateContentRemovalFailureException(e.getMessage());
}

return TemporateContentUnitDto.of(
temporate.getProvider(),
temporate.getSecret(),
temporate.getLocation(),
temporate.getHash(),
temporate.getCreatedAt());
}

/**
* Retrieves content application from the content repository.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public void process() throws BackupPeriodRetrievalFailureException {
scheduledOperationExecutorService.scheduleWithFixedDelay(() -> {
StateService.getBackupProcessorGuard().lock();

// vendorFacade.listAllObjectsFromBucket(
//
// );

// vendorFacade.listAllObjectsFromBucket().get(0).

// workspaceFacade.add
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.objectstorage.service.config.ConfigService;
import com.objectstorage.service.state.StateService;
import com.objectstorage.service.vendor.VendorFacade;
import com.objectstorage.service.vendor.common.VendorConfigurationHelper;
import com.objectstorage.service.workspace.facade.WorkspaceFacade;
import io.quarkus.runtime.Startup;
import jakarta.annotation.PostConstruct;
Expand Down Expand Up @@ -156,7 +157,8 @@ public void process() throws TemporateStoragePeriodRetrievalFailureException {
vendorFacade.uploadObjectToBucket(
contentCompoundUnit.getProvider(),
contentCompoundUnit.getCredentialsFieldsFull().getExternal(),
contentCompoundUnit.getRepositoryContentUnitDto().getRoot(),
VendorConfigurationHelper.createBucketName(
contentCompoundUnit.getRepositoryContentUnitDto().getRoot()),
temporateContentDto.getLocation(),
new ByteArrayInputStream(content));
} catch (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.objectstorage.service.processor;

import com.objectstorage.dto.RepositoryContentUnitDto;
import com.objectstorage.dto.TemporateContentUnitDto;
import com.objectstorage.exception.*;
import com.objectstorage.model.*;
import com.objectstorage.repository.executor.RepositoryExecutor;
Expand Down Expand Up @@ -296,9 +297,19 @@ public byte[] downloadObject(

String workspaceUnitKey = workspaceFacade.createWorkspaceUnitKey(validationSecretsApplication);

TemporateContentUnitDto temporateContentUnit;

try {
temporateContentUnit =
repositoryFacade.retrieveTemporateContentByLocationProviderAndSecret(
location, validationSecretsUnit);
} catch (TemporateContentRemovalFailureException e) {
throw new ProcessorContentDownloadFailureException(e.getMessage());
}

try {
if (workspaceFacade.isObjectFilePresent(workspaceUnitKey, location)) {
return workspaceFacade.getObjectFile(workspaceUnitKey, location);
if (workspaceFacade.isObjectFilePresent(workspaceUnitKey, temporateContentUnit.getHash())) {
return workspaceFacade.getObjectFile(workspaceUnitKey, temporateContentUnit.getHash());
}
} catch (FileExistenceCheckFailureException | FileUnitRetrievalFailureException e) {
throw new ProcessorContentDownloadFailureException(e.getMessage());
Expand Down Expand Up @@ -453,20 +464,36 @@ public void removeObject(

throw new ProcessorContentRemovalFailureException(e1.getMessage());
}
}

try {
if (workspaceFacade.isObjectFilePresent(workspaceUnitKey, location)) {
workspaceFacade.removeObjectFile(workspaceUnitKey, location);
}
} catch (FileExistenceCheckFailureException | FileRemovalFailureException e1) {
TemporateContentUnitDto temporateContentUnit;

try {
repositoryExecutor.rollbackTransaction();
} catch (TransactionRollbackFailureException e2) {
throw new ProcessorContentRemovalFailureException(e2.getMessage());
temporateContentUnit =
repositoryFacade.retrieveTemporateContentByLocationProviderAndSecret(
location, validationSecretsUnit);
} catch (TemporateContentRemovalFailureException e1) {
try {
repositoryExecutor.rollbackTransaction();
} catch (TransactionRollbackFailureException e2) {
throw new ProcessorContentRemovalFailureException(e2.getMessage());
}

throw new ProcessorContentRemovalFailureException(e1.getMessage());
}

throw new ProcessorContentRemovalFailureException(e1.getMessage());
try {
if (workspaceFacade.isObjectFilePresent(workspaceUnitKey, temporateContentUnit.getHash())) {
workspaceFacade.removeObjectFile(workspaceUnitKey, temporateContentUnit.getHash());
}
} catch (FileExistenceCheckFailureException | FileRemovalFailureException e1) {
try {
repositoryExecutor.rollbackTransaction();
} catch (TransactionRollbackFailureException e2) {
throw new ProcessorContentRemovalFailureException(e2.getMessage());
}

throw new ProcessorContentRemovalFailureException(e1.getMessage());
}
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ private void configure() {
}
}, 0, properties.getDiagnosticsScrapeDelay(), TimeUnit.MILLISECONDS);

System.out.println(configService.getConfig().getTemporateStorage().getFrequency());

telemetryBinding.getTemporateStorageFilesAmount().set(10);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ public void uploadObjectToS3Bucket(
put("objectstorage", "true");
}
});
try {
metadata.setContentLength(inputStream.available());
} catch (IOException e) {
throw new VendorOperationFailureException(e.getMessage());
}

PutObjectRequest request = new PutObjectRequest(bucketName, fileName, inputStream, metadata);

Expand Down
4 changes: 4 additions & 0 deletions api-server/src/main/openapi/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,13 @@ components:
ContentRetrievalProviderUnit:
required:
- location
- created_at
properties:
location:
type: string
created_at:
type: integer
format: int64
ContentApplication:
required:
- root
Expand Down

0 comments on commit 7be609a

Please sign in to comment.