Skip to content
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

Delete instance api #1037

Merged
merged 22 commits into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from 29 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
38 changes: 12 additions & 26 deletions database/instance_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type InstanceDB interface {
// Save saves instance to database.
Save(i *instance.Instance) error

// Delete an instance by instance hash.
Delete(hash string) error

// Close closes underlying database connection.
Close() error
}
Expand Down Expand Up @@ -49,24 +52,15 @@ func (d *LevelDBInstanceDB) unmarshal(id string, value []byte) (*instance.Instan

// Get retrives instance by instance hash.
func (d *LevelDBInstanceDB) Get(hash string) (*instance.Instance, error) {
tx, err := d.db.OpenTransaction()
if err != nil {
return nil, err
}
b, err := tx.Get([]byte(hash), nil)

b, err := d.db.Get([]byte(hash), nil)
if err != nil {
tx.Discard()
if err == leveldb.ErrNotFound {
return nil, &ErrNotFound{ID: hash}
}
return nil, err
}
i, err := d.unmarshal(hash, b)
if err != nil {
tx.Discard()
return nil, err
}
return i, tx.Commit()
return d.unmarshal(hash, b)
}

// Save saves instance to database.
Expand All @@ -76,29 +70,21 @@ func (d *LevelDBInstanceDB) Save(i *instance.Instance) error {
return errCannotSaveWithoutHash
}

// open database transaction
tx, err := d.db.OpenTransaction()
if err != nil {
return err
}

// encode service
b, err := d.marshal(i)
if err != nil {
tx.Discard()
return err
}

// save instance with hash.
if err := tx.Put([]byte(i.Hash), b, nil); err != nil {
tx.Discard()
return err
}

return tx.Commit()
return d.db.Put([]byte(i.Hash), b, nil)
}

// Close closes database.
func (d *LevelDBInstanceDB) Close() error {
return d.db.Close()
}

// Delete deletes service from database.
func (d *LevelDBInstanceDB) Delete(hash string) error {
return d.db.Delete([]byte(hash), nil)
}
81 changes: 81 additions & 0 deletions database/instance_db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package database

import (
"io/ioutil"
"os"
"testing"

"github.com/mesg-foundation/core/instance"
"github.com/stretchr/testify/require"
)

func instancedb(t *testing.T, dir string) InstanceDB {
db, err := NewInstanceDB(dir)
require.NoError(t, err)
return db
}

func TestFindInstance(t *testing.T) {
dir, _ := ioutil.TempDir("", "TestFindInstance")
defer os.RemoveAll(dir)
db := instancedb(t, dir)
defer db.Close()
i := &instance.Instance{Hash: "xxx", ServiceHash: "yyy"}
db.Save(i)
tests := []struct {
hash string
hasError bool
}{
{hash: i.Hash, hasError: false},
{hash: "yyy", hasError: true},
}
for _, test := range tests {
instance, err := db.Get(test.hash)
if test.hasError {
require.Error(t, err)
continue
}
require.NoError(t, err)
require.NotNil(t, instance)
e, err := db.Get(instance.Hash)
require.NoError(t, err)
require.NotNil(t, e)
}
}

func TestSaveInstance(t *testing.T) {
dir, _ := ioutil.TempDir("", "TestSaveInstance")
defer os.RemoveAll(dir)
db := instancedb(t, dir)
defer db.Close()
tests := []struct {
instance *instance.Instance
hasError bool
}{
{&instance.Instance{Hash: "xxx"}, false},
{&instance.Instance{}, true},
}
for _, test := range tests {
err := db.Save(test.instance)
if test.hasError {
require.Error(t, err)
continue
}
require.NoError(t, err)
}
}

func TestDeleteInstance(t *testing.T) {
dir, _ := ioutil.TempDir("", "TestDeleteInstance")
defer os.RemoveAll(dir)
db := instancedb(t, dir)
defer db.Close()
i := &instance.Instance{Hash: "xxx", ServiceHash: "yyy"}
db.Save(i)
require.NoError(t, db.Delete("xxx"))
inst, err := db.Get("xxx")
require.Nil(t, inst)
require.Error(t, err)

require.NoError(t, db.Delete("yyy"))
}
2 changes: 1 addition & 1 deletion dev
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ docker service create \
--name engine \
--tty \
--label com.docker.stack.namespace=engine --label com.docker.stack.image=mesg/engine:$VERSION \
--mount type=bind,source=/var/run/docker.sock,destination=/var/run/docker.sock --mount type=bind,source=$HOME/.mesg,destination=/home/root/.mesg \
--mount type=bind,source=/var/run/docker.sock,destination=/var/run/docker.sock --mount type=bind,source=$HOME/.mesg,destination=/root/.mesg \
--network engine --publish 50052:50052 \
mesg/engine:$VERSION

Expand Down
4 changes: 2 additions & 2 deletions instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ package instance
// This contains a reference to a service that is running.
// Multiple instances can run for the same service as long as they have different configurations
type Instance struct {
Hash string `hash:"-"`
ServiceHash string `hash:"serviceHash"`
Hash string
ServiceHash string
}
131 changes: 122 additions & 9 deletions protobuf/api/instance.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions protobuf/api/instance.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package api;

service Instance {
rpc Create (CreateInstanceRequest) returns (CreateInstanceResponse) {}
rpc Delete (DeleteInstanceRequest) returns (DeleteInstanceResponse) {}
}

message CreateInstanceRequest {
Expand All @@ -15,3 +16,11 @@ message CreateInstanceResponse {
string hash = 2; // Service's instance hash.
antho1404 marked this conversation as resolved.
Show resolved Hide resolved
string serviceHash = 3; // Service's bare hash.
}

message DeleteInstanceRequest {
string hash = 1; // Instance hash
}

message DeleteInstanceResponse {
string hash = 1; // Instance hash
Copy link
Contributor

@krhubert krhubert Jun 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why return instance hash in response if it was sent in the request

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind much about that, I can remove but the idea is that when we delete a resource we have the id of the resource delete in order to be able to have a full asynchrony application that can on one side send the request and on the other side just receive the result and know how to update a local store or anything.

But again I'm happy to delete it if needed

}
Loading