PageRenderTime 60ms CodeModel.GetById 35ms 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. */
  22. package org.jboss.as.clustering.singleton.election;
  23. import java.util.List;
  24. import org.jboss.as.clustering.ClusterNode;
  25. import org.jboss.as.clustering.singleton.SingletonElectionPolicy;
  26. /**
  27. * A simple concrete policy service that decides which node in the cluster should be the master node to run certain HASingleton
  28. * service based on attribute "Position". The value will be divided by partition size and only remainder will be used.
  29. *
  30. * 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.
  31. *
  32. * -1 means the youngest node. -2 means the 2nd youngest node. ... -n means the nth youngest node.
  33. *
  34. * E.g. the following attribute says the singleton will be running on the 3rd oldest node of the current partition: <attribute
  35. * name="Position">2</attribute>
  36. *
  37. * If no election policy is defined, the oldest node in the cluster runs the singleton. This behaivour can be achieved with this
  38. * policy when "position" is set to 0.
  39. *
  40. * @author <a href="mailto:Alex.Fu@novell.com">Alex Fu</a>
  41. * @author <a href="mailto:galder.zamarreno@jboss.com">Galder Zamarreno</a>
  42. * @author Paul Ferraro
  43. */
  44. public class SimpleSingletonElectionPolicy implements SingletonElectionPolicy {
  45. private final int position;
  46. public SimpleSingletonElectionPolicy() {
  47. this(0);
  48. }
  49. public SimpleSingletonElectionPolicy(int position) {
  50. this.position = position;
  51. }
  52. @Override
  53. public ClusterNode elect(List<ClusterNode> candidates) {
  54. int size = candidates.size();
  55. return (size > 0) ? candidates.get(((this.position % size) + size) % size) : null;
  56. }
  57. }