List:NDB Connectors« Previous MessageNext Message »
From:Monty Taylor Date:January 23 2008 12:30am
Subject:Rev 419: From johan: "I have also updated the SampleMGMJ to show how to code it for "redundancy" so it can handle multiple management servers. Basical...
View as plain text  
At http://bazaar.launchpad.net/~ndb-connectors/ndb-connectors/devel

------------------------------------------------------------
revno: 419
revision-id:mtaylor@stripped
parent: mtaylor@stripped
committer: Monty Taylor <mtaylor@stripped>
branch nick: devel
timestamp: Tue 2008-01-22 19:30:13 -0500
message:
  From johan: "I have also updated the SampleMGMJ to show how to code it for "redundancy"
so it can handle multiple management servers. Basically, whenever an exception is thrown
then the sampleMGMJ reconnects to the management server (next on the connect string). "
modified:
  java/com/mysql/cluster/mgmj/examples/SampleMGMJ.java
samplemgmj.java-20071119093428-2f2zw02dpk5jlq9f-2
=== modified file 'java/com/mysql/cluster/mgmj/examples/SampleMGMJ.java'
--- a/java/com/mysql/cluster/mgmj/examples/SampleMGMJ.java	2007-12-10 07:39:46 +0000
+++ b/java/com/mysql/cluster/mgmj/examples/SampleMGMJ.java	2008-01-23 00:30:13 +0000
@@ -5,51 +5,86 @@
 import com.mysql.cluster.mgmj.listeners.*;
 
 public class SampleMGMJ {
+    
+    /**
+     * SampleMGMJ demonstrates how to monitor events taking place
+     * inside MySQL Cluster.
+     * <br>
+     * SampleMGMJ connects to one or more (for redundancy) management servers 
+     * as specified by the connectString as an argument, and then starts to
+     * subscribe to a number of events.
+     * <br>
+     * E.g, sampleMGMJ "host1:1186;host2:1186"  
+     * would connect sampleMGMJ to host1:1186. If host1 fails,
+     * then sampleMGMJ will reconnect to the management server on host2:1186
+     * and continue subscribing to events.
+     */
 
-	/**
-	 * @param args
+    public static void main(String[] args) throws NdbMgmException {
+	
+	if(args.length < 1)
+	    {
+		System.out.println("Usage: SampleMGMJ \"connectstring\"");
+		return;
+	    }
+	String connectString=args[0];
+	
+	/*
+	 * start monitoring forever..
 	 */
-	
-	
-	public static void main(String[] args) throws NdbMgmException {
-
-		NdbMgm mgm = NdbMgmFactory.createNdbMgm("localhost");
-
-		mgm.setConnectTimeout(4000);
-		mgm.connect(5, 3, true);
-
-		NdbFilterList theList = new NdbFilterList();
-		NdbFilterItem theItem1 = new
NdbFilterItem(15,NdbLogEventCategory.NDB_MGM_EVENT_CATEGORY_STATISTIC);
-		NdbFilterItem theItem2 = new
NdbFilterItem(15,NdbLogEventCategory.NDB_MGM_EVENT_CATEGORY_CONNECTION);
-		NdbFilterItem theItem3 = new
NdbFilterItem(15,NdbLogEventCategory.NDB_MGM_EVENT_CATEGORY_NODE_RESTART);
-
-		theList.add(theItem1);
-		theList.add(theItem2);
-		theList.add(theItem3);
-
-
-		NdbLogEventManager manager = mgm.createNdbLogEventManager(theList);
-		TransReportListener theListener1 = new TransReportListener();
-		ConnectedListener theListener2 = new ConnectedListener();
-		DisconnectListener theListener3 = new DisconnectListener();
-		NodeFailReportedListener theListener4 = new NodeFailReportedListener();
-		CommunicationOpenedListener theListener5 = new CommunicationOpenedListener();
-		CommunicationClosedListener theListener6 = new CommunicationClosedListener();
-		StartPhaseListener theListener7 = new StartPhaseListener();
-
-		System.out.println("Registering listener");
-		manager.registerListener(theListener1);
-		manager.registerListener(theListener2);
-		manager.registerListener(theListener3);
-		manager.registerListener(theListener4);
-		manager.registerListener(theListener5);
-		manager.registerListener(theListener6);
-		manager.registerListener(theListener7);
-		System.out.println("Done");
-
-		ClusterState cs = mgm.getStatus();
-		for(int i=0; i< cs.getNoOfNodes(); i++)
-		    {
+	while(true) { 
+	    try {
+		    
+		    /*
+		     * A short sleep before trying to reconnect if there
+		     * has been a failure (exception)
+		     */
+		    try {
+			Thread.sleep(50);
+			
+		    } catch(InterruptedException ie){
+			
+			break;
+		    }
+		    NdbMgm mgm = NdbMgmFactory.createNdbMgm(connectString);
+		    
+		    mgm.setConnectTimeout(4000);
+		    mgm.connect(5, 3, true);
+		    
+		    NdbFilterList theList = new NdbFilterList();
+		    NdbFilterItem theItem1 = new
NdbFilterItem(15,NdbLogEventCategory.NDB_MGM_EVENT_CATEGORY_STATISTIC);
+		    NdbFilterItem theItem2 = new
NdbFilterItem(15,NdbLogEventCategory.NDB_MGM_EVENT_CATEGORY_CONNECTION);
+		    NdbFilterItem theItem3 = new
NdbFilterItem(15,NdbLogEventCategory.NDB_MGM_EVENT_CATEGORY_NODE_RESTART);
+		    
+		    theList.add(theItem1);
+		    theList.add(theItem2);
+		    theList.add(theItem3);
+		    
+		    
+		    NdbLogEventManager manager = mgm.createNdbLogEventManager(theList);
+		    TransReportListener theListener1 = new TransReportListener();
+		    ConnectedListener theListener2 = new ConnectedListener();
+		    DisconnectListener theListener3 = new DisconnectListener();
+		    NodeFailReportedListener theListener4 = new NodeFailReportedListener();
+		    CommunicationOpenedListener theListener5 = new CommunicationOpenedListener();
+		    CommunicationClosedListener theListener6 = new CommunicationClosedListener();
+		    StartPhaseListener theListener7 = new StartPhaseListener();
+		    
+		    NRCopyFragDoneListener theListener8 = new NRCopyFragDoneListener();
+		    
+		    System.out.println("Registering listener");
+		    manager.registerListener(theListener1);
+		    manager.registerListener(theListener2);
+		    manager.registerListener(theListener3);
+		    manager.registerListener(theListener4);
+		    manager.registerListener(theListener5);
+		    manager.registerListener(theListener6);
+		    manager.registerListener(theListener7);
+		    manager.registerListener(theListener8);
+		    System.out.println("Done");
+		    
+		    ClusterState cs = mgm.getStatus();
+		    for(int i=0; i< cs.getNoOfNodes(); i++) {
 			NodeState ns = cs.getNodeState(i);
 			NodeType type= ns.getNodeType();			
 			System.out.println("Type: " + type.toString() + 
@@ -59,24 +94,36 @@
 					   + (short)(ns.getVersion()>>8 & 0xFF)
 					   + "." 
 					   + (short)(ns.getVersion()>>0 & 0xFF)
-					   + " connected address " +  ns.getConnectAddress());
-
-		    }
-				
-		while(true) { 	
-
-			manager.pollEvents(5000); 
-		}
-
+					   + " connected address " 
+					   +  ns.getConnectAddress());
+			
+		    }
+		    /**
+		     * get events until the connection
+		     * to the management server fails.
+		     * If it does, then we have to 
+		     * exit (break) the loop and reconnect.
+		     */
+		    while(true) { 	
+			try {
+			    manager.pollEvents(5000); 
+			    
+			} catch(NdbMgmException e){
+			    // break and then reconnect
+			    break; 				    
+			}
+		    }
+		    
+	    } catch(NdbMgmException e){
+		System.out.println("Something happened");
+		
+	    }
 	}
-
+    }
 }
 
 class TransReportListener extends TransReportCountersTypeListener {
 
-
-
-
 	/* (non-Javadoc)
 	 * @see
com.mysql.cluster.mgmj.NdbLogEventCategoryListener#handleEvent(com.mysql.cluster.mgmj.NdbLogEvent)
 	 */
@@ -100,10 +147,6 @@
 
 
 class DisconnectListener extends DisconnectedTypeListener {
-
-
-
-
 	/* (non-Javadoc)
 	 * @see
com.mysql.cluster.mgmj.NdbLogEventCategoryListener#handleEvent(com.mysql.cluster.mgmj.NdbLogEvent)
 	 */
@@ -128,9 +171,6 @@
 
 class CommunicationOpenedListener extends CommunicationOpenedTypeListener {
 
-
-
-
 	/* (non-Javadoc)
 	 * @see
com.mysql.cluster.mgmj.NdbLogEventCategoryListener#handleEvent(com.mysql.cluster.mgmj.NdbLogEvent)
 	 */
@@ -144,45 +184,46 @@
 
 class CommunicationClosedListener extends CommunicationClosedTypeListener {
 
-
-
 	/* (non-Javadoc)
 	 * @see
com.mysql.cluster.mgmj.NdbLogEventCategoryListener#handleEvent(com.mysql.cluster.mgmj.NdbLogEvent)
 	 */
-	@Override
+    @Override
 	public void handleEvent(CommunicationClosed event) {
-		System.out.println("Comm closed to node:" + event.getNode());
-	} 
+	System.out.println("Comm closed to node:" + event.getNode());
+    } 
 }
 
 
 class NodeFailReportedListener extends NodeFailReportedTypeListener {
-
-
-
-
-
-	/* (non-Javadoc)
-	 * @see
com.mysql.cluster.mgmj.NdbLogEventCategoryListener#handleEvent(com.mysql.cluster.mgmj.NdbLogEvent)
-	 */
-	@Override
+    /* (non-Javadoc)
+     * @see
com.mysql.cluster.mgmj.NdbLogEventCategoryListener#handleEvent(com.mysql.cluster.mgmj.NdbLogEvent)
+     */
+    @Override
 	public void handleEvent(NodeFailReported event) {
-		System.out.println("Failed node:" + event.getFailedNode());
-	} 
-}
-
+	System.out.println("Failed node:" + event.getFailedNode());
+    } 
+}
+
+
+class NRCopyFragDoneListener extends NRCopyFragDoneTypeListener
+{
+    @Override
+	public void handleEvent(NRCopyFragDone event) {
+	System.out.println("Node " + event.getSourceNodeId() +
+			   ": done copying table " + event.getTableId() +
+			   " of fragment: " + event.getFragmentId() + 
+			   " to node: " +  event.getDestNode());	
+    }
+}
 
 
 class StartPhaseListener extends StartPhaseCompletedTypeListener {
-
-
-
 	/* (non-Javadoc)
 	 * @see
com.mysql.cluster.mgmj.NdbLogEventCategoryListener#handleEvent(com.mysql.cluster.mgmj.NdbLogEvent)
 	 */
-	@Override
+    @Override
 	public void handleEvent(StartPhaseCompleted event) {
-		System.out.println("Node " + event.getSourceNodeId() );
-		//				   + ": start phase" + theEvent.getPhase());
-	} 
+	System.out.println("Node " + event.getSourceNodeId()  + 
+			   ": start phase" + event.getPhase());
+    } 
 }

Thread
Rev 419: From johan: "I have also updated the SampleMGMJ to show how to code it for "redundancy" so it can handle multiple management servers. Basical...Monty Taylor23 Jan