יום חמישי, 27 ביוני 2013

Wcf discovery high cpu consumption problem

I have encountered a critical problem regarding wcf discovery.
The following WCF code used to try to find services and announce about my services.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.ServiceModel;
  5: using System.ServiceModel.Discovery;
  6: using System.Text;
  7: using System.Threading.Tasks;
  8: 
  9: namespace WpfApplication1
 10: {
 11:     public class OperatorServicesAccessor
 12:     {
 13: 
 14:         private AnnouncementService mAnnouncementService;
 15:         private ServiceHost mAnnouncementServiceHost;
 16:         UdpAnnouncementEndpoint mUdpAnnouncementEndpoint;
 17: 
 18:         private static OperatorServicesAccessor mOperatorServicesAccessor = new OperatorServicesAccessor();
 19: 
 20:         public static OperatorServicesAccessor Instance()
 21:         {
 22:             return mOperatorServicesAccessor;
 23:         }
 24: 
 25:         private OperatorServicesAccessor()
 26:         {
 27: 
 28:         }
 29: 
 30:         public void Init()
 31:         {
 32: 
 33:             StartScanForeOperatorEntities();
 34: 
 35:             OpenAnnouncementService();
 36:         }
 37: 
 38:         /// <summary>
 39:         /// When the client recevied non aplication exception it close the channel 
 40:         /// and wait for the service to start itself 
 41:         /// </summary>
 42:         /// <param name="pFaultedChannel"></param>
 43:         /// <param name="pEx"></param>
 44:         public void ReportAboutFaultedClient(object pFaultedChannel, Exception pEx)
 45:         {
 46: 
 47:             if (pFaultedChannel == null)
 48:             {
 49:                 return;
 50:             }
 51: 
 52:             if (pEx is ProtocolException)
 53:             {
 54: 
 55:             }
 56: 
 57:             if (pEx is CommunicationException)
 58:             {
 59: 
 60:             }
 61: 
 62:         }
 63: 
 64:         void mDiscoveryClient_FindCompleted(object sender, FindCompletedEventArgs e)
 65:         {
 66:             try
 67:             {
 68: 
 69:                 switch (e.UserState.ToString())
 70:                 {
 71:                     case "IOperator":
 72: 
 73:                         foreach (EndpointDiscoveryMetadata nextEndpointDiscoveryMetadata in e.Result.Endpoints)
 74:                         {
 75:                             EndpointAddress theAddress = nextEndpointDiscoveryMetadata.Address;
 76: 
 77:                             return;
 78:                         }
 79:                         break;
 80:                 }
 81:             }
 82:             catch (Exception ee)
 83:             {
 84: 
 85:             }
 86:             finally
 87:             {
 88:                 ((DiscoveryClient)sender).Close();
 89:             }
 90: 
 91:         }
 92: 
 93:         private void StartScanForeOperatorEntities()
 94:         {
 95: 
 96:             UdpDiscoveryEndpoint mUdpDiscoveryEndpoint = new UdpDiscoveryEndpoint();
 97: 
 98:             DiscoveryClient mDiscoveryClient = new DiscoveryClient(mUdpDiscoveryEndpoint);
 99: 
100:             mDiscoveryClient.FindCompleted += new EventHandler<FindCompletedEventArgs>(mDiscoveryClient_FindCompleted);
101: 
102:             FindCriteria criteria = new FindCriteria(typeof(IOperator));
103: 
104:             criteria.Duration = TimeSpan.FromSeconds(5);
105: 
106:             mDiscoveryClient.FindAsync(criteria, "IOperator");
107: 
108:         }
109: 
110:         private void OpenAnnouncementService()
111:         {
112:             mAnnouncementService = new AnnouncementService();
113:             // Add event handlers
114: 
115:             mAnnouncementService.OnlineAnnouncementReceived +=
116:                 new EventHandler<AnnouncementEventArgs>(this.OnOnlineAnnouncement);
117:             mAnnouncementService.OfflineAnnouncementReceived +=
118:                 new EventHandler<AnnouncementEventArgs>(this.OnOfflineAnnouncement);
119: 
120: 
121:             // Create the service host with a singleton
122:             mAnnouncementServiceHost = new ServiceHost(mAnnouncementService);
123:             // Add the announcement endpoint
124:             mUdpAnnouncementEndpoint = new UdpAnnouncementEndpoint();
125: 
126:             mAnnouncementServiceHost.AddServiceEndpoint(mUdpAnnouncementEndpoint);
127:             // Open the host async
128:             mAnnouncementServiceHost.Open();
129:         }
130:         /// <summary>
131:         /// 
132:         /// Get a notification from a cell that its power up 
133:         /// </summary>
134:         /// <param name="sender"></param>
135:         /// <param name="e"></param>
136:         private void OnOnlineAnnouncement(object sender, AnnouncementEventArgs e)
137:         {
138: 
139:         }
140: 
141:         private void OnOfflineAnnouncement(object sender, AnnouncementEventArgs e)
142:         {
143: 
144:         }
145:     }
146: }
147: 

