Package org.jboss.ha.client.loadbalance

Examples of org.jboss.ha.client.loadbalance.LoadBalancePolicy


   {
      testCount++;
      List<String> targets = Arrays.asList(TARGETS);
      FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, targets);
     
      LoadBalancePolicy lbp = getLoadBalancePolicy();
      Object target = lbp.chooseTarget(fci);
     
      assertTrue(targets.contains(target));
     
      for (int i = 0; i < 10; i++)
         assertEquals(target, lbp.chooseTarget(fci));
   }
View Full Code Here


     
      Set<Object> selected = new HashSet<Object>();
     
      for (int i = 0; i < 10000; i++)
      {
         LoadBalancePolicy lbp = getLoadBalancePolicy();
         selected.add(lbp.chooseTarget(fci));
         if (selected.size() == TARGETS.length)
            break;
      }
     
      assertEquals(TARGETS.length, selected.size());
View Full Code Here

   {
      testCount++;
      List<String> targets = Arrays.asList(TARGETS);
      FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, targets);
     
      LoadBalancePolicy lbp = getLoadBalancePolicy();
      Object firstTarget;
      Object target = firstTarget = lbp.chooseTarget(fci);
     
      assertTrue(targets.contains(target));
     
      for (int i = 0; i < 5; i++)
         assertEquals(target, lbp.chooseTarget(fci));
     
      fci.removeDeadTarget(target);
      Object secondTarget;
      target = secondTarget = lbp.chooseTarget(fci);
     
      assertFalse(firstTarget.equals(secondTarget));
      assertTrue(targets.contains(target));
     
      for (int i = 0; i < 5; i++)
         assertEquals(target, lbp.chooseTarget(fci));
     
      fci.removeDeadTarget(target);
      Object thirdTarget;
      target = thirdTarget = lbp.chooseTarget(fci);
     
      assertFalse(firstTarget.equals(thirdTarget));
      assertFalse(secondTarget.equals(thirdTarget));
      assertTrue(targets.contains(target));
     
      for (int i = 0; i < 5; i++)
         assertEquals(target, lbp.chooseTarget(fci));
     
      fci.removeDeadTarget(target);
     
      assertNull(lbp.chooseTarget(fci));
   }
