github.com/weaviate/weaviate@v1.24.6/usecases/nodes/handler.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package nodes
    13  
    14  import (
    15  	"context"
    16  	"time"
    17  
    18  	"github.com/sirupsen/logrus"
    19  	"github.com/weaviate/weaviate/entities/models"
    20  	schemaUC "github.com/weaviate/weaviate/usecases/schema"
    21  )
    22  
    23  const GetNodeStatusTimeout = 30 * time.Second
    24  
    25  type authorizer interface {
    26  	Authorize(principal *models.Principal, verb, resource string) error
    27  }
    28  
    29  type db interface {
    30  	GetNodeStatus(ctx context.Context, className, verbosity string) ([]*models.NodeStatus, error)
    31  }
    32  
    33  type Manager struct {
    34  	logger        logrus.FieldLogger
    35  	authorizer    authorizer
    36  	db            db
    37  	schemaManager *schemaUC.Manager
    38  }
    39  
    40  func NewManager(logger logrus.FieldLogger, authorizer authorizer,
    41  	db db, schemaManager *schemaUC.Manager,
    42  ) *Manager {
    43  	return &Manager{logger, authorizer, db, schemaManager}
    44  }
    45  
    46  // GetNodeStatus aggregates the status across all nodes. It will try for a
    47  // maximum of the configured timeout, then mark nodes as timed out.
    48  func (m *Manager) GetNodeStatus(ctx context.Context,
    49  	principal *models.Principal, className string, verbosity string,
    50  ) ([]*models.NodeStatus, error) {
    51  	ctxWithTimeout, cancel := context.WithTimeout(ctx, GetNodeStatusTimeout)
    52  	defer cancel()
    53  
    54  	if err := m.authorizer.Authorize(principal, "list", "nodes"); err != nil {
    55  		return nil, err
    56  	}
    57  	return m.db.GetNodeStatus(ctxWithTimeout, className, verbosity)
    58  }