How to efficiently get hold of N randomly selected keys out of X?

I am writing some benchmark programs for Coherence caches and would need a way to as efficently as possible get hold of a "fairly random" selection of N keys from the cache out of the total X keys (where X >> N).
With "fairly random" I am in particular looking for a way to get keys somewhat uniformly spread over all partitions.
First i tried picking the first N keys obtained when iterating the key-set but they all seemed to come from just a few partitions (perhaps iteration is performed partition by partition?). Next I tried some algortihms I have used in the past to pick random keys from in memory-map (where one basically iterate and if a randomly generated value between 0.0 and 1.0 exceed a percentage calculatwed as the factor N/X the key is included. This does work (one may occasionally need to iterate the map more than once) but is VERY slow since iterating the key-set of a large cache remotly is kind of expensive.
All sugestions are warmly appreciated - the best would be solutions that do not require anything to be added to the pof-config (i.e. not rely on custome aggregators or invocables etc) - this because I would liek to be able to easilly use my benchmark as part of several applications without having to modify there configuration files!
/Magnus
Edited by: MagnusE on Aug 7, 2009 10:21 AM

Hi Magnus,
you can go the other way round:
With key-association you can ensure that your request goes to the partition you want it to go. You just need to generate a reverse mapping array between partition ids and integer associated key values which are mapped to that partititon.
You can use this method for it:
public static int[] generateReverseAssociatedKeysForService(DistributedCacheService service) {
     KeyPartitioningStrategy keyPartitioningStrategy = service.getKeyPartitioningStrategy();
     int partitionCount = service.getPartitionCount();
     int[] reverseKeys = new int[partitionCount];
     int i=1;
     while (partitionCount > 0) {
          Integer associatedKey = new Integer(i);
          int partitionId = keyPartitioningStrategy.getKeyPartition(associatedKey);
          if (reverseKeys[partitionId] == 0) {
               reverseKeys[partitionId] = i;
               --partitionCount;
          ++i;
     return reverseKeys;
}After this, you generate an equal amount of entries for each partitions with composite keys which implement key association in a way that it returns an integer which maps to your designated partition id to serve as your data. Alternatively you can sort an existing key-set into per-partition keysets.
After this, you just generate evenly spread random numbers between 0 and (partitionCount - 1) (both inclusive) which random number you use as an index to the reverse map to get an associated key value. Then you can generate another random number if you have multiple keys within a partition, or choose a key from the per-partition keyset for that partition if you have a set of existing keys.
This way you get keys which are as evenly spread between partitions as your random generator generating your associated key indexes is spread.
Best regards,
Robert
Edited by: robvarga on Aug 7, 2009 11:55 AM
Added some more ideas and method implementation.

Similar Messages

  • Ipod nano - how do you get rid of auto random select for playlist songs

    theres some strange icon that appears and for some reason selects random songs not the order i want the songs to play. help??
    no idea how to fix this. it works fine on itunes, on my ipod nano it just seems to do random select on all playlists.
    ipod nano   Windows XP  

    iPod Settings > Shuffle to Off.

  • IPhoto has one (random) main photo for everything on top repeatedly with other appropriate photos behind it. How do I get off that one random photo so the real photos appear on top as it should be?

    iPhoto has one same (random) main photo for everything on top repeatedly with other appropriate photos behind it. How do I get off that one random photo so the real photos appear on top as it should be?

    Backup your iPhoto library and hold down the option and command keys while launching iPhoto - hold until the first aid window appears and rebuild thumbnails - it may take a few tries
    LN

  • HT201272 My laptop pc got stolen, how can I get hold of all the itune music I purchased, or at least get a list for insurance?

    My laptop pc got stolen, how can I get hold of all the itune music I purchased, or at least get a list for insurance?

    Yu can try this program.  If unsuccessful, the contents of the iPod will be deleted if yu update/restore with the iPod in recovery mode.
    RecBoot: Easy Way to Put iPhone into Recovery Mode | Jaxov

  • When I try to print a 4X6 photo using Elements 12 it prints out 2X3.  Does anyone know how you can get hold a live person at Adobe?

    When attempting to print a 4X6 photo using Elements 12 it prints out 2X3.  Does anyone know how you can get hold of a live person at Adobe?

    I've never done a forum and am not exactly sure what to do from here.   I
    know how to do a screen shot on my phone, but not on my computer. 
    I'm about ready to throw every adobe product I have out the window!   If
    you purchase a product you should be able to get support from the  company.
    In a message dated 4/6/2015 3:04:02 P.M. US Mountain Standard Time, 
    [email protected] writes:
    When  I try to print a 4X6 photo using Elements 12 it prints out 2X3.  
    Does anyone know how you can get hold a live person at Adobe?
    created by Peru Bob (https://forums.adobe.com/people/Peru+Bob)  in 
    Photoshop Elements - View the full  discussion
    (https://forums.adobe.com/message/7412463#7412463)

  • Recently lost my iphone 4s how can i get hold of my info?

    Hi,
    It's been almost 2 weeks since i accidentally lost my iPhone, it's felt like I've lost my diary! Everything was in my phone, everything I'm in the process of trying to make a claim on my holiday insurance but I'm guessing this could take some time.  I know that when I upgraded to the iPhone 4s that I set up my phone to back up automatically but apart from this I have absolutely no idea how I can get hold of any of my contact details, photo's, calendar etc etc
    How on earth did I cope 15 years ago before I had my first mobile?!
    If anybody can help I'll be very grateful!!
    Waiting patiently with hope & faith,
    Rach

    You may not be able to
    If you set up find my iphone on the iphone itself AND it is on AND it has not been found and restored, then you may be able to locate using icloud.
    Otherwise you cannot track it.

  • How do I get the pictures folders in photoshop 7 out of My window XP computer and put them into my new computer windows 8 photoshop12

    How do I get the pictures folders in photoshop 7 out of My window XP computer and put them into my new computer windows 8 photoshop12

    I am so sorry for you and your situation. Were you using iCloud photostream?
    Otherwise I do not see any chance for your precious photos.
    Congrats anyway on the little darling!

  • HT1311 i tried to do a backup from my old iphone as i have a new one that was signed into another itunes account. it said i cant do it because the iphone rejected the request. how do i get around this? i have signed out of the old acct and into mine on th

    i tried to do a backup from my old iphone as i have a new one that was signed into another itunes account. it said i cant do it because the iphone rejected the request. how do i get around this? i have signed out of the old acct and into mine on the phone

    What is the precise wording of the error message that occurs on the old iPhone when attempting to do a backup?
    Are you attempting to backup via iTunes on the computer or iCloud?

  • How can I get a drop-down list selection also be selected in another field with the same list but a different name?

    I have a street address and a billing address. A question is posed with a checkbox — "Is the billing address the same as the street address?" If Yes is checked, the street address automatically fills the billing fields. If No is checked, the user must fill in new information. In both the street address and billing addres, the State field is a drop-down list. How can I get the drop-down list selection in the street address State also be selected in drop-down list for the billing address State?

    Has anyone done this?

  • How do i get through find my i phone with out knowing the past apple id or password?

    how do i get through find my i phone with out knowing the past apple id or password?

    If the iPhone has Activation Lock - that is, it's asking for the previous owner's Apple ID and password to activate the device - then you will need that information, or you'll need to get the previous owner to remove the device from his/her iCloud account:
    http://support.apple.com/kb/PH2702
    If you can't reach the previous owner or he/she is unwilling to cooperate, then the device is unusable by you. If you recently purchased the iPhone, return it for a refund if you can.
    Regards.

  • How do I get my caps lock and tab keys to work again?

    how do I get my caps lock and tab keys to work again?

    Have a look at > One or more keys on the keyboard do not respond

  • How do I get my HP Printer to select US Letter instead of borderless letter

    How do I get my HP Printer to select US Letter instead of borderless letter?

    Thanks. As I understand it, you want your Print dialog to retain the most recently used settings. That's reasonable and I believe that is the way it is supposed to work.
    You may consider the following a fix or a workaround. In the Print dialog you have the option of configuring your settings and saving them as a preset, as in the following screenshot:
    "Presets" are in the dropdown menu near the top.
    Does this work for you?
    If not, and you are comfortable with creating a new priner queue (which it appears you are, since you already did it trying to troubleshoot) then try this:
    Go to System Preferences > Print & Fax, select your printer by right-clicking the printer (control-click does the same thing). Select "reset printing system" from the menu that appears. Confirm that you know this will eliminate all your printers.
    This may or may not result in different actions than removing and reinstalling the printer as you did.
    Then, add the printer in the usual manner and see if that fixes anything.

  • How can I get hold of the receipt for my Mac bought in NY, Soho

    Hi, does anyone know how or where to get hold of the receipt emailed me when purchased my Mac - trouble with email.

    I have attached here a screenshot of the effect we need to be able to demonstrate. Any ideas for a way of doing something like this in Measurement Studio would be much appreciated.
    Attachments:
    Doc2.doc ‏41 KB

  • How do I get hold of my quicktime Pro registration code if now i'm away

    I'm abroad and away from home. have bought a new mac and want to get qt pro which i paid for already for my old one. however, i don't know how to get hold of my registration code which was sent to me but is in my emails on my old computer. how do get in touch with quicktime themselves?

    Try the instructions in this article.
    (29535)

  • How do I get rid of a random red box?

    there's a random red square on video, it appear while I was adjusting the color. How do I get rid of it?

    alyssag2013 wrote:
    there's a random red square on video, it appear while I was adjusting the color. How do I get rid of it?
    To get rid of the red box, click the Viewer window to select it, then press Shift+Z.
    As Jim pointed out, the red box is to help you navigate when zoomed into the viewer.
    Andy

Maybe you are looking for