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

fix: Added limit to array cache #2979

Merged
Merged
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
40 changes: 40 additions & 0 deletions Sources/IO/Core/HttpDataSetReader/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,46 @@ export interface vtkHttpDataSetReader extends vtkHttpDataSetReaderBase {
*/
getUrl(): string;

/**
* Gets an array of all cached array ids.
*/
getCachedArrayIds(): string[];


/**
* Gets the maximum size cached arrays are allowed to occupy.
* Size is given in MiB.
* If cache size is exceeded, the arrays that where not accessed
* the longest are removed.
*
* Special settings:
* -1 -> Cache unlimited
* 0 -> Cache disabled
* null -> Cache disabled
* undefined -> Cache disabled
*/
getMaxCacheSize(): number | null | undefined;

/**
* Sets the maximum size cached arrays are allowed to occupy.
* Size is given in MiB.
* If cache size is exceeded, the arrays that where not accessed
* the longest are removed.
* If set to "undefined" the cache is unlimited.
*
* Special settings:
* -1 -> Cache unlimited
* 0 -> Cache disabled
* null -> Cache disabled
* undefined -> Cache disabled
*/
setMaxCacheSize(value: number | null | undefined): void;

/**
* Clears all cached entries.
*/
clearCache(): void;

/**
*
* @param {Boolean} busy
Expand Down
82 changes: 75 additions & 7 deletions Sources/IO/Core/HttpDataSetReader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const ARRAY_BUILDERS = {
// Global methods
// ----------------------------------------------------------------------------

const cachedArrays = {};
const cachedArraysAndPromises = {};
const cachedArraysMetaData = {};
const MiB = 1024 * 1024;

const GEOMETRY_ARRAYS = {
vtkPolyData(dataset) {
Expand Down Expand Up @@ -145,26 +147,75 @@ function vtkHttpDataSetReader(publicAPI, model) {
// Internal method to fetch Array
function fetchArray(array, options = {}) {
const arrayId = `${array.ref.id}|${array.vtkClass}`;
if (!cachedArrays[arrayId]) {
if (!cachedArraysAndPromises[arrayId]) {
// Cache the promise while fetching
cachedArrays[arrayId] = model.dataAccessHelper
cachedArraysAndPromises[arrayId] = model.dataAccessHelper
.fetchArray(publicAPI, model.baseURL, array, options)
.then((newArray) => {
// Remove promise and return here if caching is disabled
if (!model.maxCacheSize) {
delete cachedArraysAndPromises[arrayId];
return newArray;
}

// Replace the promise with the array in cache once downloaded
cachedArrays[arrayId] = newArray;
cachedArraysAndPromises[arrayId] = newArray;
cachedArraysMetaData[arrayId] = { lastAccess: new Date() };

// If maxCacheSize is set to -1 the cache is unlimited
if (model.maxCacheSize < 0) {
return newArray;
}

// sort cache according to access times (potentially needed for creating space)
const cachedArrays = {};
Object.keys(cachedArraysMetaData).forEach((arrId) => {
cachedArrays[arrId] = {
array: cachedArraysAndPromises[arrId],
lastAccess: cachedArraysMetaData[arrId].lastAccess,
};
});
const sortedArrayCache = Object.entries(cachedArrays).sort((a, b) =>
Math.sign(b[1].lastAccess - a[1].lastAccess)
);

// Check cache size
const cacheSizeLimit = model.maxCacheSize * MiB;
let cacheSize = Object.values(cachedArrays).reduce(
(accSize, entry) => accSize + entry.array.values.byteLength,
0
);
dominik-werner-casra marked this conversation as resolved.
Show resolved Hide resolved

// Delete cache entries until size is below the limit
while (cacheSize > cacheSizeLimit && sortedArrayCache.length > 0) {
const [oldId, entry] = sortedArrayCache.pop();
delete cachedArraysAndPromises[oldId];
delete cachedArraysMetaData[oldId];
cacheSize -= entry.array.values.byteLength;
}

// Edge case: If the new entry is bigger than the cache limit
if (!cachedArraysMetaData[arrayId]) {
macro.vtkWarningMacro('Cache size is too small for the dataset');
}

return newArray;
});
} else {
// cacheArrays[arrayId] can be a promise or value
Promise.resolve(cachedArrays[arrayId]).then((cachedArray) => {
Promise.resolve(cachedArraysAndPromises[arrayId]).then((cachedArray) => {
if (array !== cachedArray) {
// Update last access for cache retention rules
cachedArraysMetaData[arrayId].lastAccess = new Date();

// Assign cached array as result
Object.assign(array, cachedArray);
delete array.ref;
}
});
}

return Promise.resolve(cachedArrays[arrayId]);
return Promise.resolve(cachedArraysAndPromises[arrayId]);
}

// Fetch dataset (metadata)
Expand Down Expand Up @@ -321,6 +372,16 @@ function vtkHttpDataSetReader(publicAPI, model) {
}
};

// return id's of cached arrays
publicAPI.getCachedArrayIds = () => Object.keys(cachedArraysMetaData);

// clear global array cache
publicAPI.clearCache = () =>
Object.keys(cachedArraysMetaData).forEach((k) => {
delete cachedArraysAndPromises[k];
delete cachedArraysMetaData[k];
});

// return Busy state
publicAPI.isBusy = () => !!model.requestCount;
}
Expand All @@ -336,6 +397,8 @@ const DEFAULT_VALUES = {
url: null,
baseURL: null,
requestCount: 0,
arrayCachingEnabled: true,
maxCacheSize: 2048,
// dataAccessHelper: null,
};

Expand All @@ -352,8 +415,13 @@ export function extend(publicAPI, model, initialValues = {}) {
'url',
'baseURL',
'dataAccessHelper',
'maxCacheSize',
]);
macro.set(publicAPI, model, [
'dataAccessHelper',
'progressCallback',
'maxCacheSize',
]);
macro.set(publicAPI, model, ['dataAccessHelper', 'progressCallback']);
macro.getArray(publicAPI, model, ['arrays']);
macro.algo(publicAPI, model, 0, 1);
macro.event(publicAPI, model, 'busy');
Expand Down
24 changes: 24 additions & 0 deletions Sources/IO/Core/HttpDataSetReader/test/MockDataAccessHelper.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Required for testing
*/

export interface MockDataAccessHelperCallTrackerEntry {
promise: Promise<object[]>;
called: Date;
}

export interface MockDataAccessHelperCallTracker {
fetchJSON: MockDataAccessHelperCallTrackerEntry[];
fetchArray: MockDataAccessHelperCallTrackerEntry[];
}

export interface MockDataAccessHelper {
fetchArray(instance: any, baseURL: string, array: object[], options?: object): Promise<object[]>;
fetchJSON(instance: any, url: string, options?: object): Promise<object>;
fetchText(instance: any, url: string, options?: object): Promise<string>;
fetchBinary(instance: any, url: string, options?: object): Promise<ArrayBuffer>;
fetchImage(instance: any, url: string, options?: object): Promise<HTMLImageElement>;
getCallTracker(): MockDataAccessHelperCallTracker;
}

export default MockDataAccessHelper;
157 changes: 157 additions & 0 deletions Sources/IO/Core/HttpDataSetReader/test/MockDataAccessHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { registerType } from 'vtk.js/Sources/IO/Core/DataAccessHelper';
import macro from 'vtk.js/Sources/macros';

const MockBaseURL = 'http://mockData';
const MiB = 1024 * 1024;

function createMockIndexJSON(fileId, byteLength) {
return {
origin: [0, 0, 0],
cellData: { arrays: [], vtkClass: 'vtkDataSetAttributes' },
FieldData: { arrays: [], vtkClass: 'vtkDataSetAttributes' },
vtkClass: 'vtkImageData',
pointData: {
arrays: [
{
data: {
numberOfComponents: 1,
name: 'ImageFile',
vtkClass: 'vtkDataArray',
dataType: 'Uint8Array',
ranges: [
{
max: 15583,
component: null,
min: 0,
},
],
ref: {
registration: 'setScalars',
encode: 'LittleEndian',
basepath: 'data',
id: fileId,
},
size: byteLength,
},
},
],
vtkClass: 'vtkDataSetAttributes',
},
};
}

const MockData = {
'test01/index.json': () => createMockIndexJSON('test01', 10 * MiB),
'test01/data/test01.gz': () => new Uint8Array(10 * MiB),
'test02/index.json': () => createMockIndexJSON('test02', 20 * MiB),
'test02/data/test02.gz': () => new Uint8Array(20 * MiB),
'test03/index.json': () => createMockIndexJSON('test03', 15 * MiB),
'test03/data/test03.gz': () => new Uint8Array(15 * MiB),
'test04/index.json': () => createMockIndexJSON('test04', 40 * MiB),
'test04/data/test04.gz': () => new Uint8Array(40 * MiB),
};

// ----------------------------------------------------------------------------

const CallTrackers = [];

function getCallTracker() {
const tracker = {
fetchJSON: [],
fetchArray: [],
};
CallTrackers.push(tracker);
return tracker;
}

function fetchText(instance, url, options = {}) {
return new Promise((_, reject) => {
reject(new Error('Method "fetchText()" not implemented'));
});
}

function fetchJSON(instance, url, options = {}) {
const promise = new Promise((resolve, reject) => {
if (!url.startsWith(MockBaseURL) || !url.toLowerCase().endsWith('.json')) {
reject(new Error(`No such JSON ${url}`));
return;
}
const dataId = url.split('/').slice(-2)[0];
const filename = `${dataId}/index.json`;

if (!MockData[filename]) {
reject(new Error(`No such JSON ${url}`));
return;
}
resolve(MockData[filename]());
});

CallTrackers.forEach((t) => {
t.fetchJSON.push({
promise,
called: new Date(),
});
});

return promise;
}

function fetchArray(instance, baseURL, array, options = {}) {
const url = `${baseURL}/${array.ref.basepath}/${array.ref.id}.gz`;
const promise = new Promise((resolve, reject) => {
if (!baseURL.startsWith(MockBaseURL)) {
reject(new Error(`No such array ${url}`));
return;
}

const dataId = url.split('/').slice(-3)[0];
const filename = `${dataId}/${array.ref.basepath}/${array.ref.id}.gz`;

if (!MockData[filename]) {
reject(new Error(`No such array ${url}`));
return;
}

array.buffer = MockData[filename]();
array.values = macro.newTypedArray(array.dataType, array.buffer);
delete array.ref;

if (instance?.invokeBusy) {
instance.invokeBusy(false);
}
if (instance?.modified) {
instance.modified();
}
resolve(array);
});

CallTrackers.forEach((t) => {
t.fetchArray.push({
promise,
called: new Date(),
});
});

return promise;
}

function fetchImage(instance, url, options = {}) {
return new Promise((_, reject) => {
reject(new Error('Method "fetchImage()" not implemented'));
});
}

// ----------------------------------------------------------------------------

const MockDataAccessHelper = {
fetchJSON,
fetchText,
fetchArray,
fetchImage,
getCallTracker,
};

registerType('mock', (options) => MockDataAccessHelper);

// Export fetch methods
export default MockDataAccessHelper;
Loading
Loading