Thrift Binary protocol is now a choice for users wishing to use it. Default Thrift protocol continues to be JSON.

This commit is contained in:
Renan DelValle 2017-02-13 19:31:41 -05:00
parent 75c87f34b3
commit 10c12d5a13

View file

@ -17,14 +17,12 @@ package realis
import ( import (
"encoding/base64" "encoding/base64"
"fmt"
"git.apache.org/thrift.git/lib/go/thrift" "git.apache.org/thrift.git/lib/go/thrift"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rdelval/gorealis/gen-go/apache/aurora" "github.com/rdelval/gorealis/gen-go/apache/aurora"
"github.com/rdelval/gorealis/response" "github.com/rdelval/gorealis/response"
"net/http" "net/http"
"net/http/cookiejar" "net/http/cookiejar"
"os"
"time" "time"
) )
@ -52,7 +50,7 @@ type realisClient struct {
client *aurora.AuroraSchedulerManagerClient client *aurora.AuroraSchedulerManagerClient
} }
// Wrap object to provide future flexibility // Wrapper object to provide future flexibility
type RealisConfig struct { type RealisConfig struct {
transport thrift.TTransport transport thrift.TTransport
protoFactory thrift.TProtocolFactory protoFactory thrift.TProtocolFactory
@ -64,26 +62,53 @@ func NewClient(config RealisConfig) Realis {
client: aurora.NewAuroraSchedulerManagerClientFactory(config.transport, config.protoFactory)} client: aurora.NewAuroraSchedulerManagerClientFactory(config.transport, config.protoFactory)}
} }
// Create a default configuration of the transport layer, requires a URL to test connection with. // Creates a default Thrift Transport object for communications in gorealis using an HTTP Post Client
func NewDefaultConfig(url string, timeoutms int) (RealisConfig, error) { func defaultTTransport(urlstr string, timeoutms int) (thrift.TTransport, error) {
jar, err := cookiejar.New(nil) jar, err := cookiejar.New(nil)
if err != nil { if err != nil {
return RealisConfig{}, errors.Wrap(err, "Error creating Cookie Jar") return &thrift.THttpClient{}, errors.Wrap(err, "Error creating Cookie Jar")
} }
//Custom client to timeout after 10 seconds to avoid hanging trans, err := thrift.NewTHttpPostClientWithOptions(urlstr+"/api",
trans, err := thrift.NewTHttpPostClientWithOptions(url+"/api",
thrift.THttpClientOptions{Client: &http.Client{Timeout: time.Millisecond * time.Duration(timeoutms), Jar: jar}}) thrift.THttpClientOptions{Client: &http.Client{Timeout: time.Millisecond * time.Duration(timeoutms), Jar: jar}})
if err != nil { if err != nil {
return RealisConfig{}, errors.Wrap(err, "Error creating transport") return &thrift.THttpClient{}, errors.Wrap(err, "Error creating transport")
} }
if err := trans.Open(); err != nil { if err := trans.Open(); err != nil {
fmt.Fprintln(os.Stderr) return &thrift.THttpClient{}, errors.Wrapf(err, "Error opening connection to %s", urlstr)
return RealisConfig{}, errors.Wrapf(err, "Error opening connection to %s", url)
} }
return trans, nil
}
// Create a default configuration of the transport layer, requires a URL to test connection with.
// Uses HTTP Post as transport layer and Thrift JSON as the wire protocol by default.
func NewDefaultConfig(url string, timeoutms int) (RealisConfig, error) {
return NewTJSONConfig(url, timeoutms)
}
// Creates a realis config object using HTTP Post and Thrift JSON protocol to communicate with Aurora.
func NewTJSONConfig(url string, timeoutms int) (RealisConfig, error) {
trans, err := defaultTTransport(url, timeoutms)
if err != nil {
return RealisConfig{}, errors.Wrap(err, "Error creating realis config")
}
httpTrans := (trans).(*thrift.THttpClient)
httpTrans.SetHeader("Content-Type", "application/x-thrift")
return RealisConfig{transport: trans, protoFactory: thrift.NewTJSONProtocolFactory()}, nil
}
// Creates a realis config config using HTTP Post and Thrift Binary protocol to communicate with Aurora.
func NewTBinaryConfig(url string, timeoutms int) (RealisConfig, error) {
trans, err := defaultTTransport(url, timeoutms)
if err != nil {
return RealisConfig{}, errors.Wrap(err, "Error creating realis config")
}
httpTrans := (trans).(*thrift.THttpClient) httpTrans := (trans).(*thrift.THttpClient)
httpTrans.SetHeader("Accept", "application/vnd.apache.thrift.binary") httpTrans.SetHeader("Accept", "application/vnd.apache.thrift.binary")
httpTrans.SetHeader("Content-Type", "application/vnd.apache.thrift.binary") httpTrans.SetHeader("Content-Type", "application/vnd.apache.thrift.binary")
@ -300,7 +325,6 @@ func (r realisClient) GetTaskStatus(query *aurora.TaskQuery)(tasks []*aurora.Sch
return response.ScheduleStatusResult(resp).GetTasks(), nil return response.ScheduleStatusResult(resp).GetTasks(), nil
} }
func (r realisClient) FetchTaskConfig(instKey aurora.InstanceKey) (*aurora.TaskConfig, error) { func (r realisClient) FetchTaskConfig(instKey aurora.InstanceKey) (*aurora.TaskConfig, error) {
ids := make(map[int32]bool) ids := make(map[int32]bool)