Skip to content

Commit

Permalink
[DE-6473] - merging into main module
Browse files Browse the repository at this point in the history
  • Loading branch information
vishwa05-rzp committed Aug 21, 2024
1 parent c724c31 commit 98e03a1
Show file tree
Hide file tree
Showing 25 changed files with 128 additions and 524 deletions.
4 changes: 2 additions & 2 deletions build/docker/dev/Dockerfile.trino_rest
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ FROM alpine:latest

COPY --from=builder /src/bin/api /app/
COPY --from=builder /src/config/ /app/config/
COPY build/docker/entrypoint.sh /app/
COPY build/docker/dev/trino-rest_entrypoint.sh /app/

ENV WORKDIR=/app
ENV DUMB_INIT_SETSID=0
Expand All @@ -36,6 +36,6 @@ RUN apk add --update --no-cache dumb-init su-exec ca-certificates curl

EXPOSE 8000

RUN chmod +x entrypoint.sh
RUN chmod +x trino-rest_entrypoint.sh

ENTRYPOINT ["./api"]
11 changes: 6 additions & 5 deletions trino-rest/cmd/api/main.go → cmd/trino_rest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import (
"fmt"
"net/http"
"strconv"
"trino-api/internal/app"
"trino-api/internal/boot"

"github.com/razorpay/trino-gateway/internal/boot"
trino_rest "github.com/razorpay/trino-gateway/internal/trino_rest"
)

