Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix linter #30

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 51 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,65 @@ output:

linters:
enable:
- errcheck
- gofmt
- goimports
- gosec
- govet
- whitespace

disable:
- godox
- ineffassign
- errcheck
- staticcheck
- gosimple
- stylecheck
- wsl
- typecheck
- revive
- unused
disable:
- gochecknoinits
- gochecknoglobals

linter-settings:
linters-settings:
govet:
# report about shadowed variables
check-shadowing: true

# enable or disable analyzers by name
# run `go tool vet help` to see all analyzers
enable-all: true
enable-all: true
golint:
min-confidence: 0.8
gocritic:
enabled-checks:
- appendCombine
- argOrder
- badCond
- dupBranchBody
- dupCase
- dupSubExpr
- elseif
- hugeParam
- initClause
- rangeValCopy
- sloppyLen
- typeSwitchVar
- underef
- unlambda
- unslice
gofmt:
simplify: true
goimports:
local-prefixes: github.com/myorg/mypackage
errcheck:
check-type-assertions: true
check-blank: true

issues:
exclude-use-default: false
exclude-rules:
- linters:
- govet
text: "composite literal uses unkeyed fields"
- linters:
- golint
text: "should have comment or be unexported"
- linters:
- staticcheck
text: "SA5001: should check returned error before deferring"

20 changes: 10 additions & 10 deletions integration-tests/batch_quickstart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func getPinotClientFromBroker() *pinot.Connection {
return pinotClient
}

