github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/core/notif.go (about)

     1  // Package core provides core metadata and in-cluster API
     2  /*
     3   * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package core
     6  
     7  import (
     8  	"strings"
     9  	"time"
    10  )
    11  
    12  // On the sending side, intra-cluster notification is a tuple containing answers
    13  // to the following existential questions:
    14  // 	- when to notify
    15  // 	- who to notify
    16  // 	- how to notify
    17  // The "how" part is usually notification-type specific - hence, the callback.
    18  //
    19  // TODO: add more notification types over time
    20  //       and, in particular, other than xaction completion notifications.
    21  
    22  /////////////////////////
    23  // notification sender //
    24  /////////////////////////
    25  
    26  // enum: when to notify
    27  const (
    28  	UponTerm     = Upon(1 << iota) // success or fail is separately provided via error
    29  	UponProgress                   // periodic (BytesCount, ObjCount)
    30  )
    31  
    32  type (
    33  	Upon int
    34  
    35  	// intra-cluster notification interface
    36  	Notif interface {
    37  		OnFinishedCB() func(Notif, error, bool /*aborted*/)
    38  		OnProgressCB() func(Notif)
    39  		NotifyInterval() time.Duration // notify interval in secs
    40  		LastNotifTime() int64          // time last notified
    41  		SetLastNotified(now int64)
    42  		Upon(u Upon) bool
    43  		Subscribers() []string
    44  		ToNotifMsg(aborted bool) NotifMsg
    45  	}
    46  
    47  	// intra-cluster notification message
    48  	NotifMsg struct {
    49  		UUID     string `json:"uuid"`    // xaction UUID
    50  		NodeID   string `json:"node_id"` // notifier node ID
    51  		Kind     string `json:"kind"`    // xaction `Kind`
    52  		ErrMsg   string `json:"err"`     // error.Error()
    53  		Data     []byte `json:"message"` // (e.g. usage: custom progress stats)
    54  		AbortedX bool   `json:"aborted"` // true if aborted (see related: Snap.AbortedX)
    55  	}
    56  )
    57  
    58  func (msg *NotifMsg) String() (s string) {
    59  	var sb strings.Builder
    60  	sb.WriteString("nmsg-")
    61  	sb.WriteString(msg.Kind)
    62  	sb.WriteByte('[')
    63  	sb.WriteString(msg.UUID)
    64  	sb.WriteByte(']')
    65  	sb.WriteString("<=")
    66  	sb.WriteString(msg.NodeID)
    67  	if msg.ErrMsg != "" {
    68  		sb.WriteString(", err: ")
    69  		sb.WriteString(msg.ErrMsg)
    70  	}
    71  	return sb.String()
    72  }