-
Notifications
You must be signed in to change notification settings - Fork 143
Enhance SessionContext information #2135
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,7 @@ var documentStore = new DocumentStore | |
LoadBalanceBehavior = LoadBalanceBehavior.UseSessionContext, | ||
|
||
// Assign a method that sets the default context string | ||
// This string will be used for sessions that do Not provide a context string | ||
// This method will be used for sessions that do Not have their context string set explicitly | ||
// A sample GetDefaultContext method is defined below | ||
LoadBalancerPerSessionContextSelector = GetDefaultContext, | ||
|
||
|
@@ -92,11 +92,21 @@ var documentStore = new DocumentStore | |
<TabItem value="LoadBalance_6" label="LoadBalance_6"> | ||
<CodeBlock language="csharp"> | ||
{`// A customized method for getting a default context string | ||
|
||
public const string ContextKey = "RavenDB.Context"; | ||
|
||
private string GetDefaultContext(string dbName) | ||
\{ | ||
// Method is invoked by RavenDB with the database name | ||
// Use that name - or return any string of your choice | ||
return "DefaultContextString"; | ||
// Get the string set by the web application elsewhere. | ||
var ctx = System.Web.HttpContext.Current; | ||
if (ctx == null || ctx.Items.Contains(ContextKey) == false) | ||
Scooletz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
// return a safe default | ||
return dbName; | ||
} | ||
|
||
// Return the context set elsewhere or default to the dbName | ||
return (ctx.Items[ContextKey] as string) ?? dbName; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
\} | ||
`} | ||
</CodeBlock> | ||
|
@@ -120,12 +130,15 @@ using (var session = documentStore.OpenSession()) | |
{`// Open a session that will use a UNIQUE context string: | ||
using (var session = documentStore.OpenSession()) | ||
\{ | ||
// Call SetContext, pass a unique context string for this session | ||
session.Advanced.SessionInfo.SetContext("SomeOtherContext"); | ||
// Call SetContext, pass a unique context string for this session. | ||
// In this case, a team identifier, where both employees belong to. | ||
Scooletz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
session.Advanced.SessionInfo.SetContext("team/9-A"); | ||
|
||
// For all Read & Write requests made in this session, | ||
Scooletz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// node to access is calculated from the unique string & the seed defined on the store | ||
var employee = session.Load<Employee>("employees/1-A"); | ||
var employee1 = session.Load<Employee>("employees/1-A"); | ||
|
||
Scooletz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
var employee2 = session.Load<Employee>("employees/3-A"); | ||
\} | ||
`} | ||
</CodeBlock> | ||
|
@@ -250,15 +263,13 @@ using (documentStore) | |
|
||
## When to use | ||
|
||
* Distributing _Read & Write_ requests among the cluster nodes can be beneficial | ||
when a set of sessions handle a specific set of documents or similar data. | ||
Load balancing can be achieved by routing requests from the sessions that handle similar topics to the same node, while routing other sessions to other nodes. | ||
* When a session handles a specific set of documents or data, that can be scoped under a shared context. | ||
Setting the context can greatly reduce chances for conflicts. It can be useful, when used with the [optimistic concurrency](../../../client-api/session/configuration/how-to-enable-optimistic-concurrency/), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Conflicts happen only on writes, while the load-balancing behavior applies to both reads and writes. ========= Use this text: =========
|
||
to ensure that requests for the same data are routed to the same node. | ||
|
||
* Another usage example can be setting the session's context to be the current user. | ||
Thus spreading the _Read & Write_ requests per user that logs into the application. | ||
* When a user or a tenant owns their data and can benefit from having them served from one node. Also, in regards to the conflicts and the optimistic concurrency checks mentioned above. | ||
|
||
* Once setting the load balance to be per session-context, | ||
in the case when detecting that many or all sessions send requests to the same node, | ||
* Once setting the load balance to be per session-context, in the case when detecting that many or all sessions send requests to the same node, | ||
a further level of node randomization can be added by changing the seed. | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method => This default string
The important point is that it’s not the method itself that’s used, but its return value - the default context string.
The alternative phrasing (have their context string set explicitly) is 'passive voice' and adds unnecessary complexity. It’s less clear than the original
Better to stick with:
// This default string will be used for sessions that do Not provide a context string
Another alternative (if you wanna retain 'method'):
// This method will be called to provide a default context string for sessions that do not set one explicitly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot add a comment above, so dding here
Need to add a line break between lines #63 & #64
(The current rendering does not place line 64 on a new line)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This mention of the default string is based on the fact that the method currently returns just a string and is not context aware. I do agree with the passive voice being too complex. I think though, that if you set the load balance behavior, you should provide something else here (the other discussion) than just a default string.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So use 3'rd suggestion for the comment:
// This method will be called to provide a default context string for sessions that do not set one explicitly