View Full Code Here

     
      assert lbpClass != null && !lbpClass.trim().equals("") : LoadBalancePolicy.class.getSimpleName()
            + " class name is required, but is not specified; improperly bound reference in JNDI";
     
      ClassLoader tccl = Thread.currentThread().getContextClassLoader();
      LoadBalancePolicy loadBalancePolicy = (LoadBalancePolicy) tccl.loadClass(lbpClass).newInstance();
     
      FamilyWrapper wrapper = getFamilyWrapper(name, refAddrs);
     
      Class<?>[] interfaces = {this.getProxyFactoryClass()};
      Interceptor[] interceptors = { IsLocalProxyFactoryInterceptor.singleton,
View Full Code Here

      // Get Interceptors
      Interceptor[] interceptors = this.getInterceptors();

      Class<? extends LoadBalancePolicy> policyClass = isOnlyHome ? beanClusteringInfo.getHomeLoadBalancePolicy() : beanClusteringInfo.getLoadBalancePolicy();
      LoadBalancePolicy lbp;
      try
      {
         lbp = policyClass.newInstance();
      }
      catch (Exception e)
View Full Code Here

      // Get Interceptors
      Interceptor[] interceptors = this.getInterceptors();

      Class<? extends LoadBalancePolicy> policyClass = isOnlyHome ? beanClusteringInfo.getHomeLoadBalancePolicy() : beanClusteringInfo.getLoadBalancePolicy();
      LoadBalancePolicy lbp;
      try
      {
         lbp = policyClass.newInstance();
      }
      catch (Exception e)
View Full Code Here

      return "ClusterChooserInterceptor";
   }

   public Object invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable
   {
      LoadBalancePolicy lb = (LoadBalancePolicy) invocation.getMetaData(CLUSTERED_REMOTING, LOADBALANCE_POLICY);
      FamilyWrapper family = (FamilyWrapper) invocation.getMetaData(CLUSTERED_REMOTING, CLUSTER_FAMILY_WRAPPER);

      // we give the opportunity, to any server interceptor, to know if this a
      // first invocation to a node or if it is a failovered call
      //
      int failoverCounter = 0;
      String familyName = family.get().getFamilyName();
      invocation.getMetaData().addMetaData(CLUSTERED_REMOTING, CLUSTER_FAMILY, familyName, PayloadKey.AS_IS);
     
      InvokerLocator target = null;
      InvokerLocator txStickyTarget = null;
      Object tpc = this.getTransactionPropagationContext();
      if (tpc != null)
      {
         txStickyTarget = txStickyTargets.get(tpc);
         if (txStickyTarget != null)
         {
            if (family.get().getTargets().contains(txStickyTarget))
            {
               if (trace)
               {
                  log.trace("Using transaction sticky target: " + txStickyTarget);
               }
               target = txStickyTarget;
            }
            else
            {
               throw new RuntimeException("Current transaction is stuck to " + txStickyTarget + " which is no longer available.  Halting invocation.");
            }
         }
      }
     
      if (target == null)
      {
         if (lb instanceof AopLoadBalancePolicy)
         {
            target = (InvokerLocator) ((AopLoadBalancePolicy) lb).chooseTarget(family.get(), invocation);
         }
         else
         {
            target = (InvokerLocator) lb.chooseTarget(family.get());
         }
      }
     
      Throwable lastException = null;
      boolean failoverAuthorized = true;
      while ((target != null) && failoverAuthorized)
      {
         invocation.getMetaData().addMetaData(CLUSTERED_REMOTING, FAILOVER_COUNTER, new Integer(failoverCounter), PayloadKey.AS_IS);
         invocation.getMetaData().addMetaData(InvokeRemoteInterceptor.REMOTING, InvokeRemoteInterceptor.INVOKER_LOCATOR, target, PayloadKey.AS_IS);
         invocation.getMetaData().addMetaData(CLUSTERED_REMOTING, CLUSTER_VIEW_ID, new Long(family.get().getCurrentViewId()), PayloadKey.AS_IS);
        
         boolean definitivlyRemoveNodeOnFailure = true;
         lastException = null;
         try
         {
            Object rsp = invocation.invokeNext();
            List<?> newReplicants = (List<?>) invocation.getResponseAttachment("replicants");
            if (newReplicants != null)
            {
               long newViewId = ((Long) invocation.getResponseAttachment("viewId")).longValue();
               family.get().updateClusterInfo(newReplicants, newViewId);
            }
           
            if (txStickyTarget == null)
            {
               if (invocationHasReachedAServer(target))
               {
                  if (trace)
                  {
                     log.trace("Setting " + target + " as the new sticky target");
                  }
                  txStickyTarget = target;
               }
            }
           
            return rsp;
         }
         catch (DispatcherConnectException dce)
         {
            //In case of graceful shutdown, the target object will no longer exist in the Dispatcher,
            //fail over to another server if we can...
            //For now remove the node "definitely"
            lastException = dce;
         }
         catch (CannotConnectException ex)
         {
            lastException = ex;
         }
         catch (GenericClusteringException gce)
         {
            lastException = gce;
            // this is a generic clustering exception that contain the
            // completion status: usefull to determine if we are authorized
            // to re-issue a query to another node
            //
            if (gce.getCompletionStatus() == GenericClusteringException.COMPLETED_NO)
            {
               // we don't want to remove the node from the list of failed
               // node UNLESS there is a risk to indefinitively loop
               //
               if (family.get().getTargets().size() >= failoverCounter)
               {
                  if (!gce.isDefinitive())
                     definitivlyRemoveNodeOnFailure = false;
               }
            }
            else
            {
               this.invocationHasReachedAServer(target);
               throw new RuntimeException("Clustering exception thrown", gce);
            }
         }
         catch (Throwable t)
         {
            // Just in case this get wrapped in a Throwable. This can happen when
            // the exception is generated inside the container and it is wrapped in
            // a ForwardId exception.
            if (t.getCause() instanceof GenericClusteringException)
            {
               GenericClusteringException gce = (GenericClusteringException)t.getCause();
               lastException = gce;
               // this is a generic clustering exception that contain the
               // completion status: usefull to determine if we are authorized
               // to re-issue a query to another node
               //
               if (gce.getCompletionStatus() == GenericClusteringException.COMPLETED_NO)
               {
                  // we don't want to remove the node from the list of failed
                  // node UNLESS there is a risk to indefinitively loop
                  //
                  if (family.get().getTargets().size() >= failoverCounter)
                  {
                     if (!gce.isDefinitive())
                        definitivlyRemoveNodeOnFailure = false;
                  }
               }
               else
               {
                  this.invocationHasReachedAServer(target);
                  throw new RuntimeException("Clustering exception thrown", gce);
               }
            }
            else
            {
               throw t;
            }
         }

        
         // If we reach here, this means that we must fail-over
         family.get().removeDeadTarget(target);
         if (!definitivlyRemoveNodeOnFailure)
         {
            family.get().resetView();
         }

         target = (InvokerLocator) lb.chooseTarget(family.get());
         if (target == null)
         {
            if (lastException != null)
            {
               throw new RuntimeException("cluster invocation failed, last exception was: ", lastException);
View Full Code Here

      return "ClusterChooserInterceptor";
   }

   public Object invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable
   {
      LoadBalancePolicy lb = (LoadBalancePolicy) invocation.getMetaData(CLUSTERED_REMOTING, LOADBALANCE_POLICY);
      FamilyWrapper family = (FamilyWrapper) invocation.getMetaData(CLUSTERED_REMOTING, CLUSTER_FAMILY_WRAPPER);

      // we give the opportunity, to any server interceptor, to know if this a
      // first invocation to a node or if it is a failovered call
      //
      int failoverCounter = 0;
      String familyName = family.get().getFamilyName();
      invocation.getMetaData().addMetaData(CLUSTERED_REMOTING, CLUSTER_FAMILY, familyName, PayloadKey.AS_IS);
     
      InvokerLocator target = null;
      InvokerLocator txStickyTarget = null;
      boolean supportsTxStickiness = family.isTxStickinessSupported();
      if (supportsTxStickiness) {
         Object tpc = this.getTransactionPropagationContext();
         if (tpc != null)
         {
            txStickyTarget = txStickyTargets.get(tpc);
            if (txStickyTarget != null)
            {
               if (family.get().getTargets().contains(txStickyTarget))
               {
                  if (trace)
                  {
                     log.trace("Using transaction sticky target: " + txStickyTarget);
                  }
                  target = txStickyTarget;
               }
               else
               {
                  throw new RuntimeException("Current transaction is stuck to " + txStickyTarget + " which is no longer available.  Halting invocation.");
               }
            }
         }
      }
     
      if (target == null)
      {
         if (lb instanceof AopLoadBalancePolicy)
         {
            target = (InvokerLocator) ((AopLoadBalancePolicy) lb).chooseTarget(family.get(), invocation);
         }
         else
         {
            target = (InvokerLocator) lb.chooseTarget(family.get());
         }
      }
     
      Throwable lastException = null;
      boolean failoverAuthorized = true;
      while ((target != null) && failoverAuthorized)
      {
         invocation.getMetaData().addMetaData(CLUSTERED_REMOTING, FAILOVER_COUNTER, new Integer(failoverCounter), PayloadKey.AS_IS);
         invocation.getMetaData().addMetaData(InvokeRemoteInterceptor.REMOTING, InvokeRemoteInterceptor.INVOKER_LOCATOR, target, PayloadKey.AS_IS);
         invocation.getMetaData().addMetaData(CLUSTERED_REMOTING, CLUSTER_VIEW_ID, new Long(family.get().getCurrentViewId()), PayloadKey.AS_IS);
        
         boolean definitivlyRemoveNodeOnFailure = true;
         lastException = null;
         try
         {
            Object rsp = invocation.invokeNext();
            List<?> newReplicants = (List<?>) invocation.getResponseAttachment("replicants");
            if (newReplicants != null)
            {
               long newViewId = ((Long) invocation.getResponseAttachment("viewId")).longValue();
               family.get().updateClusterInfo(newReplicants, newViewId);
            }
           
            if (txStickyTarget == null)
            {
               if (invocationHasReachedAServer(target))
               {
                  if (trace)
                  {
                     log.trace("Setting " + target + " as the new sticky target");
                  }
                  txStickyTarget = target;
               }
            }
           
            return rsp;
         }
         catch (DispatcherConnectException dce)
         {
            //In case of graceful shutdown, the target object will no longer exist in the Dispatcher,
            //fail over to another server if we can...
            //For now remove the node "definitely"
            lastException = dce;
         }
         catch (CannotConnectException ex)
         {
            lastException = ex;
         }
         catch (GenericClusteringException gce)
         {
            lastException = gce;
            // this is a generic clustering exception that contain the
            // completion status: usefull to determine if we are authorized
            // to re-issue a query to another node
            //
            if (gce.getCompletionStatus() == GenericClusteringException.COMPLETED_NO)
            {
               // we don't want to remove the node from the list of failed
               // node UNLESS there is a risk to indefinitively loop
               //
               if (family.get().getTargets().size() >= failoverCounter)
               {
                  if (!gce.isDefinitive())
                     definitivlyRemoveNodeOnFailure = false;
               }
            }
            else
            {
               this.invocationHasReachedAServer(target);
               throw new RuntimeException("Clustering exception thrown", gce);
            }
         }
         catch (Throwable t)
         {
            // Just in case this get wrapped in a Throwable. This can happen when
            // the exception is generated inside the container and it is wrapped in
            // a ForwardId exception.
            if (t.getCause() instanceof GenericClusteringException)
            {
               GenericClusteringException gce = (GenericClusteringException)t.getCause();
               lastException = gce;
               // this is a generic clustering exception that contain the
               // completion status: usefull to determine if we are authorized
               // to re-issue a query to another node
               //
               if (gce.getCompletionStatus() == GenericClusteringException.COMPLETED_NO)
               {
                  // we don't want to remove the node from the list of failed
                  // node UNLESS there is a risk to indefinitively loop
                  //
                  if (family.get().getTargets().size() >= failoverCounter)
                  {
                     if (!gce.isDefinitive())
                        definitivlyRemoveNodeOnFailure = false;
                  }
               }
               else
               {
                  this.invocationHasReachedAServer(target);
                  throw new RuntimeException("Clustering exception thrown", gce);
               }
            }
            else
            {
               throw t;
            }
         }

        
         // If we reach here, this means that we must fail-over
         family.get().removeDeadTarget(target);
         if (!definitivlyRemoveNodeOnFailure)
         {
            family.get().resetView();
         }

         target = (InvokerLocator) lb.chooseTarget(family.get());
         if (target == null)
         {
            if (lastException != null)
            {
               throw new RuntimeException("cluster invocation failed, last exception was: ", lastException);
View Full Code Here

   public void testBasicCycle()
   {
      testCount++;
      FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, Arrays.asList(TARGETS));
      LoadBalancePolicy lbp = getLoadBalancePolicy();
      int expectedCursor = -1;
      for (int i = 0; i < 100; i++)
      {
         Object target = lbp.chooseTarget(fci);
         if (expectedCursor < 0)
         {
            expectedCursor = fci.getCursor();
         }
         else
View Full Code Here

   public void testFailover()
   {
      testCount++;
      List<String> targets = new ArrayList<String>(Arrays.asList(TARGETS));
      FamilyClusterInfo fci = ClusteringTargetsRepository.initTarget(FAMILY_BASE + testCount, targets);
      LoadBalancePolicy lbp = getLoadBalancePolicy();
     
      String target = (String) lbp.chooseTarget(fci);
      int cursor = fci.getCursor();
      assertEquals(target, fci.getTargets().get(cursor));
     
      while (targets.size() > 1)
      {
         fci.removeDeadTarget(target);
         targets.remove(target);
         assertEquals(targets, fci.getTargets());
        
         int expectedCursor = ++cursor % targets.size();
        
         target = (String) lbp.chooseTarget(fci);
         cursor = fci.getCursor();
         assertEquals(expectedCursor, cursor);
         assertEquals(target, fci.getTargets().get(cursor));
      }
   }
View Full Code Here

TOP

Related Classes of org.jboss.ha.client.loadbalance.LoadBalancePolicy

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.