func main() {
//Initialize context
ctx, cancel := context.WithCancel(boot.NewContext(context.Background()))
defer cancel()

boot.Init()
logger := boot.InitLogger(ctx)
boot.TrinoRestInit()
logger := boot.InitLoggerTrinoRest(ctx)

app, err := app.NewApp(&boot.Config)
app, err := trino_rest.NewApp(&boot.Config)
if err != nil {
logger.Fatal(fmt.Sprintf("Failed to initialize app: %v", err))
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.1
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v1.19.0-beta1 h1:pf9nCe/s+KimJnG4j1pmUA2ACX/b518uFr1Llrikt6c=
github.com/gopherjs/gopherjs v1.19.0-beta1/go.mod h1:VdmTFLUMAU41SxYIwoZFn1RyDsP68VDAf5cwYBFXWLY=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
Expand Down
31 changes: 31 additions & 0 deletions internal/boot/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ func init() {
}
}

func TrinoRestInit() {
// Init config
err := config_reader.NewDefaultConfig().Load(GetEnv(), &Config)
if err != nil {
log.Fatal(err)
}
InitLoggerTrinoRest(context.Background())
}

// Fetch env for bootstrapping
func GetEnv() string {
environment := os.Getenv("APP_ENV")
Expand Down Expand Up @@ -151,6 +160,13 @@ func NewContext(ctx context.Context) context.Context {
return ctx
}

func TrinoRestNewContext(ctx context.Context) context.Context {
if ctx == nil {
ctx = context.Background()
}
return ctx
}

func InitLogger(ctx context.Context) *logger.ZapLogger {
lgrConfig := logger.Config{
LogLevel: Config.App.LogLevel,
Expand All @@ -164,3 +180,18 @@ func InitLogger(ctx context.Context) *logger.ZapLogger {

return Logger
}

func InitLoggerTrinoRest(ctx context.Context) *logger.ZapLogger {
lgrConfig := logger.Config{
LogLevel: logger.Info,
ContextString: "trino_client",
}

Logger, err := logger.NewLogger(lgrConfig)

if err != nil {
panic("failed to initialize logger")
}

return Logger
}
37 changes: 32 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package config

import (
"time"

"github.com/razorpay/trino-gateway/pkg/spine/db"
)

type Config struct {
App App
Auth Auth
Db db.Config
Gateway Gateway
Monitor Monitor
App App
Auth Auth
Db db.Config
Gateway Gateway
Monitor Monitor
TrinoRest TrinoRestConfig
}

// App contains application-specific config values
Expand Down Expand Up @@ -53,3 +56,27 @@ type Monitor struct {
}
HealthCheckSql string
}

type TrinoRestConfig struct {
AppEnv string
ServiceName string
Hostname string
Port int
MetricsPort int
ShutdownTimeout time.Duration
ShutdownDelay time.Duration
MaxRecords int
TrinoBackendDB TrinoBackendDB
}

type TrinoBackendDB struct {
Dialect string
Protocol string
URL string
Port int
Username string
Password string
Catalog string
Schema string
DSN string
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@ package handler
import (
"encoding/json"
"net/http"
"trino-api/internal/app/process"
"trino-api/internal/config"
"trino-api/internal/model"
"trino-api/internal/services/trino"
"trino-api/internal/utils"

"github.com/razorpay/trino-gateway/internal/config"
"github.com/razorpay/trino-gateway/internal/trino_rest/model"
"github.com/razorpay/trino-gateway/internal/trino_rest/process"
"github.com/razorpay/trino-gateway/internal/trino_rest/services/trino"
"github.com/razorpay/trino-gateway/internal/trino_rest/utils"
)

type Handler struct {
TrinoClient *trino.Client
cfg *config.Config
TrinoClient *trino.Client
cfg *config.Config
queryProcessor process.QueryProcessor
}

// NewHandler initializes the Handler with the TrinoClient and config.
func NewHandler(trinoClient *trino.Client, cfg *config.Config) *Handler {
func NewHandler(trinoClient *trino.Client, cfg *config.Config, processor process.QueryProcessor) *Handler {
if processor == nil {
processor = &process.DefaultProcessor{}
}
return &Handler{
TrinoClient: trinoClient,
cfg: cfg,
TrinoClient: trinoClient,
cfg: cfg,
queryProcessor: processor,
}
}
func (h *Handler) QueryHandler() http.HandlerFunc {
Expand All @@ -39,12 +45,12 @@ func (h *Handler) QueryHandler() http.HandlerFunc {
return
}
defer rows.Close()
columns, rowData, err := process.QueryResult(rows)
columns, rowData, err := h.queryProcessor.QueryResult(rows)
if err != nil {
utils.RespondWithError(w, http.StatusUnprocessableEntity, "Unable to process: "+err.Error())
return
}
if len(rowData) > h.cfg.App.MaxRecords {
if len(rowData) > h.cfg.TrinoRest.MaxRecords {
utils.RespondWithError(w, http.StatusRequestEntityTooLarge, "Response data is too big")
return
}
Expand Down
1 change: 1 addition & 0 deletions internal/trino_rest/handler/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package handler
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@ type ReqData struct {
}

type RespData struct {
Status string `json:"status,omitempty"`
Columns []Column `json:"columns,omitempty"`
Data [][]Datum `json:"data,omitempty"`
Error *Error `json:"error,omitempty"`
Status string `json:"status,omitempty"`
Columns []Column `json:"columns,omitempty"`
Data []map[string]interface{} `json:"data,omitempty"`
Error *Error `json:"error,omitempty"`
}

type Column struct {
Name string `json:"name"`
Type string `json:"type"`
}

type Datum struct {
Data interface{} `json:"data"`
}
type Error struct {
Message string `json:"message"`
ErrorCode int64 `json:"errorCode"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ package process

import (
"database/sql"
"trino-api/internal/model"

"github.com/razorpay/trino-gateway/internal/trino_rest/model"
)

type QueryProcessor interface {
QueryResult(rows *sql.Rows) ([]model.Column, []map[string]interface{}, error)
}

type DefaultProcessor struct{}

// this method will parse the column and its type and the rows of data and send it back
func QueryResult(rows *sql.Rows) ([]model.Column, [][]model.Datum, error) {
func (p *DefaultProcessor) QueryResult(rows *sql.Rows) ([]model.Column, []map[string]interface{}, error) {
var (
resultColumns []model.Column
dataRows [][]model.Datum
dataRows []map[string]interface{}
)
columns, err := rows.Columns()
if err != nil {
Expand All @@ -29,9 +36,9 @@ func QueryResult(rows *sql.Rows) ([]model.Column, [][]model.Datum, error) {
}

for rows.Next() {
var dataRow []model.Datum
columns := make([]interface{}, len(resultColumns))
colPtrs := make([]interface{}, len(resultColumns))
rowMap := make(map[string]interface{})

for i := range columns {
colPtrs[i] = &columns[i]
Expand All @@ -41,10 +48,10 @@ func QueryResult(rows *sql.Rows) ([]model.Column, [][]model.Datum, error) {
return nil, nil, err
}

for _, col := range columns {
dataRow = append(dataRow, model.Datum{Data: col})
for i, col := range columns {
rowMap[resultColumns[i].Name] = col
}
dataRows = append(dataRows, dataRow)
dataRows = append(dataRows, rowMap)
}
return resultColumns, dataRows, nil
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package routes

import (
"trino-api/internal/app/handler"
"github.com/razorpay/trino-gateway/internal/trino_rest/handler"

"github.com/gorilla/mux"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import (
"time"

// blank import as this is needed to implement trino go client

_ "github.com/trinodb/trino-go-client/trino"
)

type Client struct {
db *sql.DB
}

type TrinoClient interface {
Query(query string) (*sql.Rows, error)
}

func NewTrinoClient(dsn string) (*Client, error) {
db, err := sql.Open("trino", dsn)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package app
package trino_rest

import (
"fmt"
"trino-api/internal/app/handler"
"trino-api/internal/app/routes"
"trino-api/internal/config"
"trino-api/internal/services/trino"

"github.com/razorpay/trino-gateway/internal/config"
"github.com/razorpay/trino-gateway/internal/trino_rest/handler"
"github.com/razorpay/trino-gateway/internal/trino_rest/routes"
"github.com/razorpay/trino-gateway/internal/trino_rest/services/trino"

"github.com/gorilla/mux"
)
Expand All @@ -18,16 +19,16 @@ type App struct {

func NewApp(cfg *config.Config) (*App, error) {

if cfg.Db.DSN == "" {
if cfg.TrinoRest.TrinoBackendDB.DSN == "" {
return nil, fmt.Errorf("configuration or database settings are missing")
}

trinoClient, err := trino.NewTrinoClient(cfg.Db.DSN)
trinoClient, err := trino.NewTrinoClient(cfg.TrinoRest.TrinoBackendDB.DSN)
if err != nil {
return nil, err
}

handler := *handler.NewHandler(trinoClient, cfg)
handler := *handler.NewHandler(trinoClient, cfg, nil)

router := mux.NewRouter()
routes.RegisterRoutes(router, &handler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package utils
import (
"encoding/json"
"net/http"
"trino-api/internal/model"

"github.com/razorpay/trino-gateway/internal/trino_rest/model"
)

// send back the response in the json format
Expand All @@ -24,11 +25,3 @@ func RespondWithError(w http.ResponseWriter, code int, message string) {
}
RespondWithJSON(w, code, resp)
}

func RespondWithRunningStatus(w http.ResponseWriter, code int, message string) {
resp := model.RespData{
Status: "Running",
Error: nil,
}
RespondWithJSON(w, code, resp)
}
Loading

0 comments on commit 98e03a1

Please sign in to comment.