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

Add in RESTier-based DynamicService #2747

Draft
wants to merge 1 commit into
base: hackathon/peachy/main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion App.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,96 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ODataServiceTests", "ODataS
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataSourceGenerator", "ODataService\DataSourceGenerator\DataSourceGenerator.csproj", "{E6271DA9-A12E-4484-AC50-43F55C1F2DF5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeneratedDataSourceTests", "GeneratedDataSourceTests\GeneratedDataSourceTests.csproj", "{7DECA341-442D-41D5-89B4-B3BCD3676758}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GeneratedDataSourceTests", "GeneratedDataSourceTests\GeneratedDataSourceTests.csproj", "{7DECA341-442D-41D5-89B4-B3BCD3676758}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Project2", "Project2\Project2.csproj", "{7D4313F7-300D-405A-A92B-285EE1045FAC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiService", "ODataService\ODataService\ApiService.csproj", "{B379640E-9064-438D-8DA5-6F7B394C2C46}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RESTier", "RESTier", "{0193DBDA-F7D5-4A71-8097-13D74DF3DE58}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Restier.Core", "ODataService\RESTier\src\Microsoft.Restier.Core\Microsoft.Restier.Core.csproj", "{052E17C4-C151-4964-A873-979682F8619B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Restier.AspNet", "ODataService\RESTier\src\Microsoft.Restier.AspNet\Microsoft.Restier.AspNet.csproj", "{573D523D-AF49-460B-98C7-AB9FB930AC60}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.OData", "ODataService\WebAPI\src\Microsoft.AspNet.OData\Microsoft.AspNet.OData.csproj", "{A6F9775D-F7E2-424E-8363-79644A73038F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DynamicService", "ODataService\DynamicService\DynamicService.csproj", "{E8072054-3D92-4321-86B1-E038616AAA3E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataSourceManager", "ODataService\DataSourceManager\DataSourceManager.csproj", "{BED3CAAD-C9A8-4F29-B35A-3D73A9DECE87}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
CodeAnalysis|Any CPU = CodeAnalysis|Any CPU
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{413A6974-270D-4F33-BA5D-BDB2080F684C}.CodeAnalysis|Any CPU.ActiveCfg = Release|Any CPU
{413A6974-270D-4F33-BA5D-BDB2080F684C}.CodeAnalysis|Any CPU.Build.0 = Release|Any CPU
{413A6974-270D-4F33-BA5D-BDB2080F684C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{413A6974-270D-4F33-BA5D-BDB2080F684C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{413A6974-270D-4F33-BA5D-BDB2080F684C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{413A6974-270D-4F33-BA5D-BDB2080F684C}.Release|Any CPU.Build.0 = Release|Any CPU
{1978960D-EFF1-4AB1-902B-0DE7BEADA5A1}.CodeAnalysis|Any CPU.ActiveCfg = Release|Any CPU
{1978960D-EFF1-4AB1-902B-0DE7BEADA5A1}.CodeAnalysis|Any CPU.Build.0 = Release|Any CPU
{1978960D-EFF1-4AB1-902B-0DE7BEADA5A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1978960D-EFF1-4AB1-902B-0DE7BEADA5A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1978960D-EFF1-4AB1-902B-0DE7BEADA5A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1978960D-EFF1-4AB1-902B-0DE7BEADA5A1}.Release|Any CPU.Build.0 = Release|Any CPU
{E6271DA9-A12E-4484-AC50-43F55C1F2DF5}.CodeAnalysis|Any CPU.ActiveCfg = Release|Any CPU
{E6271DA9-A12E-4484-AC50-43F55C1F2DF5}.CodeAnalysis|Any CPU.Build.0 = Release|Any CPU
{E6271DA9-A12E-4484-AC50-43F55C1F2DF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E6271DA9-A12E-4484-AC50-43F55C1F2DF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E6271DA9-A12E-4484-AC50-43F55C1F2DF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E6271DA9-A12E-4484-AC50-43F55C1F2DF5}.Release|Any CPU.Build.0 = Release|Any CPU
{7DECA341-442D-41D5-89B4-B3BCD3676758}.CodeAnalysis|Any CPU.ActiveCfg = Release|Any CPU
{7DECA341-442D-41D5-89B4-B3BCD3676758}.CodeAnalysis|Any CPU.Build.0 = Release|Any CPU
{7DECA341-442D-41D5-89B4-B3BCD3676758}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7DECA341-442D-41D5-89B4-B3BCD3676758}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7DECA341-442D-41D5-89B4-B3BCD3676758}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7DECA341-442D-41D5-89B4-B3BCD3676758}.Release|Any CPU.Build.0 = Release|Any CPU
{7D4313F7-300D-405A-A92B-285EE1045FAC}.CodeAnalysis|Any CPU.ActiveCfg = Release|Any CPU
{7D4313F7-300D-405A-A92B-285EE1045FAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7D4313F7-300D-405A-A92B-285EE1045FAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D4313F7-300D-405A-A92B-285EE1045FAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D4313F7-300D-405A-A92B-285EE1045FAC}.Release|Any CPU.Build.0 = Release|Any CPU
{B379640E-9064-438D-8DA5-6F7B394C2C46}.CodeAnalysis|Any CPU.ActiveCfg = Debug|Any CPU
{B379640E-9064-438D-8DA5-6F7B394C2C46}.CodeAnalysis|Any CPU.Build.0 = Debug|Any CPU
{B379640E-9064-438D-8DA5-6F7B394C2C46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B379640E-9064-438D-8DA5-6F7B394C2C46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B379640E-9064-438D-8DA5-6F7B394C2C46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B379640E-9064-438D-8DA5-6F7B394C2C46}.Release|Any CPU.Build.0 = Release|Any CPU
{052E17C4-C151-4964-A873-979682F8619B}.CodeAnalysis|Any CPU.ActiveCfg = Debug|Any CPU
{052E17C4-C151-4964-A873-979682F8619B}.CodeAnalysis|Any CPU.Build.0 = Debug|Any CPU
{052E17C4-C151-4964-A873-979682F8619B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{052E17C4-C151-4964-A873-979682F8619B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{052E17C4-C151-4964-A873-979682F8619B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{052E17C4-C151-4964-A873-979682F8619B}.Release|Any CPU.Build.0 = Release|Any CPU
{573D523D-AF49-460B-98C7-AB9FB930AC60}.CodeAnalysis|Any CPU.ActiveCfg = Debug|Any CPU
{573D523D-AF49-460B-98C7-AB9FB930AC60}.CodeAnalysis|Any CPU.Build.0 = Debug|Any CPU
{573D523D-AF49-460B-98C7-AB9FB930AC60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{573D523D-AF49-460B-98C7-AB9FB930AC60}.Debug|Any CPU.Build.0 = Debug|Any CPU
{573D523D-AF49-460B-98C7-AB9FB930AC60}.Release|Any CPU.ActiveCfg = Release|Any CPU
{573D523D-AF49-460B-98C7-AB9FB930AC60}.Release|Any CPU.Build.0 = Release|Any CPU
{A6F9775D-F7E2-424E-8363-79644A73038F}.CodeAnalysis|Any CPU.ActiveCfg = CodeAnalysis|Any CPU
{A6F9775D-F7E2-424E-8363-79644A73038F}.CodeAnalysis|Any CPU.Build.0 = CodeAnalysis|Any CPU
{A6F9775D-F7E2-424E-8363-79644A73038F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A6F9775D-F7E2-424E-8363-79644A73038F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A6F9775D-F7E2-424E-8363-79644A73038F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A6F9775D-F7E2-424E-8363-79644A73038F}.Release|Any CPU.Build.0 = Release|Any CPU
{E8072054-3D92-4321-86B1-E038616AAA3E}.CodeAnalysis|Any CPU.ActiveCfg = Debug|Any CPU
{E8072054-3D92-4321-86B1-E038616AAA3E}.CodeAnalysis|Any CPU.Build.0 = Debug|Any CPU
{E8072054-3D92-4321-86B1-E038616AAA3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E8072054-3D92-4321-86B1-E038616AAA3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E8072054-3D92-4321-86B1-E038616AAA3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E8072054-3D92-4321-86B1-E038616AAA3E}.Release|Any CPU.Build.0 = Release|Any CPU
{BED3CAAD-C9A8-4F29-B35A-3D73A9DECE87}.CodeAnalysis|Any CPU.ActiveCfg = Debug|Any CPU
{BED3CAAD-C9A8-4F29-B35A-3D73A9DECE87}.CodeAnalysis|Any CPU.Build.0 = Debug|Any CPU
{BED3CAAD-C9A8-4F29-B35A-3D73A9DECE87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BED3CAAD-C9A8-4F29-B35A-3D73A9DECE87}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BED3CAAD-C9A8-4F29-B35A-3D73A9DECE87}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BED3CAAD-C9A8-4F29-B35A-3D73A9DECE87}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -54,6 +114,13 @@ Global
{1978960D-EFF1-4AB1-902B-0DE7BEADA5A1} = {957D9415-CE59-47B5-ABB8-A5EA12863F4E}
{E6271DA9-A12E-4484-AC50-43F55C1F2DF5} = {621819DE-DDB7-41FC-A874-757464A90050}
{7DECA341-442D-41D5-89B4-B3BCD3676758} = {957D9415-CE59-47B5-ABB8-A5EA12863F4E}
{B379640E-9064-438D-8DA5-6F7B394C2C46} = {621819DE-DDB7-41FC-A874-757464A90050}
{0193DBDA-F7D5-4A71-8097-13D74DF3DE58} = {621819DE-DDB7-41FC-A874-757464A90050}
{052E17C4-C151-4964-A873-979682F8619B} = {0193DBDA-F7D5-4A71-8097-13D74DF3DE58}
{573D523D-AF49-460B-98C7-AB9FB930AC60} = {0193DBDA-F7D5-4A71-8097-13D74DF3DE58}
{A6F9775D-F7E2-424E-8363-79644A73038F} = {0193DBDA-F7D5-4A71-8097-13D74DF3DE58}
{E8072054-3D92-4321-86B1-E038616AAA3E} = {621819DE-DDB7-41FC-A874-757464A90050}
{BED3CAAD-C9A8-4F29-B35A-3D73A9DECE87} = {621819DE-DDB7-41FC-A874-757464A90050}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {736D9CFE-B80E-445C-8328-D46C08F1FC8D}
Expand Down
19 changes: 19 additions & 0 deletions ODataService/DataSourceManager/DataSourceManager.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<ApplicationIcon />
<OutputType>Library</OutputType>
<StartupObject />
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.6.1" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="2.2.0" />
<PackageReference Include="Microsoft.Restier.AspNetCore" Version="1.0.0" />
<PackageReference Include="Microsoft.Restier.Core" Version="1.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Timers;

namespace DataSourceManager.DataStoreManager
{
/// <summary>
/// Default resource management class to manage resources.
/// Use a dictionary to easily access the resource by <see cref="TKey"/> and make a constraint on the total number of resources.
/// Use a timer for each resource, when the resource live longer than <see cref="MaxDataStoreInstanceLifeTime"/>, it will be destroyed automatically.
/// </summary>
public class DefaultDataStoreManager<TKey, TDataStoreType> :IDataStoreManager<TKey, TDataStoreType> where TDataStoreType : class, new()
{
/// <summary>
/// The max capacity of the resource container, this is a constraint for memory cost.
/// </summary>
public int MaxDataStoreInstanceCapacity { get; set; }

/// <summary>
/// The max life time of each resource. When the resource lives longer than that, it will be destroyed automatically.
/// Besides, when the resource container is full, the resource live longest will be destroyed.
/// </summary>
public TimeSpan MaxDataStoreInstanceLifeTime { get; set; }

private Dictionary<TKey, DataStoreInstanceWrapper> _dataStoreDict = new Dictionary<TKey, DataStoreInstanceWrapper>();

public DefaultDataStoreManager()
{
MaxDataStoreInstanceCapacity = 1000;
MaxDataStoreInstanceLifeTime = new TimeSpan(0, 15, 0);
}

public TDataStoreType ResetDataStoreInstance(TKey key)
{
if (_dataStoreDict.ContainsKey(key))
{
_dataStoreDict[key] = new DataStoreInstanceWrapper(key, MaxDataStoreInstanceLifeTime.TotalMilliseconds, ResouceTimeoutHandler);
}
else
{
AddDataStoreInstance(key);
}

return _dataStoreDict[key].DataStore;
}

public TDataStoreType GetDataStoreInstance(TKey key)
{
if (_dataStoreDict.ContainsKey(key))
{
_dataStoreDict[key].UpdateLastUsedDateTime();
}
else
{
AddDataStoreInstance(key);
}

return _dataStoreDict[key].DataStore;
}

private TDataStoreType AddDataStoreInstance(TKey key)
{
if (_dataStoreDict.Count >= MaxDataStoreInstanceCapacity)
{
// No resource lives longer than maxLifeTime, find the one lives longest and remove it.
var minLastUsedTime = DateTime.Now;
TKey minKey = default(TKey);

foreach (var val in _dataStoreDict)
{
var resourceLastUsedTime = val.Value.DataStoreLastUsedDateTime;
if (resourceLastUsedTime < minLastUsedTime)
{
minLastUsedTime = resourceLastUsedTime;
minKey = val.Key;
}
}

DeleteDataStoreInstance(minKey);
}

System.Diagnostics.Trace.TraceInformation("The resouce dictionary size right now is {0}", _dataStoreDict.Count);
_dataStoreDict.Add(key, new DataStoreInstanceWrapper(key, MaxDataStoreInstanceLifeTime.TotalMilliseconds, ResouceTimeoutHandler));
return _dataStoreDict[key].DataStore;
}

private DefaultDataStoreManager<TKey, TDataStoreType> DeleteDataStoreInstance(TKey key)
{
if (_dataStoreDict.ContainsKey(key))
{
_dataStoreDict[key].StopTimer();
_dataStoreDict.Remove(key);
}

return this;
}

private void ResouceTimeoutHandler(object source, EventArgs e)
{
var resouceUnit = source as DataStoreInstanceWrapper;
if (resouceUnit != null)
{
System.Diagnostics.Trace.TraceInformation(resouceUnit.DatastoreKey + " timeout occured, now destroy it!");
DeleteDataStoreInstance(resouceUnit.DatastoreKey);
}
}

private class DataStoreInstanceWrapper
{
public TKey DatastoreKey { get; private set; }

public TDataStoreType DataStore { get; private set; }

public DateTime DataStoreLastUsedDateTime { get; private set; }

private Timer DataStoreTimer { get; set; }

private double _dataStoreLifeTime;

private EventHandler _timerTimeoutHandler;

public DataStoreInstanceWrapper(TKey key, double dataStoreLifeTime, EventHandler dataStoreTimeoutHandler)
{
DatastoreKey = key;
DataStore = new TDataStoreType();
DataStoreLastUsedDateTime = DateTime.Now;
_dataStoreLifeTime = dataStoreLifeTime;
_timerTimeoutHandler += dataStoreTimeoutHandler;
InitTimer();
}

public DataStoreInstanceWrapper UpdateLastUsedDateTime()
{
UpdateTimer();
DataStoreLastUsedDateTime = DateTime.Now;
return this;
}

public void StopTimer()
{
DataStoreTimer.Stop();
}

private Timer InitTimer()
{
DataStoreTimer = new Timer(_dataStoreLifeTime);
DataStoreTimer.Elapsed += (sender, args) =>
{
if (_timerTimeoutHandler != null)
{
_timerTimeoutHandler.Invoke(this, args);
}
};
DataStoreTimer.Start();
return DataStoreTimer;
}

private void UpdateTimer()
{
DataStoreTimer.Stop();
DataStoreTimer = InitTimer();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

namespace DataSourceManager.DataStoreManager
{
/// <summary>
/// This is a data store management interface to manage the datastore with its key.
/// </summary>
/// <typeparam name="TKey">Type of the key to identify the data store.</typeparam>
/// <typeparam name="TDataStoreType">Type of the real data store.</typeparam>
public interface IDataStoreManager<TKey, TDataStoreType>
{
/// <summary>
/// Get the data store instance by its key.
/// </summary>
/// <param name="key">The key to identify the data store.</param>
/// <returns>Return the data store with key.</returns>
TDataStoreType GetDataStoreInstance(TKey key);

/// <summary>
/// Find the resource by key and reset it to origin.
/// </summary>
/// <param name="key">The key to identify the data store.</param>
/// <returns>Return the data store after reseting.</returns>
TDataStoreType ResetDataStoreInstance(TKey key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

namespace DataSourceManager.DataStoreManager
{
/// <summary>
/// Data store manager for managing a single (i.e., read-only) datastore
/// </summary>
public class SingleDataStoreManager<TKey, TDataStoreType> : IDataStoreManager<TKey, TDataStoreType> where TDataStoreType : class, new()
{
private TDataStoreType dataStore;

public SingleDataStoreManager()
{
dataStore = new TDataStoreType();
}

public TDataStoreType ResetDataStoreInstance(TKey key)
{
return dataStore = new TDataStoreType();
}

public TDataStoreType GetDataStoreInstance(TKey key)
{
return dataStore;
}
}
}
36 changes: 36 additions & 0 deletions ODataService/DataSourceManager/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("Microsoft.OData.Service.Library")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Microsoft.OData.Service.Library")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[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("1ca9b17f-d3f8-4fc3-a992-5135dccab9de")]

// 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")]
Loading