Skip to content

Commit

Permalink
GetForSession now expects a *types.Session instead of a string
Browse files Browse the repository at this point in the history
  • Loading branch information
xetorthio committed Sep 20, 2017
1 parent 495219c commit 2a26109
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 36 deletions.
2 changes: 1 addition & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
task.NewCheckPorts(e, f),
task.NewCheckSwarmPorts(e, f),
task.NewCheckSwarmStatus(e, f),
task.NewCollectStats(e, f),
task.NewCollectStats(e, f, s),
}
sch, err := scheduler.NewScheduler(tasks, s, e, core)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion docker/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

type FactoryApi interface {
GetForSession(sessionId string) (DockerApi, error)
GetForSession(session *types.Session) (DockerApi, error)
GetForInstance(instance *types.Instance) (DockerApi, error)
}

Expand Down
4 changes: 2 additions & 2 deletions docker/factory_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type FactoryMock struct {
mock.Mock
}

func (m *FactoryMock) GetForSession(sessionId string) (DockerApi, error) {
args := m.Called(sessionId)
func (m *FactoryMock) GetForSession(session *types.Session) (DockerApi, error) {
args := m.Called(session)
return args.Get(0).(DockerApi), args.Error(1)
}

Expand Down
2 changes: 1 addition & 1 deletion docker/local_cached_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type instanceEntry struct {
client DockerApi
}

func (f *localCachedFactory) GetForSession(sessionId string) (DockerApi, error) {
func (f *localCachedFactory) GetForSession(session *types.Session) (DockerApi, error) {
f.rw.Lock()
defer f.rw.Unlock()

Expand Down
60 changes: 51 additions & 9 deletions provisioner/dind.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"strings"

lru "github.com/hashicorp/golang-lru"
"github.com/play-with-docker/play-with-docker/config"
"github.com/play-with-docker/play-with-docker/docker"
"github.com/play-with-docker/play-with-docker/id"
Expand All @@ -22,10 +23,12 @@ type DinD struct {
factory docker.FactoryApi
storage storage.StorageApi
generator id.Generator
cache *lru.Cache
}

func NewDinD(generator id.Generator, f docker.FactoryApi, s storage.StorageApi) *DinD {
return &DinD{generator: generator, factory: f, storage: s}
c, _ := lru.New(5000)
return &DinD{generator: generator, factory: f, storage: s, cache: c}
}

func checkHostnameExists(sessionId, hostname string, instances []*types.Instance) bool {
Expand Down Expand Up @@ -73,7 +76,7 @@ func (d *DinD) InstanceNew(session *types.Session, conf types.InstanceConfig) (*
Networks: []string{session.Id},
}

dockerClient, err := d.factory.GetForSession(session.Id)
dockerClient, err := d.factory.GetForSession(session)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -105,8 +108,23 @@ func (d *DinD) InstanceNew(session *types.Session, conf types.InstanceConfig) (*
return instance, nil
}

func (d *DinD) getSession(sessionId string) (*types.Session, error) {
var session *types.Session
if s, found := d.cache.Get(sessionId); !found {
s, err := d.storage.SessionGet(sessionId)
if err != nil {
return nil, err
}
session = s
d.cache.Add(sessionId, s)
} else {
session = s.(*types.Session)
}
return session, nil
}

func (d *DinD) InstanceDelete(session *types.Session, instance *types.Instance) error {
dockerClient, err := d.factory.GetForSession(session.Id)
dockerClient, err := d.factory.GetForSession(session)
if err != nil {
return err
}
Expand All @@ -118,23 +136,35 @@ func (d *DinD) InstanceDelete(session *types.Session, instance *types.Instance)
}

func (d *DinD) InstanceExec(instance *types.Instance, cmd []string) (int, error) {
dockerClient, err := d.factory.GetForSession(instance.SessionId)
session, err := d.getSession(instance.SessionId)
if err != nil {
return -1, err
}
dockerClient, err := d.factory.GetForSession(session)
if err != nil {
return -1, err
}
return dockerClient.Exec(instance.Name, cmd)
}

func (d *DinD) InstanceResizeTerminal(instance *types.Instance, rows, cols uint) error {
dockerClient, err := d.factory.GetForSession(instance.SessionId)
session, err := d.getSession(instance.SessionId)
if err != nil {
return err
}
dockerClient, err := d.factory.GetForSession(session)
if err != nil {
return err
}
return dockerClient.ContainerResize(instance.Name, rows, cols)
}

func (d *DinD) InstanceGetTerminal(instance *types.Instance) (net.Conn, error) {
dockerClient, err := d.factory.GetForSession(instance.SessionId)
session, err := d.getSession(instance.SessionId)
if err != nil {
return nil, err
}
dockerClient, err := d.factory.GetForSession(session)
if err != nil {
return nil, err
}
Expand All @@ -151,7 +181,11 @@ func (d *DinD) InstanceUploadFromUrl(instance *types.Instance, fileName, dest, u
if resp.StatusCode != 200 {
return fmt.Errorf("Could not download file [%s]. Status code: %d\n", url, resp.StatusCode)
}
dockerClient, err := d.factory.GetForSession(instance.SessionId)
session, err := d.getSession(instance.SessionId)
if err != nil {
return err
}
dockerClient, err := d.factory.GetForSession(session)
if err != nil {
return err
}
Expand All @@ -166,7 +200,11 @@ func (d *DinD) InstanceUploadFromUrl(instance *types.Instance, fileName, dest, u
}

func (d *DinD) getInstanceCWD(instance *types.Instance) (string, error) {
dockerClient, err := d.factory.GetForSession(instance.SessionId)
session, err := d.getSession(instance.SessionId)
if err != nil {
return "", err
}
dockerClient, err := d.factory.GetForSession(session)
if err != nil {
return "", err
}
Expand All @@ -184,7 +222,11 @@ func (d *DinD) getInstanceCWD(instance *types.Instance) (string, error) {
}

func (d *DinD) InstanceUploadFromReader(instance *types.Instance, fileName, dest string, reader io.Reader) error {
dockerClient, err := d.factory.GetForSession(instance.SessionId)
session, err := d.getSession(instance.SessionId)
if err != nil {
return err
}
dockerClient, err := d.factory.GetForSession(session)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions provisioner/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewOverlaySessionProvisioner(df docker.FactoryApi) SessionProvisionerApi {
}

func (p *overlaySessionProvisioner) SessionNew(s *types.Session) error {
dockerClient, err := p.dockerFactory.GetForSession(s.Id)
dockerClient, err := p.dockerFactory.GetForSession(s)
if err != nil {
// We assume we are out of capacity
return fmt.Errorf("Out of capacity")
Expand Down Expand Up @@ -52,7 +52,7 @@ func (p *overlaySessionProvisioner) SessionNew(s *types.Session) error {
}
func (p *overlaySessionProvisioner) SessionClose(s *types.Session) error {
// Disconnect L2 router from the network
dockerClient, err := p.dockerFactory.GetForSession(s.Id)
dockerClient, err := p.dockerFactory.GetForSession(s)
if err != nil {
log.Println(err)
return err
Expand Down
4 changes: 2 additions & 2 deletions provisioner/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (d *windows) InstanceNew(session *types.Session, conf types.InstanceConfig)
}
instanceName := fmt.Sprintf("%s_%s", session.Id[:8], winfo.id)

dockerClient, err := d.factory.GetForSession(session.Id)
dockerClient, err := d.factory.GetForSession(session)
if err != nil {
d.releaseInstance(winfo.id)
return nil, err
Expand Down Expand Up @@ -94,7 +94,7 @@ func (d *windows) InstanceNew(session *types.Session, conf types.InstanceConfig)
}

func (d *windows) InstanceDelete(session *types.Session, instance *types.Instance) error {
dockerClient, err := d.factory.GetForSession(session.Id)
dockerClient, err := d.factory.GetForSession(session)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pwd/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestClientNew(t *testing.T) {
sp := provisioner.NewOverlaySessionProvisioner(_f)

_g.On("NewId").Return("aaaabbbbcccc")
_f.On("GetForSession", "aaaabbbbcccc").Return(_d, nil)
_f.On("GetForSession", mock.AnythingOfType("*types.Session")).Return(_d, nil)
_d.On("CreateNetwork", "aaaabbbbcccc", dtypes.NetworkCreate{Attachable: true, Driver: "overlay"}).Return(nil)
_d.On("GetDaemonHost").Return("localhost")
_d.On("ConnectNetwork", config.L2ContainerName, "aaaabbbbcccc", "").Return("10.0.0.1", nil)
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestClientCount(t *testing.T) {
sp := provisioner.NewOverlaySessionProvisioner(_f)

_g.On("NewId").Return("aaaabbbbcccc")
_f.On("GetForSession", "aaaabbbbcccc").Return(_d, nil)
_f.On("GetForSession", mock.AnythingOfType("*types.Session")).Return(_d, nil)
_d.On("CreateNetwork", "aaaabbbbcccc", dtypes.NetworkCreate{Attachable: true, Driver: "overlay"}).Return(nil)
_d.On("GetDaemonHost").Return("localhost")
_d.On("ConnectNetwork", config.L2ContainerName, "aaaabbbbcccc", "").Return("10.0.0.1", nil)
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestClientResizeViewPort(t *testing.T) {
sp := provisioner.NewOverlaySessionProvisioner(_f)

_g.On("NewId").Return("aaaabbbbcccc")
_f.On("GetForSession", "aaaabbbbcccc").Return(_d, nil)
_f.On("GetForSession", mock.AnythingOfType("*types.Session")).Return(_d, nil)
_d.On("CreateNetwork", "aaaabbbbcccc", dtypes.NetworkCreate{Attachable: true, Driver: "overlay"}).Return(nil)
_d.On("GetDaemonHost").Return("localhost")
_d.On("ConnectNetwork", config.L2ContainerName, "aaaabbbbcccc", "").Return("10.0.0.1", nil)
Expand Down
10 changes: 6 additions & 4 deletions pwd/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ func TestInstanceResizeTerminal(t *testing.T) {
ipf := provisioner.NewInstanceProvisionerFactory(provisioner.NewWindowsASG(_f, _s), provisioner.NewDinD(_g, _f, _s))
sp := provisioner.NewOverlaySessionProvisioner(_f)

s := &types.Session{Id: "aaaabbbbcccc"}
_d.On("ContainerResize", "foobar", uint(24), uint(80)).Return(nil)
_f.On("GetForSession", "aaaabbbbcccc").Return(_d, nil)
_s.On("SessionGet", "aaaabbbbcccc").Return(s, nil)
_f.On("GetForSession", s).Return(_d, nil)

p := NewPWD(_f, _e, _s, sp, ipf)

Expand All @@ -52,7 +54,7 @@ func TestInstanceNew(t *testing.T) {
sp := provisioner.NewOverlaySessionProvisioner(_f)

_g.On("NewId").Return("aaaabbbbcccc")
_f.On("GetForSession", "aaaabbbbcccc").Return(_d, nil)
_f.On("GetForSession", mock.AnythingOfType("*types.Session")).Return(_d, nil)
_d.On("CreateNetwork", "aaaabbbbcccc", dtypes.NetworkCreate{Attachable: true, Driver: "overlay"}).Return(nil)
_d.On("GetDaemonHost").Return("localhost")
_d.On("ConnectNetwork", config.L2ContainerName, "aaaabbbbcccc", "").Return("10.0.0.1", nil)
Expand Down Expand Up @@ -120,7 +122,7 @@ func TestInstanceNew_WithNotAllowedImage(t *testing.T) {
sp := provisioner.NewOverlaySessionProvisioner(_f)

_g.On("NewId").Return("aaaabbbbcccc")
_f.On("GetForSession", "aaaabbbbcccc").Return(_d, nil)
_f.On("GetForSession", mock.AnythingOfType("*types.Session")).Return(_d, nil)
_d.On("CreateNetwork", "aaaabbbbcccc", dtypes.NetworkCreate{Attachable: true, Driver: "overlay"}).Return(nil)
_d.On("GetDaemonHost").Return("localhost")
_d.On("ConnectNetwork", config.L2ContainerName, "aaaabbbbcccc", "").Return("10.0.0.1", nil)
Expand Down Expand Up @@ -189,7 +191,7 @@ func TestInstanceNew_WithCustomHostname(t *testing.T) {
sp := provisioner.NewOverlaySessionProvisioner(_f)

_g.On("NewId").Return("aaaabbbbcccc")
_f.On("GetForSession", "aaaabbbbcccc").Return(_d, nil)
_f.On("GetForSession", mock.AnythingOfType("*types.Session")).Return(_d, nil)
_d.On("CreateNetwork", "aaaabbbbcccc", dtypes.NetworkCreate{Attachable: true, Driver: "overlay"}).Return(nil)
_d.On("GetDaemonHost").Return("localhost")
_d.On("ConnectNetwork", config.L2ContainerName, "aaaabbbbcccc", "").Return("10.0.0.1", nil)
Expand Down
2 changes: 1 addition & 1 deletion pwd/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (p *pwd) SessionDeployStack(s *types.Session) error {

w := sessionBuilderWriter{sessionId: s.Id, event: p.event}

dockerClient, err := p.dockerFactory.GetForSession(s.Id)
dockerClient, err := p.dockerFactory.GetForSession(s)
if err != nil {
log.Println(err)
return err
Expand Down
2 changes: 1 addition & 1 deletion pwd/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestSessionNew(t *testing.T) {
sp := provisioner.NewOverlaySessionProvisioner(_f)

_g.On("NewId").Return("aaaabbbbcccc")
_f.On("GetForSession", "aaaabbbbcccc").Return(_d, nil)
_f.On("GetForSession", mock.AnythingOfType("*types.Session")).Return(_d, nil)
_d.On("CreateNetwork", "aaaabbbbcccc", dtypes.NetworkCreate{Attachable: true, Driver: "overlay"}).Return(nil)
_d.On("GetDaemonHost").Return("localhost")
_d.On("ConnectNetwork", config.L2ContainerName, "aaaabbbbcccc", "").Return("10.0.0.1", nil)
Expand Down
3 changes: 2 additions & 1 deletion scheduler/task/check_swarm_ports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestCheckSwarmPorts_RunWhenManager(t *testing.T) {
IP: "10.0.0.1",
Name: "aaaabbbb_node1",
SessionId: "aaaabbbbcccc",
Hostname: "node1",
}
info := dockerTypes.Info{
Swarm: swarm.Info{
Expand All @@ -43,7 +44,7 @@ func TestCheckSwarmPorts_RunWhenManager(t *testing.T) {
f.On("GetForInstance", i).Return(d, nil)
d.On("GetDaemonInfo").Return(info, nil)
d.On("GetSwarmPorts").Return([]string{"node1", "node2"}, []uint16{8080, 9090}, nil)
e.M.On("Emit", CheckSwarmPortsEvent, "aaaabbbbcccc", []interface{}{DockerSwarmPorts{Manager: i.Name, Instances: []string{i.Name, "aaaabbbb_node2"}, Ports: []int{8080, 9090}}}).Return()
e.M.On("Emit", CheckSwarmPortsEvent, "aaaabbbbcccc", []interface{}{DockerSwarmPorts{Manager: i.Name, Instances: []string{i.Hostname, "node2"}, Ports: []int{8080, 9090}}}).Return()

task := NewCheckSwarmPorts(e, f)
ctx := context.Background()
Expand Down
22 changes: 19 additions & 3 deletions scheduler/task/collect_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (

dockerTypes "github.com/docker/docker/api/types"
units "github.com/docker/go-units"
lru "github.com/hashicorp/golang-lru"
"github.com/play-with-docker/play-with-docker/docker"
"github.com/play-with-docker/play-with-docker/event"
"github.com/play-with-docker/play-with-docker/pwd/types"
"github.com/play-with-docker/play-with-docker/router"
"github.com/play-with-docker/play-with-docker/storage"
)

type InstanceStats struct {
Expand All @@ -28,6 +30,8 @@ type collectStats struct {
event event.EventApi
factory docker.FactoryApi
cli *http.Client
cache *lru.Cache
storage storage.StorageApi
}

var CollectStatsEvent event.EventType
Expand Down Expand Up @@ -71,7 +75,18 @@ func (t *collectStats) Run(ctx context.Context, instance *types.Instance) error
t.event.Emit(CollectStatsEvent, instance.SessionId, stats)
return nil
}
dockerClient, err := t.factory.GetForSession(instance.SessionId)
var session *types.Session
if sess, found := t.cache.Get(instance.SessionId); !found {
s, err := t.storage.SessionGet(instance.SessionId)
if err != nil {
return err
}
t.cache.Add(s.Id, s)
session = s
} else {
session = sess.(*types.Session)
}
dockerClient, err := t.factory.GetForSession(session)
if err != nil {
log.Println(err)
return err
Expand Down Expand Up @@ -119,7 +134,7 @@ func proxyHost(r *http.Request) (*url.URL, error) {
return u, nil
}

func NewCollectStats(e event.EventApi, f docker.FactoryApi) *collectStats {
func NewCollectStats(e event.EventApi, f docker.FactoryApi, s storage.StorageApi) *collectStats {
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 1 * time.Second,
Expand All @@ -131,7 +146,8 @@ func NewCollectStats(e event.EventApi, f docker.FactoryApi) *collectStats {
cli := &http.Client{
Transport: transport,
}
return &collectStats{event: e, factory: f, cli: cli}
c, _ := lru.New(5000)
return &collectStats{event: e, factory: f, cli: cli, cache: c, storage: s}
}

func calculateCPUPercentUnix(previousCPU, previousSystem uint64, v *dockerTypes.StatsJSON) float64 {
Expand Down
Loading

0 comments on commit 2a26109

Please sign in to comment.