From 96e39cbb4ce86fd6436a4b44246064440c834bfd Mon Sep 17 00:00:00 2001 From: Hugo Gonzalez Date: Thu, 26 Jan 2017 18:37:45 +0100 Subject: [PATCH] Cache all nodes instead the chosen one --- .../authenticationwebserviceclient.go | 31 ++++++++--------- .../datawebserviceclient.go | 34 +++++++++---------- .../metadatawebserviceclient.go | 32 ++++++++--------- 3 files changed, 48 insertions(+), 49 deletions(-) diff --git a/root/authenticationwebserviceclient/authenticationwebserviceclient.go b/root/authenticationwebserviceclient/authenticationwebserviceclient.go index 98917a6..1ac7c1a 100644 --- a/root/authenticationwebserviceclient/authenticationwebserviceclient.go +++ b/root/authenticationwebserviceclient/authenticationwebserviceclient.go @@ -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 } diff --git a/root/datawebserviceclient/datawebserviceclient.go b/root/datawebserviceclient/datawebserviceclient.go index 8b495a8..cc41bbc 100644 --- a/root/datawebserviceclient/datawebserviceclient.go +++ b/root/datawebserviceclient/datawebserviceclient.go @@ -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 { diff --git a/root/metadatawebserviceclient/metadatawebserviceclient.go b/root/metadatawebserviceclient/metadatawebserviceclient.go index c44e6a9..80f6243 100644 --- a/root/metadatawebserviceclient/metadatawebserviceclient.go +++ b/root/metadatawebserviceclient/metadatawebserviceclient.go @@ -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 }