Esempio n. 1
0
func NewManager(zkAddrs []string, kafkaRoot string, conf *sarama.Config) (*Manager, error) {

	if kafkaRoot == "/" {
		kafkaRoot = ""
	}

	zkClient, err := zookeeper.NewZkClient(zkAddrs)
	if err != nil {
		return nil, errors.Trace(err)
	}

	brokerAddrs, brokersList, err := getBrokerAddrs(zkClient, kafkaRoot)
	if err != nil {
		return nil, errors.Trace(err)
	}

	client, err := sarama.NewClient(brokerAddrs, conf)
	if err != nil {
		return nil, errors.Trace(err)
	}

	manager := &Manager{
		client:      client,
		zkClient:    zkClient,
		kafkaRoot:   kafkaRoot,
		brokerAddrs: brokerAddrs,
		brokersList: brokersList,
	}

	err = manager.RefreshMetadata()
	if err != nil {
		return nil, errors.Trace(err)
	}

	return manager, nil
}
Esempio n. 2
0
func NewMetadata(config *config.Config, sconfig *sarama.Config) (*Metadata, error) {
	zkClient, err := zookeeper.NewZkClient(strings.Split(config.MetaDataZKAddr, ","))
	if err != nil {
		return nil, errors.Trace(err)
	}

	zkRoot := config.MetaDataZKRoot
	if strings.EqualFold(zkRoot, root) {
		zkRoot = ""
	}

	groupConfigPath := fmt.Sprintf("%s%s", zkRoot, groupConfigPathSuffix)
	queuePath := fmt.Sprintf("%s%s", zkRoot, queuePathSuffix)
	servicePath := fmt.Sprintf("%s%s", zkRoot, servicePathPrefix)
	operationPath := fmt.Sprintf("%s%s", zkRoot, operationPathPrefix)

	err = zkClient.CreateRec(groupConfigPath, "", 0)
	if err != nil && err != zk.ErrNodeExists {
		return nil, errors.Trace(err)
	}

	err = zkClient.CreateRec(queuePath, "", 0)
	if err != nil && err != zk.ErrNodeExists {
		return nil, errors.Trace(err)
	}

	err = zkClient.CreateRec(servicePath, "", 0)
	if err != nil && err != zk.ErrNodeExists {
		return nil, errors.Trace(err)
	}

	err = zkClient.CreateRec(operationPath, "", 0)
	if err != nil && err != zk.ErrNodeExists {
		return nil, errors.Trace(err)
	}

	manager, err := kafka.NewManager(strings.Split(config.KafkaZKAddr, ","), config.KafkaZKRoot, sconfig)
	if err != nil {
		return nil, errors.Trace(err)
	}

	metadata := &Metadata{
		config:          config,
		zkClient:        zkClient,
		manager:         manager,
		groupConfigPath: groupConfigPath,
		queuePath:       queuePath,
		servicePath:     servicePath,
		operationPath:   operationPath,
		queueConfigs:    make(map[string]QueueConfig),
		closeCh:         make(chan struct{}),
	}

	err = metadata.RefreshMetadata()
	if err != nil {
		return nil, errors.Trace(err)
	}

	go func(m *Metadata) {
		timeout := time.NewTicker(sconfig.Metadata.RefreshFrequency)
		for {
			select {
			case <-timeout.C:
				err := m.RefreshMetadata()
				if err != nil {
					log.Warnf("timeout refresh metadata err : %s", err)
				}
			case <-m.closeCh:
				timeout.Stop()
				return
			}
		}
	}(metadata)

	return metadata, nil
}