SlideShare uma empresa Scribd logo
1 de 116
АКТЁРЫ В JVM
Простая архитектура для
конкурентных отказоустойчивых
масштабируемых Java-приложений
Данные
Данные
Данные
Данные
Данные
Данные
Данные
Данные
Данные
Данные
Данные
Данные
Calculate



Get Lock



 Modify     Данные



Release
 Lock
Get Lock A



 Modify A



 IF (cond)   Get Lock B   A

              Modify B    B
 Release      Release
 Lock A       Lock B
Get Lock A



              Modify A
Get Lock A


             Get Lock B
 Modify A

              Modify B
 Release
 Lock A
              Release
              Lock B


              Release
              Lock A
2
    1                     Get Lock B
Get Lock A
                             2
   1                       Modify B
 Modify A

                              2
                          Get Lock A
      1          1
 IF (cond)   Get Lock B
                             2
                           Modify B
                1
              Modify B
                              2
                          Release A
    1            1
Release A    Release B
                              2
                          Release B
1            1            2
Get Lock A   Get Lock A   Get Lock B

   1            1            2
 Modify A     Modify A     Modify B

    1            1            2
Get Lock B   Get Lock B   Get Lock A

   1            1            2
 Modify B     Modify B     Modify B
                              2
    1            1        Release A
Release B    Release B
                              2
    2            1        Release B
Get Lock B   Release A        1
                          Get Lock A
   2             2
 Modify B    Get Lock B      1
                           Modify A
    1           2
Release A     Modify B        1
                          Get Lock B
    2            2
Get Lock A   Get Lock A      1
   2            2          Modify B
 Modify B     Modify B
                              1
    2            2        Release B
Release A    Release A
    2            2            1
Release B    Release B    Release A
1            1            2            2
Get Lock A   Get Lock A   Get Lock B   Get Lock B

   1            1            2            2
 Modify A     Modify A     Modify B     Modify B

    1            1            2            1
Get Lock B   Get Lock B   Get Lock A   Get Lock A

   1            1            2            1
 Modify B     Modify B     Modify B     Modify A
                              2
    1            1        Release A        1
Release B    Release B                 Get Lock B
                              2
    2            1        Release B        2
Get Lock B   Release A        1        Get Lock A
                          Get Lock A
   2             2
 Modify B    Get Lock B      1
                           Modify A
    1           2
Release A     Modify B        1
                          Get Lock B
    2            2
Get Lock A   Get Lock A      1
   2            2          Modify B
 Modify B     Modify B
                              1
    2            2        Release B
Release A    Release A
    2            2            1
Release B    Release B    Release A
От чего избавиться?

Что улучшить?
Взаимные блокировки


Последовательный доступ
Разделяемая память
Синхронные вызовы

Очередь событий
Актер =
состояние + поведение +
     почтовый ящик
A
A  AA   A A
A       AAAA
   A
  A A A AA A
  A    A A
   AA A
A AA A AA   A
A A AA A
 AA
        AA AA
 A AA A
A
A   Данные
A   Данные




A   A
A   Данные




A   A

A
A   A
A   A
A   A
        AA
A   A   A
A   A
         AA
A   A    A
    A
A    A
    A
A


A
A
Action   Action




Action



Action
A
Action
         F
             Action



Get!     F   Action




Action   F
A
Action
         F
             Action


Action
             Action


Action   F
Get!
         F
Данные
A4



     A8
A4


     A8
Call!    Action




         Action



Action   Return




Action
Action   Action




Action   Action



Return   Return
A
Данные
         Данные
          копия   A
                  Данные
                  копия 2
A
A   A       A
        A
                A




                    →
?
A
Start
A
          Login



 A         A
Start    Register



           A
          Email
        Dispatcher
A
Login
A
        Login

         A
        Login
 A
Login    A
        Login

         A
        Login
A
             Sender

              A
             Sender
    A
  Email       A
Dispatcher   Sender

              A
             Sender
A           A
        UserSession1   User1



 A
Login
A           A
        UserSession1   User1



 A          A
Login   UserSession2


                        A
                       User2
A           A
        UserSession1   User1



 A          A
Login   UserSession2


            A           A
        UserSession3   User2
A
    A          A
UserSession1
               A
A            A
UserSession1   Document1
A            A
UserSession1   Document1


                 A
               Document7
A            A
UserSession3   Document4


    A            A
UserSession1   Document1


    A            A
UserSession5   Document7
A           A              A
          Login      Session       User



 A         A
Start    Register
                               A
                           Document


           A
          Email
        Dispatcher
A                  A
Session              User




            A
          Document
A
Session1
A
Session1




   A
Document1
A
         Session1




              A
        Document1




A      A          A      A
User   User       User   User
A
         Document1




 A       A           A       A
User1   User2       User3   User4
A
                   Document1




         A         A          A        A
       User1     User2       User3   User4




  A        A        A         A       A        A
