github.com/m3db/m3@v1.5.0/src/metrics/policy/staged_policy.go (about)

     1  // Copyright (c) 2017 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 policy
    22  
    23  import (
    24  	"encoding/json"
    25  	"fmt"
    26  )
    27  
    28  var (
    29  	// DefaultStagedPolicies represents a default staged policies.
    30  	DefaultStagedPolicies StagedPolicies
    31  
    32  	// DefaultPoliciesList represents a default policies list.
    33  	DefaultPoliciesList = PoliciesList{DefaultStagedPolicies}
    34  )
    35  
    36  // StagedPolicies represent a list of policies at a specified version.
    37  type StagedPolicies struct {
    38  	// Cutover is when the policies take effect.
    39  	CutoverNanos int64
    40  
    41  	// Tombstoned determines whether the associated (rollup) metric has been tombstoned.
    42  	Tombstoned bool
    43  
    44  	// policies represent the list of policies.
    45  	policies []Policy
    46  }
    47  
    48  // NewStagedPolicies create a new staged policies.
    49  func NewStagedPolicies(cutoverNanos int64, tombstoned bool, policies []Policy) StagedPolicies {
    50  	return StagedPolicies{CutoverNanos: cutoverNanos, Tombstoned: tombstoned, policies: policies}
    51  }
    52  
    53  // Reset resets the staged policies.
    54  func (p *StagedPolicies) Reset() { *p = DefaultStagedPolicies }
    55  
    56  // IsDefault returns whether this is a default staged policies.
    57  func (p StagedPolicies) IsDefault() bool {
    58  	return p.CutoverNanos == 0 && !p.Tombstoned && p.hasDefaultPolicies()
    59  }
    60  
    61  // Policies returns the policies and whether the policies are the default policies.
    62  func (p StagedPolicies) Policies() ([]Policy, bool) {
    63  	return p.policies, p.hasDefaultPolicies()
    64  }
    65  
    66  // Equals returns whether two staged policies are equal.
    67  func (p StagedPolicies) Equals(other StagedPolicies) bool {
    68  	if p.CutoverNanos != other.CutoverNanos || p.Tombstoned != other.Tombstoned {
    69  		return false
    70  	}
    71  	currPolicies, currIsDefault := p.Policies()
    72  	otherPolicies, otherIsDefault := other.Policies()
    73  	if currIsDefault && otherIsDefault {
    74  		return true
    75  	}
    76  	if currIsDefault || otherIsDefault {
    77  		return false
    78  	}
    79  	if len(currPolicies) != len(otherPolicies) {
    80  		return false
    81  	}
    82  	for i := 0; i < len(currPolicies); i++ {
    83  		if currPolicies[i] != otherPolicies[i] {
    84  			return false
    85  		}
    86  	}
    87  	return true
    88  }
    89  
    90  // String is the representation of staged policies.
    91  func (p StagedPolicies) String() string {
    92  	b, err := json.Marshal(p)
    93  	if err != nil {
    94  		return fmt.Sprintf("[invalid staged policies: %v]", err)
    95  	}
    96  	return string(b)
    97  }
    98  
    99  func (p StagedPolicies) hasDefaultPolicies() bool {
   100  	return IsDefaultPolicies(p.policies)
   101  }
   102  
   103  // MarshalJSON returns the JSON encoding of staged policies.
   104  func (p StagedPolicies) MarshalJSON() ([]byte, error) {
   105  	return json.Marshal(newStagedPoliciesJSON(p))
   106  }
   107  
   108  // UnmarshalJSON unmarshals JSON-encoded data into staged policies.
   109  func (p *StagedPolicies) UnmarshalJSON(data []byte) error {
   110  	var spj stagedPoliciesJSON
   111  	err := json.Unmarshal(data, &spj)
   112  	if err != nil {
   113  		return err
   114  	}
   115  	*p = spj.StagedPolicies()
   116  	return nil
   117  }
   118  
   119  // stagedPoliciesJSON is used for marshaling and unmarshaling staged policies.
   120  type stagedPoliciesJSON struct {
   121  	CutoverNanos int64    `json:"cutoverNanos"`
   122  	Tombstoned   bool     `json:"tombstoned"`
   123  	Policies     []Policy `json:"policies"`
   124  }
   125  
   126  func newStagedPoliciesJSON(sp StagedPolicies) stagedPoliciesJSON {
   127  	return stagedPoliciesJSON{
   128  		CutoverNanos: sp.CutoverNanos,
   129  		Tombstoned:   sp.Tombstoned,
   130  		Policies:     sp.policies,
   131  	}
   132  }
   133  
   134  func (spj stagedPoliciesJSON) StagedPolicies() StagedPolicies {
   135  	return NewStagedPolicies(spj.CutoverNanos, spj.Tombstoned, spj.Policies)
   136  }
   137  
   138  // PoliciesList is a list of staged policies.
   139  type PoliciesList []StagedPolicies
   140  
   141  // IsDefault determines whether this is a default policies list.
   142  func (l PoliciesList) IsDefault() bool {
   143  	return len(l) == 1 && l[0].IsDefault()
   144  }
   145  
   146  // VersionedPoliciesList is a versioned policies list.
   147  type VersionedPoliciesList struct {
   148  	// Version is the version associcated with the policies in the list.
   149  	Version int `json:"version"`
   150  
   151  	// PoliciesList contains the list of staged policies.
   152  	PoliciesList PoliciesList `json:"policiesList"`
   153  }