Correct initialization of ReliableCollection from StateManager - service-fabric-stateful

Does anyone have any insights to which of the following two pseudo-patterns are the correct way of instantiating/utilizing reliable collections within a stateful service fabric service? Specifically, wondering if one approach is more performant, more memory-consuming or even more error prone.
Approach 1 (get instance from StateManager inside method):
public class MyService : IService {
public async Task<string> GetSomeValueAsync(string input){
var reliableDic = await StateManager.GetOrAddAsync<IReliableDictionary<string, string>>(StateKey);
var result = await reliableDic.TryGetValue(input);
return result.HasValue ? result.Value : null;
}
}
Approach 2 (store collection as member variable on class)
public class MyService : IService {
private bool _isInitialized;
private readonly object _lock = new object();
private IReliableDictionary<string, string> _dictionary;
private async Task Initialize(){
if (_isInitialized){
return;
}
lock(_lock){
if (_isInitialized){
return;
}
_dictionary = await StateManager.GetOrAddAsync<IReliableDictionary<string, string>>(StateKey);
_isInitialized = true;
}
}
public async Task<string> GetSomeValueAsync(string input){
await Initialize();
var result = await _dictionary.TryGetValue(input);
return result.HasValue ? result.Value : null;
}
}
So, approach 1 fetches the dictionary from the StateManager in each method while approach 2 does a lazy initialization check and then uses class members.
Most samples we see are using approach 1, but the idea behind approach 2 is to store the reliable dictionary in an instance field and avoid any StateManager.GetOrAddAsync hit in each method as well as centralize the handling of the StateKey which could be beneficial in larger services with many methods and potentially more reliable collections.
Would love to learn if there are any pitfalls or inefficiencies in either approach (obviously approach 2 is more verbose and uses more lines of code, but that is not the primary concern).

Actually there is no real reason to cache result of StateManager.GetOrAddAsync except saving memory allocations of Task object or making it available in places where having StateManager available isn't appropriate.
The reason for this is quite simple - the StateManager already caches the instance of IRealiableState for you. So it returns the same instance each time you do StateManager.GetOrAddAsync (here is the official answer from Microsoft).
You also can check it yourself with the very simple test (the c is true):
var q1 = stateManager.GetOrAddAsync<IReliableDictionary<string, string>>("MyDict")
.GetAwaiter().GetResult();
var q2 = stateManager.GetOrAddAsync<IReliableDictionary<string, string>>("MyDict")
.GetAwaiter().GetResult();
var c = q1 == q2;

Related

Reactor spring mongodb repository combine multiple results together

