-
Notifications
You must be signed in to change notification settings - Fork 54
Глейзер Роман #57
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
Open
RomanGleyzer
wants to merge
6
commits into
kontur-courses:master
Choose a base branch
from
RomanGleyzer:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Глейзер Роман #57
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d7d401f
Parallel (1 балл)
RomanGleyzer 868529c
RoundRobin (2 балла)
RomanGleyzer fc7b672
Smart (2 балла)
RomanGleyzer c3a8b36
Правки ParallelClusterClient
RomanGleyzer 3d07edc
Исправил проблему из-за которой не проходил тест ShouldNotSpendTimeOnBad
RomanGleyzer 48129e4
Исправление SmartClusterClient
RomanGleyzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
54 changes: 42 additions & 12 deletions
54
homework 2/ClusterClient/Clients/ParallelClusterClient.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,53 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using log4net; | ||
|
|
||
| namespace ClusterClient.Clients | ||
| namespace ClusterClient.Clients; | ||
|
|
||
| public class ParallelClusterClient(string[] replicaAddresses) : ClusterClientBase(replicaAddresses) | ||
| { | ||
| public class ParallelClusterClient : ClusterClientBase | ||
| public override async Task<string> ProcessRequestAsync(string query, TimeSpan timeout) | ||
| { | ||
| public ParallelClusterClient(string[] replicaAddresses) : base(replicaAddresses) | ||
| { | ||
| } | ||
| if (ReplicaAddresses == null || ReplicaAddresses.Length == 0) | ||
| throw new InvalidOperationException("Реплика адресов не указана"); | ||
|
|
||
| public override Task<string> ProcessRequestAsync(string query, TimeSpan timeout) | ||
| var firstSuccess = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously); | ||
|
|
||
| var remaining = ReplicaAddresses.Length; | ||
| Exception? lastError = null; | ||
|
|
||
| foreach (var uri in ReplicaAddresses) | ||
| { | ||
| throw new NotImplementedException(); | ||
| var webRequest = CreateRequest(uri + "?query=" + query); | ||
| Log.InfoFormat($"Обработка {webRequest.RequestUri}"); | ||
|
|
||
| var task = ProcessRequestAsync(webRequest); | ||
|
|
||
| _ = task.ContinueWith(t => | ||
| { | ||
| if (t.Status == TaskStatus.RanToCompletion) | ||
| { | ||
| firstSuccess.TrySetResult(t.Result); | ||
| return; | ||
| } | ||
|
|
||
| var ex = t.Exception?.GetBaseException() ?? new TaskCanceledException(t); | ||
| lastError = ex; | ||
| _ = t.Exception; | ||
|
|
||
| if (Interlocked.Decrement(ref remaining) == 0) | ||
| firstSuccess.TrySetException(lastError ?? | ||
| new Exception("Не удалось получить ответ ни от одной реплики")); | ||
| }, TaskContinuationOptions.ExecuteSynchronously); | ||
| } | ||
|
|
||
| protected override ILog Log => LogManager.GetLogger(typeof(ParallelClusterClient)); | ||
| var completed = await Task.WhenAny(firstSuccess.Task, Task.Delay(timeout)); | ||
| if (completed != firstSuccess.Task) | ||
| throw new TimeoutException(); | ||
|
|
||
| return await firstSuccess.Task; | ||
| } | ||
| } | ||
|
|
||
| protected override ILog Log => LogManager.GetLogger(typeof(ParallelClusterClient)); | ||
| } |
59 changes: 47 additions & 12 deletions
59
homework 2/ClusterClient/Clients/RoundRobinClusterClient.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,58 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Diagnostics; | ||
| using System.Threading.Tasks; | ||
| using log4net; | ||
|
|
||
| namespace ClusterClient.Clients | ||
| namespace ClusterClient.Clients; | ||
|
|
||
| public class RoundRobinClusterClient(string[] replicaAddresses) : ClusterClientBase(replicaAddresses) | ||
| { | ||
| public class RoundRobinClusterClient : ClusterClientBase | ||
| public override async Task<string> ProcessRequestAsync(string query, TimeSpan timeout) | ||
| { | ||
| public RoundRobinClusterClient(string[] replicaAddresses) : base(replicaAddresses) | ||
| { | ||
| } | ||
| if (ReplicaAddresses == null || ReplicaAddresses.Length == 0) | ||
| throw new InvalidOperationException("Реплика адресов не указана"); | ||
|
|
||
| public override Task<string> ProcessRequestAsync(string query, TimeSpan timeout) | ||
| var sw = Stopwatch.StartNew(); | ||
| Exception lastError = null; | ||
|
|
||
| for (var i = 0; i < ReplicaAddresses.Length; i++) | ||
| { | ||
| throw new NotImplementedException(); | ||
| var remaining = timeout - sw.Elapsed; | ||
| if (remaining <= TimeSpan.Zero) | ||
| break; | ||
|
|
||
| var remainingReplicas = ReplicaAddresses.Length - i; | ||
| var slice = TimeSpan.FromTicks(remaining.Ticks / remainingReplicas); | ||
|
|
||
| var uri = ReplicaAddresses[i]; | ||
| var webRequest = CreateRequest(uri + "?query=" + query); | ||
|
|
||
| Log.InfoFormat($"Обработка {webRequest.RequestUri}"); | ||
|
|
||
| var resultTask = ProcessRequestAsync(webRequest); | ||
|
|
||
| var completed = await Task.WhenAny(resultTask, Task.Delay(slice)); | ||
| if (completed != resultTask) | ||
| { | ||
| _ = resultTask.ContinueWith(t => _ = t.Exception, TaskContinuationOptions.OnlyOnFaulted); | ||
| continue; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| return await resultTask; | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| lastError = e; | ||
| } | ||
| } | ||
|
|
||
| protected override ILog Log => LogManager.GetLogger(typeof(RoundRobinClusterClient)); | ||
| if (lastError != null) | ||
| throw lastError; | ||
|
|
||
| throw new TimeoutException(); | ||
| } | ||
| } | ||
|
|
||
| protected override ILog Log => LogManager.GetLogger(typeof(RoundRobinClusterClient)); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,74 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Diagnostics; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using log4net; | ||
|
|
||
| namespace ClusterClient.Clients | ||
| namespace ClusterClient.Clients; | ||
|
|
||
| public class SmartClusterClient(string[] replicaAddresses) : ClusterClientBase(replicaAddresses) | ||
| { | ||
| public class SmartClusterClient : ClusterClientBase | ||
| public override async Task<string> ProcessRequestAsync(string query, TimeSpan timeout) | ||
| { | ||
| public SmartClusterClient(string[] replicaAddresses) : base(replicaAddresses) | ||
| if (ReplicaAddresses == null || ReplicaAddresses.Length == 0) | ||
| throw new InvalidOperationException("Реплика адресов не указана"); | ||
|
|
||
| var sw = Stopwatch.StartNew(); | ||
|
|
||
| var perReplicaTimeout = TimeSpan.FromTicks(timeout.Ticks / ReplicaAddresses.Length); | ||
| var firstSuccess = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously); | ||
|
|
||
| var remaining = ReplicaAddresses.Length; | ||
| Exception lastError = null; | ||
|
|
||
| foreach (var uri in ReplicaAddresses) | ||
| { | ||
| var webRequest = CreateRequest(uri + "?query=" + query); | ||
| Log.InfoFormat($"Обработка {webRequest.RequestUri}"); | ||
|
|
||
| var task = ProcessRequestAsync(webRequest); | ||
|
|
||
| _ = task.ContinueWith(t => | ||
| { | ||
| if (t.Status == TaskStatus.RanToCompletion) | ||
| { | ||
| firstSuccess.TrySetResult(t.Result); | ||
| return; | ||
| } | ||
|
|
||
| var ex = t.Exception?.GetBaseException() ?? new TaskCanceledException(t); | ||
| Interlocked.Exchange(ref lastError, ex); | ||
|
|
||
| if (Interlocked.Decrement(ref remaining) == 0) | ||
| firstSuccess.TrySetException( | ||
| Volatile.Read(ref lastError) ?? | ||
| new Exception("Не удалось получить успешный ответ ни от одной реплики")); | ||
| }, TaskContinuationOptions.ExecuteSynchronously); | ||
|
|
||
| var completed = await Task.WhenAny(firstSuccess.Task, task, Task.Delay(perReplicaTimeout)); | ||
|
|
||
| if (completed == firstSuccess.Task) | ||
| return await firstSuccess.Task; | ||
|
|
||
| if (completed != task) continue; | ||
|
|
||
| if (task.Status == TaskStatus.RanToCompletion) | ||
| return task.Result; | ||
| } | ||
|
|
||
| public override Task<string> ProcessRequestAsync(string query, TimeSpan timeout) | ||
| var remainingTotal = timeout - sw.Elapsed; | ||
| if (remainingTotal > TimeSpan.Zero) | ||
| { | ||
| throw new NotImplementedException(); | ||
| var completed = await Task.WhenAny(firstSuccess.Task, Task.Delay(remainingTotal)); | ||
| if (completed == firstSuccess.Task) | ||
| return await firstSuccess.Task; | ||
| } | ||
|
|
||
| protected override ILog Log => LogManager.GetLogger(typeof(SmartClusterClient)); | ||
| if (firstSuccess.Task.IsCompleted) | ||
| return await firstSuccess.Task; | ||
|
|
||
| throw new TimeoutException(); | ||
| } | ||
| } | ||
|
|
||
| protected override ILog Log => LogManager.GetLogger(typeof(SmartClusterClient)); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Что делает эта строка?
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.
В этой строке ловится исключение у запроса, который мы перестали ждать из-за того, что перешли к следующей реплике. Я так сделал для избежания вызова исключения UnobservedTaskException