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

Make kernel_id as a conditional optional field #1300

Merged
merged 4 commits into from Jul 13, 2023
Merged

Make kernel_id as a conditional optional field #1300

merged 4 commits into from Jul 13, 2023

Conversation

allstrive
Copy link
Contributor

Fix for #1299

@lresende lresende added the bug label Jul 7, 2023
@lresende lresende requested a review from kevin-bates July 8, 2023 03:48
@kevin-bates kevin-bates requested a review from Zsailer July 8, 2023 15:17
@kevin-bates
Copy link
Member

kevin-bates commented Jul 8, 2023

I think keeping kernel_id required is important, especially when the only time one won't be available is when action == "start" and status == "error".

I poked around and found the if-then-else construct can be used to introduce conditionality into schemas and, by amending the schema in this PR with the following, we can introduce the idea that kernel_id NOT be required only on failed start requests. (Note that kernel_id: type: string should be restored and the description updated to state that kernel_id is conditionally required, e.g.,: This field is required except when action is start and status is error.)

if:
  properties:
    status: 
        const: error
    action:
        const: start
else:
  required:
    - kernel_id 

I verified this behavior using https://yamlschemavalidator.datree.io/ with the following instance data and updating action and status relative to the presence/absence of kernel_id:

action: start
status: error
--kernel_id: foo
kernel_name: bar
msg: text

I don't feel this overly complicates the schema and keeps kernel_id required for the majority (88%) of the action/status combinations. That said, I'd like @Zsailer's input and have requested his review.

Here's the suggested schema:
"$id": https://events.jupyter.org/jupyter_server/kernel_actions/v1
version: 1
title: Kernel Manager activities
personal-data: true
description: |
  Record events of a kernel manager.
type: object    
required:
  - action
  - msg
properties:
  action:
    enum:
      - start
      - interrupt
      - shutdown
      - restart
    description: |
      Action performed by the Kernel Manager.

      This is a required field.

      Possible values:

      1. start
         A kernel has been started with the given kernel id.

      2. interrupt
         A kernel has been interrupted for the given kernel id.

      3. shutdown
         A kernel has been shut down for the given kernel id.

      4. restart
         A kernel has been restarted for the given kernel id.
  kernel_id:
    type: string
    description: |
      Kernel id.
      
      This field is required except when action is start and status is error.
  kernel_name:
    type: string
    description: |
      Name of the kernel.
  status:
    enum:
      - error
      - success
    description: |
      Status received from a rest api operation to kernel server.

      This is a required field.

      Possible values:

      1. error
         Error response from a rest api operation to kernel server.

      2. success
         Success response from a rest api operation to kernel server.
  status_code:
    type: number
    description: |
      Http response codes from a rest api operation to kernel server.
      Examples: 200, 400, 502, 503, 599 etc
  msg:
    type: string
    description: |
      Description of the event specified in action.
if:
  properties:
    status: 
        const: error
    action:
        const: start
else:
  required:
    - kernel_id 

@allstrive
Copy link
Contributor Author

Thanks @kevin-bates for that wonderful suggestion 😄
Learnt something today - conditional validation in schema. I will give it a try and post an update here.
Have a great weekend.

@kevin-bates
Copy link
Member

This stanza might be a bit more straightforward (and direct):

# Do not require kernel_id on failed start attempts
if:
  not:
    properties:
      status: 
        const: error
      action:
        const: start
then:
  required:
    - kernel_id 

Copy link
Contributor

@rajmusuku rajmusuku left a comment

Choose a reason for hiding this comment

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

lgtm

@lresende lresende self-requested a review July 10, 2023 22:19
kernel_id=self.kernel_id, kernel_name=self.kernel_name, action=action
),
}
if self.kernel_id:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is required to ensure that , it doesn't produce message with value
kernel_id : null as that will fail with current schema. The current schema won't accept null value.

Copy link
Member

@kevin-bates kevin-bates left a comment

Choose a reason for hiding this comment

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

Thanks for the updates. This looks good to me.

I suspect the build failures are unrelated, albeit related to JSON schema. However, since the first commit on the PR also failed with a similar issue, I don't think the failure is related to the use of the conditional.

I'm hoping we can get @Zsailer's take on this before merging.

@allstrive allstrive changed the title Make kernel_id as optional field and allow None as a value Make kernel_id as a conditional optional field Jul 12, 2023
@blink1073
Copy link
Collaborator

I just pushed a fix to ignore that deprecation warning for now, as I did in pytest-jupyter. The true fix will involve some refactoring in jupyter_events to handle the new resolver API in jsonschema.

@Zsailer
Copy link
Member

Zsailer commented Jul 12, 2023

Apologies for the delayed response here.

I poked around and found the if-then-else construct can be used to introduce conditionality into schemas

Nice find @kevin-bates! Thank you for taking the time to dive into JSON schemas and find this.

This PR looks good to me. Just for future reference—Since this is a, in essence, a bug fix in the schema, there's no need to version the schema.

@allstrive
Copy link
Contributor Author

@blink1073 We are still seeing jsonschema related error , this time it is downstream in jupyterlab-server. Do we need it to be all green for the PR to be merged ? If so, then we will have to apply the similar fix to jupyterlab-server that you did for jupyter-server.

@blink1073
Copy link
Collaborator

That looks like a problem for jupyterlab_server to solve, not related to this PR, merging!

@blink1073 blink1073 merged commit 2563bae into jupyter-server:main Jul 13, 2023
33 of 35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants