| 101 | } |
| 102 | |
| 103 | func (s *Topom) SlotCreateActionRange(beg, end int, gid int, must bool) error { |
| 104 | s.mu.Lock() |
| 105 | defer s.mu.Unlock() |
| 106 | ctx, err := s.newContext() |
| 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | |
| 111 | if !(beg >= 0 && beg <= end && end < MaxSlotNum) { |
| 112 | return errors.Errorf("invalid slot range [%d,%d]", beg, end) |
| 113 | } |
| 114 | |
| 115 | g, err := ctx.getGroup(gid) |
| 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | if len(g.Servers) == 0 { |
| 120 | return errors.Errorf("group-[%d] is empty", g.Id) |
| 121 | } |
| 122 | |
| 123 | var pending []int |
| 124 | for sid := beg; sid <= end; sid++ { |
| 125 | m, err := ctx.getSlotMapping(sid) |
| 126 | if err != nil { |
| 127 | return err |
| 128 | } |
| 129 | if m.Action.State != models.ActionNothing { |
| 130 | if !must { |
| 131 | continue |
| 132 | } |
| 133 | return errors.Errorf("slot-[%d] action already exists", sid) |
| 134 | } |
| 135 | if m.GroupId == g.Id { |
| 136 | if !must { |
| 137 | continue |
| 138 | } |
| 139 | return errors.Errorf("slot-[%d] already in group-[%d]", sid, g.Id) |
| 140 | } |
| 141 | pending = append(pending, m.Id) |
| 142 | } |
| 143 | |
| 144 | for _, sid := range pending { |
| 145 | m, err := ctx.getSlotMapping(sid) |
| 146 | if err != nil { |
| 147 | return err |
| 148 | } |
| 149 | defer s.dirtySlotsCache(m.Id) |
| 150 | |
| 151 | m.Action.State = models.ActionPending |
| 152 | m.Action.Index = ctx.maxSlotActionIndex() + 1 |
| 153 | m.Action.TargetId = g.Id |
| 154 | if err := s.storeUpdateSlotMapping(m); err != nil { |
| 155 | return err |
| 156 | } |
| 157 | } |
| 158 | return nil |
| 159 | } |
| 160 | |