A standard code for the mission of searching for services and registering  for hello goodbye announcements  .
When I execute the code the CPU consumption of the application is climbing and stay high for a while.
I found out that the WCF infrastructure generate a lot of exceptions after executing the code .
 
Capture12


The visual studio debugger doesn't break in any exception so I try to figure out the exceptions source using windbg.


Using WinDBG
loading  sos :
.loadby sos clr
configure to catache the exceptions :
sxe clr


Then allot of exceptions started to  arrive:
(1298.1df8): CLR exception - code e0434352 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
KERNELBASE!RaiseException+0x54:
7579b760 c9              leave


Investigate the exceptions:
sxe 0xe0434352

0:000> !PrintException
Exception object: 01e45768
Exception type:   System.ArgumentException
Message:          The 'result' argument must be of type Message.
InnerException:   <none>
StackTrace (generated):
    SP       IP       Function
    0020ECC8 519CD54D System_ServiceModel_ni!System.ServiceModel.Dispatcher.MessageOperationFormatter.SerializeReply(System.ServiceModel.Channels.MessageVersion, System.Object[], System.Object)+0x336c31
    0020ECE4 50D2907F System_ServiceModel_ni!System.ServiceModel.Dispatcher.DispatchOperationRuntime.SerializeOutputs(System.ServiceModel.Dispatcher.MessageRpc ByRef)+0x8f
    0020ED44 50D26C42 System_ServiceModel_ni!System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(System.ServiceModel.Dispatcher.MessageRpc ByRef)+0x562

0:000> !CLRStack
OS Thread Id: 0x1df8 (0)
Child SP       IP Call Site
0020ec0c 7579b760 [HelperMethodFrame: 0020ec0c]
0020ec7c 50d26cbf *** WARNING: Unable to verify checksum for C:\Windows\assembly\NativeImages_v4.0.30319_32\System.ServiceModel\23288b62c0dc0d181c1ffc0614644b6d\System.ServiceModel.ni.dll
*** ERROR: Module load completed but symbols could not be loaded for C:\Windows\assembly\NativeImages_v4.0.30319_32\System.ServiceModel\23288b62c0dc0d181c1ffc0614644b6d\System.ServiceModel.ni.dll
System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(System.ServiceModel.Dispatcher.MessageRpc ByRef)
0020eda8 50d26582 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(System.ServiceModel.Dispatcher.MessageRpc ByRef)
0020ede0 50d261eb System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(System.ServiceModel.Dispatcher.MessageRpc ByRef)
0020edf4 50d25067 System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean)
0020ee40 50d33dcd System.ServiceModel.Dispatcher.MessageRpc+Wrapper.Resume(Boolean ByRef)
0020ee60 51484d49 System.ServiceModel.Dispatcher.ThreadBehavior.ResumeProcessing(System.ServiceModel.Dispatcher.IResumeMessageRpc)
0020ee7c 51484c26 System.ServiceModel.Dispatcher.ThreadBehavior.SynchronizationContextStartCallback(System.Object)
0020ee84 646077ef *** WARNING: Unable to verify checksum for C:\Windows\assembly\NativeImages_v4.0.30319_32\WindowsBase\4692c27dd31276c56a76ec0da8d3e453\WindowsBase.ni.dll
*** ERROR: Module load completed but symbols could not be loaded for C:\Windows\assembly\NativeImages_v4.0.30319_32\WindowsBase\4692c27dd31276c56a76ec0da8d3e453\WindowsBase.ni.dll
System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
0020eea4 6460768a MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
0020eee8 645f2d5c System.Windows.Threading.DispatcherOperation.InvokeImpl()