func getCustomHttpClient() *http.Client {
func getCustomHTTPClient() *http.Client {
httpClient := &http.Client{
Timeout: 15 * time.Second,
Transport: &http.Transport{
Expand All @@ -69,24 +69,24 @@ func getCustomHttpClient() *http.Client {
return httpClient
}

func getPinotClientFromZookeeperAndCustomHttpClient() *pinot.Connection {
pinotClient, err := pinot.NewFromZookeeperAndClient([]string{"localhost:" + zookeeperPort}, "", "QuickStartCluster", getCustomHttpClient())
func getPinotClientFromZookeeperAndCustomHTTPClient() *pinot.Connection {
pinotClient, err := pinot.NewFromZookeeperAndClient([]string{"localhost:" + zookeeperPort}, "", "QuickStartCluster", getCustomHTTPClient())
if err != nil {
log.Fatalln(err)
}
return pinotClient
}

func getPinotClientFromControllerAndCustomHttpClient() *pinot.Connection {
pinotClient, err := pinot.NewFromControllerAndClient("localhost:"+controllerPort, getCustomHttpClient())
func getPinotClientFromControllerAndCustomHTTPClient() *pinot.Connection {
pinotClient, err := pinot.NewFromControllerAndClient("localhost:"+controllerPort, getCustomHTTPClient())
if err != nil {
log.Fatalln(err)
}
return pinotClient
}

func getPinotClientFromBrokerAndCustomHttpClient() *pinot.Connection {
pinotClient, err := pinot.NewFromBrokerListAndClient([]string{"localhost:" + brokerPort}, getCustomHttpClient())
func getPinotClientFromBrokerAndCustomHTTPClient() *pinot.Connection {
pinotClient, err := pinot.NewFromBrokerListAndClient([]string{"localhost:" + brokerPort}, getCustomHTTPClient())
if err != nil {
log.Fatalln(err)
}
Expand All @@ -101,9 +101,9 @@ func TestSendingQueriesToPinot(t *testing.T) {
getPinotClientFromZookeeper(),
getPinotClientFromController(),
getPinotClientFromBroker(),
getPinotClientFromZookeeperAndCustomHttpClient(),
getPinotClientFromControllerAndCustomHttpClient(),
getPinotClientFromBrokerAndCustomHttpClient(),
getPinotClientFromZookeeperAndCustomHTTPClient(),
getPinotClientFromControllerAndCustomHTTPClient(),
getPinotClientFromBrokerAndCustomHTTPClient(),
}

table := "baseballStats"
Expand Down
1 change: 1 addition & 0 deletions pinot/brokerSelector.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package pinot provides a client for Pinot, a real-time distributed OLAP datastore.
package pinot

type brokerSelector interface {
Expand Down
2 changes: 2 additions & 0 deletions pinot/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ func (c *Connection) ExecuteSQL(table string, query string) (*BrokerResponse, er
return brokerResp, err
}

// OpenTrace for the connection
func (c *Connection) OpenTrace() {
c.trace = true
}

// CloseTrace for the connection
func (c *Connection) CloseTrace() {
c.trace = false
}
7 changes: 6 additions & 1 deletion pinot/connectionFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"net/http"
"strings"

log "github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -100,7 +102,10 @@ func NewWithConfigAndClient(config *ClientConfig, httpClient *http.Client) (*Con
}
if conn != nil {
// TODO: error handling results into `make test` failure.
conn.brokerSelector.init()
if err := conn.brokerSelector.init(); err != nil {
log.Errorf("Failed to initialize broker selector: %v", err)
return conn, err
}
return conn, nil
}
return nil, fmt.Errorf(
Expand Down
8 changes: 6 additions & 2 deletions pinot/connectionFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ func TestPinotClients(t *testing.T) {
assert.NotNil(t, pinotClient1)
assert.NotNil(t, pinotClient1.brokerSelector)
assert.NotNil(t, pinotClient1.transport)
assert.Nil(t, err)
// Since there is no zk setup, so an error will be raised
assert.NotNil(t, err)
pinotClient2, err := NewWithConfig(&ClientConfig{
ZkConfig: &ZookeeperConfig{
ZookeeperPath: []string{"localhost:2181"},
Expand All @@ -27,11 +28,14 @@ func TestPinotClients(t *testing.T) {
assert.NotNil(t, pinotClient2)
assert.NotNil(t, pinotClient2.brokerSelector)
assert.NotNil(t, pinotClient2.transport)
assert.Nil(t, err)
// Since there is no zk setup, so an error will be raised
assert.NotNil(t, err)
pinotClient3, err := NewFromController("localhost:9000")
assert.NotNil(t, pinotClient3)
assert.NotNil(t, pinotClient3.brokerSelector)
assert.NotNil(t, pinotClient3.transport)
// Since there is no controller setup, so an error will be raised
assert.NotNil(t, err)
_, err = NewWithConfig(&ClientConfig{})
assert.NotNil(t, err)
assert.True(t, strings.Contains(err.Error(), "please specify"))
Expand Down
18 changes: 13 additions & 5 deletions pinot/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ func TestConnectionWithControllerBasedBrokerSelector(t *testing.T) {
func TestSendingQueryWithTraceOpen(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var request map[string]string
json.NewDecoder(r.Body).Decode(&request)
err := json.NewDecoder(r.Body).Decode(&request)
assert.Equal(t, request["trace"], "true")
assert.Nil(t, err)
}))
defer ts.Close()
pinotClient, err := NewFromBrokerList([]string{ts.URL})
Expand All @@ -123,13 +124,16 @@ func TestSendingQueryWithTraceOpen(t *testing.T) {
assert.NotNil(t, pinotClient.transport)
assert.Nil(t, err)
pinotClient.OpenTrace()
pinotClient.ExecuteSQL("", "select teamID, count(*) as cnt, sum(homeRuns) as sum_homeRuns from baseballStats group by teamID limit 10")
resp, err := pinotClient.ExecuteSQL("", "select teamID, count(*) as cnt, sum(homeRuns) as sum_homeRuns from baseballStats group by teamID limit 10")
assert.Nil(t, resp)
assert.NotNil(t, err)
}

func TestSendingQueryWithTraceClose(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var request map[string]string
json.NewDecoder(r.Body).Decode(&request)
err := json.NewDecoder(r.Body).Decode(&request)
assert.Nil(t, err)
_, ok := request["trace"]
assert.False(t, ok)
}))
Expand All @@ -139,8 +143,12 @@ func TestSendingQueryWithTraceClose(t *testing.T) {
assert.NotNil(t, pinotClient.brokerSelector)
assert.NotNil(t, pinotClient.transport)
assert.Nil(t, err)
pinotClient.ExecuteSQL("", "select teamID, count(*) as cnt, sum(homeRuns) as sum_homeRuns from baseballStats group by teamID limit 10")
resp, err := pinotClient.ExecuteSQL("", "select teamID, count(*) as cnt, sum(homeRuns) as sum_homeRuns from baseballStats group by teamID limit 10")
assert.Nil(t, resp)
assert.NotNil(t, err)
pinotClient.OpenTrace()
pinotClient.CloseTrace()
pinotClient.ExecuteSQL("", "select teamID, count(*) as cnt, sum(homeRuns) as sum_homeRuns from baseballStats group by teamID limit 10")
resp, err = pinotClient.ExecuteSQL("", "select teamID, count(*) as cnt, sum(homeRuns) as sum_homeRuns from baseballStats group by teamID limit 10")
assert.Nil(t, resp)
assert.NotNil(t, err)
}
34 changes: 16 additions & 18 deletions pinot/controllerBasedBrokerSelector.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,31 @@ var (
}
)

// HTTPClient is an interface for http.Client
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}

type controllerBasedSelector struct {
client HTTPClient
config *ControllerConfig
controllerAPIReqUrl string
controllerAPIReqURL string
tableAwareBrokerSelector
}

func (s *controllerBasedSelector) init() error {
if s.config.UpdateFreqMs == 0 {
s.config.UpdateFreqMs = defaultUpdateFreqMs
}
u, err := getControllerRequestUrl(s.config.ControllerAddress)
var err error
s.controllerAPIReqURL, err = getControllerRequestURL(s.config.ControllerAddress)
if err != nil {
log.Error(err)
log.Errorf("An error occurred when parsing controller address: %v", err)
return err
}
s.controllerAPIReqUrl = u

err = s.updateBrokerData()
if err != nil {
log.Errorf(
"An error occurred when fetching broker data from controller API for the first time, Error: %v",
err,
)
if err = s.updateBrokerData(); err != nil {
log.Errorf("An error occurred when fetching broker data from controller API for the first time, Error: %v", err)
return err
}
go s.setupInterval()
Expand All @@ -73,7 +70,7 @@ func (s *controllerBasedSelector) setupInterval() {
}
}

func getControllerRequestUrl(controllerAddress string) (string, error) {
func getControllerRequestURL(controllerAddress string) (string, error) {
tokenized := strings.Split(controllerAddress, "://")
addressWithScheme := controllerAddress
if len(tokenized) > 1 {
Expand All @@ -91,12 +88,9 @@ func getControllerRequestUrl(controllerAddress string) (string, error) {
}

func (s *controllerBasedSelector) createControllerRequest() (*http.Request, error) {
r, err := http.NewRequest("GET", s.controllerAPIReqUrl, nil)
r, err := http.NewRequest("GET", s.controllerAPIReqURL, nil)
if err != nil {
return r, fmt.Errorf(
"Caught exception when creating controller API request: %v",
err,
)
return r, fmt.Errorf("Caught exception when creating controller API request: %v", err)
}
for k, v := range controllerDefaultHTTPHeader {
r.Header.Add(k, v)
Expand All @@ -116,14 +110,18 @@ func (s *controllerBasedSelector) updateBrokerData() error {
if err != nil {
return fmt.Errorf("Got exceptions while sending controller API request: %v", err)
}
defer resp.Body.Close()
defer func() {
if err := resp.Body.Close(); err != nil {
log.Error("Unable to close response body. ", err)
}
}()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("An error occurred when reading controller API response: %v", err)
}
var c controllerResponse
if err = decodeJsonWithNumber(bodyBytes, &c); err != nil {
if err = decodeJSONWithNumber(bodyBytes, &c); err != nil {
return fmt.Errorf("An error occurred when decoding controller API response: %v", err)
}
s.rwMux.Lock()
Expand Down
Loading
Loading