Skip to content

Commit

Permalink
Merge branch 'master' of github.com:tomdionysus/trinity
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdionysus committed Dec 26, 2019
2 parents 8d83842 + 038c12e commit 8bc06d7
Show file tree
Hide file tree
Showing 28 changed files with 404 additions and 63 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ trinity-server --ca=<CA_PEM> --cert=<CERT_PEM> [other flags]

## Documentation

| Document | Description |
|:-------------------------------------|:-------------------------------------------------------|
| [Design Goals](docs/design-goals.md) | Design Goals of the project, roadmaps, reference |
| [Progress](docs/progress.md) | Project Progress |
| [Encryption](docs/encryption.md) | Encryption and security guide |
| Document | Description |
|:------------------------------------------------|:-------------------------------------------------|
| [Design Goals](docs/design-goals.md) | Design Goals of the project, roadmaps, reference |
| [Progress](docs/progress.md) | Project Progress |
| [Encryption](docs/encryption.md) | Encryption and security guide |
| [Consistency Modes](docs/consistency_modes.md) | Consistency Modes in Trinity |

## Component Package Repositories

Expand Down
11 changes: 11 additions & 0 deletions bin/launch_multiple_nodes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
# This script launch several node so it easier to test federation and data repartition

build/trinity-server --ca cert/ca.pem --cert cert/localhost.pem --loglevel info -memcache --memcacheport 11212 --node localhost:13531 --port 13532 &
build/trinity-server --ca cert/ca.pem --cert cert/localhost.pem --loglevel info -memcache --memcacheport 11213 --node localhost:13531 --port 13533 &
build/trinity-server --ca cert/ca.pem --cert cert/localhost.pem --loglevel info -memcache --memcacheport 11214 --node localhost:13531 --port 13534 &
build/trinity-server --ca cert/ca.pem --cert cert/localhost.pem --loglevel info -memcache --memcacheport 11215 --node localhost:13531 --port 13535 &
build/trinity-server --ca cert/ca.pem --cert cert/localhost.pem --loglevel info -memcache --memcacheport 11216 --node localhost:13531 --port 13536 &
build/trinity-server --ca cert/ca.pem --cert cert/localhost.pem --loglevel info -memcache --memcacheport 11217 --node localhost:13531 --port 13537 &
build/trinity-server --ca cert/ca.pem --cert cert/localhost.pem --loglevel info -memcache --memcacheport 11218 --node localhost:13531 --port 13538 &
build/trinity-server --ca cert/ca.pem --cert cert/localhost.pem --loglevel info -memcache --memcacheport 11219 --node localhost:13531 --port 13539 &
6 changes: 6 additions & 0 deletions bin/stress_test_connection.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
# This script just spawn and kill instance to see how the system react

while [ 1 ]; do
timeout 1 build/trinity-server --ca cert/ca.pem --cert cert/localhost.pem --loglevel info --node localhost:13531 --port 13539
done
6 changes: 4 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package config

import (
"errors"
"flag"
"fmt"
)