Session1 Session2 Session3 Session4 Session5 Session6
A        Данные

Document1
A        Данные

Document1
A   Данные
A   Данные
GET


      Данные
       Копия
                   Данные
                    Копия
A   Данные
UPDATE
Actor
A
        UserSession1   User1



 A          A
Login   UserSession2


            A           A
        UserSession3   User2
To:
Session1




     A         A
   Login   UserSession2


               A           A
           UserSession3   User2
To:
Session1




     A         A
   Login   UserSession2


               A           A
           UserSession3   User2
To:
Session1
               A           A
           UserSession1   User1



     A         A
   Login   UserSession2


               A           A
           UserSession3   User2
GET
      A            Данные




          Данные
           Копия   Данные
                    Копия
A           A
        UserSession1   User1



 A          A
Login   UserSession2


            A
        UserSession3   User2
A           A
        UserSession1   User1



 A
Login   UserSession2



        UserSession3   User2
A           A
        UserSession1   User1



 A
Login
A
A
A
A
A
A
F   A
F   A
F   A
F   A
F   A
    F   A
F
    F   A
    F   A
F   A
    F   A
F
    F   A
    F   A
F   A
    F   A
F
    F   A
    F   A
akka.io
import akka.actor.Actor
import akka.event.EventHandler

public class HelloActor extends Actor {

    def receive = {
         case "hello" => HelloService().hi
         case s: String => HelloService().sayHello(s)
         case _ => EventHandler.info(this, "unknown message")
    }
}
//Start
val actorRef = Actor.actorOf[MyActor]
actorRef.start()

//Send message
actorRef ! "hello"
actorRef ! new Date()

//Obtain actors
val actors = Actor.registry.actorsFor(classOf[HelloActor])
//Ask
val response = actorRef ? "hello"


//Get result
val result = response.get




Actor.become( newRecieveFunction )
import akka.actor.UntypedActor;
import akka.event.EventHandler;

public class HelloActor extends UntypedActor {

    public void onReceive(Object message) {

        if (message instanceof String) {
            HelloService.getInstance().sayHello(message);
        }

    }

}
//Start
ActorRef ref = Actors.actorOf(HelloActor.class);
ref.start();

//Send message
ref.tell("hello");
ref.tell(new Date());

//Obtain actors
ActorRef[] actors =
               Actors.registry().actorsFor(HelloActor.class);
//Ask
Future response = actorRef.ask("hello");
…
//Get result
String result = response.get();

//Hello, synchronous call! Long time no see.
String syncResult = actorRef.ask("hello").get();
• Actors
• Typed Actors
• Supervisors
• Routing
• Futures
• STM
• FSM
• TestKit
class SomeSpec extends … TestKit {
    "A Worker" must {
       "send timely replies" in {
          val worker = actorOf(...)
          within (500 millis) {
             worker ! "some work"
          }
       }
    }
}
Виктор Тесленко
victor@teslenko.com.ua
twitter: @vir

Mais conteúdo relacionado