I'm kind of new to reactive programing and currently working on a spring webflux based application. I'm stuck between few questions.
public class FooServiceImpl {
#Autowired
private FooDao fooDao;
#Autowired
private AService aService;
#Autowired
private BService bService;
public long calculateSomething(long fooId) {
Foo foo = fooDao.findById(fooId); // Blocking call one
if (foo == null) {
foo = new Foo();
}
Long bCount = bService.getCountBByFooId(fooId); // Blocking call two
AEntity aEntity = aService.getAByFooId(fooId); // Blocking call three
// Do some calculation using foo, bCount and aEntity
// ...
// ...
return someResult;
}
}
This is the way we write a blocking code which uses three external API call results (let's consider as DB calls). I'm struggling to convert this into a reactive code, If all three becomes mono and if I subscribe all three will the outer subscriber get blocked?
public Mono<Long> calculateSomething(long fooId) {
return Mono.create(sink -> {
Mono<Foo> monoFoo = fooDao.findById(fooId); // Reactive call one
monoFoo.subscribe(foo -> {
if (foo == null) {
foo = new Foo();
}
Mono<Long> monoCount = bService.getCountBByFooId(fooId); // Reactive call two
monoCount.subscribe(aLong -> {
Mono<AEntity> monoA = aService.getAByFooId(fooId); // Reactive call three
monoA.subscribe(aEntity -> {
//...
//...
sink.success(someResult);
});
});
});
};
}
I saw there is a function called zip, but it only works with two results, So is there a way to apply it here?
Also what will happen if we get subscribe for something inside create method, Will it block the thread?
Would be very thankful if you could help me.
If you gave me the calculation you want you do with those values, it would be easier for me to show the reactor way of doing it. But lets suppose you want to read a value from database and then use that value for another thing. Use flatmaps and make a unique Flux reducing the lines of code and complexity, no need to use subscribe() as told by the other people. Example:
return fooDao.findById(fooId)
.flatmap(foo -> bService.getCountBByFooId(foo))
.flatmap(bCount -> aService.getAByFooId(fooId).getCount()+bCount);

RxJava- When not to use an Observable?

I came across a scenario in using RxJava and I am not quite sure if I should use an Observable<T> or a final ImmutableList<T>.
Basically, if I import a final and immutable dataset once and never again, should I really expose that as a cold Observable<T>?
public final class StrategyManager {
private static final StrategyManager instance = new StrategyManager();
private final ImmutableList<Strategy> strategies;
private StrategyManager() {
strategies = //import from db
}
public Observable<Strategy> getStrategies() {
return Observable.from(strategies);
}
public static StrategyManager get() {
return instance;
}
}
Or should I just expose it as an ImmutableList<T>?
public final class StrategyManager {
private static final StrategyManager instance = new StrategyManager();
private final ImmutableList<Strategy> strategies;
private StrategyManager() {
strategies = //import from db
}
public ImmutableList<Strategy> getStrategies() {
return strategies;
}
public static StrategyManager get() {
return instance;
}
}
If I expose it as an ImmutableList<T>, the clients have one less monad to deal with for something that will always be constant.
However, maybe I lose flexibility and should use an Observable<T>. For instance, I can decide to use RxJava-JDBC to query the data directly on each call without any caching. Or I can cache() or even replay() so the data can expire and free up memory.
public final class StrategyManager {
private static final StrategyManager instance = new StrategyManager();
private final Observable<Strategy> strategies;
private Database db = null;
private StrategyManager() {
strategies = db.select("SELECT * FROM STRATEGY")
.autoMap(Strategy.class)
.replay(1, TimeUnit.MINUTES)
.autoConnect();
}
public Observable<Strategy> getStrategies() {
return strategies;
}
public static StrategyManager get() {
return instance;
}
}
So my question is, are there situations to not use an Observable? Or in a reactive application, should I always use an Observable even for constant data sets that will not change? Am I right that I should use the latter for flexibility and easily changing behaviors?
I like the question. I suppose there a lots of factors involved in the decision to use a reactive API and no clear Yes or No answer, just a judgement call on what the future might hold.
should I always use an Observable even for constant data sets that will not change?
If you want maximal flexibility, don't mind burdening the client with using RxJava, don't mind debugging difficulties (you've seen long RxJava stacktraces) then use an Observable. Note that even for a "constant data set that will not change" your memory constraints might change and a large data set might not be suitable for holding in memory any more.
Another thing to keep in mind is that Observables do have some processing overhead (volatile reads on every emission for potentially every operator in the chain) so for performance reasons it's sometimes good not to use.
Your use cases, your data and your benchmarks will really determine which way you go.
Would be interesting to hear from API designers within Netflix (and anywhere else) about their experience.

How to resolve generic type at runtime

I'm trying to build a command processor that can take any command that implements a marker interface (or maybe descends from a base class). The processor will handle the command that it is asked to process. However I'm struggling with resolving the true generic type as Resolve(Type) returns an object.
I'm not sure is how to cast this if at all possible?
public void Process(ICommand command)
{
var c = command.GetType();
var t = typeof(ICommandHandler<>).MakeGenericType(new[] { c });
var o = container.Resolve(t);
//((ICommandHandler)o).Handle(command); *** This doesn't work
}
The calling code would be something like this -
Dispatcher.Process(new SomeCommand(Guid.NewGuid(),"Param1",12345));
If you absolutely have to call the ICommandHandler<T>.Handle method and you have no other control over the design of the system, then reflection may be your only choice. There's no great way to deal with the switch from generic to non-generic.
Otherwise, you may have a couple of options.
First, if your Dispatcher.Process can be made generic, you can save all the casting.
public static class Dispatcher
{
public static void Process<T>(T command) where T : ICommand
{
var handler = container.Resolve<ICommandHandler<T>>();
handler.Handle(command);
}
}
This is a pretty common solution to a problem like this that I've seen out in the wild.
If you can't do that, then you may be able to make your ICommandHandler<T> interface implement a non-generic ICommandHandler base interface.
public interface ICommandHandler
{
void Handle(ICommand command);
}
public interface ICommandHandler<T> : ICommandHandler
{
void Handle(T command);
}
In this latter case you'd have to switch your strongly-typed command handler implementations to call the same internal logic for generic or basic handling or you'll get different handling based on the call, which would be bad:
public class SomeCommandHandler : ICommandHandler<SomeCommand>
{
public void Handle(ICommand command)
{
var castCommand = command as SomeCommand;
if(castCommand == null)
{
throw new NotSupportedException("Wrong command type.");
}
// Hand off to the strongly-typed version.
this.Handle(castCommand);
}
public void Handle(SomeCommand command)
{
// Here's the actual handling logic.
}
}
Then when you resolve the strongly-typed ICommandHandler<T> your cast down to ICommandHandler (as shown in your question's sample code) will work.
This is also a pretty common solution, but I've seen it more in systems that existed before generics were available where an updated API was being added.
However, in all cases here, the problem really isn't that Autofac is returning an object; it's a class/type design problem that affects any generic-to-non-generic conversion scenario.
Using Reflection - but is this the best way to approach this?
public void Process(Command command)
{
var c = command.GetType();
var ot = typeof(ICommandHandler<>);
var type = ot.MakeGenericType(new[] { c });
var mi = type.GetMethod("Handle");
var o = container.Resolve(type);
mi.Invoke(o, new object[] { command });
}

What is the best usage of Dispose for "Entity Framework"?

What is the true usage of dispose ?
This is first (my) approach:
public class RequestService : IDisposable
{
requestDBEntities db;
public RequestService() //Constructor
{
db = new requestDBEntities ();
}
public List<RequestStatus> ddlFill()
{
return (from rs in db.reqStatus select rs).ToList();
}
//Some Insert,Update,Delete methods {}...
public void Dispose()
{
db.Dispose(); //<--Dispose requestDBEntities();
GC.SuppressFinalize(this);
}
And second approach:
public class RequestService : IDisposable
{
requestDBEntities db;
public List<RequestStatus> ddlFill()
{
using (db = new requestDBEntities())
return (from rs in db.reqStatus select rs).ToList();
}
//Some Insert,Update,Delete methods {}...
public void Dispose()
{
GC.SuppressFinalize(this);
}
Page Code-behind:
using (RequestService _reqService = new RequestService ())
{
ddlRequestStatus.DataSource = _reqService.ddlFill();
ddlRequestStatus.DataBind();
//Some Insert,Update,Delete operations...
}
Thank you..
It's difficult (impossible) to say which is the better approach, since we don't know your exact design choice. From the perspective of a simple app, it may seem that the two examples are equivalent (more or less), but they really do behave in entirely different ways.
The fundamental question here is: Should the lifetime of db be comparable to the lifetime of its enclosing RequestService instance?
If yes, then the first code example is the way to do things. Resources will be managed as expected (db disposes its stuff at the same time the RequestService object is asked to dispose of its resources), and the lifetime of the two will be pretty much identical (db is constructed in the RequestService constructor, and db become collectable as soon as its RequestService object becomes collectable).
However, if the answer is "no", then the second example is (almost) the way to do things - since you generate a collection view of the RequestStatus object from db, you really have no reason to keep db around any longer. However, to make this a bit better, may I suggest restricting the scope of db to within the ddlFill method:
public List<RequestStatus> ddlFill()
{
using (var db = new requestDBEntities())
return (from rs in db.reqStatus select rs).ToList();
}
No need for an extraneous class member when a stack value suffices.
What for in second example you have declared requestDBEntities db; at class level instead of having it as a stack variable?
Comparing your approaches, the question is - are you ready to create requestDBEntities on each call? If you are - second approach is better, in fact if there is nothing else you haven'y posted - you don't need dispose at all. But you'll have extra time penalty on instantiating/releasing requestDBEntities on each call.

Resolving a collection of services from a service type

I have a rather complex bit of resolving going on in Autofac. Basically I want all the objects in the container which implement a specifically named method with a specific argument type. I have implemented some somewhat insane code to get it for me
var services = (from registrations in _componentContext.ComponentRegistry.Registrations
from service in registrations.Services
select service).Distinct();
foreach (var service in services.OfType<Autofac.Core.TypedService>())
{
foreach (var method in service.ServiceType.GetMethods().Where(m => m.Name == "Handle"
&& m.GetParameters().Where(p => p.ParameterType.IsAssignableFrom(implementedInterface)).Count() > 0))
{
var handler = _componentContext.Resolve(service.ServiceType);
method.Invoke(handler, new Object[] { convertedMessage });
}
}
My problem arises in that the handler returned the the resolution step is always the same handler and I cannot see a way to resolve a collection of the the registrations which are tied to the service as one might normally do with container.Resolve>().
I feel like I'm pushing pretty hard against what AutoFac was designed to do and might do better with a MEF based solution. Is there an easy AutoFac based solution to this issue or should I hop over to a more composition based approach?
G'day,
In MEF you could use 'Method Exports' for this (http://mef.codeplex.com/wikipage?title=Declaring%20Exports) but that might be a bit drastic. There are a couple of ways to achieve what you want in Autofac.
You can make the above code work by searching for registrations rather than services:
var implementorMethods = _componentContext.ComponentRegistry.Registrations
.Select(r => new {
Registration = r,
HandlerMethod = r.Services.OfType<TypedService>()
.SelectMany(ts => ts.ServiceType.GetMethods()
.Where(m => m.Name == "Handle" && ...))
.FirstOrDefault()
})
.Where(im => im.HandlerMethod != null);
foreach (var im in implementorMethods)
{
var handler = _componentContext.ResolveComponent(im.Registration, new List<Parameter>());
im.HandlerMethod.Invoke(handler, new object[] { convertedMessage });
}
I.e. implementorMethods is a list of the components implementing a handler method, along with the method itself. ResolveComponent() doesn't rely on a service to identify the implementation, so there's no problem with the service not uniquely identifying a particular implementor.
This technique in general will probably perform poorly (if perf is a concern here) but also as you suspect will work against the design goals of Autofac (and MEF,) eliminating some of the benefits.
Ideally you need to define a contract for handlers that will let you look up all handlers for a message type in a single operation.
The typical recipe looks like:
interface IHandler<TMessage>
{
void Handle(TMessage message);
}
Handlers then implement the appropriate interface:
class FooHandler : IHandler<Foo> { ... }
...and get registered at build-time like so:
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(typeof(FooHandler).Assembly)
.AsClosedTypesOf(typeof(IHandler<>));
To invoke the handlers, define a message dispatcher contract:
interface IMessageDispatcher
{
void Dispatch(object message);
}
...and then its implementation:
class AutofacMessageDispatcher : IMessageDispatcher
{
static readonly MethodInfo GenericDispatchMethod =
typeof(AutofacMessageDispatcher).GetMethod(
"GenericDispatch", BindingFlags.NonPublic | BindingFlags.Instance);
IComponentContext _cc;
public AutofacMessageDispatcher(IComponentContext cc)
{
_cc = cc;
}
public void Dispatch(object message)
{
var dispatchMethod = GenericDispatchMethod
.MakeGenericMethod(message.GetType());
dispatchMethod.Invoke(this, new[] { message });
}
void GenericDispatch<TMessage>(TMessage message)
{
var handlers = _cc.Resolve<IEnumerable<IHandler<TMessage>>>();
foreach (var handler in handlers)
handler.Handle(message);
}
}
...which is registered like so:
builder.RegisterType<AutofacMessageDispatcher>()
.As<IMessageDispatcher>();
The component that feeds in the messages will then resolve/use IMessageDispatcher to get the messages to the handlers.
var dispatcher = _cc.Resolve<IMessageDispatcher>();
dispatcher.Dispatch(message);
There are still ways to do this without the interface, but all rely on creating some kind of contract that uniquely defines handlers for a particular message (e.g. a delegate.)
In the long run the generic handler pattern will be the easiest to maintain.
Hope this helps, Nick.