ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » General IBM MQ Support » MQCB multi threaded

Post new topic  Reply to topic Goto page 1, 2  Next
 MQCB multi threaded « View previous topic :: View next topic » 
Author Message
yasaboy
PostPosted: Tue Jul 01, 2014 8:31 pm    Post subject: MQCB multi threaded Reply with quote

Voyager

Joined: 23 Jun 2014
Posts: 90

HI,

Suppose there are 100 queues in a single Queue Manager. I want to implement MQCB's for all those queues. And to make the process parallel I have 5 threads. So rather than using a single thread for all the 100 queue CallBacks, I want to distribute the MQCB process into these 5 threads as 20 Queues per thread.

So my question is what is the best way to do it?

Could I use a single MQHCONN // connection handler // among all the five threads or need 5 separate handlers for each thread.

Thanks
Back to top
View user's profile Send private message
PaulClarke
PostPosted: Wed Jul 02, 2014 12:44 am    Post subject: Reply with quote

Grand Master

Joined: 17 Nov 2005
Posts: 1002
Location: New Zealand

Well, there are a number of ways you can do this but the key thing to realise is that if you want to do 5 things at the same time then you need 5 MQHCONNs. There are lots of advantages of using MQCB but perhaps the key on is that you can listen on multiple queues at the same time. It is not designed to be a workload scheduler though.

One decision you need to make is whether you need to process messages in order from the queues. If you do then the simplest way is to open 20 queues in each MQHCONN. If you don't then you could open all 100 queues in each MQHCONN. This would maximise your chances of parallelism so you could, potentially, be processing 5 messages from the same queue on all 5 MQHCONNs.

An alternative approach could be to use the MDB method. You use MQCB to browse the 100 queues in a single MQHCONN. You then design your own dispatcher to then process the message using a 'get message by token'. Using this method you can do whatever dispatching algorithm fits your application best. Although it is clearly a little more complicated.

Hope this helps,

P.
_________________
Paul Clarke
MQGem Software
www.mqgem.com
Back to top
View user's profile Send private message Visit poster's website
yasaboy
PostPosted: Sat Jul 05, 2014 5:29 am    Post subject: Reply with quote

Voyager

Joined: 23 Jun 2014
Posts: 90

PaulClarke wrote:
Well, there are a number of ways you can do this but the key thing to realise is that if you want to do 5 things at the same time then you need 5 MQHCONNs. P.


What is the way to define multiple MQHCONN's dynamically in the code. Suppose there are three threads so I want to create 3 handlers in the code.

The way that I am currently doing it is like this which is embarrassing. Please help. Thanks

Code:
MQHCONN  Hcon1;     
MQHCONN  Hcon2;     

if(THREADID ==1 )
        strcpy(QMName,".QM.TEST1");
   MQCONN(QMName, &Hcon1, &CompCode, &CReason);
if(THREADID ==2)
        strcpy(QMName,".QM.TEST1");
   MQCONN(QMName, &Hcon2, &CompCode, &CReason);
Back to top
View user's profile Send private message
PaulClarke
PostPosted: Sat Jul 05, 2014 5:51 am    Post subject: Reply with quote

Grand Master

Joined: 17 Nov 2005
Posts: 1002
Location: New Zealand

With such a small amount of code it's hard to see quite what you are doing. However, I suspect that this code is inside some top level function which means your MQHCONN variables are being defined on the stack. As such you can just change you code to....

Code:

void MyTopLevelThreadFunction(...)
{
  MQHCONN  Hcon;     

  MQCONN(QMName, &Hcon, &CompCode, &CReason);

  <do some MQ stuff>

  MQDISC(&Hconn,&CompCode,&Reason);
}



Since each thread will have it's own stack then you will have multiple instantiations of Hcon. It makes sense (probably) that QMName is the same for all threads and are initialised before the threads are invoked.

Of course the other way to do this is to allocate a structure on the heap. This is the usual way of doing something like this if the application is in any way complicated. By allocating a structure on the heap then you can keep all the state for that thread easily addressable off a single pointer.

Cheers,
Paul.
_________________
Paul Clarke
MQGem Software
www.mqgem.com
Back to top
View user's profile Send private message Visit poster's website
yasaboy
PostPosted: Sat Jul 05, 2014 6:04 am    Post subject: Reply with quote

Voyager

Joined: 23 Jun 2014
Posts: 90

PaulClarke wrote:

Code:

void MyTopLevelThreadFunction(...)
{
  MQHCONN  Hcon;     

  MQCONN(QMName, &Hcon, &CompCode, &CReason);

  <do some MQ stuff>

  MQDISC(&Hconn,&CompCode,&Reason);
}




Hi,
The code you have added above is exactly the same now I am using. But the thing is when I use only one "Hcon" it gives a segment fault as below

