func TestCanMarkSentinelsAlive(t *testing.T) {

	state := NewSentinelState(func(_ types.Sentinel) {})
	sentinel := types.Sentinel{Host: "10.1.1.2", Port: 12345}
	sentinelMessage := SentinelAdded{Sentinel: sentinel}
	sentinelPingMessage := SentinelPing{Sentinel: sentinel}
	state.Notify(&sentinelMessage)
	state.Notify(&sentinelPingMessage)

	responseChannel := make(chan SentinelTopology)
	state.GetState(TopologyRequest{ReplyChannel: responseChannel})

	topologyState := <-responseChannel

	if len(topologyState.Sentinels) != 1 {
		t.Error("Topology count should be 1")
	}

	sen, _ := topologyState.FindSentinelInfo(sentinel)

	if sen.SentinelLocation != sentinel.GetLocation() {
		t.Error("Wrong host found")
	}

	if sen.State != SentinelMarkedAlive {
		t.Error("Sentinel in wrong state")
	}
}
func TestCanAddCLustersToSentinel(t *testing.T) {

	state := NewSentinelState(func(_ types.Sentinel) {})
	sentinel := types.Sentinel{Host: "10.1.1.2", Port: 12345}

	sentinelMessage := SentinelAdded{Sentinel: sentinel}
	sentinelInfoMessage := SentinelClustersMonitoredUpdate{Sentinel: sentinel, Clusters: []string{"A"}}

	state.Notify(&sentinelMessage)
	state.Notify(&sentinelInfoMessage)

	responseChannel := make(chan SentinelTopology)
	state.GetState(TopologyRequest{ReplyChannel: responseChannel})

	topologyState := <-responseChannel

	if len(topologyState.Sentinels) != 1 {
		t.Error("Topology count should be 1")
	}

	sen, _ := topologyState.FindSentinelInfo(sentinel)

	if sen.SentinelLocation != sentinel.GetLocation() {
		t.Error("Wrong host found")
	}

	if len(sen.Clusters) != 1 {
		t.Error("Wrong number of clusters")
	}

	if sen.Clusters[0] != "A" {
		t.Error("Wrong cluster found")
	}
}
func TestCanMarkSentinelsDown(t *testing.T) {

	state := NewSentinelState(func(_ types.Sentinel) {})
	sentinel := types.Sentinel{Host: "10.1.1.2", Port: 12345}

	sentinelAddedMessage := &SentinelAdded{Sentinel: sentinel}
	sentinelLostMessage := &SentinelLost{Sentinel: sentinel}

	responseChannel := make(chan SentinelTopology)

	state.Notify(sentinelAddedMessage)
	state.Notify(sentinelLostMessage)

	state.GetState(TopologyRequest{ReplyChannel: responseChannel})
	topologyState := <-responseChannel
	fmt.Print(util.String(topologyState))

	if len(topologyState.Sentinels) != 1 {
		t.Error("Topology count should be 1")
	}

	sen, _ := topologyState.FindSentinelInfo(sentinel)

	if sen.SentinelLocation != sentinel.GetLocation() {
		t.Error("Wrong host found")
	}

	if sen.State != SentinelMarkedDown {
		t.Errorf("Sentinel in wrong state : %d", sen.State)
	}
}
Beispiel #4
0
func NewMonitor(sentinel types.Sentinel, manager Manager, redisConnection redis.RedisConnection) (*Monitor, error) {

	uri := sentinel.GetLocation()

	channel := make(chan redis.RedisPubSubReply)
	pubSubClient, err := redis.NewPubSubClient(uri, channel, redisConnection)

	if err != nil {
		return nil, err
	}

	client, err := redis.NewSentinelClient(sentinel, redisConnection)

	if err != nil {
		return nil, err
	}

	monitor := &Monitor{pubSubClient: pubSubClient,
		client:          client,
		channel:         channel,
		manager:         manager,
		sentinel:        sentinel,
		redisConnection: redisConnection}
	return monitor, nil
}
Beispiel #5
0
func (m *SentinelManager) startNewMonitor(sentinel types.Sentinel) {

	monitor, err := NewMonitor(sentinel, m, m.redisConnection)

	if err != nil {
		logger.Error.Printf("Error starting monitor %s : %s", sentinel.GetLocation(), err.Error())
		m.Notify(&SentinelLost{Sentinel: sentinel})
		return
	}

	err = monitor.StartMonitoringMasterEvents(m.switchmasterchannel)

	if err != nil {
		logger.Error.Printf("Error starting monitoring events %s : %s", sentinel.GetLocation(), err.Error())
		m.Notify(&SentinelLost{Sentinel: sentinel})
	}
}
func NewSentinelClient(sentinel types.Sentinel, redisConnection RedisConnection) (*SentinelClient, error) {

	uri := sentinel.GetLocation()

	redisclient, err := redisConnection.GetConnection("tcp", uri)

	if err != nil {
		logger.Info.Printf("SentinelClient : not connected to %s, %s", uri, err.Error())
		return nil, err
	}

	client := &SentinelClient{
		redisClient: redisclient,
		sentinel:    sentinel,
	}

	return client, nil
}
Beispiel #7
0
func (topology SentinelTopology) createKey(sentinel types.Sentinel) string {
	return sentinel.GetLocation()
}