Model-Oriented Software Architecture 3-resource management model Reading Notes (6)-pooling Model

Source: Internet
Author: User

3.2 pooling Mode

The pooling (pool) mode describes how to use resources that are no longer needed in a loop to avoid expensive operations to obtain and release resources. Once resources are recycled and placed in the pool, they lose their identity and status.

 

1. Problem

Many systems require fast and predictable access to resources. Such resources include network connections, object instances, threads, and memory. In addition to fast and predictable resource access, the system also requires that the solution be scalable to the number of resources used. In addition, each user request should experience few changes within the access time. Therefore, the acquisition time of resource A should not be significantly different from that of resource B (resource A and resource B are of the same type ). To solve the problem mentioned above, we need to solve the following points:

1) scalability ). Released resources should be reused to avoid repeated overhead.

2) performance ). Avoid the waste of CPU cycles caused by repeated acquisition and release of resources.

3) predictability ). The time the resource user obtains the resource should be predictable (even if the time for obtaining the resource directly from the resource provider may be unpredictable ).

4) simplicity ). The solution should be relatively simple to minimize applicationsProgramComplexity.

5) stability ). Repeated acquisition and release of resources may increase the risk of system instability. For example, repeated acquisition and release of memory may cause memory fragmentation for operating systems without complex memory management mechanisms. The solution should minimize system instability.

 

2. Solution

Manage multiple instances of a class of resources in the pool. This resource pool allows us to reuse resources that are no longer needed by resource users. Released resources are stored in the pool.

To increase efficiency, the resource pool obtains a fixed number of resources in advance after creation. If the demand for resources exceeds the number of resources in the pool, it will delay obtaining more resources. You can choose to release unused resources, such as the methods recorded in the Evictor mode or leasing mode.

When a resource is released and put back to the pool, the resource user or resource pool should lose its identity, depending on the policy used. Before being reused, the resource needs to be reinitialized. If a resource is an object, it may be useful to provide a separate initialization interface. Resource users or resource pools do not use resource identifiers (in many cases a pointer or a reference) To differentiate objects.

 

3. Structure

Resource users obtain and use resources.

A resource is an entity (such as memory or thread ).

Resource pools manage resources, respond to resource acquisition requests, and deliver resources to resource users.

Resource providers (such as operating systems) possess and manage resources.

 

 

4. Implementation

1) Select a resource. Find the resources that will benefit from the resource pool. Simplify resource management by categorizing resources into several different resource pools by type. Putting different types of resources into the same pool will complicate their management, because it is necessary to divide multiple sub-pools and perform specialized searches.

2) determines the maximum size of the resource pool. To avoid resource depletion, You need to define the maximum number of resources maintained by the resource pool. The maximum number of resources in the resource pool is equal to the number of resources obtained in advance plus the number of resources obtained on demand. The maximum number of resources is usually set at the resource pool initialization time, however, it can also be set based on configurable parameters (such as the current load of the system.

3) determine the number of resources obtained in advance. To minimize the resource acquisition time, we recommend that you obtain at least the average number of resources normally used in advance, which will reduce the resource acquisition during routine application execution. User Requirements and system analysis help you determine the average number of resources to be obtained. It is important to remember that too many pre-acquired resources will become a burden, because they will lead to additional resource competition and should be avoided.

4) define resource interfaces. Provides interfaces that must be implemented for all resources in the resource pool. This interface helps maintain a group of resources.

5) define the resource pool interface. Provides an interface for resource users to obtain and release resources.

6) Clear resources. There are many resources in the pool, which means there are a lot of unused resources, which wastes space and reduces performance. In order to minimize the decline in system performance, the size of the resource pool should be reduced to a reasonable size, which can be achieved by releasing some resources from the pool.

7) determine the meaning of Resource Recycling. Resource Recycling varies with resource types. For example, to reuse a thread resource cyclically, you need to understand its stack and initialize the memory.

8) determines the failure handling policy. Exception and/or error messages should be handled for any failed retrieval of recoverable resources or release of resources. If it is not possible to recover from failure, an exception should be thrown or an empty resource should be returned to the resource.

 

5. Conclusion

Advantages:

1) performance ). The pooling mode can improve application performance because it helps reduce the time spent releasing resources and re-obtaining resources. When a resource has been acquired by the resource pool, the resource user can quickly obtain the resource.

2) predictability ). If the resource has been obtained in advance, the resource user obtains the average target resource within the specified time. With an appropriate clearing policy, most get requests can be met through the resource pool. Because it is predictable to search for and release previously acquired resources, resource users are more predictable to obtain resources than to obtain resources from resource providers.

3) simplicity ). Resource users do not need to call additional memory management routines. Resource users can obtain and release resources transparently or directly from the resource pool.

4) stability and scalability ). If the demand exceeds the available resources, new resources will be created. Resource pools delay resource clearing based on the clearing policy because they need to be recycled. This reduces the cost of releasing and re-obtaining resources, thus increasing system stability and scalability.

5) share ). Use the pool as a mediator so that unused resources can be shared among resource users, which is also good for memory usage and reduces the overall memory space overhead of the application. The resource pool does not synchronize access to the resource. Therefore, if synchronization is required, the resource user must do it by himself.

 

Disadvantages:

1) overhead ). Managing Resources in the pool consumes additional CPU cycles. However, these CPU cycles are generally less than the CPU cycles required to release and obtain resources again.

2) complexity ). Resource users have to explicitly release resources back to the resource pool. Leasing and other modes can be used to solve this problem.

3) synchronization ). In the synchronization environment, requests to obtain resource pools must be synchronized to avoid competition that may damage the Associated State.

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.