Skip to content

Commit

Permalink
implement new service start style
Browse files Browse the repository at this point in the history
while using new instance db.
  • Loading branch information
ilgooz committed Jun 5, 2019
1 parent 62fba75 commit 6881295
Show file tree
Hide file tree
Showing 20 changed files with 576 additions and 39 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Config struct {

Database struct {
ServiceRelativePath string
InstanceRelativePath string
ExecutionRelativePath string
}
}
Expand Down Expand Up @@ -81,6 +82,7 @@ func New() (*Config, error) {
c.Core.Name = "engine"
c.Core.Path = filepath.Join(home, ".mesg")
c.Core.Database.ServiceRelativePath = filepath.Join("database", "services", serviceDBVersion)
c.Core.Database.InstanceRelativePath = filepath.Join("database", "instances", serviceDBVersion)
c.Core.Database.ExecutionRelativePath = filepath.Join("database", "executions", executionDBVersion)
c.Docker.Core.Path = "/mesg"
c.Docker.Socket = "/var/run/docker.sock"
Expand Down
8 changes: 7 additions & 1 deletion core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func initDependencies() (*dependencies, error) {
return nil, err
}

// init instance db.
instanceDB, err := database.NewInstanceDB(filepath.Join(config.Core.Path, config.Core.Database.InstanceRelativePath))
if err != nil {
return nil, err
}

// init execution db.
executionDB, err := database.NewExecutionDB(filepath.Join(config.Core.Path, config.Core.Database.ExecutionRelativePath))
if err != nil {
Expand All @@ -54,7 +60,7 @@ func initDependencies() (*dependencies, error) {
m := dockermanager.New(c)

// init sdk.
sdk := sdk.New(m, c, serviceDB, executionDB)
sdk := sdk.New(m, c, serviceDB, instanceDB, executionDB)

return &dependencies{
config: config,
Expand Down
104 changes: 104 additions & 0 deletions database/instance_db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package database

import (
"encoding/json"

"github.com/mesg-foundation/core/service"
"github.com/syndtr/goleveldb/leveldb"
)

// InstanceDB describes the API of Instance database.
type InstanceDB interface {
// Get retrives instance by instance hash.
Get(hash string) (*service.Instance, error)

// Save saves instance to database.
Save(i *service.Instance) error

// Close closes underlying database connection.
Close() error
}

// LevelDBInstanceDB is a database for storing services' instances.
type LevelDBInstanceDB struct {
db *leveldb.DB
}

// NewInstanceDB returns the database which is located under given path.
func NewInstanceDB(path string) (*LevelDBInstanceDB, error) {
db, err := leveldb.OpenFile(path, nil)
if err != nil {
return nil, err
}
return &LevelDBInstanceDB{db: db}, nil
}

// marshal returns the byte slice from service.
func (d *LevelDBInstanceDB) marshal(i *service.Instance) ([]byte, error) {
return json.Marshal(i)
}

// unmarshal returns the service from byte slice.
func (d *LevelDBInstanceDB) unmarshal(id string, value []byte) (*service.Instance, error) {
var s service.Instance
if err := json.Unmarshal(value, &s); err != nil {
return nil, &DecodeError{ID: id}
}
return &s, nil
}

// Get retrives instance by instance hash.
func (d *LevelDBInstanceDB) Get(hash string) (*service.Instance, error) {
tx, err := d.db.OpenTransaction()
if err != nil {
return nil, err
}
b, err := tx.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()
}

// Save saves instance to database.
func (d *LevelDBInstanceDB) Save(i *service.Instance) error {
// check service
if i.Hash == "" {
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()
}

// Close closes database.
func (d *LevelDBInstanceDB) Close() error {
return d.db.Close()
}
222 changes: 222 additions & 0 deletions protobuf/api/instance.pb.go

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

18 changes: 18 additions & 0 deletions protobuf/api/instance.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";

package api;

service Instance {
rpc Create (CreateInstanceRequest) returns (CreateInstanceResponse) {}
}

message CreateInstanceRequest {
string id = 1; // Service's sid or hash.
repeated string env = 2; // Env vars to apply to service's instance on runtime.
}

message CreateInstanceResponse {
string sid = 1; // Service's id.
string hash = 2; // Service's instance hash.
string serviceHash = 3; // Service's bare hash.
}
1 change: 1 addition & 0 deletions scripts/build-proto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ GRPC_PLUGIN="--go_out=plugins=grpc,paths=source_relative:."
protoc $GRPC_PLUGIN --proto_path=$PROJECT $GRPC/definition/execution.proto
protoc $GRPC_PLUGIN --proto_path=$PROJECT $GRPC/definition/service.proto
protoc $GRPC_PLUGIN --proto_path=$PROJECT $GRPC/api/service.proto
protoc $GRPC_PLUGIN --proto_path=$PROJECT $GRPC/api/instance.proto
protoc $GRPC_PLUGIN --proto_path=$PROJECT $GRPC/coreapi/api.proto
protoc $GRPC_PLUGIN --proto_path=$PROJECT $GRPC/api/api.proto
protoc $GRPC_PLUGIN --proto_path=$PROJECT $GRPC/serviceapi/api.proto
Loading

0 comments on commit 6881295

Please sign in to comment.