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

implement new service start style #1024

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
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.
}
Loading