Distributed Computing Made Easy

附件
DistCompute.gif(8.2 K)
附件
figure1.jpg(16.4 K)
附件
figure2.jpg(9.2 K)
附件
figure3.jpg(26.5 K)
附件
figure4.jpg(17.0 K)
 
切换到幻灯片模式

Introduction



In case you haven't noticed, distributed computing is hard.



The problem is that it is becoming increasingly important in the world of enterprise application development. Today, developers continuously need to address questions like: How do you
enhance scalability by scaling the application beyond a single node? How can you guarantee high-availability, eliminate single points of failure, and make sure that you meet your customer
SLAs?
All questions that, in one way or the other, imply distributed computing.



For many developers, the most natural way of tackling the problem would be to divide up the architecture into groups of components or services that are distributed among different
servers. While this is not surprising, considering the heritage of CORBA, EJB, COM and RMI that most developers carry around, if you decide to go down this path then you are in
for a lot of trouble. Most of the time it is not worth the effort and will give you more problems than it solves.



For example, Martin Fowler thinks that a design like this


"...sucks like an inverted hurricane" and continues with the following discussion (from his book


Patterns of Enterprise Application Architecture):


“Hence, we get to my First Law of Distributed Object Design: Don't distribute your objects.


How, then, do you effectively use multiple processors? In most cases the way to go is clustering. Put all the classes into a single process, and then run multiple copies of that
process on various nodes. That way, each process uses local calls to get the job done and thus does things faster. You can also use fine-grained interfaces for all the classes within the
process and thus get better maintainability with a simpler programming model.”


The main benefit of using clustering is a simplified programming model. The way I see it, clustering, and distribution in general, is something that should be transparent to the
application developer. It is clearly a cross-cutting concern that should be orthogonal to and layered upon the
application, a service that belongs in the runtime. In other words, what we ultimately need is clustering at the JVM level.



Sample problem



In this article I will walk you through a fairly generic, but common, distributed computing problem, and show how it can be simplified - to become almost trivial - using clustering at
the JVM level.



First, let's define the problem. We need some sort of system that:




  • Distributes out and executes a set of tasks on N number of nodes

  • Can collect the result

  • Load-balances itself

  • Scales well



To simplify the implementation, we will only have to support tasks that are so-called


embarrassingly parallel, which means they have no shared state, but can be executed in complete
isolation. Luckily, a majority of applications actually fit into this category.



One of the most well-known and common patterns that solve our problem is the so-called Master/Worker pattern. So, let's take a look at how it works.



The Master/Worker pattern



The Master/Worker pattern consists of two logical entities: a Master, and one or more instances of a Worker. The Master initiates the computation
by creating a set of tasks, puts them in some shared space and then waits for the tasks to be picked up and completed by the Workers.



The shared space is usually some sort of Shared Queue, but it can also be implemented as a


Tuple Space (for example in Linda programming environments where the pattern is used
extensively).



One of the advantages of using this pattern is that the algorithm automatically balances the load. This is possible due to the simple fact that, the work set is shared, and the
workers continue to pull work from the set until there is no more work to be done.



The algorithm usually has good scalability as long as the number of tasks, by far exceeds the number of workers and if the tasks take a fairly similar amount of time to complete.





Thread-based single node implementation



We start by implementing the solution as a regular single node multi-threaded application, based on the Master/Worker pattern explained in the previous section.



The ExecutorService interface in the java.util.concurrent package (since


Java 5) provides direct support for the Master/Worker pattern, and this is something that we will take advantage
of. We are also going to use the


Spring Framework's
dependency injection (DI) capabilities to wire up and configure the system.



We have two entities: the Master , which coordinates the scheduling of the Work and the collection of the result, and the Shared Queue , which
represents the shared space where the pending Work resides. These entities are defined as two different Spring beans named master and queue that are wired up
and configured in the Spring bean config file. There is no need to define a Worker bean since the worker is "hidden" and managed under the hood by the
ExecutorService.



The figure above illustrates that conceptually, the ExecutorService consists of a Master that holds a reference to a Shared Queue (in our case a
BlockingQueue) as well as N number of Workers, where each Worker has a reference to the same single Shared Queue.



