Atomic Types
Atomics in Ignite can be read and updated from any node in the cluster.
This is a legacy Apache Ignite documentationThe new documentation is hosted here: https://ignite.apache.org/docs/latest/
Overview
Ignite supports distributed atomic long and atomic reference , similar to java.util.concurrent.atomic.AtomicLong and java.util.concurrent.atomic.AtomicReference respectively.
Atomics in Ignite are distributed across the cluster, essentially enabling performing atomic operations (such as increment-and-get or compare-and-set) with the same globally-visible value. For example, you could update the value of an atomic long on one node and read it from another node.
Features
- Retrieve current value.
- Atomically modify current value.
- Atomically increment or decrement current value.
- Atomically compare-and-set the current value to new value.
Distributed atomic long and atomic reference can be obtained via IgniteAtomicLong and IgniteAtomicReference interfaces respectively, as shown below:
Ignite ignite = Ignition.ignite();
IgniteAtomicLong atomicLong = ignite.atomicLong(
"atomicName", // Atomic long name.
0, // Initial value.
false // Create if it does not exist.
)Ignite ignite = Ignition.ignite();
// Create an AtomicReference.
IgniteAtomicReference<Boolean> ref = ignite.atomicReference(
"refName", // Reference name.
"someVal", // Initial value for atomic reference.
true // Create if it does not exist.
);Below is a usage example of IgniteAtomicLong and IgniteAtomicReference:
Ignite ignite = Ignition.ignite();
// Initialize atomic long.
final IgniteAtomicLong atomicLong = ignite.atomicLong("atomicName", 0, true);
// Increment atomic long on local node.
System.out.println("Incremented value: " + atomicLong.incrementAndGet());Ignite ignite = Ignition.ignite();
// Initialize atomic reference.
IgniteAtomicReference<String> ref = ignite.atomicReference("refName", "someVal", true);
// Compare old value to new value and if they are equal,
//only then set the old value to new value.
ref.compareAndSet("WRONG EXPECTED VALUE", "someNewVal"); // Won't change.All atomic operations provided by IgniteAtomicLong and IgniteAtomicReference are synchronous. The time an atomic operation will take depends on the number of nodes performing concurrent operations with the same instance of atomic long, the intensity of these operations, and network latency.
IgniteCacheinterface hasputIfAbsent()andreplace()methods, which provide the same CAS functionality as atomic types.
Atomic Configuration
Atomics in Ignite can be configured via atomicConfiguration property of IgniteConfiguration. The following configuration parameters can be used :
Setter Method | Description | Default |
|---|---|---|
| Set number of backups. | 0 |
| Set cache mode for all atomic types. |
|
| Sets the number of sequence values reserved for | 1000 |
Example
<bean class="org.apache.ignite.configuration.IgniteConfiguration">
...
<property name="atomicConfiguration">
<bean class="org.apache.ignite.configuration.AtomicConfiguration">
<!-- Set number of backups. -->
<property name="backups" value="1"/>
<!-- Set number of sequence values to be reserved. -->
<property name="atomicSequenceReserveSize" value="5000"/>
</bean>
</property>
</bean>AtomicConfiguration atomicCfg = new AtomicConfiguration();
// Set number of backups.
atomicCfg.setBackups(1);
// Set number of sequence values to be reserved.
atomicCfg.setAtomicSequenceReserveSize(5000);
IgniteConfiguration cfg = new IgniteConfiguration();
// Use atomic configuration in Ignite configuration.
cfg.setAtomicConfiguration(atomicCfg);
// Start Ignite node.
Ignition.start(cfg);Updated 11 months ago
