Skip to content

Commit 1b5867a

Browse files
committed
fix consensus
1 parent b9e5761 commit 1b5867a

7 files changed

Lines changed: 69 additions & 6 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ gradlew.bat
1313

1414
# log
1515
logs
16+
consensus-logs
1617

1718
# lombok
1819
out

consensus-logs/copycat-1-1.log

-1 MB
Binary file not shown.
-128 Bytes
Binary file not shown.

consensus-logs/copycat.meta

-128 Bytes
Binary file not shown.

src/main/java/org/tron/consensus/client/Client.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
import io.atomix.catalyst.transport.netty.NettyTransport;
55
import io.atomix.copycat.client.ConnectionStrategies;
66
import io.atomix.copycat.client.CopycatClient;
7+
import org.apache.kafka.clients.consumer.ConsumerRecord;
8+
import org.apache.kafka.clients.consumer.ConsumerRecords;
79
import org.tron.consensus.common.GetQuery;
810
import org.tron.consensus.common.PutCommand;
11+
import org.tron.overlay.kafka.ConsumerWorker;
912

1013
import java.util.Arrays;
1114
import java.util.Collection;
1215
import java.util.concurrent.CompletableFuture;
1316

14-
public class Client {
17+
public class Client{
1518

1619
private static CopycatClient client = null;
1720

@@ -47,10 +50,24 @@ public static void putMessage(String[] args) {
4750
}
4851

4952
public static void getMessage1(String key) {
53+
5054
client.submit(new GetQuery(key)).thenAccept(result -> {
51-
System.out.println("Consensus " + key + " is: " +
52-
result);
55+
System.out.println("Consensus " + key + " is: " + result);
5356
});
57+
// Thread thread = new Thread(() -> {
58+
// while (true) {
59+
// try {
60+
// client.submit(new GetQuery(key)).thenAccept(result -> {
61+
// System.out.println("Consensus " + key + " is: " +
62+
// result);
63+
// });
64+
// Thread.sleep(5000);
65+
// } catch (InterruptedException e) {
66+
// e.printStackTrace();
67+
// }
68+
// }
69+
// });
70+
//thread.start();
5471
}
5572
public static void getMessage(String key) {
5673
Object result = client.submit(new GetQuery(key)).join();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.tron.consensus.common;
2+
3+
import io.atomix.copycat.Operation;
4+
5+
public class Listen implements Operation {
6+
7+
}

src/main/java/org/tron/consensus/common/MapstateMachine.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
package org.tron.consensus.common;
22

3+
import io.atomix.catalyst.concurrent.Scheduled;
34
import io.atomix.copycat.server.Commit;
45
import io.atomix.copycat.server.Snapshottable;
56
import io.atomix.copycat.server.StateMachine;
7+
import io.atomix.copycat.server.session.ServerSession;
8+
import io.atomix.copycat.server.session.SessionListener;
69
import io.atomix.copycat.server.storage.snapshot.SnapshotReader;
710
import io.atomix.copycat.server.storage.snapshot.SnapshotWriter;
811

912
import java.util.HashMap;
1013
import java.util.Map;
14+
import java.util.Set;
15+
import java.util.HashSet;
1116

12-
public class MapstateMachine extends StateMachine implements Snapshottable {
17+
public class MapstateMachine extends StateMachine implements Snapshottable,SessionListener {
1318
private Map<Object, Object> map = new HashMap<>();
19+
private Set<ServerSession> sessions = new HashSet<>();
20+
private Set<ServerSession> listeners = new HashSet<>();
1421

15-
public Object put(Commit<PutCommand> commit) {
22+
public void listen(Commit<org.tron.consensus.common.Listen> commit) {
23+
listeners.add(commit.session());
24+
commit.close();
25+
}
26+
27+
public Object put(Commit<org.tron.consensus.common.PutCommand> commit) {
1628
try {
1729
map.put(commit.operation().key(), commit.operation().value());
1830
} finally {
31+
commit.session();
1932
commit.close();
2033
}
2134
return null;
2235
}
2336

24-
public Object get(Commit<GetQuery> commit) {
37+
public Object get(Commit<org.tron.consensus.common.GetQuery> commit) {
2538
try {
2639
return map.get(commit.operation().key());
2740
} finally {
@@ -38,4 +51,29 @@ public void snapshot(SnapshotWriter writer) {
3851
public void install(SnapshotReader reader) {
3952
map = reader.readObject();
4053
}
54+
55+
/* Listening for Session State Changes */
56+
// called when a new session is registered by a client
57+
@Override
58+
public void register(ServerSession session) {
59+
sessions.add(session);
60+
}
61+
62+
// called when a session is unregistered by a client
63+
@Override
64+
public void unregister(ServerSession session) {
65+
66+
}
67+
68+
// called when a session is expired by the cluster
69+
@Override
70+
public void expire(ServerSession session) {
71+
72+
}
73+
74+
// called after a session is either unregistered by a client or expired by the leader
75+
@Override
76+
public void close(ServerSession session) {
77+
sessions.remove(session);
78+
}
4179
}

0 commit comments

Comments
 (0)