Does MongoCollection.forEach need to be thread safe? - mongodb

When using the MongoDB Async Java Driver:
Does the following callback need to use a AtomicInteger counter or would a normal int do the job?
Block<Document> theBlock = new Block<Document>() {
AtomicInteger counter = new AtomicInteger();
#Override
public void apply(final Document document) {
counter.incrementAndGet();
}
};
SingleResultCallback<Void> callbackWhenFinished = ...
collection.find().forEach(theBlock, callbackWhenFinished);

The only real difference between the MongoDB Java API and its async counterpart is that the methods of the latter are non-blocking and take callbacks as arguments. This means that what you receive in your callback is equivalent to what the method returns in the non-async API.
Here, you use the find method. It returns a "normal" iterable, so calling forEach on it will not result in multiple threads.
In other words, you don't need an AtomicInteger: your apply method is called sequentially, by the same thread.
If you still have doubts or need a "proof", you can do one of the following:
add a System.out.println(Thread.currentThread().getName()); inside your block. You will see it is always performed by the same thread;
add a breakpoint inside your block, configured to stop only the thread. Once again, the breakpoint will block the whole code.

Related

Isolates in Flutter - Where can I place the isolates inside my Flutter code

I have some confusion regarding how Isolates can be used inside a Flutter application.
If we go through the documentation, It is said that functions that you pass inside the isolates should only be declared as top-level functions. Does that mean we cannot declare them inside a class ?
I created a class TestIsolate inside my lib/business_logic/bloc folder.
class TestIsolate {
Future<void> handle(int _m) async {
final response = ReceivePort();
await Isolate.spawn(_isolate, response.sendPort);
final sendPort = await response.first as SendPort;
final answer = ReceivePort();
sendPort.send([_m, answer.sendPort]);
await answer.first.then((p) {
log(p);
});
}
static void _isolate(SendPort _initialReplyTo) {
final port = ReceivePort();
_initialReplyTo.send(port.sendPort);
port.listen((message) {
final data = message[0] as int;
final send = message[1] as SendPort;
send.send(_syncHandle(data));
});
}
}
Future<String> _syncHandle(int data) async {
return 'done - $data';
}
I then called await TestIsolate.handle(15) upon an onTap event from my presentation layer which worked like a charm.
Am I doing this correctly ? If yes, can we call handle() placed inside TestIsolate class as a top-level function?
Any help would be really appreciated!
What you are doing is correct. The source you reference (which I will point out is an article, not documentation) says:
The function passed to the isolate spawn() must be a top-level function *(a function that is not within the boundary of a class) or a static method.
You are spawning an isolate with an entry point of _isolate(), which is a static method. So, according to your source, that is ok.
However, it may be the case that your source is outdated. According to the changelog for Dart 2.15:
Allow closures both in inter-isolate messages as well as as entrypoints in Isolate.spawn(<entrypoint>, ...) calls. Closures and their enclosing context may need to be copied in this process. The enclosing context is - as with normal messages - verified to only contain objects that are sendable.
Note of caution: The Dart VM's current representation of enclosing variables in closures can make closures hang on to more variables than strictly needed. Using such closures in inter-isolate communication can therefore lead to copying of larger transitive object graphs. If the extended transitive closure includes objects that are illegal to send, the sending will fail.
It would appear that closure (and non-static method) arguments to Isolate.spawn() were introduced after your source article was written.
As the changelog cautions though, you do want to be cognizant of the memory copying that will occur.
Further references:
Is DartDocs about the entry of Isolate.spawn wrong or something?

Bound variable in ViewModel is not updating the displayed value

