Skip to content

Commit

Permalink
CR changes 2
Browse files Browse the repository at this point in the history
Signed-off-by: shirady <[email protected]>
  • Loading branch information
shirady committed Nov 14, 2024
1 parent 80a0e2c commit 9670d02
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ config.INLINE_MAX_SIZE = 4096;
config.OBJECT_SDK_BUCKET_CACHE_EXPIRY_MS = 60000;

// Object SDK bucket_namespace_cache allow stat of the config file
config.ALLOW_BUCKET_NS_CACHE_STAT = true;
config.NC_ENABLE_BUCKET_NS_CACHE_STAT_VALIDATION = true;

//////////////////////////////
// OPERATOR RELATED //
Expand Down
40 changes: 33 additions & 7 deletions src/sdk/bucketspace_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
}
}
}
bucket.stat = await this.stat_bucket(bucket.name.unwrap());
bucket.stat = await this.stat_bucket_config_file(bucket.name.unwrap());
return bucket;
} catch (err) {
const rpc_error = translate_error_codes(err, entity_enum.BUCKET);
Expand All @@ -165,33 +165,59 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
}
}

/**
* check_bucket_config will change the bucket_storage_path
* and - TODO: Bucket owner check
* @param {object} bucket
* @returns {Promise<boolean>}
*/
async check_bucket_config(bucket) {
const bucket_storage_path = bucket.path;
return this.check_stat_bucket_storage_path(bucket_storage_path);
//TODO: Bucket owner check
}

/**
* check_stat_bucket_storage_path will return the true
* if there is stat output on the bucket storage path
* (in case the stat throws an error it would return false)
* @param {string} bucket_storage_path
* @returns {Promise<boolean>}
*/
async check_stat_bucket_storage_path(bucket_storage_path) {
try {
await nb_native().fs.stat(this.fs_context, bucket_storage_path);
//TODO: Bucket owner check
return true;
} catch (err) {
return false;
}
}

/**
* stat_bucket will return the stat output on bucket config file
* stat_bucket_config_file will return the stat output on bucket config file
* @param {string} bucket_name
* @returns {Promise<nb.NativeFSStats>}
*/
async stat_bucket(bucket_name) {
if (config.ALLOW_BUCKET_NS_CACHE_STAT) {
async stat_bucket_config_file(bucket_name) {
if (config.NC_ENABLE_BUCKET_NS_CACHE_STAT_VALIDATION) {
try {
const bucket_config_path = this.config_fs.get_bucket_path_by_name(bucket_name);
return nb_native().fs.stat(this.fs_context, bucket_config_path);
return this.config_fs.stat_bucket_config_file(bucket_name);
} catch (err) {
dbg.log4('stat_bucket got an error', err, 'ignoring...');
}
}
}

/**
* check_same_stat will return true if both stat outputs are the same
* @param {nb.NativeFSStats} stat1
* @param {nb.NativeFSStats} stat2
* @returns {boolean}
*/
check_same_stat(stat1, stat2) {
return stat1.ino === stat2.ino && stat1.mtimeNsBigint === stat2.mtimeNsBigint;
}

////////////
// BUCKET //
////////////
Expand Down
11 changes: 11 additions & 0 deletions src/sdk/config_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,17 @@ class ConfigFS {
return new_path;
}

/**
* stat_bucket_config_file will return the stat output on bucket config file
* please notice that stat might throw an error - you should wrap it with try-catch and handle the error
* @param {string} bucket_name
* @returns {Promise<nb.NativeFSStats>}
*/
stat_bucket_config_file(bucket_name) {
const bucket_config_path = this.get_bucket_path_by_name(bucket_name);
return nb_native().fs.stat(this.fs_context, bucket_config_path);
}

/**
* is_bucket_exists returns true if bucket config path exists in config dir
* @param {string} bucket_name
Expand Down
3 changes: 2 additions & 1 deletion src/sdk/nb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,8 @@ interface BucketSpace {

read_account_by_access_key({ access_key: string }): Promise<any>;
read_bucket_sdk_info({ name: string }): Promise<any>;
stat_bucket({ bucket_name: string }): Promise<any>; // only implemented in bucketspace_fs
stat_bucket_config_file(bucket_name: string): Promise<any>; // only implemented in bucketspace_fs
check_same_stat(current_stat:nb.NativeFSStats, new_stat: nb.NativeFSStats); // only implemented in bucketspace_fs

list_buckets(params: object, object_sdk: ObjectSDK): Promise<any>;
read_bucket(params: object): Promise<any>;
Expand Down
11 changes: 4 additions & 7 deletions src/sdk/object_sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,13 @@ class ObjectSDK {
async _validate_bucket_namespace(data, params) {
const time = Date.now();
// stat check (only in bucketspace FS)
const ns_allow_stat_bucket = Boolean(this._get_bucketspace().stat_bucket);
const bs = this._get_bucketspace();
const ns_allow_stat_bucket = Boolean(bs.stat_bucket_config_file);
if (ns_allow_stat_bucket) {
try {
const current_stat = await this._get_bucketspace().stat_bucket(params.name);
const current_stat = await bs.stat_bucket_config_file(params.name);
if (current_stat) {
const same_stat = current_stat.ino === data.stat.ino && current_stat.mtimeNsBigint === data.stat.mtimeNsBigint;
const same_stat = bs.check_same_stat(current_stat, data.bucket.stat);
if (!same_stat) { // config file of bucket was changed
return false;
}
Expand Down Expand Up @@ -322,7 +323,6 @@ class ObjectSDK {
* ns: nb.Namespace;
* bucket: object;
* valid_until: number;
* stat: object;
* }}
*/
_setup_bucket_namespace(bucket) {
Expand All @@ -345,7 +345,6 @@ class ObjectSDK {
ns: namespace_cache,
bucket,
valid_until: time + config.OBJECT_SDK_BUCKET_CACHE_EXPIRY_MS,
stat: undefined
};
}
if (this._is_single_namespace(bucket.namespace)) {
Expand All @@ -360,15 +359,13 @@ class ObjectSDK {
),
bucket,
valid_until: time + config.OBJECT_SDK_BUCKET_CACHE_EXPIRY_MS,
stat: bucket.stat // will be a value only in NSFS
};
}
// MERGE NAMESPACE
return {
ns: this._setup_merge_namespace(bucket),
bucket,
valid_until: time + config.OBJECT_SDK_BUCKET_CACHE_EXPIRY_MS,
stat: undefined
};
}

Expand Down

0 comments on commit 9670d02

Please sign in to comment.