Exemplo n.º 1
0
func main() {
	flag.Parse()

	ch, err := tchannel.NewChannel("ping", nil)
	if err != nil {
		log.Fatalf("channel did not create successfully: %v", err)
	}

	logger := log.StandardLogger()

	worker := &worker{
		channel: ch,
		ringpop: ringpop.NewRingpop("ping-app", *hostport, ch, &ringpop.Options{
			Logger: bark.NewLoggerFromLogrus(logger),
		}),
		logger: logger,
	}

	if err := worker.RegisterPong(); err != nil {
		log.Fatalf("could not register pong handler: %v", err)
	}

	if err := worker.channel.ListenAndServe(*hostport); err != nil {
		log.Fatalf("could not listen on given hostport: %v", err)
	}

	opts := new(ringpop.BootstrapOptions)
	opts.File = *hostfile

	if _, err := worker.ringpop.Bootstrap(opts); err != nil {
		log.Fatalf("ringpop bootstrap failed: %v", err)
	}

	select {}
}
Exemplo n.º 2
0
func TestGetFields(t *testing.T) {
	var logger bark.Logger

	// Plain logger
	logger = bark.NewLoggerFromLogrus(logrus.New())
	require.Equal(t, logger.Fields(), bark.Fields(nil))

	// Add nil, don't crash
	logger = bark.NewLoggerFromLogrus(logrus.New())
	logger = logger.WithFields(nil)
	require.Equal(t, logger.Fields(), bark.Fields(nil))

	// One field added
	logger = bark.NewLoggerFromLogrus(logrus.New())
	logger = logger.WithField("foo", "bar")
	require.Equal(t, logger.Fields(), bark.Fields{"foo": "bar"})

	// Two fields added at once
	logger = bark.NewLoggerFromLogrus(logrus.New())
	logger = logger.WithFields(bark.Fields{"foo": "bar", "baz": "bump"})
	require.Equal(t, logger.Fields(), bark.Fields{"foo": "bar", "baz": "bump"})

	// One then one
	logger = bark.NewLoggerFromLogrus(logrus.New())
	logger = logger.WithField("foo", "bar")
	logger = logger.WithField("baz", "bump")
	require.Equal(t, logger.Fields(), bark.Fields{"foo": "bar", "baz": "bump"})

	// Two then one
	logger = bark.NewLoggerFromLogrus(logrus.New())
	logger = logger.WithFields(bark.Fields{"foo": "bar", "baz": "bump"})
	logger = logger.WithField("x", "y")
	require.Equal(t, logger.Fields(), bark.Fields{"foo": "bar", "baz": "bump", "x": "y"})

	// One then two
	logger = bark.NewLoggerFromLogrus(logrus.New())
	logger = logger.WithField("x", "y")
	logger = logger.WithFields(bark.Fields{"foo": "bar", "baz": "bump"})
	require.Equal(t, logger.Fields(), bark.Fields{"foo": "bar", "baz": "bump", "x": "y"})

	// Two then two
	logger = bark.NewLoggerFromLogrus(logrus.New())
	logger = logger.WithFields(bark.Fields{"foo": "bar", "baz": "bump"})
	logger = logger.WithFields(bark.Fields{"a": "b", "c": "d"})
	require.Equal(t, logger.Fields(), bark.Fields{"foo": "bar", "baz": "bump", "a": "b", "c": "d"})

	// Add empty map
	logger = bark.NewLoggerFromLogrus(logrus.New())
	logger = logger.WithFields(bark.Fields{})
	require.Equal(t, logger.Fields(), bark.Fields{})
}
Exemplo n.º 3
0
func newWorker(address string, channel *tchannel.Channel) *worker {
	logger := bark.NewLoggerFromLogrus(logrus.StandardLogger())

	return &worker{
		address: address,
		ringpop: ringpop.NewRingpop("pingpong", address, channel, &ringpop.Options{
			Logger: logger,
		}),
	}
}
Exemplo n.º 4
0
// NewForwarder returns a new forwarder
func NewForwarder(s Sender, ch tchannel.Registrar, logger log.Logger) *Forwarder {
	if logger == nil {
		logger = log.NewLoggerFromLogrus(&logrus.Logger{
			Out: ioutil.Discard,
		})
	}

	return &Forwarder{
		sender:  s,
		channel: ch,
		logger:  logger,
	}
}
Exemplo n.º 5
0
func defaultOptions() *Options {
	logger := log.NewLoggerFromLogrus(&logrus.Logger{
		Out: ioutil.Discard,
	})

	opts := &Options{
		Logger:        logger,
		Statter:       new(noopStatsReporter),
		ReplicaPoints: 100,
	}

	return opts
}
Exemplo n.º 6
0
// NewReplicator returns a new Replicator instance that makes calls with the given
// SubChannel to the service defined by SubChannel.GetServiceName(). The given n/w/r
// values will be used as defaults for the replicator when none are provided
func NewReplicator(s Sender, channel tchannel.Registrar, logger log.Logger,
	opts *Options) *Replicator {

	if logger == nil {
		logger = log.NewLoggerFromLogrus(&logrus.Logger{
			Out: ioutil.Discard,
		})
	}

	f := forward.NewForwarder(s, channel, logger)

	opts = mergeDefaultOptions(opts, &Options{3, 1, 3, Parallel})
	return &Replicator{s, channel, f, logger, opts}
}
Exemplo n.º 7
0
func main() {
	var logrusLogger *logrus.Logger = logrus.New()
	logrusLogger.Formatter = new(logrus.JSONFormatter)

	if len(os.Args) != 2 {
		logrus.Error("Must pass an arg to test program...")
		os.Exit(0)
	}

	switch os.Args[1] {
	case "logrus.Fatal":
		logrusLogger.Fatal("fatal error")
	case "logrus.Fatalf":
		logrusLogger.Fatalf("fatal error%s", "fatal error")
	case "bark.Fatal":
		bark.NewLoggerFromLogrus(logrusLogger).Fatal("fatal error")
	case "bark.Fatalf":
		bark.NewLoggerFromLogrus(logrusLogger).Fatalf("fatal error%s", "fatal error")
	}

	logrus.Error("Expected fatal methods to exit...")
	os.Exit(0)
}
Exemplo n.º 8
0
func newWorker(address string, channel *tchannel.Channel) *worker {
	logger := bark.NewLoggerFromLogrus(logrus.StandardLogger())

	rp, err := ringpop.New("pingpong",
		ringpop.Channel(channel),
		ringpop.Identity(address),
		ringpop.Logger(logger),
	)
	if err != nil {
		log.Fatalf("Unable to create Ringpop: %v", err)
	}

	return &worker{
		address: address,
		ringpop: rp,
	}
}
Exemplo n.º 9
0
func main() {
	flag.Parse()

	ch, err := tchannel.NewChannel("ping", nil)
	if err != nil {
		log.Fatalf("channel did not create successfully: %v", err)
	}

	logger := log.StandardLogger()

	rp, err := ringpop.New("ping-app",
		ringpop.Channel(ch),
		ringpop.Identity(*hostport),
		ringpop.Logger(bark.NewLoggerFromLogrus(logger)),
	)
	if err != nil {
		log.Fatalf("Unable to create Ringpop: %v", err)
	}

	worker := &worker{
		channel: ch,
		ringpop: rp,
		logger:  logger,
	}

	if err := worker.RegisterPong(); err != nil {
		log.Fatalf("could not register pong handler: %v", err)
	}

	if err := worker.channel.ListenAndServe(*hostport); err != nil {
		log.Fatalf("could not listen on given hostport: %v", err)
	}

	opts := new(swim.BootstrapOptions)
	opts.File = *hostfile

	if _, err := worker.ringpop.Bootstrap(opts); err != nil {
		log.Fatalf("ringpop bootstrap failed: %v", err)
	}

	select {}
}
Exemplo n.º 10
0
func createCluster(hostport string, seednodes []string, app clusterApp) *cluster {
	channel, err := tchannel.NewChannel("kbase", nil)
	if err != nil {
		log.Fatalf("channel did not create successfully: %v", err)
	}

	ringpop, err := ringpop.New("kbase-app",
		ringpop.Channel(channel),
		ringpop.Identity(hostport),
		ringpop.Logger(bark.NewLoggerFromLogrus(log)),
	)
	if err != nil {
		log.Fatalf("Unable to create Ringpop: %v", err)
	}

	if err := channel.ListenAndServe(hostport); err != nil {
		log.Fatalf("could not listen on given hostport: %v", err)
	}

	opts := new(swim.BootstrapOptions)
	if len(seednodes) > 0 {
		opts.Hosts = seednodes
	}

	if _, err := ringpop.Bootstrap(opts); err != nil {
		log.Fatalf("ringpop bootstrap failed: %v", err)
	}

	cluster := &cluster{
		/*ring: Ring{
			hcodes: []int{},
			servers: make(map[int]string),
		},*/
		ringpop: ringpop,
		channel: channel,
		app:     app}

	ringpop.RegisterListener(cluster)

	return cluster
}
Exemplo n.º 11
0
func main() {
	verbose := flag.Bool("verbose", false, "enable debug level logging")
	flag.Parse()

	if !hostportPattern.MatchString(*hostport) {
		log.Fatalf("bad hostport: %s", *hostport)
	}

	ch, err := tchannel.NewChannel("ringpop", nil)
	if err != nil {
		log.Fatalf("could not create channel: %v", err)
	}

	logger := log.StandardLogger()
	if *verbose {
		logger.Level = log.DebugLevel
	}
	rp, _ := ringpop.New("ringpop",
		ringpop.Channel(ch),
		ringpop.Identity(*hostport),
		ringpop.Logger(bark.NewLoggerFromLogrus(logger)),
	)

	if err := ch.ListenAndServe(*hostport); err != nil {
		log.Fatalf("could not listen on %s: %v", *hostport, err)
	}

	opts := &swim.BootstrapOptions{}
	opts.File = *hostfile

	_, err = rp.Bootstrap(opts)
	if err != nil {
		log.Fatalf("bootstrap failed: %v", err)
	}

	// block
	select {}
}
Exemplo n.º 12
0
func defaultOptions() *Options {
	opts := &Options{
		BootstrapFile: "./hosts.json",

		SuspicionTimeout:  5000 * time.Millisecond,
		MinProtocolPeriod: 200 * time.Millisecond,

		JoinTimeout:        1000 * time.Millisecond,
		PingTimeout:        1500 * time.Millisecond,
		PingRequestTimeout: 5000 * time.Millisecond,

		PingRequestSize: 3,

		RollupFlushInterval: 5000 * time.Millisecond,
		RollupMaxUpdates:    250,

		Logger: log.NewLoggerFromLogrus(&logrus.Logger{
			Out: ioutil.Discard,
		}),
	}

	return opts
}
Exemplo n.º 13
0
// Create a bark wrapper for a logrus logger backed by a buffer
func getBarkLogger() (bark.Logger, *bytes.Buffer) {
	logrusLogger, buffer := getLogrusLogger()
	return bark.NewLoggerFromLogrus(logrusLogger), buffer
}