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

feat: add support for agent cache #225

Merged
merged 31 commits into from
Jul 28, 2023
Merged

feat: add support for agent cache #225

merged 31 commits into from
Jul 28, 2023

Conversation

winstxnhdw
Copy link
Contributor

@winstxnhdw winstxnhdw commented Jul 21, 2023

Closes #142.

  • Add support for agent cache
  • Applied some lint suggestions
  • Add tests for agent cache

Copy link
Contributor

@marcin-krystianc marcin-krystianc left a comment

Choose a reason for hiding this comment

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

Hi @winstxnhdw,

Thanks for your contribution. I see that you included multiple, unrelated changes into the PR. I think it is better to keep the scope of this PR just to caching mechanism and add those other features in next PRs.

Consul/Client_Options.cs Outdated Show resolved Hide resolved
Consul/Client_Options.cs Outdated Show resolved Hide resolved
Consul/Client_Options.cs Outdated Show resolved Hide resolved
Consul/Lock.cs Outdated Show resolved Hide resolved
Consul/Lock.cs Outdated Show resolved Hide resolved
Consul/Lock.cs Outdated Show resolved Hide resolved
Consul/Client_Options.cs Outdated Show resolved Hide resolved
Consul/Client_Options.cs Outdated Show resolved Hide resolved
Consul/Client_Options.cs Outdated Show resolved Hide resolved
Consul.Test/ClientTest.cs Outdated Show resolved Hide resolved
Copy link
Contributor

@marcin-krystianc marcin-krystianc left a comment

Choose a reason for hiding this comment

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

From reading about [Background refresh Caching]{https://developer.hashicorp.com/consul/api-docs/features/caching#background-refresh-caching} I conclude that the Agent_UseCache seems to be sufficient and a dedicated test for it is not necessary.

Consul.Test/AgentTest.cs Outdated Show resolved Hide resolved
Consul.Test/AgentTest.cs Outdated Show resolved Hide resolved
Consul.Test/AgentTest.cs Outdated Show resolved Hide resolved
Consul.Test/AgentTest.cs Outdated Show resolved Hide resolved
Consul.Test/AgentTest.cs Outdated Show resolved Hide resolved
Consul.Test/ClientTest.cs Show resolved Hide resolved
Consul/Client_GetRequests.cs Outdated Show resolved Hide resolved
Consul/Client_GetRequests.cs Outdated Show resolved Hide resolved
/// <summary>
/// The headers returned by the Consul server
/// </summary>
public HttpResponseHeaders Headers { get; set; }
Copy link
Contributor

@marcin-krystianc marcin-krystianc Jul 26, 2023

Choose a reason for hiding this comment

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

I'm not sure about exposing the HttpResponseHeaders in the response object. It changes the contract of this class dramatically, and the HttpResponseHeaders class is very implementation specific. I think it is better to use simpler types and add two extra fields for X-Cache and Age headers.
@mfkl @jgiannuzzi Any thoughts?

Copy link
Contributor Author

@winstxnhdw winstxnhdw Jul 26, 2023

Choose a reason for hiding this comment

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

It changes the contract of this class dramatically, and the HttpResponseHeaders class is very implementation specific.

From what the intellisense tells me, the base request method always returns a HttpResponseMessage which always contains a Headers property with HttpResponseHeaders. Exposing this property will also allow users full access to the raw Headers where they would not have before.

In fact I am even of the opinion to keep the entire original response in a RawResponse property because IMO, as a library, I'd want to provide the user an easier experience when interfacing with the Consul API, while not blocking one of the valid ways a user may want to inspect the original response.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that it gives the user much flexibility. So I understand your motivation, but I believe that exposing the HttpResponseHeaders would be a bad API design practice.
It is an implementation-specific class. Once we would expose it we would never be able to change our implementation. I'm not saying that we are ever going to do that, but I'm saying that it is going to strongly couple implementation and the API together which is a bad practice.
The number of different HTTP headers that are going to be used by the consul is very limited, so we can parse them in our library for the user. If the number of possible headers was very high, we would need more flexibility as you suggest.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, that makes a lot of sense.

Okay, in that case, we can declare it as a private property and retrieve the value from our test using reflection?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, that makes a lot of sense.

Okay, in that case, we can declare it as a private property and retrieve the value from our test using reflection?

I don't think we need reflection, I think the best way to do this is by adding two new extra public properties (XCache and Age) in the QueryResult class. Then update the ParseQueryHeaders method to parse those headers. It will make it possible to implement the test, but also it will make it possible for the end user to know whether the result comes from the cache.

Copy link
Contributor Author

@winstxnhdw winstxnhdw Jul 27, 2023

Choose a reason for hiding this comment

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

I am wondering if we need to update the following constructor overload for QueryResult.

public QueryResult(QueryResult other) : base(other)
{
    LastIndex = other.LastIndex;
    LastContact = other.LastContact;
    KnownLeader = other.KnownLeader;
}

to

public QueryResult(QueryResult other) : base(other)
{
    LastIndex = other.LastIndex;
    LastContact = other.LastContact;
    KnownLeader = other.KnownLeader;
    AddressTranslationEnabled = other.AddressTranslationEnabled;
    XCache = other.XCache;
    Age = Age;
}

Consul/Interfaces/ICatalogEndpoint.cs Outdated Show resolved Hide resolved
Consul/Client_GetRequests.cs Outdated Show resolved Hide resolved
Consul/Client_GetRequests.cs Outdated Show resolved Hide resolved
Consul/Client_Results.cs Outdated Show resolved Hide resolved
Consul/Client_Results.cs Outdated Show resolved Hide resolved
winstxnhdw and others added 4 commits July 27, 2023 21:45
Co-authored-by: Marcin Krystianc <marcin.krystianc@gmail.com>
Co-authored-by: Marcin Krystianc <marcin.krystianc@gmail.com>
Consul.Test/AgentTest.cs Outdated Show resolved Hide resolved
Consul.Test/AgentTest.cs Outdated Show resolved Hide resolved
Consul/Client_GetRequests.cs Outdated Show resolved Hide resolved
Copy link
Contributor

@marcin-krystianc marcin-krystianc left a comment

Choose a reason for hiding this comment

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

Generally, it is good to be merged now. I just noticed that we do unnecessary validation of arguments, It neither adds any value nor does much harm - but I still would remove it.

Consul/Client_GetRequests.cs Outdated Show resolved Hide resolved
Consul/Client_GetRequests.cs Outdated Show resolved Hide resolved
Consul/Client_GetRequests.cs Outdated Show resolved Hide resolved
Consul/Client_GetRequests.cs Outdated Show resolved Hide resolved
Copy link
Contributor

@marcin-krystianc marcin-krystianc left a comment

Choose a reason for hiding this comment

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

Looks good 👍 Thank you for your patience!

@marcin-krystianc marcin-krystianc merged commit fc9c4f8 into G-Research:master Jul 28, 2023
147 checks passed
@winstxnhdw winstxnhdw deleted the usecache branch July 31, 2023 14:34
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.

Does support Agent Caching?
2 participants