Scala notes and akka notes

Source: Internet
Author: User
Features that should be guaranteed by non-blocking programs

:

  • Wait for unrelated wait-free: ensure that each call is completed within a limited number of steps, regardless of the number of other call steps. Killing Several threads
  • Lock-free: Some threads that execute this program will be delayed, but make sure that at least one thread can continue until the execution is complete. This should prevent possible starvation, that is, to prevent a thread from being delayed forever.
  • Obstruction blocking unrelated obstruction-free: There is a time point after which the non-blocking method is executed independently within a limited number of steps
  • Programs unrelated to the lock must be independent of obstacles, and vice versa.
  • Optimistic parallel control is unrelated to obstacle blocking. when a conflict is detected, the system rolls back the modification, accepts a scheduling policy, and then tries again later.

If you have to perform blocking operations, such as thread sleep for a period of uncertain time, wait time, RDBMS driver, message API, and Io, it is very easy to put into future, but it may cause performance problems and OOM. Solution to the blocking problem:

Place blocking calls in one or more actor-managed router. Make sure to configure a thread pool

When putting blocking into the future, you must ensure that there is a maximum number of future calls; otherwise, oom is easy. You can also configure a thread pool with an upper limit.

Use a single thread to manage a group of blocked resources and issue events through actor messages.

Actorref

State serializability and Restoration

Behavior

Mailbox

The default value is the first-in-first-out queue, which can be a priority queue. However, in akka, the current behavior must always process the next outgoing message without scanning mailbox. When a message fails to be processed, that is to say, failure has happened. Of course, this behavior can be rewritten.

Is supervisor, can create many children, create and stop are asynchronous, return immediately, please remember!

Monitoring policy, that is, the failure Processing Mechanism

A superior entrusts a task to a lower-level for execution and supervision.

Root

/User/System

Supported by the custom system

One-to-one monitoring, oneforonestrategy

Multiple-to-one monitoring: allforonestrategy, the failure of an actor will affect the failure of the actor of the same group of peers, applicable

Mailbox will not be cleared during restart

Actor path and address Routing

Different types of actorreferences

A local actor ref is used only. Messages cannot be sent to a remote JVM across networks.

Local actor ref with protocol and remote address information

Router-type local actor ref, messages are sent directly to its children

Remote ref, serialized message transmission

Promiseactorref

Deadletteractorref

Emptylocalactorref

Unavailable but existing: Root monitoring, the one pseudo-Supervisor

Logging. standaroutlogger

Actor instantiation

Do not declare another actor in one actor

Do not pass this pointer of actor to props

Props is a configuration tool class used to configure the information required to create actor, such as parameters and class names.

Provide a props factory for each actor to create an actor.

We recommend that the actor hierarchy support failure processing.

The path and uid represent an actor. The path and uid will not change during restart.

Messgedispatchers is the engine of the akka System and the execution context of executioncontext.

Can be used to execute any code

Each actorsystem has a default configurable dispatcher.

Fork-join-executor thread-pool-executor

Four types of dispatcher to create actor or thread

Dispatcher: Default, event-driven, which binds a group of actors to a thread pool and shares threads,

Pinneddispatcher: one actor and one thread, which are not shared,

Balancingdispatcher: event-driven, which distributes tasks of heavy actor to idle actor. All actors share a mailbox, the premise is that all the actor instances can process messages in mailbox and cannot be used as a router dispatcher.

Callingthreaddispatcher: invocations are executed only in the current thread. Different threads can call the same actor without creating a new thread. It is suitable for testing.

Mailbox

Follow these steps to determine the mailbox type:

1. Take the mailbox item in the deployment configuration of actor as the standard. Otherwise

2. The props of the actor contains the configuration of mailbox, such as withmailbox. Otherwise

3. The mailbox-type in the dispatcher configuration of the actor prevails. Otherwise

4. Take the mailbox type contained by the actor as the standard, for example, with requiresmessagequeue [boundedmessagequeuesemantics]. Otherwise

5. Take the mailbox type contained by the dispatcher of the actor as the standard. Otherwise

6. The default value is akka. Actor. Default-Mailbox (infinite mailbox, java. util. Concurrent. concurrent1_queue, singleconsumeronlyunboundedmailbox is a more efficient mailbox, but it cannot be used for balancingdispatcher)

Mailboxtype [actorsystem. settings, config] can be used to customize the mailbox. akka's built-in mailbox types include akka. Dispatch .:

UnboundedMailbox-java.util.concurrent.ConcurrentLinkedQueue, non-blocking, Unlimited message, unbounded

Singleconsumeronlyunboundedmailbox-an efficient multi-producer single-consumer queue with non-blocking and unlimited messages

BoundedMailbox-java.util.concurrent.LinkedBlockingQueue, blocking, limited messages, bounded

UnboundedPriorityMailbox-java.util.concurrent.PriorityBlockingQueue, blocking, unlimited messages

Boundedprioritymailbox-java. util. Concurrent. priorityblockingqueue encapsulated by akka. util. boundedblockingqueue, blocking, limited message

Message routing routeees

Akka comes with the following routing rules: akka. routing.

Akka. Routing. roundrobinroutinglogic

Akka. Routing. randomroutinglogic
? Akka. Routing. smallestmailboxroutinglogic
? Akka. Routing. broadcastroutinglogic
? Akka. Routing. scattergatherfirstcompletedroutinglogic
? Akka. Routing. consistenthashingroutinglogic

Use Case in case class and object to generate a large number of template code

, The class using case is automatically generated as follows:

Apply method: you do not need to use new to create an object,

The get method is automatically created for the parameters (Val by default) of the constructor. If the parameter is declared as VAR, The get/set method is automatically generated.

Tostring, equals, hashcode, COPY method

Unapply: Used in a match expression.

The goal of case class is to create immutable records for better pattern recognition.

Chain call

Return Value: This. type can be chained. If it is a class that cannot be inherited, the method returns this directly.

Method method Function

DefMethodname (Arg: type, funcname (argn: type) => type ){

}

Input => output => ing Conversion

ValFuncname = (argn: type1): type2 => {.... Type2}

Method cannot be passed as a parameter, while function can. function is a variable of the function type.

A function is an object inherited from trait: function0, function1 ,...... Function22

Some functions

Functional Programming philosophy: Apply a function to a parameter

In Java, full function processing is performed on parameters.

In Scala, only some parameters can be input, and functions can be partially processed. Partial Functions

Assign values to one part of a multi-parameter function, and the other part is used as the parameter method of the new function.

It is not a one-to-one ing. It cannot process all inputs and filter out some inputs.

Partialfunction [inputargstype, returntype] = {...... }

Scala set

Explicit: predicate is a function or method that returns Boolean.

Anonymous function: () => {........ }

Implicit loop: Set Method filter foreach map reduceleft... Can implicitly perform a for loop on elements.

Set level

Top-level three-tier trait: starts with the iterable In The Middle Of The traversable, and then three sub-Worlds: seq set Map

A large collection system with these three branches and broad-leaf

It is best to use an immutable vector or variable arraybuffer for a sequence.

Map

Other classes that look like a set

Enumeration enumeration: limited set iterator: provides many traversal methods option: a valid tool for processing null tuple ancestor: a collection of Heterogeneous Data

When the number of elements in a set is large, lazy set must be used instead of strict set. The memory is not so large: view method of the set is used.

In scala: the end method is evaluated from right to left.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.