01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.ha.singleton;
23:
24: import org.jboss.ha.framework.interfaces.ClusterNode;
25: import org.jboss.ha.framework.interfaces.HAPartition;
26:
27: /**
28: * A simple concrete policy service that decides which node in the cluster should be
29: * the master node to run certain HASingleton service based on attribute "Position".
30: * The value will be divided by partition size and only remainder will be used.
31: *
32: * Let's say partition size is n:
33: * 0 means the first oldest node.
34: * 1 means the 2nd oldest node.
35: * ...
36: * n-1 means the nth oldest node.
37: *
38: * -1 means the youngest node.
39: * -2 means the 2nd youngest node.
40: * ...
41: * -n means the nth youngest node.
42: *
43: * E.g. the following attribute says the singleton will be running on the 3rd oldest node of
44: * the current partition:
45: * <attribute name="Position">2</attribute>
46: *
47: * @author <a href="mailto:Alex.Fu@novell.com">Alex Fu</a>
48: * @version $Revision: 46010 $
49: */
50: public class HASingletonElectionPolicySimple extends
51: HASingletonElectionPolicyBase implements
52: HASingletonElectionPolicySimpleMBean {
53: // Attributes
54: private int mPosition = 0; // Default
55:
56: /**
57: * @see HASingletonElectionPolicySimpleMBean#setPosition(int)
58: */
59: public void setPosition(int pos) {
60: this .mPosition = pos;
61: }
62:
63: /**
64: * @see HASingletonElectionPolicySimpleMBean#getPosition()
65: */
66: public int getPosition() {
67: return this .mPosition;
68: }
69:
70: public ClusterNode pickSingleton() {
71: return pickSingleton(getHAPartition());
72: }
73:
74: public ClusterNode pickSingleton(HAPartition partition) {
75: ClusterNode[] nodes = partition.getClusterNodes();
76:
77: int size = nodes.length;
78: int remainder = ((this.mPosition % size) + size) % size;
79:
80: return nodes[remainder];
81: }
82: }
|