Skip to content
Open
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
1 change: 1 addition & 0 deletions .nextchanges/cli/serving-endpoints-wait-progress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* `serving-endpoints create` and `update-config` now show each served entity's deployment state (for example `Provisioning`) and the time waited while they wait for the endpoint, instead of only `IN_PROGRESS`. When stderr is not a terminal, each state change is printed as a line on stderr instead of printing nothing. ([#7659](https://github.com/databricks/cli/pull/7659))
13 changes: 13 additions & 0 deletions acceptance/cmd/workspace/serving-endpoints/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "iris-clf",
"config": {
"served_entities": [
{
"entity_name": "main.default.iris",
"entity_version": "1",
"workload_size": "Small",
"scale_to_zero_enabled": true
}
]
}
}
1 change: 1 addition & 0 deletions acceptance/cmd/workspace/serving-endpoints/out.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cloud = false
34 changes: 34 additions & 0 deletions acceptance/cmd/workspace/serving-endpoints/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

=== Create waits and reports progress on stderr; stdout is the endpoint JSON

>>> [CLI] serving-endpoints create --json @input.json
{
"name": "iris-clf",
"state": {
"config_update": "NOT_UPDATING",
"ready": "NOT_READY"
}
}

=== Create with --no-wait prints the create response and no progress

>>> [CLI] serving-endpoints create --no-wait --json {"name": "iris-clf-2"}
{
"name": "iris-clf-2"
}

=== Update config waits and reports progress

>>> [CLI] serving-endpoints update-config iris-clf --json @update.json
current status: IN_PROGRESS ([ELAPSED])
{
"name": "iris-clf",
"state": {
"config_update": "NOT_UPDATING",
"ready": "NOT_READY"
}
}

=== Help mentions the wait and --no-wait

>>> [CLI] serving-endpoints create --help
13 changes: 13 additions & 0 deletions acceptance/cmd/workspace/serving-endpoints/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
title "Create waits and reports progress on stderr; stdout is the endpoint JSON"
trace $CLI serving-endpoints create --json @input.json > out.create.json
jq '{name, state}' out.create.json
rm out.create.json

title "Create with --no-wait prints the create response and no progress"
trace $CLI serving-endpoints create --no-wait --json '{"name": "iris-clf-2"}' | jq '{name}'

title "Update config waits and reports progress"
trace $CLI serving-endpoints update-config iris-clf --json @update.json | jq '{name, state}'

title "Help mentions the wait and --no-wait"
trace $CLI serving-endpoints create --help | contains.py "Use --no-wait to return immediately" > /dev/null
6 changes: 6 additions & 0 deletions acceptance/cmd/workspace/serving-endpoints/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Cloud = false

# Progress lines include the time waited so far.
[[Repls]]
Old = '\((\d+m)?\d+s\)'
New = '([ELAPSED])'
10 changes: 10 additions & 0 deletions acceptance/cmd/workspace/serving-endpoints/update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"served_entities": [
{
"entity_name": "main.default.iris",
"entity_version": "2",
"workload_size": "Small",
"scale_to_zero_enabled": true
}
]
}
219 changes: 219 additions & 0 deletions cmd/workspace/serving-endpoints/overrides.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package serving_endpoints

import (
"context"
"errors"
"fmt"
"strings"
"time"

"github.com/databricks/cli/libs/cmdctx"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/flags"
"github.com/databricks/databricks-sdk-go/service/serving"
"github.com/spf13/cobra"
)

// waitProgress reports progress while create or update-config waits for the
// endpoint to finish updating.
//
// The generated wait only shows state.config_update, which stays IN_PROGRESS
// for the whole deployment. Each served entity carries a more useful
// deployment_state_message (for example "Container image creation in progress"
// or "Provisioning"), so we show that instead, with the time waited so far.
//
// A spinner prints nothing when stderr is not a terminal, which left agents and
// CI with no output for the full deployment. In that case we print one line to
// stderr each time the message changes. Stdout is unchanged either way.
type waitProgress struct {
start time.Time
spinner interface {
Update(string)
Close()
}
last string
}

func newWaitProgress(ctx context.Context) *waitProgress {
p := &waitProgress{start: time.Now()}
if cmdio.GetInteractiveMode(ctx) != cmdio.InteractiveModeNone {
p.spinner = cmdio.NewSpinner(ctx)
}
return p
}

func (p *waitProgress) report(ctx context.Context, i *serving.ServingEndpointDetailed) {
msg := endpointProgressMessage(i)
line := fmt.Sprintf("%s (%s)", msg, time.Since(p.start).Truncate(time.Second))
if p.spinner != nil {
p.spinner.Update(line)
return
}
// Without a spinner, print a line only when the message changes, not on every
// poll. Skip the final poll: the update is done and the result follows on
// stdout, so commands that finish immediately print exactly what they used to.
if i.State != nil && i.State.ConfigUpdate == serving.EndpointStateConfigUpdateNotUpdating {
return
}
if msg != p.last {
p.last = msg
cmdio.LogProgress(ctx, line)
}
}