The cause of the exceptions is the WCF infrastructure !!!!
Configure the application  or logging the messages :


  1: <?xml version="1.0" encoding="utf-8" ?>
  2: <configuration>
  3:     <startup> 
  4:         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  5:     </startup>
  6:   <system.serviceModel>
  7: 
  8: 
  9:     <diagnostics>
 10:       <!-- Enable Message Logging here -->
 11:       <!-- log all messages received or sent at the transport or service model levels -->
 12:       <messageLogging logEntireMessage="true" maxMessagesToLog="300" logMessagesAtServiceLevel="true" logMalformedMessages="true" logMessagesAtTransportLevel="true"/>
 13:     </diagnostics>
 14: 
 15:   </system.serviceModel>
 16: 
 17:   <system.diagnostics>
 18:     <sources>
 19:       <source name="System.ServiceModel" switchValue="Information,ActivityTracing" propagateActivity="true">
 20:         <listeners>
 21:           <add name="xml"/>
 22:         </listeners>
 23:       </source>
 24:       <source name="System.ServiceModel.MessageLogging">
 25:         <listeners>
 26:           <add name="xml"/>
 27:         </listeners>
 28:       </source>
 29:     </sources>
 30:     <sharedListeners>
 31:       <add initializeData="C:\logs\SimulatorGuiB10.svclog" type="System.Diagnostics.XmlWriterTraceListener" name="xml"/>
 32:     </sharedListeners>
 33:     <trace autoflush="true"/>
 34:   </system.diagnostics>
 35: </configuration>


Inspect the  log :Capture13 The message with Action 'http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/fault' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver.  Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None). 

I understand that there  is  contract mismatch


So I checked the address of the Discovery endpoint and the Announcement endpoint


Capture14


Capture15


The contract is different between them  but the address is the same .
Resolving the problem:
Set the address manually to be different solve the problem:


  1: private void OpenAnnouncementService()
  2:         {
  3:             mAnnouncementService = new AnnouncementService();
  4:             // Add event handlers
  5: 
  6:             mAnnouncementService.OnlineAnnouncementReceived +=
  7:                 new EventHandler<AnnouncementEventArgs>(this.OnOnlineAnnouncement);
  8:             mAnnouncementService.OfflineAnnouncementReceived +=
  9:                 new EventHandler<AnnouncementEventArgs>(this.OnOfflineAnnouncement);
 10: 
 11: 
 12:             // Create the service host with a singleton
 13:             mAnnouncementServiceHost = new ServiceHost(mAnnouncementService);
 14:             // Add the announcement endpoint
 15:             mUdpAnnouncementEndpoint = new UdpAnnouncementEndpoint();
 16: 
 17:             mUdpAnnouncementEndpoint.MulticastAddress = new Uri("soap.udp://239.255.255.251:3702/");
 18: 
 19:             mAnnouncementServiceHost.AddServiceEndpoint(mUdpAnnouncementEndpoint);
 20:             // Open the host async
 21:             mAnnouncementServiceHost.Open();
 22:         }



 

Write and read from mongodb using spring integration

I extend the spring integration demo:Samples (Basic) – MongoDb and add a read capabilities to the MongoDbOutboundAdapterDemo class.

