Skip to content
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
1 change: 1 addition & 0 deletions .github/workflows/ci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ jobs:
go mod verify
go mod tidy -diff
GOOS=darwin go vet ./...
go test ./...
# go tool would build staticcheck for GOOS too, and Linux cannot run a darwin binary.
go build -modfile=tools.mod -o "$RUNNER_TEMP/staticcheck" honnef.co/go/tools/cmd/staticcheck
GOOS=darwin "$RUNNER_TEMP/staticcheck" ./...
Expand Down
43 changes: 43 additions & 0 deletions apple/Native/HanaBridge/internal/hana/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ func (b *Bridge) Explain(sessionID uint64, operationID uint64, requestJSON []byt
return encoded, encodedFailure(bridgeFailure)
}

func (b *Bridge) OpenCursor(sessionID uint64, operationID uint64, requestJSON []byte) (result []byte, failure []byte) {
encoded, bridgeFailure := b.openCursorOnSession(sessionID, operationID, requestJSON)
return encoded, encodedFailure(bridgeFailure)
}

func (b *Bridge) Fetch(sessionID uint64, operationID uint64) (result []byte, failure []byte) {
encoded, bridgeFailure := b.fetchOnSession(sessionID, operationID)
return encoded, encodedFailure(bridgeFailure)
}

func (b *Bridge) CloseCursor(sessionID uint64, operationID uint64) (result []byte, failure []byte) {
encoded, bridgeFailure := b.closeCursorOnSession(sessionID, operationID)
return encoded, encodedFailure(bridgeFailure)
}

func (b *Bridge) Ping(sessionID uint64, operationID uint64) (failure []byte) {
return encodedFailure(b.pingSession(sessionID, operationID))
}
Expand Down Expand Up @@ -101,6 +116,34 @@ func (b *Bridge) explainOnSession(sessionID uint64, operationID uint64, requestJ
return entry.explain(operationID, request)
}

func (b *Bridge) openCursorOnSession(sessionID uint64, operationID uint64, requestJSON []byte) ([]byte, *bridgeError) {
entry, failure := b.sessions.lookup(sessionID)
if failure != nil {
return nil, failure
}
request, failure := decodeRequest[cursorRequest](requestJSON)
if failure != nil {
return nil, failure
}
return entry.openCursor(operationID, request)
}

func (b *Bridge) fetchOnSession(sessionID uint64, operationID uint64) ([]byte, *bridgeError) {
entry, failure := b.sessions.lookup(sessionID)
if failure != nil {
return nil, failure
}
return entry.fetch(operationID)
}

func (b *Bridge) closeCursorOnSession(sessionID uint64, operationID uint64) ([]byte, *bridgeError) {
entry, failure := b.sessions.lookup(sessionID)
if failure != nil {
return nil, failure
}
return entry.closeCursor(operationID)
}