func (p *waitProgress) close() {
if p.spinner != nil {
p.spinner.Close()
}
}

// endpointProgressMessage describes where the endpoint is in its update. It
// prefers the per-entity state of the pending config, falls back to the active
// config, and falls back to the generated "current status" text when no entity
// reports a state.
func endpointProgressMessage(i *serving.ServingEndpointDetailed) string {
var parts []string
if i != nil {
var entities []serving.ServedEntityOutput
if i.PendingConfig != nil {
entities = i.PendingConfig.ServedEntities
} else if i.Config != nil {
entities = i.Config.ServedEntities
}
for _, e := range entities {
if e.State == nil {
continue
}
state := e.State.DeploymentStateMessage
if state == "" {
state = string(e.State.Deployment)
}
if state == "" {
continue
}
if e.Name != "" {
state = e.Name + ": " + state
}
parts = append(parts, state)
}
}
if len(parts) > 0 {
return strings.Join(parts, "; ")
}
var status serving.EndpointStateConfigUpdate
if i != nil && i.State != nil {
status = i.State.ConfigUpdate
}
return fmt.Sprintf("current status: %s", status)
}

// unmarshalJSONFlag applies --json to req the same way the generated command
// does, including rendering non-fatal diagnostics such as unknown fields.
func unmarshalJSONFlag(cmd *cobra.Command, req any) error {
if !cmd.Flags().Changed("json") {
return nil
}
jsonFlag, ok := cmd.Flags().Lookup("json").Value.(*flags.JsonFlag)
if !ok {
return errors.New("unexpected type for --json flag")
}
diags := jsonFlag.Unmarshal(req)
if diags.HasError() {
return diags.Error()
}
if len(diags) > 0 {
return cmdio.RenderDiagnostics(cmd.Context(), diags)
}
return nil
}

func waitFlags(cmd *cobra.Command) (skipWait bool, timeout time.Duration, err error) {
skipWait, err = cmd.Flags().GetBool("no-wait")
if err != nil {
return false, 0, err
}
timeout, err = cmd.Flags().GetDuration("timeout")
return skipWait, timeout, err
}

const waitHelp = `

By default this command waits until the endpoint finishes updating (up to
--timeout, 20m by default), printing each served entity's deployment state
to stderr. Use --no-wait to return immediately, then check progress with
"databricks serving-endpoints get NAME".`

func createOverride(createCmd *cobra.Command, createReq *serving.CreateServingEndpoint) {
createCmd.Long += waitHelp
createCmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
w := cmdctx.WorkspaceClient(ctx)

if err := unmarshalJSONFlag(cmd, createReq); err != nil {
return err
}
if !cmd.Flags().Changed("json") {
createReq.Name = args[0]
}
skipWait, timeout, err := waitFlags(cmd)
if err != nil {
return err
}

wait, err := w.ServingEndpoints.Create(ctx, *createReq)
if err != nil {
return err
}
if skipWait {
return cmdio.Render(ctx, wait.Response)
}
p := newWaitProgress(ctx)
info, err := wait.OnProgress(func(i *serving.ServingEndpointDetailed) {
p.report(ctx, i)
}).GetWithTimeout(timeout)
p.close()
if err != nil {
return err
}
return cmdio.Render(ctx, info)
}
}

func updateConfigOverride(updateConfigCmd *cobra.Command, updateConfigReq *serving.EndpointCoreConfigInput) {
updateConfigCmd.Long += waitHelp
updateConfigCmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
w := cmdctx.WorkspaceClient(ctx)

if err := unmarshalJSONFlag(cmd, updateConfigReq); err != nil {
return err
}
updateConfigReq.Name = args[0]
skipWait, timeout, err := waitFlags(cmd)
if err != nil {
return err
}

wait, err := w.ServingEndpoints.UpdateConfig(ctx, *updateConfigReq)
if err != nil {
return err
}
if skipWait {
return cmdio.Render(ctx, wait.Response)
}
p := newWaitProgress(ctx)
info, err := wait.OnProgress(func(i *serving.ServingEndpointDetailed) {
p.report(ctx, i)
}).GetWithTimeout(timeout)
p.close()
if err != nil {
return err
}
return cmdio.Render(ctx, info)
}
}

func init() {
createOverrides = append(createOverrides, createOverride)
updateConfigOverrides = append(updateConfigOverrides, updateConfigOverride)
}
Loading
Loading