github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/shard_status.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 db
    13  
    14  import (
    15  	"strings"
    16  
    17  	"github.com/pkg/errors"
    18  	"github.com/weaviate/weaviate/entities/storagestate"
    19  )
    20  
    21  func (s *Shard) initStatus() {
    22  	s.statusLock.Lock()
    23  	defer s.statusLock.Unlock()
    24  
    25  	s.status = storagestate.StatusReady
    26  }
    27  
    28  func (s *Shard) GetStatus() storagestate.Status {
    29  	s.statusLock.Lock()
    30  	defer s.statusLock.Unlock()
    31  
    32  	return s.status
    33  }
    34  
    35  func (s *Shard) isReadOnly() bool {
    36  	return s.GetStatus() == storagestate.StatusReadOnly
    37  }
    38  
    39  func (s *Shard) compareAndSwapStatus(old, new string) (storagestate.Status, error) {
    40  	s.statusLock.Lock()
    41  	defer s.statusLock.Unlock()
    42  
    43  	if s.status.String() != old {
    44  		return s.status, nil
    45  	}
    46  
    47  	return s.status, s.updateStatusUnlocked(new)
    48  }
    49  
    50  func (s *Shard) UpdateStatus(in string) error {
    51  	s.statusLock.Lock()
    52  	defer s.statusLock.Unlock()
    53  
    54  	return s.updateStatusUnlocked(in)
    55  }
    56  
    57  // updateStatusUnlocked updates the status without locking the statusLock.
    58  // Warning: Use UpdateStatus instead.
    59  func (s *Shard) updateStatusUnlocked(in string) error {
    60  	targetStatus, err := storagestate.ValidateStatus(strings.ToUpper(in))
    61  	if err != nil {
    62  		return errors.Wrap(err, in)
    63  	}
    64  
    65  	s.status = targetStatus
    66  	s.updateStoreStatus(targetStatus)
    67  
    68  	s.index.logger.
    69  		WithField("action", "update shard status").
    70  		WithField("class", s.index.Config.ClassName).
    71  		WithField("shard", s.name).
    72  		WithField("status", in)
    73  
    74  	return nil
    75  }
    76  
    77  func (s *Shard) updateStoreStatus(targetStatus storagestate.Status) {
    78  	s.store.UpdateBucketsStatus(targetStatus)
    79  }