func (b *Bridge) pingSession(sessionID uint64, operationID uint64) *bridgeError {
entry, failure := b.sessions.lookup(sessionID)
if failure != nil {
Expand Down
191 changes: 191 additions & 0 deletions apple/Native/HanaBridge/internal/hana/cursor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package hana

import (
"context"
"database/sql"
"sync/atomic"
)

const (
defaultCursorBatchRows = 1_000
maxCursorBatchRows = 100_000
cursorBatchBytes = 4 << 20
noCursorMessage = "no cursor is open for this operation"
cursorParametersMessage = "a cursor takes no parameters"
)

type cursorRequest struct {
executeRequest
BatchRows int `json:"batchRows"`
}

func (r cursorRequest) batchRows() int {
if r.BatchRows <= 0 {
return defaultCursorBatchRows
}
return min(r.BatchRows, maxCursorBatchRows)
}

type cursor struct {
op *operation
rows *sql.Rows
reader *resultReader
maxRows int
maxBytes int
}

func (c *cursor) nextBatch() (*rowBatch, error) {
batch := &rowBatch{maxRows: c.maxRows, maxBytes: c.maxBytes}
ended, err := c.reader.read(batch)
batch.done = ended
return batch, err
}

type openedStatement struct {
header *resultEnvelope
rows *sql.Rows
columns []columnInfo
}

func (s *session) openCursor(operationID uint64, request cursorRequest) ([]byte, *bridgeError) {
if request.hasParameters() {
return nil, internalError(cursorParametersMessage)
}
// Once the result exists a stop sends no CANCEL SESSION: it would mostly reach an idle session, and a refused
// cancel severs the session. The row loop polls the stop flag, and the forced sever covers an unanswered fetch.
var resultReady atomic.Bool
op, conn, failure := s.beginOperation(operationID, func(reason stopReason) {
if !resultReady.Load() {
s.interruptStatement(reason)
}
})
if failure != nil {
return nil, failure
}
op.stopAfter(secondsDuration(request.TimeoutSeconds))
opened, err := openStatement(conn, op, request.SQL)
if err != nil {
return nil, s.endOperation(op, err)
}
if opened.rows == nil {
s.slot.finish(op)
opened.header.sessionLost = s.currentState() == sessionLost
return appendCursorOpenedJSON(nil, opened.header, &rowBatch{done: true}), nil
}
resultReady.Store(true)
// The query timeout covers producing the result. Moving its rows can take far longer.
op.disarmDeadline()
current := &cursor{
op: op,
rows: opened.rows,
reader: &resultReader{rows: opened.rows, columns: opened.columns, op: op},
maxRows: request.batchRows(),
maxBytes: cursorBatchBytes,
}
batch, failure := s.advance(current)
if failure != nil {
return nil, failure
}
opened.header.sessionLost = s.currentState() == sessionLost
return appendCursorOpenedJSON(nil, opened.header, batch), nil
}

func (s *session) fetch(operationID uint64) ([]byte, *bridgeError) {
current, failure := s.takeCursor(operationID)
if failure != nil {
return nil, failure
}
batch, failure := s.advance(current)
if failure != nil {
return nil, failure
}
return appendCursorBatchJSON(nil, batch, s.currentState() == sessionLost), nil
}

func (s *session) closeCursor(operationID uint64) ([]byte, *bridgeError) {
current, failure := s.takeCursor(operationID)
if failure != nil {
return nil, failure
}
_, err := runUnderWatchdog(cancelDeadline, s.loseConnection, current.rows.Close)
if isConnectionFailure(err) {
s.loseConnection()
}
s.slot.finish(current.op)
return appendCursorClosedJSON(nil, s.currentState() == sessionLost), nil
}

func (s *session) advance(current *cursor) (*rowBatch, *bridgeError) {
batch, err := current.nextBatch()
if err == nil && !batch.done {
if s.park(current) {
return batch, nil
}
err = errSlotClosed
}
err = joinCleanupFailure(err, current.rows.Close())
if failure := s.endOperation(current.op, err); failure != nil {
return nil, failure
}
return batch, nil
}

func (s *session) endOperation(op *operation, err error) *bridgeError {
op.settle()
var failure *bridgeError
if err != nil {
failure = s.failure(op, err)
}
s.slot.finish(op)
return failure
}

func (s *session) takeCursor(operationID uint64) (*cursor, *bridgeError) {
s.mu.Lock()
defer s.mu.Unlock()
if s.state == sessionClosed {
return nil, closedError()
}
current := s.parkedCursor
if current == nil || current.op.id != operationID {
return nil, internalError(noCursorMessage)
}
s.parkedCursor = nil
return current, nil
}

func (s *session) park(current *cursor) bool {
s.mu.Lock()
defer s.mu.Unlock()
if s.state == sessionClosed {
return false
}
s.parkedCursor = current
return true
}

func openStatement(conn *sql.Conn, op *operation, statement string) (openedStatement, error) {
if op.stopped() {
return openedStatement{}, errOperationStopped
}
if routesToExec(statement) {
envelope, err := runCountingStatement(conn, statement)
return openedStatement{header: envelope}, err
}
rows, err := conn.QueryContext(context.Background(), statement)
if err != nil {
return openedStatement{}, err
}
found, err := moveToFirstResultSet(rows)
var columns []columnInfo
if err == nil && found {
columns, err = describeColumns(rows)
}
if err != nil || !found {
if err = joinCleanupFailure(err, rows.Close()); err != nil {
return openedStatement{}, err
}
return openedStatement{header: affectedRowsEnvelope(0)}, nil
}
return openedStatement{header: tabularEnvelope(columns), rows: rows, columns: columns}, nil
}
Loading
Loading