Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit 6df9dc2

Browse files
authored
[v. small] Persistent storage for asyncs 2 (#894)
Just style: * change some internal struct to small letter to not pollute global symbols * merge error and no-error flow into one.
1 parent 90400ea commit 6df9dc2

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed

quesma/quesma/async_search_storage/in_memory.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ func elapsedTime(t time.Time) time.Duration {
7575
return time.Since(t)
7676
}
7777

78-
type AsyncQueryIdWithTime struct {
78+
type asyncQueryIdWithTime struct {
7979
id string
8080
time time.Time
8181
}
8282

8383
func (e *AsyncQueriesEvictor) tryEvictAsyncRequests(timeFun func(time.Time) time.Duration) {
84-
var ids []AsyncQueryIdWithTime
84+
var ids []asyncQueryIdWithTime
8585
e.AsyncRequestStorage.Range(func(key string, value *AsyncRequestResult) bool {
8686
if timeFun(value.added) > EvictionInterval {
87-
ids = append(ids, AsyncQueryIdWithTime{id: key, time: value.added})
87+
ids = append(ids, asyncQueryIdWithTime{id: key, time: value.added})
8888
}
8989
return true
9090
})

quesma/quesma/search.go

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (q *QueryRunner) handleAsyncSearch(ctx context.Context, indexPattern string
135135
return q.handleSearchCommon(ctx, indexPattern, body, &async, QueryLanguageDefault)
136136
}
137137

138-
type AsyncSearchWithError struct {
138+
type asyncSearchWithError struct {
139139
response *model.SearchResp
140140
translatedQueryBody []types.TranslatedSQLQuery
141141
err error
@@ -173,33 +173,33 @@ func (q *QueryRunner) checkProperties(ctx context.Context, plan *model.Execution
173173
return nil, nil
174174
}
175175

176-
func (q *QueryRunner) runExecutePlanAsync(ctx context.Context, plan *model.ExecutionPlan, queryTranslator IQueryTranslator, table *clickhouse.Table, doneCh chan AsyncSearchWithError, optAsync *AsyncQuery) {
176+
func (q *QueryRunner) runExecutePlanAsync(ctx context.Context, plan *model.ExecutionPlan, queryTranslator IQueryTranslator, table *clickhouse.Table, doneCh chan asyncSearchWithError, optAsync *AsyncQuery) {
177177
go func() {
178178
defer recovery.LogAndHandlePanic(ctx, func(err error) {
179-
doneCh <- AsyncSearchWithError{err: err}
179+
doneCh <- asyncSearchWithError{err: err}
180180
})
181181

182182
translatedQueryBody, results, err := q.searchWorker(ctx, plan, table, doneCh, optAsync)
183183
if err != nil {
184-
doneCh <- AsyncSearchWithError{translatedQueryBody: translatedQueryBody, err: err}
184+
doneCh <- asyncSearchWithError{translatedQueryBody: translatedQueryBody, err: err}
185185
return
186186
}
187187

188188
if len(plan.Queries) > 0 && len(results) == 0 {
189189
// if there are no queries, empty results are fine
190190
logger.ErrorWithCtx(ctx).Msgf("no hits, sqls: %v", translatedQueryBody)
191-
doneCh <- AsyncSearchWithError{translatedQueryBody: translatedQueryBody, err: errors.New("no hits")}
191+
doneCh <- asyncSearchWithError{translatedQueryBody: translatedQueryBody, err: errors.New("no hits")}
192192
return
193193
}
194194

195195
results, err = q.postProcessResults(plan, results)
196196
if err != nil {
197-
doneCh <- AsyncSearchWithError{translatedQueryBody: translatedQueryBody, err: err}
197+
doneCh <- asyncSearchWithError{translatedQueryBody: translatedQueryBody, err: err}
198198
}
199199

200200
searchResponse := queryTranslator.MakeSearchResponse(plan.Queries, results)
201201

202-
doneCh <- AsyncSearchWithError{response: searchResponse, translatedQueryBody: translatedQueryBody, err: err}
202+
doneCh <- asyncSearchWithError{response: searchResponse, translatedQueryBody: translatedQueryBody, err: err}
203203
}()
204204
}
205205

@@ -209,7 +209,7 @@ func (q *QueryRunner) executePlan(ctx context.Context, plan *model.ExecutionPlan
209209
path := contextValues.RequestPath
210210
opaqueId := contextValues.OpaqueId
211211

212-
doneCh := make(chan AsyncSearchWithError, 1)
212+
doneCh := make(chan asyncSearchWithError, 1)
213213

214214
sendMainPlanResult := func(responseBody []byte, err error) {
215215
if optComparePlansCh != nil {
@@ -460,32 +460,19 @@ func (q *QueryRunner) handleSearchCommon(ctx context.Context, indexPattern strin
460460
}
461461

462462
func (q *QueryRunner) storeAsyncSearch(qmc *ui.QuesmaManagementConsole, id, asyncId string,
463-
startTime time.Time, path string, body types.JSON, result AsyncSearchWithError, keep bool, opaqueId string) (responseBody []byte, err error) {
463+
startTime time.Time, path string, body types.JSON, result asyncSearchWithError, keep bool, opaqueId string) (responseBody []byte, err error) {
464+
464465
took := time.Since(startTime)
465-
if result.err != nil {
466-
if keep {
467-
q.AsyncRequestStorage.Store(asyncId, // maybe responseBody line below should be an empty array?
468-
async_search_storage.NewAsyncRequestResult(nil, result.err, time.Now(), false))
469-
}
466+
bodyAsBytes, _ := body.Bytes()
467+
if result.err == nil {
468+
okStatus := 200
469+
asyncResponse := queryparser.SearchToAsyncSearchResponse(result.response, asyncId, false, &okStatus)
470+
responseBody, err = asyncResponse.Marshal()
471+
} else {
470472
responseBody, _ = queryparser.EmptyAsyncSearchResponse(asyncId, false, 503)
471473
err = result.err
472-
bodyAsBytes, _ := body.Bytes()
473-
qmc.PushSecondaryInfo(&ui.QueryDebugSecondarySource{
474-
Id: id,
475-
AsyncId: asyncId,
476-
OpaqueId: opaqueId,
477-
Path: path,
478-
IncomingQueryBody: bodyAsBytes,
479-
QueryBodyTranslated: result.translatedQueryBody,
480-
QueryTranslatedResults: responseBody,
481-
SecondaryTook: took,
482-
})
483-
return
484474
}
485-
okStatus := 200
486-
asyncResponse := queryparser.SearchToAsyncSearchResponse(result.response, asyncId, false, &okStatus)
487-
responseBody, err = asyncResponse.Marshal()
488-
bodyAsBytes, _ := body.Bytes()
475+
489476
qmc.PushSecondaryInfo(&ui.QueryDebugSecondarySource{
490477
Id: id,
491478
AsyncId: asyncId,
@@ -496,6 +483,7 @@ func (q *QueryRunner) storeAsyncSearch(qmc *ui.QuesmaManagementConsole, id, asyn
496483
QueryTranslatedResults: responseBody,
497484
SecondaryTook: took,
498485
})
486+
499487
if keep {
500488
compressedBody := responseBody
501489
isCompressed := false
@@ -505,9 +493,9 @@ func (q *QueryRunner) storeAsyncSearch(qmc *ui.QuesmaManagementConsole, id, asyn
505493
isCompressed = true
506494
}
507495
}
508-
q.AsyncRequestStorage.Store(asyncId,
509-
async_search_storage.NewAsyncRequestResult(compressedBody, err, time.Now(), isCompressed))
496+
q.AsyncRequestStorage.Store(asyncId, async_search_storage.NewAsyncRequestResult(compressedBody, err, time.Now(), isCompressed))
510497
}
498+
511499
return
512500
}
513501

@@ -563,13 +551,13 @@ func (q *QueryRunner) deleteAsyncSeach(id string) ([]byte, error) {
563551
return []byte(`{"acknowledged":true}`), nil
564552
}
565553

566-
func (q *QueryRunner) reachedQueriesLimit(ctx context.Context, asyncId string, doneCh chan<- AsyncSearchWithError) bool {
554+
func (q *QueryRunner) reachedQueriesLimit(ctx context.Context, asyncId string, doneCh chan<- asyncSearchWithError) bool {
567555
if q.AsyncRequestStorage.Size() < asyncQueriesLimit && q.asyncQueriesCumulatedBodySize() < asyncQueriesLimitBytes {
568556
return false
569557
}
570558
err := errors.New("too many async queries")
571559
logger.ErrorWithCtx(ctx).Msgf("cannot handle %s, too many async queries", asyncId)
572-
doneCh <- AsyncSearchWithError{err: err}
560+
doneCh <- asyncSearchWithError{err: err}
573561
return true
574562
}
575563

@@ -775,7 +763,7 @@ func (q *QueryRunner) searchWorkerCommon(
775763
func (q *QueryRunner) searchWorker(ctx context.Context,
776764
plan *model.ExecutionPlan,
777765
table *clickhouse.Table,
778-
doneCh chan<- AsyncSearchWithError,
766+
doneCh chan<- asyncSearchWithError,
779767
optAsync *AsyncQuery) (translatedQueryBody []types.TranslatedSQLQuery, resultRows [][]model.QueryResultRow, err error) {
780768
if optAsync != nil {
781769
if q.reachedQueriesLimit(ctx, optAsync.asyncId, doneCh) {

0 commit comments

Comments
 (0)