Master

The master bean implements the ExecutorService interface. This interface provides methods that can produce a


Future, or a list of Futures, for tracking progress of one or more
asynchronous tasks, e.g. to schedule Work and wait for Work to be completed. The master bean is implemented using the


proxy pattern and simply delegates to a ThreadPoolExecutor instance, which is a concrete implementation of the
ExecutorService interface that uses a thread pool to manage the Worker threads. Delegating in this way allows for simpler configuration such as default values in the
Spring bean config.



Here is how we could implement the master bean:


 
public class Master implements ExecutorService {

private final ExecutorService m_executor;

public Master(BlockingQueue workQueue) {
m_executor = new ThreadPoolExecutor(
10, 100, 300L, TimeUnit.SECONDS, workQueue);
}

public Master(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
BlockingQueue workQueue) {
m_executor = new ThreadPoolExecutor(
corePoolSize, maximumPoolSize,
keepAliveTime, TimeUnit.SECONDS, workQueue);
}

public List invokeAll(Collection work) throws InterruptedException {
return m_executor.invokeAll(work);
}

... // remaining methods are omitted

}


Shared Queue

Upon creation, the master bean is handed a reference to the shared queue bean, which is an instance of one of the classes that implements the
java.util.concurrent.BlockingQueue interface.



The queue bean holds all the pending Work. We need to have a single instance of this queue that can be available to all workers, and we therefore define it as a
Singleton in the bean config XML file.



It is called a blocking queue because it will block and wait for more Work to be added to the queue if the queue is empty. Additionally, an optional capacity limit can be
set and will, if set, prevent excessive queue expansion, if the limit has been reached then the queue will block until at least one element has been removed.



Assembly

These two beans can now be wired up by the Spring bean config file:


 
<beans>

<bean id="master" class="demo.masterworker.Master">
<constructor-arg ref="queue"/>
</bean>

<bean id="queue" class="java.util.concurrent.LinkedBlockingQueue"/>

</beans>



Usage

Using the Master/Worker implementation is now simply a matter of getting the bean from the application context and invoking the invokeAll(..), or one of the other similar
methods, in order to schedule work:


 
ApplicationContext ctx =
new ClassPathXmlApplicationContext("*/master-worker.xml");

// get the master from the application context
ExecutorService master = (ExecutorService) ctx.getBean("master");

// create a collection with some work
Collection<Callable> work = new ArrayList<Callable>();
for (int i = 0; i < 100; i++) {
work.add(new Callable() {
public Object call() {
... // perform work - code omitted
}
});
}

// schedule the work and wait until all work is done
List<FutureTask> result = master.invokeAll(work);



Discussion

This was a good exercise and the implementation is useful as it is, but this article is about distributed computing so let's now take a look at how we can turn this multi-threaded,
single JVM implementation into a distributed multi-JVM implementation.



Enter Terracotta for Spring.



Introduction to Terracotta for Spring



Terracotta for Spring is a runtime for Spring-based applications that provides transparent and high
performance clustering for your Spring applications with zero changes to the application code.



With Terracotta for Spring, developers can create single-node Spring applications as usual. They simply have to define which Spring application contexts they want to
have clustered in the configuration file. Terracotta for Spring handles the rest. Spring applications are clustered automatically and transparently and are guaranteed to have
the same semantics across the cluster as on the single node.



The main features that we will make use of in our sample application are:



  • Drops in and out transparently
    No changes to existing code necessary, does not even require the source code. The application is transparently instrumented at load
    time, based on a minimal declarative XML configuration. Terracotta for Spring does not require any classes to implement Serializable, Externalizable or any other interface.
    This is possible since we do not use serialization, but are only shipping the actual deltas over the wire.


  • Natural clustering of Spring beans
    Life-cycle semantics and scope for Spring beans are preserved across the cluster - within the same logical
    ApplicationContext. The current clusterable bean types are Singleton and Session Scoped beans. The user can declaratively configure which beans in which Spring
    application contexts to cluster.


  • Object identity is preserved
    Java's "pass-by-reference" semantics are maintained across the cluster, so regular object references work. This means that you
    can use your domain model the way you have designed it, as well as traditional OO design patterns etc. without a need to think about distribution, caching or clustering.


  • Distributed coordination
    The Java Memory Model is transparently maintained throughout the cluster,
    including distributed coordination, for example wait(), notify(), synchronized() {...} etc.


  • Memory management
    It also provides distributed garbage collection and functions also as a virtual heap. For example, run an application with 200 G heap on a
    machine with 4 G of RAM in which memory is paged in and out on a demand basis.



