Skip to content

Commit

Permalink
Закончили задачку.
Browse files Browse the repository at this point in the history
  • Loading branch information
kokovin committed Jul 3, 2014
1 parent 603557f commit 0e643ac
Show file tree
Hide file tree
Showing 34 changed files with 6,864 additions and 94 deletions.
816 changes: 816 additions & 0 deletions 09-NoSQL/Assemblies/GrEmit.XML

Large diffs are not rendered by default.

Binary file added 09-NoSQL/Assemblies/GrEmit.dll
Binary file not shown.
Binary file added 09-NoSQL/Assemblies/GrEmit.pdb
Binary file not shown.
Binary file added 09-NoSQL/Assemblies/GroBuf.dll
Binary file not shown.
Binary file added 09-NoSQL/Assemblies/GroBuf.pdb
Binary file not shown.
Binary file added 09-NoSQL/Assemblies/GroboContainer.dll
Binary file not shown.
Binary file added 09-NoSQL/Assemblies/GroboContainer.pdb
Binary file not shown.
Binary file added 09-NoSQL/Assemblies/WebPersonal.Core.dll
Binary file not shown.
Binary file added 09-NoSQL/Assemblies/WebPersonal.Core.pdb
Binary file not shown.
Binary file added 09-NoSQL/Assemblies/log4net.dll
Binary file not shown.
Binary file added 09-NoSQL/Assemblies/nunit.framework.dll
Binary file not shown.
5,622 changes: 5,622 additions & 0 deletions 09-NoSQL/Assemblies/nunit.framework.xml

Large diffs are not rendered by default.

40 changes: 0 additions & 40 deletions 09-NoSQL/CrashCourse/Client/ServiceProxy.cs

This file was deleted.

