This repository was archived by the owner on Nov 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Persistent storage for asyncs 3 #895
Closed
Closed
Changes from 3 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
0e25749
some done
trzysiek 622c4cc
Merge branch 'main' into persistent-storage-2
trzysiek 5928f10
Some more done
trzysiek 3661361
Before reverse to 1 index
trzysiek 56245b8
some more
trzysiek 29679c3
Almost
trzysiek 7656f8b
99% done, need to debug tests.
trzysiek 49956b0
Some more
trzysiek 144042a
All tests pass, maybe it works?
trzysiek a2c621e
Linter
trzysiek 576b195
Minor, now should be all
trzysiek 532f39e
Merge branch 'main' into persistent-storage-2
trzysiek 47229e0
Fix linter
trzysiek 47cc725
Fix config data for smoke test
trzysiek 385fcb3
Debug for smoke test
trzysiek c37e8e3
Some fixes for smoke test
trzysiek aa72974
Merge branch 'main' into persistent-storage-2
trzysiek 57982de
unskip all tests
trzysiek 821b872
Reskip tests
trzysiek a96bd11
Style
trzysiek 3921080
small add test
trzysiek eb17c02
Merge branch 'main' into persistent-storage-2
trzysiek b4ef499
Merge branch 'main' into persistent-storage-2
trzysiek 45239e3
Cleanup
trzysiek 51b1e68
Cleanup 2
trzysiek 89a5180
Fix all tests
trzysiek dc40a90
Fix linter
trzysiek ffdc79e
Final if manual test passes
trzysiek cb402dc
Last: 1 name
trzysiek 9afe54a
merge main
nablaone 1499a49
Linter
nablaone a72f606
Unify error handling
nablaone 6a72ca4
Remove gorountines. Simplification
nablaone 96e9fa6
fmt
nablaone 44b337c
Fix error handling
nablaone 1abc897
Merge branch 'main' into persistent-storage-2
8419c0e
Fix compilation after merge
161407b
Merge branch 'main' into persistent-storage-2
trzysiek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // Copyright Quesma, licensed under the Elastic License 2.0. | ||
| // SPDX-License-Identifier: Elastic-2.0 | ||
| package persistence | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/gob" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "quesma/logger" | ||
| "quesma/quesma/config" | ||
| ) | ||
|
|
||
| const MAX_DOC_COUNT = 10000 // prototype TODO: fix/make configurable/idk/etc | ||
|
|
||
| // so far I serialize entire struct and keep only 1 string in ES | ||
| type ElasticDatabaseWithEviction struct { | ||
| ctx context.Context | ||
| *ElasticJSONDatabase // maybe remove and copy fields here | ||
| EvictorInterface | ||
| sizeInBytesLimit int64 | ||
| } | ||
|
|
||
| func NewElasticDatabaseWithEviction(ctx context.Context, cfg config.ElasticsearchConfiguration, indexName string, sizeInBytesLimit int64) *ElasticDatabaseWithEviction { | ||
| return &ElasticDatabaseWithEviction{ | ||
| ElasticJSONDatabase: NewElasticJSONDatabase(cfg, indexName), | ||
| EvictorInterface: &Evictor{}, | ||
| sizeInBytesLimit: sizeInBytesLimit, | ||
| } | ||
| } | ||
|
|
||
| // mutexy? or what | ||
| func (db *ElasticDatabaseWithEviction) Put(id string, row Sizeable) bool { | ||
| bytesNeeded := db.SizeInBytes() + row.SizeInBytes() | ||
| if bytesNeeded > db.SizeInBytesLimit() { | ||
| logger.InfoWithCtx(db.ctx).Msg("Database is full, evicting documents") | ||
| //docsToEvict, bytesEvicted := db.SelectToEvict(db.getAll(), bytesNeeded-db.SizeInBytesLimit()) | ||
| //db.evict(docsToEvict) | ||
| //bytesNeeded -= bytesEvicted | ||
| } | ||
| if bytesNeeded > db.SizeInBytesLimit() { | ||
| // put document | ||
| return false | ||
| } | ||
|
|
||
| serialized, err := db.serialize(row) | ||
| if err != nil { | ||
| logger.WarnWithCtx(db.ctx).Msg("Error serializing document, id:" + id) | ||
| return false | ||
| } | ||
|
|
||
| err = db.ElasticJSONDatabase.Put(id, serialized) | ||
| if err != nil { | ||
| logger.WarnWithCtx(db.ctx).Msgf("Error putting document, id: %s, error: %v", id, err) | ||
| return false | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| // co zwraca? zrobić switch na oba typy jakie teraz mamy? | ||
| func (db *ElasticDatabaseWithEviction) Get(id string) (string, bool) { // probably change return type to *Sizeable | ||
| value, success, err := db.ElasticJSONDatabase.Get(id) | ||
| if err != nil { | ||
| logger.WarnWithCtx(db.ctx).Msgf("Error getting document, id: %s, error: %v", id, err) | ||
| return "", false | ||
| } | ||
| return value, success | ||
| } | ||
|
|
||
| func (db *ElasticDatabaseWithEviction) Delete(id string) { | ||
| // mark as deleted, don't actually delete | ||
| // (single document deletion is hard in ES, it's done by evictor for entire index) | ||
| } | ||
|
|
||
| func (db *ElasticDatabaseWithEviction) DocCount() (count int, success bool) { | ||
| // TODO: add WHERE not_deleted | ||
|
|
||
| // Build the query to get only document IDs | ||
| elasticsearchURL := fmt.Sprintf("%s/_search", db.indexName) | ||
| query := `{ | ||
| "_source": false, | ||
| "size": 0, | ||
| "track_total_hits": true | ||
| }` | ||
|
|
||
| resp, err := db.httpClient.Request(context.Background(), "GET", elasticsearchURL, []byte(query)) | ||
| defer resp.Body.Close() | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| jsonAsBytes, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| switch resp.StatusCode { | ||
| case http.StatusOK: | ||
| break | ||
| default: | ||
| logger.WarnWithCtx(db.ctx).Msgf("failed to get from elastic: %s, response status code: %v", string(jsonAsBytes), resp.StatusCode) | ||
| return | ||
| } | ||
|
|
||
| // Unmarshal the JSON response | ||
| var result map[string]interface{} | ||
| if err = json.Unmarshal(jsonAsBytes, &result); err != nil { | ||
| logger.WarnWithCtx(db.ctx).Msgf("Error parsing the response JSON: %s", err) | ||
| return | ||
| } | ||
|
|
||
| count = int(result["hits"].(map[string]interface{})["total"].(map[string]interface{})["value"].(float64)) // TODO: add some checks... to prevent panic | ||
| return count, true | ||
| } | ||
|
|
||
| func (db *ElasticDatabaseWithEviction) SizeInBytes() (sizeInBytes int64, success bool) { | ||
| elasticsearchURL := fmt.Sprintf("%s/_search", db.indexName) | ||
|
|
||
| // Build the query to get only document IDs | ||
| query := fmt.Sprintf(`{"_source": false, "size": %d}`, MAX_DOC_COUNT) | ||
| } | ||
|
|
||
| func (db *ElasticDatabaseWithEviction) SizeInBytesLimit() int64 { | ||
| return db.sizeInBytesLimit | ||
| } | ||
|
|
||
| func (db *ElasticDatabaseWithEviction) getAll() *basicDocumentInfo { | ||
| // send query | ||
| return nil | ||
| } | ||
|
|
||
| func (db *ElasticDatabaseWithEviction) evict(documents []*basicDocumentInfo) { | ||
|
|
||
| } | ||
|
|
||
| func (db *ElasticDatabaseWithEviction) serialize(row Sizeable) (serialized string, err error) { | ||
| var b bytes.Buffer | ||
|
|
||
| enc := gob.NewEncoder(&b) // maybe create 1 encoder forever | ||
| if err = enc.Encode(row); err != nil { | ||
| fmt.Println("Error encoding struct:", err) | ||
| return | ||
| } | ||
|
|
||
| return b.String(), nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Copyright Quesma, licensed under the Elastic License 2.0. | ||
| // SPDX-License-Identifier: Elastic-2.0 | ||
| package persistence | ||
|
|
||
| type EvictorInterface interface { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not used. We may delete it. |
||
| SelectToEvict(documents []*basicDocumentInfo, sizeNeeded int64) (evictThese []*basicDocumentInfo, bytesEvicted int64) | ||
| } | ||
|
|
||
| // It's only 1 implementation, which looks well suited for ElasticSearch. | ||
| // It can be implemented differently. | ||
| type Evictor struct{} | ||
|
|
||
| func (e *Evictor) SelectToEvict(documents []*basicDocumentInfo, sizeNeeded int64) (evictThese []*basicDocumentInfo, bytesEvicted int64) { | ||
| if sizeNeeded <= 0 { | ||
| return // check if it's empty array or nil | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.