Let's go multi-JVM



So far, we have only implemented a regular, single node, multi-threaded, implementation of the Master/Worker pattern (that can be used as a single node implementation the way it
is). But the interesting thing is that in order to turn this implementation from a multi-threaded application into a distributed, multi-JVM application, we do not need to write any code at
all. All we need to do is to drop in Terracotta for Spring along with its XML configuration file, in which we simply define which Spring beans we want to cluster. In our case
this means the queue bean, since this queue needs to be available in the whole cluster, e.g. distributed. This is something that we accomplish by simply configuring the bean as
Singleton in the bean config XML file as well as listing it among the shared beans in the Terracotta for Spring configuration file.



Here is an example of a configuration file that would make the single node implementation distributed. The important parts are highlighted in bold. First, we have the path(s) to the
Spring bean config file(s) that are used to configure the application context we want to share. Second, we have the names of the beans to cluster and the name has to be defined in one
of the bean config files.


 
<?xml version="1.0" encoding="UTF-8"?>
<tc:tc-config xmlns:tc="http://www.terracottatech.com/config-v1">
<application>
<dso>
<spring>
<application name="*">
<application-contexts>
<application-context>
<paths>
<path>*/master-worker.xml</path>
</paths>
<beans>
<bean name="queue"/>
</beans>
</application-context>
</application-contexts>
</application>
</spring>
</dso>
</application>
</tc:tc-config>




Now we have turned our single-node, multi-threaded application into a distributed, multi-node application. What this means in practice is that we are now able to run the original code
that was written for a single JVM - without any thought about distribution or clustering - in a distributed environment, with the exact same semantics as on a single node. We have also
seen that we can transparently cluster, not only user-defined classes, but the core Java class libraries, including its concurrency abstractions.



Under the hood



Terracotta uses aspect-oriented technologies to adapt the application at class load time. In this
phase it extends the application in order to ensure that the semantics of Java are correctly maintained across the cluster, including object references, thread coordination,
garbage collection etc.



For example, it (as mentioned above) maintains the semantics of regular synchronized blocks across the cluster by taking a cluster-wide lock for the object instance that you are
synchronizing on right before entering the block and releasing it right after exiting the block. You can declaratively define the exact semantics for the lock (read, write or concurrent).
Another example is the invocation of notifyAll(), which is turned into a cluster wide notification allowing for all nodes to contend for the lock.



This is what is happening under the hood in our sample application when Terracotta is coordinating the access to the BlockingQueue and FutureTasks (throughout the
cluster). Terracotta supports distributed use of any other thread coordination abstraction, such as Barriers, Semaphores, Mutexes, Guards etc., as well as any custom written
abstraction. The only requirement is that it has to use Java's synchronization primitives internally (an article on how to implement distributed barriers using Terracotta
can be found


here).



I also mentioned before that Terracotta does not use serialization. This means that any regular Plain Old Java Object (POJO) can be shared as well as referenced from a
shared instance
(part of the shared object graph). What this also means is that Terracotta is not sending the whole object graph to all nodes but breaks down the graph into pure data and is only
sending the actual "delta" over the wire, meaning the actual changes, the data that is "stale" on the other node(s). Since it has a central server (see below) that keeps track of who is
referencing who on each node, it can also work in a lazy manner and only send the changes to the node(s) that references objects that are "dirty" and need the changes.



The architecture is hub and spoke based, meaning there is a central server which is managing the clients, it uses TCP/IP so the server just needs to be somewhere on the
network. The client in this case is simply your regular application together with the Terracotta libraries. The server is not a single-point of failure, but has a SAN-based
solution to support fail-over in an active-passive manner. This means that you can have an arbitrary number of (passive) servers waiting in line and upon failure the selected one will pick
up right where the master server that has crashed left off.



