Example #1
0
File: command.go Project: h12w/kpax
func (cmd *TailCommand) Exec(cl model.Cluster) error {
	if cmd.Format == "" {
		var err error
		cmd.Format, err = formatDetector{cl, cmd.Topic}.detect()
		if err != nil {
			return err
		}
	}
	var partitions []int32
	if cmd.Partition == -1 {
		var err error
		partitions, err = cl.Partitions(cmd.Topic)
		if err != nil {
			return err
		}
	} else {
		partitions = []int32{int32(cmd.Partition)}
	}
	cr := consumer.New(cl)
	var wg sync.WaitGroup
	wg.Add(len(partitions))
	for _, partition := range partitions {
		start, err := cr.FetchOffsetByTime(cmd.Topic, partition, proto.Earliest)
		if err != nil {
			return err
		}
		end, err := cr.FetchOffsetByTime(cmd.Topic, partition, proto.Latest)
		if err != nil {
			return err
		}
		if start < end-int64(cmd.Count) {
			start = end - int64(cmd.Count)
		}
		go func(partition int32, start, end int64) {
			defer wg.Done()
			offset := start
			for offset < end {
				messages, err := cr.Consume(cmd.Topic, partition, offset)
				if err != nil {
					log.Println(err)
					return
				}
				if len(messages) == 0 {
					break
				}
				for _, msg := range messages {
					line, err := cmd.Format.Sprint(msg.Value)
					if err != nil {
						log.Println(err)
						break
					}
					fmt.Println(line)
				}
				offset = messages[len(messages)-1].Offset + 1
			}
		}(partition, start, end)
	}
	wg.Wait()
	return nil
}
Example #2
0
File: command.go Project: h12w/kpax
func (cmd *RollbackCommand) Exec(cl model.Cluster) error {
	if cmd.Format == "" {
		var err error
		cmd.Format, err = formatDetector{cl, cmd.Topic}.detect()
		if err != nil {
			return err
		}
	}
	partitions, err := cl.Partitions(cmd.Topic)
	if err != nil {
		return err
	}
	cr := consumer.New(cl)
	var wg sync.WaitGroup
	wg.Add(len(partitions))
	timeFunc := cmd.Format.unmarshalTime(cmd.TimeField)
	for _, partition := range partitions {
		go func(partition int32) {
			defer wg.Done()
			offset, err := cr.SearchOffsetByTime(cmd.Topic, partition, time.Time(cmd.Start), timeFunc)
			if err != nil {
				log.Println(err)
				return
			}
			if err := cr.Commit(cmd.Topic, partition, cmd.Group, offset); err != nil {
				log.Println(err)
			}
			fmt.Printf("\t%d:%d\n", partition, offset)
		}(partition)
	}
	wg.Wait()
	return nil

}
Example #3
0
File: command.go Project: h12w/kpax
func (cmd *ConsumeCommand) Exec(cl model.Cluster) error {
	if cmd.Format == "" {
		var err error
		cmd.Format, err = formatDetector{cl, cmd.Topic}.detect()
		if err != nil {
			return err
		}
	}
	partitions, err := cl.Partitions(cmd.Topic)
	if err != nil {
		return err
	}
	cr := consumer.New(cl)
	var wg sync.WaitGroup
	wg.Add(len(partitions))
	var cnt int64
	timeFunc := cmd.Format.unmarshalTime(cmd.TimeField)
	for _, partition := range partitions {
		go func(partition int32) {
			defer wg.Done()
			partCnt, err := cmd.consumePartition(cr, partition, timeFunc)
			if err != nil {
				log.Println(err)
				return
			}
			atomic.AddInt64(&cnt, partCnt)
		}(partition)
	}
	wg.Wait()
	if cmd.Count {
		fmt.Println(cnt)
	}
	return nil
}
Example #4
0
File: command.go Project: h12w/kpax
func (cmd *MetaCommand) Exec(cl model.Cluster) error {
	b, err := cl.Leader(cmd.Topic, int32(cmd.Partition))
	if err != nil {
		return err
	}
	res, err := proto.Metadata(cmd.Topic).Fetch(b)
	if err != nil {
		return err
	}
	fmt.Println(toJSON(res))
	return nil
}
Example #5
0
File: api.go Project: h12w/kpax
func (p *Payload) Produce(c model.Cluster) error {
	leader, err := c.Leader(p.Topic, p.Partition)
	if err != nil {
		return err
	}
	if err := p.DoProduce(leader); err != nil {
		if IsNotLeader(err) {
			c.LeaderIsDown(p.Topic, p.Partition)
		}
		return err
	}
	return nil
}
Example #6
0
File: api.go Project: h12w/kpax
func (o *Offset) Commit(c model.Cluster) error {
	coord, err := c.Coordinator(o.Group)
	if err != nil {
		return err
	}
	if err := o.DoCommit(coord); err != nil {
		if IsNotCoordinator(err) {
			c.CoordinatorIsDown(o.Group)
		}
		return err
	}
	return nil
}
Example #7
0
File: api.go Project: h12w/kpax
func (o *OffsetByTime) Fetch(c model.Cluster) (int64, error) {
	leader, err := c.Leader(o.Topic, o.Partition)
	if err != nil {
		return -1, err
	}
	offset, err := o.DoFetch(leader)
	if err != nil {
		if IsNotLeader(err) {
			c.LeaderIsDown(o.Topic, o.Partition)
		}
		return -1, err
	}
	return offset, nil
}
Example #8
0
File: api.go Project: h12w/kpax
func (o *Offset) Fetch(c model.Cluster) (int64, error) {
	coord, err := c.Coordinator(o.Group)
	if err != nil {
		return -1, err
	}
	offset, err := o.DoFetch(coord)
	if err != nil {
		if IsNotCoordinator(err) {
			c.CoordinatorIsDown(o.Group)
		}
		return -1, err
	}
	return offset, nil
}
Example #9
0
File: api.go Project: h12w/kpax
func (m *Messages) Consume(c model.Cluster) (MessageSet, error) {
	leader, err := c.Leader(m.Topic, m.Partition)
	if err != nil {
		return nil, err
	}
	ms, err := m.DoConsume(leader)
	if err != nil {
		if IsNotLeader(err) {
			c.LeaderIsDown(m.Topic, m.Partition)
		}
		return nil, err
	}
	return ms, nil
}
Example #10
0
File: command.go Project: h12w/kpax
func (cmd *OffsetCommand) Exec(cl model.Cluster) error {
	partitions, err := cl.Partitions(cmd.Topic)
	if err != nil {
		return err
	}
	cr := consumer.New(cl)
	fmt.Printf("topic: %s, group: %s\n", cmd.Topic, cmd.Group)
	for _, partition := range partitions {
		offset := int64(-1)
		var err error
		if cmd.Latest {
			offset, err = cr.FetchOffsetByTime(cmd.Topic, partition, proto.Latest)
		} else if cmd.Earliest {
			offset, err = cr.FetchOffsetByTime(cmd.Topic, partition, proto.Earliest)
		} else {
			offset, err = cr.Offset(cmd.Topic, partition, cmd.Group)
		}
		if err != nil {
			return err
		}
		fmt.Printf("\t%d:%d\n", partition, offset)
	}
	return nil
}