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

     1  // Package xact provides core functionality for the AIStore eXtended Actions (xactions).
     2  /*
     3   * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package xact
     6  
     7  import (
     8  	"net/http"
     9  	"net/url"
    10  
    11  	"github.com/NVIDIA/aistore/api/apc"
    12  	"github.com/NVIDIA/aistore/cmn"
    13  	"github.com/NVIDIA/aistore/cmn/cos"
    14  	"github.com/NVIDIA/aistore/core"
    15  	"github.com/NVIDIA/aistore/core/meta"
    16  	"github.com/NVIDIA/aistore/nl"
    17  	jsoniter "github.com/json-iterator/go"
    18  )
    19  
    20  type (
    21  	NotifXactListener struct {
    22  		nl.ListenerBase
    23  	}
    24  
    25  	NotifXact struct {
    26  		Xact core.Xact
    27  		nl.Base
    28  	}
    29  )
    30  
    31  // interface guard
    32  var (
    33  	_ core.Notif  = (*NotifXact)(nil)
    34  	_ nl.Listener = (*NotifXactListener)(nil)
    35  )
    36  
    37  ///////////////////////
    38  // NotifXactListener //
    39  ///////////////////////
    40  
    41  func NewXactNL(uuid, kind string, smap *meta.Smap, srcs meta.NodeMap, bck ...*cmn.Bck) *NotifXactListener {
    42  	if srcs == nil {
    43  		srcs = smap.Tmap.ActiveMap()
    44  	}
    45  	return &NotifXactListener{
    46  		ListenerBase: *nl.NewNLB(uuid, kind, "", srcs, 0, bck...),
    47  	}
    48  }
    49  
    50  func (nxb *NotifXactListener) WithCause(cause string) *NotifXactListener {
    51  	nxb.Common.Cause = cause
    52  	return nxb
    53  }
    54  
    55  func (*NotifXactListener) UnmarshalStats(rawMsg []byte) (stats any, finished, aborted bool, err error) {
    56  	snap := &core.Snap{}
    57  	if err = jsoniter.Unmarshal(rawMsg, snap); err != nil {
    58  		return
    59  	}
    60  	stats = snap
    61  	aborted, finished = snap.IsAborted(), snap.Finished()
    62  	return
    63  }
    64  
    65  func (nxb *NotifXactListener) QueryArgs() cmn.HreqArgs {
    66  	args := cmn.HreqArgs{Method: http.MethodGet, Query: make(url.Values, 2)}
    67  	args.Query.Set(apc.QparamWhat, apc.WhatXactStats)
    68  	args.Query.Set(apc.QparamUUID, nxb.UUID())
    69  	args.Path = apc.URLPathXactions.S
    70  	return args
    71  }
    72  
    73  ///////////////
    74  // NotifXact //
    75  ///////////////
    76  
    77  func (nx *NotifXact) ToNotifMsg(aborted bool) core.NotifMsg {
    78  	return core.NotifMsg{
    79  		UUID:     nx.Xact.ID(),
    80  		Kind:     nx.Xact.Kind(),
    81  		Data:     cos.MustMarshal(nx.Xact.Snap()),
    82  		AbortedX: aborted,
    83  	}
    84  }