Skip to content

Commit d095742

Browse files
author
Mikhail Podtserkovskiy
committed
strategies
- abstractions - tests - persistent strategy
1 parent 01dff73 commit d095742

22 files changed

+502
-82
lines changed

config-sample.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
"port": 4444,
1111
"strategy_list": [
1212
{
13-
"type": "default",
14-
"limit": 0
13+
"type": "persistent"
1514
}
1615
],
1716
"busy_node_duration": "15m",

config-test.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
"port": 4444,
1111
"strategy_list": [
1212
{
13-
"type": "default",
14-
"limit": 0
13+
"type": "persistent"
1514
}
1615
],
1716
"busy_node_duration": "15m",

config/config.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ type Grid struct {
2323
}
2424

2525
type Strategy struct {
26-
Type string `json:"type"`
27-
Limit int `json:"limit"`
26+
Config map[string]string `json:"config"` // ex. docker config, kubernetes config, etc.
27+
Type string `json:"type"`
28+
Limit int `json:"limit"`
29+
NodeList []Node `json:"node_list"`
30+
}
31+
32+
type Node struct {
33+
Config map[string]string `json:"config"` // ex. image_name, etc.
34+
Capabilities map[string]interface{} `json:"capabilities"`
35+
Limit int `json:"limit"`
2836
}
2937

3038
type Logger struct {

handlers/registerNode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (h *RegisterNode) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
4646
poolCapabilitiesList[i] = pool.Capabilities(value)
4747
}
4848
err = h.Pool.Add(
49-
pool.NodeTypeRegular,
49+
pool.NodeTypePersistent,
5050
register.Configuration.Host+":"+strconv.Itoa(register.Configuration.Port),
5151
poolCapabilitiesList,
5252
)

invoke.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@ import (
55
"github.com/qa-dev/jsonwire-grid/config"
66

77
"errors"
8-
"github.com/qa-dev/jsonwire-grid/storage"
8+
"github.com/qa-dev/jsonwire-grid/pool"
9+
"github.com/qa-dev/jsonwire-grid/pool/strategy/persistent"
910
"github.com/qa-dev/jsonwire-grid/storage/mysql"
1011
)
1112

12-
func invokeStorageFactory(config config.Config) (factory storage.StorageFactoryInterface, err error) {
13+
type StorageFactoryInterface interface {
14+
Create(config.Config) (pool.StorageInterface, error)
15+
}
16+
17+
type StrategyFactoryInterface interface {
18+
Create(config.Config, pool.StorageInterface) (pool.StrategyInterface, error)
19+
}
20+
21+
func invokeStorageFactory(config config.Config) (factory StorageFactoryInterface, err error) {
1322
switch config.DB.Implementation {
1423
case "mysql":
1524
factory = new(mysql.Factory)
@@ -18,3 +27,18 @@ func invokeStorageFactory(config config.Config) (factory storage.StorageFactoryI
1827
}
1928
return
2029
}
30+
31+
func invokeStrategyFactoryList(config config.Config, storage pool.StorageInterface) (factoryList []StrategyFactoryInterface, err error) {
32+
for _, strategyConfig := range config.Grid.StrategyList {
33+
switch strategyConfig.Type {
34+
case string(pool.NodeTypePersistent):
35+
factoryList = append(factoryList, new(persistent.StrategyFactory))
36+
//case string(pool.NodeTypeKubernetes):
37+
//factoryList = append(factoryList, new(kubernetes.StrategyFactory))
38+
default:
39+
err = errors.New("Undefined strategy type: " + strategyConfig.Type)
40+
return
41+
}
42+
}
43+
return
44+
}

main.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,21 @@ func main() {
5555
if err != nil {
5656
log.Fatalf("Can't create storage factory, %s", err)
5757
}
58-
poolInstance := pool.NewPool(storage)
58+
strategyFactoryList, err := invokeStrategyFactoryList(*cfg, storage)
59+
if err != nil {
60+
log.Fatalf("Can't create strategy factory list, %s", err)
61+
}
62+
var strategyList []pool.StrategyInterface
63+
for _, strategyFactory := range strategyFactoryList {
64+
strategy, err := strategyFactory.Create(*cfg, storage)
65+
if err != nil {
66+
log.Fatalf("Can't create strategy, %s", err)
67+
}
68+
strategyList = append(strategyList, strategy)
69+
}
70+
71+
strategyListStruct := pool.NewStrategyList(strategyList)
72+
poolInstance := pool.NewPool(storage, strategyListStruct)
5973
poolInstance.SetBusyNodeDuration(busyNodeDuration)
6074
poolInstance.SetReservedNodeDuration(reservedNodeDuration)
6175

pool/node.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const (
1111
type NodeType string
1212

1313
const (
14-
NodeTypeRegular NodeType = "regular"
15-
NodeTypeTemporary NodeType = "temporary"
14+
NodeTypePersistent NodeType = "persistent"
15+
NodeTypeKubernetes NodeType = "kubernetes"
1616
)
1717

1818
type Node struct {

pool/pool.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,25 @@ type StorageInterface interface {
2626
Remove(Node) error
2727
}
2828

29+
type StrategyInterface interface {
30+
Reserve(Capabilities) (Node, error)
31+
CleanUp(Node) error
32+
}
33+
2934
type Pool struct {
3035
storage StorageInterface
3136
busyNodeDuration time.Duration
3237
reservedNodeDuration time.Duration
38+
strategyList StrategyListInterface
3339
}
3440

35-
func NewPool(storage StorageInterface) *Pool {
36-
return &Pool{storage, defaultBusyNodeDuration, defaultReservedNodeDuration}
41+
func NewPool(storage StorageInterface, strategyList StrategyListInterface) *Pool {
42+
return &Pool{
43+
storage: storage,
44+
busyNodeDuration: defaultBusyNodeDuration,
45+
reservedNodeDuration: defaultReservedNodeDuration,
46+
strategyList: strategyList,
47+
}
3748
}
3849

3950
func (p *Pool) SetBusyNodeDuration(duration time.Duration) {
@@ -45,11 +56,12 @@ func (p *Pool) SetReservedNodeDuration(duration time.Duration) {
4556
}
4657

4758
// TODO: research close transaction and defer close mysql result body.
48-
func (p *Pool) ReserveAvailableNode(capabilities Capabilities) (*Node, error) {
49-
node, err := p.storage.ReserveAvailable(capabilities)
59+
func (p *Pool) ReserveAvailableNode(caps Capabilities) (*Node, error) {
60+
node, err := p.strategyList.Reserve(caps)
5061
if err != nil {
5162
err = errors.New("Can't reserve available node, " + err.Error())
5263
log.Error(err)
64+
return nil, err
5365
}
5466
return &node, err
5567
}
@@ -91,7 +103,7 @@ func (p *Pool) GetNodeByAddress(address string) (*Node, error) {
91103
}
92104

93105
func (p *Pool) CleanUpNode(node *Node) error {
94-
err := p.storage.SetAvailable(*node)
106+
err := p.strategyList.CleanUp(*node)
95107
if err != nil {
96108
err = errors.New("Can't clean up node: " + err.Error())
97109
log.Error(err)

0 commit comments

Comments
 (0)