github.com/m3db/m3@v1.5.0/src/cluster/services/util.go (about)

     1  // Copyright (c) 2018 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 services
    22  
    23  import (
    24  	"fmt"
    25  
    26  	"github.com/m3db/m3/src/cluster/generated/proto/placementpb"
    27  	"github.com/m3db/m3/src/cluster/kv"
    28  	"github.com/m3db/m3/src/cluster/placement"
    29  )
    30  
    31  const (
    32  	placementPrefix = "_sd.placement"
    33  	metadataPrefix  = "_sd.metadata"
    34  	keyFormat       = "%s/%s"
    35  )
    36  
    37  type keyFn func(sid ServiceID) string
    38  
    39  func placementNamespace(ns string) string {
    40  	if ns == "" {
    41  		ns = placementPrefix
    42  	}
    43  
    44  	return ns
    45  }
    46  
    47  func metadataNamespace(ns string) string {
    48  	if ns == "" {
    49  		ns = metadataPrefix
    50  	}
    51  
    52  	return ns
    53  }
    54  
    55  func keyFnWithNamespace(namespace string) keyFn {
    56  	return func(sid ServiceID) string {
    57  		return fmt.Sprintf(keyFormat, namespace, serviceKey(sid))
    58  	}
    59  }
    60  
    61  func adKey(sid ServiceID, id string) string {
    62  	return fmt.Sprintf(keyFormat, serviceKey(sid), id)
    63  }
    64  
    65  func serviceKey(s ServiceID) string {
    66  	if s.Environment() == "" {
    67  		return s.Name()
    68  	}
    69  	return fmt.Sprintf(keyFormat, s.Environment(), s.Name())
    70  }
    71  
    72  func validateServiceID(sid ServiceID) error {
    73  	if sid.Name() == "" {
    74  		return errNoServiceName
    75  	}
    76  
    77  	return nil
    78  }
    79  
    80  func placementProtoFromValue(v kv.Value) (*placementpb.Placement, error) {
    81  	var placementProto placementpb.Placement
    82  	if err := v.Unmarshal(&placementProto); err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	return &placementProto, nil
    87  }
    88  
    89  func placementFromValue(v kv.Value) (placement.Placement, error) {
    90  	placementProto, err := placementProtoFromValue(v)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	p, err := placement.NewPlacementFromProto(placementProto)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  
   100  	return p.SetVersion(v.Version()), nil
   101  }