Discussion



As you have seen, with the use of Terracotta for Spring it is possible to turn a regular single-node multi-threaded implementation into a distributed, multi-JVM implementation
without any code changes and while maintaining the exact same semantics. This is extremely powerful and opens up for a completely new way of implementing distributed applications (see
Future work below). The main points in this exercise have been to see that Terracotta for Spring takes care of:



  • Transparent sharing of the state for the application across multiple distributed nodes

  • Coordination of resources is maintained across multiple distributed nodes

  • Pass-by-reference semantics is maintained across multiple distributed nodes

  • Declarative configuration with zero changes to existing code



Future work



I believe that this way of developing distributed applications, with sharing of state, resource coordination and distributed memory management done at the JVM level, can simplify how
we implement applications immensely, since we can focus on the logic and concepts and do not need to worry about the distribution mechanisms and problems.



It would for example be an interesting exercise to implement a


Blackboard System, something that is generally very hard, due to all the potential problems related to distributed
computing that needs to be addressed. But using Terracotta, the implementation could be simplified to a single-node multi-threaded application, e.g. one could work at a higher level,
focusing on getting the concepts and algorithms right, while other cross-cutting concerns like distributed sharing of state, distributed coordination, distributed memory management etc.
are layered upon the application afterwards. The same holds for


Tuple Space implementations, such as


JavaSpaces etc.



Resources



Terracotta for Spring is Free Software for production use. You can find more info here:




Acknowledgment



Thanks to Eugene Kuleshov and Chris Richardson for valuable feedback.



About the Author


Jonas Bonér is Sr. Engineer at Terracotta Inc. with a focus on strategy, product development and architecture, and technical evangelism. Prior to Terracotta, Jonas was a senior software
engineer for the JRockit team at BEA Systems, where he was working on runtime tools, JVM support for AOP and technology evangelism. He is the founder of the AspectWerkz AOP framework and
committer to the Eclipse AspectJ 5 project.



平均得分
(0 次评分)





文章来自: tss
标签: Grid Distributed Computing 
评论: 24 | 查看次数: 703
  • 共有 24 条评论
  • 1
  • 2
  • |
  • >>
