github.com/m3db/m3@v1.5.0/src/cmd/services/m3query/config/remote_config.go (about)

     1  // Copyright (c) 2019 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 config
    22  
    23  import (
    24  	"github.com/m3db/m3/src/query/storage"
    25  )
    26  
    27  // Remote is an option for remote storage.
    28  type Remote struct {
    29  	// ErrorBehavior describes what this remote client should do on error.
    30  	ErrorBehavior storage.ErrorBehavior
    31  	// Name is the name for this remote client.
    32  	Name string
    33  	// Addresses are the remote addresses for this client.
    34  	Addresses []string
    35  }
    36  
    37  func makeRemote(
    38  	name string,
    39  	addresses []string,
    40  	global storage.ErrorBehavior,
    41  	override *storage.ErrorBehavior,
    42  ) Remote {
    43  	if override != nil {
    44  		global = *override
    45  	}
    46  
    47  	return Remote{Name: name, Addresses: addresses, ErrorBehavior: global}
    48  }
    49  
    50  // RemoteOptions are the options for RPC configurations.
    51  type RemoteOptions interface {
    52  	// ServeEnabled describes if this RPC should serve rpc requests.
    53  	ServeEnabled() bool
    54  	// ServeAddress describes the address this RPC server will listening on.
    55  	ServeAddress() string
    56  	// ListenEnabled describes if this RPC should connect to remote clients.
    57  	ListenEnabled() bool
    58  	// ReflectionEnabled describes if this RPC server should have reflection
    59  	// enabled.
    60  	ReflectionEnabled() bool
    61  	// Remotes is a list of remote clients.
    62  	Remotes() []Remote
    63  }
    64  
    65  type remoteOptions struct {
    66  	enabled           bool
    67  	reflectionEnabled bool
    68  	address           string
    69  	remotes           []Remote
    70  }
    71  
    72  // RemoteOptionsFromConfig builds remote options given a set of configs.
    73  func RemoteOptionsFromConfig(cfg *RPCConfiguration) RemoteOptions {
    74  	if cfg == nil {
    75  		return &remoteOptions{}
    76  	}
    77  
    78  	// NB: Remote storage enabled should be determined by which options are
    79  	// present unless enabled is explicitly set to false.
    80  	enabled := true
    81  	if cfg.Enabled != nil {
    82  		enabled = *cfg.Enabled
    83  	}
    84  
    85  	// NB: Remote storages warn on failure by default.
    86  	defaultBehavior := storage.BehaviorWarn
    87  	if cfg.ErrorBehavior != nil {
    88  		defaultBehavior = *cfg.ErrorBehavior
    89  	}
    90  
    91  	remotes := make([]Remote, 0, len(cfg.Remotes)+1)
    92  	if len(cfg.RemoteListenAddresses) > 0 {
    93  		remotes = append(remotes, makeRemote("default", cfg.RemoteListenAddresses,
    94  			defaultBehavior, nil))
    95  	}
    96  
    97  	for _, remote := range cfg.Remotes {
    98  		remotes = append(remotes, makeRemote(remote.Name,
    99  			remote.RemoteListenAddresses, defaultBehavior, remote.ErrorBehavior))
   100  	}
   101  
   102  	return &remoteOptions{
   103  		enabled:           enabled,
   104  		reflectionEnabled: cfg.ReflectionEnabled,
   105  		address:           cfg.ListenAddress,
   106  		remotes:           remotes,
   107  	}
   108  }
   109  
   110  func (o *remoteOptions) ServeEnabled() bool {
   111  	return o.enabled && len(o.address) > 0
   112  }
   113  
   114  func (o *remoteOptions) ServeAddress() string {
   115  	return o.address
   116  }
   117  
   118  func (o *remoteOptions) ListenEnabled() bool {
   119  	return o.enabled && len(o.remotes) > 0
   120  }
   121  
   122  func (o *remoteOptions) ReflectionEnabled() bool {
   123  	return o.enabled && o.reflectionEnabled
   124  }
   125  
   126  func (o *remoteOptions) Remotes() []Remote {
   127  	return o.remotes
   128  }