// Config struct hold config information for the node
type Config struct {
Nodes NodeURLs
CA *string
Expand All @@ -17,6 +17,7 @@ type Config struct {
HostAddr *string
}

// NewConfig init a new Config struct with default value
func NewConfig() *Config {
inst := &Config{}

Expand All @@ -38,10 +39,11 @@ func NewConfig() *Config {
return inst
}

// Validate configuration
func (cfg *Config) Validate() (bool, []error) {
errs := []error{}
if *cfg.Port < 0 || *cfg.Port > 65535 {
errs = append(errs, errors.New(fmt.Sprintf("Port %d is invalid (0-65535)", *cfg.Port)))
errs = append(errs, fmt.Errorf("Port %d is invalid (0-65535)", *cfg.Port))
}
return len(errs) == 0, errs
}
3 changes: 2 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package config

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewConfig(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions config/node_urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"
)

// NodeURLs Hold all the discovered nodes url as a list
type NodeURLs []string

// Set a new url to the list of nodes url
func (nurl *NodeURLs) Set(value string) error {
*nurl = append(*nurl, value)
return nil
Expand Down
3 changes: 2 additions & 1 deletion config/node_urls_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package config

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNodeURLs(t *testing.T) {
Expand Down
17 changes: 17 additions & 0 deletions docs/consistency_modes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
![Trinity DB Logo](../gfx/trinity_m.png)

# Trinity DB - Consistency Modes

Trinity is intended to have 3 consistency modes, selectable per client session.

## WRITE_UNCOMMITTED

Writes will return immediately - the write is not guaranteed to have been persisted to disk on any node node, the disk persistence and replication on nodes will happen asynchronously. This is the fastest and least consistent mode.

## WRITE_COMMITED

Writes will return when they have been persisted to disk on at least one node, the replication to other nodes will happen asynchronously. This is the **default** mode and represents a balance between performance and consistency.

## WRITE_REPLICATED

Writes will return only after they have been persisted on all replication nodes - the write is guaranteed to have been persisted across multiple nodes. This is the slowest and most consistent mode.
2 changes: 1 addition & 1 deletion docs/progress.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ Work continues on [bplustree](https://github.com/tomdionysus/bplustree) which wi

## BUGS

* In rare cicrumstances a node will be notified of its own address, and connect to itself.
* In rare circumstances a node will be notified of its own address, and connect to itself.
25 changes: 20 additions & 5 deletions kvstore/kvstore.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package kvstore

import (
"time"

bt "github.com/tomdionysus/binarytree"
ch "github.com/tomdionysus/consistenthash"
"github.com/tomdionysus/trinity/util"
"time"
)

// In Memory Key Value Store for testing.
// KVStore In Memory Key Value Store for testing.
type KVStore struct {
Logger *util.Logger
store *bt.Tree
expiry map[int64][]ch.Key
running bool
}

// Item struct represent an entry in the store
type Item struct {
Key string
Data []byte
Flags int16
}

// NewKVStore create and initialize a new KVStore
func NewKVStore(logger *util.Logger) *KVStore {
inst := &KVStore{
Logger: logger,
Expand All @@ -31,14 +34,21 @@ func NewKVStore(logger *util.Logger) *KVStore {
return inst
}

// Init the KVStore
func (kvs *KVStore) Init() {
kvs.Logger.Debug("Memcache", "Init")
kvs.Logger.Debug("KVStore", "Init")
}

// Start the KVStore goroutine
// This goroutine looks for expiry of value and remove them from the KVStore
func (kvs *KVStore) Start() {
if kvs.running {
return
}

kvs.running = true
go func() {
kvs.Logger.Debug("Memcache", "Started")
kvs.Logger.Debug("KVStore", "Started")
for kvs.running {
expiretime := time.Now().UTC().Unix()
toexpire, found := kvs.expiry[expiretime]
Expand All @@ -49,15 +59,17 @@ func (kvs *KVStore) Start() {
}
delete(kvs.expiry, expiretime)
}
time.Sleep(900 * time.Millisecond)
time.Sleep(500 * time.Millisecond)
}
}()
}

// Stop the value expiry
func (kvs *KVStore) Stop() {
kvs.running = false
}

// Set a value in the KVStore
func (kvs *KVStore) Set(key string, value []byte, flags int16, expiry *time.Time) {
keymd5 := ch.NewMD5Key(key)
item := &Item{Key: key, Data: value, Flags: flags}
Expand All @@ -72,11 +84,13 @@ func (kvs *KVStore) Set(key string, value []byte, flags int16, expiry *time.Time
}
}

// IsSet return if a key is set in the store
func (kvs *KVStore) IsSet(key string) bool {
_, _, isset := kvs.Get(key)
return isset
}

// Get a value by key from the store
func (kvs *KVStore) Get(key string) ([]byte, int16, bool) {
keymd5 := ch.NewMD5Key(key)
ok, valueInt := kvs.store.Get(keymd5)
Expand All @@ -90,6 +104,7 @@ func (kvs *KVStore) Get(key string) ([]byte, int16, bool) {
}
}

// Delete a value by key from the store
func (kvs *KVStore) Delete(key string) bool {
kvs.Logger.Debug("KVStore", "DELETE [%s]", key)
return kvs.deleteKey(ch.NewMD5Key(key))
Expand Down
5 changes: 3 additions & 2 deletions kvstore/kvstore_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package kvstore

import (
"github.com/stretchr/testify/assert"
"github.com/tomdionysus/trinity/util"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/tomdionysus/trinity/util"
)

func TestNewKVStore(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/tomdionysus/trinity/kvstore"
"github.com/tomdionysus/trinity/network"
"github.com/tomdionysus/trinity/util"

// "github.com/tomdionysus/trinity/packets"
"os"
)
Expand Down
14 changes: 9 additions & 5 deletions network/ca_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package network
import (
"crypto/x509"
"errors"
"github.com/tomdionysus/trinity/util"
"io/ioutil"

"github.com/tomdionysus/trinity/util"
)

// CAPool struct represent a pool of x509 certificate
type CAPool struct {
Pool *x509.CertPool
Logger *util.Logger
}

// NewCAPool Create and initialise a CAPool
func NewCAPool(logger *util.Logger) *CAPool {
inst := &CAPool{
Pool: x509.NewCertPool(),
Expand All @@ -21,15 +24,16 @@ func NewCAPool(logger *util.Logger) *CAPool {
return inst
}

func (cp *CAPool) LoadPEM(certFile string) error {
cabytes, err := ioutil.ReadFile(certFile)
// LoadPEM load a PEM file by name
func (cp *CAPool) LoadPEM(certFileName string) error {
cabytes, err := ioutil.ReadFile(certFileName)
if err != nil {
cp.Logger.Error("CAPool", "Cannot Load CA File '%s': %s", certFile, err.Error())
cp.Logger.Error("CAPool", "Cannot Load CA File '%s': %s", certFileName, err.Error())
return err
}

if !cp.Pool.AppendCertsFromPEM(cabytes) {
cp.Logger.Error("CAPool", "Cannot Parse PEM CA File '%s'", certFile)
cp.Logger.Error("CAPool", "Cannot Parse PEM CA File '%s'", certFileName)
return errors.New("Cannot Parse CA File")
}

Expand Down
3 changes: 2 additions & 1 deletion network/ca_pool_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package network

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/tomdionysus/trinity/util"
"testing"
)

func TestNewCAPool(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions network/ciphers.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package network

// Ciphers exports the list of supported cypher suit
var Ciphers map[uint16]string = map[uint16]string{
0x0005: "TLS_RSA_WITH_RC4_128_SHA",
0x000a: "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
Expand Down

0 comments on commit 8bc06d7

Please sign in to comment.