PageRenderTime 41ms CodeModel.GetById 18ms app.highlight 15ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/clustering/singleton/src/main/java/org/jboss/as/clustering/singleton/election/SimpleSingletonElectionPolicy.java

#
Java | 64 lines | 18 code | 7 blank | 39 comment | 0 complexity | f9db20b1f684352169f6acdc012de9c3 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
 1/*
 2 * JBoss, Home of Professional Open Source.
 3 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 4 * as indicated by the @author tags. See the copyright.txt file in the
 5 * distribution for a full listing of individual contributors.
 6 *
 7 * This is free software; you can redistribute it and/or modify it
 8 * under the terms of the GNU Lesser General Public License as
 9 * 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 */
22package org.jboss.as.clustering.singleton.election;
23
24import java.util.List;
25
26import org.jboss.as.clustering.ClusterNode;
27import org.jboss.as.clustering.singleton.SingletonElectionPolicy;
28
29/**
30 * A simple concrete policy service that decides which node in the cluster should be the master node to run certain HASingleton
31 * service based on attribute "Position". The value will be divided by partition size and only remainder will be used.
32 *
33 * Let's say partition size is n: 0 means the first oldest node. 1 means the 2nd oldest node. ... n-1 means the nth oldest node.
34 *
35 * -1 means the youngest node. -2 means the 2nd youngest node. ... -n means the nth youngest node.
36 *
37 * E.g. the following attribute says the singleton will be running on the 3rd oldest node of the current partition: <attribute
38 * name="Position">2</attribute>
39 *
40 * If no election policy is defined, the oldest node in the cluster runs the singleton. This behaivour can be achieved with this
41 * policy when "position" is set to 0.
42 *
43 * @author <a href="mailto:Alex.Fu@novell.com">Alex Fu</a>
44 * @author <a href="mailto:galder.zamarreno@jboss.com">Galder Zamarreno</a>
45 * @author Paul Ferraro
46 */
47public class SimpleSingletonElectionPolicy implements SingletonElectionPolicy {
48
49    private final int position;
50
51    public SimpleSingletonElectionPolicy() {
52        this(0);
53    }
54
55    public SimpleSingletonElectionPolicy(int position) {
56        this.position = position;
57    }
58
59    @Override
60    public ClusterNode elect(List<ClusterNode> candidates) {
61        int size = candidates.size();
62        return (size > 0) ? candidates.get(((this.position % size) + size) % size) : null;
63    }
64}