Will Erlang Scheduler make process causing re-queue problem? - asynchronous
Background: I'm confuse with Erlang's scheduler for a long time until have a look at The Beam Book. I had do some research on async/non-blocking programming in some language(Elixir/Erlang, Scala/Java, Golang) which include Actor pattern, Future/Promise, coroutine mainly. Coroutine and Actor pattern is similarly in term of they can both think as lightweight process.
The Problem
I'm find a weakness of the async programming: It will cause a lightweight-process(or a task) re-queue to the end of the scheduler Ready Queue if it invoke any of the block operation. The block operation will not block OS-Thread but it occurred mainly because of invoke a async action such as 'aio_read'.
re-queue means the process will be put at the end of Scheduler even if the process is just scheduled.In server-side programming, it will make a client request delay with a relatively long time compare with it should process time.The Beam Book give a detail description:
A processs trying to do a receive on an empty mailbox or on a mailbox with no matching messages will yield and go into the waiting state.
When a message is delivered to an inbox the sending process will check whether the receiver is sleeping in the waiting state, and in that case it will wake the process, change its state to runable, and put it at the end of the appropriate ready queue.
The affect could be see in many benchmark test: More request, more response time for every request.
A good scheduler should make the response time nearly the real process time of the request if ignore OS-Thread Context Switch.
I haven't seem others discuss the aspect yet.
As a conclusion, there are two question:
1. I want make a confirm whether re-queue problem really exist in async-programming world.
2. Besides, Is Erlang really has the problem if it handle tens of thousands of the process, especially use many GenServer.call in a request-response-chain?
Your answers are here: https://hamidreza-s.github.io/erlang/scheduling/real-time/preemptive/migration/2016/02/09/erlang-scheduler-details.html
Reposting it here, allowing edits.
Erlang Scheduling
Erlang as a real-time platform for multitasking uses Preemptive Scheduling. The responsibility of an Erlang scheduler is selecting a Process and executing their code. It also does Garbage Collection and Memory Management. The factor of selecting a process for execution is based on their priority level which is configurable per process and in each priority level processes are scheduled in a round robin fashion. On the other hand the factor of preempting a process from execution is based on a certain number of Reductions since the last time it was selected for execution, regardless of its priority level. The reduction is a counter per process that is normally incremented by one for each function call. It is used for preempting processes and context switching them when the counter of a process reaches the maximum number of reductions. For example in Erlang/OTP R12B this maximum number was 2000 reductions.
The scheduling of tasks in Erlang has a long history. It has been changing over the time. These changes were affected by the changes in SMP (Symmetric Multi-Processing) feature of Erlang.
Scheduling Before R11B
Before R11B Erlang did not have SMP support, so just one scheduler was run in the main OS process’s thread and accordingly just one Run Queue existed. The scheduler picked runnable Erlang processes and IO tasks from the run queue and executed them.
Erlang VM
+--------------------------------------------------------+
| |
| +-----------------+ +-----------------+ |
| | | | | |
| | Scheduler +--------------> Task # 1 | |
| | | | | |
| +-----------------+ | Task # 2 | |
| | | |
| | Task # 3 | |
| | | |
| | Task # 4 | |
| | | |
| | Task # N | |
| | | |
| +-----------------+ |
| | | |
| | Run Queue | |
| | | |
| +-----------------+ |
| |
+--------------------------------------------------------+
This way there was no need to lock data structures but the written application couldn’t take advantage of parallelism.
Scheduling In R11B and R12B
SMP support was added to Erlang VM so it could have 1 to 1024 schedulers each was run in one OS process’s thread. However, in this version schedulers could pick runnable tasks from just one common run queue.
Erlang VM
+--------------------------------------------------------+
| |
| +-----------------+ +-----------------+ |
| | | | | |
| | Scheduler # 1 +--------------> Task # 1 | |
| | | +---------> | |
| +-----------------+ | +----> Task # 2 | |
| | | | | |
| +-----------------+ | | | Task # 3 | |
| | | | | | | |
| | Scheduler # 2 +----+ | | Task # 4 | |
| | | | | | |
| +-----------------+ | | Task # N | |
| | | | |
| +-----------------+ | +-----------------+ |
| | | | | | |
| | Scheduler # N +---------+ | Run Queue | |
| | | | | |
| +-----------------+ +-----------------+ |
| |
+--------------------------------------------------------+
Because of the resulting parallelism of this method, all shared data structures are protected with locks. For example the run queue itself is a shared data structure which must be protected. Although the lock can provide performance penalty, the performance improvements which was achieved in multi-core processors systems was interesting.
Some known bottlenecks in this version was as follows:
The common run queue becomes a bottleneck when the number of schedulers increases.
Increasing the involved lock of ETS tables which also affects Mnesia.
Increasing the lock conflicts when many processes are sending messages to the same process.
A process waiting to get a lock can block its scheduler.
However, separating run queues per scheduler was picked to solve these bottleneck issues in next versions.
Scheduling After R13B
In this version each scheduler has its own run queue. It decreases the number of lock conflicts in systems with many schedulers on many cores and also improves the overall performance.
Erlang VM
+--------------------------------------------------------+
| |
| +-----------------+-----------------+ |
| | | | |
| | Scheduler # 1 | Run Queue # 1 <--+ |
| | | | | |
| +-----------------+-----------------+ | |
| | |
| +-----------------+-----------------+ | |
| | | | | |
| | Scheduler # 2 | Run Queue # 2 <----> Migration |
| | | | | Logic |
| +-----------------+-----------------+ | |
| | |
| +-----------------+-----------------+ | |
| | | | | |
| | Scheduler # N | Run Queue # N <--+ |
| | | | |
| +-----------------+-----------------+ |
| |
+--------------------------------------------------------+
This way the locking conflicts when accessing the run queue is solved but introduces some new concerns:
How fair is the process of dividing tasks among run queues?
What if one scheduler gets overloaded with tasks while others are idle?
Based on what order a scheduler can steal tasks from an overloaded scheduler?
What if we started many schedulers but there all so few tasks to do?
These concerns lead the Erlang team to introduce a concept for making scheduling fair and efficient, the Migration Logic. It tries to control and balance run queues based on the statistics that collects from the system.
However we should not depend on the scheduling to remain exactly as it is today, because it is likely to be changed in future releases in order to get better.
As a conclusion, there are two question:
1. I want make a confirm whether re-queue problem really exist in async-programming world.
2. Besides, Is Erlang really has the problem if it handle tens of thousands of the process, especially use many GenServer.call in a
request-response-chain?
Depending which Erlang version are you talking about there are different tradeoffs. Since there are multiple queues in newer Erlang versions your problem does not exist in the form you are specifying it.
I have seen Erlang (and its VM) handle millions of Erlang processes just fine, also used an Erlang based system to handle 100.000+ clients with really tight SLA on the latency. Not sure about the details of your problem, but a reasonably written Erlang/Elixir service can handle the workload you specified unless there is something you left out.
Related
Getting around borrowing as mutable after immutable [duplicate]
There are several wrapper types in the Rust standard library: The cells in the std::cell module: Cell and RefCell The reference-counted wrappers, like Rc and Arc. The types in the std::sync module: Mutex or AtomicBool for example As I understand it, these are wrappers which provide further possibilities than a simple reference. While I understand some basics, I cannot see the whole picture. What do they do exactly? Do cells and reference-counted families provide orthogonal or similar features?
There are two essential concepts in Rust: Ownership, Mutability. The various pointer types (Box, Rc, Arc) are concerned with Ownership: they allow controlling whether there is a single or multiple owners for a single object. On the other hand, the various cells (Cell, RefCell, Mutex, RwLock, AtomicXXX) are concerned with Mutability. The founding rule of Rust's safety is Aliasing NAND Mutability. That is, an object can only be safely mutated if there is no outstanding reference to its interior. This rule is generally enforced at compile time by the borrow checker: if you have a &T, you cannot also have a &mut T to the same object in scope, if you have a &mut T, you cannot also have any reference to the same object in scope. However, sometimes, this is not flexible enough. Sometimes you DO need (or want) the ability to have multiple references to the same object and yet mutate it. Enter the cells. The idea of Cell and RefCell is to permit mutability in the presence of aliasing in a controlled manner: Cell prevents the formation of reference to its interior, avoiding dangling references, RefCell shifts the enforcement of Aliasing XOR Mutability from compile time to runtime. This functionality is sometimes described as providing interior mutability, that is where an object which otherwise looks immutable from the outside (&T) can actually be mutated. When this mutability extends across multiple threads, you will instead use Mutex, RwLock or AtomicXXX; they provide the same functionality: AtomicXXX are just Cell: no reference to the interior, just moving in/out, RwLock is just RefCell: can obtain references to the interior through guards, Mutex is a simplified version of RwLock which does not distinguish between a read-only guard and write guard; so conceptually similar to a RefCell with only a borrow_mut method. If you come from a C++ background: Box is unique_ptr, Arc is shared_ptr, Rc is a non thread-safe version of shared_ptr. And the cells provide a similar functionality as mutable, except with additional guarantees to avoid aliasing issues; think of Cell as std::atomic and RefCell as a non thread-safe version of std::shared_mutex (which throws instead of blocking if the lock is taken).
Thanks to Matthieu's good answer, here is a diagram to help people to find the wrapper they need: +-----------+ | Ownership | +--+--------+ +================+ | +-Static----->| T |(1) | | +================+ | | | | +================+ | +-----------+ | Local Val| Cell<T> |(1) +-Unique-->| Borrowing +--+-Dynamic---->|----------------| | +-----------+ | Ref| RefCell<T> |(1) | | +================+ | | | | +================+ | | Threaded | AtomicT |(2) | +-Dynamic---->|----------------| | | Mutex<T> |(1) | | RwLock<T> |(1) | +================+ | | | +================+ | +-No--------->| Rc<T> | | | +================+ | Locally +-----------+ | +-Shared-->| Mutable? +--+ +================+ | +-----------+ | Val| Rc<Cell<T>> | | +-Yes-------->|----------------| | Ref| Rc<RefCell<T>> | | +================+ | | | +================+ | +-No--------->| Arc<T> | | | +================+ | Shared +-----------+ | +-Between->| Mutable? +--+ +================+ Threads +-----------+ | | Arc<AtomicT> |(2) +-Yes-------->|----------------| | Arc<Mutex<T>> | | Arc<RwLock<T>> | +================+ In those cases, T can be replaced with Box<T> Use AtomicT when T is a bool or a number To know if you should use Mutex or RwLock, see this related question.
Robotframework + Process + Log timestamp
I would understand how to properly display logs of a Python process executed in Roboframework. Let's suppose that ${result1.stdout} is this one: *INFO:1536834347873* ciao ciao *INFO:1536834347883* ciao ciao2 Is it possibile to transform them in two robotframework logs with the right date? If I use the "Log keyword" they will have the keywork execution timestamp... Rr Note: I'm executing the python script with "start process". I need this because I need to run in parallel multiple scripts | | Start Process | python .${/}files${/}testPython.py | alias=First | shell=yes | stderr=/tmp/provaerr1.log | stdout=/tmp/prova1.log | | | ${result1}= | Wait For Process | First | | | Log | ${result1.stdout} |
RDO Openstack, Ceilometer, Heat autoscaling instance
Below is the code and instructions which I use to scale the instances on the RDO. Can anyone tell me why new instances are not created. What could be the reason and where I can see why it is not working ? The alarm is created but instances did not create. +--------------------------------------+------------------------------------+-------+----------+---------+------------+-------------------------------+------------------+ | Alarm ID | Name | State | Severity | Enabled | Continuous | Alarm condition | Time constraints | +--------------------------------------+------------------------------------+-------+----------+---------+------------+-------------------------------+------------------+ | 50d52c2f-fdaf-4767-818d-3cfd91c1848a | simple-cpu_alarm_high-26y5dsoucoub | alarm | low | True | True | cpu_util > 0.1 during 1 x 60s | None | +--------------------------------------+------------------------------------+-------+----------+---------+------------+-------------------------------+------------------+ I wait more then 10 min (default interval time) but the new instance is not created I did all steps from this side Simple auto scaling environment with Heat and I have Stack with two instances. I waited even 30 min and nothing. I load manually these instances CPU to 70 % but nothing change. Any Ideas ?
Large image upload from Horizon will be saving state forever
I am running openstack icehouse and trying to upload a 16 GB image from horizon. Horizon and Glance are running on seperate machines. The image is fully copied to Glance Node(checked by verifying image file size under /var/lib/glance/images/). But the image status continues to be in saving state forever, The status does not become active. The output of nova image-show displays. +----------------------+--------------------------------------+ | Property | Value | +----------------------+--------------------------------------+ | OS-EXT-IMG-SIZE:size | 16947544064 | | created | 2014-11-06T13:26:25Z | | id | 46f6218d-2f17-493a-8bc9-a46562cbefff | | minDisk | 0 | | minRam | 0 | | name | very_huge_image | | progress | 50 | | status | SAVING | | updated | 2014-11-06T13:26:25Z | +----------------------+--------------------------------------+ But when I try to upload same image with Glance CLI from horizon machine then in some time image status becomes active and ready to use. Any help/suggestion would be greatly appreciated. Thanks in advance.
Can ECB be restricted to "take over" only the current buffer when it's activated?
From the get go: sorry if I'm not using the proper emacs terminology -- I'm relatively wet behind the ears in the emacs world. Most of my work in emacs is for programming R, and I'm using ESS and ECB to do so quite happily. I'd like to build a custom ECB layout which uses the entire bottom of the screen as my R console, while putting some ECB-specific buffers on the left. Using ECB-esque layout diagrams, I'd like my layout to look like pretty much exactly like "left13", except I'd like the entirety of the "compilation" buffer to be my running R console (or any shell, for that matter): ------------------------------------------------------- | | | | | | | | | | | | | | | | | | | | | | Directories | Edit | | | | | | | | | | | | | | | | | | | | | | ------------------------------------------------------- | | | R Console | | | ------------------------------------------------------- If I can just split my buffer in two (vertically), then call ecb-activate from the top buffer (and not allow it to touch my bottom buffer), I'm imagining it could work (hence the subject of my question). That doesn't work, though, and I don't know how to get an entier "bottom pane" out of a layout to work in the way I like using trying to use ECB's customize layout functionality. Does anybody know if/how I can do this?
Short answer: No. Longer answer: Unfortunately, ECB completely takes over Emacs "window" management at a very low level. So it's all or nothing. You can't comfortably combine it with regular window splitting. What you might be able to do, is to adjust the layout ECB gives you or to program a custom layout. (Some assembly required.)