MCPcopy
hub / github.com/CodisLabs/codis / trySwitchGroupMaster

Method trySwitchGroupMaster

pkg/topom/topom_group.go:347–385  ·  view source on GitHub ↗
(gid int, master string, cache *redis.InfoCache)

Source from the content-addressed store, hash-verified

345}
346
347func (s *Topom) trySwitchGroupMaster(gid int, master string, cache *redis.InfoCache) error {
348 ctx, err := s.newContext()
349 if err != nil {
350 return err
351 }
352 g, err := ctx.getGroup(gid)
353 if err != nil {
354 return err
355 }
356
357 var index = func() int {
358 for i, x := range g.Servers {
359 if x.Addr == master {
360 return i
361 }
362 }
363 for i, x := range g.Servers {
364 rid1 := cache.GetRunId(master)
365 rid2 := cache.GetRunId(x.Addr)
366 if rid1 != "" && rid1 == rid2 {
367 return i
368 }
369 }
370 return -1
371 }()
372 if index == -1 {
373 return errors.Errorf("group-[%d] doesn't have server %s with runid = '%s'", g.Id, master, cache.GetRunId(master))
374 }
375 if index == 0 {
376 return nil
377 }
378 defer s.dirtyGroupCache(g.Id)
379
380 log.Warnf("group-[%d] will switch master to server[%d] = %s", g.Id, index, g.Servers[index].Addr)
381
382 g.Servers[0], g.Servers[index] = g.Servers[index], g.Servers[0]
383 g.OutOfSync = true
384 return s.storeUpdateGroup(g)
385}
386
387func (s *Topom) EnableReplicaGroups(gid int, addr string, value bool) error {
388 s.mu.Lock()

Callers 1

SwitchMastersMethod · 0.95

Calls 7

newContextMethod · 0.95
dirtyGroupCacheMethod · 0.95
storeUpdateGroupMethod · 0.95
getGroupMethod · 0.80
GetRunIdMethod · 0.80
ErrorfMethod · 0.80
WarnfMethod · 0.80

Tested by

no test coverage detected