Link between partition id and distributed map keys

408 views
Skip to first unread message

dai

unread,
Jan 25, 2011, 6:25:59 AM1/25/11
to Hazelcast, david...@fmr.com
Hi,

Is there a way in Hazelcast to tell which distributed map keys belong
to which partition? So would a hazelcast node be able to listen to the
migration events and once it receives a migration completed event, it
could tell if a partition was migrated to that node and if yes, which
keys of a particular distributed map are in that partition?

Thanks,
David

Talip Ozturk

unread,
Jan 25, 2011, 6:59:25 AM1/25/11
to haze...@googlegroups.com, david...@fmr.com
David,

Hazelcast won't give you all the keys in a particular partition but
you can find out yourself easily.

Here is the code for your question:

PartitionService partitionService = Hazelcast.getPartitionService();
partitionService.addMigrationListener(new MigrationListener () {
public void migrationStarted(MigrationEvent migrationEvent) {
}

public void migrationCompleted(MigrationEvent migrationEvent) {
if (migrationEvent.getNewOwner().localMember()) {
// great! I am the new owner
// find the keys I own right now
Set keys = map.localKeySet();
for (Object key : keys) {
// is this key just migrated?
if
(partitionService.getPartition(key).getPartitionId() ==
migrationEvent.getPartitionId()) {
// super.. this key just
migrated to me!!
}
}
}
}
});


Make sure you read this section:
http://www.hazelcast.com/documentation.jsp#InternalsDistributedMap


http://twitter.com/oztalip

> --
> You received this message because you are subscribed to the Google Groups "Hazelcast" group.
> To post to this group, send email to haze...@googlegroups.com.
> To unsubscribe from this group, send email to hazelcast+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/hazelcast?hl=en.
>
>

dai

unread,
Jan 25, 2011, 9:16:06 AM1/25/11
to Hazelcast, David Kiss
Awesome! And thanks for your quick response!
I must have missed this part in the hazelcast reference.

There's one thing in your solution that's not clear to me (and I hope
I didn't miss it in the reference this time):
Let's say there are 5 distributed maps in a hazelcast cluster and each
of them has a value for Key1. Does this mean that the partition that
owns Key1 will contain all the values for Key1 in the 5 maps?

Or if I rephrase it: are partitions balanced out based on the count of
keys or based on data size in bytes?

D.

Talip Ozturk

unread,
Jan 25, 2011, 9:35:27 AM1/25/11
to haze...@googlegroups.com, David Kiss
> Let's say there are 5 distributed maps in a hazelcast cluster and each
> of them has a value for Key1. Does this mean that the partition that
> owns Key1 will contain all the values for Key1 in the 5 maps?

Yes. Entries with Key1 on each map will fall into the same partition.

This is explained here:
http://www.hazelcast.com/documentation.jsp#KeyBasedDistributedExecution


> Or if I rephrase it: are partitions balanced out based on the count of
> keys or based on data size in bytes?

No.. neither. Hazelcast doesn't care about number of keys or data
size. It cares about how partitions are distributed among members.
Hazelcast contains 271 (default and configurable) virtual partitions.
Hazelcast will try to evenly distribute the partitions. Say you have
10 node cluster then each node will own around 27 partitions.

-talip

dai

unread,
Jan 25, 2011, 9:38:04 AM1/25/11
to Hazelcast
Great, thanks!

D.
Reply all
Reply to author
Forward
0 new messages