All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.partition.PartitionAware Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2008-2024, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.partition;

/**
 * PartitionAware means that data will be based in the same member based on the partition key
 * and implementing tasks will be executed on the {@link #getPartitionKey()}'s owner member.
 * 

* This achieves data affinity. Data and execution occurs on the same partition. *

* In Hazelcast, disparate data structures will be stored on the same partition, * based on the partition key. For example, if "Steve" was used, then the following would be on one partition. *

    *
  • a customers IMap with an entry of key "Steve" *
  • an orders IMap using a customer key type implementing PartitionAware with key "Steve" *
  • any queue named "Steve" *
  • any PartitionAware object with partition key "Steve" *
* * If you have a {@link com.hazelcast.core.IExecutorService} which needs to deal with a customer and a customer's * orders, you can achieve optimal performance by putting them on the same partition. *

* {@link com.hazelcast.core.DistributedObject} also has a notion of the partition key which is of type String * to ensure that the same partition as distributed Objects Strings is used for the partition key. * * @see com.hazelcast.core.DistributedObject * @param key type */ @FunctionalInterface public interface PartitionAware { /** * The key that will be used by Hazelcast to specify the partition. * You should give the same key for objects that you want to be in the same partition. *

* The contract of {@link #getPartitionKey()} method is as follows: *

* Let us define {@code toData(o)} as serialized form of given object obtained by using * Hazelcast Serialization configured for the cluster (the exact method used is * {@link com.hazelcast.internal.serialization.SerializationService#toData SerializationService.toData} * from an internal SPI). *

* Assume {@code PartitionAware a, b} are objects (e.g. IMap keys) and * {@code T pk1 = a.getPartitionKey(), pk2 = b.getPartitionKey()} are partition key values. *

* Then {@link #getPartitionKey()} implementation must obey the following contract: *

    *
  1. (mandatory) Deterministic partitioning: if {@code a.equals(b)} then either * {@code toData(a.getPartitionKey()).equals(toData(b.getPartitionKey()))} * or {@code a.getPartitionKey() == null && b.getPartitionKey() == null}
  2. *
  3. (recommended) Reasonable partitioning: if {@code a.equals(b)} then * {@code a.getPartitionKey().equals(b.getPartitionKey())}
  4. *
  5. (recommended) Reasonable partitioning key serialization (if custom * serialization is used): if {@code pk1.equals(pk2)} then * {@code toData(pk1).equals(toData(pk2))}
  6. *
  7. The above stated conditions must hold when the {@link #getPartitionKey()} * is invoked any number of times on any member or client, regardless of time * (note that some partitioned data structures support persistence) and if the JVM * is restarted, regardless of client/member version and JVM version (across all JVMs * ever used in given deployment), timezone, locale and similar differences in the * environment.
  8. *
* Adhering to the contract guarantees stable partitioning of the data and ability to find * appropriate member and partition who owns given object during querying and modifications. *

* Notes: *

    *
  • {@link #getPartitionKey()} contract is similar to {@link Object#hashCode() hashCode()} * but with stricter long-term requirements.
  • *
  • Partition key is not compared directly, only serialized form is compared or used to * calculate partition id.
  • *
  • For unequal objects {@link #getPartitionKey()} may return the same or different values * according to specific partitioning use case needs.
  • *
* * @return the key that specifies the partition. Returning {@code null} or {@code this} will * cause the result as if the object did not implement {@link PartitionAware}. If the * returned key itself implements {@link PartitionAware}, this fact will be ignored * and the key will be treated as a plain object. */ T getPartitionKey(); }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy