Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2009 eXo Platform SAS.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.exoplatform.services.jcr.ext.replication.recovery;
 
 
 import java.util.List;
Created by The eXo Platform SAS.

Author(s):
Alex Reshetnyak
Version:
$Id: ConectionFailDetector.java 111 2008-11-11 11:11:11Z rainf0x $
 
 
 public class ConnectionFailDetector implements StateListener
 {
   
The apache logger.
 
    private static Log log = ExoLogger.getLogger("exo.jcr.component.ext.ConnectionFailDetector");

   
Definition the VIEW_CHECK timeout.
 
    private static final int VIEW_CHECK = 200;

   
The definition timeout for information.
 
    private static final int INFORM_TIMOUT = 5000;

   
Definition the BEFORE_CHECK timeout.
 
    private static final int BEFORE_CHECK = 10000;

   
Definition the BEFORE_INIT timeout.
 
    private static final int BEFORE_INIT = 60000;

   
Definition the AFTER_INIT timeout.
 
    private static final int AFTER_INIT = 60000;

   
The ChannelManager will be transmitted or receive the Packets.
 
    private final ChannelManager channelManager;

   
The name of workspace.
 
    private final String workspaceName;

   
The channel name.
 
    private String channelName;

   
The ReconectTtread will be initialized reconnect to cluster.
 
    private ReconectTtread reconectTtread;

   
Start value for lastViewSize.
 
   private int lastViewSize = 2;

   
Start value for allInited.
   private boolean allInited = false;

   
The PersistentDataManager will be used to workspace for set state 'read-only'.
   private final PersistentDataManager dataManager;

   
The RecoveryManager will be initialized cluster node synchronization.
   private final RecoveryManager recoveryManager;

   
The own priority value.
   private final int ownPriority;

   
The own name in cluster.
   private final String ownName;

   
The list of names to other participants in cluster.
   private final List<StringotherPartisipants;

   
The priority checker (static or dynamic).
   private final AbstractPriorityChecker priorityChecker;

   
The view checker.
   private final ViewChecker viewChecker;

   
ConnectionFailDetector constructor.

Parameters:
channelManager the ChannelManager
dataManager the PersistentDataManager
recoveryManager the RecoveryManager
ownPriority the own priority
otherParticipants the list of names to other participants in cluster
ownName the own name in cluster
priprityType the priority type (dynamic or static)s
workspaceName String, the name of workspace
   public ConnectionFailDetector(ChannelManager channelManagerPersistentDataManager dataManager,
      RecoveryManager recoveryManagerint ownPriorityList<StringotherParticipantsString ownName,
      String priprityTypeString workspaceName)
   {
      this. = channelManager;
      this. = dataManager;
      this. = workspaceName;
      this. = recoveryManager;
      this. = ownPriority;
      this. = ownName;
      this. = new ArrayList<String>(otherParticipants);
      if (priprityType.equals(.))
          = new StaticPriorityChecker(channelManagerownPriorityownNameotherParticipants);
      else if (priprityType.equals(.))
          = new DynamicPriorityChecker(channelManagerownPriorityownNameotherParticipants);
      else
          = new GenericPriorityChecker(channelManagerownPriorityownNameotherParticipants);
       = new ViewChecker();
      .start();
   }

   
   public void onStateChanged(StateEvent event)
   {
      .putView(event);
   }

   
viewAccepted.

Parameters:
viewSize int, the view size
Throws:
java.lang.InterruptedException will be generated the exception InterruptedException
org.exoplatform.services.jcr.ext.replication.PriorityDucplicatedException will be generated the exception PriorityDucplicatedException
   private void viewAccepted(int viewSizethrows InterruptedExceptionPriorityDucplicatedException
   {
      Thread.sleep();
      if (viewSize > 1)
          = true;
      if ( == true)
          = viewSize;
      {
         .info( + " set read-only");
         .setReadOnly(true);
         throw new PriorityDucplicatedException("The priority was duplicated :  own priority = " + 
            + ", other priority = " + .getOtherPriorities());
      }
      if (.isAllOnline())
      {
         if ( != null)
         {
            .setStop(false);
             = null;
         }
         memberRejoin();
         return;
      }
      if ( instanceof GenericPriorityChecker)
      {
         if ( == 1 && ( == null || .isStoped() == true))
         {
             = new ReconectTtread(true);
            .start();
         }
      }
      else if ( instanceof StaticPriorityChecker || .size() == 1)
      {
         if (.isDebugEnabled())
         {
            .debug("lastViewSize == 1 && !priorityChecker.isMaxPriority() == "
               + ( == 1 && !.isMaxPriority()));
            .debug("lastViewSize > 1 && !priorityChecker.isMaxOnline() == "
               + ( > 1 && !.isMaxOnline()));
         }
         if ( == 1 && !.isMaxPriority())
         {
            if ( == null || .isStoped() == true)
            {
                = new ReconectTtread(true);
               .start();
               memberSuspect();
            }
         }
         else if ( != null && .isAllOnline())
         {
            .setStop(false);
             = null;
         }
         else if ( > 1 && !.isMaxOnline())
         {
            if ( == null || .isStoped() == true)
            {
                = new ReconectTtread(true);
               .start();
               memberSuspect();
            }
         }
      }
      else if ( instanceof DynamicPriorityChecker && .size() > 1)
      {
         if ( == 1 && !.isMaxPriority())
         {
            if ( == null || .isStoped() == true)
            {
                = new ReconectTtread(true);
               .start();
               memberSuspect();
            }
         }
         else if ( != null && .isAllOnline())
         {
            .setStop(false);
             = null;
         }
      }
   }

   
The view checker. Will be check View.
   private class ViewChecker extends Thread
   {
      
The view queue.
      private final ConcurrentLinkedQueue<Integerqueue = new ConcurrentLinkedQueue<Integer>();
      public ViewChecker()
      {
         super("ViewChecker");
      }
      
      
putView.

Parameters:
event StateEvent, the event
      public void putView(StateEvent event)
      {
         .offer(event.getMembers().size());
      }

      
      public void run()
      {
         while (true)
         {
            try
            {
               Integer viewSize = .poll();
               if (viewSize != null)
                  viewAccepted(viewSize);
               sleep( * 2);
            }
            catch (PriorityDucplicatedException e)
            {
               .error("The wrong priority :"e);
            }
            catch (Throwable t)
            {
               .error("View check error :"t);
            }
         }
      }
   }

   
The ReconectTtread will be initialized reconnect to cluster.
   private class ReconectTtread extends Thread
   {
      
The 'isStop' is a flag to run() stop.
      private boolean isStop;

      
ReconectTtread constructor.

Parameters:
isStop the 'isStop' value
      public ReconectTtread(boolean isStop)
      {
         super("ReconectTtread");
         .info("Thread '" + getName() + "' is init ...");
         this. = isStop;
      }

      
      public void run()
      {
         .info("Thread '" + getName() + "' is run ...");
         while ()
         {
            try
            {
               .info("Connect to channel : " + );
               Thread.sleep();
               int curruntOnlin = 1;
               if (.getChannel() != null)
               {
                  while (.getChannel().getView() == null)
                     Thread.sleep();
                  curruntOnlin = .getChannel().getView().size();
               }
               if ( && (curruntOnlin <= 1 || ((curruntOnlin > 1) && !.isMaxOnline())))
               {
                  .disconnect();
                  Thread.sleep();
                  .connect();
               }
               else
               {
                   = false;
               }
               Thread.sleep();
            }
            catch (Exception e)
            {
               .info(ee);
            }
         }
      }

      
setStop.

Parameters:
isStop the 'isStop' value
      public void setStop(boolean isStop)
      {
         this. = isStop;
      }

      
isStoped.

Returns:
boolean return the 'isStop' value
      public boolean isStoped()
      {
         return !;
      }
   }

   
   public void memberRejoin()
   {
      if (!( instanceof GenericPriorityChecker))
      {
         .info( + " set not read-only");
         .setReadOnly(false);
      }
      .info( + " recovery start ...");
   }

   
Call this method if maxPriority member was suspected.
   public void memberSuspect()
   {
      if (!( instanceof GenericPriorityChecker))
      {
         .info( + " set read-only");
         .setReadOnly(true);
      }
   }
New to GrepCode? Check out our FAQ X