github.com/m3db/m3@v1.5.0/src/cluster/placement/placements.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package placement
    22  
    23  import (
    24  	"errors"
    25  	"sort"
    26  
    27  	"github.com/m3db/m3/src/cluster/generated/proto/placementpb"
    28  )
    29  
    30  var (
    31  	errNilPlacement               = errors.New("placement is nil")
    32  	errNilPlacementSnapshotsProto = errors.New("PlacementSnapshots proto is nil")
    33  	errEmptyPlacementSnapshots    = errors.New("placement snapshots is empty")
    34  )
    35  
    36  // Placements represents a placement that is backward compatible with
    37  // the deprecated concept of staged placement.
    38  type Placements struct {
    39  	latest Placement
    40  }
    41  
    42  // NewPlacementsFromLatest creates Placements from latest placement.
    43  func NewPlacementsFromLatest(p Placement) (*Placements, error) {
    44  	if p == nil {
    45  		return nil, errNilPlacement
    46  	}
    47  
    48  	return &Placements{
    49  		latest: p,
    50  	}, nil
    51  }
    52  
    53  // NewPlacementsFromProto creates Placements from proto.
    54  func NewPlacementsFromProto(p *placementpb.PlacementSnapshots) (*Placements, error) {
    55  	if p == nil {
    56  		return nil, errNilPlacementSnapshotsProto
    57  	}
    58  
    59  	if p.CompressMode == placementpb.CompressMode_ZSTD {
    60  		placementProto, err := decompressPlacementProto(p.CompressedPlacement)
    61  		if err != nil {
    62  			return nil, err
    63  		}
    64  
    65  		placement, err := NewPlacementFromProto(placementProto)
    66  		if err != nil {
    67  			return nil, err
    68  		}
    69  		return &Placements{
    70  			latest: placement,
    71  		}, nil
    72  	}
    73  
    74  	// Fallback to Snapshots field for backward compatibility.
    75  	n := len(p.Snapshots)
    76  	if n == 0 {
    77  		return nil, errEmptyPlacementSnapshots
    78  	}
    79  
    80  	all := make([]Placement, 0, n)
    81  	for _, snapshot := range p.Snapshots {
    82  		placement, err := NewPlacementFromProto(snapshot)
    83  		if err != nil {
    84  			return nil, err
    85  		}
    86  		all = append(all, placement)
    87  	}
    88  
    89  	// Keep only the latest.
    90  	sort.Sort(placementsByCutoverAsc(all))
    91  	return &Placements{
    92  		latest: all[n-1],
    93  	}, nil
    94  }
    95  
    96  // Proto converts Placements to a proto.
    97  func (placements *Placements) Proto() (*placementpb.PlacementSnapshots, error) {
    98  	latest, err := placements.latest.Proto()
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	return &placementpb.PlacementSnapshots{
   103  		Snapshots: []*placementpb.Placement{latest},
   104  	}, nil
   105  }
   106  
   107  // ProtoCompressed converts Placements to a proto with compressed placement.
   108  func (placements *Placements) ProtoCompressed() (*placementpb.PlacementSnapshots, error) {
   109  	latest, err := placements.latest.Proto()
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  
   114  	compressed, err := compressPlacementProto(latest)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	return &placementpb.PlacementSnapshots{
   120  		CompressMode:        placementpb.CompressMode_ZSTD,
   121  		CompressedPlacement: compressed,
   122  	}, nil
   123  }
   124  
   125  // Latest returns the latest placement.
   126  func (placements *Placements) Latest() Placement {
   127  	return placements.latest
   128  }
   129  
   130  type placementsByCutoverAsc []Placement
   131  
   132  func (s placementsByCutoverAsc) Len() int { return len(s) }
   133  
   134  func (s placementsByCutoverAsc) Less(i, j int) bool {
   135  	return s[i].CutoverNanos() < s[j].CutoverNanos()
   136  }
   137  
   138  func (s placementsByCutoverAsc) Swap(i, j int) { s[i], s[j] = s[j], s[i] }