Destaque

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destaque (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Actors in Java

  • 1. АКТЁРЫ В JVM Простая архитектура для конкурентных отказоустойчивых масштабируемых Java-приложений
  • 11.
  • 15. Calculate Get Lock Modify Данные Release Lock
  • 16. Get Lock A Modify A IF (cond) Get Lock B A Modify B B Release Release Lock A Lock B
  • 17. Get Lock A Modify A Get Lock A Get Lock B Modify A Modify B Release Lock A Release Lock B Release Lock A
  • 18. 2 1 Get Lock B Get Lock A 2 1 Modify B Modify A 2 Get Lock A 1 1 IF (cond) Get Lock B 2 Modify B 1 Modify B 2 Release A 1 1 Release A Release B 2 Release B
  • 19. 1 1 2 Get Lock A Get Lock A Get Lock B 1 1 2 Modify A Modify A Modify B 1 1 2 Get Lock B Get Lock B Get Lock A 1 1 2 Modify B Modify B Modify B 2 1 1 Release A Release B Release B 2 2 1 Release B Get Lock B Release A 1 Get Lock A 2 2 Modify B Get Lock B 1 Modify A 1 2 Release A Modify B 1 Get Lock B 2 2 Get Lock A Get Lock A 1 2 2 Modify B Modify B Modify B 1 2 2 Release B Release A Release A 2 2 1 Release B Release B Release A
  • 20. 1 1 2 2 Get Lock A Get Lock A Get Lock B Get Lock B 1 1 2 2 Modify A Modify A Modify B Modify B 1 1 2 1 Get Lock B Get Lock B Get Lock A Get Lock A 1 1 2 1 Modify B Modify B Modify B Modify A 2 1 1 Release A 1 Release B Release B Get Lock B 2 2 1 Release B 2 Get Lock B Release A 1 Get Lock A Get Lock A 2 2 Modify B Get Lock B 1 Modify A 1 2 Release A Modify B 1 Get Lock B 2 2 Get Lock A Get Lock A 1 2 2 Modify B Modify B Modify B 1 2 2 Release B Release A Release A 2 2 1 Release B Release B Release A
  • 24. Актер = состояние + поведение + почтовый ящик
  • 25. A
  • 26. A AA A A A AAAA A A A A AA A A A A AA A A AA A AA A A A AA A AA AA AA A AA A
  • 27. A
  • 28. A Данные
  • 29. A Данные A A
  • 30. A Данные A A A
  • 31. A A
  • 32. A A
  • 33. A A AA A A A
  • 34. A A AA A A A A A A A
  • 35.
  • 36. A A
  • 37. A Action Action Action Action
  • 38. A Action F Action Get! F Action Action F
  • 39. A Action F Action Action Action Action F Get! F
  • 40.
  • 42.
  • 43. A4 A8
  • 44. A4 A8
  • 45. Call! Action Action Action Return Action
  • 46. Action Action Action Action Return Return
  • 47.
  • 48. A Данные Данные копия A Данные копия 2
  • 49. A A A A A A →
  • 50. ?
  • 51.
  • 53. A Login A A Start Register A Email Dispatcher
  • 55. A Login A Login A Login A Login A Login
  • 56. A Sender A Sender A Email A Dispatcher Sender A Sender
  • 57. A A UserSession1 User1 A Login
  • 58. A A UserSession1 User1 A A Login UserSession2 A User2
  • 59. A A UserSession1 User1 A A Login UserSession2 A A UserSession3 User2
  • 60. A A A UserSession1 A
  • 61. A A UserSession1 Document1
  • 62. A A UserSession1 Document1 A Document7
  • 63. A A UserSession3 Document4 A A UserSession1 Document1 A A UserSession5 Document7
  • 64. A A A Login Session User A A Start Register A Document A Email Dispatcher
  • 65.
  • 66. A A Session User A Document
  • 68. A Session1 A Document1
  • 69. A Session1 A Document1 A A A A User User User User
  • 70. A Document1 A A A A User1 User2 User3 User4
  • 71. A Document1 A A A A User1 User2 User3 User4 A A A A A A Session1 Session2 Session3 Session4 Session5 Session6
  • 72. A Данные Document1
  • 73. A Данные Document1
  • 74. A Данные
  • 75. A Данные GET Данные Копия Данные Копия
  • 76. A Данные UPDATE
  • 77. Actor
  • 78. A UserSession1 User1 A A Login UserSession2 A A UserSession3 User2
  • 79. To: Session1 A A Login UserSession2 A A UserSession3 User2
  • 80. To: Session1 A A Login UserSession2 A A UserSession3 User2
  • 81. To: Session1 A A UserSession1 User1 A A Login UserSession2 A A UserSession3 User2
  • 82. GET A Данные Данные Копия Данные Копия
  • 83. A A UserSession1 User1 A A Login UserSession2 A UserSession3 User2
  • 84. A A UserSession1 User1 A Login UserSession2 UserSession3 User2
  • 85. A A UserSession1 User1 A Login
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92. A
  • 93. A
  • 94.
  • 95.
  • 97. F A F A F A F A
  • 98. F A F A F F A F A
  • 99. F A F A F F A F A
  • 100. F A F A F F A F A
  • 101.
  • 102.
  • 104.
  • 105.
  • 106. import akka.actor.Actor import akka.event.EventHandler public class HelloActor extends Actor { def receive = { case "hello" => HelloService().hi case s: String => HelloService().sayHello(s) case _ => EventHandler.info(this, "unknown message") } }
  • 107. //Start val actorRef = Actor.actorOf[MyActor] actorRef.start() //Send message actorRef ! "hello" actorRef ! new Date() //Obtain actors val actors = Actor.registry.actorsFor(classOf[HelloActor])
  • 108. //Ask val response = actorRef ? "hello" //Get result val result = response.get Actor.become( newRecieveFunction )
  • 109.
  • 110. import akka.actor.UntypedActor; import akka.event.EventHandler; public class HelloActor extends UntypedActor { public void onReceive(Object message) { if (message instanceof String) { HelloService.getInstance().sayHello(message); } } }
  • 111. //Start ActorRef ref = Actors.actorOf(HelloActor.class); ref.start(); //Send message ref.tell("hello"); ref.tell(new Date()); //Obtain actors ActorRef[] actors = Actors.registry().actorsFor(HelloActor.class);
  • 112. //Ask Future response = actorRef.ask("hello"); … //Get result String result = response.get(); //Hello, synchronous call! Long time no see. String syncResult = actorRef.ask("hello").get();
  • 113. • Actors • Typed Actors • Supervisors • Routing • Futures • STM • FSM • TestKit
  • 114. class SomeSpec extends … TestKit { "A Worker" must { "send timely replies" in { val worker = actorOf(...) within (500 millis) { worker ! "some work" } } } }
  • 115.