Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a way to override default consistency level per agent #3920

Closed

Conversation

pierresouchay
Copy link
Contributor

Following conversation #3911

This patch allow to change the default behavior of Consul regarding
consistency

It still requires discussion probably and futher test, just a start for discussion

pierresouchay and others added 21 commits February 26, 2018 17:24
Following conversation hashicorp#3911

This patch allow to change the default behavior of Consul regarding
consistency
…lelism to avoid timing issues on their contended CPUs
This avoids a conflict with #datacenter later on the page. We're mixing
histroic manually specified anchors with generated anchors (via
redcarpet / middleman-hashicorp) so we have to manually override the
automatic generation here.

I was tempted to rewrite the old manual anchors to use the automatic
generation, but there is no way to maintain backwards compatibility,
so will leave that for a time when it is appropriate for us to break
links (or redirect them, etc).

Fixes hashicorp#3916
@pierresouchay
Copy link
Contributor Author

Hello @banks,

What do you think about this kind of implementation, would it be mergeable on long term?

Regards

@banks
Copy link
Member

banks commented Mar 14, 2018

I'll discuss again with the team on our Friday call and see if there is consensus on this. Thanks for all you effort chasing down these optimisations.

Copy link
Contributor

@preetapan preetapan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pierresouchay I just got a chance to review this and added some more comments. We are ready to take this after that though, so this will probably get merged this week after you address my latest feedback.

@@ -461,6 +461,14 @@ type RuntimeConfig struct {
// flag: -datacenter string
Datacenter string

// Defines the maximum stale value for discovery path. Defauls to "0s".
// Discovery paths are /v1/catalog/* and /v1/heath/* paths
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v1/health

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

agent/http.go Outdated
defaults = false
}
if maxStale := query.Get("max_stale"); maxStale != "" {
fmt.Printf("**** max_stale=%s\n", maxStale)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops... sorry, fixed

@@ -632,13 +634,70 @@ func TestParseConsistency(t *testing.T) {
}
}

// ensureConsistency check if consistency modes are correclty applied
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in correctly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DONE

agent/http.go Outdated
if defaults {
path := req.URL.Path
if strings.HasPrefix(path, "/v1/catalog") || strings.HasPrefix(path, "/v1/health") {
b.MaxStaleDuration = s.agent.config.DiscoveryMaxStale
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrite this part as follows to make it a bit cleaner

if s.agent.config.DiscoveryMaxState > 0 {
  b.MaxStaleDuration = s.agent.config.DiscoveryMaxStale
  b.AllowStale = true
}

agent/http.go Outdated
if b.MaxStaleDuration.Nanoseconds() > 0 {
b.AllowStale = true
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon further discussion on this, we thought it would be nice to add a header when the default consistency mode is overridden, so one suggestion we have is to add a new response header. This method could add a header like resp.AddHeader("X-Consul-Effective-Consistency-Level","stale") or resp.AddHeader("X-Consul-Effective-Consistency-Level","leader") etc. That way if a client lib is making a default request to an agent that has DiscoveryMaxStale set and is being overridden by this block, the response should tell you what it used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, doing it as well

@preetapan
Copy link
Contributor

Also please mention the new discovery_max_stale property in the docs here - https://github.com/hashicorp/consul/blob/master/website/source/docs/agent/options.html.md#configuration-key-reference

agent/http.go Outdated
dur, err := time.ParseDuration(maxStale)
if err != nil {
resp.WriteHeader(http.StatusBadRequest)
fmt.Fprint(resp, "Invalid max_stale time")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also include the bad value here, so something like fmt.Fprint(resp, "Invalid max_stale value %q", maxStale)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DONE

Minor fixes (typos in comments)
Removed fmt.Printf debug code
Added documentation to new option
@pierresouchay
Copy link
Contributor Author

@preetapan Thank you very much for the review, I made the all the fixes.

The X-Consul-Effective-Consistency-Level is a bit more intrusive than expected since I had to add a setMeta in prepared queries as well (was missing, was maybe a bug)

I also updated the documentation

@@ -917,6 +917,14 @@ Consul will not enable TLS for the HTTP API unless the `https` port has been ass
leader, so this lets Consul continue serving requests in long outage scenarios where no leader can
be elected.

* <a name="discovery_max_stale"></a><a href="discovery_max_stale">`discovery_max_stale`</a> - In a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrite as

`discovery_max_stale` This enables stale requests for all service discovery HTTP endpoints. This is similar to how `max_stale` works for DNS requests. If this value is zero (default) then all service discovery HTTP endpoints are forwarded to the leader. If this value is greater than zero, any Consul server can handle the service discovery request.  If a Consul server is behind the leader by more than discovery_max_stale, the query will be re-evaluated on the leader to get more up-to-date results. 

@@ -157,6 +173,8 @@ type QueryMeta struct {

// Used to indicate if there is a known leader node
KnownLeader bool

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment

@preetapan
Copy link
Contributor

Did the few last changes in PR #4004 , closing this one out

@preetapan preetapan closed this Mar 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants