github.com/weaviate/weaviate@v1.24.6/usecases/schema/transactions.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 schema
    13  
    14  import (
    15  	"encoding/json"
    16  	"time"
    17  
    18  	"github.com/pkg/errors"
    19  	"github.com/weaviate/weaviate/entities/models"
    20  	"github.com/weaviate/weaviate/usecases/cluster"
    21  	"github.com/weaviate/weaviate/usecases/sharding"
    22  )
    23  
    24  const (
    25  	// write-only
    26  	AddClass            cluster.TransactionType = "add_class"
    27  	AddProperty         cluster.TransactionType = "add_property"
    28  	mergeObjectProperty cluster.TransactionType = "merge_object_property"
    29  
    30  	// tenant types
    31  	addTenants    cluster.TransactionType = "add_tenants"
    32  	updateTenants cluster.TransactionType = "update_tenants"
    33  	deleteTenants cluster.TransactionType = "delete_tenants"
    34  
    35  	DeleteClass cluster.TransactionType = "delete_class"
    36  	UpdateClass cluster.TransactionType = "update_class"
    37  
    38  	// read-only
    39  	ReadSchema cluster.TransactionType = "read_schema"
    40  
    41  	// repairs
    42  	RepairClass    cluster.TransactionType = "repair_class"
    43  	RepairProperty cluster.TransactionType = "repair_property"
    44  	RepairTenant   cluster.TransactionType = "repair_tenant"
    45  
    46  	DefaultTxTTL = 60 * time.Second
    47  )
    48  
    49  // any tx that is listed here will be tried to commit if it is still open on
    50  // startup
    51  var resumableTxs = []cluster.TransactionType{
    52  	addTenants,
    53  }
    54  
    55  // any tx that is listed here will bypass the ready-check, i.e. they will
    56  // execute even if the DB is unready.
    57  var allowUnreadyTxs = []cluster.TransactionType{
    58  	ReadSchema, // required at startup, does not write
    59  	RepairClass,
    60  	RepairProperty,
    61  	RepairTenant,
    62  }
    63  
    64  type AddClassPayload struct {
    65  	Class *models.Class   `json:"class"`
    66  	State *sharding.State `json:"state"`
    67  }
    68  
    69  type AddPropertyPayload struct {
    70  	ClassName string           `json:"className"`
    71  	Property  *models.Property `json:"property"`
    72  }
    73  
    74  type MergeObjectPropertyPayload struct {
    75  	ClassName string           `json:"className"`
    76  	Property  *models.Property `json:"property"`
    77  }
    78  
    79  // TenantCreate represents properties of a specific tenant (physical shard)
    80  type TenantCreate struct {
    81  	Name   string   `json:"name"`
    82  	Nodes  []string `json:"nodes"`
    83  	Status string   `json:"status"`
    84  }
    85  
    86  type TenantUpdate struct {
    87  	Name   string `json:"name"`
    88  	Status string `json:"status"`
    89  }
    90  
    91  // AddTenantsPayload allows for adding multiple tenants to a class
    92  type AddTenantsPayload struct {
    93  	Class   string         `json:"class_name"`
    94  	Tenants []TenantCreate `json:"tenants"`
    95  }
    96  
    97  type UpdateTenantsPayload struct {
    98  	Class   string         `json:"class_name"`
    99  	Tenants []TenantUpdate `json:"tenants"`
   100  }
   101  
   102  // DeleteTenantsPayload allows for removing multiple tenants from a class
   103  type DeleteTenantsPayload struct {
   104  	Class   string   `json:"class_name"`
   105  	Tenants []string `json:"tenants"`
   106  }
   107  
   108  type DeleteClassPayload struct {
   109  	ClassName string `json:"className"`
   110  }
   111  
   112  type UpdateClassPayload struct {
   113  	ClassName string        `json:"className"`
   114  	Class     *models.Class `json:"class"`
   115  
   116  	// For now, the state cannot be updated yet, but this will be a requirement
   117  	// in the future, for example, with dynamically changing replication, so we
   118  	// should already make sure that state is part of the transaction payload
   119  	State *sharding.State `json:"state"`
   120  }
   121  
   122  type ReadSchemaPayload struct {
   123  	Schema *State `json:"schema"`
   124  }
   125  
   126  func UnmarshalTransaction(txType cluster.TransactionType,
   127  	payload json.RawMessage,
   128  ) (interface{}, error) {
   129  	switch txType {
   130  	case AddClass, RepairClass:
   131  		return unmarshalRawJson[AddClassPayload](payload)
   132  	case AddProperty, RepairProperty:
   133  		return unmarshalRawJson[AddPropertyPayload](payload)
   134  	case mergeObjectProperty:
   135  		return unmarshalRawJson[MergeObjectPropertyPayload](payload)
   136  	case DeleteClass:
   137  		return unmarshalRawJson[DeleteClassPayload](payload)
   138  	case UpdateClass:
   139  		return unmarshalRawJson[UpdateClassPayload](payload)
   140  	case ReadSchema:
   141  		return unmarshalRawJson[ReadSchemaPayload](payload)
   142  	case addTenants, RepairTenant:
   143  		return unmarshalRawJson[AddTenantsPayload](payload)
   144  	case updateTenants:
   145  		return unmarshalRawJson[UpdateTenantsPayload](payload)
   146  	case deleteTenants:
   147  		return unmarshalRawJson[DeleteTenantsPayload](payload)
   148  	default:
   149  		return nil, errors.Errorf("unrecognized schema transaction type %q", txType)
   150  
   151  	}
   152  }
   153  
   154  // unmarshalRawJson returns the result of marshalling json payload
   155  func unmarshalRawJson[T any](payload json.RawMessage) (T, error) {
   156  	var v T
   157  	err := json.Unmarshal(payload, &v)
   158  
   159  	return v, err
   160  }