Skip to content

Commit

Permalink
Changes to fix bugs found during certification testing
Browse files Browse the repository at this point in the history
  • Loading branch information
nmalocicvega committed Dec 20, 2024
1 parent 547d657 commit 5451108
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 30 deletions.
12 changes: 11 additions & 1 deletion state/ravendb/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ metadata:
- name: KeyPath
description: |
Path to the key for secure connection
example: "/path/to/key"
example: "/path/to/key"
- name: EnableTTL
description: |
Boolean values that enables or disables RaveDB TTL functionality
example: "true"
default: "true"
- name: TTLFrequency
description: |
Sets RavenDB frequency on running background task and deleting records
example: "15"
default: "60"
87 changes: 59 additions & 28 deletions state/ravendb/ravendb.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ const (
certPath = "certPath"
keyPath = "keyPath"
enableTTL = "enableTTL"
ttlFrequency = "ttlFrequency"
changeVector = "@change-vector"
expires = "@expires"
defaultEnableTTL = true
defaultTTLFrequency = int64(60)
)

type RavenDB struct {
Expand All @@ -64,6 +66,7 @@ type RavenDBMetadata struct {
CertPath string
KeyPath string
EnableTTL bool
TTLFrequency int64
}

type Item struct {
Expand Down Expand Up @@ -93,25 +96,15 @@ func (r *RavenDB) Init(ctx context.Context, metadata state.Metadata) (err error)
return err
}

fmt.Println("parsed metadata")
//TODO: Operation timeout?
store, err := r.getRavenDBStore(ctx)
if err != nil {
return fmt.Errorf("error in creating Raven DB Store")
}

configurationExppiration := ravendb.ExpirationConfiguration{
Disabled: !r.metadata.EnableTTL,
}
operation, err := ravendb.NewConfigureExpirationOperationWithConfiguration(&configurationExppiration)
if err != nil {
return fmt.Errorf("error in creating expiration operation")
}

err = store.Maintenance().Send(operation)
if err != nil {
return fmt.Errorf("error in sending expiration operation")
}

r.initTTL(store)
r.setupDatabase(store)
r.documentStore = store

return nil
Expand Down Expand Up @@ -152,32 +145,32 @@ func (r *RavenDB) Get(ctx context.Context, req *state.GetRequest) (*state.GetRes
var item *Item
err = session.Load(&item, req.Key)
if err != nil {
return &state.GetResponse{}, fmt.Errorf("error storing data %s", err)
return &state.GetResponse{}, fmt.Errorf("error loading data %s", err)
}
if item == nil {
return &state.GetResponse{}, nil
}
ravenMeta, err := session.GetMetadataFor(item)
if err != nil {
return &state.GetResponse{}, fmt.Errorf("error getting metadata for %s", req.Key)
}
var ttlResp string
var etagResp string

var meta map[string]string
var ttl, okTTL = ravenMeta.Get("@Expires")
var ttl, okTTL = ravenMeta.Get(expires)
if okTTL {
ttlResp = ttl.(string)
} else {
ttlResp = ""
meta = map[string]string{
state.GetRespMetaKeyTTLExpireTime: ttl.(string),
}
}
var eTag, okETag = ravenMeta.Get("@ChangeVector")

var etagResp string
var eTag, okETag = ravenMeta.Get(changeVector)
if okETag {
etagResp = eTag.(string)
} else {
etagResp = ""
}

meta = map[string]string{
state.GetRespMetaKeyTTLExpireTime: ttlResp,
}

resp := &state.GetResponse{
Data: []byte(item.Value),
ETag: &etagResp,
Expand All @@ -200,6 +193,7 @@ func (r *RavenDB) Set(ctx context.Context, req *state.SetRequest) error {

err = session.SaveChanges()
if err != nil {
fmt.Println("error saving changes:", err)
return fmt.Errorf("error saving changes %s", err)
}
return nil
Expand Down Expand Up @@ -315,6 +309,7 @@ func (r *RavenDB) setInternal(ctx context.Context, req *state.SetRequest, sessio
// First write wins, we send empty change vector to check if exists
err = session.StoreWithChangeVectorAndID(item, "", req.Key)
if err != nil {
fmt.Println(err)
return fmt.Errorf("error storing data: %s", err)
}
} else {
Expand All @@ -327,6 +322,7 @@ func (r *RavenDB) setInternal(ctx context.Context, req *state.SetRequest, sessio
}

if err != nil {
fmt.Println(err)
return fmt.Errorf("error storing data: %s", err)
}
}
Expand All @@ -343,9 +339,8 @@ func (r *RavenDB) setInternal(ctx context.Context, req *state.SetRequest, sessio
}
expiry := time.Now().Add(time.Second * time.Duration(*reqTTL)).UTC()
iso8601String := expiry.Format("2006-01-02T15:04:05.9999999Z07:00")
metaData.Put("@expires", iso8601String)
metaData.Put(expires, iso8601String)
}

return nil
}

Expand All @@ -362,6 +357,7 @@ func getRavenDBMetaData(meta state.Metadata) (RavenDBMetadata, error) {
m := RavenDBMetadata{
DatabaseName: defaultDatabaseName,
EnableTTL: defaultEnableTTL,
TTLFrequency: defaultTTLFrequency,
}

err := kitmd.DecodeMetadata(meta.Properties, &m)
Expand Down Expand Up @@ -413,6 +409,41 @@ func (r *RavenDB) Close() error {
}

r.documentStore.Close()

return nil
}

func (r *RavenDB) initTTL(store *ravendb.DocumentStore) {
configurationExppiration := ravendb.ExpirationConfiguration{
Disabled: !r.metadata.EnableTTL,
DeleteFrequencyInSec: &r.metadata.TTLFrequency,
}
operation, err := ravendb.NewConfigureExpirationOperationWithConfiguration(&configurationExppiration)
if err != nil {
fmt.Println("error setting expiration operation")
return
}

err = store.Maintenance().Send(operation)
if err != nil {
fmt.Println("error sending command")
}
}

func (r *RavenDB) setupDatabase(store *ravendb.DocumentStore) {
operation := ravendb.NewGetDatabaseRecordOperation(r.metadata.DatabaseName)
err := store.Maintenance().Server().Send(operation)
fmt.Println(err)
if err == nil {
if operation.Command != nil && operation.Command.RavenCommandBase.StatusCode == 404 {
databaseRecord := ravendb.DatabaseRecord{
DatabaseName: r.metadata.DatabaseName,
Disabled: false,
}
createOp := ravendb.NewCreateDatabaseOperation(&databaseRecord, 1)
err = store.Maintenance().Server().Send(createOp)
if err != nil {
return
}
}
}
}
6 changes: 5 additions & 1 deletion state/ravendb/ravendb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestGetRavenDBMetadata(t *testing.T) {
assert.Equal(t, properties[serverURL], metadata.ServerURL)
assert.Equal(t, defaultDatabaseName, metadata.DatabaseName)
assert.Equal(t, defaultEnableTTL, metadata.EnableTTL)
assert.Equal(t, defaultTTLFrequency, metadata.TTLFrequency)
})

t.Run("With custom database name", func(t *testing.T) {
Expand All @@ -38,13 +39,15 @@ func TestGetRavenDBMetadata(t *testing.T) {
assert.Equal(t, properties[serverURL], metadata.ServerURL)
assert.Equal(t, properties[databaseName], metadata.DatabaseName)
assert.Equal(t, defaultEnableTTL, metadata.EnableTTL)
assert.Equal(t, defaultTTLFrequency, metadata.TTLFrequency)
})

t.Run("With custom enable ttl value", func(t *testing.T) {
t.Run("With custom enable ttl values", func(t *testing.T) {
properties := map[string]string{
serverURL: "127.0.0.1",
databaseName: "TestDB",
enableTTL: "false",
ttlFrequency: "15",
}
m := state.Metadata{
Base: metadata.Base{Properties: properties},
Expand All @@ -55,6 +58,7 @@ func TestGetRavenDBMetadata(t *testing.T) {
assert.Equal(t, properties[serverURL], metadata.ServerURL)
assert.Equal(t, properties[databaseName], metadata.DatabaseName)
assert.Equal(t, false, metadata.EnableTTL)
assert.Equal(t, int64(15), metadata.TTLFrequency)
})

t.Run("with https without cert and key", func(t *testing.T) {
Expand Down

0 comments on commit 5451108

Please sign in to comment.