Skip to content

Commit

Permalink
Merge pull request hazelcast#628 from hazelcast/update-cp-samples-to-ee
Browse files Browse the repository at this point in the history
Updated CP samples to EE [HZ-4480]
- changed terminate() method on shutdown() to prevent last member hanging issue
  • Loading branch information
arodionov authored Apr 8, 2024
2 parents db2a8c5 + a34a02f commit c1209a1
Show file tree
Hide file tree
Showing 25 changed files with 154 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.cp.IAtomicLong;

import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;
import static java.util.concurrent.TimeUnit.SECONDS;

/**
* Configures a CP subsystem of 3 CP members. When you run 3 instances of this
* class, it will form the CP subsystem. Then, it fetches
* a CP {@link IAtomicLong} proxy and increments the counter value
* {@link #NUMBER_OF_INCREMENTS} times.
*
* You have to set your Hazelcast Enterprise license key to make this code sample work.
* Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
*/
public class CPMember {

Expand All @@ -20,6 +24,7 @@ public class CPMember {

public static void main(String[] args) throws InterruptedException {
Config config = new Config();
config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
config.getCPSubsystemConfig().setCPMemberCount(CP_MEMBER_COUNT);
HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
IAtomicLong counter = hz.getCPSubsystem().getAtomicLong("counter");
Expand All @@ -43,6 +48,6 @@ public static void main(String[] args) throws InterruptedException {
// always destroy CP Subsystem data structures otherwise it can lead to a memory leak
counter.destroy();

hz.getLifecycleService().terminate();
hz.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.cp.IAtomicReference;

import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;
import static java.util.concurrent.TimeUnit.SECONDS;

/**
* Configures a CP subsystem of 3 CP members. When you run 3 instances of this
* class, it will form the CP subsystem. Then, it fetches
* a CP {@link IAtomicReference} proxy and increments the integer value with
* CAS {@link #NUMBER_OF_INCREMENTS} times.
*
* You have to set your Hazelcast Enterprise license key to make this code sample work.
* Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
*/
public class CPMember {

Expand All @@ -20,6 +24,7 @@ public class CPMember {

public static void main(String[] args) throws InterruptedException {
Config config = new Config();
config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
config.getCPSubsystemConfig().setCPMemberCount(CP_MEMBER_COUNT);
HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
IAtomicReference<Integer> ref = hz.getCPSubsystem().getAtomicReference("reference");
Expand Down Expand Up @@ -47,6 +52,6 @@ public static void main(String[] args) throws InterruptedException {
// always destroy CP Subsystem data structures otherwise it can lead to a memory leak
ref.destroy();

hz.getLifecycleService().terminate();
hz.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.cp.ICountDownLatch;

import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;
import static java.util.concurrent.TimeUnit.SECONDS;

/**
Expand All @@ -13,13 +14,17 @@
* a CP {@link ICountDownLatch} proxy. Each CP member performs some dummy work
* and decrements the count value. After that, it waits for all CP members to
* complete their work.
*
* You have to set your Hazelcast Enterprise license key to make this code sample work.
* Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
*/
public class CPMember {

private static final int CP_MEMBER_COUNT = 3;

public static void main(String[] args) throws Exception {
Config config = new Config();
config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
config.getCPSubsystemConfig().setCPMemberCount(CP_MEMBER_COUNT);
HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
ICountDownLatch latch = hz.getCPSubsystem().getCountDownLatch("latch");
Expand Down Expand Up @@ -51,6 +56,6 @@ public static void main(String[] args) throws Exception {
// always destroy CP Subsystem data structures otherwise it can lead to a memory leak
latch.destroy();

hz.getLifecycleService().terminate();
hz.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;

/**
* Configures a CP subsystem of 3 CP members. When you run 3 instances of this
* class, it will form the CP subsystem.
Expand All @@ -13,13 +15,17 @@
* <p>
* If you terminate one more process, after some time the remaining member
* will print CP group majority lost event to the console.
*
* You have to set your Hazelcast Enterprise license key to make this code sample work.
* Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
*/
public class CPMember {

private static final int CP_MEMBER_COUNT = 3;

public static void main(String[] args) {
Config config = new Config();
config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
config.getCPSubsystemConfig().setCPMemberCount(CP_MEMBER_COUNT);

HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;

/**
* Configures a CP subsystem of 3 CP members. When you run 3 instances of this
* class, it will form the CP subsystem. After leader election is completed
Expand All @@ -12,6 +14,9 @@
* If you terminate one of the processes, after some time that CP member will be
* automatically removed from CP subsystem and remaining members will print
* the removed CP member to the console.
*
* You have to set your Hazelcast Enterprise license key to make this code sample work.
* Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
*/
public class CPMember {

Expand All @@ -20,6 +25,7 @@ public class CPMember {

public static void main(String[] args) {
Config config = new Config();
config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
config.getCPSubsystemConfig()
.setCPMemberCount(CP_MEMBER_COUNT)
.setSessionTimeToLiveSeconds(AUTO_REMOVE_MISSING_MEMBER_SEC)
Expand Down
8 changes: 4 additions & 4 deletions enterprise/cp-map/pom.xml → cp-subsystem/cp-map/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<packaging>jar</packaging>

<artifactId>cp-map</artifactId>
<name>Enterprise - CP Subsystem CPMap</name>
<name>Enterprise Advanced - CP Subsystem CPMap</name>

<description>
Enterprise - CP Subsystem CPMap
Enterprise Advanced - CP Subsystem CPMap
</description>

<parent>
<groupId>com.hazelcast.samples.enterprise</groupId>
<artifactId>enterprise</artifactId>
<groupId>com.hazelcast.samples.cp</groupId>
<artifactId>cp-subsystem</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.cp.CPMap;

import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;
import static java.util.concurrent.TimeUnit.SECONDS;

/**
* Configures a CP subsystem of 3 CP members. When you run 3 instances of this
* class, it will form the CP subsystem. Then, it fetches
* a CP {@link CPMap} proxy and increments the integer value with
* CAS {@link #NUMBER_OF_INCREMENTS} times.
*
* You have to set your Hazelcast Enterprise license key to make this code sample work.
* Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
*/
public class CPMember {

Expand All @@ -20,6 +24,7 @@ public class CPMember {

public static void main(String[] args) throws InterruptedException {
Config config = new Config();
config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
config.getCPSubsystemConfig().setCPMemberCount(CP_MEMBER_COUNT);
HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
CPMap<String, Integer> cpMap = hz.getCPSubsystem().getMap("cp-map");
Expand Down Expand Up @@ -55,6 +60,6 @@ public static void main(String[] args) throws InterruptedException {
// always destroy CP Subsystem data structures otherwise it can lead to a memory leak
cpMap.destroy();

hz.getLifecycleService().terminate();
hz.shutdown();
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Date;
import java.util.Random;

import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;
import static java.util.concurrent.TimeUnit.SECONDS;

/**
Expand All @@ -18,6 +19,9 @@
* {@link #NUMBER_OF_ROUNDS} times. At any time, {@code CP_MEMBER_COUNT - 1}
* CP members will be holding a permit and the last member will be waiting for
* one of them to release a permit.
*
* You have to set your Hazelcast Enterprise license key to make this code sample work.
* Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
*/
public class CPMember {

Expand All @@ -26,6 +30,7 @@ public class CPMember {

public static void main(String[] args) throws Exception {
Config config = new Config();
config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
config.getCPSubsystemConfig().setCPMemberCount(CP_MEMBER_COUNT);
HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
ISemaphore semaphore = hz.getCPSubsystem().getSemaphore("semaphore");
Expand All @@ -51,7 +56,7 @@ public static void main(String[] args) throws Exception {
// always destroy CP Subsystem data structures otherwise it can lead to a memory leak
semaphore.destroy();

hz.getLifecycleService().terminate();
hz.shutdown();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@

import java.util.concurrent.TimeUnit;

import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;

/**
* This code sample demonstrates that a FencedLock is eventually released when
* its holder does not commit CP session heartbeats.
*
* You have to set your Hazelcast Enterprise license key to make this code sample work.
* Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
*/
public class AutoReleaseFencedLockOnSessionClose {

public static void main(String[] args) throws InterruptedException {
Config config = new Config();
config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
CPSubsystemConfig cpSubsystemConfig = config.getCPSubsystemConfig();
cpSubsystemConfig.setCPMemberCount(3);
cpSubsystemConfig.setSessionHeartbeatIntervalSeconds(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.cp.lock.FencedLock;

import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;

/**
* This code sample demonstrates that a Hazelcast instance releases the lock
* instances it holds during shutdown.
*
* You have to set your Hazelcast Enterprise license key to make this code sample work.
* Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
*/
public class AutoReleaseFencedLockOnShutdown {

public static void main(String[] args) {
Config config = new Config();
config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
CPSubsystemConfig cpSubsystemConfig = config.getCPSubsystemConfig();
cpSubsystemConfig.setCPMemberCount(3);
HazelcastInstance hz1 = Hazelcast.newHazelcastInstance(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@

import java.util.concurrent.TimeUnit;

import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;

/**
* This code sample demonstrates basic usage of the locking methods available
* in the {@link FencedLock} interface
*
* You have to set your Hazelcast Enterprise license key to make this code sample work.
* Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
*/
public class FencedLockBasicUsage {

public static void main(String[] args) {
Config config = new Config();
config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
config.getCPSubsystemConfig().setCPMemberCount(3);
HazelcastInstance hz1 = Hazelcast.newHazelcastInstance(config);
HazelcastInstance hz2 = Hazelcast.newHazelcastInstance(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@
import java.util.Collection;
import java.util.concurrent.ExecutionException;

import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;

/**
* This code sample demonstrates that a FencedLock can be released when
* the CP session of its current holder is closed via the API. We can use this
* API when we know for sure that the current lock holder is crashed.
*
* You have to set your Hazelcast Enterprise license key to make this code sample work.
* Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
*/
public class ForceReleaseFencedByClosingSession {

public static void main(String[] args) throws InterruptedException, ExecutionException {
Config config = new Config();
config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
CPSubsystemConfig cpSubsystemConfig = config.getCPSubsystemConfig();
cpSubsystemConfig.setCPMemberCount(3);
HazelcastInstance hz1 = Hazelcast.newHazelcastInstance(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@
import java.util.Collection;
import java.util.concurrent.ExecutionException;

import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;

/**
* This code sample demonstrates failure of a reentrant lock() call
* after CP session of the lock holder is closed in the CP group.
*
* You have to set your Hazelcast Enterprise license key to make this code sample work.
* Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
*/
public class FailWhenLockOwnershipIsLost {

public static void main(String[] args) throws InterruptedException, ExecutionException {
Config config = new Config();
config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
CPSubsystemConfig cpSubsystemConfig = config.getCPSubsystemConfig();
cpSubsystemConfig.setCPMemberCount(3);
HazelcastInstance hz1 = Hazelcast.newHazelcastInstance(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.cp.lock.FencedLock;

import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;

/**
* This code sample demonstrates how fencing tokens returned by the FencedLock
* increases every time the lock switches from the available state to the held
* state.
*
* You have to set your Hazelcast Enterprise license key to make this code sample work.
* Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
*/
public class MonotonicFencingTokens {

public static void main(String[] args) {
Config config = new Config();
config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
CPSubsystemConfig cpSubsystemConfig = config.getCPSubsystemConfig();
cpSubsystemConfig.setCPMemberCount(3);
HazelcastInstance hz1 = Hazelcast.newHazelcastInstance(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@
import com.hazelcast.cp.lock.FencedLock;
import com.hazelcast.cp.lock.exception.LockAcquireLimitReachedException;

import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;

/**
* This code sample demonstrates how a FencedLock can be used
* as a non-reentrant mutex.
*
* You have to set your Hazelcast Enterprise license key to make this code sample work.
* Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
*/
public class NonReentrantMutex {

public static void main(String[] args) {
FencedLockConfig lockConfig = new FencedLockConfig("my-lock");
lockConfig.disableReentrancy();
Config config = new Config();
config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
config.getCPSubsystemConfig().setCPMemberCount(3);
config.getCPSubsystemConfig().addLockConfig(lockConfig);
HazelcastInstance hz1 = Hazelcast.newHazelcastInstance(config);
Expand Down
Loading

0 comments on commit c1209a1

Please sign in to comment.