Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
Conflicts:
	distributed-executor/executing-on-key-owner/src/main/java/VerifyTask.java
  • Loading branch information
alparslanavci committed Dec 31, 2015
2 parents 7c396ef + aeff8d0 commit dd50843
Show file tree
Hide file tree
Showing 190 changed files with 2,378 additions and 728 deletions.
7 changes: 2 additions & 5 deletions cluster-quorum/pom.xml
Expand Up @@ -4,7 +4,6 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>


<artifactId>cluster-quorum</artifactId>
<name>Cluster Quorum</name>
<version>0.1-SNAPSHOT</version>
Expand All @@ -19,10 +18,8 @@
<dependencies>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<artifactId>hazelcast</artifactId>
<version>${hazelcast.version}</version>
</dependency>
</dependencies>


</project>
</project>
25 changes: 25 additions & 0 deletions cluster/pom.xml
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>cluster</artifactId>
<name>Cluster</name>

<parent>
<groupId>com.hazelcast.samples</groupId>
<artifactId>code-samples</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<dependencies>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<version>${hazelcast.version}</version>
</dependency>
</dependencies>

</project>
40 changes: 40 additions & 0 deletions cluster/src/main/java/ClusterFrozenState.java
@@ -0,0 +1,40 @@
import com.hazelcast.cluster.ClusterState;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.instance.GroupProperty;

public class ClusterFrozenState {

public static void main(String[] args) {
System.setProperty(GroupProperty.PHONE_HOME_ENABLED.getName(), "false");

HazelcastInstance instance1 = Hazelcast.newHazelcastInstance();
HazelcastInstance instance2 = Hazelcast.newHazelcastInstance();

System.out.println("Instance-1 Cluster State: " + instance1.getCluster().getClusterState());
System.out.println("Instance-2 Cluster State: " + instance2.getCluster().getClusterState());

instance2.getCluster().changeClusterState(ClusterState.FROZEN);
System.out.println("Instance-1 Cluster State: " + instance1.getCluster().getClusterState());
System.out.println("Instance-2 Cluster State: " + instance2.getCluster().getClusterState());

// shutdown 1st instance and start it back
// it should re-join to the 2nd node
instance1.shutdown();
instance1 = Hazelcast.newHazelcastInstance();

System.out.println("Instance-1 Members: " + instance1.getCluster().getMembers());
System.out.println("Instance-2 Members: " + instance2.getCluster().getMembers());
System.out.println("Instance-1 Cluster State: " + instance1.getCluster().getClusterState());
System.out.println("Instance-2 Cluster State: " + instance2.getCluster().getClusterState());

// a new instance cannot join to a frozen cluster
try {
Hazelcast.newHazelcastInstance();
} catch (IllegalStateException expected) {
System.err.println("New node cannot join to the cluster: " + expected);
}

Hazelcast.shutdownAll();
}
}
46 changes: 46 additions & 0 deletions cluster/src/main/java/ClusterPassiveState.java
@@ -0,0 +1,46 @@
import com.hazelcast.cluster.ClusterState;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;

public class ClusterPassiveState {

public static void main(String[] args) {
HazelcastInstance instance1 = Hazelcast.newHazelcastInstance();
HazelcastInstance instance2 = Hazelcast.newHazelcastInstance();

System.out.println("Instance-1 Cluster State: " + instance1.getCluster().getClusterState());
System.out.println("Instance-2 Cluster State: " + instance2.getCluster().getClusterState());

IMap<Object, Object> map = instance2.getMap("test-map");
// initialize partition assignments before taking cluster to the PASSIVE state
map.size();

instance2.getCluster().changeClusterState(ClusterState.PASSIVE);
System.out.println("Instance-1 Cluster State: " + instance1.getCluster().getClusterState());
System.out.println("Instance-2 Cluster State: " + instance2.getCluster().getClusterState());

// shutdown 1st instance and start it back
// it should re-join to the 2nd node
instance1.shutdown();
instance1 = Hazelcast.newHazelcastInstance();

System.out.println("Instance-1 Members: " + instance1.getCluster().getMembers());
System.out.println("Instance-2 Members: " + instance2.getCluster().getMembers());
System.out.println("Instance-1 Cluster State: " + instance1.getCluster().getClusterState());
System.out.println("Instance-2 Cluster State: " + instance2.getCluster().getClusterState());

// readonly operations are allowed
System.out.println("map.get() = " + map.get("key"));
System.out.println("map.containsKey() = " + map.containsKey("key"));

// non-readonly operations are NOT allowed
try {
map.put("key", "value");
} catch (IllegalStateException e) {
System.err.println("Cannot put! Cluster is in PASSIVE state! -> " + e);
}

Hazelcast.shutdownAll();
}
}
19 changes: 19 additions & 0 deletions cluster/src/main/java/ClusterShutdown.java
@@ -0,0 +1,19 @@
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

