Example #1
0
func (c *ScribeCollector) newConnection() (scribe.Scribe, error) {
	addr, err := net.ResolveTCPAddr("tcp", c.addr)
	if err != nil {
		return nil, err
	}
	socket := thrift.NewTSocketFromAddrTimeout(addr, timeout)
	transport := thrift.NewTFramedTransport(socket)
	if err := transport.Open(); err != nil {
		return nil, err
	}
	proto := thrift.NewTBinaryProtocolTransport(transport)
	s := scribe.NewScribeClientProtocol(transport, proto, proto)
	return s, nil
}
Example #2
0
func scribeClientFactory(addr string, timeout time.Duration) func() (scribe.Scribe, error) {
	return func() (scribe.Scribe, error) {
		a, err := net.ResolveTCPAddr("tcp", addr)
		if err != nil {
			return nil, err
		}
		socket := thrift.NewTSocketFromAddrTimeout(a, timeout)
		transport := thrift.NewTFramedTransport(socket)
		if err := transport.Open(); err != nil {
			socket.Close()
			return nil, err
		}
		proto := thrift.NewTBinaryProtocolTransport(transport)
		client := scribe.NewScribeClientProtocol(transport, proto, proto)
		return client, nil
	}
}
Example #3
0
func newConnection(node, keyspace string, timeout int, authentication map[string]string) (*connection, error) {
	addr, err := net.ResolveTCPAddr("tcp", node)
	if err != nil {
		return nil, err
	}

	c := &connection{node: node}

	c.socket = thrift.NewTSocketFromAddrTimeout(addr, 0)

	// socket not open yet, so no error expected.
	c.socket.SetTimeout(time.Duration(timeout) * time.Millisecond)

	c.transport = thrift.NewTFramedTransport(c.socket)
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
	c.client = cassandra.NewCassandraClientFactory(c.transport, protocolFactory)

	// simulate timeout support for the underlying Dial() in .Open(). needless to say this sucks
	// restore sanity to this for Go v1 with the new DialTimeout() func
	ch := make(chan bool, 1)
	go func() {
		err = c.transport.Open()
		ch <- true
	}()
	timedOut := false
	select {
	case <-time.After(time.Duration(timeout) * time.Millisecond):
		timedOut = true
	case <-ch:
	}
	if timedOut {
		return nil, ErrorConnectionTimeout
	}
	if err != nil {
		return nil, err
	}

	version, err := c.client.DescribeVersion()
	if err != nil {
		c.close()
		return nil, err
	}
	versionComponents := strings.Split(version, ".")
	if len(versionComponents) < 1 {
		return nil, ErrorInvalidThriftVersion
	}
	majorVersion, err := strconv.Atoi(versionComponents[0])
	if err != nil {
		return nil, ErrorInvalidThriftVersion
	}
	if majorVersion < LOWEST_COMPATIBLE_VERSION {
		return nil, ErrorWrongThriftVersion
	}

	if len(authentication) > 0 {
		ar := cassandra.NewAuthenticationRequest()
		ar.Credentials = make(map[string]string)
		for k, v := range authentication {
			ar.Credentials[k] = v
		}
		err := c.client.Login(ar)
		if err != nil {
			switch err.(type) {
			case *cassandra.AuthenticationException:
				return nil, ErrorAuthenticationFailed
			case *cassandra.AuthorizationException:
				return nil, ErrorAuthorizationFailed
			default:
				return nil, err
			}
		}
	}

	err = c.client.SetKeyspace(keyspace)
	if err != nil {
		switch err.(type) {
		case *cassandra.InvalidRequestException:
			c.close()
			return nil, ErrorSetKeyspace
		default:
			c.close()
			return nil, err
		}
	}
	c.keyspace = keyspace

	return c, nil
}
Example #4
0
func newConnection(n *node, keyspace string, timeout time.Duration, authentication map[string]string) (*connection, error) {

	addr, err := net.ResolveTCPAddr("tcp", n.node)
	if err != nil {
		return nil, err
	}

	c := &connection{node: n}

	c.socket = thrift.NewTSocketFromAddrTimeout(addr, timeout)

	c.transport = thrift.NewTFramedTransport(c.socket)
	protocol := thrift.NewTBinaryProtocolTransport(c.transport)
	c.client = cassandra.NewCassandraClientProtocol(c.transport, protocol, protocol)

	if err = c.transport.Open(); err != nil {
		return nil, err
	}

	version, err := c.client.DescribeVersion()
	if err != nil {
		c.close()
		return nil, err
	}
	versionComponents := strings.Split(version, ".")
	if len(versionComponents) < 1 {
		return nil, errors.New(fmt.Sprint("Cannot parse the Thrift API version number: ", version))
	}
	majorVersion, err := strconv.Atoi(versionComponents[0])
	if err != nil {
		return nil, errors.New(fmt.Sprint("Cannot parse the Thrift API version number: ", version))
	}
	if majorVersion < LOWEST_COMPATIBLE_VERSION {
		return nil, errors.New(fmt.Sprint("Unsupported Thrift API version, lowest supported is ", LOWEST_COMPATIBLE_VERSION,
			", server reports ", majorVersion))
	}

	if len(authentication) > 0 {
		ar := cassandra.NewAuthenticationRequest()
		ar.Credentials = authentication
		err := c.client.Login(ar)
		if err != nil {
			c.close()
			switch err.(type) {
			case *cassandra.AuthenticationException:
				return nil, errors.New("Login error: cannot authenticate with the given credentials")
			case *cassandra.AuthorizationException:
				return nil, errors.New("Login error: the given credentials are not authorized to access the server")
			default:
				return nil, err
			}
		}
	}

	err = c.client.SetKeyspace(keyspace)
	if err != nil {
		c.close()
		switch err.(type) {
		case *cassandra.InvalidRequestException:
			err = errors.New("Cannot set the keyspace " + keyspace)
		}
		return nil, err
	}

	return c, nil
}