The spring context

  1: <?xml version="1.0" encoding="UTF-8"?>
  2: <beans xmlns="http://www.springframework.org/schema/beans"
  3:   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4:   xmlns:int="http://www.springframework.org/schema/integration"
  5:   xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
  6:   xmlns:mongo="http://www.springframework.org/schema/data/mongo"
  7:         xmlns:beans="http://www.springframework.org/schema/beans"
  8:   xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
  9:     http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
 10:     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 11:                 http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
 12: 
 13:   <mongo:db-factory id="mongoDbFactory" dbname="test"/>
 14: 
 15:   <int-mongodb:outbound-channel-adapter id="deafultAdapter" />
 16:       
 17:   <int-mongodb:outbound-channel-adapter id="adapterWithConverter" mongo-converter="stringConverter"/>
 18: 
 19:   <int-mongodb:inbound-channel-adapter id="simpleInboundAdapter" channel="outputChannel" 
 20:                        query="{address.state : 'CA'}">    
 21:             <int:poller fixed-rate="1000" max-messages-per-poll="1"/>
 22:   </int-mongodb:inbound-channel-adapter>
 23: 
 24:     <int:publish-subscribe-channel id="outputChannel">
 25:     </int:publish-subscribe-channel>
 26:  </beans>
 27: 

 The code


  1: package org.springframework.integration.samples.mongodb.outbound;
  2: 
  3: import org.springframework.context.ApplicationContext;
  4: import org.springframework.context.support.FileSystemXmlApplicationContext;
  5: import org.springframework.integration.Message;
  6: import org.springframework.integration.MessageChannel;
  7: import org.springframework.integration.core.MessageHandler;
  8: import org.springframework.integration.core.SubscribableChannel;
  9: import org.springframework.integration.message.GenericMessage;
 10: import org.springframework.integration.samples.mongodb.domain.Address;
 11: import org.springframework.integration.samples.mongodb.domain.Person;
 12: import org.springframework.integration.samples.mongodb.util.DemoUtils;
 13: /**
 14: *
 15: * @author of the orginal source Oleg Zhurakousky
 16: * @Add reading Zvika Peer 
 17: */
 18: public class MongoDbOutboundAdapterDemo {
 19: 
 20: 
 21:   public static void main(String[] args) throws Exception {
 22:     DemoUtils.prepareMongoFactory(); // will clean up MOngoDb
 23:     new MongoDbOutboundAdapterDemo().runDefaultAdapter();
 24:   }
 25: 
 26:   public void runDefaultAdapter() throws Exception {
 27: 
 28:                 ApplicationContext context  =
 29:         new FileSystemXmlApplicationContext(
 30:                 "C:\\learn\\spring\\integration\\samples\\basic\\mongodb\\src\\main\\java\\org\\springframework\\integration\\samples\\mongodb\\outbound\\mongodb-out-config.xml"
 31:                         );
 32:                         
 33:       MessageChannel messageChannel = context.getBean("deafultAdapter", MessageChannel.class);
 34:       messageChannel.send(new GenericMessage<Person>(this.createPersonA()));
 35:       messageChannel.send(new GenericMessage<Person>(this.createPersonB()));
 36:       messageChannel.send(new GenericMessage<Person>(this.createPersonC()));
 37: 
 38:              SubscribableChannel outputChannel = context.getBean("outputChannel", SubscribableChannel.class);
 39: 
 40:             outputChannel.subscribe(new MessageHandler()
 41:             {
 42:                 @Override
 43:                 public void handleMessage(Message<?> msg)
 44:                 {
 45:                    System.out.println("==> HelloWorldDemo Async message: " + msg.getPayload());
 46:                 }
 47:             });
 48:                 
 49:               Thread.sleep(4000);
 50:             System.out.println("Finished");
 51:         }
 52:   }