Code:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffe3fff700 (LWP 29357)]
0x00007ffff435eecc in xcsLoadFunction () from /opt/mqm/lib64/libmqe.so
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.107.el6.x86_64 keyutils-libs-1.4-4.el6.x86_64 krb5-libs-1.10.3-10.el6.x86_64 libaio-0.3.107-10.el6.x86_64 libcom_err-1.41.12-14.el6.x86_64 libgcc-4.4.7-3.el6.x86_64 libselinux-2.0.94-5.3.el6.x86_64 libstdc++-4.4.7-3.el6.x86_64 libxml2-2.7.6-8.el6_3.4.x86_64 nss-softokn-freebl-3.12.9-11.el6.x86_64 numactl-2.0.7-6.el6.x86_64 openssl-1.0.0-27.el6.x86_64 zlib-1.2.3-29.el6.x86_64
(gdb) bt
#0  0x00007ffff435eecc in xcsLoadFunction () from /opt/mqm/lib64/libmqe.so
#1  0x00007ffff42e9e19 in InitProcessInitialisation () from /opt/mqm/lib64/libmqe.so
#2  0x00007ffff42ebde9 in xcsInitializeEx () from /opt/mqm/lib64/libmqe.so
#3  0x00007ffff44706a4 in zswMQCONN_Call () from /opt/mqm/lib64/libmqe.so


But when I use different handler names as Hcon1 , Hcon2 for each thread it works fine. I have been stuck on this for days now. Please help.
Back to top
View user's profile Send private message
PaulClarke
PostPosted: Sat Jul 05, 2014 6:17 am    Post subject: Reply with quote

Grand Master

Joined: 17 Nov 2005
Posts: 1002
Location: New Zealand

Well, it's rather hard for me to guess exactly what is wrong since I don't have access to the MQ code nor your application code. However, I would suspect this problem was more environmental than just a simple case of coding the MQCONN call differently.

So, if it were me I would be checking that I am definitely building the code as 64bit using a 64bit compiler. That I am compiling as a threaded program and that I am linked with the MQ threaded libraries. Those kinds of things. It does seem suspicious that you've gone bang in xcsLoadFunction, this is down where MQ is dynamically loading one of it's low libraries. It suggests that perhaps the environment you are running is not suitable for whatever is being loaded.

Cheers,
Paul.
_________________
Paul Clarke
MQGem Software
www.mqgem.com
Back to top
View user's profile Send private message Visit poster's website
yasaboy
PostPosted: Sat Jul 05, 2014 8:29 am    Post subject: Reply with quote

Voyager

Joined: 23 Jun 2014
Posts: 90

@PaulClarke, thanks mate Ill look into that.
Back to top
View user's profile Send private message
yasaboy
PostPosted: Tue Jul 08, 2014 7:30 pm    Post subject: Reply with quote

Voyager

Joined: 23 Jun 2014
Posts: 90

Hi,

Sorry for reopening the topic. I could resolve the above problem after some effort. I have some more clarifications Tobe made regarding multithreaded behavior.


Code:
void WorkerThread::OnRun()
{
    MQCONN(QMName, &Hcon, &CompCode, &CReason);
    MQOPEN(Hcon,&od,O_options,&Hobj,&CompCode,&Reason);
    [b]cbd.CallbackFunction = (void*)MessageConsumer;[/b]
    gmo.Options = MQGMO_NO_SYNCPOINT;
   MQCB(Hcon, MQOP_REGISTER, &cbd, Hobj, &md, &gmo, &CompCode,  &Reason);
   MQCTL(Hcon, MQOP_START, &ctlo, &CompCode, &Reason);

}


This is the function that multiple threads access to make callbacks. and the MessageConsumer function is defined out of the class globally.

Code:
void MessageConsumer(MQHCONN   hConn,
                      MQMD    * pMsgDesc,
                      MQGMO   * pGetMsgOpts,
                      MQBYTE  * Buffer,
                      MQCBC   * pContext)
{

}


like this. So my question is does this make the function per thread or is each thread access the same function sequentially ?
Back to top
View user's profile Send private message
PaulClarke
PostPosted: Tue Jul 08, 2014 8:55 pm    Post subject: Reply with quote

Grand Master

Joined: 17 Nov 2005
Posts: 1002
Location: New Zealand

I should say that I am not an expert in C++ nor in what we've implemented for MQCB in C++. So, my comments are referring to the architectural design and what we've done for the C language.

As far as threading is concerned a callback function is sequential with other callbacks for the same MQHCONN. In other words the same callback function used by different MQHOBJs for the same MQHCONN will never be invoked multiple times at the same instant. However, if you register the same callback, even for the same queue, but on different MQHCONNs then the callback function could be invoked multiple times at the same instance. You can never have more threads going on (i.e threading) than you have MQHCONNs.

So then the questions comes down to what is the relationship between MQHCONNs and callback threads. For local bindings it is simple, it is 1 for 1. Each MQHCONN will have a thread for calling consumers. For the client it is a little more complicated. By default, consumer threads will come in and out of existence as 'work' demands, and they are shared. So, even though you have 3 MQHCONNs. they may all share the same consumer thread and therefore calls to consumer functions across the 3 connections would be serialised. If you want to ensure that each MQHCONN has it's own thread and always uses it (like the local bindings case) then you can use the MQCTLO_THREAD_AFFINITY option.

I hope this helps a little.

