Skip to content

Commit

Permalink
#4682 PlanB State Store
Browse files Browse the repository at this point in the history
  • Loading branch information
stroomdev66 committed Jan 22, 2025
1 parent 400095c commit 56d0ca9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void storePart(final FileDescriptor fileDescriptor,
}
}
} catch (final Exception e) {
LOGGER.error(e::getMessage, e);
throw new RuntimeException(e.getMessage(), e);
}
}
Expand All @@ -109,6 +110,7 @@ public void storePart(final FileDescriptor fileDescriptor,
}
}
} catch (final IOException e) {
LOGGER.error(e::getMessage, e);
throw new UncheckedIOException(e);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import stroom.event.logging.rs.api.AutoLogged.OperationType;
import stroom.util.logging.LambdaLogger;
import stroom.util.logging.LambdaLoggerFactory;
import stroom.util.logging.LogUtil;
import stroom.util.shared.PermissionException;

import jakarta.inject.Inject;
Expand All @@ -31,6 +32,7 @@
import jakarta.ws.rs.core.StreamingOutput;

import java.io.InputStream;
import java.util.function.Supplier;

@AutoLogged(OperationType.UNLOGGED)
public class FileTransferResourceImpl implements FileTransferResource {
Expand All @@ -47,27 +49,34 @@ public FileTransferResourceImpl(final Provider<FileTransferService> fileTransfer
@AutoLogged(OperationType.UNLOGGED)
@Override
public Response fetchSnapshot(final SnapshotRequest request) {
LOGGER.debug(() -> "Snapshot request: " + request);
try {
// Check the status before we start streaming snapshot data as it is hard to capture meaningful errors mid
// stream.
fileTransferServiceProvider.get().checkSnapshotStatus(request);

// Stream the snapshhot content to the client as ZIP data
final StreamingOutput streamingOutput = output -> {
fileTransferServiceProvider.get().fetchSnapshot(request, output);
try {
fileTransferServiceProvider.get().fetchSnapshot(request, output);
} catch (final Exception e) {
LOGGER.error(e::getMessage, e);
throw e;
}
};

LOGGER.debug(() -> "Sending snapshot: " + request);
return Response
.ok(streamingOutput, MediaType.APPLICATION_OCTET_STREAM)
.build();
} catch (final NotModifiedException e) {
LOGGER.debug(e::getMessage, e);
LOGGER.debug(() -> "Snapshot not modified: " + request + " " + e.getMessage(), e);
throw new WebApplicationException(e.getMessage(), Status.NOT_MODIFIED);
} catch (final PermissionException e) {
LOGGER.debug(e::getMessage, e);
LOGGER.error(() -> "Snapshot permission exception : " + request + " " + e.getMessage(), e);
throw new WebApplicationException(e.getMessage(), Status.UNAUTHORIZED);
} catch (final Exception e) {
LOGGER.debug(e::getMessage, e);
LOGGER.debug(() -> "Snapshot not found: " + request + " " + e.getMessage(), e);
throw new WebApplicationException(e.getMessage(), Status.NOT_FOUND);
}
}
Expand All @@ -79,18 +88,27 @@ public Response sendPart(final long createTime,
final String fileHash,
final String fileName,
final InputStream inputStream) {
final Supplier<String> messageDetail = () -> LogUtil.message(
"createTime={}, metaId={}, fileHash={}, fileName={}",
createTime,
metaId,
fileHash,
fileName);

try {
LOGGER.debug(() -> "Receiving part: " + messageDetail.get());
fileTransferServiceProvider.get().receivePart(createTime, metaId, fileHash, fileName, inputStream);
LOGGER.debug(() -> "Successfully received part: " + messageDetail.get());
return Response
.ok()
.build();
} catch (final PermissionException e) {
LOGGER.debug(e::getMessage, e);
LOGGER.error(LogUtil.message("Permission exception receiving part: " + messageDetail.get()), e);
return Response
.status(Status.UNAUTHORIZED.getStatusCode(), e.getMessage())
.build();
} catch (final Exception e) {
LOGGER.debug(e::getMessage, e);
LOGGER.error(LogUtil.message("Exception receiving part: " + messageDetail.get()), e);
return Response
.status(Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getMessage())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.util.Objects;

@JsonPropertyOrder(alphabetic = true)
@JsonInclude(Include.NON_NULL)
public class SnapshotRequest {
Expand All @@ -22,7 +24,7 @@ public class SnapshotRequest {
@JsonCreator
public SnapshotRequest(@JsonProperty("planBDocRef") final DocRef planBDocRef,
@JsonProperty("effectiveTime") final long effectiveTime,
@JsonProperty("currentSnapshotTime")final Long currentSnapshotTime) {
@JsonProperty("currentSnapshotTime") final Long currentSnapshotTime) {
this.planBDocRef = planBDocRef;
this.effectiveTime = effectiveTime;
this.currentSnapshotTime = currentSnapshotTime;
Expand All @@ -39,4 +41,32 @@ public long getEffectiveTime() {
public Long getCurrentSnapshotTime() {
return currentSnapshotTime;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final SnapshotRequest request = (SnapshotRequest) o;
return effectiveTime == request.effectiveTime &&
Objects.equals(planBDocRef, request.planBDocRef) &&
Objects.equals(currentSnapshotTime, request.currentSnapshotTime);
}

@Override
public int hashCode() {
return Objects.hash(planBDocRef, effectiveTime, currentSnapshotTime);
}

@Override
public String toString() {
return "SnapshotRequest{" +
"planBDocRef=" + planBDocRef +
", effectiveTime=" + effectiveTime +
", currentSnapshotTime=" + currentSnapshotTime +
'}';
}
}

0 comments on commit 56d0ca9

Please sign in to comment.