Skip to content

Commit

Permalink
Fail if locked
Browse files Browse the repository at this point in the history
Signed-off-by: Romy <[email protected]>
  • Loading branch information
romayalon committed Mar 9, 2025
1 parent e524d4a commit 30215b2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/manage_nsfs/manage_nsfs_cli_errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,15 @@ ManageCLIError.NooBaaServiceIsNotActive = Object.freeze({
http_code: 400,
});

// TEMPORARY -
// currently, only one lifecyle worker process can run at a time
// in the fututre, we will allow multiple workers to run while they are not working on the same buckets
ManageCLIError.LifecyleWorkerAlreadyRunning = Object.freeze({
code: 'LifecyleWorkerAlreadyRunning',
message: 'Lifecycle worker can not run another lifecyle worker process is already running.',
http_code: 400,
});

///////////////////////////////
// ERRORS MAPPING //
///////////////////////////////
Expand All @@ -556,7 +565,8 @@ ManageCLIError.RPC_ERROR_TO_MANAGE = Object.freeze({
NO_SUCH_USER: ManageCLIError.InvalidAccountDistinguishedName,
INVALID_MASTER_KEY: ManageCLIError.InvalidMasterKey,
INVALID_BUCKET_NAME: ManageCLIError.InvalidBucketName,
CONFIG_DIR_VERSION_MISMATCH: ManageCLIError.ConfigDirUpdateBlocked
CONFIG_DIR_VERSION_MISMATCH: ManageCLIError.ConfigDirUpdateBlocked,
LOCKED: ManageCLIError.LifecyleWorkerAlreadyRunning
});

const NSFS_CLI_ERROR_EVENT_MAP = {
Expand Down
2 changes: 1 addition & 1 deletion src/manage_nsfs/nc_lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function run_lifecycle_under_lock(config_fs) {
dbg.log0('run_lifecycle_under_lock acquired lock - start lifecycle');
await run_lifecycle(config_fs);
dbg.log0('run_lifecycle_under_lock done lifecycle - released lock');
});
}, true);
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/native/fs/fs_napi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1823,9 +1823,11 @@ struct FileFlock : public FSWrapWorker<FileWrap>
struct FileFcntlLock : public FSWrapWorker<FileWrap>
{
struct flock fl;
int op;
FileFcntlLock(const Napi::CallbackInfo& info)
: FSWrapWorker<FileWrap>(info)
, fl()
, op(F_OFD_SETLKW)
{
// lock entire file
fl.l_whence = SEEK_SET;
Expand All @@ -1846,6 +1848,16 @@ struct FileFcntlLock : public FSWrapWorker<FileWrap>
SetError("invalid lock type");
}
}
if (info.Length() > 2 && !info[2].IsUndefined()) {
auto op_string = info[2].As<Napi::String>().Utf8Value();
if (op_string == "THROW") {
op = F_OFD_SETLK;
} else if (op_string == "WAIT") {
op = F_OFD_SETLKW;
} else {
SetError("invalid lock operation");
}
}

Begin(XSTR() << "FileFcntlLock" << DVAL(_wrap->_path));
}
Expand All @@ -1855,7 +1867,7 @@ struct FileFcntlLock : public FSWrapWorker<FileWrap>
CHECK_WRAP_FD(fd);
// This uses F_OFD_SETLKW instead for discussion related to this choice
// refer: https://github.com/noobaa/noobaa-core/pull/8174
SYSCALL_OR_RETURN(fcntl(fd, F_OFD_SETLKW, &fl));
SYSCALL_OR_RETURN(fcntl(fd, op, &fl));
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/sdk/nb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ interface NativeFile {
fsync(fs_context: NativeFSContext): Promise<void>;
fd: number;
flock(fs_context: NativeFSContext, operation: "EXCLUSIVE" | "SHARED" | "UNLOCK"): Promise<void>;
fcntllock(fs_context: NativeFSContext, operation: "EXCLUSIVE" | "SHARED" | "UNLOCK"): Promise<void>;
fcntllock(fs_context: NativeFSContext, type: "EXCLUSIVE" | "SHARED" | "UNLOCK", op?: "THROW" | "WAIT"): Promise<void>;
}

interface NativeDir {
Expand Down
10 changes: 8 additions & 2 deletions src/util/native_fs_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,13 +718,19 @@ function translate_error_codes(err, entity) {
* @param {nb.NativeFSContext} fs_context
* @param {string} lock_path
* @param {Function} cb
* @param {boolean} [throw_if_locked]
*/
async function lock_and_run(fs_context, lock_path, cb) {
async function lock_and_run(fs_context, lock_path, cb, throw_if_locked = false) {
const lockfd = await nb_native().fs.open(fs_context, lock_path, 'w');

try {
await lockfd.fcntllock(fs_context, 'EXCLUSIVE');
await lockfd.fcntllock(fs_context, 'EXCLUSIVE', throw_if_locked ? 'THROW' : undefined);
await cb();
} catch (err) {
dbg.error('lock_and_run: error', err);
if (err.code === 'EAGAIN' && throw_if_locked) {
throw new RpcError('LOCKED', `Resource is locked - lock path= ${lock_path}`);
}
} finally {
await lockfd.close(fs_context);
}
Expand Down

0 comments on commit 30215b2

Please sign in to comment.