Cheers,
Paul.
_________________
Paul Clarke
MQGem Software
www.mqgem.com
Back to top
View user's profile Send private message Visit poster's website
gbaddeley
PostPosted: Wed Jul 09, 2014 3:22 pm    Post subject: Re: MQCB multi threaded Reply with quote

Jedi Knight

Joined: 25 Mar 2003
Posts: 2538
Location: Melbourne, Australia

yasaboy wrote:
HI,

Suppose there are 100 queues in a single Queue Manager. I want to implement MQCB's for all those queues. And to make the process parallel I have 5 threads. So rather than using a single thread for all the 100 queue CallBacks, I want to distribute the MQCB process into these 5 threads as 20 Queues per thread.

So my question is what is the best way to do it?

Could I use a single MQHCONN // connection handler // among all the five threads or need 5 separate handlers for each thread.

Thanks


How about using 100 alias queues that resolve to a single local queue? You can then consume from them all in one thread without using MQCB. Or was that not the point of your question?
_________________
Glenn
Back to top
View user's profile Send private message
bruce2359
PostPosted: Wed Jul 09, 2014 3:28 pm    Post subject: Reply with quote

Poobah

Joined: 05 Jan 2008
Posts: 9469
Location: US: west coast, almost. Otherwise, enroute.

The object handle is to the resolved name.
_________________
I like deadlines. I like to wave as they pass by.
ב''ה
Lex Orandi, Lex Credendi, Lex Vivendi. As we Worship, So we Believe, So we Live.
Back to top
View user's profile Send private message
yasaboy
PostPosted: Wed Jul 09, 2014 8:33 pm    Post subject: Re: MQCB multi threaded Reply with quote

Voyager

Joined: 23 Jun 2014
Posts: 90

@ GrandmasterI am not too much familiar with the alias queue concept even though I read it some where. Anyway I could make the prototype working to the above configuration of mine using 5 threads, 5 connection handlers and 10 queues for a staring .
[quote="gbaddeley"]
yasaboy wrote:
HI,
How about using 100 alias queues that resolve to a single local queue? You can then consume from them all in one thread without using MQCB. Or was that not the point of your question?


Anyway now I have some more problems. Here are my thread related statistics of MQCB's for 10 second time stamp.
Code:

============= MQCB Thread Statistics ===============
Message Counter is 13795
Thread1 Counter is 4605
Thread2 Counter is 4922
Thread3 Counter is 5113
============================================


The count of threads doesn't match with total message count. Is the problem, with me updating Message counter value and some more messages come before I update thread1,2,3 values and they get updated ?
Back to top
View user's profile Send private message
PaulClarke
PostPosted: Wed Jul 09, 2014 8:44 pm    Post subject: Reply with quote

Grand Master

Joined: 17 Nov 2005
Posts: 1002
Location: New Zealand

It's a little hard for us to guess what the problem might be without us seeing your code. The key question I guess is what kind of locking do you use for the 'Message Counter'. If you have 3 threads all updating this value then clearly you need some locking around the update. Unless you use some form of atomic operation of course.

I am a little confused though...you say you use 5 threads and yet you are only report 3.

Cheers,
Paul.
_________________
Paul Clarke
MQGem Software
www.mqgem.com
Back to top
View user's profile Send private message Visit poster's website
yasaboy
PostPosted: Wed Jul 09, 2014 9:11 pm    Post subject: Reply with quote

Voyager

Joined: 23 Jun 2014
Posts: 90

Hi Paul,

PaulClarke wrote:
Im a little confused though...you say you use 5 threads and yet you are only report 3.

Cheers,
Paul.


Even though I have 5 threads listening I only pumped messages to only 3 queues registered callbacks in three different threads for the testing purposes. Sorry that you were confused. I only checked the functionality using 3 of the 5 threads that's why the above statics.


Anyway thanks for mentioning the locking and I didn't do any silly me. I will look into that.
Back to top
View user's profile Send private message
yasaboy
PostPosted: Tue Oct 28, 2014 11:27 pm    Post subject: Delete Message while waiting in a MQCB Reply with quote

Voyager

Joined: 23 Jun 2014
Posts: 90

Hi,

I am waiting in a MQOP_START_WAIT status for a particular thread for a particulate Queue.

I am using MQGMO_BROWSE_NEXT option to get the messages from the queue. So messages are not deleted from it and queue grows.

Suppose at a particular moment I want to clear this Queue. Is there any method that I can use to delete the messages from the queue while waiting in a CallBack in the same thread ?


callback function is,

Code:
gmo.Options = MQGMO_BROWSE_NEXT;
      //cout << " Connected to Input  queue -> " << pzQueueName << endl;
      MQCB(m_Hcon,
         MQOP_REGISTER,
         &cbd,
         pMQSQueue->m_Hobj ,
         &md,
         &gmo,
         &pMQSQueue->m_OpenCode,
         &pMQSQueue->m_Reason);


MQCTL(m_Hcon, MQOP_START_WAIT, &ctlo, &CompCode, &m_Reason);
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Goto page 1, 2  Next Page 1 of 2

MQSeries.net Forum Index » General IBM MQ Support » MQCB multi threaded
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.