-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NIFI-3214: Added fetch and replace to DistributedMapCache
- Using fetch and replace together can provide optimistic locking for concurrency control. - Added fetch to get cache entry with its meta data such as revision number. - Added replace to update cache only if it has not been updated. - Added Map Cache protocol version 2 for those new operations. - Existing operations such as get or put can work with protocol version 1.
- Loading branch information
1 parent
a794166
commit 42cf830
Showing
18 changed files
with
682 additions
and
86 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
...c/main/java/org/apache/nifi/distributed/cache/client/AtomicDistributedMapCacheClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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.nifi.distributed.cache.client; | ||
|
||
import org.apache.nifi.annotation.documentation.CapabilityDescription; | ||
import org.apache.nifi.annotation.documentation.Tags; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* <p>This interface defines an API that can be used for interacting with a | ||
* Distributed Cache that functions similarly to a {@link java.util.Map Map}. | ||
* | ||
* <p>In addition to the API defined in {@link DistributedMapCacheClient} super class, | ||
* this class provides methods for concurrent atomic updates those are added since Map Cache protocol version 2. | ||
* | ||
* <p>If a remote cache server doesn't support Map Cache protocol version 2, these methods throw UnsupportedOperationException. | ||
*/ | ||
@Tags({"distributed", "client", "cluster", "map", "cache"}) | ||
@CapabilityDescription("Provides the ability to communicate with a DistributedMapCacheServer. This allows " | ||
+ "multiple nodes to coordinate state with a single remote entity.") | ||
public interface AtomicDistributedMapCacheClient extends DistributedMapCacheClient { | ||
|
||
interface CacheEntry<K, V> { | ||
|
||
long getRevision(); | ||
|
||
K getKey(); | ||
|
||
V getValue(); | ||
|
||
} | ||
|
||
/** | ||
* Fetch a CacheEntry with a key. | ||
* @param <K> the key type | ||
* @param <V> the value type | ||
* @param key the key to lookup in the map | ||
* @param keySerializer key serializer | ||
* @param valueDeserializer value deserializer | ||
* @return A CacheEntry instance if one exists, otherwise <cod>null</cod>. | ||
* @throws IOException if unable to communicate with the remote instance | ||
*/ | ||
<K, V> CacheEntry<K, V> fetch(K key, Serializer<K> keySerializer, Deserializer<V> valueDeserializer) throws IOException; | ||
|
||
/** | ||
* Replace an existing key with new value. | ||
* @param <K> the key type | ||
* @param <V> the value type | ||
* @param key the key to replace | ||
* @param value the new value for the key | ||
* @param keySerializer key serializer | ||
* @param valueSerializer value serializer | ||
* @param revision a revision that was retrieved by a preceding fetch operation, if the key is already updated by other client, | ||
* this doesn't match with the one on server, therefore the replace operation will not be performed. | ||
* If there's no existing entry for the key, any revision can replace the key. | ||
* @return true only if the key is replaced. | ||
* @throws IOException if unable to communicate with the remote instance | ||
*/ | ||
<K, V> boolean replace(K key, V value, Serializer<K> keySerializer, Serializer<V> valueSerializer, long revision) throws IOException; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...nt-service/src/main/java/org/apache/nifi/distributed/cache/client/StandardCacheEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* 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.nifi.distributed.cache.client; | ||
|
||
public class StandardCacheEntry<K,V> implements AtomicDistributedMapCacheClient.CacheEntry<K,V> { | ||
|
||
private final K key; | ||
private final V value; | ||
private final long revision; | ||
|
||
|
||
public StandardCacheEntry(final K key, final V value, final long revision) { | ||
this.key = key; | ||
this.value = value; | ||
this.revision = revision; | ||
} | ||
|
||
@Override | ||
public long getRevision() { | ||
return revision; | ||
} | ||
|
||
@Override | ||
public K getKey() { | ||
return key; | ||
} | ||
|
||
@Override | ||
public V getValue() { | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.