-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Persistent storage for asyncs 3 #895
Draft
trzysiek
wants to merge
3
commits into
main
Choose a base branch
from
persistent-storage-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+207
−16
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 { | ||
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 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 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 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to doc if
err != nil
body might benil
. So it looks like nil dereferencing or do I miss something?