github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/aggregator/integration/election.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 integration
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/cluster/services"
    27  	"github.com/m3db/m3/src/cluster/services/leader"
    28  
    29  	integration "github.com/m3db/m3/src/integration/resources/docker/dockerexternal/etcdintegration"
    30  	"github.com/stretchr/testify/require"
    31  	clientv3 "go.etcd.io/etcd/client/v3"
    32  )
    33  
    34  var (
    35  	testClusterSize     = 1
    36  	testEnvironment     = "testEnv"
    37  	testServiceName     = "testSvc"
    38  	testZone            = "testZone"
    39  	testElectionTTLSecs = 5
    40  )
    41  
    42  type testCluster struct {
    43  	t             *testing.T
    44  	cluster       *integration.Cluster
    45  	leaderService services.LeaderService
    46  }
    47  
    48  func newTestCluster(t *testing.T) *testCluster {
    49  	integration.BeforeTestExternal(t)
    50  	cluster := &testCluster{
    51  		t: t,
    52  		cluster: integration.NewCluster(t, &integration.ClusterConfig{
    53  			Size: testClusterSize,
    54  		}),
    55  	}
    56  	return cluster
    57  }
    58  
    59  func (tc *testCluster) LeaderService() services.LeaderService {
    60  	if tc.leaderService != nil {
    61  		return tc.leaderService
    62  	}
    63  
    64  	svc, err := leader.NewService(tc.etcdClient(), tc.options())
    65  	require.NoError(tc.t, err)
    66  	tc.leaderService = svc
    67  	return tc.leaderService
    68  }
    69  
    70  func (tc *testCluster) Close() {
    71  	if tc.leaderService != nil {
    72  		// amainsd: check error here!
    73  		_ = tc.leaderService.Close()
    74  	}
    75  	tc.cluster.Terminate(tc.t)
    76  }
    77  
    78  func (tc *testCluster) etcdClient() *clientv3.Client {
    79  	return tc.cluster.RandClient()
    80  }
    81  
    82  func (tc *testCluster) options() leader.Options {
    83  	sid := services.NewServiceID().
    84  		SetEnvironment(testEnvironment).
    85  		SetName(testServiceName).
    86  		SetZone(testZone)
    87  	eopts := services.NewElectionOptions().
    88  		SetTTLSecs(testElectionTTLSecs)
    89  	return leader.NewOptions().
    90  		SetServiceID(sid).
    91  		SetElectionOpts(eopts)
    92  }