Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HMS-5039: eliminates re-uploads of finished uploads #392

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ it('Render base upload modal', async () => {

jest
.spyOn(React, 'useState')
.mockImplementationOnce(() => realUseState([{ sha256: 'string', uuid: 'string' }] as unknown))
.mockImplementationOnce(() => realUseState([{ sha256: 'string', uuid: 'string', href: '' }] as unknown))
.mockImplementationOnce(() => realUseState(true as unknown));

const { queryByText } = render(<UploadContent />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const useStyles = createUseStyles({
const UploadContent = () => {
const classes = useStyles();
const { repoUUID: uuid } = useParams();
const [fileUUIDs, setFileUUIDs] = useState<{ sha256: string; uuid: string }[]>([]);
const [fileUUIDs, setFileUUIDs] = useState<{ sha256: string; uuid: string; href: string }[]>([]);
const [confirmModal, setConfirmModal] = useState(false);

const rootPath = useRootPath();
Expand All @@ -37,7 +37,18 @@ const UploadContent = () => {

const { mutateAsync: uploadItems, isLoading } = useAddUploadsQuery({
repoUUID: uuid!,
uploads: fileUUIDs,
uploads: fileUUIDs
.filter((f) => f.href.length == 0)
.map((f) => ({
sha256: f.sha256,
uuid: f.uuid,
})),
artifacts: fileUUIDs
.filter((f) => f.href.length > 0)
.map((f) => ({
sha256: f.sha256,
href: f.href,
})),
});

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import UploadStatusItem from './UploadStatusItem';
import StatusIcon from 'Pages/Repositories/AdminTaskTable/components/StatusIcon';
import { getFileChecksumSHA256, type Chunk, type FileInfo } from './helpers';
import { createUseStyles } from 'react-jss';
import { createUpload, uploadChunk } from 'services/Content/ContentApi';
import { createUpload, searchUploads, uploadChunk } from 'services/Content/ContentApi';
import Loader from 'components/Loader';
import { UploadIcon } from '@patternfly/react-icons';

Expand All @@ -27,7 +27,9 @@ export const BATCH_SIZE = 5;
export const MAX_RETRY_COUNT = 3;

interface Props {
setFileUUIDs: React.Dispatch<React.SetStateAction<{ sha256: string; uuid: string }[]>>;
setFileUUIDs: React.Dispatch<
React.SetStateAction<{ sha256: string; uuid: string; href: string }[]>
>;
isLoading: boolean;
}

Expand All @@ -53,9 +55,10 @@ export default function FileUploader({ setFileUUIDs, isLoading }: Props) {

useEffect(() => {
if (completedCount === fileCount) {
const items = Object.values(currentFiles).map(({ uuid, checksum }) => ({
const items = Object.values(currentFiles).map(({ uuid, checksum, artifactHref }) => ({
sha256: checksum,
uuid,
href: artifactHref ?? '',
}));

setFileUUIDs(items);
Expand Down Expand Up @@ -181,7 +184,7 @@ export default function FileUploader({ setFileUUIDs, isLoading }: Props) {
chunks.push({ start, end, queued: false, completed: false, retryCount: 0 });
}

let checksum: string;
let checksum: string = '';
let error: string | undefined = undefined;

try {
Expand All @@ -192,18 +195,38 @@ export default function FileUploader({ setFileUUIDs, isLoading }: Props) {

let uuid: string = '';
let created: string = '';
let artifactHref: string | undefined = undefined;
if (!error) {
try {
const res = await createUpload(file.size);
uuid = res.upload_uuid;
created = res.created;
} catch (err) {
error = 'Failed to create upload file: ' + (err as Error).message;
const res = await searchUploads({ sha256: [checksum] });
const href = res.found.get(checksum);
if (href !== undefined) {
artifactHref = href;
for (const ch of chunks) {
ch.completed = true;
}
} else {
try {
const res = await createUpload(file.size);
uuid = res.upload_uuid;
created = res.created;
} catch (err) {
error = 'Failed to create upload file: ' + (err as Error).message;
}
}
}

setCurrentFiles((prev) => {
prev[file.name] = { uuid, created, chunks, file, checksum, error, failed: !!error };
prev[file.name] = {
uuid,
created,
chunks: chunks,
file,
checksum,
artifactHref,
completed: !!artifactHref,
error,
failed: !!error,
};
return { ...prev };
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type FileInfo = {
created: string;
chunks: Chunk[];
checksum: string;
artifactHref?: string;
error?: string;
completed?: boolean;
failed?: boolean;
Expand Down
26 changes: 25 additions & 1 deletion src/services/Content/ContentApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ export interface UploadChunkRequest {

export interface AddUploadRequest {
uploads: { sha256: string; uuid: string }[];
artifacts: { sha256: string; href: string }[];
repoUUID: string;
}

Expand All @@ -274,6 +275,15 @@ export interface AddUploadResponse {
object_uuid: string;
}

export interface SearchUploadsRequest {
sha256: string[];
}

export interface SearchUploadsResponse {
found: Map<string, string>;
missing: string[];
}

export const getPopularRepositories: (
page: number,
limit: number,
Expand Down Expand Up @@ -561,11 +571,25 @@ export const uploadChunk: (chunkRequest: UploadChunkRequest) => Promise<UploadRe

export const addUploads: (chunkRequest: AddUploadRequest) => Promise<AddUploadResponse> = async ({
uploads,
artifacts,
repoUUID,
}) => {
const { data } = await axios.post(
`/api/content-sources/v1.0/repositories/${repoUUID}/add_uploads/`,
{ uploads },
{
uploads: uploads,
artifacts: artifacts,
},
);
return data;
};

export const searchUploads: (
searchRequest: SearchUploadsRequest,
) => Promise<SearchUploadsResponse> = async (hashes) => {
const { data } = await axios.post('/api/content-sources/v1/repositories/uploads/search', {
sha256: hashes.sha256,
});
data.found = new Map<string, string>(Object.entries(data.found));
return data;
};
Loading