How to prevent a connection from a single out-port blocking packets on another connection on the same port? - noflo

I'm trying to take output from a single process (P1) and perform parallel tasks on it using other processes (P2 and P3). So far so simple.
To do this I'm connecting P2 and P3 to the single out-port of P1. In my mind, this should mean that P1 emits packets through its out port that are picked up by both P2 and P3 simultaneously, in parallel.
What I'm finding is that P2 and P3 aren't started in parallel and instead one of the processes will wait until the other has finished processing (or at least it seems that way to me).
For example, here is a simple graph that should take a JSON input then simultaneously grab a timestamp and parse the JSON. Another timestamp is taken after parsing the JSON and this is used as a basic method for calculating how long the JSON parsing took.
Notice the ordering of the connections going from the ajax/Get out port (the timestamp connection was added last).
In this case the difference in the timestamps is around 5ms, which roughly lines up with how long the JSON parse takes in a non-NoFlo environment (it's actually a little longer in NoFlo for some reason).
Now take the same graph but this time the connection-order from the ajax/Get out port has changed (the parse connection was added last):
This time the difference between the timestamps is around 40–50ms, which is clearly a massive difference and far larger than what the parse takes outside of NoFlo.
I'd really appreciate it if someone can shed some light on the following:
Why are the timings so different depending on the connection order?
How can I ensure that the 2 connections coming from ajax/Get are triggered and run in parallel (ie. they don't wait on each other)?
If it helps, here's a JSON export of the graph from FlowHub.
I've also put together a simple graph using the CLI and have managed to get a better insight into the flow of the graph and perhaps shed some light on what might be causing this:
# This executes in the correct order, though likely by
# coincidence and not due to true parallelisation.
#
# Time1 is run and outputted before Time2.
#
Read(filesystem/ReadFile) OUT -> IN Time1(objects/GetCurrentTimestamp)
Read OUT -> IN Parse(strings/ParseJson)
# This executes the entire Parse path before going back to grab
# and output Time1.
#
# Time1 is run and outputted *after* Time2
# Read doesn't send a disconnect message to Parse until *after*
# Time 1 is outputted.
#
# Read doesn't send a disconnect message to Time1 until *after*
# the Parse path has finished disconnecting.
#
# Read(filesystem/ReadFile) OUT -> IN Parse(strings/ParseJson)
# Read OUT -> IN Time1(objects/GetCurrentTimestamp)
Time1 OUT -> IN Display1(core/Output)
Parse OUT -> IN Time2(objects/GetCurrentTimestamp)
Time2 OUT -> IN Display2(core/Output)
'sample.geojson' -> IN Read
When run with the Read to Time1 connection defined before Read to Parse then the network is in order, though I've noticed that Read waits until everything else has completed before firing a disconnect (is that right?):
DATA -> ENCODING Read() CONN
DATA -> ENCODING Read() DATA
DATA -> ENCODING Read() DISC
DATA -> IN Read() CONN
DATA -> IN Read() DATA
DATA -> IN Read() DISC
Read() OUT -> IN Time1() CONN
Read() OUT -> IN Time1() < sample.geojson
Read() OUT -> IN Parse() CONN
Read() OUT -> IN Parse() < sample.geojson
Parse() OUT -> IN Time2() CONN
Parse() OUT -> IN Time2() < sample.geojson
Read() OUT -> IN Time1() DATA
Time1() OUT -> IN Display1() CONN
Time1() OUT -> IN Display1() DATA
1422549101639
Read() OUT -> IN Parse() DATA
Parse() OUT -> IN Time2() DATA
Time2() OUT -> IN Display2() CONN
Time2() OUT -> IN Display2() DATA
1422549101647
Read() OUT -> IN Time1() > sample.geojson
Read() OUT -> IN Parse() > sample.geojson
Parse() OUT -> IN Time2() > sample.geojson
Read() OUT -> IN Time1() DISC
Time1() OUT -> IN Display1() DISC
Read() OUT -> IN Parse() DISC
Parse() OUT -> IN Time2() DISC
Time2() OUT -> IN Display2() DISC
If I switch the order so the Read to Parse connection is defined first then everything goes wrong and Time1 isn't even sent a packet from Read until the entire Parse path has completed (so Time1 is actually after Time2 now):
DATA -> ENCODING Read() CONN
DATA -> ENCODING Read() DATA
DATA -> ENCODING Read() DISC
DATA -> IN Read() CONN
DATA -> IN Read() DATA
DATA -> IN Read() DISC
Read() OUT -> IN Parse() CONN
Read() OUT -> IN Parse() < sample.geojson
Parse() OUT -> IN Time2() CONN
Parse() OUT -> IN Time2() < sample.geojson
Read() OUT -> IN Time1() CONN
Read() OUT -> IN Time1() < sample.geojson
Read() OUT -> IN Parse() DATA
Parse() OUT -> IN Time2() DATA
Time2() OUT -> IN Display2() CONN
Time2() OUT -> IN Display2() DATA
1422549406952
Read() OUT -> IN Time1() DATA
Time1() OUT -> IN Display1() CONN
Time1() OUT -> IN Display1() DATA
1422549406954
Read() OUT -> IN Parse() > sample.geojson
Parse() OUT -> IN Time2() > sample.geojson
Read() OUT -> IN Time1() > sample.geojson
Read() OUT -> IN Parse() DISC
Parse() OUT -> IN Time2() DISC
Time2() OUT -> IN Display2() DISC
Read() OUT -> IN Time1() DISC
Time1() OUT -> IN Display1() DISC
If this is correct behaviour, then how do I run the 2 branches in parallel without one blocking the other?
I've tried making every component asynchronous, I've tried both that and using the WirePattern, I've tried creating multiple out ports and sending the data through all of them at once. No joy – it always comes down to the order in which the first edges are connected. I'm pulling my hair out with this as it's completely blocking my use of NoFlo for ViziCities :(

NoFlo can't do multiple things in parallel due to the single-threaded nature of the JavaScript engine. I/O calls run in their own threads, but their callbacks always return us to the main thread where NoFlo runs.
In NoFlo, as long as we're dealing with synchronous components (like everything else than ajax/Get in your example graph appears to be), we execute depth-first to ensure fast throughput.
This means that the sub-flow from the first outbound connection of ajax/Get runs to completion first, then the second.
What you would want to happen here is breadth-first execution instead of depth-first. There has been some discussion on enabling that via edge metadata, but until then a way to do this would be to add core/RepeatAsync nodes between the connections of interest.
In long term the other interesting approach would be to enable running parts of the flow in their own threads via RemoteSubgraph and Web Workers. Theoretically we could even run each process in its own thread, giving full compatibility with classical FBP. But this would come at a hefty start-up cost.

I won't consider the browser example, it is indeed depth-first because of how browser-side JavaScript works, as bergie explained.
The CLI example is more interesting though, because noflo-nodejs uses EventEmitters extensively. It still isn't truly parallel but it is more concurrent.
What we see here is a side effect of the following:
Events are processed in the order of their occurrence.
The order in which branches are defined in the graph affects the order in which events occur.
Most components are triggered by data event rather than disconnect. They don't wait for disconnect to process data and send the result along.
In the aggregate it explains why the first branch executes before the second branch and why disconnects follow after all data has been processed already.
It might give you an impression of pure synchrony here, but despite the facts listed above, the system is still concurrent. If Read sent multiple packets at a decent speed, you would see events for branch 1 and branch 2 intermixed.
Update:
Here's a common trick for turning a sync component into async:
setTimeout(function() {
doTheSyncJob(); // actual code here
callback();
}, 0);

Related

what is Mono.zipDelayError equal function in Flux

I want to Triger the below two calls parallelly and combine the Mono and flux.
Mono<EmpAddressDetail> empAddDetail = getTimeoutDuration()
.flatMapDelayError(duration -> timeoutWrappedEmpDetailFlux(Service.getemeEmpAddress(empno),
duration, Exception.ErrorCode.TIMED_OUT), CONCURRENCY, PREFETCH);
Flux<Employee> empInfo= getTimeoutDuration()
.flatMap(duration -> mapEmpTypes(empTypes)
.map(empTypedata -> Tuples.of(duration, empTypedata)))
.flatMapDelayError(durationEmpTuple -> getEmpdetails(empno, durationBusinessTuple.getT1(), durationEmpTuple.getT2())
.filter(empdetails -> requestTypes.contains(empdetails.getType()))
.doOnNext(empdetails -> empdetails.setEmpId(empno)), CONCURRENCY, PREFETCH);
tried with Mono.zipDelayError but looking alternate in flux without converting my flux object into mono. do we have any method in flux for Triger the parallel call and merge the result.

How to keep redis connection open when reading from reactive API

I am continuously listening on redis streams using the spring reactive api(using lettuce driver). I am using a standalone connection. It seems like the reactor's event loop opens a new connection every time it reads the messages instead of keeping the connection open. I see a lot of TIME_WAIT ports in my machine when i run my program. Is this normal? Is there a way to let lettuce know to re-use the connection instead of reconnecting every time?
This is my code:
StreamReceiver<String, MapRecord<String, String, String>> receiver = StreamReceiver.create(factory);
return receiver
.receive(Consumer.from(keyCacheStreamsConfig.getConsumerGroup(), keyCacheStreamsConfig.getConsumer()),
StreamOffset.create(keyCacheStreamsConfig.getStreamName(), ReadOffset.lastConsumed()))//
// flatMap reads 256 messages by default and processes them in the given scheduler
.flatMap(record -> Mono.fromCallable(() -> consumer.consume(record)).subscribeOn(Schedulers.boundedElastic()))//
.doOnError(t -> {
log.error("Error processing.", t);
streamConnections.get(nodeName).setDirty(true);
})//
.onErrorContinue((err, elem) -> log.error("Error processing message. Continue listening."))//
.subscribe();
Looks like the spring-data-redis library re-uses the connection only if the poll timeout is set to '0' in the stream receiver options and pass it as the second argument in StreamReceiver.create(factory, options). Figured by looking into spring-data-redis' source code.

How to Integrate Akka.Net Streams with AspNet core, or giraffe

When I use Giraffe or ASP.Net Core in general, I can create an actor system, add it as a service and then get It thought the request handler select any actor and ask/tell a message.
Either using Cluster.Sharding or a normal user/actor I know it will be a single instance of the actor in the whole system processing multiple messages.
How can I do the same communication with Streams? They don’t seem to be references in the router, or the actor system as the actor paths: Actor References, Path and Addresses.
Should this be done differently?
Copying from the IO section, I could materialize one graph to handle each request, but in general I communicate with “Singletons” like Domain Driven Design Aggregate Roots to handle the domain logic (thats why the sharding module), I’m not sure how to do Singleton Sinks that can be used in the newly materialized graph in the request handler, as there must be only one sink for all the requests.
There are many ways to integrate akka streams with external systems. The one that makes it easy recipient would be Source.queue (somewhat similar to System.Threading.Channels and predating them). You can materialize your stream at initialization point and then register queue endpoints in Giraffe DI - this way you don't pay cost of the same stream initialization on every request:
open Akka.Streams
open Akkling
open Akkling.Streams
open FSharp.Control.Tasks.Builders
let run () = task {
use sys = System.create "sys" <| Configuration.defaultConfig()
use mat = sys.Materializer()
// construct a stream with async queue on both ends with buffer for 10 elements
let sender, receiver =
Source.queue OverflowStrategy.Backpressure 10
|> Source.map (fun x -> x * x)
|> Source.toMat (Sink.queue) Keep.both
|> Graph.run mat
// send data to a queue - quite often result could be just ignored
match! sender.OfferAsync 2 with
| :? QueueOfferResult.Enqueued -> () // successfull
| :? QueueOfferResult.Dropped -> () // doesn't happen in OverflowStrategy.Backpressure
| :? QueueOfferResult.QueueClosed -> () // queue has been already closed
| :? QueueOfferResult.Failure as f -> eprintfn "Unexpected failure: %O" f.Cause
// try to receive data from the queue
match! receiver.AsyncPull() with
| Some data -> printfn "Received: %i" data
| None -> printfn "Stream has been prematurelly closed"
// asynchronously close the queue
sender.Complete()
do! sender.WatchCompletionAsync()
}

What does it mean that an object handle has so many TimerQueueTimer references

I have an app in which I suspect a memory leak. Not only in the heap, but it seems to me the whole working set is growing for each request that is made to my app. I am trying to debug it according to these instructions but I am having a hard time interpreting what I see. I am using the dotnet-dump tool to analyze a dump.
All in all I have 618 DocumentClient instances if I interpret it correctly. Of course that will add up to a lot of data in strings, byte arrays etc.
Statistics:
MT Count TotalSize Class Name
00007f853c355110 618 187872 Microsoft.Azure.Cosmos.DocumentClient
Here is a snippet of a single reference taken from the method table of the document client. See the pastebin for full reference. It continues for 1200+ lines with mostly TimerQueueTimer references.
00007F85AF2F10D8 (strong handle)
-> 00007F84C80FBAD8 System.Object[]
-> 00007F84C80FBB00 System.Threading.ThreadLocal`1+LinkedSlotVolatile[[System.Collections.Concurrent.ConcurrentBag`1+WorkStealingQueue[[System.IDisposable, System.Private.CoreLib]], System.Collections.Concurrent]][]
-> 00007F84C80FBB40 System.Threading.ThreadLocal`1+LinkedSlot[[System.Collections.Concurrent.ConcurrentBag`1+WorkStealingQueue[[System.IDisposable, System.Private.CoreLib]], System.Collections.Concurrent]]
-> 00007F84C80FBB70 System.Collections.Concurrent.ConcurrentBag`1+WorkStealingQueue[[System.IDisposable, System.Private.CoreLib]]
-> 00007F84C80FBBB0 System.IDisposable[]
-> 00007F84C80FBA90 System.Diagnostics.DiagnosticListener+DiagnosticSubscription
-> 00007F84C80FAF30 Microsoft.ApplicationInsights.AspNetCore.DiagnosticListeners.HostingDiagnosticListener
-> 00007F84C80EB450 Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration
-> 00007F84C80D5688 Microsoft.ApplicationInsights.Extensibility.Implementation.ApplicationId.ApplicationInsightsApplicationIdProvider
-> 00007F84C80D5A60 Microsoft.ApplicationInsights.Extensibility.Implementation.ApplicationId.ProfileServiceWrapper
-> 00007F84C80D5A88 System.Net.Http.HttpClient
-> 00007F84C80D5AD0 System.Net.Http.HttpClientHandler
-> 00007F84C80D5B00 System.Net.Http.SocketsHttpHandler
-> 00007F84D80D1018 System.Net.Http.RedirectHandler
-> 00007F84D80D1000 System.Net.Http.HttpConnectionHandler
-> 00007F84D80D0D38 System.Net.Http.HttpConnectionPoolManager
-> 00007F84D80D0F70 System.Threading.Timer
-> 00007F84D80D0FE8 System.Threading.TimerHolder
-> 00007F84D80D0F88 System.Threading.TimerQueueTimer
-> 00007F84C80533A0 System.Threading.TimerQueue
-> 00007F84D910F3C0 System.Threading.TimerQueueTimer
-> 00007F84D910EE58 System.Threading.TimerQueueTimer
-> 00007F84D910A680 System.Threading.TimerQueueTimer
https://pastebin.com/V8CNQjR7
Do I have an Application Insights or Cosmos memory leak? Why are there so many TimerQueueTimer references?
await Task.Delay create new TimerQueueTimer on every call.
Lots of TimerQueueTimer is sign of someone is using await Task.Delay() in a loop, instead of using simple new Timer().
-> Microsoft.Azure.Cosmos.Routing.GlobalEndpointManager+<StartRefreshLocationTimer>d__25
-> Microsoft.Azure.Cosmos.Routing.GlobalEndpointManager
Looks like GlobalEndpointManager of Microsoft.Azure.Cosmos uses await Task.Delay every time exception is thrown in StartRefreshLocationTimer method of GlobalEndpointManager.cs class
You can try few things here:
1) Check which exception is thrown and how to avoid it.
My guess this should help log exception:
DefaultTrace.TraceSource.Listeners.Add(new System.Diagnostics.ConsoleTraceListener())
(check example)
2) make sure ShouldRefreshEndpoints returns false, if it's ok for your app :)

eunit: How to test a simple process?

I'm currently writing a test for a module that runs in a simple process started with spawn_link(?MODULE, init, [self()]).
In my eunit tests, I have a setup and teardown function defined and a set of test generators.
all_tests_test_() ->
{inorder, {
foreach,
fun setup/0,
fun teardown/1,
[
fun my_test/1
]}
}.
The setup fun creates the process-under-test:
setup() ->
{ok, Pid} = protocol:start_link(),
process_flag(trap_exit,true),
error_logger:info_msg("[~p] Setting up process ~p~n", [self(), Pid]),
Pid.
The test looks like this:
my_test(Pid) ->
[ fun() ->
error_logger:info_msg("[~p] Sending to ~p~n", [self(), Pid]),
Pid ! something,
receive
Msg -> ?assertMatch(expected_result, Msg)
after
500 -> ?assert(false)
end
end ].
Most of my modules are gen_server but for this I figured it'll be easier without all gen_server boilerplate code...
The output from the test looks like this:
=INFO REPORT==== 31-Mar-2014::21:20:12 ===
[<0.117.0>] Setting up process <0.122.0>
=INFO REPORT==== 31-Mar-2014::21:20:12 ===
[<0.124.0>] Sending to <0.122.0>
=INFO REPORT==== 31-Mar-2014::21:20:12 ===
[<0.122.0>] Sending expected_result to <0.117.0>
protocol_test: my_test...*failed*
in function protocol_test:'-my_test/1-fun-0-'/0 (test/protocol_test.erl, line 37)
**error:{assertion_failed,[{module,protocol_test},
{line,37},
{expression,"false"},
{expected,true},
{value,false}]}
From the Pids you can see that whatever process was running setup (117) was not the same that was running the test case (124). The process under test however is the same (122). This results in a failing test case because the receive never gets the message und runs into the timeout.
Is that the expected behaviour that a new process gets spawned by eunit to run the test case?
An generally, is there a better way to test a process or other asynchronous behaviour (like casts)? Or would you suggest to always use gen_server to have a synchronous interface?
Thanks!
[EDIT]
To clarify, how protocol knows about the process, this is the start_link/0 fun:
start_link() ->
Pid = spawn_link(?MODULE, init, [self()]),
{ok, Pid}.
The protocol ist tightly linked to the caller. If the either of them crashes I want the other one to die as well. I know I could use gen_server and supervisors and actually it did that in parts of the application, but for this module, I thought it was a bit over the top.
did you try:
all_tests_test_() ->
{inorder, {
foreach,
local,
fun setup/0,
fun teardown/1,
[
fun my_test/1
]}
}.
From the doc, it seems to be what you need.
simple solution
Just like in Pascal answer, adding the local flag to test description might solve some your problem, but it will probably cause you some additional problems in future, especially when you link yourself to created process.
testing processes
General practice in Erlang is that while process abstraction is crucial for writing (designing and thinking about) programs, it is not something that you would expose to user of your code (even if it is you). Instead expecting someone to send you message with proper data, you wrap it in function call
get_me_some_expected_result(Pid) ->
Pid ! something,
receive
Msg ->
Msg
after 500
timeouted
end
and then test this function rather than receiving something "by hand".
To distinguish real timeout from received timeouted atom, one can use some pattern matching, and let it fail in case of error
get_me_some_expected_result(Pid) ->
Pid ! something,
receive
Msg ->
{ok, Msg}
after 500
timeouted
end
in_my_test() ->
{ok, ValueToBeTested} = get_me_some_expected_result().
In addition, since your process could receive many different messages in meantime, you can make sure that you receive what you think you receive with little pattern-matching and local reference
get_me_some_expected_result(Pid) ->
Ref = make_ref(),
Pid ! {something, Ref},
receive
{Ref, Msg} ->
{ok, Msg}
after 500
timeouted
end
And now receive will ignore (leave for leter) all messages that will not have same Reg that you send to your process.
major concern
One thing that I do not really understand, is how does process you are testing know where to send back received message? Only logical solution would be getting pid of it's creator during initialization (call to self/0 inside protocol:start_link/0 function). But then our new process can communicate only with it's creator, which might not be something you expect, and which is not how tests are run.
So simplest solution would be sending "return address" with each call; which again could be done in our wrapping function.
get_me_some_expected_result(Pid) ->
Ref = make_ref(),
Pid ! {something, Ref, self()},
receive
{Ref, Msg} ->
{ok, Msg}
after 500
timeouted
end
Again, anyone who will use this get_me_some_expected_result/1 function will not have to worry about message passing, and testing such functions makes thing extremely easier.
Hope this helps at least a little.
Maybe it's simply because you are using the foreach EUnit fixture in place of the setup one.
There, try the setup fixture: the one that uses {setup, Setup, Cleanup, Tests} instead of {inorder, {foreach, …}}