Repository: incubator-ignite Updated Branches: refs/heads/ignite-443 [created] 2cbf4406d
IGNITE-443 Moved CacheConflictResolver interfaces to Ignite. Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/540304db Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/540304db Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/540304db Branch: refs/heads/ignite-443 Commit: 540304dbfc4551cded0ceea4c92c6ccc39ca2d8b Parents: 6b7dcb8 Author: nikolay_tikhonov <ntikho...@gridgain.com> Authored: Tue Mar 10 17:28:32 2015 +0300 Committer: nikolay_tikhonov <ntikho...@gridgain.com> Committed: Tue Mar 10 18:37:13 2015 +0300 ---------------------------------------------------------------------- .../ignite/cache/CacheConflictContext.java | 72 +++++++++++++++++ .../ignite/cache/CacheConflictResolver.java | 34 ++++++++ .../ignite/cache/CacheConflictResolverMode.java | 71 +++++++++++++++++ .../apache/ignite/cache/CacheResolveEntry.java | 81 ++++++++++++++++++++ 4 files changed, 258 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/540304db/modules/core/src/main/java/org/apache/ignite/cache/CacheConflictContext.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/CacheConflictContext.java b/modules/core/src/main/java/org/apache/ignite/cache/CacheConflictContext.java new file mode 100644 index 0000000..7e597d5 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/cache/CacheConflictContext.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.ignite.cache; + +import org.jetbrains.annotations.*; + +/** + * Data center replication receiver cache conflict resolution context. + */ +public interface CacheConflictContext<K, V> { + /** + * Gets old (existing) cache entry. + * + * @return Old (existing) cache entry. + */ + public CacheResolveEntry<K, V> oldEntry(); + + /** + * Gets new cache entry. + * + * @return New cache entry. + */ + public CacheResolveEntry<K, V> newEntry(); + + /** + * Force cache to ignore new entry and leave old (existing) entry unchanged. + */ + public void useOld(); + + /** + * Force cache to apply new entry overwriting old (existing) entry. + * <p> + * Note that updates from remote data centers always have explicit TTL, while local data center + * updates will only have explicit TTL in case {@link CacheEntry#timeToLive(long)} was called + * before update. In the latter case new entry will pick TTL of the old (existing) entry, even + * if it was set through update from remote data center. it means that depending on concurrent + * update timings new update might pick unexpected TTL. For example, consider that three updates + * of the same key are performed: local update with explicit TTL (1) followed by another local + * update without explicit TTL (2) and one remote update (3). In this case you might expect that + * update (2) will pick TTL set during update (1). However, in case update (3) occurrs between (1) + * and (2) and it overwrites (1) during conflict resolution, then update (2) will pick TTL of + * update (3). To have predictable TTL in such cases you should either always set it explicitly + * through {@code GridCacheEntry.timeToLive(long)} or use {@link #merge(Object, long)}. + */ + public void useNew(); + + /** + * Force cache to use neither old, nor new, but some other value passed as argument. In this case old + * value will be replaced with merge value and update will be considered as local. + * <p> + * Also in case of merge you have to specify new TTL explicitly. For unlimited TTL use {@code 0}. + * + * @param mergeVal Merge value or {@code null} to force remove. + * @param ttl Time to live in milliseconds. + */ + public void merge(@Nullable V mergeVal, long ttl); +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/540304db/modules/core/src/main/java/org/apache/ignite/cache/CacheConflictResolver.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/CacheConflictResolver.java b/modules/core/src/main/java/org/apache/ignite/cache/CacheConflictResolver.java new file mode 100644 index 0000000..58347ff --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/cache/CacheConflictResolver.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.ignite.cache; + +/** + * Cache conflict resolver. In case particular cache can be updated from multiple topologies, then + * conflict resolver will be used to determine which value to pick in case conflicting topologies update the same key + * and this conflict cannot be resolved automatically for some reason. + * <p> + * You can inject any resources in implementation of this interface. + */ +public interface CacheConflictResolver<K, V> { + /** + * Resolve conflicting key update. + * + * @param ctx Conflict resolution context. + */ + public void resolve(CacheConflictContext<K, V> ctx); +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/540304db/modules/core/src/main/java/org/apache/ignite/cache/CacheConflictResolverMode.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/CacheConflictResolverMode.java b/modules/core/src/main/java/org/apache/ignite/cache/CacheConflictResolverMode.java new file mode 100644 index 0000000..e0ed100 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/cache/CacheConflictResolverMode.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.ignite.cache; + +import org.jetbrains.annotations.*; + +/** + * Cache conflict resolver mode. + * <p> + * Each cache entry has data center ID. In case cache update is performed and either old or new cache entry has data + * center ID which differs from local data center ID defined by {@link org.apache.ignite.configuration.IgniteConfiguration#getDataCenterId()}, then + * we consider such situation as conflict. + * <p> + * If both entries participating in conflict have the same data cneter ID (i.e. they both were replicated from the same + * remote data center), then GridGain can potentially resolve this conflict automatically based on entry metadata. + * <p> + * But in case old and new entries have different data center IDs (i.e. in active-active scenario when cache is updated + * in both data centers participating in data center replication, or when cache is updated from multiple remote data + * centers), then explicit conflict resolution is required. + * <p> + * This enumeration provides several different strategies for conflict resolution. + */ +public enum CacheConflictResolverMode { + /** + * Ignite will automatically resolve conflicts when possible (i.e. when both old and new entries have the same + * data center ID). In all other situations, conflict resolution will be delegated to + * {@link CacheConflictResolver} configured through + * {@link DrReceiverCacheConfiguration#getConflictResolver()}. In case conflict resolver is not configured, + * GridGain will overwrite old entry with a new one. + * <p> + * This mode is default. + */ + AUTO, + + /** + * GridGain will always delegate to conflict resolver. This applies to all possible cases, even if both old and new + * entries have local data center ID and therefore are not considered conflicting when cache is not data center + * replication receiver. + * <p> + * In this mode {@link DrReceiverCacheConfiguration#getConflictResolver()} is mandatory. + */ + ALWAYS; + + /** Enumerated values. */ + private static final CacheConflictResolverMode[] VALS = values(); + + /** + * Efficiently gets enumerated value from its ordinal. + * + * @param ord Ordinal value. + * @return Enumerated value or {@code null} if ordinal out of range. + */ + @Nullable public static CacheConflictResolverMode fromOrdinal(int ord) { + return ord >= 0 && ord < VALS.length ? VALS[ord] : null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/540304db/modules/core/src/main/java/org/apache/ignite/cache/CacheResolveEntry.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/CacheResolveEntry.java b/modules/core/src/main/java/org/apache/ignite/cache/CacheResolveEntry.java new file mode 100644 index 0000000..f556ed4 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/cache/CacheResolveEntry.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.ignite.cache; + +import org.jetbrains.annotations.*; + +/** + * Data center replication cache entry. + */ +public interface CacheResolveEntry<K, V> { + /** + * Gets entry's key. + * + * @return Entry's key. + */ + public K key(); + + /** + * Gets entry's value. + * + * @return Entry's value. + */ + @Nullable public V value(); + + /** + * Gets entry's TTL. + * + * @return Entry's TTL. + */ + public long ttl(); + + /** + * Gets entry's expire time. + * + * @return Entry's expire time. + */ + public long expireTime(); + + /** + * Gets ID of initiator data center. + * + * @return ID of initiator data center. + */ + public byte dataCenterId(); + + /** + * Gets entry's topology version in initiator data center. + * + * @return Entry's topology version in initiator data center. + */ + public int topologyVersion(); + + /** + * Gets entry's order in initiator data center. + * + * @return Entry's order in initiator data center + */ + public long order(); + + /** + * Gets entry's global time in initiator data center. + * + * @return Entry's global time in initiator data center + */ + public long globalTime(); +}