public class ClusterShutdown {

public static void main(String[] args) {
final HazelcastInstance instance1 = Hazelcast.newHazelcastInstance();
final HazelcastInstance instance2 = Hazelcast.newHazelcastInstance();

System.out.println("Instance-1 members: " + instance1.getCluster().getMembers());
System.out.println("Instance-2 members: " + instance2.getCluster().getMembers());

// shutdown cluster
instance2.getCluster().shutdown();

System.out.println("Instance-1: Is running?: " + instance1.getLifecycleService().isRunning());
System.out.println("Instance-2: Is running?: " + instance2.getLifecycleService().isRunning());
}
}
@@ -1,6 +1,6 @@
<hazelcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/config
http://www.hazelcast.com/schema/config/hazelcast-config-3.2.xsd"
http://www.hazelcast.com/schema/config/hazelcast-config-3.6.xsd"
xmlns="http://www.hazelcast.com/schema/config">

<network>
Expand All @@ -13,4 +13,4 @@
<max-size>10</max-size>
</queue>

</hazelcast>
</hazelcast>
@@ -1,11 +1,9 @@
<hazelcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/config
http://www.hazelcast.com/schema/config/hazelcast-config-3.2.xsd"
http://www.hazelcast.com/schema/config/hazelcast-config-3.6.xsd"
xmlns="http://www.hazelcast.com/schema/config">

<!-- we need to configure the queue store here -->
<queue name="queue">

</queue>

</hazelcast>
</hazelcast>
@@ -1,6 +1,6 @@
<hazelcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/config
http://www.hazelcast.com/schema/config/hazelcast-config-3.5.xsd"
http://www.hazelcast.com/schema/config/hazelcast-config-3.6.xsd"
xmlns="http://www.hazelcast.com/schema/config">

<network>
Expand All @@ -14,4 +14,4 @@
<time-to-live-seconds>5</time-to-live-seconds>
</ringbuffer>

</hazelcast>
</hazelcast>
@@ -1,6 +1,7 @@
import java.io.Serializable;

public class EchoTask implements Runnable, Serializable {

private final String msg;

public EchoTask(String msg) {
Expand All @@ -11,7 +12,9 @@ public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Echo:" + msg);

System.out.println("Echo: " + msg);
}
}
Expand Up @@ -3,13 +3,15 @@
import com.hazelcast.core.IExecutorService;