12 changes: 12 additions & 0 deletions 09-NoSQL/CrashCourse/CrashCourse.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Service", "Service\Service.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{7B9907D3-D0F4-4566-96E8-3E3859ABF08E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{5E9DEBC1-7FDF-4455-9D9E-645661258649}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HighLevelClient", "HighLevelClient\HighLevelClient.csproj", "{057BA9C0-E56D-4A74-BED2-7A0AF447E4E3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -19,6 +23,14 @@ Global
{7B9907D3-D0F4-4566-96E8-3E3859ABF08E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7B9907D3-D0F4-4566-96E8-3E3859ABF08E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7B9907D3-D0F4-4566-96E8-3E3859ABF08E}.Release|Any CPU.Build.0 = Release|Any CPU
{5E9DEBC1-7FDF-4455-9D9E-645661258649}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E9DEBC1-7FDF-4455-9D9E-645661258649}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E9DEBC1-7FDF-4455-9D9E-645661258649}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E9DEBC1-7FDF-4455-9D9E-645661258649}.Release|Any CPU.Build.0 = Release|Any CPU
{057BA9C0-E56D-4A74-BED2-7A0AF447E4E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{057BA9C0-E56D-4A74-BED2-7A0AF447E4E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{057BA9C0-E56D-4A74-BED2-7A0AF447E4E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{057BA9C0-E56D-4A74-BED2-7A0AF447E4E3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
37 changes: 37 additions & 0 deletions 09-NoSQL/CrashCourse/HighLevelClient/EnterpriseClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Net;
using Client;
using Client.Parameters;

namespace HighLevelClient
{
public class EnterpriseClient
{
private static readonly Random random = new Random();
private readonly IPEndPoint[] endpoints;
private readonly ServiceClient internalClient;

public EnterpriseClient(IPEndPoint[] endpoints)
{
this.endpoints = endpoints;
internalClient = new ServiceClient();
}

private IPEndPoint NextReplica
{
get { return endpoints[random.Next(endpoints.Length)]; }
}

public void Write(string key, string value)
{
Data data = internalClient.Read(key, NextReplica) ?? new Data();
internalClient.Write(key, new Data { Value = value, Version = data.Version + 1 }, NextReplica);
}

public string Read(string key)
{
Data read = internalClient.Read(key, NextReplica);
return read == null ? null : read.Value;
}
}
}
59 changes: 59 additions & 0 deletions 09-NoSQL/CrashCourse/HighLevelClient/HighLevelClient.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{057BA9C0-E56D-4A74-BED2-7A0AF447E4E3}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HighLevelClient</RootNamespace>
<AssemblyName>HighLevelClient</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="EnterpriseClient.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Client\Client.csproj">
<Project>{7b9907d3-d0f4-4566-96e8-3e3859abf08e}</Project>
<Name>Client</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
36 changes: 36 additions & 0 deletions 09-NoSQL/CrashCourse/HighLevelClient/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("HighLevelClient")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HighLevelClient")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("72dd8122-885e-4306-938f-de6ad4e1f8e7")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
10 changes: 5 additions & 5 deletions 09-NoSQL/CrashCourse/Service/Http/ReadHttpMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ namespace Service.Http
public class ReadHttpMethod : IHttpMethod
{
private readonly Disk disk;
private readonly IGroboSerializer groboSerializer;
private readonly IJsonSerializer jsonSerializer;

public ReadHttpMethod(Disk disk, IGroboSerializer groboSerializer)
public ReadHttpMethod(Disk disk, IJsonSerializer jsonSerializer)
{
this.disk = disk;
this.groboSerializer = groboSerializer;
this.jsonSerializer = jsonSerializer;
}

#region IHttpMethod Members

public void Process(HttpContext context)
{
var readParameters = groboSerializer.Deserialize<ReadParameters>(context.Request.Body);
var readParameters = jsonSerializer.Deserialize<ReadParameters>(context.Request.Body);
var result = disk.Read(readParameters.Key);
context.Response.BodyStream.WriteBytes(groboSerializer.Serialize(result));
context.Response.BodyStream.WriteBytes(jsonSerializer.Serialize(result));
}

#endregion
Expand Down
13 changes: 6 additions & 7 deletions 09-NoSQL/CrashCourse/Service/Http/WriteHttpMethod.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Client.Parameters;
using GroBuf;
using SKBKontur.WebPersonal.Core.Networking.ServerSide.Http;
using SKBKontur.WebPersonal.Core.Serialization;
using Service.Infrastructure;
Expand All @@ -9,23 +8,23 @@ namespace Service.Http
public class WriteHttpMethod : IHttpMethod
{
private readonly Disk disk;
private readonly IGroboSerializer groboSerializer;
private readonly IJsonSerializer jsonSerializer;
private readonly Replicator replicator;

public WriteHttpMethod(Disk disk, IGroboSerializer groboSerializer, Replicator replicator)
public WriteHttpMethod(Disk disk, IJsonSerializer jsonSerializer, Replicator replicator)
{
this.disk = disk;
this.groboSerializer = groboSerializer;
this.jsonSerializer = jsonSerializer;
this.replicator = replicator;
}

#region IHttpMethod Members

public void Process(HttpContext context)
{
var writeParameters = groboSerializer.Deserialize<WriteParameters>(context.Request.Body);
disk.Write(writeParameters.Key, writeParameters.Value);
replicator.Replicate(writeParameters.Key, writeParameters.Value);
var writeParameters = jsonSerializer.Deserialize<WriteParameters>(context.Request.Body);
if (disk.Write(writeParameters.Key, writeParameters.Value))
replicator.Replicate(writeParameters.Key, writeParameters.Value);
}

#endregion
Expand Down
39 changes: 6 additions & 33 deletions 09-NoSQL/CrashCourse/Service/Infrastructure/Disk.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Client;
using Client.Parameters;
using SKBKontur.WebPersonal.Core.ApplicationCommon;

namespace Service.Infrastructure
{
Expand All @@ -25,39 +20,17 @@ public Data Read(string key)
}
}

public void Write(string key, Data value)
public bool Write(string key, Data value)
{
lock (storage)
{
if (storage.ContainsKey(key) && value.CompareTo(storage[key]) > 0)
if (!storage.ContainsKey(key) || value.CompareTo(storage[key]) > 0)
{
storage[key] = value;
return true;
}
}
}
}

public class Replicator
{
private readonly IServiceClient serviceClient;
private readonly IPEndPoint[] endpoints;

public Replicator(IServiceClient serviceClient, IApplicationSettings applicationSettings)
{
endpoints = applicationSettings.GetIPEndpointsArray("endpoints");
this.serviceClient = serviceClient;
}

public void Replicate(string key, Data value)
{
Task.Run(() => ReplicateInternal(key, value));
}

private void ReplicateInternal(string key, Data value)
{
foreach (var endPoint in endpoints)
{
Thread.Sleep(5000);
serviceClient.Write(key, value, endPoint);
}
return false;
}
}
}
53 changes: 53 additions & 0 deletions 09-NoSQL/CrashCourse/Service/Infrastructure/Replicator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Client;
using Client.Parameters;
using SKBKontur.WebPersonal.Core.ApplicationCommon;
using log4net;

namespace Service.Infrastructure
{
public class Replicator
{
private readonly IServiceClient serviceClient;
private readonly IPEndPoint[] endpoints;
private static ILog log = LogManager.GetLogger(typeof (Replicator));

public Replicator(IServiceClient serviceClient, IApplicationSettings applicationSettings)
{
endpoints = applicationSettings.GetIPEndpointsArray("endpoints");
this.serviceClient = serviceClient;
}

public void Replicate(string key, Data value)
{
Task.Run(() => ReplicateInternal(key, value));
}

private void ReplicateInternal(string key, Data value)
{
foreach (var endPoint in endpoints)
WriteToSingleReplica(key, value, endPoint);
}

private void WriteToSingleReplica(string key, Data value, IPEndPoint endPoint)
{
log.InfoFormat("Write '{1}':'{2}',{3} to {0}", endPoint, key, value.Value, value.Version);
Task.Delay(5000).ContinueWith(t =>
{
try
{
serviceClient.Write(key, value, endPoint);
log.InfoFormat("Write success '{1}':'{2}',{3} to {0}", endPoint, key, value.Value, value.Version);
}
catch (Exception)
{
log.WarnFormat("Write error '{1}':'{2}',{3} to {0}", endPoint, key, value.Value, value.Version);
WriteToSingleReplica(key, value, endPoint);
}
});
}
}
}
Loading

0 comments on commit 0e643ac

Please sign in to comment.