游客 [2008-08-25 22:33:39]
游客 [2008-08-19 15:16:01]
游客 [2008-08-19 15:13:22]
游客 [2008-08-19 15:12:45]
sijialgc [2008-08-12 18:42:15]
奥运会开始了,大家为奥运加油吧!
上海中铁规模不断发展壮大,是上海市规模较大的物流公司货运公司之一.提供专业的上海物流上海货运项目.在原服务项目上加开了上海货运公司上海物流公司,更好的为大家服务.
专业的大众搬场/大众搬场公司/上海大众搬场-上海大众搬场物流有限公司竭诚为您服务.欢迎来电咨询.详情请进入:http://www.dz-bc.com.cn/专业的搬家公司/上海搬家公司/上海搬家/搬场公司/上海搬场公司/上海搬场/-上海大众搬场物流有限公司竭诚为您服务.欢迎来电咨询.详情请进入:http://www.sh-dzbc.cn/
优惠的特价机票/打折机票/上海机票/国际机票/飞机票-上海不夜城国际旅行社欢迎您,竭诚为宾客提供热情、快捷、细致的规范化服务。详情请进入:特价机票飞机票
友情链接:上海搬家
游客 [2008-08-12 12:55:06]
World of Warcraft Gold
WoW Gold
Buy WoW Accounts
Buy WoW Gold
WoW Power Leveling
WoW Honor Points Leveling
WoW Skill Leveling
WoW Reputation Leveling
WoW Leveling Packages
WoW CD Key
WoW Game Time Card
WOW Gold Europe
Buy WoW EU Gold
WoW Accounts For Sale
Cheap WoW Power Leveling
Lineage 2 Adena
Lineage2 Adena
Lineage 2 Accounts
L2 Account
Lineage 2 Power Leveling
Lineage 2 CD Key
Lineage 2 Time Card
Lotro Gold
Lotro Gold
LoTRO Accounts
Lotro Power Leveling
Lotro Cd Key
Lotro Time Card
LoTRO EU Gold
Cheap LoTRO Accounts
Lord of the Rings Online Power Leveling
Cheap WoW Gold
Wow gold
Wow gold
Wow powerleveling
Wow honor leveling
Wow cd key
Wow accounts
World of warcraft gold
Wow PTR Patch 2.3.2
Cheapest wow gold
Buy wow gold
Lineage 2 adena
Lineage 2 accounts
Lineage 2 powerleveling
Lotro gold
Lotro powerleveling
WoW Gold Reviews
WoW Burning Crusade
World of warcraft gold
Buy wow gold
Sell world of warcraft gold
Cheapest wow gold
World of warcraft gold
Buy wow account
Buy world of warcraft account
Wow power leveling
World of warcraft power leveling
Wow Honor Points Leveling
Wow Pvp Honor Leveling
Wow skill leveling
Wow Honor Leveling
Wow Cd Key
Wow Game Time Card
Wow gold for sale
Buy wow gold
World of warcraft gold buying
Wow accounts
Cheap wow power leveling
World Of Warcraft Cd Key
World Of Warcraft Time Card
Lineage 2 adena
Cheap lineage2 adena
Buy l2 adena
L2 adena
Lineage2 adena
Lineage 2 accounts
L2 accounts
Lineage 2 power leveling
Cheap Lotro Gold
Lord Of The Rings Online Gold
Buy Lotro Accounts
Lord Of The Rings Online Power Leveling
Lotro Cd Key
Lord Time Card
Cheapest Lotro Gold
Lord Of The Ring Online Gold
Lotro Gold
Lord of the rings online accounts
Lotro Power Leveling
Lord Of The Rings Online Cd Key
Lord Of The Rings Online Time Card
World of Warcraft Gold
WoW Gold
Lotro Gold
Lineage 2 Adena
Lineage2 Adena
Lineage 2 Accounts
Lineage 2 Power Leveling
Lineage 2 CD Key
Buy WoW Gold
WoW Accounts
WOW Power
Leveling
WoW CD Key
WOW Gold Europe
Buy WoW EU Gold
WoW EU Accounts
World of Warcraft Powerleveling
World of Warcraft CD Key
Buy LoTRO US Gold
LoTRO Accounts
Lotro Power Leveling
LoTRO US CD Key
LoTRO Europe Gold
World of Warcraft Gold
Buy LoTRO EU Gold
Cheap LoTRO Accounts
WoW Gold
Age of Conan time card
Age of Conan cd key
Age of Conan Gold
Buy Age of Conan Gold
Age of Conan Accounts
Age of Conan Power Leveling
Cheap Age of Conan Gold
AoC Gold
AoC Accounts
Cheap Age of Conan Power Leveling
Age of Conan Gold
Cheap Age of Conan Gold
Age of Conan Accounts
Aoc Power Leveling
Age of Conan CD Key
Aoc Gold
Age of Conan Gold Europe
Aoc Accounts
Age of Conan Power Leveling
Age of Conan Time Card
游客 [2008-08-07 14:34:38]
游客 [2008-08-06 14:11:45]
游客 [2008-08-06 13:33:01]
游客 [2008-08-06 13:32:27]
For those who are inexperienced with purchasing the lotro gold,lotro accountsand lotro powerleveling from lotro goldsellers,there are many lotro gold sellers that are frauds and scams that can not be removed from the internet.Firstly.reviewsof the lord of the rings online gold sellers and hearing out what other customers have to see can be very helpful.
wow gold
buy wow gold
lotro gold
aoc gold
lineage 2 adena
wow gold
buy wow gold
lotro gold
aoc gold
lineage 2 adena