public class MasterMember {

public static void main(String[] args) throws Exception {
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
IExecutorService executor = hz.getExecutorService("executor");
for (int k = 1; k <= 1; k++) {

for (int i = 1; i <= 1; i++) {
Thread.sleep(1000);
System.out.println("Producing echo task: " + k);
executor.execute(new EchoTask("" + k));
System.out.println("Producing echo task: " + i);
executor.execute(new EchoTask("" + i));
}
System.out.println("MasterMember finished!");

Expand Down
Expand Up @@ -8,16 +8,20 @@
import java.util.concurrent.Future;

public class MasterMember {

public static void main(String[] args) throws Exception {
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
Map<String, Integer> map = hz.getMap("map");
for (int k = 0; k < 5; k++)
for (int i = 0; i < 5; i++) {
map.put(UUID.randomUUID().toString(), 1);
}
IExecutorService executor = hz.getExecutorService("executor");

Map<Member, Future<Integer>> result = executor.submitToAllMembers(new SumTask());
int sum = 0;
for (Future<Integer> future : result.values())
for (Future<Integer> future : result.values()) {
sum += future.get();
}

System.out.println("Result: " + sum);
}
Expand Down
@@ -1,6 +1,7 @@
import com.hazelcast.core.Hazelcast;

public class SlaveMember {

public static void main(String[] args) {
Hazelcast.newHazelcastInstance();
}
Expand Down
Expand Up @@ -5,8 +5,8 @@
import java.io.Serializable;
import java.util.concurrent.Callable;

public class SumTask implements
Callable<Integer>, Serializable, HazelcastInstanceAware {
public class SumTask implements Callable<Integer>, Serializable, HazelcastInstanceAware {

private transient HazelcastInstance hz;

public void setHazelcastInstance(HazelcastInstance hz) {
Expand All @@ -20,7 +20,7 @@ public Integer call() throws Exception {
System.out.println("Calculating for key: " + key);
result += map.get(key);
}
System.out.println("Local Result: " + result);
System.out.println("Local result: " + result);
return result;
}
}
Expand Up @@ -6,13 +6,17 @@
import java.util.UUID;

public class MasterMember {

public static void main(String[] args) throws Exception {
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
Map<String, String> map = hz.getMap("map");
for (int k = 0; k < 10; k++)
for (int i = 0; i < 10; i++) {
map.put(UUID.randomUUID().toString(), "");
}

IExecutorService executor = hz.getExecutorService("executor");
for (String key : map.keySet())
for (String key : map.keySet()) {
executor.executeOnKeyOwner(new VerifyTask(key), key);
}
}
}
@@ -1,6 +1,7 @@
import com.hazelcast.core.Hazelcast;

public class SlaveMember {

public static void main(String[] args) {
Hazelcast.newHazelcastInstance();
}
Expand Down
Expand Up @@ -5,6 +5,7 @@
import java.io.Serializable;

public class VerifyTask implements Runnable, Serializable, HazelcastInstanceAware {

private final String key;
private transient HazelcastInstance hz;

Expand All @@ -19,6 +20,6 @@ public void setHazelcastInstance(HazelcastInstance hz) {
public void run() {
IMap map = hz.getMap("map");
boolean localKey = map.localKeySet().contains(key);
System.out.println("Key is local:" + localKey);
System.out.println("Key " + key + " is local: " + localKey);
}
}
Expand Up @@ -4,8 +4,8 @@
import java.io.Serializable;
import java.util.concurrent.Callable;

public class ComputationHeavyTask implements
Callable<Integer>, Serializable, HazelcastInstanceAware {
public class ComputationHeavyTask implements Callable<Integer>, Serializable, HazelcastInstanceAware {

private transient HazelcastInstance hz;

public void setHazelcastInstance(HazelcastInstance hz) {
Expand Down
Expand Up @@ -6,10 +6,13 @@
import java.util.concurrent.Future;

public class StartDataMember {

public static void main(String[] args) throws Exception {
final HazelcastInstance hz = Hazelcast.newHazelcastInstance();
final IExecutorService executor = hz.getExecutorService("executor");
final Future<Integer> future = executor.submit(new ComputationHeavyTask(), MemberSelectors.LITE_MEMBER_SELECTOR);
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
IExecutorService executor = hz.getExecutorService("executor");

Future<Integer> future = executor.submit(new ComputationHeavyTask(), MemberSelectors.LITE_MEMBER_SELECTOR);

System.out.println("Result: " + future.get());
}
}
Expand Up @@ -2,9 +2,11 @@
import com.hazelcast.core.Hazelcast;

public class StartLiteMember {

public static void main(String[] args) {
final Config config = new Config();
Config config = new Config();
config.setLiteMember(true);

Hazelcast.newHazelcastInstance(config);
}
}
Expand Up @@ -8,6 +8,6 @@ public EchoTask(String msg) {
}

public void run() {
System.out.println(msg);
System.out.println("Echo: " + msg);
}
}
Expand Up @@ -4,11 +4,13 @@
import com.hazelcast.core.Member;

public class MasterMember {

public static void main(String[] args) {
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
IExecutorService executorService = hz.getExecutorService("executor");

for (Member member : hz.getCluster().getMembers()) {
EchoTask task = new EchoTask("echo" + member.getSocketAddress());
EchoTask task = new EchoTask(member.getSocketAddress().toString());
executorService.executeOnMember(task, member);
}
}
Expand Down

0 comments on commit dd50843

Please sign in to comment.