Skip to content

Commit

Permalink
Cache all nodes instead the chosen one
Browse files Browse the repository at this point in the history
  • Loading branch information
labkode committed Jan 26, 2017
1 parent bffa47a commit 96e39cb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,28 @@ func New(logger levels.Levels, cm root.ContextManager, registryDriver root.Regis
}

func (c *webServiceClient) getAuthenticationURL(ctx context.Context) (string, error) {
u, ok := c.cache.Get("url")
var nodes []root.RegistryNode
v, ok := c.cache.Get("nodes")
if ok {
c.logger.Info().Log("msg", "authentication-node chosen from cache", "authentication-node-url", u.(string))
return u.(string), nil
c.logger.Info().Log("msg", "nodes obtained from cache")
nodes = v.([]root.RegistryNode)
} else {
ns, err := c.registryDriver.GetNodesForRol(ctx, "authentication-node")
if err != nil {
return "", err
}
if len(ns) == 0 {
return "", fmt.Errorf("there are not authentication-nodes alive")
}
c.logger.Info().Log("msg", "nodes obtained from registry")
nodes = ns
}
c.cache.Set("nodes", nodes, cache.DefaultExpiration)

// TODO(labkode) the logic for choosing a node is very rudimentary.
// In the future would be nice to have at least RoundRobin.
// Thanks that clients are registry aware we an use our own algorithms
// based on some prometheus metrics like load.
// TODO(labkode) add caching behaviour
nodes, err := c.registryDriver.GetNodesForRol(ctx, "authentication-node")
if err != nil {
return "", err
}
if len(nodes) == 0 {
return "", fmt.Errorf("there are not authentication-nodes alive")
}
c.logger.Info().Log("msg", "got authentication-nodes", "numnodes", len(nodes))
chosenNode := nodes[rand.Intn(len(nodes))]
c.logger.Info().Log("msg", "authentication-node chosen", "authentication-node-url", chosenNode.URL())
chosenURL := chosenNode.URL() + "/auth"
c.cache.Set("url", chosenURL, cache.DefaultExpiration)
return chosenURL, nil
}

Expand Down
34 changes: 17 additions & 17 deletions root/datawebserviceclient/datawebserviceclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,29 @@ func New(logger levels.Levels, cm root.ContextManager, registryDriver root.Regis
}

func (c *webServiceClient) getDataURL(ctx context.Context) (string, error) {
u, ok := c.cache.Get("url")
var nodes []root.RegistryNode

v, ok := c.cache.Get("nodes")
if ok {
c.logger.Info().Log("msg", "data-node chosen from cache", "data-node-url", u.(string))
return u.(string), nil
}
c.logger.Info().Log("msg", "nodes obtained from cache")
nodes = v.([]root.RegistryNode)
} else {
ns, err := c.registryDriver.GetNodesForRol(ctx, "data-node")
if err != nil {
return "", err
}
if len(ns) == 0 {
return "", fmt.Errorf("there are not data-nodes alive")
}
c.logger.Info().Log("msg", "nodes obtained from registry")
nodes = ns
}
c.cache.Set("nodes", nodes, cache.DefaultExpiration)

// TODO(labkode) the logic for choosing a node is very rudimentary.
// In the future would be nice to have at least RoundRobin.
// Thanks that clients are registry aware we an use our own algorithms
// based on some prometheus metrics like load.
// TODO(labkode) add caching behaviour
nodes, err := c.registryDriver.GetNodesForRol(ctx, "data-node")
if err != nil {
return "", err
}
if len(nodes) == 0 {
return "", fmt.Errorf("there are not data-nodes alive")
}
c.logger.Info().Log("msg", "got data-nodes", "numnodes", len(nodes))
chosenNode := nodes[rand.Intn(len(nodes))]
c.logger.Info().Log("msg", "data-node chosen", "data-node-url", chosenNode.URL())
chosenURL := chosenNode.URL() + "/data"
c.cache.Set("url", chosenURL, cache.DefaultExpiration)
return chosenURL, nil
}
func (c *webServiceClient) UploadFile(ctx context.Context, user root.User, path string, r io.ReadCloser, clientChecksum string) error {
Expand Down
32 changes: 16 additions & 16 deletions root/metadatawebserviceclient/metadatawebserviceclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,29 @@ func New(logger levels.Levels, cm root.ContextManager, registryDriver root.Regis
}

func (c *webServiceClient) getMetaDataURL(ctx context.Context) (string, error) {
u, ok := c.cache.Get("url")
var nodes []root.RegistryNode

v, ok := c.cache.Get("nodes")
if ok {
c.logger.Info().Log("msg", "metadata-node chosen from cache", "metadata-node-url", u.(string))
return u.(string), nil
nodes = v.([]root.RegistryNode)
c.logger.Info().Log("msg", "nodes obtained from cache")
} else {
ns, err := c.registryDriver.GetNodesForRol(ctx, "metadata-node")
if err != nil {
return "", err
}
if len(ns) == 0 {
return "", fmt.Errorf("there are not metadata-nodes alive")
}
c.logger.Info().Log("msg", "nodes obtained from registry")
nodes = ns
}
c.cache.Set("nodes", nodes, cache.DefaultExpiration)

// TODO(labkode) the logic for choosing a node is very rudimentary.
// In the future would be nice to have at least RoundRobin.
// Thanks that clients are registry aware we an use our own algorithms
// based on some prometheus metrics like load.
// TODO(labkode) add caching behaviour
nodes, err := c.registryDriver.GetNodesForRol(ctx, "metadata-node")
if err != nil {
return "", err
}
if len(nodes) == 0 {
return "", fmt.Errorf("there are not metadata-nodes alive")
}
c.logger.Info().Log("msg", "got metadata-nodes", "numnodes", len(nodes))
chosenNode := nodes[rand.Intn(len(nodes))]
c.logger.Info().Log("msg", "metadata-node chosen", "metadata-node-url", chosenNode.URL())
chosenURL := chosenNode.URL() + "/meta"
c.cache.Set("url", chosenURL, cache.DefaultExpiration)
return chosenURL, nil
}

Expand Down

0 comments on commit 96e39cb

Please sign in to comment.