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

Improve Exception Handling #16

Merged
merged 3 commits into from
Sep 11, 2024
Merged

Conversation

Amar-C
Copy link
Collaborator

@Amar-C Amar-C commented Sep 6, 2024

Summary

Currently, the plugin when encountering an error from the driver, logs messages using log.Fatal(err) that prints the error message and calls os.Exit(1) which terminates the program immediately without letting users try/catch the exception in k6.js files.

This PR removes the use of log.Fatal() which but allows users to try/catch the exceptions in the k6 JS files and if not print the message.

Testing

k6 script (truncated to remove user/pass etc.):

// This code is run once before any VUs are started.
export function setup() {
    const key_prefix = 'k6:prefix:';

    console.log(`Connected to ${hostname}`);
    console.log(`inserting ${doc_count} documents into ${bucket_name}.${scope_name}.${collection_name} with key prefix ${key_prefix} ...`);

    const i = 0;
    try {
        client.insert(bucket_name, scope_name, collection_name, key_prefix + i, { 'hello': 'world' });
        client.insert(bucket_name, scope_name, collection_name, key_prefix + i, { 'hello': 'world' });

    } catch (error) {
        console.log('error inserting document: ' + error);
    }

    console.log('done inserting')
    return { key_prefix: key_prefix };
}

export default function (data) {
    const key = data.key_prefix + Math.floor(Math.random() * doc_count)

    var res = client.findOne(bucket_name, scope_name, collection_name, key);
    console.log(res)
}

Current: Program immediately exits and does not honor the try/catch

$ ./code/xk6-couchbase/xk6-couchbase run --duration 2s ~/code/k6-scripts/exception_cb.js

          /\      |‾‾| /‾‾/   /‾‾/
     /\  /  \     |  |/  /   /  /
    /  \/    \    |     (   /   ‾‾\
   /          \   |  |\  \ |  (‾)  |
  / __________ \  |__| \__\ \_____/ .io

     execution: local
        script: /home/amchaudh/code/k6-scripts/exception_cb.js
        output: -

     scenarios: (100.00%) 1 scenario, 1 max VUs, 32s max duration (incl. graceful stop):
              * default: 1 looping VUs for 2s (gracefulStop: 30s)

INFO[0000] Connected to test-hdd-1.in-couchbase.ei4.ird.disco.linkedin.com  source=console
INFO[0000] inserting 1 documents into beer-sample._default.k6 with key prefix k6:prefix: ...  source=console
INFO[0000] 2024/09/06 17:20:42 document exists | {"status_code":2,"document_id":"k6:prefix:0","bucket":"beer-sample","scope":"_default","collection":"k6","collection_id":8,"error_name":"KEY_EEXISTS","error_description":"key already exists, or CAS mismatch","opaque":8,"last_dispatched_to":"lor1-0000866.int.linkedin.com:11210","last_dispatched_from":"100.76.134.11:52988","last_connection_id":"067822df61e7c430/40e82ea512b8b0f8"}
$
$ ====================] setup()
$ --------------------]

New Behavior: try/catch works as expected


          /\      |‾‾| /‾‾/   /‾‾/
     /\  /  \     |  |/  /   /  /
    /  \/    \    |     (   /   ‾‾\
   /          \   |  |\  \ |  (‾)  |
  / __________ \  |__| \__\ \_____/ .io

     execution: local
        script: /home/amchaudh/code/k6-scripts/exception_cb.js
        output: -

     scenarios: (100.00%) 1 scenario, 1 max VUs, 32s max duration (incl. graceful stop):
              * default: 1 looping VUs for 2s (gracefulStop: 30s)

INFO[0000] Connected to test-hdd-1.in-couchbase.ei4.ird.disco.linkedin.com  source=console
INFO[0000] inserting 1 documents into beer-sample._default.k6 with key prefix k6:prefix: ...  source=console
INFO[0000] error inserting document: GoError: document exists | {"status_code":2,"document_id":"k6:prefix:0","bucket":"beer-sample","scope":"_default","collection":"k6","collection_id":8,"error_name":"KEY_EEXISTS","error_description":"key already exists, or CAS mismatch","opaque":8,"last_dispatched_to":"lor1-0000866.int.linkedin.com:11210","last_dispatched_from":"100.76.134.11:47206","last_connection_id":"48b5ab73fc7067ed/a60e54da7b4af4f4"}  source=console
INFO[0000] done inserting                                source=console
INFO[0001] {"hello":"world"}                             source=console
INFO[0001] {"hello":"world"}                             source=console
INFO[0001] {"hello":"world"}                             source=console
INFO[0001] {"hello":"world"}                             source=console
INFO[0001] {"hello":"world"}                             source=console
INFO[0001] {"hello":"world"}                             source=console
INFO[0001] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0002] {"hello":"world"}                             source=console
INFO[0003] {"hello":"world"}                             source=console

     data_received........: 0 B 0 B/s
     data_sent............: 0 B 0 B/s
     iteration_duration...: avg=78.68ms min=38.31ms med=38.44ms max=740.87ms p(90)=38.63ms p(95)=251.78ms
     iterations...........: 34  12.336728/s
     vus..................: 1   min=1       max=1
     vus_max..............: 1   min=1       max=1


running (02.8s), 0/1 VUs, 34 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs  2s

@Amar-C Amar-C marked this pull request as ready for review September 6, 2024 17:31
@thotasrinath thotasrinath self-requested a review September 9, 2024 05:41
Copy link
Owner

@thotasrinath thotasrinath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Looks good to me

@thotasrinath thotasrinath merged commit 6066efd into thotasrinath:master Sep 11, 2024
1 check passed
@Amar-C
Copy link
Collaborator Author

Amar-C commented Sep 11, 2024

Thanks @thotasrinath.
Could you please release a new version?

@thotasrinath
Copy link
Owner

Released the latest source. Please check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants