github.com/weaviate/weaviate@v1.24.6/adapters/clients/cluster_classifications.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 clients
    13  
    14  import (
    15  	"bytes"
    16  	"context"
    17  	"encoding/json"
    18  	"io"
    19  	"net/http"
    20  	"net/url"
    21  
    22  	"github.com/pkg/errors"
    23  	"github.com/weaviate/weaviate/usecases/cluster"
    24  )
    25  
    26  type ClusterClassifications struct {
    27  	client *http.Client
    28  }
    29  
    30  func NewClusterClassifications(httpClient *http.Client) *ClusterClassifications {
    31  	return &ClusterClassifications{client: httpClient}
    32  }
    33  
    34  func (c *ClusterClassifications) OpenTransaction(ctx context.Context, host string,
    35  	tx *cluster.Transaction,
    36  ) error {
    37  	path := "/classifications/transactions/"
    38  	method := http.MethodPost
    39  	url := url.URL{Scheme: "http", Host: host, Path: path}
    40  
    41  	pl := txPayload{
    42  		Type:    tx.Type,
    43  		ID:      tx.ID,
    44  		Payload: tx.Payload,
    45  	}
    46  
    47  	jsonBytes, err := json.Marshal(pl)
    48  	if err != nil {
    49  		return errors.Wrap(err, "marshal transaction payload")
    50  	}
    51  
    52  	req, err := http.NewRequestWithContext(ctx, method, url.String(),
    53  		bytes.NewReader(jsonBytes))
    54  	if err != nil {
    55  		return errors.Wrap(err, "open http request")
    56  	}
    57  
    58  	req.Header.Set("content-type", "application/json")
    59  
    60  	res, err := c.client.Do(req)
    61  	if err != nil {
    62  		return errors.Wrap(err, "send http request")
    63  	}
    64  
    65  	defer res.Body.Close()
    66  	if res.StatusCode != http.StatusCreated {
    67  		if res.StatusCode == http.StatusConflict {
    68  			return cluster.ErrConcurrentTransaction
    69  		}
    70  
    71  		body, _ := io.ReadAll(res.Body)
    72  		return errors.Errorf("unexpected status code %d (%s)", res.StatusCode,
    73  			body)
    74  	}
    75  
    76  	return nil
    77  }
    78  
    79  func (c *ClusterClassifications) AbortTransaction(ctx context.Context, host string,
    80  	tx *cluster.Transaction,
    81  ) error {
    82  	path := "/classifications/transactions/" + tx.ID
    83  	method := http.MethodDelete
    84  	url := url.URL{Scheme: "http", Host: host, Path: path}
    85  
    86  	req, err := http.NewRequestWithContext(ctx, method, url.String(), nil)
    87  	if err != nil {
    88  		return errors.Wrap(err, "open http request")
    89  	}
    90  
    91  	res, err := c.client.Do(req)
    92  	if err != nil {
    93  		return errors.Wrap(err, "send http request")
    94  	}
    95  
    96  	defer res.Body.Close()
    97  	if res.StatusCode != http.StatusNoContent {
    98  		return errors.Errorf("unexpected status code %d", res.StatusCode)
    99  	}
   100  
   101  	return nil
   102  }
   103  
   104  func (c *ClusterClassifications) CommitTransaction(ctx context.Context, host string,
   105  	tx *cluster.Transaction,
   106  ) error {
   107  	path := "/classifications/transactions/" + tx.ID + "/commit"
   108  	method := http.MethodPut
   109  	url := url.URL{Scheme: "http", Host: host, Path: path}
   110  
   111  	req, err := http.NewRequestWithContext(ctx, method, url.String(), nil)
   112  	if err != nil {
   113  		return errors.Wrap(err, "open http request")
   114  	}
   115  
   116  	res, err := c.client.Do(req)
   117  	if err != nil {
   118  		return errors.Wrap(err, "send http request")
   119  	}
   120  
   121  	defer res.Body.Close()
   122  	if res.StatusCode != http.StatusNoContent {
   123  		return errors.Errorf("unexpected status code %d", res.StatusCode)
   124  	}
   125  
   126  	return nil
   127  }