Concept, design and implementation of hot replacement in Java classes (4)

Source: Internet
Author: User

    Online system upgrade Design Principles

    In the above section, we provide an example of hot replacement of Java classes. After mastering this technology, we have the foundation for online system upgrade. However, for a real product system, upgrading this province is a very complicated project. If you want to upgrade it online, it will be more complicated. Among them, hot replacement of implementation classes is only the last step. The online upgrade requirements will have a profound impact on the overall design of the system. Next we will talk about some principles of online upgrade System Design:

    ◆ At the beginning of system design, we should consider which parts of the system need to be upgraded online and which parts are stable.

    Although we can design a system as any part of it can be upgraded online, the cost is very high and there is no need for it. Therefore, it is wise to clearly define the part of the system that needs to be upgraded online in the future. These parts are often system business logic rules, algorithms, and so on.

    ◆ Design a standardized and consistent system status conversion method

    Replacing a class is just onlineTo make the system run properly after the upgrade, you must maintain the consistency of the system status before and after the upgrade. Therefore, during the design, consider the system states involved in the online upgrade part, design these States to facilitate acquisition, setup, and conversion, and use a consistent approach.

    ◆ Clarify the system upgrade Control Protocol

    This principle is about the timing and Process Control of online system upgrades. It is a very dangerous activity to upgrade the system without considering its current running status. Therefore, in system design, we must consider and reserve control points for online system upgrades, and define clear and clear upgrade protocols to coordinate and control the upgrade sequence of multiple upgrade entities, to ensure that the system is in a definite State at any time of upgrade.

    ◆ Considering the rollback mechanism when the upgrade fails

    Even if we have made a very meticulous design, it is difficult to fundamentally ensure that the system upgrade is successful, especially for large-scale distributed systems. Therefore, when designing the system, consider the rollback mechanism after the upgrade fails.

    Online system upgrading

    First, let's briefly introduce the structure of the instance and the work to be done. In our example, there are three major entities, one being the upgrading control entity, and the other being the working entity. Both are implemented based on activeobject and communicate through command messages (for details about activeobject, see the author's other article "Building a Java concurrency model framework ").

    The upgrade control entity provides an external management command interface in the form of RMI to receive external online upgrade commands. A working entity has two message queues, one for receiving tasks assigned to it (we use a timer to regularly send Task Command messages to it). We call it a task queue; the other is used to interact with the upgrade control entity to complete the upgrade process. We call it the control queue. The task in the working entity is very simple. It simply prints a string using the foo class we introduced earlier, but this time the string is saved as the State in the working entity, dynamically set to the foo class instance. The upgrade Protocol process is as follows:

    When the update control entity receives an online update command from RMI, it sends a preparation upgrade message to the task queue of the two working entities and waits for a response. When the working entity receives the update preparation message in the task queue, it immediately sends a ready message to the update control entity and switches to the control queue for further upgrade instructions. After the update control entity receives the ready message from the two working entities, it sends a start update message to each control queue of the two working entities and waits for the result. After receiving the upgrade message, the working entity performs the actual upgrade, that is, the hot replacement class we described earlier. Then, send the update completion message to the update control entity. After receiving an update message from two working entities, the update control entity sends a continue message to each control queue of the two working entities. After receiving the continue message, the working entity, switch to the task queue to continue working. the upgrade process is complete. The main code snippets are as follows (the definition and execution details of command messages are omitted ):

 
  1. // Upgrade control entity key code
  2. Class upgradecontroller extends activeobject {
  3. Int nready = 0;
  4. Int nfinished = 0;
  5. Worker [] Workers;
  6. ......
  7. // This method is called when an external upgrade command message is received.
  8. Public void askforupgrade (){
  9. For (INT I = 0; I <workers. length; I ++)
  10. Workers [I]. gettaskqueue (). enqueue (New prepareupgradecmd (Workers [I]);
  11. }
  12.  
  13. // This method is called when a ready command message is received from the working entity.
  14. Public void readyforupgrade (string worker_name ){
  15. Nready ++;
  16. If (nready = workers. Length ){
  17. For (INT I = 0; I <workers. length; I ++)
  18. Workers [I]. getcontrolqueue (). enqueue (New
  19. Startupgradecmd (Workers [I]);
  20. }
  21. }
  22.  
  23. // This method is called when you receive the upgrade Command Message from the working entity.
  24. Public void finishupgrade (string worker_name ){
  25. Nfinished ++;
  26. If (nfinished = workers. Length ){
  27. For (INT I = 0; I <workers. length; I ++)
  28. Workers [I]. getcontrolqueue (). enqueue (New
  29. Contineworkcmd (Workers [I]);
  30.  
  31. }
  32. }
  33. ......
  34.  
  35. }
  36.  
  37. // Key code of the working entity
  38. Class worker extends activeobject {
  39. Upgradecontroller UGC;
  40. Hotswapcl hscl;
  41. Ifoo Foo;
  42. String state = "Hello world! ";
  43. ......
  44. // This method is called when you receive the update preparation Command Message of the update control entity.
  45. Public void prepareupgrade (){
  46. Switchtocontrolqueue ();
  47. UGC. getmsgqueue (). enqueue (New readyforupdatecmd (UGC, this ));
  48. }
  49.  
  50. // This method is called when you receive the start update command message of the update control entity.
  51. Public void startupgrade (string worker_name ){
  52. Doupgrade ();
  53. UGC. getmsgqueue (). enqueue (New finishupgradecmd (UGC, this ));
  54. }
  55.  
  56. // This method is called when you receive the resume command message of the update control entity.
  57. Public void continuework (string worker_name ){
  58. Switchtotaskqueue ();
  59. }
  60.  
  61. // When receiving the scheduled command message, this method is called.
  62. Public void dowork (){
  63. Foo. sayhello ();
  64. }
  65.  
  66. // Actual upgrade action
  67. Private void doupgrade (){
  68. Hscl = new howswapcl (".../swap", new string [] {"foo "});
  69. Class CLS = hscl. loadclass ("foo ");
  70. Foo = (ifoo) CLS. newinstance ();
  71. Foo. setstate (State );
  72. }
  73. }
  74.  
  75. // Ifoo Interface Definition
  76. Interface ifoo {
  77. Void setstate (string );
  78. Void sayhello ();
  79. }

In the implementation of the first version of the foo class, the set strings are printed directly. In the second version, the set string is first converted to uppercase and then printed. The example is simple. It aims to express upgrade changes in rules or algorithms. In addition, we did not mention exceptions such as message timeout and upgrade failure, which must be considered in actual product development.

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.