func (self *NsqdCoordinator) updateChannelOffsetOnSlave(tc *coordData, channelName string, offset ChannelConsumerOffset) *CoordErr { topicName := tc.topicInfo.Name partition := tc.topicInfo.Partition if !tc.IsMineISR(self.myNode.GetID()) { return ErrTopicWriteOnNonISR } if coordLog.Level() >= levellogger.LOG_DETAIL { coordLog.Debugf("got update channel(%v) offset on slave : %v", channelName, offset) } coord, coordErr := self.getTopicCoord(topicName, partition) if coordErr != nil { return ErrMissingTopicCoord } topic, localErr := self.localNsqd.GetExistingTopic(topicName, partition) if localErr != nil { coordLog.Warningf("slave missing topic : %v", topicName) // TODO: leave the isr and try re-sync with leader return &CoordErr{localErr.Error(), RpcCommonErr, CoordSlaveErr} } if topic.GetTopicPart() != partition { coordLog.Errorf("topic on slave has different partition : %v vs %v", topic.GetTopicPart(), partition) return ErrLocalMissingTopic } var ch *nsqd.Channel ch, localErr = topic.GetExistingChannel(channelName) // if a new channel on slave, we should set the consume offset by force if localErr != nil { offset.AllowBackward = true ch = topic.GetChannel(channelName) coordLog.Infof("slave init the channel : %v, %v, offset: %v", topic.GetTopicName(), channelName, ch.GetConfirmed()) } if ch.IsEphemeral() { coordLog.Errorf("ephemeral channel %v should not be synced on slave", channelName) } currentEnd := ch.GetChannelEnd() if nsqd.BackendOffset(offset.VOffset) > currentEnd.Offset() { coordLog.Debugf("update channel(%v) consume offset exceed end %v on slave : %v", channelName, offset, currentEnd) // cache the offset (using map?) to reduce the slave channel flush. coord.consumeMgr.Lock() cur, ok := coord.consumeMgr.channelConsumeOffset[channelName] if !ok || cur.VOffset < offset.VOffset { coord.consumeMgr.channelConsumeOffset[channelName] = offset } coord.consumeMgr.Unlock() if offset.Flush { topic.ForceFlush() currentEnd = ch.GetChannelEnd() if nsqd.BackendOffset(offset.VOffset) > currentEnd.Offset() { offset.VOffset = int64(currentEnd.Offset()) offset.VCnt = currentEnd.TotalMsgCnt() } } else { return nil } } err := ch.ConfirmBackendQueueOnSlave(nsqd.BackendOffset(offset.VOffset), offset.VCnt, offset.AllowBackward) if err != nil { coordLog.Warningf("update local channel(%v) offset %v failed: %v, current channel end: %v, topic end: %v", channelName, offset, err, currentEnd, topic.TotalDataSize()) if err == nsqd.ErrExiting { return &CoordErr{err.Error(), RpcNoErr, CoordTmpErr} } return &CoordErr{err.Error(), RpcCommonErr, CoordSlaveErr} } return nil }