The result
Finished
==> HelloWorldDemo Async message: [{ "_id" : { "$oid" : "51c945b3fe3a5df7ab5f7a3d"} , "_class" : "org.springframework.integration.samples.mongodb.domain.Person" , "fname" : "John" , "lname" : "Doe" , "address" : { "street" : "3401 Hillview Ave" , "city" : "Palo Alto" , "zip" : "94304" , "state" : "CA"}}, { "_id" : { "$oid" : "51c945b3fe3a5df7ab5f7a3e"} , "_class" : "org.springframework.integration.samples.mongodb.domain.Person" , "fname" : "Josh" , "lname" : "Doe" , "address" : { "street" : "123 Main st" , "city" : "San Francisco" , "zip" : "94115" , "state" : "CA"}}]


Resources:
http://static.springsource.org/spring-integration/reference/html/mongodb.html

Start handle the searching in ROR

The modal

  1: class SearchFor < ActiveRecord::Base
  2:   attr_accessible :pharse
  3: end
  4: 

The controller

  1: class OperatorsController < ApplicationController
  2:   # GET /operators
  3:   # GET /operators.json
  4:   def index
  5:     @operators = Operator.all
  6:     @searchP  = SearchFor.new 
  7:   @searchP.pharse = "zvika"
  8:   
  9:     respond_to do |format|
 10:       format.html # index.html.erb
 11:       format.json { render json: @operators }
 12:     
 13:   end
 14:   end

The view

  1: <h2>Search for operator:</h2>
  2: 
  3:  <div class="field">
  4:     <p>The search pharse: <%= @searchP.pharse%><br /></p>
  5:  </div>
  6: 

The result
http://localhost:3000/operators
Capture41

render a simple view in yii

Changes in the SiteController.php:
Add

  1: 
  2:   public function actionOperators()
  3:   {
  4:     $emodel=new OperatorModel;
  5:     
  6:     $model->Name = "zvika";
  7:     
  8:     // display the login form
  9:     $this->render('pages/Operators',array('model'=>$model));
 10:   }

The simple operator view:


  1: <?php
  2: /* @var $this SiteController */
  3: 
  4: $this->pageTitle=Yii::app()->name . ' - Operators';
  5: $this->breadcrumbs=array(
  6:   'Operators',
  7: );
  8: ?>
  9: <h1>Operators</h1>
 10: 
 11:  <?php echo $model->Name; ?>
 12:  
 13:  <?php echo CHtml::activeLabel($model,'Name'); ?>
 14:  

The operator model


  1: <?php
  2: class OperatorModel extends CFormModel
  3: {
  4:     public $Name;
  5: }
  6: ?>

Add the menu item to the main.php


  1: <div id="mainmenu">
  2:     <?php $this->widget('zii.widgets.CMenu',array(
  3:       'items'=>array(
  4:         array('label'=>'Home', 'url'=>array('/site/index')),
  5:         array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
  6:           array('label'=>'Operators', 'url'=>array('/site/Operators')),
  7:         array('label'=>'Contact', 'url'=>array('/site/contact')),
  8:         array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
  9:         array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
 10:       ),
 11:     )); ?>

The result :
Capture42

Outer join in cyper

The following code :

  1: def handle_row(row):
  2:     print ("cypher query result:")
  3:     print ( len (row))
  4:     for nextNode in row:
  5:         print (nextNode)
  6:         print (type(nextNode))
  7: 
  8: 
  9: theCyperCode = "START n=node(1) ,other=node(2, 3)  RETURN  n,other"
 10: 
 11: cypher.execute(graph_db, theCyperCode, row_handler=handle_row)

returns
cypher query result:
2
(1 {"name":"Bruce Willis"})
<class 'py2neo.neo4j.Node'>
(2 {"name":"John McClane"})
<class 'py2neo.neo4j.Node'>
cypher query result:
2
(1 {"name":"Bruce Willis"})
<class 'py2neo.neo4j.Node'>
(3 {"name":"Alan Rickman"})
<class 'py2neo.neo4j.Node'>
Start