lotro gold
lotro gold for sale
lord of the rings online gold
游客 [2008-08-06 09:28:32]
游客 [2008-08-01 17:19:39]
移民
投资移民
加拿大移民
技术移民
移民加拿大
澳洲技术移民
德国移民
移民澳洲
澳洲移民
出国移民
移民出国
英国移民
澳大利亚移民
加拿大投资移民
加拿大技术移民
美国留学
法国留学
北欧留学
瑞典留学
芬兰留学
澳洲留学
除湿机
抽湿机
工业除湿机
空气净化器
空气净化机
步进电机
联轴器
真空泵
工作服
职业装
北京工作服
定做工作服
北京二手空调回收
空调维修
物资回收
防腐设备
风机
铠装热电偶
精密铸造
美术培训
美术高考
美术高考培训
画室
北京画室
谐波治理
无功补偿
防腐管道
英美制丝锥
继电保护测试仪
日语学校
日语培训
安装卫星电视
安装卫星天线
北京安装卫星电视
北京安装卫星天线
针孔摄像机
望远镜
夜视仪
探测狗
窃听器
无线耳机
屏蔽器
金属探测器
隔墙监听器
国标舞
拉丁舞
喷码机
针孔摄像机
烤瓷牙
除沫器
土壤水分速测仪
土壤水分测定仪
土壤水分测量仪
土壤墒情记录仪
农药残留速测仪
土壤化肥速测仪
土壤养分测试仪
信号隔离器
信号分配器
隔离器
温度变送器
电流变送器
配电器
隔离配电器
隔离模块
糖尿病足
煤气发生炉
高低温试验箱
振动试验台
恒温恒湿试验箱
恒温恒湿箱
恒温箱
振动台
盐雾箱
老化台
盐雾试验箱
高低温箱
低温试验箱
振动试验机
合同纠纷
房产纠纷
劳动纠纷
房地产律师
制氮机
在职研究生
液体壁纸
清水模板
冷弯型钢
roll forming
开口闪点仪
凝固点仪
闭口闪点仪
运动粘度仪
粘度仪
抗乳化测定仪
丝网除沫器
气液过滤网
除雾器
丝网除雾器
波纹填料
三菱电机空调
牛仔服
牛仔服装厂
牛仔休闲
牛仔裤
牛仔品牌
牛仔专卖店
虹吸
虹吸雨水
虹吸排水
有压流
同层排水
walk throught metal detector
恒温器
马达保护器
热保护器
温度开关
温控器
过流保护器
藏獒
Google左侧优化
舞台设计
铅丝笼
石笼网
烧烤网
振动筛网
拖链
光纤熔接机
光缆监测系统
光时域反射仪
OTDR
nike shoes
air jordan
不锈钢反应斧
游客 [2008-08-01 11:13:31]
For those who are inexperienced with purchasing the lotro gold,lotro accountsand lotro powerleveling from lotro goldsellers,there are many lotro gold sellers that are frauds and scams that can not be removed from the internet.Firstly.reviewsof the lord of the rings online gold sellers and hearing out what other customers have to see can be very helpful.
wow gold
buy wow gold
lotro gold
aoc gold
lineage 2 adena
wow gold
buy wow gold
lotro gold
aoc gold
lineage 2 adena

lotro gold
lotro gold for sale
lord of the rings online gold
游客 [2008-08-01 11:13:06]
For those who are inexperienced with purchasing the lotro gold,lotro accountsand lotro powerleveling from lotro goldsellers,there are many lotro gold sellers that are frauds and scams that can not be removed from the internet.Firstly.reviewsof the lord of the rings online gold sellers and hearing out what other customers have to see can be very helpful.
wow gold
buy wow gold
lotro gold
aoc gold
lineage 2 adena
wow gold
buy wow gold
lotro gold
aoc gold
lineage 2 adena

lotro gold
lotro gold for sale
lord of the rings online gold
游客 [2008-07-29 14:15:44]

优惠的打折机票|国际机票|上海机票|飞机票|特价机票为您提供优质服务。详情请进http://www.01fly.com.cn

专业的搬家公司|上海搬家公司|上海搬家|搬场公司|上海搬场公司|上海搬场公司为您提供优质的搬家,搬场服务。详情请进http://www.shdz-bc.cn

专业的大众搬场公司|上海大众搬场|大众搬场公司为您提供优质的搬家,搬场服务。详情请进http://www.dz-bc.com.cn

上海中铁公司为您提供上海货运公司|上海货运|物流公司|上海物流公司|上海物流竭诚欢迎各企业及新老客户前来洽谈合作事宜。详情请进http://www.didicars.cn,本公司全天候24小时为您服务。

  • 共有 24 条评论
  • 1
  • 2
  • |
  • >>
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启