Skip to content

Commit

Permalink
MINOR: [Go] Use shared cache in SQLite example. (apache#33983)
Browse files Browse the repository at this point in the history
### Rationale for this change

When calling this server with a few concurrent requests, you can easily force the Go stdlib to spawn a new connection. Since only the `:memory:` DSN is specified, the stdlib doesn't know any better and effectively creates a new database. The result is a lot of `no such table` errors.

### What changes are included in this PR?

This change adds the `?cache=shared` as specified at:

https://www.sqlite.org/sharedcache.html#enabling_shared_cache_mode

This ensures that these new connections participate in same in-memory database and can see all the tables created at server start.

### Are these changes tested?

I haven't written any additional tests here, because this behavior is difficult to test. Suggestions are welcome on an approach here.

### Are there any user-facing changes?

No.

* Closes: apache#33983

Authored-by: Brett Buddin <[email protected]>
Signed-off-by: Matt Topol <[email protected]>
  • Loading branch information
brettbuddin authored Feb 7, 2023
1 parent 63cb14e commit 39bad54
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ func main() {

flag.Parse()

srv, err := example.NewSQLiteFlightSQLServer()
db, err := example.CreateDB()
if err != nil {
log.Fatal(err)
}
defer db.Close()

srv, err := example.NewSQLiteFlightSQLServer(db)
if err != nil {
log.Fatal(err)
}
Expand Down
35 changes: 20 additions & 15 deletions go/arrow/flight/flightsql/example/sqlite_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,8 @@ func prepareQueryForGetKeys(filter string) string {
` ORDER BY pk_catalog_name, pk_schema_name, pk_table_name, pk_key_name, key_sequence`
}

type Statement struct {
stmt *sql.Stmt
params [][]interface{}
}

type SQLiteFlightSQLServer struct {
flightsql.BaseServer
db *sql.DB

prepared sync.Map
}

func NewSQLiteFlightSQLServer() (*SQLiteFlightSQLServer, error) {
db, err := sql.Open("sqlite", ":memory:")
func CreateDB() (*sql.DB, error) {
db, err := sql.Open("sqlite", "file::memory:?cache=shared")
if err != nil {
return nil, err
}
Expand All @@ -178,10 +166,27 @@ func NewSQLiteFlightSQLServer() (*SQLiteFlightSQLServer, error) {
INSERT INTO intTable (keyName, value, foreignId) VALUES ('negative one', -1, 1);
INSERT INTO intTable (keyName, value, foreignId) VALUES (NULL, NULL, NULL);
`)

if err != nil {
db.Close()
return nil, err
}

return db, nil
}

type Statement struct {
stmt *sql.Stmt
params [][]interface{}
}

type SQLiteFlightSQLServer struct {
flightsql.BaseServer
db *sql.DB

prepared sync.Map
}

func NewSQLiteFlightSQLServer(db *sql.DB) (*SQLiteFlightSQLServer, error) {
ret := &SQLiteFlightSQLServer{db: db}
ret.Alloc = memory.DefaultAllocator
for k, v := range SqlInfoResultMap() {
Expand Down
8 changes: 7 additions & 1 deletion go/arrow/flight/flightsql/sqlite_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package flightsql_test

import (
"context"
"database/sql"
"os"
"strings"
"testing"
Expand All @@ -42,6 +43,7 @@ import (
type FlightSqliteServerSuite struct {
suite.Suite

db *sql.DB
srv *example.SQLiteFlightSQLServer
s flight.Server
cl *flightsql.Client
Expand Down Expand Up @@ -71,7 +73,9 @@ func (s *FlightSqliteServerSuite) SetupTest() {
var err error
s.mem = memory.NewCheckedAllocator(memory.DefaultAllocator)
s.s = flight.NewServerWithMiddleware(nil)
s.srv, err = example.NewSQLiteFlightSQLServer()
s.db, err = example.CreateDB()
s.Require().NoError(err)
s.srv, err = example.NewSQLiteFlightSQLServer(s.db)
s.Require().NoError(err)
s.srv.Alloc = s.mem

Expand All @@ -89,6 +93,8 @@ func (s *FlightSqliteServerSuite) TearDownTest() {
s.Require().NoError(s.cl.Close())
s.s.Shutdown()
s.srv = nil
err := s.db.Close()
s.Require().NoError(err)
s.mem.AssertSize(s.T(), 0)
}

Expand Down

0 comments on commit 39bad54

Please sign in to comment.