本文整理匯總了Golang中github.com/funkygao/gafka/zk.ZkCluster類的典型用法代碼示例。如果您正苦於以下問題:Golang ZkCluster類的具體用法?Golang ZkCluster怎麽用?Golang ZkCluster使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ZkCluster類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: consumePartition
func (this *WatchAppError) consumePartition(zkcluster *zk.ZkCluster, consumer sarama.Consumer,
topic string, partitionId int32, offset int64, msgCh chan<- *sarama.ConsumerMessage, wg *sync.WaitGroup) {
defer wg.Done()
p, err := consumer.ConsumePartition(topic, partitionId, offset)
if err != nil {
log.Error("%s %s/%d: offset=%d %v", zkcluster.Name(), topic, partitionId, offset, err)
return
}
defer p.Close()
for {
select {
case <-this.Stop:
return
case err := <-p.Errors():
log.Critical("cluster[%s] %s/%d: %s", zkcluster.Name(), topic, partitionId, err)
return
case msg := <-p.Messages():
if msg != nil && this.predicate(msg.Value) {
msgCh <- msg
}
}
}
}
示例2: clusterSummary
func (this *Clusters) clusterSummary(zkcluster *zk.ZkCluster) (brokers, topics, partitions int, flat, cum int64) {
brokerInfos := zkcluster.Brokers()
brokers = len(brokerInfos)
kfk, err := sarama.NewClient(zkcluster.BrokerList(), saramaConfig())
if err != nil {
this.Ui.Error(err.Error())
return
}
defer kfk.Close()
topicInfos, _ := kfk.Topics()
topics = len(topicInfos)
for _, t := range topicInfos {
alivePartitions, _ := kfk.WritablePartitions(t)
partitions += len(alivePartitions)
for _, partitionID := range alivePartitions {
latestOffset, _ := kfk.GetOffset(t, partitionID, sarama.OffsetNewest)
oldestOffset, _ := kfk.GetOffset(t, partitionID, sarama.OffsetOldest)
flat += (latestOffset - oldestOffset)
cum += latestOffset
}
}
return
}
示例3: consumeCluster
func (this *Peek) consumeCluster(zkcluster *zk.ZkCluster, topicPattern string,
partitionId int, msgChan chan *sarama.ConsumerMessage) {
brokerList := zkcluster.BrokerList()
if len(brokerList) == 0 {
return
}
kfk, err := sarama.NewClient(brokerList, sarama.NewConfig())
if err != nil {
this.Ui.Output(err.Error())
return
}
//defer kfk.Close() // FIXME how to close it
topics, err := kfk.Topics()
if err != nil {
this.Ui.Output(err.Error())
return
}
for _, t := range topics {
if patternMatched(t, topicPattern) {
go this.simpleConsumeTopic(zkcluster, kfk, t, int32(partitionId), msgChan)
}
}
}
示例4: makePub
func (this *Mirror) makePub(c2 *zk.ZkCluster) (sarama.AsyncProducer, error) {
cf := sarama.NewConfig()
cf.Metadata.RefreshFrequency = time.Minute * 10
cf.Metadata.Retry.Max = 3
cf.Metadata.Retry.Backoff = time.Second * 3
cf.ChannelBufferSize = 1000
cf.Producer.Return.Errors = true
cf.Producer.Flush.Messages = 2000 // 2000 message in batch
cf.Producer.Flush.Frequency = time.Second // flush interval
cf.Producer.Flush.MaxMessages = 0 // unlimited
cf.Producer.RequiredAcks = sarama.WaitForLocal
cf.Producer.Retry.Backoff = time.Second * 4
cf.Producer.Retry.Max = 3
cf.Net.DialTimeout = time.Second * 30
cf.Net.WriteTimeout = time.Second * 30
cf.Net.ReadTimeout = time.Second * 30
switch this.Compress {
case "gzip":
cf.Producer.Compression = sarama.CompressionGZIP
case "snappy":
cf.Producer.Compression = sarama.CompressionSnappy
}
return sarama.NewAsyncProducer(c2.BrokerList(), cf)
}
示例5: resetTopicConfig
func (this *Topics) resetTopicConfig(zkcluster *zk.ZkCluster, topic string) {
zkAddrs := zkcluster.ZkConnectAddr()
key := "retention.ms"
cmd := pipestream.New(fmt.Sprintf("%s/bin/kafka-topics.sh", ctx.KafkaHome()),
fmt.Sprintf("--zookeeper %s", zkAddrs),
fmt.Sprintf("--alter"),
fmt.Sprintf("--topic %s", topic),
fmt.Sprintf("--deleteConfig %s", key),
)
err := cmd.Open()
swallow(err)
defer cmd.Close()
scanner := bufio.NewScanner(cmd.Reader())
scanner.Split(bufio.ScanLines)
output := make([]string, 0)
for scanner.Scan() {
output = append(output, scanner.Text())
}
swallow(scanner.Err())
path := zkcluster.GetTopicConfigPath(topic)
this.Ui.Info(path)
for _, line := range output {
this.Ui.Output(line)
}
}
示例6: consumePartition
func (this *Peek) consumePartition(zkcluster *zk.ZkCluster, kfk sarama.Client, consumer sarama.Consumer,
topic string, partitionId int32, msgCh chan *sarama.ConsumerMessage, offset int64) {
p, err := consumer.ConsumePartition(topic, partitionId, offset)
if err != nil {
this.Ui.Error(fmt.Sprintf("%s %s/%d: offset=%d %v", zkcluster.Name(), topic, partitionId, offset, err))
return
}
defer p.Close()
n := int64(0)
for {
select {
case <-this.quit:
return
case msg := <-p.Messages():
msgCh <- msg
n++
if this.lastN > 0 && n >= this.lastN {
return
}
}
}
}
示例7: clusterTopConsumers
func (this *Top) clusterTopConsumers(zkcluster *zk.ZkCluster) {
var topic string
for {
total := zkcluster.TotalConsumerOffsets(this.topicPattern)
if this.topicPattern != "" {
topic = this.topicPattern
} else {
topic = "-all-"
}
key := zkcluster.Name() + ":" + topic
this.mu.Lock()
this.consumerCounters[key] = float64(total)
this.mu.Unlock()
if !this.dashboardGraph {
this.mu.Lock()
this.counters[key] = float64(total)
this.mu.Unlock()
}
time.Sleep(this.topInterval)
}
}
示例8: addTopic
func (this *Topics) addTopic(zkcluster *zk.ZkCluster, topic string, replicas,
partitions int) error {
this.Ui.Info(fmt.Sprintf("creating kafka topic: %s", topic))
ts := sla.DefaultSla()
ts.Partitions = partitions
ts.Replicas = replicas
lines, err := zkcluster.AddTopic(topic, ts)
if err != nil {
return err
}
for _, l := range lines {
this.Ui.Output(color.Yellow(l))
}
if this.ipInNumber {
this.Ui.Output(fmt.Sprintf("\tzookeeper.connect: %s", zkcluster.ZkConnectAddr()))
this.Ui.Output(fmt.Sprintf("\t broker.list: %s",
strings.Join(zkcluster.BrokerList(), ",")))
} else {
this.Ui.Output(fmt.Sprintf("\tzookeeper.connect: %s", zkcluster.NamedZkConnectAddr()))
this.Ui.Output(fmt.Sprintf("\t broker.list: %s",
strings.Join(zkcluster.NamedBrokerList(), ",")))
}
return nil
}
示例9: makePub
func (this *Mirror) makePub(c2 *zk.ZkCluster) (sarama.AsyncProducer, error) {
// TODO setup batch size
cf := sarama.NewConfig()
switch this.compress {
case "gzip":
cf.Producer.Compression = sarama.CompressionGZIP
case "snappy":
cf.Producer.Compression = sarama.CompressionSnappy
}
return sarama.NewAsyncProducer(c2.BrokerList(), cf)
}
示例10: makeSub
func (this *Mirror) makeSub(c1 *zk.ZkCluster, group string, topics []string) (*consumergroup.ConsumerGroup, error) {
cf := consumergroup.NewConfig()
cf.Zookeeper.Chroot = c1.Chroot()
cf.Offsets.CommitInterval = time.Second * 10
cf.Offsets.ProcessingTimeout = time.Second
cf.ChannelBufferSize = 0
cf.Consumer.Return.Errors = true
cf.Consumer.MaxProcessingTime = 100 * time.Millisecond // chan recv timeout
sub, err := consumergroup.JoinConsumerGroup(group, topics, c1.ZkZone().ZkAddrList(), cf)
return sub, err
}
示例11: makeSub
func (this *Mirror) makeSub(c1 *zk.ZkCluster, group string, topics []string) (*consumergroup.ConsumerGroup, error) {
cf := consumergroup.NewConfig()
cf.Zookeeper.Chroot = c1.Chroot()
cf.Offsets.CommitInterval = time.Second * 10
cf.Offsets.ProcessingTimeout = time.Second
cf.Consumer.Offsets.Initial = sarama.OffsetOldest
cf.ChannelBufferSize = 256
cf.Consumer.Return.Errors = true
cf.OneToOne = false
sub, err := consumergroup.JoinConsumerGroup(group, topics, c1.ZkZone().ZkAddrList(), cf)
return sub, err
}
示例12: delTopic
func (this *Topics) delTopic(zkcluster *zk.ZkCluster, topic string) error {
this.Ui.Info(fmt.Sprintf("deleting kafka topic: %s", topic))
lines, err := zkcluster.DeleteTopic(topic)
if err != nil {
return err
}
for _, l := range lines {
this.Ui.Output(color.Yellow(l))
}
return nil
}
示例13: clusterTopProducers
func (this *TopBroker) clusterTopProducers(zkcluster *zk.ZkCluster) {
kfk, err := sarama.NewClient(zkcluster.BrokerList(), sarama.NewConfig())
if err != nil {
return
}
defer kfk.Close()
for {
hostOffsets := make(map[string]int64)
topics, err := kfk.Topics()
swallow(err)
<-this.signalsCh[zkcluster.Name()]
for _, topic := range topics {
if !patternMatched(topic, this.topic) {
continue
}
partions, err := kfk.WritablePartitions(topic)
swallow(err)
for _, partitionID := range partions {
leader, err := kfk.Leader(topic, partitionID)
swallow(err)
latestOffset, err := kfk.GetOffset(topic, partitionID,
sarama.OffsetNewest)
swallow(err)
host, _, err := net.SplitHostPort(leader.Addr())
swallow(err)
if this.shortIp {
host = shortIp(host)
}
if _, present := hostOffsets[host]; !present {
hostOffsets[host] = 0
}
hostOffsets[host] += latestOffset
}
}
this.hostOffsetCh <- hostOffsets
kfk.RefreshMetadata(topics...)
}
}
示例14: printCluster
func (this *LsZk) printCluster(zkcluster *zk.ZkCluster) {
this.Ui.Output(color.Green(zkcluster.Name()))
children, err := zkcluster.ListChildren(this.recursive)
if err != nil {
this.Ui.Error(fmt.Sprintf("%s%s", strings.Repeat(" ", 4), err))
return
}
for _, c := range children {
this.Ui.Output(fmt.Sprintf("%s%s", strings.Repeat(" ", 4), c))
if strings.HasSuffix(c, "brokers") {
this.Ui.Output(fmt.Sprintf("%s%s/ids", strings.Repeat(" ", 4), c))
this.Ui.Output(fmt.Sprintf("%s%s/topics", strings.Repeat(" ", 4), c))
}
}
}
示例15: makeMirror
func (this *Mirror) makeMirror(c1, c2 *zk.ZkCluster) {
pub, err := this.makePub(c2)
swallow(err)
topics, topicsChanges, err := c1.WatchTopics()
swallow(err)
log.Printf("topics: %+v", topics)
if len(topics) == 0 {
log.Println("empty topics")
return
}
group := fmt.Sprintf("%s.%s._mirror_", c1.Name(), c2.Name())
sub, err := this.makeSub(c1, group, topics)
swallow(err)
pumpStopper := make(chan struct{})
go this.pump(sub, pub, pumpStopper)
LOOP:
for {
select {
case <-topicsChanges:
log.Println("topics changed, stopping pump...")
pumpStopper <- struct{}{} // stop pump
<-pumpStopper // await pump cleanup
// refresh c1 topics
topics, err = c1.Topics()
if err != nil {
// TODO how to handle this err?
log.Println(err)
}
log.Printf("topics: %+v", topics)
sub, err = this.makeSub(c1, group, topics)
if err != nil {
// TODO how to handle this err?
log.Println(err)
}
go this.pump(sub, pub, pumpStopper)
case <-this.quit:
log.Println("awaiting pump cleanup...")
<-pumpStopper
log.Printf("total transferred: %s %smsgs",
gofmt.ByteSize(this.transferBytes),
gofmt.Comma(this.transferN))
break LOOP
}
}
pub.Close()
}