I was trying to create an countdown timer in ViewModel but i didnt found any method to do that so i ve tried to do this with task delay and while loop but it ends after first task delay. Do u know any other way how to do that or how to repair that one.
public PageViewModel()
{
MethodName();
}
public async void MethodName()
{
CountSeconds = 10;
while (CountSeconds > 0)
{
await Task.Delay(1000);
CountSeconds--;
}
}
The reason why you can`t see others is related to context. You trying to run async code in non-async context.
To solve this problem you can do several ways, which way to choose is your choice and depends on your needs:
await MethodName();
async Task MethodName()
{
CountSeconds = 10;
while (CountSeconds > 0)
{
await Task.Delay(1000);
CountSeconds--;
}
}
Another way is to create various of tasks and execute them, here you can see methods, which can help you.
And as Rand Random metioned it's not about MAUI, it`s about understanding of async programming itself, so for you will be usefull read more about it.
You can use Dispatacher.StartTimer() (available in the DispatcherExtensions class) to create a function that will execute every x seconds/minutes/hours (depending of what you're setting) using the device's clock.
To access the Application's Dispatcher from any class, use the following line:
var dispatcher = Application.Current.Dispatcher;
Since there is no documentation available yet for MAUI, you can read the Device.StartTimer() documentation from Xamarin, which acts exactly the same.

Why can't I use local variable in a callback?

Let's say I've a stream() which returns Stream<int>. stream().listen returns StreamSubscription.
var subs = stream().listen((e) {
if (someCondition) subs.cancel(); // Error
});
I don't understand why is there an error, because by the time I start listening for events in the listen method, I would have definitely a valid object subs.
Note: I know this can be done by creating a StreamSubscription instance/top-level variable but why they have prevented the use of local variable like this?
We know that Stream.listen does not call its callback until after a value is returned, but the Dart compiler does not.
Consider the following function, which simply calls a callback and returns the result:
T execute<T>(T Function() callback) => callback();
Now, consider using it to assign a variable:
int myVariable = execute(() => myVariable + 1);
The problem here is that the given callback is called synchronously, before myVariable is assigned, but it tries to use myVariable to calculate a value!
To resolve this issue with your stream question, you can use the new late keyword. Using late tells the compiler that you know the variable will be assigned by the time it's accessed.
late final StreamSubscription<MyType> subscription;
subscription = stream().listen(/* ... */);
Likely because it's possible that subs will be used before it's assigned. We know that the callback passed to listen will be called on stream events, but it's also possible that the callback is called immediately and it's return value or a calculation done by it may be required for the return value of the function it was passed to.
Take this fakeFunc for instance, which I made an extension on the int class for convenience:
extension FakeListen on int {
int fakeFunc(int Function(int x) callback, int val) {
return callback(val);
}
}
The return value depends on the result of callback!
int subs = x.fakeFunc((e) {
print(e);
subs.toString();//error
return e + 1;
}, 5);
I can't use subs because subs will be guaranteed to not be exist at this point. It's not declared. This can be easily solved by moving the declaration to a separate line, but also forces you to make it nullable. Using late here won't even help, because subs won't exist by the time you try to use it in the callback.
Your scenario is different, but this is an example of where allowing that would fail. Your scenario involves a callback that is called asynchronously, so there shouldn't be any issues with using subs in the callback, but the analyzer doesn't know that. Even async-marked methods could have this issue as async methods run synchronously up until its first await. It's up to the programmer to make the right decision, and my guess is that this error is to prevent programmers from making mistakes.

Can I use AtomicReference to get value of a Mono and code still remain reactive

Sorry, I am new to reactive paradigm. Is is possible to use AtomicReference to get value of a Mono since reactive code can run asynchronously and different events run on different thread. Please see the sample below. I am also not sure if this piece of code is considered reactive
sample code:
public static void main(String[] a) {
AtomicReference<UserDTO> dto = new AtomicReference<>();
Mono.just(new UserDTO())
.doOnNext(d -> d.setUserId(123L))
.subscribe(d -> dto.set(d));
UserDTO result = dto.get();
dto.set(null);
System.out.println(result); // produce UserDTO(userId=123)
System.out.println(dto.get()); // produce null
}
The code snippet you have shared is not guaranteed to always work. There is no way to guarantee that the function inside doOnNext will happen before dto.get(). You have created a race condition.
You can run the follow code to simulate this.
AtomicReference<UserDTO> dto = new AtomicReference<>();
Mono.just(new UserDTO())
.delayElement(Duration.ofSeconds(1))
.doOnNext(d -> d.setUserId(123L))
.subscribe(dto::set);
UserDTO result = dto.get();
System.out.println(result); // produces null
To make this example fully reactive, you should print out in the subscribe operator
Mono.just(new UserDTO())
.doOnNext(d -> d.setUserId(123L))
.subscribe(System.out::println)
In a more "real world" example, your method would return a Mono<UserDTO> and you would then perform transformations on this using map or flatMap operators.
** EDIT **
If you are looking to make a blocking call within a reactive stream this previous stack overflow question contains a good answer

Reactive mono how to propagate subscription context to `doOnSubscribe` and `doFinally`

I have following aspect that tracks method execution time:
public Object addMetricsToReactiveMonoTimedMethod(ProceedingJoinPoint pjp, ReactiveTimed reactiveTimed) throws Throwable {
StopWatch stopWatch = new StopWatch();
Mono<?> mono = (Mono<?>) pjp.proceed();
return mono
.doOnSubscribe(subscription -> stopWatch.start())
.doFinally(signalType -> {
stopWatch.stop();
logTimer(pjp, stopWatch, reactiveTimed.name(), signalType);
});
}
Method itself looks like that:
public Mono<String> sayHi() {
return Mono.just("hi")
.subscriberContext(context -> context.put("requestId", "requestId"));
}
How can I get requestId variable from subscriber context in my aspect method? I want to use it in doFinally to know which request was profiled.
Disclaimer: I have never used Reactor or anything like it in my whole life. I found this question due to the aspectj tag.
After a quick look at the Mono Javadoc to me it looks like you could just call subscriberContext(Function<Context, Context>) again in the aspect, just like in the target method. You get the existing context as an input parameter for the function or lambda and can do with it what you want. The result of your dummy function/lambda would be a new context, but you can just discard it. I have not tested it, but I mean something like this:
// ...
Mono<?> mono = (Mono<?>) pjp.proceed();
// Alternatively, use a List<Context> with one element, a Stack<Context>, ...
Context[] targetContext = new Context[1];
mono.subscriberContext(context -> {
targetContext[0] = context;
// We can also return null, it does not matter because we are not interested
// in the newly created context, only in the original one we salvaged into the
// outer array.
return context;
});
System.out.println("Now do whatever you need to do with " + targetContext[0]);
// ...
I use a single-element array/list - instead you could "abuse" any other wrapper object such as an atomic reference or a thread-local as a wrapper because you cannot directly assign a to a Context variable from inside the lambda. The code would not compile because the outer variable referenced from inside the lambda needs to be effectively final.