-
Notifications
You must be signed in to change notification settings - Fork 571
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added async API to sessions, added first draft of async sample.
- Loading branch information
Showing
12 changed files
with
554 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" /> | ||
</startup> | ||
</configuration> |
39 changes: 39 additions & 0 deletions
39
cs/playground/FasterKVAsyncSample/FasterKVAsyncSample.csproj
This file contains 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.2</TargetFramework> | ||
<Platforms>x64</Platforms> | ||
<LangVersion>preview</LangVersion> | ||
<RuntimeIdentifiers>win7-x64;linux-x64</RuntimeIdentifiers> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<RootNamespace>FasterKVAsyncSample</RootNamespace> | ||
<ErrorReport>prompt</ErrorReport> | ||
<RestoreProjectStyle>PackageReference</RestoreProjectStyle> | ||
<Prefer32Bit>true</Prefer32Bit> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)' == 'Debug'"> | ||
<DefineConstants>TRACE;DEBUG</DefineConstants> | ||
<DebugType>full</DebugType> | ||
<DebugSymbols>true</DebugSymbols> | ||
<OutputPath>bin\x64\Debug\</OutputPath> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)' == 'Release'"> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\x64\Release\</OutputPath> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="App.config" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\core\FASTER.core.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains 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 |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using FASTER.core; | ||
|
||
namespace FasterKVAsyncSample | ||
{ | ||
public class Program | ||
{ | ||
static FasterKV<CacheKey, CacheValue, CacheInput, CacheOutput, CacheContext, CacheFunctions> faster; | ||
static int numOps = 0; | ||
|
||
/// <summary> | ||
/// Main program entry point | ||
/// </summary> | ||
static void Main() | ||
{ | ||
var path = "FasterKVAsyncSample"; | ||
|
||
var log = Devices.CreateLogDevice(path + "hlog.log", deleteOnClose: true); | ||
var objlog = Devices.CreateLogDevice(path + "hlog.obj.log", deleteOnClose: true); | ||
|
||
var logSettings = new LogSettings { LogDevice = log, ObjectLogDevice = objlog }; | ||
var checkpointSettings = new CheckpointSettings { CheckpointDir = path, CheckPointType = CheckpointType.FoldOver }; | ||
var serializerSettings = new SerializerSettings<CacheKey, CacheValue> { keySerializer = () => new CacheKeySerializer(), valueSerializer = () => new CacheValueSerializer() }; | ||
|
||
faster = new FasterKV<CacheKey, CacheValue, CacheInput, CacheOutput, CacheContext, CacheFunctions> | ||
(1L << 20, new CacheFunctions(), logSettings, checkpointSettings, serializerSettings); | ||
|
||
const int NumParallelTasks = 1; | ||
ThreadPool.SetMinThreads(2 * Environment.ProcessorCount, 2 * Environment.ProcessorCount); | ||
TaskScheduler.UnobservedTaskException += (object sender, UnobservedTaskExceptionEventArgs e) => | ||
{ | ||
Console.WriteLine($"Unobserved task exception: {e.Exception}"); | ||
e.SetObserved(); | ||
}; | ||
|
||
Task[] tasks = new Task[NumParallelTasks]; | ||
for (int i = 0; i < NumParallelTasks; i++) | ||
{ | ||
int local = i; | ||
tasks[i] = Task.Run(() => AsyncOperator(local)); | ||
} | ||
|
||
// Threads for reporting, commit | ||
new Thread(new ThreadStart(ReportThread)).Start(); | ||
new Thread(new ThreadStart(CommitThread)).Start(); | ||
|
||
Task.WaitAll(tasks); | ||
} | ||
|
||
/// <summary> | ||
/// Async operations on FasterKV | ||
/// </summary> | ||
static async Task AsyncOperator(int id) | ||
{ | ||
using var session = faster.NewSession(id.ToString()); | ||
Random rand = new Random(id); | ||
|
||
bool batched = true; | ||
|
||
await Task.Yield(); | ||
|
||
if (!batched) | ||
{ | ||
// Single commit version - append each item and wait for commit | ||
// Needs high parallelism (NumParallelTasks) for perf | ||
// Needs separate commit thread to perform regular commit | ||
// Otherwise we commit only at page boundaries | ||
while (true) | ||
{ | ||
try | ||
{ | ||
await session.UpsertAsync(new CacheKey(rand.Next()), new CacheValue(rand.Next()), true); | ||
Interlocked.Increment(ref numOps); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"{nameof(AsyncOperator)}({id}): {ex}"); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
// Batched version - we enqueue many entries to memory, | ||
// then wait for commit periodically | ||
int count = 0; | ||
while (true) | ||
{ | ||
await session.UpsertAsync(new CacheKey(rand.Next()), new CacheValue(rand.Next())); | ||
if (count++ % 100 == 0) | ||
{ | ||
await session.WaitForCommitAsync(); | ||
Interlocked.Add(ref numOps, 100); | ||
} | ||
} | ||
} | ||
} | ||
|
||
static void ReportThread() | ||
{ | ||
long lastTime = 0; | ||
long lastValue = numOps; | ||
|
||
Stopwatch sw = new Stopwatch(); | ||
sw.Start(); | ||
|
||
while (true) | ||
{ | ||
Thread.Sleep(5000); | ||
|
||
var nowTime = sw.ElapsedMilliseconds; | ||
var nowValue = numOps; | ||
|
||
Console.WriteLine("Operation Throughput: {0} ops/sec, Tail: {1}", | ||
(nowValue - lastValue) / (1000 * (nowTime - lastTime)), faster.Log.TailAddress); | ||
lastValue = nowValue; | ||
lastTime = nowTime; | ||
} | ||
} | ||
|
||
static void CommitThread() | ||
{ | ||
//Task<LinkedCommitInfo> prevCommitTask = null; | ||
while (true) | ||
{ | ||
Thread.Sleep(5000); | ||
|
||
faster.TakeFullCheckpoint(out _); | ||
faster.CompleteCheckpointAsync().GetAwaiter().GetResult(); | ||
} | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
cs/playground/FasterKVAsyncSample/Properties/AssemblyInfo.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
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: AssemblyDescription("")] | ||
[assembly: AssemblyCopyright("Copyright © 2017")] | ||
[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("17bdd0a5-98e5-464a-8a00-050d9ff4c562")] |
Oops, something went wrong.